Skip to content

Commit

Permalink
feat(bcrypt): hash support string salt (#851)
Browse files Browse the repository at this point in the history
* feat(bcrypt): hash support string salt

* fix(bcrypt): fix string logic
  • Loading branch information
richerfu committed Jul 10, 2024
1 parent a2016ee commit 1992fa2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/bcrypt/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export declare function genSalt(round: number, version?: '2a' | '2x' | '2y' | '2

export declare function genSaltSync(round: number, version?: '2a' | '2x' | '2y' | '2b'): string

export declare function hash(input: string | Buffer, cost?: number | undefined | null, salt?: Buffer | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>
export declare function hash(input: string | Buffer, cost?: number | undefined | null, salt?: string | Buffer | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>

export declare function hashSync(input: string | Buffer, cost?: number | undefined | null, salt?: Buffer | undefined | null): string
export declare function hashSync(input: string | Buffer, cost?: number | undefined | null, salt?: string | Buffer | undefined | null): string

export declare function verify(password: string | Buffer, hash: string | Buffer, signal?: AbortSignal | undefined | null): Promise<boolean>

Expand Down
10 changes: 6 additions & 4 deletions packages/bcrypt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ pub fn gen_salt_js(
pub fn hash_sync(
input: Either<String, Buffer>,
cost: Option<u32>,
salt: Option<Buffer>,
salt: Option<Either<String, Buffer>>,
) -> Result<String> {
let salt = if let Some(salt) = salt {
let mut s = [0u8; 16];
s.copy_from_slice(salt.as_ref());
let buf = either_string_buffer_as_bytes(&salt);
s.copy_from_slice(&buf[..16]);
s
} else {
gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?
Expand All @@ -71,12 +72,13 @@ pub fn hash_sync(
pub fn hash(
input: Either<String, JsBuffer>,
cost: Option<u32>,
salt: Option<Buffer>,
salt: Option<Either<String, Buffer>>,
signal: Option<AbortSignal>,
) -> Result<AsyncTask<HashTask>> {
let salt = if let Some(salt) = salt {
let mut s = [0u8; 16];
s.copy_from_slice(salt.as_ref());
let buf = either_string_buffer_as_bytes(&salt);
s.copy_from_slice(&buf[..16]);
s
} else {
gen_salt().map_err(|err| Error::new(Status::InvalidArg, format!("{err}")))?
Expand Down

0 comments on commit 1992fa2

Please sign in to comment.