This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+40
View File
@@ -0,0 +1,40 @@
'use strict';
const crypto = require('crypto');
const MAX_UINT31 = 2147483647; // 2**31 - 1
/**
* Generates a cryptographically secure random string to be used as a password
* salt, using Node's built-in crypto.randomBytes() function.
* @public
* @param {number} length The length of the salt to be generated.
* Must be an integer within the range (`0` <= `length` <= `2^31-1`).
* @return {Promise.<Buffer>} The salt as a Buffer.
*/
function gensalt(length) {
// Salt length Validation
if (typeof length !== 'number' || !Number.isInteger(length)) {
return Promise.reject(
new TypeError("The 'length' parameter must be an integer")
);
}
if (length < 0 || length > MAX_UINT31) {
return Promise.reject(
new TypeError(
`The 'length' parameter must be in the range (0 <= length <= ${MAX_UINT31})`
)
);
}
return new Promise((resolve, reject) => {
crypto.randomBytes(length, (err, salt) => {
/* istanbul ignore next */
if (err) reject(err);
else resolve(salt);
});
});
}
module.exports = gensalt;
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018-2020 Simone Primarosa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+59
View File
@@ -0,0 +1,59 @@
{
"name": "@kdf/salt",
"version": "2.0.1",
"description": "Crypto secure salt generator",
"license": "MIT",
"homepage": "https://github.com/simonepri/node-kdf/tree/master/packages/salt#readme",
"repository": {
"type": "git",
"url": "https://github.com/simonepri/node-kdf.git",
"directory": "packages/salt"
},
"bugs": {
"url": "https://github.com/simonepri/node-kdf/issues",
"email": "simonepri@outlook.com"
},
"author": "Simone Primarosa <simonepri@outlook.com> (https://simoneprimarosa.com)",
"contributors": [
"Simone Primarosa <simonepri@outlook.com> (https://simoneprimarosa.com)"
],
"keywords": [
"kdf",
"salt",
"crypt",
"passwords",
"hashing",
"password",
"crypto"
],
"main": "index.js",
"files": [
"index.js"
],
"engines": {
"node": ">=10"
},
"scripts": {
"lint": "xo",
"test": "nyc ava",
"update": "npx npm-check -u"
},
"devDependencies": {
"ava": "^3.9.0",
"nyc": "^15.1.0",
"xo": "~0.27.2"
},
"ava": {
"verbose": true
},
"nyc": {
"reporter": [
"lcovonly",
"text"
]
},
"xo": {
"prettier": true,
"space": true
}
}
+141
View File
@@ -0,0 +1,141 @@
<h1 align="center">
<b>kdf-salt</b>
</h1>
<p align="center">
<!-- Version - npm -->
<a href="https://www.npmjs.com/package/@kdf/salt">
<img src="https://img.shields.io/npm/v/@kdf/salt.svg" alt="Latest version on npm" />
</a>
<!-- Downloads - npm -->
<a href="https://npm-stat.com/charts.html?package=@kdf/salt">
<img src="https://img.shields.io/npm/dt/@kdf/salt.svg" alt="Downloads on npm" />
</a>
<!-- License - MIT -->
<a href="https://github.com/simonepri/node-kdf/blob/master/packages/salt/license">
<img src="https://img.shields.io/github/license/simonepri/node-kdf.svg" alt="Project license" />
</a>
<br/>
<!-- Lint -->
<a href="https://github.com/simonepri/node-kdf/actions?query=workflow:salt-lint+branch:master">
<img src="https://github.com/simonepri/node-kdf/workflows/salt-lint/badge.svg?branch=master" alt="Lint status" />
</a>
<!-- Test - macOS -->
<a href="https://github.com/simonepri/node-kdf/actions?query=workflow:salt-test-macos+branch:master">
<img src="https://github.com/simonepri/node-kdf/workflows/salt-test-macos/badge.svg?branch=master" alt="Test macOS status" />
</a>
<!-- Test - Ubuntu -->
<a href="https://github.com/simonepri/node-kdf/actions?query=workflow:salt-test-ubuntu+branch:master">
<img src="https://github.com/simonepri/node-kdf/workflows/salt-test-ubuntu/badge.svg?branch=master" alt="Test Ubuntu status" />
</a>
<!-- Test - Windows -->
<a href="https://github.com/simonepri/node-kdf/actions?query=workflow:salt-test-windows+branch:master">
<img src="https://github.com/simonepri/node-kdf/workflows/salt-test-windows/badge.svg?branch=master" alt="Test Windows status" />
</a>
<br/>
<!-- Coverage - Codecov -->
<a href="https://codecov.io/gh/simonepri/node-kdf/tree/master/packages/salt">
<img src="https://codecov.io/gh/simonepri/node-kdf/branch/master/graph/badge.svg?flag=salt" alt="Codecov Coverage report" />
</a>
<!-- DM - Snyk -->
<a href="https://snyk.io/test/github/simonepri/node-kdf?targetFile=packages/salt/package.json">
<img src="https://snyk.io/test/github/simonepri/node-kdf/badge.svg?targetFile=packages/salt/package.json" alt="Known Vulnerabilities" />
</a>
<!-- DM - David -->
<a href="https://david-dm.org/simonepri/node-kdf?path=packages/salt">
<img src="https://david-dm.org/simonepri/node-kdf/status.svg?path=packages/salt" alt="Dependency Status" />
</a>
<br/>
<!-- Code Style - XO-Prettier -->
<a href="https://github.com/xojs/xo">
<img src="https://img.shields.io/badge/code_style-XO+Prettier-5ed9c7.svg" alt="XO Code Style used" />
</a>
<!-- Test Runner - AVA -->
<a href="https://github.com/avajs/ava">
<img src="https://img.shields.io/badge/test_runner-AVA-fb3170.svg" alt="AVA Test Runner used" />
</a>
<!-- Test Coverage - Istanbul -->
<a href="https://github.com/istanbuljs/nyc">
<img src="https://img.shields.io/badge/test_coverage-NYC-fec606.svg" alt="Istanbul Test Coverage used" />
</a>
<!-- Init - ni -->
<a href="https://github.com/simonepri/ni">
<img src="https://img.shields.io/badge/initialized_with-ni-e74c3c.svg" alt="NI Scaffolding System used" />
</a>
<!-- Release - np -->
<a href="https://github.com/sindresorhus/np">
<img src="https://img.shields.io/badge/released_with-np-6c8784.svg" alt="NP Release System used" />
</a>
</p>
<p align="center">
🎲 Crypto secure salt generator
<br/>
<sub>
Coded with ❤️ by <a href="#authors">Simone Primarosa</a>.
</sub>
</p>
## Install
```bash
npm install --save @kdf/salt
```
## Usage
```js
const gensalt = require('@kdf/salt');
gensalt(16).then(salt => {
console.log(salt);
// => <Buffer 70 2c 6e 1c c1 07 2e 26 dc 82 52 4e 1b cf 1d 23>
console.log(salt.toString('base64'));
// => cCxuHMEHLibcglJOG88dIw==
console.log(salt.toString('hex'));
// => 702c6e1cc1072e26dc82524e1bcf1d23
})
```
## API
<a name="gensalt"></a>
### gensalt(length) ⇒ <code>Promise.&lt;Buffer&gt;</code>
Generates a cryptographically secure random string to be used as a password salt
, using Node's built-in crypto.randomBytes() function.
**Kind**: global function
**Returns**: <code>Promise.&lt;Buffer&gt;</code> - The salt as a Buffer.
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| length | <code>number</code> | The length of the salt to be generated. Must be an integer within the range (`0` <= `length` <= `2^31-1`). |
## Contributing
Contributions are REALLY welcome and if you find a security flaw in this code, PLEASE [report it][new issue].
## Authors
- **Simone Primarosa** - *Github* ([@simonepri][github:simonepri]) • *Twitter* ([@simoneprimarosa][twitter:simoneprimarosa])
See also the list of [contributors][contributors] who participated in this project.
## License
This project is licensed under the MIT License - see the [license][license] file for details.
<!-- Links -->
[new issue]: https://github.com/simonepri/node-kdf/issues/new
[contributors]: https://github.com/simonepri/node-kdf/contributors
[license]: https://github.com/simonepri/node-kdf/blob/master/packages/salt/license
[github:simonepri]: https://github.com/simonepri
[twitter:simoneprimarosa]: http://twitter.com/intent/user?screen_name=simoneprimarosa