Добавлен модуль sjcl для шифрования AES, т.к. WebCrypto API не работает с http, а только с https

This commit is contained in:
Book Pauk
2019-03-16 01:11:20 +07:00
parent a64687f64f
commit 9cbaf22270
5 changed files with 168 additions and 62 deletions

View File

@@ -1,70 +1,25 @@
import {Buffer} from 'safe-buffer';
import sjclWrapper from './sjclWrapper';
//не менять
const iv = Buffer.from('B6E2XejNh2dS');
let aesKeys = {};
const iv = 'B6E2XejNh2dS';
const salt = 'Liberama project is awesome';
export async function aesKeyFromPassword(password) {
return await window.crypto.subtle.importKey(
"raw", //only "raw" is allowed
Buffer.from(password), //your password
{
name: "PBKDF2",
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["deriveKey"] //can be any combination of "deriveKey" and "deriveBits"
).then((key) => {
return window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: Buffer.from('Liberama project is awesome'),//не менять
iterations: 1000,
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
key, //your key from generateKey or importKey
{ //the key type you want to create based on the derived bits
name: "AES-GCM", //can be any AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH", or "HMAC")
//the generateKey parameters for that type of algorithm
length: 256, //can be 128, 192, or 256
},
false, //whether the derived key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] //limited to the options in that algorithm's importKey
);
});
}
export async function aesEncrypt(data, password) {
if (!aesKeys[password])
aesKeys[password] = await aesKeyFromPassword(password);
const key = aesKeys[password];
return await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv
},
key, //from generateKey or importKey above
data //ArrayBuffer of data you want to encrypt
export function aesEncrypt(data, password) {
return sjclWrapper.codec.bytes.fromBits(
sjclWrapper.encryptArray(
password, sjclWrapper.codec.bytes.toBits(data), {iv, salt}
).ct
);
}
export async function aesDecrypt(data, password) {
if (!aesKeys[password])
aesKeys[password] = await aesKeyFromPassword(password);
const key = aesKeys[password];
return await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv
},
key, //from generateKey or importKey above
data //ArrayBuffer of the data
export function aesDecrypt(data, password) {
return sjclWrapper.codec.bytes.fromBits(
sjclWrapper.decryptArray(
password, {ct: sjclWrapper.codec.bytes.toBits(data)}, {iv, salt}
)
);
}
export async function sha256(data) {
return await crypto.subtle.digest("SHA-256", Buffer.from(data));
export function sha256(str) {
return sjclWrapper.codec.bytes.fromBits(sjclWrapper.hash.sha256.hash(str));
}