summaryrefslogtreecommitdiffstats
path: root/security-utils
diff options
context:
space:
mode:
authorMorten Tokle <mortent@verizonmedia.com>2019-10-03 10:29:46 +0200
committerMorten Tokle <mortent@verizonmedia.com>2019-10-03 10:32:39 +0200
commit7f31c41e3a434033a4ce47a97dd1cc32ccb4d58b (patch)
treefa1486520c2cc0580992e7016baff30b16845b14 /security-utils
parent42813a5f158973444253db38f006d25e62dd66cd (diff)
Read signature algorithm from key
Diffstat (limited to 'security-utils')
-rw-r--r--security-utils/src/main/java/com/yahoo/security/SignatureUtils.java23
1 files changed, 23 insertions, 0 deletions
diff --git a/security-utils/src/main/java/com/yahoo/security/SignatureUtils.java b/security-utils/src/main/java/com/yahoo/security/SignatureUtils.java
index 7560fbbd40d..13bc140d797 100644
--- a/security-utils/src/main/java/com/yahoo/security/SignatureUtils.java
+++ b/security-utils/src/main/java/com/yahoo/security/SignatureUtils.java
@@ -2,6 +2,7 @@
package com.yahoo.security;
import java.security.GeneralSecurityException;
+import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
@@ -24,6 +25,11 @@ public class SignatureUtils {
}
}
+ /** Returns a signature instance which computes a hash of its content, before signing with the given private key. */
+ public static Signature createSigner(PrivateKey key) {
+ return createSigner(key, getSignatureAlgorithm(key));
+ }
+
/** Returns a signature instance which computes a hash of its content, before verifying with the given public key. */
public static Signature createVerifier(PublicKey key, SignatureAlgorithm algorithm) {
try {
@@ -34,4 +40,21 @@ public class SignatureUtils {
throw new IllegalStateException(e);
}
}
+
+ /** Returns a signature instance which computes a hash of its content, before verifying with the given public key. */
+ public static Signature createVerifier(PublicKey key) {
+ return createVerifier(key, getSignatureAlgorithm(key));
+ }
+
+ /* Returns a signature algorithm supported by the key based on SHA512 */
+ private static SignatureAlgorithm getSignatureAlgorithm(Key key) {
+ switch (key.getAlgorithm()) {
+ case "EC":
+ return SignatureAlgorithm.SHA512_WITH_ECDSA;
+ case "RSA":
+ return SignatureAlgorithm.SHA512_WITH_RSA;
+ default:
+ throw new RuntimeException("Unknown Key algorithm " + key.getAlgorithm());
+ }
+ }
}