digest
acd7be03019c5b0791efed326c77937cc72b82ab0f4ce152b9beb8c8595d2a13
signature
3044022078351d9817d3f4f449a7ebeeab8f1be11513c89a0e25d71828e7ff6cab25000d02201836ca3f071a7ea66a84a398763db0c9426bff38adc57d423b8b11b6219c38a8
maven dependency
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
public class ECSDASign {
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
PublicKey pubk = getPemPublicKey("/Users/zhouchunchen/Desktop/pub.pem","EC");
byte digest[] = Hex.decode("acd7be03019c5b0791efed326c77937cc72b82ab0f4ce152b9beb8c8595d2a13");
byte signature[] = Hex.decode("3044022078351d9817d3f4f449a7ebeeab8f1be11513c89a0e25d71828e7ff6cab25000d02201836ca3f071a7ea66a84a398763db0c9426bff38adc57d423b8b11b6219c38a8");
verify(digest, signature, pubk);
}
public static PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
String b64pk = FileUtils.readFileToString(new File(filename), "UTF-8");
b64pk = b64pk.replace("-----BEGIN PUBLIC KEY-----\n", "");
b64pk = b64pk.replace("-----END PUBLIC KEY-----", "");
Base64 b64 = new Base64();
byte [] decoded = b64.decode(b64pk);
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
private static void verify(byte[] digest, byte[] signature, PublicKey publicKey )
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException, NoSuchProviderException {
final Signature sig = Signature.getInstance("SHA256withECDSA", "BC");
sig.initVerify( publicKey );
sig.update(digest);
System.out.println(sig.verify( signature ));
}
}