Check Out Mobile Application Proposal

Sha-256 encoding / encryption using salt in android studio.

 Sha-256 encoding is a one way process. we can encrypt that data but there is no way to decrypt.

    The below is the proper code for generating the sea-256 data. The output must be in the byte[].

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
/*Adding salt to the encoding process...*/
messageDigest.update(salt.getBytes());
shaData = messageDigest.digest(data.getBytes());


        So we need to convert byte[] to hexadecimal string. We have two methods to convert byte array to hexadecimal string.

Method 1

StringBuilder stringBuilder = new StringBuilder();
for (byte shaDatum : shaData){
stringBuilder.append(Integer.toString((shaDatum & 0xff) + 0x100, 16 ));
}

Method 2

StringBuilder hexString = new StringBuilder(2 * shaData.length);
for (byte shaDatum : shaData){
String hex = Integer.toHexString(0xff & shaDatum);
if (hex.length() == 1){
hexString.append("0");
}
hexString.append(hex);
stringBuilder = hexString;
}
        
THANK YOU

Comments