grill-blog/crypto.ts

27 lines
832 B
TypeScript
Raw Normal View History

2024-05-13 22:50:54 +01:00
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
export interface Crypto {
hash(value: string): Promise<string>;
compare(value: string, hash: string): Promise<boolean>;
}
export class BcryptCrypto implements Crypto {
public hash(value: string): Promise<string> {
return bcrypt.hash(value);
}
public compare(value: string, hash: string): Promise<boolean> {
return bcrypt.compare(value, hash);
}
}
export class MockCrypto implements Crypto {
// deno-lint-ignore require-await
public async hash(value: string): Promise<string> {
return bcrypt.hashSync(value, bcrypt.genSaltSync(4));
}
// deno-lint-ignore require-await
public async compare(value: string, hash: string): Promise<boolean> {
return bcrypt.compareSync(value, hash);
}
}