-
-
Save sohrabsaran/5f1260a5410ee38b9e88dc186af37f70 to your computer and use it in GitHub Desktop.
Uses the SubtleCrypto interface of the Web Cryptography API to encrypt and decrypt text using AES-GCM (AES Galois counter mode).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Forked from https://gist.github.com/chrisveness/43bcda93af9f646d083fad678071b90a | |
License: MIT | |
This version at: https://gist.github.com/sohrabsaran/5f1260a5410ee38b9e88dc186af37f70 | |
Changes: | |
1. Fixed regex bug in ctStr.match (fixed later in the original in https://gist.github.com/chrisveness/43bcda93af9f646d083fad678071b90a/revisions#diff-d559f25d942e0fc455a7207c04b00d94) | |
2. Added polyfill for TextEncoder | |
*/ | |
/** | |
* Encrypts plaintext using AES-GCM with supplied password, for decryption with aesGcmDecrypt(). | |
* (c) Chris Veness MIT Licence | |
* | |
* @param {String} plaintext - Plaintext to be encrypted. | |
* @param {String} password - Password to use to encrypt plaintext. | |
* @returns {String} Encrypted ciphertext. | |
* | |
* @example | |
* const ciphertext = await aesGcmEncrypt('my secret text', 'pw'); | |
* aesGcmEncrypt('my secret text', 'pw').then(function(ciphertext) { console.log(ciphertext); }); | |
*/ | |
async function aesGcmEncrypt(plaintext, password) { | |
const pwUtf8 = new TextEncoder().encode(password); // encode password as UTF-8 | |
const pwHash = await crypto.subtle.digest('SHA-256', pwUtf8); // hash the password | |
const iv = crypto.getRandomValues(new Uint8Array(12)); // get 96-bit random iv | |
const alg = { name: 'AES-GCM', iv: iv }; // specify algorithm to use | |
const key = await crypto.subtle.importKey('raw', pwHash, alg, false, ['encrypt']); // generate key from pw | |
const ptUint8 = new TextEncoder().encode(plaintext); // encode plaintext as UTF-8 | |
const ctBuffer = await crypto.subtle.encrypt(alg, key, ptUint8); // encrypt plaintext using key | |
const ctArray = Array.from(new Uint8Array(ctBuffer)); // ciphertext as byte array | |
const ctStr = ctArray.map(byte => String.fromCharCode(byte)).join(''); // ciphertext as string | |
const ctBase64 = btoa(ctStr); // encode ciphertext as base64 | |
const ivHex = Array.from(iv).map(b => ('00' + b.toString(16)).slice(-2)).join(''); // iv as hex string | |
return ivHex+ctBase64; // return iv+ciphertext | |
} | |
/** | |
* Decrypts ciphertext encrypted with aesGcmEncrypt() using supplied password. | |
* (c) Chris Veness MIT Licence | |
* | |
* @param {String} ciphertext - Ciphertext to be decrypted. | |
* @param {String} password - Password to use to decrypt ciphertext. | |
* @returns {String} Decrypted plaintext. | |
* | |
* @example | |
* const plaintext = await aesGcmDecrypt(ciphertext, 'pw'); | |
* aesGcmDecrypt(ciphertext, 'pw').then(function(plaintext) { console.log(plaintext); }); | |
*/ | |
async function aesGcmDecrypt(ciphertext, password) { | |
const pwUtf8 = new TextEncoder().encode(password); // encode password as UTF-8 | |
const pwHash = await crypto.subtle.digest('SHA-256', pwUtf8); // hash the password | |
const iv = ciphertext.slice(0,24).match(/.{2}/g).map(byte => parseInt(byte, 16)); // get iv from ciphertext | |
const alg = { name: 'AES-GCM', iv: new Uint8Array(iv) }; // specify algorithm to use | |
const key = await crypto.subtle.importKey('raw', pwHash, alg, false, ['decrypt']); // use pw to generate key | |
const ctStr = atob(ciphertext.slice(24)); // decode base64 ciphertext | |
const ctUint8 = new Uint8Array(ctStr.match(/[\s\S]/g).map(ch => ch.charCodeAt(0))); // ciphertext as Uint8Array | |
// note: why doesn't ctUint8 = new TextEncoder().encode(ctStr) work? | |
const plainBuffer = await crypto.subtle.decrypt(alg, key, ctUint8); // decrypt ciphertext using key | |
const plaintext = new TextDecoder().decode(plainBuffer); // decode password from UTF-8 | |
return plaintext; // return the plaintext | |
} | |
//from https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder | |
if (typeof TextEncoder === "undefined") { | |
TextEncoder=function TextEncoder(){}; | |
TextEncoder.prototype.encode = function encode(str) { | |
"use strict"; | |
var Len = str.length, resPos = -1, resArr = new Uint8Array(Len * 3); | |
for (var point=0, nextcode=0, i = 0; i !== Len; ) { | |
point = str.charCodeAt(i), i += 1; | |
if (point >= 0xD800 && point <= 0xDBFF) { | |
if (i === Len) { | |
resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/; | |
resArr[resPos += 1] = 0xbd/*0b10111101*/; break; | |
} | |
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae | |
nextcode = str.charCodeAt(i); | |
if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) { | |
point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000; | |
i += 1; | |
if (point > 0xffff) { | |
resArr[resPos += 1] = (0x1e/*0b11110*/<<3) | (point>>>18); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>12)&0x3f/*0b00111111*/); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/); | |
continue; | |
} | |
} else { | |
resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/; | |
resArr[resPos += 1] = 0xbd/*0b10111101*/; continue; | |
} | |
} | |
if (point <= 0x007f) { | |
resArr[resPos += 1] = (0x0/*0b0*/<<7) | point; | |
} else if (point <= 0x07ff) { | |
resArr[resPos += 1] = (0x6/*0b110*/<<5) | (point>>>6); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/); | |
} else { | |
resArr[resPos += 1] = (0xe/*0b1110*/<<4) | (point>>>12); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/); | |
resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/); | |
} | |
} | |
resArr = new Uint8Array(resArr.buffer.slice(0, resPos+1)); | |
return resArr; | |
}; | |
TextEncoder.prototype.toString = function(){return "[object TextEncoder]"}; | |
if (Object.defineProperty) { | |
Object.defineProperty(TextEncoder.prototype,"encoding",{get:function(){if(Object.getPrototypeOf | |
(this)!==TextEncoder.prototype)throw TypeError("Illegal invocation");else return"utf-8"}}); | |
} else { | |
TextEncoder.prototype.encoding = "utf-8"; | |
} | |
if(typeof Symbol!=="undefined")TextEncoder.prototype[Symbol.toStringTag]="TextEncoder"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment