wordhashx 0.0.0-stage → 0.1.0-beta
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/README.md +44 -2
- package/index.d.ts +8 -0
- package/index.js +25 -0
- package/package.json +22 -4
- package/pieces.js +394548 -0
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# WordHashX
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Hashes made of word pieces from the full English Wiktionary, built on [SpeckHashX](https://www.npmjs.com/package/speckhashx).
|
|
4
|
+
Try it at [wordhashx.pages.dev](https://wordhashx.pages.dev).
|
|
5
|
+
|
|
6
|
+
> **Beta.** Outputs are subject to change.
|
|
7
|
+
|
|
8
|
+
## Use
|
|
9
|
+
|
|
10
|
+
`npm i wordhashx@beta` (works in Node, Bun and browsers):
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { whx, pieces } from 'wordhashx'
|
|
14
|
+
|
|
15
|
+
whx('abc', 4) // 'FojtikRoynishElismsExpugn'
|
|
16
|
+
pieces('abc', 4) // ['fojtik', 'roynish', 'elisms', 'expugn']
|
|
17
|
+
whx('abc', 1) // 'Vpirna'
|
|
18
|
+
whx(bytes, 14) // 14 pieces
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The number is k, how many pieces the hash has, from 1 to 64. That hash is called WHX-k.
|
|
22
|
+
Each k is its own hash: WHX-4 and WHX-5 of the same message are unrelated.
|
|
23
|
+
|
|
24
|
+
Strings are hashed as UTF-8, and an `ArrayBuffer`, typed array or `DataView` as its bytes.
|
|
25
|
+
|
|
26
|
+
## Security
|
|
27
|
+
|
|
28
|
+
Each piece adds 18.5 bits. Security stops growing at WHX-14, where SpeckHashX's 32 bytes of state are the limit.
|
|
29
|
+
|
|
30
|
+
| | Bits | Tries to find two messages with the same hash | Tries to find a message for a given hash |
|
|
31
|
+
|---|---|---|---|
|
|
32
|
+
| WHX-1 | 18.5 | 2<sup>9.2</sup> | 2<sup>18.5</sup> |
|
|
33
|
+
| WHX-4 | 74.3 | 2<sup>37.1</sup> | 2<sup>74.3</sup> |
|
|
34
|
+
| WHX-7 | 130.1 | 2<sup>65</sup> | 2<sup>130.1</sup> |
|
|
35
|
+
| WHX-10 | 185.8 | 2<sup>92.9</sup> | 2<sup>185.8</sup> |
|
|
36
|
+
| WHX-13 | 241.6 | 2<sup>120.8</sup> | 2<sup>241.6</sup> |
|
|
37
|
+
| WHX-14 to 64 | 260.2 and up | 2<sup>128</sup> | 2<sup>256</sup> |
|
|
38
|
+
|
|
39
|
+
How it works, every k and test vectors: [docs/whx.md](https://github.com/multipleof4/WordHashX/blob/main/docs/whx.md).
|
|
40
|
+
|
|
41
|
+
## Wiktionary
|
|
42
|
+
|
|
43
|
+
The 394,548 pieces come from [Wiktionary](https://en.wiktionary.org/), by its contributors,
|
|
44
|
+
under [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) and the [GFDL](https://www.gnu.org/licenses/fdl-1.3.html).
|
|
45
|
+
How they are made: [rules](https://github.com/multipleof4/WordHashX/blob/main/docs/rules.md).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A string is hashed as UTF-8; a buffer, typed array or DataView as its bytes. Anything else throws. */
|
|
2
|
+
type Data = string | ArrayBuffer | ArrayBufferView
|
|
3
|
+
|
|
4
|
+
/** WHX-k of `d`: k pieces, each capitalized, joined. `k` is an integer from 1 to 64. */
|
|
5
|
+
export function whx(d: Data, k: number): string
|
|
6
|
+
|
|
7
|
+
/** The k pieces of WHX-k of `d`, lowercase, in order. `k` is an integer from 1 to 64. */
|
|
8
|
+
export function pieces(d: Data, k: number): string[]
|
package/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { shx } from 'speckhashx'
|
|
2
|
+
import list from './pieces.js'
|
|
3
|
+
|
|
4
|
+
// The pieces in order: digit d of a hash is piece d.
|
|
5
|
+
const P = list.split('\n'), N = BigInt(P.length)
|
|
6
|
+
|
|
7
|
+
// Bytes to ask SpeckHashX for k pieces: the fewest n with 256^n >= N^k * 2^128.
|
|
8
|
+
const bytes = k => {
|
|
9
|
+
const t = N ** BigInt(k) << 128n
|
|
10
|
+
let n = 0
|
|
11
|
+
while (1n << BigInt(8 * n) < t) n++
|
|
12
|
+
return n
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// The k pieces of WHX-k of d, lowercase, in order.
|
|
16
|
+
export const pieces = (d, k) => {
|
|
17
|
+
if (!Number.isInteger(k) || k < 1 || k > 64) throw RangeError('k must be an integer from 1 to 64')
|
|
18
|
+
const o = []
|
|
19
|
+
let x = BigInt('0x' + shx(d, bytes(k)).hex()) % N ** BigInt(k)
|
|
20
|
+
while (k--) o.unshift(P[Number(x % N)]), x /= N
|
|
21
|
+
return o
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// WHX-k of d: its pieces, each capitalized, joined.
|
|
25
|
+
export const whx = (d, k) => pieces(d, k).map(p => p[0].toUpperCase() + p.slice(1)).join('')
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wordhashx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta",
|
|
4
|
+
"description": "Hashes made of word pieces from the English Wiktionary, built on SpeckHashX. Beta: outputs are subject to change.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"files": ["index.js", "index.d.ts", "pieces.js"],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "bun build.js",
|
|
11
|
+
"test": "bun test",
|
|
12
|
+
"prepack": "bun build.js"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"speckhashx": "1.3.0"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/multipleof4/WordHashX.git",
|
|
20
|
+
"directory": "pkg/npm"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://wordhashx.pages.dev",
|
|
23
|
+
"keywords": ["hash", "words", "wordlist", "speckhashx", "wiktionary"]
|
|
24
|
+
}
|