@natsubate/secrets-token-generator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +21 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 natsubate
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @natsubate/secrets-token-generator [](https://www.npmjs.com/package/@natsubate/secrets-token-generator)
|
|
2
|
+
|
|
3
|
+
A lightweight utility for generating cryptographically secure random tokens in Node.js, similar to Python's secrets module.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @natsubate/secrets-token-generator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```TypeScript
|
|
14
|
+
import { tokenBytes, tokenHex, tokenUrlSafe } from "@natsubate/secrets-token-generator";
|
|
15
|
+
|
|
16
|
+
const tokenByBuffer = tokenBytes(32); // 32bytes secure random bytes
|
|
17
|
+
|
|
18
|
+
const tokenByHex = tokenHex(32); // 32bytes secure random HEX string
|
|
19
|
+
console.log(tokenByHex); // -> 7b6d3b21cd1a40b3baf2013c475247fa2c577bfef691e4661cce35fc846f328f
|
|
20
|
+
|
|
21
|
+
const tokenByUrlSafeBase64 = tokenUrlSafe(32); // 32bytes secure random by Base64URL
|
|
22
|
+
console.log(tokenByUrlSafeBase64); // -> 7H8lLtZ6A5C96rfpxtbsRiB2lmXj3ctukOM4kywRIwY
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
### tokenBytes(bytes: number): Buffer
|
|
28
|
+
|
|
29
|
+
Generates a random value of the specified byte count using cryptographically secure pseudo-random number generator (CSPRNG) and returns the buffer.
|
|
30
|
+
|
|
31
|
+
- **`bytes`**: `number` - Random bytes length (e.g., 32).
|
|
32
|
+
- **Returns**: `Buffer` - Raw bytes.
|
|
33
|
+
|
|
34
|
+
### tokenHex(bytes: number): string
|
|
35
|
+
|
|
36
|
+
Generates a random value of the specified byte length using CSPRNG and returns the HEX string.
|
|
37
|
+
|
|
38
|
+
- **`bytes`**: `number` - Random bytes length (e.g., 32).
|
|
39
|
+
- **Returns**: `string` - HEX encoded token.
|
|
40
|
+
|
|
41
|
+
### tokenUrlSafe(bytes: number): string
|
|
42
|
+
|
|
43
|
+
Generates a random value of the specified byte length using CSPRNG and returns a Base64 URL string.
|
|
44
|
+
|
|
45
|
+
- **`bytes`**: `number` - Random bytes length (e.g., 32).
|
|
46
|
+
- **Returns**: `string` - Base64URL encoded token.
|
|
47
|
+
|
|
48
|
+
## Supported Environments
|
|
49
|
+
|
|
50
|
+
* Node.js 18 or later
|
|
51
|
+
|
|
52
|
+
## TypeScript Type Definition
|
|
53
|
+
|
|
54
|
+
Included.
|
|
55
|
+
|
|
56
|
+
## Security Notice
|
|
57
|
+
|
|
58
|
+
* The implementation uses the Node.js standard module `crypto.randomBytes`.
|
|
59
|
+
* While it can be used for security-critical purposes such as CSRF tokens, session IDs, and API keys,
|
|
60
|
+
ensure the required entropy amount (byte length) is appropriately set according to your use case.
|
|
61
|
+
* Also consider the storage and transmission methods for generated tokens (e.g., using HTTPS, setting the Secure cookie flag).
|
|
62
|
+
|
|
63
|
+
## Notes
|
|
64
|
+
|
|
65
|
+
* All functions are synchronous.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
* MIT License
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// -*- coding: utf-8 -*-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.tokenUrlSafe = exports.tokenHex = exports.tokenBytes = void 0;
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const tokenBytes = (bytes) => {
|
|
7
|
+
if (bytes < 1) {
|
|
8
|
+
throw new Error("bytes must be positive");
|
|
9
|
+
}
|
|
10
|
+
;
|
|
11
|
+
return (0, node_crypto_1.randomBytes)(bytes);
|
|
12
|
+
};
|
|
13
|
+
exports.tokenBytes = tokenBytes;
|
|
14
|
+
const tokenHex = (bytes) => {
|
|
15
|
+
return (0, exports.tokenBytes)(bytes).toString("hex");
|
|
16
|
+
};
|
|
17
|
+
exports.tokenHex = tokenHex;
|
|
18
|
+
const tokenUrlSafe = (bytes) => {
|
|
19
|
+
return (0, exports.tokenBytes)(bytes).toString("base64url");
|
|
20
|
+
};
|
|
21
|
+
exports.tokenUrlSafe = tokenUrlSafe;
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@natsubate/secrets-token-generator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight utility for generating cryptographically secure random tokens in Node.js, similar to Python's secrets module.",
|
|
5
|
+
"repository": "https://github.com/natsubate/secrets-token-generator.git",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"author": "",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"secrets",
|
|
18
|
+
"crypto",
|
|
19
|
+
"token",
|
|
20
|
+
"CSPRNG",
|
|
21
|
+
"base64url"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^25.0.10",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
}
|
|
34
|
+
}
|