The most interesting thing about hashing is that even if you start with the correct line, the result can still be completely wrong if these lines are combined / encoded / decoded incorrectly.
The biggest problem is that most of these functions actually work with binary representation of input strings. So, how these lines are decoded is of great importance. Please note that the same line generates completely different binaries when decoding as UTF-8 and Hex? This means that the results are Hash, ToBase64, etc. Also will be completely different.
// Result: UTF-8: 65-65-68-69 writeOutput("<br>UTF-8: "& arrayToList(charsetDecode("AADE", "UTF-8"), "-")); // Result: HEX: -86--34 writeOutput("<br>HEX: "& arrayToList(binaryDecode("AADE", "HEX"), "-"));
Possible Solution:
The problem with the current code is that ToBase64 assumes the input string is encoded as UTF-8. While Hash () actually returns a hexadecimal string. Therefore, ToBase64 () decodes it incorrectly. Instead, use binaryDecode and binaryEncode to convert the hash from hex to base64:
resultAsHex = Hash( NONCE & TS & Hash(PW,"SHA-1"), "SHA-1"); resultAsBase64 = binaryEncode(binaryDecode(resultAsHex, "HEX"), "base64"); writeDump(resultAsBase64);
More reliable solution:
Having said that, be very careful with string concatenation and hashing. Because it does not always give the expected results . Without knowing more about this particular API, I cannot fully understand what it expects. However, it is generally safer to work with binary values ββonly. Unfortunately, the CF ArrayAppend () function does not support binary array support, but you can easily use the Apache ArrayUtils class that is associated with CF.
ArrayUtils = createObject("java", "org.apache.commons.lang.ArrayUtils"); // Combine binary of NONCE + TS nonceBytes = charsetDecode(NONCE, "UTF-8"); timeBytes = charsetDecode(TS, "UTF-8"); combinedBytes = ArrayUtils.addAll(nonceBytes, timeBytes); // Combine with binary of SECRET secretBytes = binaryDecode( Hash(PW,"SHA-1"), "HEX"); combinedBytes = ArrayUtils.addAll(combinedBytes, secretBytes); // Finally, HASH the binary and convert to base64 resultAsHex = hash(combinedBytes, "SHA-1"); resultAsBase64 = binaryEncode(binaryDecode(resultAsHex, "hex"), "base64"); writeDump(resultAsBase64);