DSSUtils { public static byte[] digest(final DigestAlgorithm digestAlgorithm, final byte[] data) { Objects.requireNonNull(data, "The data cannot be null"); switch (digestAlgorithm) { case SHAKE128: return computeDigest(new SHAKEDigest(128), data); case SHAKE256: return computeDigest(new SHAKEDigest(256), data); default: final MessageDigest messageDigest = getMessageDigest(digestAlgorithm); return messageDigest.digest(data); } } private  DSSUtils(); static String formatInternal(final Date date); static String formatDateWithCustomFormat(final Date date, final String format); static String toHex(final byte[] value); static String convertToPEM(final CertificateToken cert); static boolean isStartWithASN1SequenceTag(InputStream is); static byte[] convertToDER(String pemContent); static CertificateToken loadCertificate(final File file); static CertificateToken loadCertificate(final InputStream inputStream); static Collection<CertificateToken> loadCertificateFromP7c(InputStream is); static CertificateToken loadCertificate(final byte[] input); static CertificateToken loadCertificateFromBase64EncodedString(final String base64Encoded); static Collection<CertificateToken> loadPotentialIssuerCertificates(final CertificateToken cert, final DataLoader loader); static String getSHA1Digest(final String stringToDigest); static byte[] digest(final DigestAlgorithm digestAlgorithm, final byte[] data); static MessageDigest getMessageDigest(DigestAlgorithm digestAlgorithm); static byte[] encodeRSADigest(final DigestAlgorithm digestAlgorithm, final byte[] digest); static byte[] digest(final DigestAlgorithm digestAlgo, final InputStream inputStream); static byte[] digest(DigestAlgorithm digestAlgorithm, DSSDocument document); static byte[] digest(DigestAlgorithm digestAlgorithm, byte[]... data); static InputStream toInputStream(final File file); static InputStream toByteArrayInputStream(final File file); static byte[] toByteArray(final File file); static DSSDocument splitDocument(DSSDocument origin, int start, int end); static byte[] toByteArray(final DSSDocument document); static byte[] toByteArray(final InputStream inputStream); static CMSSignedData toCMSSignedData(final DSSDocument document); static boolean isTimestampToken(final DSSDocument document); static long getFileByteSize(DSSDocument dssDocument); static void saveToFile(final byte[] bytes, final File file); static String getNormalizedString(final String str); static String getDeterministicId(final Date signingTime, TokenIdentifier id); static String getMD5Digest(byte[] bytes); static X500Principal getX500PrincipalOrNull(final String x500PrincipalString); static Date getUtcDate(final int year, final int month, final int day); static void printSecurityProviders(); static byte readFirstByte(final DSSDocument dssDocument); static boolean compareFirstBytes(final DSSDocument dssDocument, byte[] byteArray); static byte[] concatenate(byte[]... arrays); static String decodeUrl(String uri); static long skipAvailableBytes(InputStream is, long n); static long readAvailableBytes(DSSDocument dssDocument, byte[] b); static long readAvailableBytes(InputStream is, byte[] b); static long readAvailableBytes(InputStream is, byte[] b, int off, int len); static String encodeURI(String fileURI); static String getExceptionMessage(Exception e); static Digest getDigest(DigestAlgorithm digestAlgo, DSSDocument dssDocument); static String removeControlCharacters(String str); static final byte[] EMPTY_BYTE_ARRAY; }
@Test public void digest() { Security.addProvider(DSSSecurityProvider.getSecurityProvider()); byte[] data = "Hello world!".getBytes(StandardCharsets.UTF_8); assertEquals("d3486ae9136e7856bc42212385ea797094475802", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA1, data))); assertEquals("7e81ebe9e604a0c97fef0e4cfe71f9ba0ecba13332bde953ad1c66e4", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA224, data))); assertEquals("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA256, data))); assertEquals("f6cde2a0f819314cdde55fc227d8d7dae3d28cc556222a0a8ad66d91ccad4aad6094f517a2182360c9aacf6a3dc323162cb6fd8cdffedb0fe038f55e85ffb5b6", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA512, data))); assertEquals("d3ee9b1ba1990fecfd794d2f30e0207aaa7be5d37d463073096d86f8", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA3_224, data))); assertEquals("d6ea8f9a1f22e1298e5a9506bd066f23cc56001f5d36582344a628649df53ae8", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA3_256, data))); assertEquals("f9210511d0b2862bdcb672daa3f6a4284576ccb24d5b293b366b39c24c41a6918464035ec4466b12e22056bf559c7a49", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA3_384, data))); assertEquals("95decc72f0a50ae4d9d5378e1b2252587cfc71977e43292c8f1b84648248509f1bc18bc6f0b0d0b8606a643eff61d611ae84e6fbd4a2683165706bd6fd48b334", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHA3_512, data))); assertEquals("ee8ee3ada079996b80d926eef439a502", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHAKE128, data))); assertEquals("e80627c7a1dd02229936bb2822572025e17b91ef3a94f7ade9d810aee8d6a873", Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHAKE256, data))); String shake256_512 = "e80627c7a1dd02229936bb2822572025e17b91ef3a94f7ade9d810aee8d6a873f3d6795a6f7b042a3b65ba0faa872f32e513eb8f460dc60768ee86a05d22e7ac"; assertEquals(512, Utils.fromHex(shake256_512).length * 8); assertEquals(shake256_512, Utils.toHex(DSSUtils.digest(DigestAlgorithm.SHAKE256_512, data))); }