@eyhn/crypto
Advanced tools
Comparing version 1.1.5 to 1.1.6
@@ -9,19 +9,12 @@ "use strict"; | ||
} | ||
else { | ||
return (ba) => { | ||
var l = ba.length; | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256); | ||
} | ||
return ba; | ||
}; | ||
} | ||
return (ba) => { | ||
var l = ba.length; | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256); | ||
} | ||
} | ||
else { | ||
// Node.JS | ||
const nodecrypto = require('crypto'); | ||
return (ba) => nodecrypto.randomFillSync(ba); | ||
} | ||
return ba; | ||
}; | ||
} | ||
exports.default = prng; | ||
//# sourceMappingURL=prng.js.map |
@@ -26,3 +26,3 @@ "use strict"; | ||
value = view[i].toString(16); | ||
result += (value.length === 1 ? '0' + value : value); | ||
result += value.padStart(2, '0'); | ||
} | ||
@@ -29,0 +29,0 @@ return result; |
{ | ||
"name": "@eyhn/crypto", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "lib/index.d.ts", |
import { TypedArray } from "../interface/TypedArray"; | ||
import * as crypto from 'crypto'; | ||
@@ -9,16 +8,12 @@ export default function prng(): (ba: TypedArray) => TypedArray { | ||
return (ba) => window.crypto.getRandomValues(ba); | ||
} else { | ||
return (ba) => { | ||
var l = ba.length | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256) | ||
} | ||
return ba; | ||
} | ||
} | ||
} else { | ||
// Node.JS | ||
const nodecrypto: typeof crypto = require('crypto'); | ||
return (ba) => nodecrypto.randomFillSync(ba); | ||
} | ||
return (ba) => { | ||
var l = ba.length | ||
while (l--) { | ||
ba[l] = Math.floor(Math.random() * 256) | ||
} | ||
return ba; | ||
} | ||
} |
export function hexToArrayBuffer(hex: string) { | ||
if (typeof hex !== 'string') { | ||
throw new TypeError('Expected input to be a string') | ||
throw new TypeError('Expected input to be a string'); | ||
} | ||
if ((hex.length % 2) !== 0) { | ||
throw new RangeError('Expected string to be an even number of characters') | ||
throw new RangeError('Expected string to be an even number of characters'); | ||
} | ||
var view = new Uint8Array(hex.length / 2) | ||
var view = new Uint8Array(hex.length / 2); | ||
for (var i = 0; i < hex.length; i += 2) { | ||
view[i / 2] = parseInt(hex.substring(i, i + 2), 16) | ||
view[i / 2] = parseInt(hex.substring(i, i + 2), 16); | ||
} | ||
return view.buffer | ||
return view.buffer; | ||
} | ||
@@ -21,15 +21,15 @@ | ||
if (typeof arrayBuffer !== 'object' || arrayBuffer === null || typeof arrayBuffer.byteLength !== 'number') { | ||
throw new TypeError('Expected input to be an ArrayBuffer') | ||
throw new TypeError('Expected input to be an ArrayBuffer'); | ||
} | ||
var view = new Uint8Array(arrayBuffer) | ||
var result = '' | ||
var value | ||
var view = new Uint8Array(arrayBuffer); | ||
var result = ''; | ||
var value; | ||
for (var i = 0; i < view.length; i++) { | ||
value = view[i].toString(16) | ||
result += (value.length === 1 ? '0' + value : value) | ||
value = view[i].toString(16); | ||
result += value.padStart(2, '0'); | ||
} | ||
return result | ||
return result; | ||
} |
@@ -191,2 +191,7 @@ import crypto = require('../lib/'); | ||
}) | ||
it('hex', () => { | ||
const hex = crypto.tools.arrayBufferToHex(new ArrayBuffer(128)); | ||
expect(hex.length).toEqual(128 * 2); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147627
1934