Skip to content

Commit

Permalink
feat: #369 sha256.ts secured and browser/node agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
lucanicoladebiasi committed Apr 5, 2024
1 parent 9db5bc2 commit 4c30dfc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/hash/keccak256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function keccak256(
// Assert that the returnType is valid
assertIsValidReturnType('keccak256', returnType);
const hash = keccak_256(data);
return returnType === 'buffer' ? Buffer.from(hash) : Hex0x.of(hash);
return returnType === 'buffer' ? hash : Hex0x.of(hash);
}

export { keccak256 };
51 changes: 33 additions & 18 deletions packages/core/src/hash/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,70 @@ import { type ReturnType } from './types';
/* --- Overloaded functions start --- */

/**
* Computes the sha256 hash of the given data.
* Returns the hash as a Buffer.
* Calculates the SHA-256 hash of the given data.
*
* @param data - The input data (either a Buffer or string) for which the hash needs to be computed.
* @returns A Buffer containing the 256-bit sha256 hash of the provided data.
* Secure audit function.
* * {@link sha256}
*
* @param {string | Uint8Array} data - The data to calculate the hash for.
* @return {Uint8Array} - The SHA-256 hash of the given data.
*/
function sha256(data: string | Uint8Array): Buffer;
function sha256(data: string | Uint8Array): Uint8Array;

/**
* Computes the sha256 hash of the given data.
* Returns the hash as a Buffer.
*
* Secure audit function.
* * {@link sha256}
*
* @param data - The input data (either a Buffer or string) for which the hash needs to be computed.
* @param returnType - The format in which to return the hash. Either 'buffer' or 'hex'.
* @returns {Buffer} A Buffer containing the 256-bit sha256 hash of the provided data.
*/
function sha256(data: string | Uint8Array, returnType: 'buffer'): Buffer;
/**
* Calculates the SHA-256 hash of the given data.
*
* @param {string | Uint8Array} data - The data to calculate the SHA-256 hash for.
* @param {'buffer'} returnType - The return type for the hash. Currently only supports 'buffer'.
*
* @return {Uint8Array} - The SHA-256 hash as a Uint8Array.
*/
function sha256(data: string | Uint8Array, returnType: 'buffer'): Uint8Array;

/**
* Computes the sha256 hash of the given data.
* Returns the hash as a hex string, prefixed with `0x`.
* Calculates the SHA-256 hash of the given data.
*
* @param data - The input data (either a Buffer or string) for which the hash needs to be computed.
* @param returnType - The format in which to return the hash. Either 'hex' or 'buffer'.
* @returns {string} A string representing the hexadecimal format of the 256-bit sha256 hash, prefixed with `0x`.
* Secure audit function.
* * {@link sha256}
*
* @param {string | Uint8Array} data - The input data to be hashed.
* @param {'hex'} returnType - The desired return type of the hash.
* @return {string} The SHA-256 hash of the data in the specified return type.
*/
function sha256(data: string | Uint8Array, returnType: 'hex'): string;

/* --- Overloaded functions end --- */

/**
* Calculates the SHA-256 hash of the given data.
* Computes the SHA-256 hash of the given data.
*
* Secure audit function.
* * [_sha256](https://github.com/paulmillr/noble-hashes?tab=readme-ov-file#sha2-sha256-sha384-sha512-sha512_256)
*
* @param {string | Uint8Array} data - The data to be hashed. It can be either a string or a Uint8Array.
* @param {ReturnType} returnType - The return type. Default is 'buffer'. Valid options are 'buffer' or 'string'.
* @returns {Buffer | string} - The SHA-256 hash value. If the returnType is 'buffer', it returns a Buffer object. If the returnType is 'string', it returns a hexadecimal string representation
* of the hash.
* @param {string | Uint8Array} data - The data to compute the hash for.
* @param {ReturnType} [returnType='buffer'] - The desired return type for the hash. Defaults to 'buffer'.
*
* @return {Uint8Array | string} - The computed SHA-256 hash.
*/
function sha256(
data: string | Uint8Array,
returnType: ReturnType = 'buffer'
): Buffer | string {
): Uint8Array | string {
// Assert that the returnType is valid
assertIsValidReturnType('sha256', returnType);
const hash = _sha256(data);
return returnType === 'buffer' ? Buffer.from(hash) : Hex0x.of(hash);
return returnType === 'buffer' ? hash : Hex0x.of(hash);
}

export { sha256 };
2 changes: 1 addition & 1 deletion packages/core/tests/secp256k1/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const signature = Buffer.from(
/**
* Simple message hashs
*/
const messageHashBuffer = keccak256('hello world');
const messageHashBuffer = Buffer.from(keccak256('hello world'));
const validMessageHashes = [messageHashBuffer];
const invalidMessageHashes = [Buffer.from('some_invalid_stuff', 'hex')];

Expand Down

0 comments on commit 4c30dfc

Please sign in to comment.