UNPKG

blake2s-js

Version:

Pure JavaScript implementation of BLAKE2s cryptographic hash function.

57 lines (51 loc) 1.72 kB
<!doctype html> <html> <head> <title>BLAKE2s-js Demo</title> </head> <style> /* Taken from blake2.net */ html,body { color: #000000; background-color: white; font-family: sans-serif; } h1 { font-size:275%; } a:link {color:#aa0000; text-decoration:none;} a:visited {color:#aa0000; text-decoration:none;} a:hover {color:#dd0000; text-decoration:none} #digest { font-family: monospace; } </style> <script src="../blake2s.js"></script> <body> <h1>BLAKE2s in JavaScript</h1> <p>Input:<br> <textarea id=input cols=80 rows=10>Enter text and press the button.</textarea><br> Digest length: <input id=digest-length type=number min=1 max=32 value=32> Key: <input id=key size=32> <p><button onclick="calculateDigest()">Digest &rarr;</button> <span id=digest></span> <p><a href="https://github.com/dchest/blake2s-js">Source code</a> <script> function decodeUTF8(s) { var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length); for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i); return b; } function calculateDigest() { // Get message converting to Unix line breaks. var msg = document.getElementById('input').value.replace(/\r\n/g, '\n'); var key = document.getElementById('key').value; var length = document.getElementById('digest-length').value; try { var h = new BLAKE2s(length, decodeUTF8(key)); } catch (e) { alert("Error: " + e); } h.update(decodeUTF8(msg)); document.getElementById('digest').innerHTML = h.hexDigest(); } </script> </body> </html>