|
| 1 | +// Copyright 2015 runtime.js project authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | +var isaac = require('./isaac-wrapper'); |
| 17 | +var EntropySource = require('./entropy-source'); |
| 18 | +var sources = require('./sources'); |
| 19 | +var typeutils = require('typeutils'); |
| 20 | +var getDefaultSource = sources.getDefaultSource; |
| 21 | +require('./js-random-source'); |
| 22 | + |
| 23 | +exports.EntropySource = EntropySource; |
| 24 | +exports.addEntropySource = sources.addEntropySource; |
| 25 | + |
| 26 | +/** |
| 27 | + * Request random bytes from the best available entropy source. This |
| 28 | + * function is asynchronous bacause it might take some time to gather |
| 29 | + * enough entropy from the source. This might use /dev/random as a backend |
| 30 | + * in KVM and might be extremely slow depending on the buffer size and |
| 31 | + * the amount of available entropy. |
| 32 | + * |
| 33 | + * @param {number|Uint8Array} value Number of bytes to request or buffer to fill |
| 34 | + * @param {function} cb Callback with the resulting Uint8Array buffer argument |
| 35 | + */ |
| 36 | +exports.getTrueRandomValues = function(value, cb) { |
| 37 | + var u8 = null; |
| 38 | + if (typeutils.isNumber(value)) { |
| 39 | + u8 = new Uint8Array(value); |
| 40 | + } |
| 41 | + |
| 42 | + if (value instanceof Uint8Array) { |
| 43 | + u8 = value; |
| 44 | + } |
| 45 | + |
| 46 | + if (!u8) { |
| 47 | + throw new Error('getTrueRandomValues: argument 0 is not a number or Uint8Array'); |
| 48 | + } |
| 49 | + |
| 50 | + if (u8.length === 0) { |
| 51 | + throw new Error('getTrueRandomValues: buffer length must be greater than 0'); |
| 52 | + } |
| 53 | + |
| 54 | + if (!typeutils.isFunction(cb)) { |
| 55 | + throw new Error('getTrueRandomValues: argument 1 is not a function'); |
| 56 | + } |
| 57 | + |
| 58 | + var defaultSource = getDefaultSource(); |
| 59 | + if (!defaultSource) { |
| 60 | + throw new Error('getTrueRandomValues: no entropy source available'); |
| 61 | + } |
| 62 | + |
| 63 | + defaultSource.getBytes(u8, function() { |
| 64 | + isaac.seed(u8); |
| 65 | + cb(u8); |
| 66 | + }); |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Request random bytes from the CSPRNG seeded with enough entropy. |
| 71 | + * |
| 72 | + * @param {number|Uint8Array} value Number of bytes to request or buffer to fill |
| 73 | + * @return {Uint8Array} Buffer filled with random data |
| 74 | + */ |
| 75 | +exports.getRandomValues = function(value) { |
| 76 | + var u8 = null; |
| 77 | + if (typeutils.isNumber(value)) { |
| 78 | + u8 = new Uint8Array(value); |
| 79 | + } |
| 80 | + |
| 81 | + if (value instanceof Uint8Array) { |
| 82 | + u8 = value; |
| 83 | + } |
| 84 | + |
| 85 | + if (!u8) { |
| 86 | + throw new Error('getRandomValues: argument 0 is not a number or Uint8Array'); |
| 87 | + } |
| 88 | + |
| 89 | + if (u8.length === 0) { |
| 90 | + throw new Error('getRandomValues: buffer length must be greater than 0'); |
| 91 | + } |
| 92 | + |
| 93 | + for (var i = 0; i < u8.length; i++) { |
| 94 | + u8[i] = isaac.getByte(); |
| 95 | + } |
| 96 | + |
| 97 | + return u8; |
| 98 | +}; |
0 commit comments