sha256 hash String - Android java.lang

Android examples for java.lang:String Hash

Description

sha256 hash String

Demo Code


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class Main {
  private static final String LOG_TAG = "";

  public static String sha256String(String source) {
    byte[] hash = null;
    String hashCode = null;// w  ww  .  j  a va 2 s.c  o m
    try {
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      hash = digest.digest(source.getBytes());
    } catch (NoSuchAlgorithmException e) {
      Log.wtf(LOG_TAG, "Can't calculate SHA-256");
    }

    if (hash != null) {
      StringBuilder hashBuilder = new StringBuilder();
      for (int i = 0; i < hash.length; i++) {
        String hex = Integer.toHexString(hash[i]);
        if (hex.length() == 1) {
          hashBuilder.append("0");
          hashBuilder.append(hex.charAt(hex.length() - 1));
        } else {
          hashBuilder.append(hex.substring(hex.length() - 2));
        }
      }
      hashCode = hashBuilder.toString();
    }

    return hashCode;
  }
}

Related Tutorials