CheckSum.java 735 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package chat.rocket.android.helper;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA-256, MD5, ...
 */
public class CheckSum {
  /**
   * SHA-256.
   */
  public static String sha256(String orig) {
Tiago Cunha's avatar
Tiago Cunha committed
15
    MessageDigest messageDigest;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    try {
      messageDigest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException exception) {
      return null;
    }
    messageDigest.update(orig.getBytes(Charset.forName("UTF-8")));

    StringBuilder stringBuilder = new StringBuilder();
    for (byte b : messageDigest.digest()) {
      stringBuilder.append(String.format("%02x", b & 0xff));
    }

    return stringBuilder.toString();
  }
}