Message Digests in Android
Overview
Message Digests are one way hash functions that take arbitrary-sized data and returns fixed-length hash value.
Steps to use Message Digests
public static String encryptString(String data) {
String salt = "iamraajkanchan";
String output;
try {
//Create Byte valiable
byte[] dataByte;
data = data + salt;
dataByte = data.getBytes("UTF-8");
//Define hash algorithm
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
//Hashed given pain-text
dataByte = messageDigest.digest(dataByte);
// return the hash as a base 64 encoded string
output = Base64.encodeToString(dataByte, Base64.NO_WRAP);
return output;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Comments
Post a Comment