Skip to content

Commit

Permalink
chore: removed redundant crypto functionality (#3075)
Browse files Browse the repository at this point in the history
* chore: removed redundant crypto functionality

* chore: changeset
  • Loading branch information
petertonysmith94 authored Sep 3, 2024
1 parent a059ea1 commit 482bbf0
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 150 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-deers-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/crypto": patch
"@fuel-ts/errors": patch
---

chore: removed redundant crypto functionality
5 changes: 0 additions & 5 deletions apps/docs/src/guide/errors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,6 @@ When the workspace is not detected in the directory indicated in the message.

Ensure that the workspace is present in the directory specified.

### `HASHER_LOCKED`

The hashing algorithm is currently locked, any subsequent attempts to register a new implementation will throw this error.
The purpose of the lock function is to provide a way to ensure that the implementation of the specific hashing algorithm cannot be changed once it is locked. This can be useful in scenarios where you want to guarantee the integrity and consistency of the hashing function throughout your application.

### `UNKNOWN`

In cases where the error hasn't been mapped yet, this code will be used.
Expand Down
27 changes: 1 addition & 26 deletions packages/crypto/src/node/hmac.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { createHmac } from 'crypto';

let locked = false;

const COMPUTEHMAC = (
algorithm: 'sha256' | 'sha512',
key: Uint8Array,
data: Uint8Array
): BytesLike => createHmac(algorithm, key).update(data).digest();

let computeHMAC = COMPUTEHMAC;

export function computeHmac(
algorithm: 'sha256' | 'sha512',
_key: Uint8Array,
_data: Uint8Array
): string {
const key = arrayify(_key, 'key');
const data = arrayify(_data, 'data');
return hexlify(computeHMAC(algorithm, key, data));
return hexlify(createHmac(algorithm, key).update(data).digest());
}
computeHmac._ = COMPUTEHMAC;
computeHmac.lock = () => {
locked = true;
};
computeHmac.register = (
func: (algorithm: 'sha256' | 'sha512', key: Uint8Array, data: Uint8Array) => BytesLike
) => {
if (locked) {
throw new FuelError(ErrorCode.HASHER_LOCKED, 'computeHmac is locked');
}
computeHMAC = func;
};
Object.freeze(computeHmac);
34 changes: 1 addition & 33 deletions packages/crypto/src/node/pbkdf2.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { pbkdf2Sync } from 'crypto';

let locked = false;

const PBKDF2 = (
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
algo: 'sha256' | 'sha512'
): BytesLike => pbkdf2Sync(password, salt, iterations, keylen, algo);

let pBkdf2 = PBKDF2;

export function pbkdf2(
_password: BytesLike,
_salt: BytesLike,
Expand All @@ -24,24 +11,5 @@ export function pbkdf2(
): string {
const password = arrayify(_password, 'password');
const salt = arrayify(_salt, 'salt');
return hexlify(pBkdf2(password, salt, iterations, keylen, algo));
return hexlify(pbkdf2Sync(password, salt, iterations, keylen, algo));
}
pbkdf2._ = PBKDF2;
pbkdf2.lock = (): void => {
locked = true;
};
pbkdf2.register = (
func: (
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
algo: 'sha256' | 'sha512'
) => BytesLike
) => {
if (locked) {
throw new FuelError(ErrorCode.HASHER_LOCKED, 'pbkdf2 is locked');
}
pBkdf2 = func;
};
Object.freeze(pbkdf2);
20 changes: 1 addition & 19 deletions packages/crypto/src/shared/ripemd160.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify } from '@fuel-ts/utils';
import { ripemd160 as noble_ripemd160 } from '@noble/hashes/ripemd160';

let locked = false;

const helper = (data: Uint8Array): Uint8Array => noble_ripemd160(data);

let ripemd: (data: Uint8Array) => Uint8Array = helper;

export function ripemd160(_data: BytesLike): Uint8Array {
const data = arrayify(_data, 'data');
return ripemd(data);
return noble_ripemd160(data);
}
ripemd160._ = helper;
ripemd160.lock = (): void => {
locked = true;
};
ripemd160.register = (func: (data: Uint8Array) => Uint8Array) => {
if (locked) {
throw new FuelError(ErrorCode.HASHER_LOCKED, 'ripemd160 is locked');
}
ripemd = func;
};
Object.freeze(ripemd160);
29 changes: 0 additions & 29 deletions packages/crypto/test/hmac.node.test.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/crypto/test/pbkdf2.node.test.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export enum ErrorCode {

// crypto
INVALID_CREDENTIALS = 'invalid-credentials',

/** @deprecated This error code is no longer used */
HASHER_LOCKED = 'hasher-locked',

// transaction
Expand Down

0 comments on commit 482bbf0

Please sign in to comment.