For nodejs check if node-webcrypto-ossl is installed and use it

This commit is contained in:
Gilbert Consellado 2018-06-07 08:24:09 +08:00
parent 1a99f74b86
commit 99cd20c331

43
sea.js
View File

@ -161,20 +161,37 @@
} else { } else {
try{ try{
const crypto = require('crypto') const crypto = require('crypto')
//const WebCrypto = require('node-webcrypto-ossl') let nwOSSL = false
//const { subtle: ossl } = new WebCrypto({directory: 'key_storage'}) // ECDH try{
const { subtle } = require('@trust/webcrypto') // All but ECDH require.resolve('node-webcrypto-ossl')
const { TextEncoder, TextDecoder } = require('text-encoding') nwOSSL = true
Object.assign(api, { }catch(e){}
crypto, if(nwOSSL){
subtle, const WebCrypto = require('node-webcrypto-ossl')
//ossl, const { subtle: ossl } = new WebCrypto({directory: 'key_storage'}) // ECDH
TextEncoder, const { subtle } = require('@trust/webcrypto') // All but ECDH
TextDecoder, const { TextEncoder, TextDecoder } = require('text-encoding')
random: (len) => Buffer.from(crypto.randomBytes(len)) Object.assign(api, {
}) crypto,
subtle,
ossl,
TextEncoder,
TextDecoder,
random: (len) => Buffer.from(crypto.randomBytes(len))
})
} else {
const { subtle } = require('@trust/webcrypto') // All but ECDH
const { TextEncoder, TextDecoder } = require('text-encoding')
Object.assign(api, {
crypto,
subtle,
TextEncoder,
TextDecoder,
random: (len) => Buffer.from(crypto.randomBytes(len))
})
}
}catch(e){ }catch(e){
console.log("@trust/webcrypto and text-encoding are not included by default, you must add it to your package.json!"); console.log("@trust/webcrypto and text-encoding are not included by default, you must add it to your package.json! And if you are in nodejs you should include node-webcrypto-ossl.");
TRUST_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED; TRUST_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
} }
} }