Monday 29 April 2019

Cryptographic Hashing in Java

The Java platform strongly emphasizes security, including language safety, cryptography, public key infrastructure, authentication, secure communication, and access control.
The JCA is a major piece of the platform, and contains a "provider" architecture and a set of APIs for digital signatures, message digests (hashes), certificates and certificate validation, encryption (symmetric/asymmetric block/stream ciphers), key generation and management, and secure random number generation, to name a few. These APIs allow developers to easily integrate security into their application code. The architecture was designed around the following principles:
  • Implementation independence: Applications do not need to implement security algorithms. Rather, they can request security services from the Java platform. Security services are implemented in providers (see below), which are plugged into the Java platform via a standard interface. An application may rely on multiple independent providers for security functionality.
  • Implementation interoperability: Providers are interoperable across applications. Specifically, an application is not bound to a specific provider, and a provider is not bound to a specific application.
  • Algorithm extensibility: The Java platform includes a number of built-in providers that implement a basic set of security services that are widely used today. However, some applications may rely on emerging standards not yet implemented, or on proprietary services. The Java platform supports the installation of custom providers that implement such services.
Other cryptographic communication libraries available in the JDK use the JCA provider architecture, but are described elsewhere. The Java Secure Socket Extension (JSSE) provides access to Secure Socket Layer (SSL) and Transport Layer Security (TLS) implementations. The Java Generic Security Services (JGSS) (via Kerberos) APIs, and the Simple Authentication and Security Layer (SASL) can also be used for securely exchanging messages between communicating applications.
The algorithm names in this section can be specified when generating an instance of MessageDigest.


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-224
SHA-256
SHA-384 
 SHA-512


Hash algorithms defined in the FIPS PUB 180-4.

Secure hash algorithms - SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 - for computing a condensed representation of electronic data (message). When a message of any length less than 2^64 bits (for SHA-1, SHA-224, and SHA-256) or less than 2^128 (for SHA-384 and SHA-512) is input to a hash algorithm, the result is an output called a message digest. A message digest ranges in length from 160 to 512 bits, depending on the algorithm.






SHA refers to Secure Hash Algorithm to provide a secure cryptographic hash function to provide the hashes which can be used as a digital signature for various data or files. 

Concept of Blockchain  emphasis of using the digital signatures of the Blocks and linking them as a chain e.g, Bitcoin  , Ethereum  etc are a great set of examples of implenting the cryptographic function as a digital signature.


public class EncryptingHash {

public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("MD2 hash : " + getHash("","MD2"));
System.out.println("MD5 hash : " + getHash("","MD5"));
System.out.println("SHA-1 hash : " + getHash("","SHA-1"));
System.out.println("SHA-224 hash : " + getHash("","SHA-224"));
System.out.println("SHA256 hash : " + getHash("","SHA-256"));
System.out.println("SHA-384 hash : " + getHash("","SHA-384"));
System.out.println("SHA-512 hash : " + getHash("","SHA-512"));
}

static String getHash(String input,String algorithm) throws NoSuchAlgorithmException {
try {
// Static getInstance method is called with hashing algorithm
MessageDigest md = MessageDigest.getInstance(algorithm);
// digest() method called
// to calculate message digest of an input
// and return array of byte
byte[] messageDigest = md.digest(input.getBytes());
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, messageDigest);
// Convert message digest into hex value
String hashtext = number.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch (Exception e) {
System.out.println("Exception thrown : " +  e);
}
return null;
}
}
Output

MD2 hash : 8350e5a3e24c153df2275c9f80692773
MD5 hash : d41d8cd98f00b204e9800998ecf8427e
SHA-1 hash : da39a3ee5e6b4b0d3255bfef95601890afd80709
SHA-224 hash : d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
SHA256 hash : e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
SHA-384 hash : 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b
SHA-512 hash : cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e


Enjoy coding and Java !!!!!!!!!