Skip to content

Instantly share code, notes, and snippets.

@syst3mw0rm
Created October 25, 2018 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syst3mw0rm/22bb4a8c7c51516b358b482b3336d927 to your computer and use it in GitHub Desktop.
Save syst3mw0rm/22bb4a8c7c51516b358b482b3336d927 to your computer and use it in GitHub Desktop.
// helper methods starts
var lc = ["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"];
var uc = ["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F"];
function toHex(val, uppercase) {
var set = uppercase ? uc : lc;
return set[Math.floor(val / 16)] + set[val % 16];
}
function toArrayBuffer(str) {
str = str.toLowerCase().replace(/\s/g, "");
if (str.length % 2 == 1)
str = "0" + str;
var len = Math.floor(str.length / 2);
var buffer = new Uint8Array(len);
var curr = -1;
for (var i = 0; i < str.length; ++i) {
var c = str[i];
var val = lc.indexOf(c);
if (val == -1)
throw Error("unexpected character")
if (curr === -1) {
curr = 16 * val;
} else {
buffer[Math.floor(i / 2)] = curr + val;
curr = -1;
}
}
return buffer;
}
function toString(arr, opts) {
var defaultOpts = {
grouping: 0,
rowlength: 0,
uppercase: true,
};
if (opts == undefined)
opts = {}
opts = defaultOpts;
var str = "";
var group = 0, column = 0;
for (var i = 0; i < arr.length; ++i) {
str += toHex(arr[i], opts.uppercase);
if (i === arr.length - 1)
break;
if (opts.grouping > 0 && ++group === opts.grouping) {
group = 0;
if (opts.rowlength > 0 && ++column === opts.rowlength) {
column = 0;
str += "\n";
}
else
str += " ";
}
}
return str;
}
const ab2str = buf => String.fromCharCode.apply(null, new Uint16Array(buf));
const ab2hex = ab => toString(new Uint8Array(ab));
const str2ab = str => {
const buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
// helper method ends
async function importCryptoKey(key){
return await window.crypto.subtle.importKey(
"raw", // can be "jwk" or "raw"
toArrayBuffer(key).buffer,
{name: 'AES-GCM'},
true, // whether the key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] // can "encrypt", "decrypt", "wrapKey", or "unwrapKey"
)
};
async function encrypt({ text, iv, key }){
const arrayBuffer = await window.crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: toArrayBuffer(iv).buffer,
},
await importCryptoKey(key),
str2ab(text),
);
return ab2hex(arrayBuffer);
};
key = 'EEE72939E910F74104846B5BADBDDAD007A46E3392ADE713F6EE33900299C250'
iv = '523AEBE425EA83DCE42F9512'
data = '{"username":"test","password":"test"}'
res = await encrypt({text: data, iv, key});
console.log(res);
@syst3mw0rm
Copy link
Author

prints C7F3F0EA027EA0B75BF6286AD688497F265AF3AF10A8BA273B7AD9D9D3F1B6FD247E85771BAE4447F81447389A84A3145155E7DE387D6B3E181516707731EC2DC3CEF847FCA949EE484B3FC0A6C66853D42C956BA0735642D29E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment