@sd-jwt/crypto-browser 0.3.2-next.65 → 0.3.2-next.67

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/dist/index.js CHANGED
@@ -51,8 +51,8 @@ var generateSalt = (length) => {
51
51
  if (length <= 0) {
52
52
  return "";
53
53
  }
54
- const array = new Uint8Array(length);
55
- globalThis.crypto.getRandomValues(array);
54
+ const array = new Uint8Array(length / 2);
55
+ window.crypto.getRandomValues(array);
56
56
  const salt = Array.from(
57
57
  array,
58
58
  (byte) => byte.toString(16).padStart(2, "0")
package/dist/index.mjs CHANGED
@@ -24,8 +24,8 @@ var generateSalt = (length) => {
24
24
  if (length <= 0) {
25
25
  return "";
26
26
  }
27
- const array = new Uint8Array(length);
28
- globalThis.crypto.getRandomValues(array);
27
+ const array = new Uint8Array(length / 2);
28
+ window.crypto.getRandomValues(array);
29
29
  const salt = Array.from(
30
30
  array,
31
31
  (byte) => byte.toString(16).padStart(2, "0")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sd-jwt/crypto-browser",
3
- "version": "0.3.2-next.65+622c8dc",
3
+ "version": "0.3.2-next.67+63256a4",
4
4
  "description": "sd-jwt draft 7 implementation in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -49,5 +49,5 @@
49
49
  "esm"
50
50
  ]
51
51
  },
52
- "gitHead": "622c8dc2001c4ca6cc3e6da75f7516d4c70a73db"
52
+ "gitHead": "63256a4a820f2f824ee73988f58447770321bf18"
53
53
  }
package/src/crypto.ts CHANGED
@@ -2,9 +2,9 @@ export const generateSalt = (length: number): string => {
2
2
  if (length <= 0) {
3
3
  return '';
4
4
  }
5
-
6
- const array = new Uint8Array(length);
7
- globalThis.crypto.getRandomValues(array);
5
+ // a hex is represented by 2 characters, so we split the length by 2
6
+ const array = new Uint8Array(length / 2);
7
+ window.crypto.getRandomValues(array);
8
8
 
9
9
  const salt = Array.from(array, (byte) =>
10
10
  byte.toString(16).padStart(2, '0'),
@@ -1,7 +1,39 @@
1
1
  import { describe, expect, test } from 'vitest';
2
+ import { generateSalt, digest, getHasher } from '../index';
2
3
 
3
4
  describe('This file is for utility functions', () => {
4
5
  test('crypto', () => {
5
6
  expect('1').toStrictEqual('1');
6
7
  });
8
+
9
+ test('generateSalt', async () => {
10
+ const salt = generateSalt(8);
11
+ expect(salt).toBeDefined();
12
+ expect(salt.length).toBe(8);
13
+ });
14
+
15
+ test('generateSalt 0 length', async () => {
16
+ const salt = generateSalt(0);
17
+ expect(salt).toBeDefined();
18
+ expect(salt.length).toBe(0);
19
+ });
20
+
21
+ test('digest', async () => {
22
+ const payload = 'test1';
23
+ const s1 = await digest(payload);
24
+ expect(s1).toBeDefined();
25
+ expect(s1.length).toBe(32);
26
+ });
27
+
28
+ test('digest', async () => {
29
+ const payload = 'test1';
30
+ const s1 = await digest(payload, 'SHA-512');
31
+ expect(s1).toBeDefined();
32
+ expect(s1.length).toBe(64);
33
+ });
34
+
35
+ test('get hasher', async () => {
36
+ const hash = await getHasher('SHA-512')('test1');
37
+ expect(hash).toBeDefined();
38
+ });
7
39
  });