Skip to content

Commit 8e86c0f

Browse files
authored
Merge pull request #1939 from cosmos/no-wasm-argon2
Implement Argon2id using @noble/hashes
2 parents 2988ab9 + eda7b83 commit 8e86c0f

File tree

6 files changed

+14
-41
lines changed

6 files changed

+14
-41
lines changed

.pnp.cjs

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/cache/hash-wasm-npm-4.12.0-d6bb202626-6cb9505531.zip

Lines changed: 0 additions & 3 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ and this project adheres to
1111
- all: Drop support for Node.js < 22.
1212
- @cosmjs/crypto: Upgrade dependencies @noble/ciphers, @noble/curves,
1313
@noble/hashes and @scure/bip39 to v2. ([#1935])
14+
- @cosmjs/crypto: Use pure-JS implementation of Argon2id from @noble/hashes.
15+
([#1938])
1416

1517
[#1935]: https://github.com/cosmos/cosmjs/pull/1935
18+
[#1938]: https://github.com/cosmos/cosmjs/issues/1938
1619

1720
## [0.38.0] - 2025-12-30
1821

packages/crypto/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
"@noble/ciphers": "^2.1.1",
4747
"@noble/curves": "^2.0.1",
4848
"@noble/hashes": "^2.0.1",
49-
"@scure/bip39": "^2.0.1",
50-
"hash-wasm": "^4.12.0"
49+
"@scure/bip39": "^2.0.1"
5150
},
5251
"devDependencies": {
5352
"@istanbuljs/nyc-config-typescript": "^1.0.1",

packages/crypto/src/argon2.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { assert, isNonNullObject } from "@cosmjs/utils";
2-
import { type IArgon2Options, argon2id } from "hash-wasm";
1+
import { isNonNullObject } from "@cosmjs/utils";
2+
import { argon2id, ArgonOpts } from "@noble/hashes/argon2.js";
33

44
export interface Argon2idOptions {
55
/** Output length in bytes */
@@ -29,28 +29,24 @@ export function isArgon2idOptions(thing: unknown): thing is Argon2idOptions {
2929
}
3030

3131
export class Argon2id {
32+
// This is async for historic reasons. If we switch to a Wasm implementation or get argon2 in WebCrypto,
33+
// this is needed again.
3234
public static async execute(
3335
password: string,
3436
salt: Uint8Array,
3537
options: Argon2idOptions,
3638
): Promise<Uint8Array> {
37-
const opts: IArgon2Options = {
38-
password,
39-
salt,
40-
outputType: "binary",
41-
iterations: options.opsLimit,
42-
memorySize: options.memLimitKib,
43-
parallelism: 1, // no parallelism allowed, just like libsodium
44-
hashLength: options.outputLength,
39+
const opts: ArgonOpts = {
40+
t: options.opsLimit, // Time cost, iterations count
41+
m: options.memLimitKib, // Memory cost (in KB)
42+
p: 1, // parallelism
43+
dkLen: options.outputLength, // Desired number of returned bytes
4544
};
4645

4746
if (salt.length !== 16) {
4847
throw new Error(`Got invalid salt length ${salt.length}. Must be 16.`);
4948
}
5049

51-
const hash = await argon2id(opts);
52-
// guaranteed by outputType: 'binary'
53-
assert(typeof hash !== "string");
54-
return hash;
50+
return argon2id(password, salt, opts);
5551
}
5652
}

yarn.lock

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ __metadata:
289289
"@types/karma-jasmine-html-reporter": "npm:^1"
290290
"@types/node": "npm:*"
291291
glob: "npm:^13.0.0"
292-
hash-wasm: "npm:^4.12.0"
293292
jasmine: "npm:^4"
294293
jasmine-spec-reporter: "npm:^6"
295294
karma: "npm:^6.3.14"
@@ -4421,13 +4420,6 @@ __metadata:
44214420
languageName: node
44224421
linkType: hard
44234422

4424-
"hash-wasm@npm:^4.12.0":
4425-
version: 4.12.0
4426-
resolution: "hash-wasm@npm:4.12.0"
4427-
checksum: 10c0/6cb95055319810b6f673de755289960683a9831807690795f0e8918b2fd7e6ae5b82a9dc47cbace171cf2814b412ef68c315a0ead0b4d96bd3e56a05e4bde136
4428-
languageName: node
4429-
linkType: hard
4430-
44314423
"hasha@npm:^5.0.0":
44324424
version: 5.2.2
44334425
resolution: "hasha@npm:5.2.2"

0 commit comments

Comments
 (0)