mirror of
https://github.com/amark/gun.git
synced 2025-06-05 13:46:43 +00:00
25 lines
911 B
JavaScript
25 lines
911 B
JavaScript
|
|
|
|
let subtle
|
|
let subtleossl
|
|
let getRandomBytes
|
|
let crypto
|
|
var Buffer = require('./buffer');
|
|
var wc;
|
|
var api = {};
|
|
|
|
if (typeof __webpack_require__ === 'function' || typeof window !== 'undefined') {
|
|
api.crypto = wc = window.crypto || window.msCrypto // STD or M$
|
|
api.subtle = subtle = wc.subtle || wc.webkitSubtle // STD or iSafari
|
|
api.random = getRandomBytes = (len) => Buffer.from(wc.getRandomValues(new Uint8Array(Buffer.alloc(len))))
|
|
} else {
|
|
api.crypto = crypto = require('crypto')
|
|
const WebCrypto = require('node-webcrypto-ossl')
|
|
const webcrypto = new WebCrypto({directory: 'key_storage'})
|
|
api.ossl = subtleossl = webcrypto.subtle
|
|
api.subtle = subtle = require('@trust/webcrypto').subtle // All but ECDH
|
|
api.random = getRandomBytes = (len) => Buffer.from(crypto.randomBytes(len))
|
|
}
|
|
|
|
module.exports = api;
|
|
|