mirror of
https://github.com/nsacyber/HIRS.git
synced 2025-04-08 11:54:27 +00:00
Added length to DigestAlgorithm enum values.
This commit is contained in:
parent
2e767994ff
commit
7ab8d11dbb
@ -64,39 +64,8 @@ public abstract class AbstractDigest {
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
|
||||
switch (algorithm) {
|
||||
case MD2:
|
||||
if (digest.length != MD2_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
case MD5:
|
||||
if (digest.length != MD5_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
case SHA1:
|
||||
if (digest.length != SHA1_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
case SHA256:
|
||||
if (digest.length != SHA256_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
case SHA384:
|
||||
if (digest.length != SHA384_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
case SHA512:
|
||||
if (digest.length != SHA512_DIGEST_LENGTH) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Digest length does not match algorithm type");
|
||||
if (digest.length != algorithm.getLengthInBytes()) {
|
||||
throw new AbstractDigest.IllegalDigestLength(algorithm, digest);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,38 +9,42 @@ public enum DigestAlgorithm {
|
||||
/**
|
||||
* MD2 digest algorithm.
|
||||
*/
|
||||
MD2("MD2"),
|
||||
MD2("MD2", AbstractDigest.MD2_DIGEST_LENGTH),
|
||||
/**
|
||||
* MD5 digest algorithm.
|
||||
*/
|
||||
MD5("MD5"),
|
||||
MD5("MD5", AbstractDigest.MD5_DIGEST_LENGTH),
|
||||
/**
|
||||
* SHA-1 digest algorithm.
|
||||
*/
|
||||
SHA1("SHA-1"),
|
||||
SHA1("SHA-1", AbstractDigest.SHA1_DIGEST_LENGTH),
|
||||
/**
|
||||
* SHA-256 digest algorithm.
|
||||
*/
|
||||
SHA256("SHA-256"),
|
||||
SHA256("SHA-256", AbstractDigest.SHA256_DIGEST_LENGTH),
|
||||
/**
|
||||
* SHA-384 digest algorithm.
|
||||
*/
|
||||
SHA384("SHA-384"),
|
||||
SHA384("SHA-384", AbstractDigest.SHA384_DIGEST_LENGTH),
|
||||
/**
|
||||
* SHA-512 digest algorithm.
|
||||
*/
|
||||
SHA512("SHA-512");
|
||||
SHA512("SHA-512", AbstractDigest.SHA512_DIGEST_LENGTH);
|
||||
|
||||
private String standardAlgorithmName;
|
||||
private final String standardAlgorithmName;
|
||||
|
||||
private final int lengthInBytes;
|
||||
|
||||
/**
|
||||
* Creates a new <code>DigestAlgorithm</code>.
|
||||
*
|
||||
* @param standardAlgorithmName
|
||||
* Java standard algorithm name
|
||||
* @param lengthInBytes length of hash in bytes
|
||||
*/
|
||||
DigestAlgorithm(final String standardAlgorithmName) {
|
||||
DigestAlgorithm(final String standardAlgorithmName, final int lengthInBytes) {
|
||||
this.standardAlgorithmName = standardAlgorithmName;
|
||||
this.lengthInBytes = lengthInBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,6 +56,14 @@ public enum DigestAlgorithm {
|
||||
return this.standardAlgorithmName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hash length in bytes (not hex characters).
|
||||
* @return hash length in bytes (not hex characters)
|
||||
*/
|
||||
public int getLengthInBytes() {
|
||||
return lengthInBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a DigestAlgorithm object given a String. The String is expected to be one of the
|
||||
* options for standardAlgorithmName. Throws an IllegalArgumentException if no Enum exists with
|
||||
|
Loading…
x
Reference in New Issue
Block a user