forked from RandstormBTC/randstorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecureRandom1.js
183 lines (155 loc) · 5 KB
/
SecureRandom1.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Generates a private key based on Math.random() with 2 slightly different implementations.
function SecureRandom3() {
var SEED_TIME_VALUE = 1294200190000;
this.pool = null;
this.pptr = 0;
this.poolSize = 256;
this.seedTime = function () {
this.seedInt(SEED_TIME_VALUE);
};
this.seedInt = function (x) {
this.pool[this.pptr++] ^= x & 255;
this.pool[this.pptr++] ^= (x >> 8) & 255;
this.pool[this.pptr++] ^= (x >> 16) & 255;
this.pool[this.pptr++] ^= (x >> 24) & 255;
if (this.pptr >= this.poolSize) this.pptr -= this.poolSize;
};
this.getByte = function () {
if (this.state == null) {
this.seedTime();
this.state = this.ArcFour();
this.state.init(this.pool);
for (this.pptr = 0; this.pptr < this.pool.length; ++this.pptr)
this.pool[this.pptr] = 0;
this.pptr = 0;
}
return this.state.next();
};
// Initialize the pool with junk if needed.
if (this.pool == null) {
this.pool = new Array(this.poolSize);
this.pptr = 0;
var t;
while (this.pptr < this.poolSize) {
t = Math.floor(65536 * Math.random());
this.pool[this.pptr++] = t >>> 8;
this.pool[this.pptr++] = t & 255;
}
this.pptr = 0;
this.seedTime();
}
}
SecureRandom3.prototype.ArcFour = function () {
function ArcFour() {
this.i = 0;
this.j = 0;
this.S = new Array(256);
}
function ARC4init(key) {
var i, j, t;
for (i = 0; i < 256; ++i)
this.S[i] = i;
j = 0;
for (i = 0; i < 256; ++i) {
j = (j + this.S[i] + key[i % key.length]) & 255;
t = this.S[i];
this.S[i] = this.S[j];
this.S[j] = t;
}
this.i = 0;
this.j = 0;
}
function ARC4next() {
var t;
this.i = (this.i + 1) & 255;
this.j = (this.j + this.S[this.i]) & 255;
t = this.S[this.i];
this.S[this.i] = this.S[this.j];
this.S[this.j] = t;
return this.S[(t + this.S[this.i]) & 255];
}
ArcFour.prototype.init = ARC4init;
ArcFour.prototype.next = ARC4next;
return new ArcFour();
};
var SecureRandom2 = function () {
var SEED_TIME_VALUE = 1294200190000;
this.pool;
this.pptr;
this.poolSize = 256;
this.state;
// Mix in the fixed value (1294200190000) as the "current time"
this.seedTime = function () {
this.seedInt(SEED_TIME_VALUE);
};
this.seedInt = function (x) {
this.pool[this.pptr++] ^= x & 255;
this.pool[this.pptr++] ^= (x >> 8) & 255;
this.pool[this.pptr++] ^= (x >> 16) & 255;
this.pool[this.pptr++] ^= (x >> 24) & 255;
if (this.pptr >= this.poolSize) this.pptr -= this.poolSize;
};
// Arcfour is a PRNG
this.ArcFour = function () {
function Arcfour() {
this.i = 0;
this.j = 0;
this.S = new Array();
}
// Initialize arcfour context from key, an array of ints, each from [0..255]
function ARC4init(key) {
var i, j, t;
for (i = 0; i < 256; ++i)
this.S[i] = i;
j = 0;
for (i = 0; i < 256; ++i) {
j = (j + this.S[i] + key[i % key.length]) & 255;
t = this.S[i];
this.S[i] = this.S[j];
this.S[j] = t;
}
this.i = 0;
this.j = 0;
}
function ARC4next() {
var t;
this.i = (this.i + 1) & 255;
this.j = (this.j + this.S[this.i]) & 255;
t = this.S[this.i];
this.S[this.i] = this.S[this.j];
this.S[this.j] = t;
return this.S[(t + this.S[this.i]) & 255];
}
Arcfour.prototype.init = ARC4init;
Arcfour.prototype.next = ARC4next;
return new Arcfour();
};
// Initialize the pool with junk if needed.
if (this.pool == null) {
this.pool = new Array();
this.pptr = 0;
var t;
while (this.pptr < this.poolSize) {
t = Math.floor(65536 * Math.random());
this.pool[this.pptr++] = t >>> 8;
this.pool[this.pptr++] = t & 255;
}
this.pptr = 0;
}
};
// SecureRandom3
var random3 = new SecureRandom3();
var privateKeyBytes3 = new Uint8Array(32);
for (var k = 0; k < privateKeyBytes3.length; k++) {
privateKeyBytes3[k] = random3.getByte();
}
var privateKeyHex3 = Array.from(privateKeyBytes3).map(b => ('0' + b.toString(16)).slice(-2)).join('');
console.log('SecureRandom1:', privateKeyHex3);
// SecureRandom2
var random2 = new SecureRandom2();
var privateKeyBytes2 = new Uint8Array(32);
for (var j = 0; j < privateKeyBytes2.length; j++) {
privateKeyBytes2[j] = random2.pool[j];
}
var privateKeyHex2 = Array.from(privateKeyBytes2).map(b => ('0' + b.toString(16)).slice(-2)).join('');
console.log('SecureRandom2:', privateKeyHex2);