A cryptographic hash (sometimes called ‘digest’) is a kind of ‘signature’ for a text or a data file. SHA-512 generates an almost-unique 512-bit (32-byte) signature for a text. See below for the source code.
This is a companion to the SHA-256 script (where there’s more explanation). This is a reference implementation, as close as possible to the NIST specification, to help in understanding the algorithm (§ection numbers relate the code back to sections in the standard); it is not at all optimised (using Chrome on a low-to-middling Core i5 PC, in timing tests this script will hash a short message in around 0.4 – 0.6 ms; longer messages will be hashed at a speed of around 0.5 – 1 MB/sec.
As SHA-512 is based on 64-bit unsigned integers, which are not natively supported by JavaScript,
this is more complex to implement in JavaScript than SHA-256. Here I’ve implemented a
Long
library for UInt64 operations; there would be more efficient means of doing
this for an optimised implementation.
Note that these scripts are intended to assist in studying the algorithms, not for production use. For production use, I would recommend the Web Cryptography API for the browser (see example), or the crypto library in Node.js. For password hashing, I have a WebCrypto example using PBKDF2.
I offer these scripts for free use and adaptation to balance my debt to the open-source info-verse. You are welcome to re-use these scripts [under an MIT licence, without any warranty express or implied] provided solely that you retain my copyright notice and a link to this page.
If you have any queries or find any problems, contact me at ku.oc.epyt-elbavom@cne-stpircs.
© 2016-2019 Chris Veness