-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.js
59 lines (52 loc) · 1.24 KB
/
crypto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { randomBytes } = require("react-native-randombytes");
exports.randomBytes =
exports.rng =
exports.pseudoRandomBytes =
exports.prng =
randomBytes;
// polyfill for webcrypto
exports.webcrypto = {
getRandomValues: function(arr) {
let orig = arr;
if (arr.byteLength !== arr.length) {
// Get access to the underlying raw bytes
arr = new Uint8Array(arr.buffer);
}
const bytes = randomBytes(arr.length);
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes[i];
}
return orig;
},
};
exports.createHash = exports.Hash = require("create-hash");
exports.createHmac = exports.Hmac = require("create-hmac");
const hashes = [
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
"md5",
"rmd160",
].concat(Object.keys(require("browserify-sign/algos")));
exports.getHashes = function() {
return hashes;
};
exports.createECDH = require("create-ecdh");
// assign from other modules
Object.assign(
exports,
require("pbkdf2"),
require("browserify-cipher"),
require("diffie-hellman"),
require("browserify-sign"),
require("public-encrypt"),
require("randomfill")
);
// implement window crypto
if (typeof window === "object") {
if (!window.crypto) {
window.crypto = exports.webcrypto;
}
}