package org.edx.mobile.util; import android.support.annotation.NonNull; import org.edx.mobile.logger.Logger; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Sha1Util { private static final Logger logger = new Logger(Sha1Util.class); /** * @param text The plain text to hash. * @return SHA1 hash of the given text or the plain text if hashing failed. */ public static String SHA1(@NonNull String text) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(text.getBytes("iso-8859-1"), 0, text.length()); byte[] sha1hash = md.digest(); return convertToHex(sha1hash); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { logger.error(e); return text; } } public static String convertToHex(@NonNull byte[] data) { StringBuilder buf = new StringBuilder(); for (byte b : data) { int halfbyte = (b >>> 4) & 0x0F; int two_halfs = 0; do { buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10))); halfbyte = b & 0x0F; } while (two_halfs++ < 1); } return buf.toString(); } }