Home > OS >  Why SHA hash generated from Angular application and online tool differs for unicode character
Why SHA hash generated from Angular application and online tool differs for unicode character

Time:07-06

The hash generated for text containing rupees symbol and unicode is always producing same hash if I execute in angular application where as it differs from the online hash generation tool Application

₹ - sha256('₹') => d8c3e87cd7f5b7d388f9dc1e35ccee09640ef3dca63d449d9ef59fc323a87a20

\u20B9 - sha256('\u20B9') = > d8c3e87cd7f5b7d388f9dc1e35ccee09640ef3dca63d449d9ef59fc323a87a20

SHA256 online hash

https://emn178.github.io/online-tools/sha256.html gives different outputs

₹ - d8c3e87cd7f5b7d388f9dc1e35ccee09640ef3dca63d449d9ef59fc323a87a20

\u20B9 - 0d5f2a869bcab0dd97ef3aefca2dc44edeb55d71a5e78a026aad412c0bbaa55c

The library which is used for hash generation js-sha256

CodePudding user response:

It seems the online SHA256 hash tool you are using doesn't support Unicode character escape sequences. So when you tell it to compute the hash of \u20B9 it doesn't pick up that you mean the rupee symbol and instead computes the hash of the six-character string with characters \, u, 2, 0, B and 9. The SHA256 hash of this string is indeed 0d5f2a869bcab0dd97ef3aefca2dc44edeb55d71a5e78a026aad412c0bbaa55c.

JavaScript supports Unicode character escapes, so in JavaScript, '\u20B9' and '₹' are the same string: they both represent a 1-character string containing only the rupee symbol. JavaScript supports other string escape sequences, for example \n represents a newline rather than a \ followed by a n. See this MDN page for a list of all JavaScript string escape sequences.

The online hash tool and the hashing library you are using both seem to be working correctly, the problem here seems to be your understanding of what is going on.

  • Related