-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (46 loc) · 1.46 KB
/
index.ts
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
import * as Random from 'expo-random';
// @ts-ignore: no module declaration for sjcl
import * as sjcl from './sjcl-mini';
import { Buffer } from 'buffer';
async function prngAddEntropy(entropyBits = 1024) {
const bytes = await Random.getRandomBytesAsync(entropyBits / 8);
const buf = new Uint32Array(new Uint8Array(bytes).buffer);
sjcl.random.addEntropy(buf as any, entropyBits, 'Random.getRandomBytesAsync');
}
prngAddEntropy();
export function randomBytes(size: number): Buffer {
const ret = sjcl.random.randomWords(Math.ceil(size / 4));
// We are always aiming for at least level 9 readiness
if (sjcl.random.getProgress(9) === 1.0) {
prngAddEntropy();
}
return new Buffer(sjcl.codec.bytes.fromBits(ret));
}
export const rng = randomBytes;
export const pseudoRandomBytes = randomBytes;
export const prng = randomBytes;
interface Hash {
update(data: Buffer): void;
digest(): Buffer;
}
class Sha1Hash implements Hash {
private hash: any;
constructor() {
this.hash = new sjcl.hash.sha1();
}
public update(data: Buffer) {
const bytes = new Uint8Array(data);
this.hash.update(sjcl.codec.bytes.toBits(bytes));
}
public digest(): Buffer {
const ret = this.hash.finalize();
const bytes = sjcl.codec.bytes.fromBits(ret);
return new Buffer(bytes);
}
}
export function createHash(algorithm: string) {
if (algorithm === 'sha1') {
return new Sha1Hash();
}
throw new Error(`hash algorithm "${algorithm}" is not supported`);
}