added support for FIPS style calculation.
package org.bouncycastle.util;
import org.bouncycastle.crypto.digests.SHA512tDigest;
+import org.bouncycastle.crypto.digests.SHAKEDigest;
+/**
+ * Basic 20 byte finger print class.
+ */
public class Fingerprint
{
private static char[] encodingTable =
private final byte[] fingerprint;
+ /**
+ * Base constructor - use SHAKE-256 (160 bits). This is the recommended one as it is also
+ * produced by the FIPS API.
+ *
+ * @param source original data to calculate the fingerprint from.
+ */
public Fingerprint(byte[] source)
+ {
+ this(source, false);
+ }
+
+ /**
+ * Base constructor.
+ *
+ * @param source original data to calculate the fingerprint from.
+ * @param useSHA512t use the old SHA512/160 calculation.
+ */
+ public Fingerprint(byte[] source, boolean useSHA512t)
+ {
+ if (useSHA512t)
+ {
+ this.fingerprint = calculateFingerprintSHA512_160(source);
+ }
+ else
{
this.fingerprint = calculateFingerprint(source);
}
+ }
public byte[] getFingerprint()
{
@@ -57,7 +85,34 @@ public int hashCode()
return Arrays.hashCode(fingerprint);
}
+ /**
+ * Return a byte array containing a calculated fingerprint for the passed in input data.
+ * This calculation is compatible with the BC FIPS API.
+ *
+ * @param input data to base the fingerprint on.
+ * @return a byte array containing a 20 byte fingerprint.
+ */
public static byte[] calculateFingerprint(byte[] input)
+ {
+ SHAKEDigest digest = new SHAKEDigest(256);
+
+ digest.update(input, 0, input.length);
+
+ byte[] rv = new byte[digest.getDigestSize()];
+
+ digest.doFinal(rv, 0, 16);
+
+ return rv;
+ }
+
+ /**
+ * Return a byte array containing a calculated fingerprint for the passed in input data.
+ * The fingerprint is based on SHA512/160.
+ *
+ * @param input data to base the fingerprint on.
+ * @return a byte array containing a 20 byte fingerprint.
+ */
+ public static byte[] calculateFingerprintSHA512_160(byte[] input)
{
SHA512tDigest digest = new SHA512tDigest(160);
