Skip to content

Commit 910f478

Browse files
committed
doc: improve jsdoc
1 parent fba05e6 commit 910f478

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

.github/workflows/master.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Node.js 24.x build
22

33
on:
44
push:
5-
branches: [master, feat/v3.0.1]
5+
branches: [master, release/**]
66

77
jobs:
88
build:

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [3.0.2] - 2025-10-18
6+
7+
- Improve JSDoc documentation
8+
- Remove last vitest reference from README.md
9+
10+
## [3.0.1] - 2025-09-28
11+
12+
- Remove references to vitest from README.md
13+
514
## [3.0.0] - 2025-09-21
615

716
- feat: minimum required version of Node.Js is 20.x

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ console.log(verify(plaintextPassword, sha256))
7171

7272
## Test
7373

74-
The tests are written with the built-in [node:assert](https://nodejs.org/api/assert.html) module, using the [vitest](https://vitest.dev/) test runner.
74+
The tests are written with the built-in [node:assert](https://nodejs.org/api/assert.html) module, and are run in the Node.Js test runner. The test runner didn't get good enough coverage reporting until v24, so that's the reason for the minimum required version of v24 for building and testing.
7575

7676
```sh
7777
$ npm test

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unixcrypt",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Node.js implementation of Unixcrypt, specifically SHA-256 and SHA-512",
55
"type": "module",
66
"exports": {

src/index.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,22 +325,42 @@ function bufferToBase64(buf: Buffer): string {
325325
}
326326

327327
/**
328-
* Create sha256 or sha512 crypt of plaintext password
329-
* @param plaintext The plaintext password
330-
* @param salt optional salt, for example "$6$salt" or "$6$rounds=10000$salt"
328+
* Create a SHA-256 or SHA-512 hash of a plaintext password using the unixcrypt format.
329+
*
330+
* @param plaintext - The password to encrypt
331+
* @param salt - Optional salt string in Unix crypt format. Examples:
332+
* - "$6$salt" - Use SHA-512 with default rounds
333+
* - "$6$rounds=10000$salt" - Use SHA-512 with 10000 rounds
334+
* - "$5$salt" - Use SHA-256 with default rounds
335+
* If omitted, generates SHA-512 hash with random salt
336+
* @returns The complete hash string in Unix crypt format
337+
* @example
338+
* // Generate SHA-512 hash with random salt
339+
* encrypt("mypassword")
340+
* // -> "$6$WHT0QXyF$LQv3c1yqBWVHxkd0LHAkC..."
341+
*
342+
* // Generate SHA-512 hash with specific salt and rounds
343+
* encrypt("mypassword", "$6$rounds=10000$saltvalue")
344+
* // -> "$6$rounds=10000$saltvalue$LQv3c1yq..."
331345
*/
332-
function encrypt(plaintext: string, salt?: string): string {
346+
export function encrypt(plaintext: string, salt?: string): string {
333347
const conf = parseSalt(salt)
334348
const hash = generateHash(plaintext, conf)
335349
return normalizeSalt(conf) + "$" + hash
336350
}
337351

338352
/**
339-
* Verify plaintext password against expected hash
340-
* @param plaintext The plaintext password
341-
* @param hash The expected hash
353+
* Verify a plaintext password against an existing unixcrypt hash.
354+
*
355+
* @param plaintext - The password to verify
356+
* @param hash - The complete hash string to verify against (including salt and rounds)
357+
* @returns True if the plaintext matches the hash, false otherwise
358+
* @example
359+
* // Verify password against hash
360+
* verify("mypassword", "$6$WHT0QXyF$LQv3c1yqBWVHxkd0LHAkC...")
361+
* // -> true or false
342362
*/
343-
function verify(plaintext: string, hash: string): boolean {
363+
export function verify(plaintext: string, hash: string): boolean {
344364
const salt = hash.slice(0, hash.lastIndexOf("$"))
345365
const computedHash = encrypt(plaintext, salt)
346366

@@ -349,5 +369,3 @@ function verify(plaintext: string, hash: string): boolean {
349369
Buffer.from(hash, "utf8"),
350370
)
351371
}
352-
353-
export { encrypt, verify }

0 commit comments

Comments
 (0)