Dear forum members,
I'm struggling to convert a password to a SHA256 hash presented as string. I keep ending up with this error:
Error: System Error
Details: Failed invoking <symbol>
The goal is to hash a password string and represent that as a string again. Example: The SHA256 hash for "password" should result in: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8". In the Javascript world the following function does the job (docs):
var sha256Hash = CryptoJS.SHA256("password").toString();
console.log("sha256Hash :: " + sha256Hash)
Using the tips mentioned in this article I got to the following code in MonkeyC, but that fails to work:
function encoding_utf8_getbytes(plain_text) {
return StringUtil.convertEncodedString(plain_text, {
:fromRepresentation => StringUtil.REPRESENTATION_STRING_PLAIN_TEXT,
:toRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY,
:encoding => StringUtil.CHAR_ENCODING_UTF8
});
}
function new_sha256hash() {
return new Cryptography.Hash({
:algorithm => Cryptography.HASH_SHA256
});
}
function computehash(hash, byte_array) {
hash.update(byte_array);
return hash.digest();
}
function convert_bytearray_tostring(byte_array) {
return StringUtil.convertEncodedString(byte_array, {
:fromRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY,
:toRepresentation => StringUtil.REPRESENTATION_STRING_PLAIN_TEXT,
:encoding => StringUtil.CHAR_ENCODING_UTF8
});
}
function convert_bytearray_tostringbase64(byte_array) {
return StringUtil.convertEncodedString(byte_array, {
:fromRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY,
:toRepresentation => StringUtil.REPRESENTATION_STRING_BASE64,
:encoding => StringUtil.CHAR_ENCODING_UTF8
});
}
// hash password to SHA256
var pwrd = "password";
var pwrd_byte_array = encoding_utf8_getbytes(pwrd);
var sha256hash = new_sha256hash();
var sha256hash_bytes = computehash(sha256hash,pwrd_byte_array);
var hashedpasswordbase64 = convert_bytearray_tostringbase64(sha256hash_bytes);
System.println("hashedpasswordbase64: " + hashedpasswordbase64);
var hashedpassword = convert_bytearray_tostring(sha256hash_bytes);
System.println("hashedpassword: " + hashedpassword);
As an aditional example I've added the conversion of the same "sha256hash_bytes" variable to a base64 string. That is working fine, but converting it to a character encoding REPRESENTATION_STRING_PLAIN_TEXT fails with the error above.