Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Last active December 1, 2025 17:14
Show Gist options
  • Select an option

  • Save shaunlebron/c9012c399e607e5301af11871e522047 to your computer and use it in GitHub Desktop.

Select an option

Save shaunlebron/c9012c399e607e5301af11871e522047 to your computer and use it in GitHub Desktop.
Kalshi signing in Java
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.Security;
import java.security.Signature;
import java.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
// include the bottom code in your classes as needed
if (Security.getProvider("BC") == null) {
Security.addProvider(new BouncyCastleProvider());
}
PrivateKey loadPrivateKey(String filepath) {
// required for converting PKCS1 private keys provided by kalshi to PKCS8 required by java
var pemParser = new PEMParser(new FileReader(filepath, StandardCharsets.UTF_8));
var converter = new JcaPEMKeyConverter().setProvider("BC");
var object = pemParser.readObject();
return converter.getKeyPair((PEMKeyPair) object).getPrivate();
}
String signTextWithPrivateKey(String text, PrivateKey privateKey) {
var signer = Signature.getInstance("SHA256withRSAandMGF1", "BC"); // same as "SHA256withRSA/PSS"
signer.initSign(privateKey);
signer.update(text.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(signer.sign());
}
@Abe21-dev
Copy link

cool thank you very much for making this but what did you read that gave you the knowledge to do this without referencing another solution

@shaunlebron
Copy link
Author

@Abe21-dev I had trouble finding solutions on google, so I think I used ChatGPT to hone in on the relevant API calls, then searched for examples to verify their usage, and read docs, etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment