Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Last active September 30, 2019 10:14
Show Gist options
  • Select an option

  • Save srkiNZ84/d5c29d86ebf0751a2e9f46035b6f5d64 to your computer and use it in GitHub Desktop.

Select an option

Save srkiNZ84/d5c29d86ebf0751a2e9f46035b6f5d64 to your computer and use it in GitHub Desktop.
Playing around with the Javascript crypto API's to see how they work
<html>
<head>
<script type="text/javascript" src="webcrypto.js"></script>
<title>Web Crypto Hello World</title>
</head>
<body>
<p>This is a super simple example of trying to get Web Cryptography working</p>
</body>
</html>
function convertStringToArrayBufferView(str)
{
var bytes = new Uint8Array(str.length);
for (var iii = 0; iii < str.length; iii++)
{
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
function convertArrayBufferViewtoString(buffer)
{
var str = "";
for (var iii = 0; iii < buffer.byteLength; iii++)
{
str += String.fromCharCode(buffer[iii]);
}
return str;
}
var crypto = window.crypto || window.msCrypto;
var key_object = null;
var promise_key = null;
var data = "SecretSquirrel";
var encrypted_data = null;
var encrypt_promise = null;
//var vector = crypto.getRandomValues(new Uint8Array(16));
var vector = null;
var secretPass = "This is a really long sentence that should really not be this long, but hey, whatcha gonna do?";
var secretDigest_promise = crypto.subtle.digest('SHA-1', convertStringToArrayBufferView(secretPass));
secretDigest_promise.then(function(secretDigest){
console.log("Digest type is: " + secretDigest);
console.log("Digest is: '" + convertArrayBufferViewtoString(secretDigest) + "' with length of " + secretDigest.byteLength);
rightsizeDigest = secretDigest.slice(0,16);
console.log("Digest type is: " + rightsizeDigest);
console.log("Digest is: '" + convertArrayBufferViewtoString(rightsizeDigest) + "' with length of " + rightsizeDigest.byteLength);
vector = rightsizeDigest;
});
//var vector = convertStringToArrayBufferView('this string has ');
var decrypt_promise = null;
var decrypted_data = null;
if(crypto.subtle)
{
promise_key = crypto.subtle.generateKey({name: "AES-CBC", length: 128}, false, ["encrypt", "decrypt"]);
promise_key.then(function(key){
key_object = key;
encrypt_data();
});
promise_key.catch = function(e){
console.log(e.message);
}
}
else
{
alert("Cryptography API not Supported");
}
function encrypt_data()
{
encrypt_promise = crypto.subtle.encrypt({name: "AES-CBC", iv: vector}, key_object, convertStringToArrayBufferView(data));
encrypt_promise.then(
function(result){
encrypted_data = new Uint8Array(result);
decrypt_data();
},
function(e){
console.log(e.message);
}
);
}
function decrypt_data()
{
decrypt_promise = crypto.subtle.decrypt({name: "AES-CBC", iv: vector}, key_object, encrypted_data);
decrypt_promise.then(
function(result){
decrypted_data = new Uint8Array(result);
console.log(convertArrayBufferViewtoString(decrypted_data));
},
function(e){
console.log(e.message);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment