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
|
# File 'opal/stdlib/securerandom.rb', line 2
def self.hex(count = nil)
count ||= 16
count = count.to_int unless `typeof count === "number"`
raise ArgumentError, 'count of hex numbers must be positive' if count < 0
%x{
count = Math.floor(count);
var repeat = Math.floor(count / 6),
remain = count % 6,
remain_total = remain * 2,
string = '',
temp;
for (var i = 0; i < repeat; i++) {
// parseInt('ff'.repeat(6), 16) == 281474976710655
temp = Math.floor(Math.random() * 281474976710655).toString(16);
if (temp.length < 12) {
// account for leading zeros gone missing
temp = '0'.repeat(12 - temp.length) + temp;
}
string = string + temp;
}
if (remain > 0) {
temp = Math.floor(Math.random()*parseInt('ff'.repeat(remain), 16)).toString(16);
if (temp.length < remain_total) {
// account for leading zeros gone missing
temp = '0'.repeat(remain_total - temp.length) + temp;
}
string = string + temp;
}
return string;
}
end
|