Skip to content

Commit 741ced3

Browse files
feat: argon2 in node.js and react native wrapper (#67)
* feat: argon2 in node.js and react native wrapper Signed-off-by: Berend Sliedrecht <berend@animo.id> * chore: update to v0.4.6 Signed-off-by: Berend Sliedrecht <berend@animo.id> --------- Signed-off-by: Berend Sliedrecht <berend@animo.id>
1 parent 69547ce commit 741ced3

File tree

17 files changed

+132
-29
lines changed

17 files changed

+132
-29
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ jobs:
3232
- name: Check Style
3333
run: pnpm style:check
3434

35-
- name: Check Types
36-
run: pnpm types:check
37-
3835
- name: Compile
3936
run: pnpm build
4037

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"style:check": "biome check --unsafe .",
1414
"style:fix": "pnpm style:check --write",
15-
"types:check": "pnpm -r check-types",
15+
"types:check": "pnpm -r types:check",
1616
"build": "pnpm -r build",
1717
"clean": "pnpm -r clean",
1818
"test": "node --import tsx --test packages/**/tests/*.test.ts",

packages/askar-nodejs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"typescript": "catalog:"
3838
},
3939
"binary": {
40-
"version": "v0.4.5",
40+
"version": "v0.4.6",
4141
"host": "https://github.com/openwallet-foundation/askar/releases/download",
4242
"packageName": "library-{platform}-{arch}.tar.gz"
4343
},

packages/askar-nodejs/src/NodeJSAskar.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
AeadParamsOptions,
3+
Argon2DerivePasswordOptions,
34
Askar,
45
AskarErrorObject,
56
EncryptedBuffer,
@@ -263,6 +264,21 @@ export class NodeJSAskar implements Askar {
263264
this.handleError(errorCode)
264265
}
265266

267+
public argon2DerivePassword(options: Argon2DerivePasswordOptions) {
268+
const { parameters, password, salt } = serializeArguments(options)
269+
270+
const ret = allocateSecretBuffer()
271+
272+
const errorCode = this.nativeAskar.askar_argon2_derive_password(parameters, password, salt, ret)
273+
274+
this.handleError(errorCode)
275+
const byteBuffer = handleReturnPointer<ByteBufferType>(ret)
276+
const bufferArray = new Uint8Array(Buffer.from(secretBufferToBuffer(byteBuffer)))
277+
this.nativeAskar.askar_buffer_free(byteBuffer)
278+
279+
return bufferArray
280+
}
281+
266282
public entryListCount(options: EntryListCountOptions): number {
267283
const { entryListHandle } = serializeArguments(options)
268284
const ret = allocateInt32Buffer()

packages/askar-nodejs/src/library/bindings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export const nativeBindings = {
3636
askar_set_default_logger: [FFI_ERROR_CODE, []],
3737
askar_set_max_log_level: [FFI_ERROR_CODE, [FFI_INT32]],
3838

39+
askar_argon2_derive_password: [FFI_ERROR_CODE, [FFI_INT8, ByteBufferStruct, ByteBufferStruct, SecretBufferStructPtr]],
40+
3941
askar_entry_list_count: [FFI_ERROR_CODE, [FFI_ENTRY_LIST_HANDLE, FFI_INT32_PTR]],
4042
askar_entry_list_free: [FFI_VOID, [FFI_ENTRY_LIST_HANDLE]],
4143
askar_entry_list_get_category: [FFI_ERROR_CODE, [FFI_ENTRY_LIST_HANDLE, FFI_INT32, FFI_STRING_PTR]],
0 Bytes
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ok } from 'node:assert'
2+
import { before, describe, test } from 'node:test'
3+
import { Argon2, Argon2Parameters } from '@openwallet-foundation/askar-shared'
4+
import { setup } from './utils'
5+
6+
describe('Argon2', () => {
7+
before(setup)
8+
9+
test('derive password', () => {
10+
const password = 'my password'
11+
const salt = 'long enough salt'
12+
13+
const passwordBytes = Uint8Array.from(Buffer.from(password))
14+
const saltBytes = Uint8Array.from(Buffer.from(salt))
15+
16+
const derivedPassword = Argon2.derivePassword(Argon2Parameters.Interactive, passwordBytes, saltBytes)
17+
18+
ok(
19+
Buffer.from(derivedPassword).toString('hex') ===
20+
'9ef87bcf828c46c0136a0d1d9e391d713f75b327c6dc190455bd36c1bae33259'
21+
)
22+
})
23+
})

packages/askar-react-native/cpp/HostObject.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ FunctionMap AskarTurboModuleHostObject::functionMapping(jsi::Runtime &rt) {
1515
fMap.insert(
1616
std::make_tuple("setDefaultLogger", &askar::setDefaultLogger));
1717

18+
fMap.insert(
19+
std::make_tuple("argon2DerivePassword", &askar::argon2DerivePassword));
20+
1821
fMap.insert(std::make_tuple("storeCopyTo", &askar::storeCopyTo));
1922
fMap.insert(std::make_tuple("storeOpen", &askar::storeOpen));
2023
fMap.insert(

packages/askar-react-native/cpp/askar.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ jsi::Value setDefaultLogger(jsi::Runtime &rt, jsi::Object options) {
2222
return createReturnValue(rt, code, nullptr);
2323
}
2424

25+
jsi::Value argon2DerivePassword(jsi::Runtime &rt, jsi::Object options) {
26+
auto parameters =
27+
jsiToValue<int8_t>(rt, options, "parameters");
28+
auto password =
29+
jsiToValue<ByteBuffer>(rt, options, "password");
30+
auto salt =
31+
jsiToValue<ByteBuffer>(rt, options, "salt");
32+
33+
SecretBuffer out;
34+
35+
ErrorCode code = askar_argon2_derive_password(parameters, password, salt, &out);
36+
37+
auto ret = createReturnValue(rt, code, &out);
38+
askar_buffer_free(out);
39+
return ret;
40+
}
41+
2542
jsi::Value entryListCount(jsi::Runtime &rt, jsi::Object options) {
2643
auto entryListHandle =
2744
jsiToValue<EntryListHandle>(rt, options, "entryListHandle");

packages/askar-react-native/cpp/askar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jsi::Value setDefaultLogger(jsi::Runtime &rt, jsi::Object options);
2020
// jsi::Value setMaxLogLevel(jsi::Runtime &rt, jsi::Object options);
2121
// jsi::Value clearCustomLogger(jsi::Runtime &rt, jsi::Object options);
2222

23+
jsi::Value argon2DerivePassword(jsi::Runtime &rt, jsi::Object options);
24+
2325
jsi::Value entryListCount(jsi::Runtime &rt, jsi::Object options);
2426
jsi::Value entryListFree(jsi::Runtime &rt, jsi::Object options);
2527
jsi::Value entryListGetCategory(jsi::Runtime &rt, jsi::Object options);

0 commit comments

Comments
 (0)