Java – How to apply SHA-512?

How to apply SHA-512?… here is a solution to the problem.

How to apply SHA-512?

I’m new to cryptographic algorithms and I’m trying to create SHA-512 to convert this mutable data to SHA-512 so that I can pass it to my project’s server, which we would appreciate.

if (pojo.getAmount() != null && !pojo.getAmount().equals("")) {

Data variables needs to convert in SHA-512
           hsString = merchantID + "" +  "" + req_id + "" + ip_address + ""
                   + notication_url + "" + package_name + "" + firstname + "" + lastname + ""
                   + middlename + "" + address1 + "" + address2 + "" + city + "" + state + ""
                   + country + "" + zip + "" + email + "" + phone + "" + client_ip + "" + ""
                   + cost + "" + currency + "" + secur3d + "" + merchantKey;

base64string one of the transaction parameters
            base64_enconded = Base64.encodeToString(hsString.getBytes(),Base64.DEFAULT);

Solution

MessageDigest can be used for encryption in java

Description – https://developer.android.com/reference/java/security/MessageDigest.html

Supported algorithms

  1. MD2
  2. MD5
  3. SHA-1
  4. SHA-256
  5. SHA-384
  6. SHA-512

Try this code

try {
    MessageDigest md = MessageDigest.getInstance("SHA-512");
    byte[] data = md.digest(hsString.getBytes());
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<data.length; i++)
    {
        sb.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1));
    }
    System.out.println(sb);

} catch(Exception e) {
    System.out.println(e); 
 }

Related Problems and Solutions