Files
self/common/tests/cryptoHmac.test.ts
Justin Hernandez 20fa5c5adc [SELF-700] feat: add mock document generator demo (#995)
* feat: add mock document generator demo

* feat: add mock document generator

* fixes

* chore: refresh workflow cache

* update lock

* build

* updates

* more fixes

* code rabbit feedback

* compiles

* save wip

* updates

* merge with dev and fixes

* fix: align hoisting and demo Jest resolver (#1003)

* chore(app): map common src paths and declare svg flag module

* ci fix

* update lock

* save wip

* chore: address yarn lock issues (#1004)

* address yarn lock issues

* fix postinstall

* fix ci

* use metro js proxy

* android build working for /app

* save wip

* fix merge

* pipeline fixes

* format

* fix pipelines

* bump limit and split

* fix pipeline issues

* chore: decouple demo app build (#1013)

* chore: decouple demo app build

* chore: move demo app to workspace

* chore: unpublish demo workspace

* fix mobile sdk tests

* updates

* remove polyfills

* update merge

* update resolutions

* update resolutions

* fix merge

* fix paths

* save wip

* save wip fixes rd2

* working android

* update lock

* save wip ios building

* fix merge

* readd public key

* fixes

* ci fixes

* fixes

* fix web building

* fix ci

* fix tests

* update lock

* fix ci rd2

* formatting and fix ci

* fix

* finalize ci fixes

* fix tests and metro config paths for building

* save wip

* install missing package for pipeline

* fix wip app building

* wip react config

* save working emulator compile

* first round of pr fixes and feedback

* clean up demo app artifacts from sdk

* Add Gradle wrapper files for mobile-sdk-demo Android build

- Added gradlew, gradlew.bat, and gradle/wrapper/ directory
- Updated .gitignore to allow committing Gradle wrapper files
- Fixes Android build error: spawn ./gradlew ENOENT

* codex feedback and fixes

* fix tests

* file renames

* revert back to dev

* add types

* coderabbit fixes

* fix tests

* fix tests

* fix test

* fixes

* fix wip coderabbit issues

* coderabbit suggestions rd 2

* fix ci pipelines and addresss warnings

* cr fixes

* convert kebab to camelCase

* save wip fixes

* update reinstall and lock files

* fixes

* remove file

* fix lint

* fix polyfill fallback issues

* ensure that mock document is not on ofac list

* prettier
2025-09-27 13:59:47 -07:00

87 lines
3.0 KiB
TypeScript

// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
// SPDX-License-Identifier: BUSL-1.1
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
import { createHmac } from '../src/polyfills/crypto';
import { hmac } from '@noble/hashes/hmac';
import { sha256 } from '@noble/hashes/sha256';
describe('HMAC Streaming', () => {
const key = 'test-key';
const message1 = 'Hello ';
const message2 = 'World!';
const fullMessage = message1 + message2;
it('should produce the same result for single vs multiple update calls', () => {
// Expected result: compute HMAC of full message in one go
const expected = hmac(
sha256,
new TextEncoder().encode(key),
new TextEncoder().encode(fullMessage)
);
const expectedHex = Array.from(expected)
.map((b: number) => b.toString(16).padStart(2, '0'))
.join('');
// Single update call
const hmacSingle = createHmac('sha256', key);
hmacSingle.update(fullMessage);
const singleResult = hmacSingle.digest('hex');
// Multiple update calls (this should be equivalent but currently fails)
const hmacMultiple = createHmac('sha256', key);
hmacMultiple.update(message1);
hmacMultiple.update(message2);
const multipleResult = hmacMultiple.digest('hex');
// These should all be equal
expect(singleResult).toBe(expectedHex);
expect(multipleResult).toBe(expectedHex);
expect(singleResult).toBe(multipleResult);
});
it('should handle binary data correctly with streaming', () => {
const chunk1 = new Uint8Array([1, 2, 3]);
const chunk2 = new Uint8Array([4, 5, 6]);
const fullData = new Uint8Array([1, 2, 3, 4, 5, 6]);
// Expected result
const expected = hmac(sha256, new TextEncoder().encode(key), fullData);
// Single update
const hmacSingle = createHmac('sha256', key);
hmacSingle.update(fullData);
const singleResult = hmacSingle.digest();
// Multiple updates
const hmacMultiple = createHmac('sha256', key);
hmacMultiple.update(chunk1);
hmacMultiple.update(chunk2);
const multipleResult = hmacMultiple.digest();
// Convert to Uint8Array for comparison (handles both Buffer and Uint8Array cases)
const singleArray = new Uint8Array(singleResult);
const multipleArray = new Uint8Array(multipleResult);
expect(singleArray).toEqual(expected);
expect(multipleArray).toEqual(expected);
expect(singleArray).toEqual(multipleArray);
});
it('should throw on subsequent digest calls (Node.js parity)', () => {
const hmacInstance = createHmac('sha256', key);
hmacInstance.update(fullMessage);
const digest1 = hmacInstance.digest('hex');
expect(typeof digest1).toBe('string');
expect(digest1.length).toBe(64); // SHA256 hex is 64 chars
// Subsequent digest calls should throw
expect(() => hmacInstance.digest('hex')).toThrow();
expect(() => hmacInstance.digest()).toThrow();
// Updates should also throw after finalization
expect(() => hmacInstance.update('more data')).toThrow();
});
});