Package ca.corbett.extras.crypt
Class SignatureUtil
java.lang.Object
ca.corbett.extras.crypt.SignatureUtil
This utility class provides convenient wrappers for creating public/private key pairs,
using a private key to sign a file, and using a public key to verify a file signature.
How do I sign a file? - You generate a key pair, save the public/private keys, and then use the private key to sign the file.
KeyPair keyPair = SignatureUtil.generateKeyPair(); SignatureUtil.saveKeyPair(keyPair, privateKeyFile, publicKeyFile); // keep the private key safe! // Generate a signature and save it to "signatureFile": SignatureUtil.signFile(dataFile, keyPair.getPrivate(), signatureFile);
You can bundle signatureFile and your publicKeyFile together with the dataFile that you wish to transmit. The receiver can use the public key to verify the signature and confirm that dataFile has not been modified since it was signed:
PublicKey publicKey = SignatureUtil.loadPublicKey(publicKeyFile);
boolean isValid = SignatureUtil.verifyFile(dataFile, signatureFile, publicKey);
if (! isValid) {
throw new Exception("The signature is wrong! The file has been modified!");
}
Note: there is a generateKeyPair method in this class as a convenience. It is perfectly compatible with the following manual generation approach:
# Generate key pair ssh-keygen -t rsa -b 2048 -m PEM -f mykey -N "" # Convert private key to PKCS#8 openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in mykey -out mykey.pkcs8 # Convert public key to X.509 PEM ssh-keygen -f mykey -e -m PEM | openssl rsa -pubin -RSAPublicKey_in -pubout -out mykey_pub.pem
Java is very particular about key formats, and it doesn't seem to like the usual openssh format. So, we use PKCS#8 in PEM format (this is what you get out of the box with the java.security classes).
- Since:
- swing-extras 2.5
- Author:
- scorbo2 with help from claude.ai
-
Method Summary
Modifier and TypeMethodDescriptionstatic KeyPairGenerates and returns a KeyPair using default settings of RSA and size 2048.static PrivateKeyloadPrivateKey(File privateKeyFile) Load a private key from a PEM file (PKCS#8 format)static PublicKeyloadPublicKey(File publicKeyFile) Load a public key from a PEM file (X.509 format)static byte[]loadSignature(File signatureFile) Loads a signature from a PEM filestatic voidsaveKeyPair(KeyPair keyPair, File privateKeyFile, File publicKeyFile) Saves the given KeyPair in PEM format to the given private and public files.static voidsavePrivateKey(PrivateKey privateKey, File privateKeyFile) Save a private key to a PEM file (PKCS#8 format)static voidsavePublicKey(PublicKey publicKey, File publicKeyFile) Saves a public key to a PEM file (X.509 format)static voidsaveSignature(byte[] signatureData, File signatureFile) Save a signature to a PEM filestatic byte[]signFile(File file, PrivateKey privateKey) Signs the given file using the given PrivateKey.static voidsignFile(File file, PrivateKey privateKey, File signatureFile) Signs the given file and saves the resulting signature to the given output file.static booleanverifyFile(File file, byte[] signatureBytes, PublicKey publicKey) Verify a file signature using the given public key.static booleanverifyFile(File file, File signatureFile, PublicKey publicKey) Verify a file signature using the given public key and the signature data from the given signature file.
-
Method Details
-
signFile
public static byte[] signFile(File file, PrivateKey privateKey) throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException Signs the given file using the given PrivateKey.- Parameters:
file- The file to be signed. Must exist.privateKey- The PrivateKey to use for signing.- Returns:
- The raw byte array of the
- Throws:
IOExceptionNoSuchAlgorithmExceptionInvalidKeyExceptionSignatureException
-
signFile
public static void signFile(File file, PrivateKey privateKey, File signatureFile) throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException Signs the given file and saves the resulting signature to the given output file.- Parameters:
file- The file to sign. Must exist.privateKey- The PrivateKey to use for signing.signatureFile- The file to which we'll encode and save the resulting signature data.- Throws:
IOExceptionNoSuchAlgorithmExceptionInvalidKeyExceptionSignatureException
-
verifyFile
public static boolean verifyFile(File file, byte[] signatureBytes, PublicKey publicKey) throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException Verify a file signature using the given public key.- Parameters:
file- The file to be verified. Must exist.signatureBytes- The raw signature data.publicKey- The PublicKey to use for verification.- Returns:
- true if signature is valid, false otherwise
- Throws:
IOExceptionNoSuchAlgorithmExceptionInvalidKeyExceptionSignatureException
-
verifyFile
public static boolean verifyFile(File file, File signatureFile, PublicKey publicKey) throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException Verify a file signature using the given public key and the signature data from the given signature file. -
loadSignature
Loads a signature from a PEM file- Throws:
IOException
-
saveSignature
Save a signature to a PEM file- Throws:
IOException
-
generateKeyPair
Generates and returns a KeyPair using default settings of RSA and size 2048.- Throws:
NoSuchAlgorithmException
-
saveKeyPair
public static void saveKeyPair(KeyPair keyPair, File privateKeyFile, File publicKeyFile) throws IOException Saves the given KeyPair in PEM format to the given private and public files.- Parameters:
keyPair- The KeyPair to be saved.privateKeyFile- The destination file for the private key (must be writable).publicKeyFile- The destination file for the public key (must be writable).- Throws:
IOException- If either file can't be saved.
-
loadPrivateKey
public static PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException Load a private key from a PEM file (PKCS#8 format) -
savePrivateKey
Save a private key to a PEM file (PKCS#8 format)- Throws:
IOException
-
loadPublicKey
public static PublicKey loadPublicKey(File publicKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException Load a public key from a PEM file (X.509 format) -
savePublicKey
Saves a public key to a PEM file (X.509 format)- Throws:
IOException
-