spritzjs
Version:
A Spritz stream-cipher implementation in JavaScript
43 lines (31 loc) • 1.84 kB
HTML
<script src="../facade-high-level.js"></script>
<script src="../spritzjs.js"></script>
<script>
(function setUp() {
// window.facadeHighLevel only exposes the high-level API
// hash, encrypt/decrypt, encryptWithIV/decryptWithIV
// it also enforces the guards refactored out from spritzjs v0.3
window.facadeHighLevel = window.facadeHighLevel;
// spritzjs module is now a constructor, with optional facade
// if no facade is provided then all spritzjs functions are exposed
window.spritzjs = window.spritzjs(window.facadeHighLevel);
}());
(function hashExample(){
var M = [65, 66, 67]; // "ABC" as a byte array
var r = 32; // 32 byte hash desired
var hashed = spritzjs.hash(M, r); // "hashed" now contains 32 bytes of hashed "M" material
console.log(hashed.length); // -> 32
console.log(hashed); // -> [2, 143, 162,..., 71]
}());
(function encryptDecryptExample(){
var K = [115, 101, 116, 101, 99, 45, 97, 115, 116, 114, 111, 110, 111, 109, 121];
var M = [84, 104, 97, 110, 107, 32, 121, 111, 117, 32, 102, 111, 114, 32, 100, 101, 99, 111, 100, 105, 110, 103, 32, 116, 104, 105, 115, 32, 112, 108, 97, 105, 110, 116, 101, 120, 116, 32, 97, 116, 32, 108, 101, 97, 115, 116, 44, 32, 73, 32, 104, 111, 112, 101, 32, 121, 111, 117, 32, 119, 105, 108, 108, 32, 116, 114, 121, 32, 115, 112, 114, 105, 116, 122, 106, 115, 33];
var encrypted = spritzjs.encrypt(K, M);
console.log(encrypted.length === M.length); // -> true
console.log(encrypted); // -> [27, 217, 247,..., 165]
var decrypted = spritzjs.decrypt(K, encrypted);
for (var i = 0; i < decrypted.length; i++) {
if (M[i] !== decrypted[i]) throw new Error("I shouldn't be thrown");
}
}());
</script>