From 7f31c41e3a434033a4ce47a97dd1cc32ccb4d58b Mon Sep 17 00:00:00 2001 From: Morten Tokle Date: Thu, 3 Oct 2019 10:29:46 +0200 Subject: Read signature algorithm from key --- .../java/com/yahoo/security/SignatureUtils.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'security-utils/src') 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()); + } + } } -- cgit v1.2.3