Skip to content

Instantly share code, notes, and snippets.

@declank
Last active August 29, 2015 14:12
Show Gist options
  • Save declank/689b972dbcbfd3ccc12c to your computer and use it in GitHub Desktop.
Save declank/689b972dbcbfd3ccc12c to your computer and use it in GitHub Desktop.
Use of HMAC-SHA256 in the Stanford Javascript Crypto Library (view at http://rawgit.com/declank/689b972dbcbfd3ccc12c/raw/sjcl.html)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Demo of <a href="http://bitwiseshiftleft.github.io/sjcl/" target="_blank">Stanford Javascript Crypto Library</a></h1>
<p>Generates HMAC using SHA-256</p>
<input type="text" id="key" value="key"><br>
<input type="text" id="message" value="The quick brown fox jumps over the lazy dog"><br>
<button id="encrypt">Encrypt</button><br>
<output id="output"></output>
<script src="//cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.0/sjcl.min.js"></script>
<script>
var encryptButton = document.getElementById("encrypt");
encryptButton.addEventListener('click', function(event) {
'use strict';
if(typeof sjcl === 'undefined') {
alert("Please wait for library to load...");
} else {
var key = document.getElementById("key").value;
var message = document.getElementById('message').value;
var hmac = new sjcl.misc.hmac(sjcl.codec.utf8String.toBits(key));
var output = "";
output += "Key: " + key + "<br>";
output += "Message: " + message + '<br>';
output += "HMAC: 0x" + sjcl.codec.hex.fromBits(hmac.encrypt(message)) + '<br>';
document.getElementById('output').innerHTML = output;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment