-
Notifications
You must be signed in to change notification settings - Fork 12
/
Crypto.java
executable file
·36 lines (33 loc) · 1.15 KB
/
Crypto.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
public class Crypto {
/**
* @return true is {@code signature} is a valid digital signature of {@code message} under the
* key {@code pubKey}. Internally, this uses RSA signature, but the student does not
* have to deal with any of the implementation details of the specific signature
* algorithm
*/
public static boolean verifySignature(PublicKey pubKey, byte[] message, byte[] signature) {
Signature sig = null;
try {
sig = Signature.getInstance("SHA256withRSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sig.initVerify(pubKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
try {
sig.update(message);
return sig.verify(signature);
} catch (SignatureException e) {
e.printStackTrace();
}
return false;
}
}