|  | 
|  | 1 | +package by.andd3dfx.digitalsignature; | 
|  | 2 | + | 
|  | 3 | +import java.io.BufferedInputStream; | 
|  | 4 | +import java.io.FileInputStream; | 
|  | 5 | +import java.io.FileOutputStream; | 
|  | 6 | +import java.io.IOException; | 
|  | 7 | +import java.math.BigInteger; | 
|  | 8 | +import java.security.*; | 
|  | 9 | +import java.security.spec.PKCS8EncodedKeySpec; | 
|  | 10 | +import java.security.spec.X509EncodedKeySpec; | 
|  | 11 | + | 
|  | 12 | +public class DigitalSignatureUtil { | 
|  | 13 | + | 
|  | 14 | +    /** | 
|  | 15 | +     * According to documentation - https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator | 
|  | 16 | +     * possible cases for algorithm are next: DiffieHellman, DSA, RSA, EC | 
|  | 17 | +     */ | 
|  | 18 | +    public KeyPair generateKeysPair(String algorithm, int keysize) throws Exception { | 
|  | 19 | +        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algorithm); | 
|  | 20 | +        SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); | 
|  | 21 | +        keyGenerator.initialize(keysize, random); | 
|  | 22 | +        return keyGenerator.generateKeyPair(); | 
|  | 23 | +    } | 
|  | 24 | + | 
|  | 25 | +    /** | 
|  | 26 | +     * Store private & public keys from keys pair to files | 
|  | 27 | +     */ | 
|  | 28 | +    public void storeKeysToFiles(KeyPair keyPair, String privateKeyFilename, String publicKeyFilename) throws IOException { | 
|  | 29 | +        PrivateKey privateKey = keyPair.getPrivate(); | 
|  | 30 | +        PublicKey publicKey = keyPair.getPublic(); | 
|  | 31 | + | 
|  | 32 | +        saveToFile(privateKeyFilename, privateKey.getEncoded()); | 
|  | 33 | +        saveToFile(publicKeyFilename, publicKey.getEncoded()); | 
|  | 34 | +    } | 
|  | 35 | + | 
|  | 36 | +    private void saveToFile(String filename, byte[] data) throws IOException { | 
|  | 37 | +        FileOutputStream publicKeyFileOutputStream = new FileOutputStream(filename); | 
|  | 38 | +        publicKeyFileOutputStream.write(data); | 
|  | 39 | +        publicKeyFileOutputStream.close(); | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    private byte[] loadFromFile(String filename) throws IOException { | 
|  | 43 | +        FileInputStream fileInputStream = new FileInputStream(filename); | 
|  | 44 | +        byte[] result = new byte[fileInputStream.available()]; | 
|  | 45 | +        fileInputStream.read(result); | 
|  | 46 | +        fileInputStream.close(); | 
|  | 47 | +        return result; | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    public PrivateKey loadPrivateKeyFromFile(String algorithm, String privateKeyFilename) throws Exception { | 
|  | 51 | +        byte[] encKey = loadFromFile(privateKeyFilename); | 
|  | 52 | +        PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(encKey); | 
|  | 53 | + | 
|  | 54 | +        KeyFactory keyFactory = KeyFactory.getInstance(algorithm); | 
|  | 55 | +        return keyFactory.generatePrivate(encodedKeySpec); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    public PublicKey loadPublicKeyFromFile(String algorithm, String publicKeyFilename) throws Exception { | 
|  | 59 | +        byte[] encKey = loadFromFile(publicKeyFilename); | 
|  | 60 | +        X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(encKey); | 
|  | 61 | + | 
|  | 62 | +        KeyFactory keyFactory = KeyFactory.getInstance(algorithm); | 
|  | 63 | +        return keyFactory.generatePublic(encodedKeySpec); | 
|  | 64 | +    } | 
|  | 65 | + | 
|  | 66 | +    /** | 
|  | 67 | +     * Sign string with definite algorithm and privateKey. | 
|  | 68 | +     * Usage: signString("SHA256withECDSA", privateKey, "Some string ...") | 
|  | 69 | +     */ | 
|  | 70 | +    public byte[] signString(String algorithm, PrivateKey privateKey, String stringTiSign) throws Exception { | 
|  | 71 | +        Signature dsa = Signature.getInstance(algorithm); | 
|  | 72 | +        dsa.initSign(privateKey); | 
|  | 73 | + | 
|  | 74 | +        byte[] strByte = stringTiSign.getBytes("UTF-8"); | 
|  | 75 | +        dsa.update(strByte); | 
|  | 76 | + | 
|  | 77 | +        return dsa.sign(); | 
|  | 78 | +    } | 
|  | 79 | + | 
|  | 80 | +    /** | 
|  | 81 | +     * Sign file with definite algorithm and privateKey. | 
|  | 82 | +     * Usage: signFile("SHA256withECDSA", privateKey, "d:/some-filename.txt") | 
|  | 83 | +     */ | 
|  | 84 | +    public byte[] signFile(String algorithm, PrivateKey privateKey, String nameOfFileToSign) throws Exception { | 
|  | 85 | +        Signature dsa = Signature.getInstance(algorithm); | 
|  | 86 | +        dsa.initSign(privateKey); | 
|  | 87 | + | 
|  | 88 | +        FileInputStream fis = new FileInputStream(nameOfFileToSign); | 
|  | 89 | +        BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); | 
|  | 90 | +        byte[] buffer = new byte[1024]; | 
|  | 91 | + | 
|  | 92 | +        int len; | 
|  | 93 | +        while ((len = bufferedInputStream.read(buffer)) >= 0) { | 
|  | 94 | +            dsa.update(buffer, 0, len); | 
|  | 95 | +        } | 
|  | 96 | +        bufferedInputStream.close(); | 
|  | 97 | +        return dsa.sign(); | 
|  | 98 | +    } | 
|  | 99 | + | 
|  | 100 | +    public String convertBytesArrayToString(byte[] signature) { | 
|  | 101 | +        return new BigInteger(1, signature).toString(16); | 
|  | 102 | +    } | 
|  | 103 | + | 
|  | 104 | +    public boolean verifyStringSignature(String algorithm, PublicKey publicKey, String signedString, byte[] signature) throws Exception { | 
|  | 105 | +        Signature sig = Signature.getInstance(algorithm); | 
|  | 106 | +        sig.initVerify(publicKey); | 
|  | 107 | +        sig.update(signedString.getBytes("UTF-8")); | 
|  | 108 | +        return sig.verify(signature); | 
|  | 109 | +    } | 
|  | 110 | + | 
|  | 111 | +    public boolean verifyFileSignature(String algorithm, PublicKey publicKey, String signedFileName, byte[] signatureToVerify) throws Exception { | 
|  | 112 | +        FileInputStream fileInputStream = new FileInputStream(signedFileName); | 
|  | 113 | +        byte[] bytes = new byte[fileInputStream.available()]; | 
|  | 114 | +        fileInputStream.read(bytes); | 
|  | 115 | +        fileInputStream.close(); | 
|  | 116 | + | 
|  | 117 | +        Signature sig = Signature.getInstance(algorithm); | 
|  | 118 | +        sig.initVerify(publicKey); | 
|  | 119 | +        sig.update(bytes); | 
|  | 120 | +        return sig.verify(signatureToVerify); | 
|  | 121 | +    } | 
|  | 122 | +} | 
0 commit comments