Skip to content

Instantly share code, notes, and snippets.

@bmatusiak
Created March 6, 2022 02:31
Show Gist options
  • Save bmatusiak/c4517056a93f96ea904e85b7f5c0ddb5 to your computer and use it in GitHub Desktop.
Save bmatusiak/c4517056a93f96ea904e85b7f5c0ddb5 to your computer and use it in GitHub Desktop.
setTimeout(function() {
Uint8Array.prototype.toArray = function() {
var o = [];
this.map(function(byte) {
o.push(byte);
});
return o;
};
Array.prototype.toArrayBuffer = function() {
return new Uint8Array(this).buffer;
};
String.prototype.toArrayBuffer = function() {
var self = this,
buffer = [];
for (let i = 0; i < self.length; i++) {
buffer.push(self.charCodeAt(i));
}
return buffer.toArrayBuffer();
};
ArrayBuffer.prototype.toArray = function() {
return (new Uint8Array(this)).toArray();
};
ArrayBuffer.prototype.toHex = function() {
return (new Uint8Array(this)).toArray().map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};
ArrayBuffer.prototype.toString = function() {
return (new Uint8Array(this)).toArray().map(function(byte) {
return String.fromCharCode(byte);
}).join('');
};
ArrayBuffer.fromHex = function(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return (new Uint8Array(result)).buffer;
};
ArrayBuffer.fromArray = function(fromArray) {
var result = [];
for (var i = 0; i < fromArray.length; i += 1) {
result.push(parseInt(fromArray[i], 10));
}
return (new Uint8Array(result)).buffer;
};
function u2f_b64(s) { //url safe encode
return btoa(s).replace(/\+/g, '-').replace(/\//g //
, '_').replace(/=/g, '');
}
function u2f_unb64(s) { //decode
s = s.replace(/-/g, '+').replace(/_/g, '/');
return atob(s + '==='.slice((s.length + 3) % 4)).toArrayBuffer();
}
var passphrase = "helloworld";
var crypto = require("crypto");
var AES = crypto.AES;
var PBKDF2 = crypto.PBKDF2;
var SHA256 = crypto.SHA256;
var salt = SHA256("pepper");
var key = SHA256(PBKDF2(passphrase, salt, {
// keySize: 32,
// iterations: 32,
hasher: 'SHA256'
}));
console.log("key", u2f_b64(key));
// console.log("key", key.toArray());
var decrypted = ("1111111111111111" ).toArrayBuffer();
// console.log("decrypted urlSafeBase64", u2f_b64(decrypted));
// console.log("decrypted hex", u2f_unb64(u2f_b64(decrypted)).toHex());
var opts = {
iv: new Uint8Array(16),
mode: 'GCM'
};
var test_encrypted2 = u2f_unb64("USsIqfItNClEgr2Js5sp0RPdc5sc63zYp9gtnD-QpuY");
console.log("test_encrypted2", "("+test_encrypted2.length+")", test_encrypted2);
var test_decrypted2 = AES.decrypt(test_encrypted2, key, opts);
console.log("test_decrypted2", test_decrypted2 && test_decrypted2.toArray());
console.log("passed?: ", decrypted === test_decrypted2);
console.log("---");
var encrypted = AES.encrypt(decrypted, key, opts);
console.log("encrypted", u2f_b64(encrypted));
console.log("test_encrypted1", "("+encrypted.length+")",encrypted);
var test_decrypted = AES.decrypt(encrypted, key, opts);
//test_encrypted1 (32) new Uint8Array([81, 43, 8, 169, 242, 45, 52, 41, 68, 130, 189, 137, 179, 155, 41, 209, 53, 62, 27, 213, 155, 116, 240, 242, 241, 158, 173, 61, 159, 139, 68, 89]).buffer
console.log("test_decrypted", test_decrypted && test_decrypted.toArray());
console.log("passed?: ", decrypted === test_decrypted);
}, 1000);
Uint8Array.prototype.toArray = function() {
var o = [];
this.map(function(byte) {
o.push(byte);
});
return o;
};
Array.prototype.toArrayBuffer = function() {
return new Uint8Array(this).buffer;
};
String.prototype.toArrayBuffer = function() {
var self = this,
buffer = [];
for (let i = 0; i < self.length; i++) {
buffer.push(self.charCodeAt(i));
}
return buffer.toArrayBuffer();
};
ArrayBuffer.prototype.toArray = function() {
return (new Uint8Array(this)).toArray();
};
ArrayBuffer.prototype.toHex = function() {
return (new Uint8Array(this)).toArray().map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};
ArrayBuffer.prototype.toString = function() {
return (new Uint8Array(this)).toArray().map(function(byte) {
return String.fromCharCode(byte);
}).join('');
};
ArrayBuffer.fromHex = function(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return (new Uint8Array(result)).buffer;
};
ArrayBuffer.fromArray = function(fromArray) {
var result = [];
for (var i = 0; i < fromArray.length; i += 1) {
result.push(parseInt(fromArray[i], 10));
}
return (new Uint8Array(result)).buffer;
};
function u2f_b64(s) { //url safe encode
return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
function u2f_unb64(s) { //decode
s = s.replace(/-/g, '+').replace(/_/g, '/');
return atob(s + '==='.slice((s.length + 3) % 4)).toArrayBuffer();
}
async function generateKey() {
return await crypto.subtle.generateKey({
"name": "AES-GCM",
"length": 128
}, true, ['encrypt', 'decrypt']);
}
async function exportKey(key) {
return await crypto.subtle.exportKey('jwk', key);
}
async function importKey(jwk) {
return await crypto.subtle.importKey('jwk', jwk, {
"name": "AES-GCM"
}, false, ['encrypt', 'decrypt']);
}
async function encrypt(string, key) {
let encoded = new TextEncoder().encode(string);
let opts = {
"name": "AES-GCM",
"iv": new Uint8Array(16),
// "tagLength": 32,
"additionalData": new Uint8Array(0)
};
let encrypted = await crypto.subtle.encrypt(opts, key, encoded);
return encrypted
}
async function decrypt(encrypted, key) {
let opts = {
"name": "AES-GCM",
"iv": new Uint8Array(16),
// "tagLength": 32,
"additionalData": new Uint8Array(0)
};
let decrypted = await crypto.subtle.decrypt(opts, key, encrypted);
let decoded = new TextDecoder().decode(decrypted);
return decoded;
}
// generateKey().then((key)=>{
// exportKey(key).then(console.log)
// })
var jwk = {
alg: "A256GCM",
ext: true,
k: "LvhhTDRAkSlfD9JJi90MAkfjaePSkoeIBia9e4QN1uA",
key_ops: ['encrypt', 'decrypt'],
kty: "oct",
};
importKey(jwk).then((key) => {
encrypt(("1111111111111111").toArrayBuffer(), key).then((encrypted) => {
console.log("encrypted", u2f_b64(encrypted))
console.log("encrypted", encrypted)
console.log("encrypted", encrypted.toArray())
decrypt(encrypted, key).then((decrypted) => {
console.log("decrypted1", decrypted)
});
});
})
importKey(jwk).then((key) => {
decrypt(u2f_unb64("USsIqfItNClEgr2Js5sp0XU0IMNh8_1l4W2jfshgCzE"), key).then((decrypted) => {
console.log("decrypted2", decrypted)
}).catch(err => console.log(err));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment