Merge pull request #487 from mhelander/sea

Sea
This commit is contained in:
Mark Nadal 2018-01-19 13:12:49 -08:00 committed by GitHub
commit 1d66aecdc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

12
sea.js
View File

@ -21,7 +21,9 @@
if(typeof window !== 'undefined'){ if(typeof window !== 'undefined'){
var wc = window.crypto || window.msCrypto; // STD or M$ var wc = window.crypto || window.msCrypto; // STD or M$
subtle = wc.subtle || wc.webkitSubtle; // STD or iSafari subtle = wc.subtle || wc.webkitSubtle; // STD or iSafari
getRandomBytes = function(len){ return wc.getRandomValues(Buffer.alloc(len)) }; getRandomBytes = function(len){
return Buffer.from(wc.getRandomValues(new Uint8Array(Buffer.alloc(len))));
};
TextEncoder = window.TextEncoder; TextEncoder = window.TextEncoder;
TextDecoder = window.TextDecoder; TextDecoder = window.TextDecoder;
sessionStorage = window.sessionStorage; sessionStorage = window.sessionStorage;
@ -34,7 +36,7 @@
var webcrypto = new WebCrypto({directory: 'key_storage'}); var webcrypto = new WebCrypto({directory: 'key_storage'});
subtleossl = webcrypto.subtle; subtleossl = webcrypto.subtle;
subtle = require('@trust/webcrypto').subtle; // All but ECDH subtle = require('@trust/webcrypto').subtle; // All but ECDH
getRandomBytes = function(len){ return crypto.randomBytes(len) }; getRandomBytes = function(len){ return Buffer.from(crypto.randomBytes(len)) };
TextEncoder = require('text-encoding').TextEncoder; TextEncoder = require('text-encoding').TextEncoder;
TextDecoder = require('text-encoding').TextDecoder; TextDecoder = require('text-encoding').TextDecoder;
// Let's have Storage for NodeJS / testing // Let's have Storage for NodeJS / testing
@ -539,7 +541,7 @@
.then(function(hashedKey){ .then(function(hashedKey){
return subtle.importKey( return subtle.importKey(
'raw', 'raw',
hashedKey, new Uint8Array(hashedKey),
'AES-CBC', 'AES-CBC',
false, false,
o o
@ -1192,7 +1194,7 @@
subtle.importKey('jwk', keystoecdsajwk(p), ecdsakeyprops, false, ['verify']) subtle.importKey('jwk', keystoecdsajwk(p), ecdsakeyprops, false, ['verify'])
.then(function(key){ .then(function(key){
sha256hash(m).then(function(mm){ sha256hash(m).then(function(mm){
subtle.verify(ecdsasignprops, key, Buffer.from(s, 'base64'), mm) subtle.verify(ecdsasignprops, key, new Uint8Array(Buffer.from(s, 'base64')), new Uint8Array(mm))
.then(function(v){ resolve(v) }) .then(function(v){ resolve(v) })
.catch(function(e){ Gun.log(e); reject(e) }); .catch(function(e){ Gun.log(e); reject(e) });
}); });
@ -1208,7 +1210,7 @@
m = (m.slice && m) || JSON.stringify(m); m = (m.slice && m) || JSON.stringify(m);
recallCryptoKey(p, s).then(function(aesKey){ recallCryptoKey(p, s).then(function(aesKey){
subtle.encrypt({ subtle.encrypt({
name: 'AES-CBC', iv: iv name: 'AES-CBC', iv: new Uint8Array(iv)
}, aesKey, new TextEncoder().encode(m)).then(function(ct){ }, aesKey, new TextEncoder().encode(m)).then(function(ct){
aesKey = getRandomBytes(32); aesKey = getRandomBytes(32);
r.ct = Buffer.from(ct, 'binary').toString('base64'); r.ct = Buffer.from(ct, 'binary').toString('base64');