GeezerVison is showing github.com — Restructure files · EntropyString/JavaScript@ccbbbaa
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +89Lines changed: 89 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const BITS_PER_BYTE = 8 | | 2 | +const { | | 3 | + abs, ceil, floor, log2 | | 4 | +} = Math | | 5 | + | | 6 | +const gcd = (a, b) => { | | 7 | + let la = a | | 8 | + let lb = b | | 9 | + while (lb !== 0) { | | 10 | + [la, lb] = [lb, la % lb] | | 11 | + } | | 12 | + return abs(la) | | 13 | +} | | 14 | +const lcm = (a, b) => (a / gcd(a, b)) * b | | 15 | + | | 16 | +const genNdxFn = (bitsPerChar) => { | | 17 | + // If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number | | 18 | + // of chars per byte. | | 19 | + if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) { | | 20 | + return (chunk, slice, bytes) => { | | 21 | + const lShift = bitsPerChar | | 22 | + const rShift = BITS_PER_BYTE - bitsPerChar | | 23 | + return ((bytes[chunk] << (lShift * slice)) & 0xff) >> rShift | | 24 | + } | | 25 | + } | | 26 | + | | 27 | + // Otherwise, while slicing off bits per char, we can possibly straddle two | | 28 | + // of bytes, so a more work is involved | | 29 | + const slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE | | 30 | + return (chunk, slice, bytes) => { | | 31 | + const bNum = chunk * slicesPerChunk | | 32 | + | | 33 | + const offset = (slice * bitsPerChar) / BITS_PER_BYTE | | 34 | + const lOffset = floor(offset) | | 35 | + const rOffset = ceil(offset) | | 36 | + | | 37 | + const rShift = BITS_PER_BYTE - bitsPerChar | | 38 | + const lShift = (slice * bitsPerChar) % BITS_PER_BYTE | | 39 | + | | 40 | + let ndx = ((bytes[bNum + lOffset] << lShift) & 0xff) >> rShift | | 41 | + | | 42 | + const r1Bits = (rOffset + 1) * BITS_PER_BYTE | | 43 | + const s1Bits = (slice + 1) * bitsPerChar | | 44 | + | | 45 | + const rShiftIt = (r1Bits - s1Bits) % BITS_PER_BYTE | | 46 | + if (rShift < rShiftIt) { | | 47 | + ndx += bytes[bNum + rOffset] >> rShiftIt | | 48 | + } | | 49 | + return ndx | | 50 | + } | | 51 | +} | | 52 | + | | 53 | +class CharSet { | | 54 | + constructor(chars) { | | 55 | + if (!(typeof chars === 'string' || chars instanceof String)) { | | 56 | + throw new Error('Invalid chars: Must be string') | | 57 | + } | | 58 | + | | 59 | + const { length } = chars | | 60 | + if (![2, 4, 8, 16, 32, 64].includes(length)) { | | 61 | + throw new Error('Invalid char count: must be one of 2,4,8,16,32,64') | | 62 | + } | | 63 | + | | 64 | + // Ensure no repeated characters | | 65 | + for (let i = 0; i < length; i += 1) { | | 66 | + const c = chars.charAt(i) | | 67 | + for (let j = i + 1; j < length; j += 1) { | | 68 | + if (c === chars.charAt(j)) { | | 69 | + throw new Error('Characters not unique') | | 70 | + } | | 71 | + } | | 72 | + } | | 73 | + | | 74 | + this.chars = chars | | 75 | + this.bitsPerChar = floor(log2(length)) | | 76 | + this.length = length | | 77 | + this.ndxFn = genNdxFn(this.bitsPerChar) | | 78 | + this.charsPerChunk = lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar | | 79 | + } | | 80 | + | | 81 | + bytesNeeded(bitLen) { | | 82 | + const count = ceil(bitLen / this.bitsPerChar) | | 83 | + return ceil((count * this.bitsPerChar) / BITS_PER_BYTE) | | 84 | + } | | 85 | +} | | 86 | + | | 87 | +module.exports = { | | 88 | + CharSet | | 89 | +} |
Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const csprngBytes = (count) => window.crypto.getRandomValues(new Uint8Array(count)) | | 2 | + | | 3 | +module.exports = { | | 4 | + csprngBytes | | 5 | +} |
Collapse file Copy file name to clipboardExpand all lines: csprng-bytes.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | const Crypto = require('crypto') | 2 | 2 |
| 3 | | -const csprngBytes = count => Buffer.from(Crypto.randomBytes(count)) | | 3 | +const csprngBytes = (count) => Buffer.from(Crypto.randomBytes(count)) | 4 | 4 |
| 5 | 5 | module.exports = { | 6 | 6 | csprngBytes | |
Collapse file Copy file name to clipboard +110Lines changed: 110 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +"use strict"; | | 2 | + | | 3 | +var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | | 4 | + | | 5 | +var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | | 6 | + | | 7 | +var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | | 8 | + | | 9 | +var BITS_PER_BYTE = 8; | | 10 | +var abs = Math.abs, | | 11 | + ceil = Math.ceil, | | 12 | + floor = Math.floor, | | 13 | + log2 = Math.log2; | | 14 | + | | 15 | +var gcd = function gcd(a, b) { | | 16 | + var la = a; | | 17 | + var lb = b; | | 18 | + | | 19 | + while (lb !== 0) { | | 20 | + var _ref = [lb, la % lb]; | | 21 | + la = _ref[0]; | | 22 | + lb = _ref[1]; | | 23 | + } | | 24 | + | | 25 | + return abs(la); | | 26 | +}; | | 27 | + | | 28 | +var lcm = function lcm(a, b) { | | 29 | + return a / gcd(a, b) * b; | | 30 | +}; | | 31 | + | | 32 | +var genNdxFn = function genNdxFn(bitsPerChar) { | | 33 | + // If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number | | 34 | + // of chars per byte. | | 35 | + if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) { | | 36 | + return function (chunk, slice, bytes) { | | 37 | + var lShift = bitsPerChar; | | 38 | + var rShift = BITS_PER_BYTE - bitsPerChar; | | 39 | + return (bytes[chunk] << lShift * slice & 0xff) >> rShift; | | 40 | + }; | | 41 | + } // Otherwise, while slicing off bits per char, we can possibly straddle two | | 42 | + // of bytes, so a more work is involved | | 43 | + | | 44 | + | | 45 | + var slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE; | | 46 | + return function (chunk, slice, bytes) { | | 47 | + var bNum = chunk * slicesPerChunk; | | 48 | + var offset = slice * bitsPerChar / BITS_PER_BYTE; | | 49 | + var lOffset = floor(offset); | | 50 | + var rOffset = ceil(offset); | | 51 | + var rShift = BITS_PER_BYTE - bitsPerChar; | | 52 | + var lShift = slice * bitsPerChar % BITS_PER_BYTE; | | 53 | + var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift; | | 54 | + var r1Bits = (rOffset + 1) * BITS_PER_BYTE; | | 55 | + var s1Bits = (slice + 1) * bitsPerChar; | | 56 | + var rShiftIt = (r1Bits - s1Bits) % BITS_PER_BYTE; | | 57 | + | | 58 | + if (rShift < rShiftIt) { | | 59 | + ndx += bytes[bNum + rOffset] >> rShiftIt; | | 60 | + } | | 61 | + | | 62 | + return ndx; | | 63 | + }; | | 64 | +}; | | 65 | + | | 66 | +var CharSet = /*#__PURE__*/function () { | | 67 | + function CharSet(chars) { | | 68 | + (0, _classCallCheck2["default"])(this, CharSet); | | 69 | + | | 70 | + if (!(typeof chars === 'string' || chars instanceof String)) { | | 71 | + throw new Error('Invalid chars: Must be string'); | | 72 | + } | | 73 | + | | 74 | + var length = chars.length; | | 75 | + | | 76 | + if (![2, 4, 8, 16, 32, 64].includes(length)) { | | 77 | + throw new Error('Invalid char count: must be one of 2,4,8,16,32,64'); | | 78 | + } // Ensure no repeated characters | | 79 | + | | 80 | + | | 81 | + for (var i = 0; i < length; i += 1) { | | 82 | + var c = chars.charAt(i); | | 83 | + | | 84 | + for (var j = i + 1; j < length; j += 1) { | | 85 | + if (c === chars.charAt(j)) { | | 86 | + throw new Error('Characters not unique'); | | 87 | + } | | 88 | + } | | 89 | + } | | 90 | + | | 91 | + this.chars = chars; | | 92 | + this.bitsPerChar = floor(log2(length)); | | 93 | + this.length = length; | | 94 | + this.ndxFn = genNdxFn(this.bitsPerChar); | | 95 | + this.charsPerChunk = lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar; | | 96 | + } | | 97 | + | | 98 | + (0, _createClass2["default"])(CharSet, [{ | | 99 | + key: "bytesNeeded", | | 100 | + value: function bytesNeeded(bitLen) { | | 101 | + var count = ceil(bitLen / this.bitsPerChar); | | 102 | + return ceil(count * this.bitsPerChar / BITS_PER_BYTE); | | 103 | + } | | 104 | + }]); | | 105 | + return CharSet; | | 106 | +}(); | | 107 | + | | 108 | +module.exports = { | | 109 | + CharSet: CharSet | | 110 | +}; |
Collapse file Copy file name to clipboardExpand all lines: dist/entropy-string.js +7-103Lines changed: 7 additions & 103 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,114 +6,20 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl | 6 | 6 |
| 7 | 7 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | 8 | 8 |
| 9 | | -var _require = require('./lib/csprng-bytes'), | | 9 | +var _require = require('./csprng-bytes'), | 10 | 10 | csprngBytes = _require.csprngBytes; | 11 | 11 |
| 12 | | -var _require2 = require('./lib/prng-bytes'), | | 12 | +var _require2 = require('./prng-bytes'), | 13 | 13 | prngBytes = _require2.prngBytes; | 14 | 14 |
| | 15 | +var _require3 = require('./charSet'), | | 16 | + CharSet = _require3.CharSet; | | 17 | + | 15 | 18 | var BITS_PER_BYTE = 8; | 16 | | -var abs = Math.abs, | 17 | | - ceil = Math.ceil, | | 19 | +var ceil = Math.ceil, | 18 | 20 | floor = Math.floor, | 19 | 21 | log2 = Math.log2, | 20 | 22 | round = Math.round; | 21 | | - | 22 | | -var gcd = function gcd(a, b) { | 23 | | - var la = a; | 24 | | - var lb = b; | 25 | | - | 26 | | - while (lb !== 0) { | 27 | | - var _ref = [lb, la % lb]; | 28 | | - la = _ref[0]; | 29 | | - lb = _ref[1]; | 30 | | - } | 31 | | - | 32 | | - return abs(la); | 33 | | -}; | 34 | | - | 35 | | -var lcm = function lcm(a, b) { | 36 | | - return a / gcd(a, b) * b; | 37 | | -}; | 38 | | - | 39 | | -var genNdxFn = function genNdxFn(bitsPerChar) { | 40 | | - // If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number | 41 | | - // of chars per byte. | 42 | | - if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) { | 43 | | - return function (chunk, slice, bytes) { | 44 | | - var lShift = bitsPerChar; | 45 | | - var rShift = BITS_PER_BYTE - bitsPerChar; | 46 | | - return (bytes[chunk] << lShift * slice & 0xff) >> rShift; | 47 | | - }; | 48 | | - } // Otherwise, while slicing off bits per char, we can possibly straddle two | 49 | | - // of bytes, so a more work is involved | 50 | | - | 51 | | - | 52 | | - var slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE; | 53 | | - return function (chunk, slice, bytes) { | 54 | | - var bNum = chunk * slicesPerChunk; | 55 | | - var offset = slice * bitsPerChar / BITS_PER_BYTE; | 56 | | - var lOffset = floor(offset); | 57 | | - var rOffset = ceil(offset); | 58 | | - var rShift = BITS_PER_BYTE - bitsPerChar; | 59 | | - var lShift = slice * bitsPerChar % BITS_PER_BYTE; | 60 | | - var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift; | 61 | | - var r1Bits = (rOffset + 1) * BITS_PER_BYTE; | 62 | | - var s1Bits = (slice + 1) * bitsPerChar; | 63 | | - var rShiftIt = (r1Bits - s1Bits) % BITS_PER_BYTE; | 64 | | - | 65 | | - if (rShift < rShiftIt) { | 66 | | - ndx += bytes[bNum + rOffset] >> rShiftIt; | 67 | | - } | 68 | | - | 69 | | - return ndx; | 70 | | - }; | 71 | | -}; | 72 | | - | 73 | | -var CharSet = | 74 | | -/*#__PURE__*/ | 75 | | -function () { | 76 | | - function CharSet(chars) { | 77 | | - (0, _classCallCheck2["default"])(this, CharSet); | 78 | | - | 79 | | - if (!(typeof chars === 'string' || chars instanceof String)) { | 80 | | - throw new Error('Invalid chars: Must be string'); | 81 | | - } | 82 | | - | 83 | | - var length = chars.length; | 84 | | - | 85 | | - if (![2, 4, 8, 16, 32, 64].includes(length)) { | 86 | | - throw new Error('Invalid char count: must be one of 2,4,8,16,32,64'); | 87 | | - } // Ensure no repeated characters | 88 | | - | 89 | | - | 90 | | - for (var i = 0; i < length; i += 1) { | 91 | | - var c = chars.charAt(i); | 92 | | - | 93 | | - for (var j = i + 1; j < length; j += 1) { | 94 | | - if (c === chars.charAt(j)) { | 95 | | - throw new Error('Characters not unique'); | 96 | | - } | 97 | | - } | 98 | | - } | 99 | | - | 100 | | - this.chars = chars; | 101 | | - this.bitsPerChar = floor(log2(length)); | 102 | | - this.length = length; | 103 | | - this.ndxFn = genNdxFn(this.bitsPerChar); | 104 | | - this.charsPerChunk = lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar; | 105 | | - } | 106 | | - | 107 | | - (0, _createClass2["default"])(CharSet, [{ | 108 | | - key: "bytesNeeded", | 109 | | - value: function bytesNeeded(bitLen) { | 110 | | - var count = ceil(bitLen / this.bitsPerChar); | 111 | | - return ceil(count * this.bitsPerChar / BITS_PER_BYTE); | 112 | | - } | 113 | | - }]); | 114 | | - return CharSet; | 115 | | -}(); | 116 | | - | 117 | 23 | var charset64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'); | 118 | 24 | var charset32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT'); | 119 | 25 | var charset16 = new CharSet('0123456789abcdef'); | @@ -178,9 +84,7 @@ var entropyBits = function entropyBits(total, risk) { | 178 | 84 | return N + log2(risk) - 1; | 179 | 85 | }; | 180 | 86 |
| 181 | | -var Entropy = | 182 | | -/*#__PURE__*/ | 183 | | -function () { | | 87 | +var Entropy = /*#__PURE__*/function () { | 184 | 88 | function Entropy() { | 185 | 89 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | 186 | 90 | bits: 128, | |
Collapse file Copy file name to clipboardExpand all lines: dist/lib/csprng-bytes-browser.js -9Lines changed: 0 additions & 9 deletions Collapse file Copy file name to clipboardExpand all lines: dist/lib/csprng-bytes.js -11Lines changed: 0 additions & 11 deletions
|