Switched to webcrypto

This commit is contained in:
Marcus Bernales 2019-09-14 12:56:48 -07:00
parent 2f0d2e3ade
commit 71e0be33f7
6 changed files with 173 additions and 137 deletions

View File

@ -54,14 +54,14 @@
"ws": "~>7.1.0" "ws": "~>7.1.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"text-encoding": "^0.7.0", "@peculiar/webcrypto": "^1.0.19",
"node-webcrypto-ossl": "^1.0.47", "emailjs": "^2.2.0",
"emailjs": "^2.2.0" "text-encoding": "^0.7.0"
}, },
"devDependencies": { "devDependencies": {
"aws-sdk": ">=2.153.0", "aws-sdk": ">=2.153.0",
"ip": "^1.1.5", "ip": "^1.1.5",
"mocha": "^5.2.0", "mocha": "^6.2.0",
"panic-manager": "^1.2.0", "panic-manager": "^1.2.0",
"panic-server": "^1.1.1", "panic-server": "^1.1.1",
"uglify-js": ">=2.8.22" "uglify-js": ">=2.8.22"

12
sea.js
View File

@ -64,6 +64,11 @@
(_, i) => String.fromCharCode(this[ i + start]) (_, i) => String.fromCharCode(this[ i + start])
).join('') ).join('')
} }
function btoa(b) {
return new Buffer(b).toString('base64');
};
if (enc === 'base64') { if (enc === 'base64') {
return btoa(this) return btoa(this)
} }
@ -91,6 +96,9 @@
} }
const input = arguments[0] const input = arguments[0]
let buf let buf
function atob(a) {
return new Buffer(a, 'base64').toString('binary');
};
if (typeof input === 'string') { if (typeof input === 'string') {
const enc = arguments[1] || 'utf8' const enc = arguments[1] || 'utf8'
if (enc === 'hex') { if (enc === 'hex') {
@ -174,7 +182,7 @@
random: (len) => Buffer.from(crypto.randomBytes(len)) random: (len) => Buffer.from(crypto.randomBytes(len))
}); });
//try{ //try{
const WebCrypto = USE('node-webcrypto-ossl', 1); const { Crypto: WebCrypto } = USE('@peculiar/webcrypto', 1);
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
//}catch(e){ //}catch(e){
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed."); //console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
@ -304,7 +312,7 @@
//SEA.pair = async (data, proof, cb) => { try { //SEA.pair = async (data, proof, cb) => { try {
SEA.pair = SEA.pair || (async (cb, opt) => { try { SEA.pair = SEA.pair || (async (cb, opt) => { try {
console.log('SHIM', shim)
var ecdhSubtle = shim.ossl || shim.subtle; var ecdhSubtle = shim.ossl || shim.subtle;
// First: ECDSA keys for signing/verifying... // First: ECDSA keys for signing/verifying...
var sa = await shim.subtle.generateKey(S.ecdsa.pair, true, [ 'sign', 'verify' ]) var sa = await shim.subtle.generateKey(S.ecdsa.pair, true, [ 'sign', 'verify' ])

View File

@ -1,24 +1,29 @@
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
function SeaArray() {}
Object.assign(SeaArray, { from: Array.from });
SeaArray.prototype = Object.create(Array.prototype);
SeaArray.prototype.toString = function(enc, start, end) {
enc = enc || "utf8";
start = start || 0;
const length = this.length;
if (enc === "hex") {
const buf = new Uint8Array(this);
return [...Array(((end && end + 1) || length) - start).keys()]
.map(i => buf[i + start].toString(16).padStart(2, "0"))
.join("");
}
if (enc === "utf8") {
return Array.from({ length: (end || length) - start }, (_, i) =>
String.fromCharCode(this[i + start])
).join("");
}
// This is Array extended to have .toString(['utf8'|'hex'|'base64']) function btoa(b) {
function SeaArray() {} return new Buffer(b).toString("base64");
Object.assign(SeaArray, { from: Array.from }) }
SeaArray.prototype = Object.create(Array.prototype)
SeaArray.prototype.toString = function(enc, start, end) { enc = enc || 'utf8'; start = start || 0;
const length = this.length
if (enc === 'hex') {
const buf = new Uint8Array(this)
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
}
if (enc === 'utf8') {
return Array.from(
{ length: (end || length) - start },
(_, i) => String.fromCharCode(this[ i + start])
).join('')
}
if (enc === 'base64') {
return btoa(this)
}
}
module.exports = SeaArray;
if (enc === "base64") {
return btoa(this);
}
};
module.exports = SeaArray;

View File

@ -1,78 +1,95 @@
// This is Buffer implementation used in SEA. Functionality is mostly
// This is Buffer implementation used in SEA. Functionality is mostly // compatible with NodeJS 'safe-buffer' and is used for encoding conversions
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions // between binary and 'hex' | 'utf8' | 'base64'
// between binary and 'hex' | 'utf8' | 'base64' // See documentation and validation for safe implementation in:
// See documentation and validation for safe implementation in: // https://github.com/feross/safe-buffer#update
// https://github.com/feross/safe-buffer#update var SeaArray = require("./array");
var SeaArray = require('./array'); function SafeBuffer(...props) {
function SafeBuffer(...props) { console.warn("new SafeBuffer() is depreciated, please use SafeBuffer.from()");
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()') return SafeBuffer.from(...props);
return SafeBuffer.from(...props) }
SafeBuffer.prototype = Object.create(Array.prototype);
Object.assign(SafeBuffer, {
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
from() {
if (!Object.keys(arguments).length) {
throw new TypeError(
"First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."
);
} }
SafeBuffer.prototype = Object.create(Array.prototype) const input = arguments[0];
Object.assign(SafeBuffer, { let buf;
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64' function atob(a) {
from() { return new Buffer(a, "base64").toString("binary");
if (!Object.keys(arguments).length) { }
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') if (typeof input === "string") {
const enc = arguments[1] || "utf8";
if (enc === "hex") {
const bytes = input
.match(/([\da-fA-F]{2})/g)
.map(byte => parseInt(byte, 16));
if (!bytes || !bytes.length) {
throw new TypeError("Invalid first argument for type 'hex'.");
} }
const input = arguments[0] buf = SeaArray.from(bytes);
let buf } else if (enc === "utf8") {
if (typeof input === 'string') { const length = input.length;
const enc = arguments[1] || 'utf8' const words = new Uint16Array(length);
if (enc === 'hex') { Array.from(
const bytes = input.match(/([\da-fA-F]{2})/g) { length: length },
.map((byte) => parseInt(byte, 16)) (_, i) => (words[i] = input.charCodeAt(i))
if (!bytes || !bytes.length) { );
throw new TypeError('Invalid first argument for type \'hex\'.') buf = SeaArray.from(words);
} } else if (enc === "base64") {
buf = SeaArray.from(bytes) const dec = atob(input);
} else if (enc === 'utf8') { const length = dec.length;
const length = input.length const bytes = new Uint8Array(length);
const words = new Uint16Array(length) Array.from(
Array.from({ length: length }, (_, i) => words[i] = input.charCodeAt(i)) { length: length },
buf = SeaArray.from(words) (_, i) => (bytes[i] = dec.charCodeAt(i))
} else if (enc === 'base64') { );
const dec = atob(input) buf = SeaArray.from(bytes);
const length = dec.length } else if (enc === "binary") {
const bytes = new Uint8Array(length) buf = SeaArray.from(input);
Array.from({ length: length }, (_, i) => bytes[i] = dec.charCodeAt(i)) } else {
buf = SeaArray.from(bytes) console.info("SafeBuffer.from unknown encoding: " + enc);
} else if (enc === 'binary') {
buf = SeaArray.from(input)
} else {
console.info('SafeBuffer.from unknown encoding: '+enc)
}
return buf
}
const byteLength = input.byteLength // what is going on here? FOR MARTTI
const length = input.byteLength ? input.byteLength : input.length
if (length) {
let buf
if (input instanceof ArrayBuffer) {
buf = new Uint8Array(input)
}
return SeaArray.from(buf || input)
}
},
// This is 'safe-buffer.alloc' sans encoding support
alloc(length, fill = 0 /*, enc*/ ) {
return SeaArray.from(new Uint8Array(Array.from({ length: length }, () => fill)))
},
// This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
allocUnsafe(length) {
return SeaArray.from(new Uint8Array(Array.from({ length : length })))
},
// This puts together array of array like members
concat(arr) { // octet array
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
}
return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), []))
} }
}) return buf;
SafeBuffer.prototype.from = SafeBuffer.from }
SafeBuffer.prototype.toString = SeaArray.prototype.toString const byteLength = input.byteLength; // what is going on here? FOR MARTTI
const length = input.byteLength ? input.byteLength : input.length;
module.exports = SafeBuffer; if (length) {
let buf;
if (input instanceof ArrayBuffer) {
buf = new Uint8Array(input);
}
return SeaArray.from(buf || input);
}
},
// This is 'safe-buffer.alloc' sans encoding support
alloc(length, fill = 0 /*, enc*/) {
return SeaArray.from(
new Uint8Array(Array.from({ length: length }, () => fill))
);
},
// This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
allocUnsafe(length) {
return SeaArray.from(new Uint8Array(Array.from({ length: length })));
},
// This puts together array of array like members
concat(arr) {
// octet array
if (!Array.isArray(arr)) {
throw new TypeError(
"First argument must be Array containing ArrayBuffer or Uint8Array instances."
);
}
return SeaArray.from(
arr.reduce((ret, item) => ret.concat(Array.from(item)), [])
);
}
});
SafeBuffer.prototype.from = SafeBuffer.from;
SafeBuffer.prototype.toString = SeaArray.prototype.toString;
module.exports = SafeBuffer;

View File

@ -1,36 +1,39 @@
const SEA = require("./root");
const Buffer = require("./buffer");
const api = { Buffer: Buffer };
var o = {};
const SEA = require('./root') if (SEA.window) {
const Buffer = require('./buffer') api.crypto = window.crypto || window.msCrypto;
const api = {Buffer: Buffer} api.subtle = (api.crypto || o).subtle || (api.crypto || o).webkitSubtle;
var o = {}; api.TextEncoder = window.TextEncoder;
api.TextDecoder = window.TextDecoder;
if(SEA.window){ api.random = len =>
api.crypto = window.crypto || window.msCrypto; Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))));
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle; }
api.TextEncoder = window.TextEncoder; if (!api.crypto) {
api.TextDecoder = window.TextDecoder; try {
api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len)))) var crypto = require("crypto", 1);
} const { TextEncoder, TextDecoder } = require("text-encoding", 1);
if(!api.crypto){try{ Object.assign(api, {
var crypto = require('crypto', 1); crypto,
const { TextEncoder, TextDecoder } = require('text-encoding', 1) //subtle,
Object.assign(api, { TextEncoder,
crypto, TextDecoder,
//subtle, random: len => Buffer.from(crypto.randomBytes(len))
TextEncoder, });
TextDecoder, //try{
random: (len) => Buffer.from(crypto.randomBytes(len)) const { Crypto: WebCrypto } = require("@peculiar/webcrypto", 1);
}); api.ossl = api.subtle = new WebCrypto({ directory: "ossl" }).subtle; // ECDH
//try{ //}catch(e){
const WebCrypto = require('node-webcrypto-ossl', 1); //console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH //}
//}catch(e){ } catch (e) {
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed."); console.log(
//} "node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!"
}catch(e){ );
console.log("node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!"); OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED; }
}} }
module.exports = api
module.exports = api;

View File

@ -1,5 +1,8 @@
var root; var root;
var Gun; var Gun;
function atob(a) {
return new Buffer(a, 'base64').toString('binary');
};
(function(){ (function(){
var env; var env;
if(typeof global !== 'undefined'){ env = global } if(typeof global !== 'undefined'){ env = global }