generate-api-key
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -22,3 +22,3 @@ "use strict"; | ||
const getCryptoApiKey = (options) => { | ||
let totalBytes; | ||
var _a; | ||
let apiKey; | ||
@@ -32,22 +32,16 @@ // Get the options. | ||
const chance = new chance_1.default(); | ||
if (options.length) { | ||
totalBytes = Math.ceil(options.length / 2); | ||
} | ||
else { | ||
// Get a random number. | ||
const numVal = chance.natural({ min: options.min, max: options.max }); | ||
// Set the total bytes. | ||
totalBytes = Math.ceil(numVal / 2); | ||
} | ||
// Determine the length for the key. | ||
const length = (_a = options.length) !== null && _a !== void 0 ? _a : chance.natural({ | ||
min: options.min, | ||
max: options.max | ||
}); | ||
// Set the total bytes. | ||
const totalBytes = Math.ceil(length / 2); | ||
// Generate the API key. | ||
apiKey = (0, crypto_1.randomBytes)(totalBytes).toString('hex'); | ||
// Check the key length. | ||
if (options.length && (apiKey.length > options.length)) { | ||
const endIndex = apiKey.length - (apiKey.length - options.length); | ||
if (apiKey.length > length) { | ||
const endIndex = apiKey.length - (apiKey.length - length); | ||
apiKey = apiKey.slice(0, endIndex); | ||
} | ||
else if (apiKey.length > options.max) { | ||
const endIndex = apiKey.length - (apiKey.length - options.max); | ||
apiKey = apiKey.slice(0, endIndex); | ||
} | ||
return apiKey; | ||
@@ -74,3 +68,3 @@ }; | ||
min: options.min, | ||
max: options.min | ||
max: options.max | ||
}); | ||
@@ -77,0 +71,0 @@ // Generate the string. |
{ | ||
"name": "generate-api-key", | ||
"description": "A library for generating random API key/access tokens", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "./dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "./dist/index.d.ts", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
55005
472