Explaining the difference between two data types can be complex, requiring more space than a comment allows. Therefore, I will elaborate on it in an answer instead.
Take a look at the source code you are referring to. Towards the end (lines 210 and 212), you'll notice that it converts the binary value into a hex string (and then to lower case, although this is not crucial unless there's a string comparison involved at the end). Essentially, your JavaScript library returns a representation as a hex formatted string.
On the other hand, your Sql function HASHBYTES
produces a result of type varbinary
(a different type from varchar, which is a string type).
Essentially, you have two distinct data types (each residing in their own realm since one has not been transformed to match the other). You haven't specified where the comparison is taking place - within the database or between the database and script. Regardless, for a valid comparison, you need to convert one type to ensure you're either comparing two string types or two binary types. Failure to compare similar types may lead to unexpected outcomes or runtime errors.
If the comparison involves strings in JavaScript, refer back to the library you are utilizing - it already features a method called wordToHex
. Simply copy that and use it to convert your Sql result into a string so you can perform a string comparison (remember to account for case sensitivity or convert everything to lower case).
Edit
I am unfamiliar with WebApi as it functions as a black box third-party service. My task is simply to send the security token mentioned above.
If the web api accepts a type like byt[]
, attaching 0x
to your string in JavaScript before sending it to the web api could potentially work. The web api would interpret the incoming parameter as a byte array and conduct the comparison using appropriate data types. Due to the opaque nature of this black box, confirming the accepted type as a byte array requires either direct inquiry or testing.