Skip to content

Merging Process #1 from sd-jwt-ts #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d55879c
Revert "fix: the bytes of the output of the hash function must be bas…
lukasjhan Feb 11, 2024
d611fa7
feat: migrate base64url
lukasjhan Feb 11, 2024
061c3d2
fix: restore tests
lukasjhan Feb 11, 2024
c0abd2e
feat: fix decoy and disclosure
lukasjhan Feb 11, 2024
cf7784a
feat: fix digest of encoded disclosure
lukasjhan Feb 12, 2024
c17a886
chore: Apply prettier
lukasjhan Feb 12, 2024
716b8c7
feat: Add test for utin8array encoding
lukasjhan Feb 12, 2024
4c15ffc
feat: Remove jose in jwt sign and verify
lukasjhan Feb 12, 2024
b29e314
feat: Remove jose in types
lukasjhan Feb 12, 2024
915278b
feat: Fix sdjwt tests
lukasjhan Feb 12, 2024
837b193
feat: implement new interface
lukasjhan Feb 12, 2024
9a375a8
feat: Fix e2e tests and type
lukasjhan Feb 12, 2024
4f7337d
feat: Fix index tests
lukasjhan Feb 12, 2024
2b2874d
refactor: using constant
lukasjhan Feb 12, 2024
da5b16e
feat: add tests for crypto functions
lukasjhan Feb 12, 2024
d6b1285
feat: Add default sha256 function
lukasjhan Feb 12, 2024
4511026
feat: fix sign_alg, hash_alg config
lukasjhan Feb 12, 2024
162585c
feat: fix examples
lukasjhan Feb 12, 2024
0168470
doc: Add dependencies
lukasjhan Feb 12, 2024
ce0aacc
doc: add decoding example
lukasjhan Feb 12, 2024
097c4a3
refactor: Remove base64 function overloading
lukasjhan Feb 13, 2024
25d0ef0
refactor: unify variable convention
lukasjhan Feb 13, 2024
613f4fe
Merge branch 'main' into dev/merge1
lukasjhan Feb 13, 2024
3b34b0c
feat: Add utf8 encode function and tests
lukasjhan Feb 13, 2024
fa6e0d7
test: Remove dup test
lukasjhan Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/sha256.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { sha256 as nobleSha256 } from '@noble/hashes/sha256';

export const sha256 = (text: string): Uint8Array => {
const uint8Array = new Uint8Array(text.length);

for (let i = 0; i < text.length; i++) {
uint8Array[i] = text.charCodeAt(i);
}
const uint8Array = toUTF8Array(text);
const hashBytes = nobleSha256(uint8Array);
return hashBytes;
};

function toUTF8Array(str: string) {
const utf8 = [];
for (let i = 0; i < str.length; i++) {
let charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
} else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(
0xe0 | (charcode >> 12),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f),
);
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode =
0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
utf8.push(
0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f),
);
}
}
return new Uint8Array(utf8);
}
93 changes: 92 additions & 1 deletion src/test/sha256.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,103 @@ describe('SHA-256 tests', () => {
expect(s1).toStrictEqual(s2);
});

test('test#1', async () => {
test('test#2', async () => {
const payload = 'email@email.com';
const s1 = bytesToHex(await digest(payload));
const ss1 = bytesToHex(await digest(s1));
const s2 = bytesToHex(sha256(payload));
const ss2 = bytesToHex(sha256(s2));
expect(ss1).toStrictEqual(ss2);
});

test('test#3', async () => {
const payload = 'こんにちは';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#4', async () => {
const payload = 'Привет Добро пожаловать';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#5', async () => {
const payload = '🧑‍💻👩‍💻';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#6', async () => {
const payload = 'مرحبا';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#7', async () => {
const payload = 'שלום';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#8', async () => {
const payload = 'स्वागत है';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#9', async () => {
const payload = 'হ্যালো';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#10', async () => {
const payload = 'Γειά σου';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#11', async () => {
const payload = 'สวัสดี';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#12', async () => {
const payload = 'Добро пожаловать';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#13', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a duplicate test with test#14?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It's dup. my mistake

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const payload = 'ሰላም';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#14', async () => {
const payload = 'ሰላም';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});

test('test#15', async () => {
const payload = 'Բարեւ Ձեզ';
const s1 = bytesToHex(await digest(payload));
const s2 = bytesToHex(sha256(payload));
expect(s1).toStrictEqual(s2);
});
});