-
Notifications
You must be signed in to change notification settings - Fork 925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instantiate RSA/EC Algorithm with both keys #147
Conversation
I thinks this is greater.Please merge soon,I can't wait to use it now 👏 |
public static Algorithm RSA256(RSAKey key) throws IllegalArgumentException { | ||
return new RSAAlgorithm("RS256", "SHA256withRSA", key); | ||
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we are force casting we should use the same type for the instanceof
public static Algorithm RSA384(RSAKey key) throws IllegalArgumentException { | ||
return new RSAAlgorithm("RS384", "SHA384withRSA", key); | ||
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null; | ||
RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey) key : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use same type for instanceof
and force casting
public static Algorithm RSA512(RSAKey key) throws IllegalArgumentException { | ||
return new RSAAlgorithm("RS512", "SHA512withRSA", key); | ||
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null; | ||
RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey) key : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
public static Algorithm ECDSA256(ECKey key) throws IllegalArgumentException { | ||
return new ECDSAAlgorithm("ES256", "SHA256withECDSA", 32, key); | ||
ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey) key : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
public static Algorithm ECDSA384(ECKey key) throws IllegalArgumentException { | ||
return new ECDSAAlgorithm("ES384", "SHA384withECDSA", 48, key); | ||
ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey) key : null; | ||
ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey) key : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
if (key == null) { | ||
throw new IllegalArgumentException("The ECKey cannot be null"); | ||
if (publicKey == null && privateKey == null) { | ||
throw new IllegalArgumentException("Both provided Keys cannot be null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think IllegalStateException
is better, since the keys are not arguments of this method, they are state of the instance.
if (!(key instanceof ECPublicKey)) { | ||
throw new IllegalArgumentException("The given ECKey is not an ECPublicKey."); | ||
if (publicKey == null) { | ||
throw new IllegalArgumentException("The given Public Key is null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IllegalStateException
if (!(key instanceof ECPrivateKey)) { | ||
throw new IllegalArgumentException("The given ECKey is not a ECPrivateKey."); | ||
if (privateKey == null) { | ||
throw new IllegalArgumentException("The given Private Key is null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IllegalStateException
if (!(key instanceof PublicKey)) { | ||
throw new IllegalArgumentException("The given RSAKey is not a RSAPublicKey."); | ||
if (publicKey == null) { | ||
throw new IllegalArgumentException("The given Public Key is null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IllegalStateException
if (!(key instanceof PrivateKey)) { | ||
throw new IllegalArgumentException("The given RSAKey is not a RSAPrivateKey."); | ||
if (privateKey == null) { | ||
throw new IllegalArgumentException("The given Private Key is null."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IllegalStateException
8dfb783
to
c4f2094
Compare
Before this PR, the
Algorithm
instance received one key (public or private). With the introduced changes, theAlgorithm
is now instantiated with both keys at the same time. So the user can define the instance once and reuse it as needed for both signing and verifying purposes.IllegalArgumentException
is thrown upon instantiation.verify
,IllegalStateException
is thrown.sign
,IllegalStateException
is thrown.