Showing posts with label hash. Show all posts
Showing posts with label hash. Show all posts

Thursday, March 26, 2015

How to hash data in Java

P
A hash function is any function that can be used to map digital data of arbitrary size to digital data of fixed size. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes. One practical use is a data structure called a hash table, widely used in computer software for rapid data lookup. Hash functions accelerate table or database lookup by detecting duplicated records in a large file. An example is finding similar stretches in DNA sequences. They are also useful in cryptography. A cryptographic hash function allows one to easily verify that some input data matches a stored hash value, but makes it hard to construct any data that would hash to the same value or find any two unique data pieces that hash to the same value. This principle is used by the PGP algorithm for data validation and by many password checking systems.

Hash functions are related to (and often confused with) checksums, check digits, fingerprints, randomization functions, error-correcting codes, and ciphers. Although these concepts overlap to some extent, each has its own uses and requirements and is designed and optimized differently. The Hash Keeper database maintained by the American National Drug Intelligence Center, for instance, is more aptly described as a catalog of file fingerprints than of hash values.

Now I will show you how to do that in Java, It's really simple.

try {
 MessageDigest md = MessageDigest.getInstance("SHA-512"); // refer the table below
 md.update("abc".getBytes());
 System.out.println(DatatypeConverter.printHexBinary(md.digest()));
} catch (NoSuchAlgorithmException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}


Algorithm Name Description
MD2 The MD2 message digest algorithm as defined in RFC 1319.
MD5 The MD5 message digest algorithm as defined in RFC 1321.
SHA-1
SHA-256
SHA-384
SHA-512
Hash algorithms defined in the FIPS PUB 180-2.

SHA-256 is a 256-bit hash function intended to provide 128 bits of security against collision attacks, while SHA-512 is a 512-bit hash function intended to provide 256 bits of security. A 384-bit hash may be obtained by truncating the SHA-512 output.

Reference

Calculate thumbprint of Certificate

P
Beside Serial Number is an unique value to identify a certificate, thumbprint is usually used by organizations as well. In Java, java.security.cert.Certificate and java.security.cert.X509Certificate is presented for a certificate but they just have getSerialNumber() interface to obtain its Serial Number. In this post, I will show you how to get thumbprint value of a certificate.
Actually, certificate thumbprint can be obtained by hash the encoded value certificate java.security.cert.Certificate and java.security.cert.X509Certificate is already supported this API.

java.security.cert.Certificate.getEncoded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wdC1jYS52bi9ycGEwMQYDVR0fBCowKDAmoCSgIoYgaHR0cDovL2NybC52bnB0LWNhLnZuL3ZucHRjYS5jcmwwDgYDVR0PAQH/BAQDAgTwMB8GA1UdJQQYMBYGCCsGAQUFBwMEBgorBgEEAYI3CgMMMB0GA1UdEQQWMBSBEnZ1ZHBAdG9taWNhbGFiLmNvbTANBgkqhkiG9w0BAQUFAAOCAgEAcjmp3e+fbPB/rzbqPkk6zEPYGy9Vzd7AumCPqa+q1sbddWgDDtn/VVoCgCbrSNfXPwaEeNGjGmgz+eDN9J9TjqAiIG0YzHerQ5kCMoA5VN4plrls+lOAHoC2DwltrZa67KQU125EjAy0YTq6x35+a/IMke+uOw0EcvCV7Tfg8bdkWkucQGQSh3fvSjfTvd+bywti0WUkj5Zxp1Auaky5HPChhQe6Ea2D+Qakv0yjlb6x5IPFXdMGVzB0lWB/T1mZ69LeOwUAb3Rs3ZNwM6ypJV6yJNT8XmNmGhUxLVbxEb11x28UrR1WdyowZ5NE/+e16BnDHYhof8yWZ15pEcWlUuZt2HdmCFK1tRNtPgYgBBH9FfyCu68aAVY2OTqIDaH19dd9wtKNISE9oJ2vt6zkC1PGyqqZfOmjjNSsAM8/qUFsjhgHd0g3w+qRcpZBxeyHGuVO8X1rFHNhYhns9Do36o8cUcQDn6MileMb5pAVc1t2LnQxA71kuOwjF6IlFXwyiSkx3cx3rUYk0KsXXOA2PvTMCS4rwYfPFaZu5uQnpmr3pJcJ/DuktjRCFlAKCGDWgsUiBAZlIRnQUQvd/tTnb0n7l/TePYaBTg9wPycmU9HD+xua2QmG/esnDaJtS3kx8xfLpSk3XiEGtD4HpiML5Zuw6oxIr5nyeVxU59add2o=";
  CertificateFactory certFactory = CertificateFactory
    .getInstance("X.509");
  InputStream in = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(cert_str));
  X509Certificate cert = (X509Certificate) certFactory
    .generateCertificate(in);
  
  MessageDigest md = MessageDigest.getInstance("SHA-1");
     byte[] der = cert.getEncoded();
     md.update(der);
     byte[] digest = md.digest();
     System.out.println(DatatypeConverter.printHexBinary(digest));

Output:

9899CAC65300F10012D3D3BAFC4345CEA5BC6669

You can check that value on Windows by opening certificate


Depends on hash algorithm you will get the different thumbprint value. In this sample, certificate thumbprint is calculate by SHA1 algorithm. As far as I know, thumbprint is  popularly computed with SHA1 or MD5.

References