/** * Returns a Hex encoded of the MD5 digest of binaries * * @param bytes * the bytes to be digested * @return the hex encoded MD5 digest */ public static String getMD5Digest(byte[] bytes) { return Utils.toHex(digest(DigestAlgorithm.MD5, bytes)); }
/** * This method loads a certificate from the given resource. The certificate must be DER-encoded and may be supplied * in binary or printable (PEM / Base64) encoding. * * If the certificate is provided in Base64 encoding, it must be bounded at the beginning by * {@code -----BEGIN CERTIFICATE-----}, and must be bounded at the end by {@code -----END CERTIFICATE-----}. * * * @param path * resource location. * @return the certificate token */ public static CertificateToken loadCertificate(final String path) { final InputStream inputStream = DSSUtils.class.getResourceAsStream(path); return loadCertificate(inputStream); }
@Override public CertificateToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return DSSUtils.loadCertificateFromBase64EncodedString(json.getAsString()); }
/** * This method loads a certificate from the given location. The certificate must be DER-encoded and may be supplied * in binary or printable (PEM / Base64) encoding. * * If the certificate is provided in Base64 encoding, it must be bounded at the beginning by * {@code -----BEGIN CERTIFICATE-----}, and must be bounded at the end by {@code -----END CERTIFICATE-----}. * * @param file * the file with the certificate * @return the certificate token */ public static CertificateToken loadCertificate(final File file) { final InputStream inputStream = DSSUtils.toByteArrayInputStream(file); return loadCertificate(inputStream); }
/** * FROM: Apache * Reads the contents of a file into a byte array. * The file is always closed. * * @param file * the file to read, must not be {@code null} * @return the file contents, never {@code null} */ public static byte[] toByteArray(final File file) { try (InputStream is = openInputStream(file)) { return toByteArray(is); } catch (Exception e) { throw new DSSException(e); } }
final byte[] digest = DSSUtils.digest(DigestAlgorithm.MD5, content); final String digestHexEncoded = DSSUtils.toHex(digest); final String cacheFileName = fileName + "." + digestHexEncoded; final File file = getCacheFile(cacheFileName); final byte[] byteArray = DSSUtils.toByteArray(file); return byteArray; } else { DSSUtils.saveToFile(returnedBytes, cacheFile);
@BeforeClass public static void init() { certificateWithAIA = DSSUtils.loadCertificate(new File("src/test/resources/TSP_Certificate_2014.crt")); assertNotNull(certificateWithAIA); certificate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4KtQo0QS5Ku+Y2BDA3bX+g8eNLQkaXTycgL4X3MyE5pBOl1OW3KOjJdfyLF+Sii+JKjNf8ZQWk0xvkBEI+nhCzDXhtKAcrkTKlXE25MiUnYoRsXkXgrzYftxAMxvFOXJji/hnX5Fe/3SBAHaE+jU6yC5nk6Q9ERii8mL0nHouMlZWSiAuXtlZDFrzwtLD2ITBECe4X60BDQfb/caO2u3HcWoG1AOvGxfQB0cMmP2njCdDf8UOqryiyky4t7Jj3ghOvETjWlwMw5ObhZ8yj8p6qFAt7+EVJfpUc1gDAolS/hJoLzohbL5LnCAnUAWsFpvG3qW1ky+X0MePXi6q/boqj2tcC4IDdsYS6RHPBvzl5+yLDccrGx1s/7vQYTMNyX0dYZzuxFZxx0bttWfjqLz3hFHlAEVmLCyUkSz761CbaT9u/G4tPP4Q8ApFfSskPI57lbLWIcwP"); assertNotNull(certificateOCSP); }
@Test public void convertToPEM() { String convertToPEM = DSSUtils.convertToPEM(certificateWithAIA); assertFalse(DSSUtils.isStartWithASN1SequenceTag(new ByteArrayInputStream(convertToPEM.getBytes()))); CertificateToken certificate = DSSUtils.loadCertificate(convertToPEM.getBytes()); assertEquals(certificate, certificateWithAIA); byte[] certDER = DSSUtils.convertToDER(convertToPEM); assertTrue(DSSUtils.isStartWithASN1SequenceTag(new ByteArrayInputStream(certDER))); CertificateToken certificate2 = DSSUtils.loadCertificate(certDER); assertEquals(certificate2, certificateWithAIA); }
@Test public void testLoadIssuerNoAIA() { CertificateToken certificate = DSSUtils.loadCertificate(new File("src/test/resources/citizen_ca.cer")); assertTrue(DSSUtils.loadPotentialIssuerCertificates(certificate, new NativeHTTPDataLoader()).isEmpty()); assertTrue(certificate.isCA()); }
/** * Creates dss document that retains the data in memory * * @param inputStream * input stream representing the document */ public InMemoryDocument(final InputStream inputStream) { this(DSSUtils.toByteArray(inputStream), null, null); }
/** * Allows to add a given array of {@code byte} as a cache file representing by the {@code urlString}. * * @param urlString * the URL to add to the cache * @param bytes * the content of the cache file */ public void saveBytesInCache(final String urlString, final byte[] bytes) { final String fileName = ResourceLoader.getNormalizedFileName(urlString); final File out = getCacheFile(fileName); DSSUtils.saveToFile(bytes, out); }
final byte[] bytes = DSSUtils.toByteArray(file); return bytes; } else { DSSUtils.saveToFile(bytes, out);
/** * Return a unique id for a date and the certificateToken id. * * @param signingTime * the signing time * @param id * the token identifier * @return an unique string */ public static String getDeterministicId(final Date signingTime, TokenIdentifier id) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos)) { if (signingTime != null) { dos.writeLong(signingTime.getTime()); } if (id != null) { dos.writeChars(id.asXmlId()); } dos.flush(); return "id-" + getMD5Digest(baos.toByteArray()); } catch (IOException e) { throw new DSSException(e); } }
/** * This method converts the given certificate into its PEM string. * * @param cert * the token to be converted to PEM * @return PEM encoded certificate */ public static String convertToPEM(final CertificateToken cert) { return convertToPEM(cert.getCertificate()); }
@Test public void testChainFromSchemeServiceDefinitionURI() { String base64 = "MIIFvjCCA6agAwIBAgIQALwvYx2O1YN6UxQOi3Bx3jANBgkqhkiG9w0BAQUFADBbMQswCQYDVQQGEwJFUzEoMCYGA1UECgwfRElSRUNDSU9OIEdFTkVSQUwgREUgTEEgUE9MSUNJQTEMMAoGA1UECwwDQ05QMRQwEgYDVQQDDAtBQyBSQUlaIERHUDAeFw0wNzAxMjUxMjA1MDhaFw0zNzAxMjUxMjA1MDhaMFsxCzAJBgNVBAYTAkVTMSgwJgYDVQQKDB9ESVJFQ0NJT04gR0VORVJBTCBERSBMQSBQT0xJQ0lBMQwwCgYDVQQLDANDTlAxFDASBgNVBAMMC0FDIFJBSVogREdQMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAgBD1t16zMJxvoxuIDlyt6pfgzPmmfJMFvPyoj0AOxjyxu6f77K/thV/pMatQqjGae3Yj83upv7YFygq/jU02EeEIeQQEf+QJ+B+LX+oGLPbU5g8/W1eFcnXC4Jg2ipP7L2qcEfA180As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FMSTjFeFdpZUgOC1wrRkxXidWQwr4566fYWhYH0w+hwK9gD6NEsMA3D7NOPCTCOx9Qst5848RsJVJ4F+ZFmT4iyTYLyglkNkeB+tSXVyC9Lg+Tvay85VyeZMSZ3PpGmpNzaQxVZl9XCfs8R6Ew4pG91eOA0BjsI1ZHY7H9e5Pomup/jTA6JwlCYooEiBM31Gdwe/3oUFNzB+NvOWdwb+ZG6va70j98EdipGWoLvjv/oJlFN2q1Nrt/u7whKp+VsVOjuZMrSpw9C+Ec4yiLha5RRiXnHX1cqwT694KIDQZIgqQChQDeDqrvCphtdHdxFQ5NBzt2HKhaSh8ggDdOdpH451rB45Jg=="; CertificateToken issuerCert = DSSUtils.loadCertificateFromBase64EncodedString(base64); assertNotNull(issuerCert); assertTrue(issuerCert.isSelfSigned()); CertificateToken childCert = DSSUtils.loadCertificate(new File("src/test/resources/es_certificate_from_SchemeServiceDefinitionURI.crt")); assertNotNull(childCert); assertFalse(childCert.isSelfSigned()); assertTrue(childCert.isSignedBy(issuerCert)); CertificateToken childCert2 = DSSUtils.loadCertificate(new File("src/test/resources/es_certificate_from_SchemeServiceDefinitionURI2.crt")); assertNotNull(childCert2); assertFalse(childCert2.isSelfSigned()); assertTrue(childCert2.isSignedBy(issuerCert)); }
/** * Creates dss document that retains the data in memory * * @param inputStream * input stream representing the document * @param name * the file name if the data originates from a file */ public InMemoryDocument(final InputStream inputStream, final String name) { this(DSSUtils.toByteArray(inputStream), name); }
/** * Saves all datafiles to specified folder * * @param container as Container object * @param path as String */ public static void saveAllFilesFromContainerToFolder(Container container, String path) { for (DataFile dataFile : container.getDataFiles()) { File file = new File(path + File.separator + dataFile.getName()); DSSUtils.saveToFile(dataFile.getBytes(), file); } }
@Test public void getMD5Digest() throws UnsupportedEncodingException { assertEquals("3e25960a79dbc69b674cd4ec67a72c62", DSSUtils.getMD5Digest("Hello world".getBytes("UTF-8"))); }
private CertificateToken createCertificateToken(Element certificateElement) { byte[] derEncoded = Base64.decodeBase64(certificateElement.getTextContent()); return DSSUtils.loadCertificate(derEncoded); }
@Override public String getDigest(final DigestAlgorithm digestAlgorithm) { String base64EncodeDigest = base64EncodeDigestMap.get(digestAlgorithm); if (base64EncodeDigest == null) { final byte[] digestBytes = DSSUtils.digest(digestAlgorithm, this); base64EncodeDigest = Utils.toBase64(digestBytes); base64EncodeDigestMap.put(digestAlgorithm, base64EncodeDigest); } return base64EncodeDigest; }