1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| import bcrypto from "bcrypt";
export async function createHash(input) {
//產生salt,10代表計算數字,越大所耗費運算資源越大
const genSalt = await bcrypto.genSalt(10);
//使用salt+內文產生hash
const hashed1 = await bcrypto.hash(input, genSalt);
//也可以將hash與salt合併一起,直接在第二個參數帶入代表運算salt的round的integer
const hashed2 = await bcrypto.hash(input, 10);
return hashed1;
}
export async function compareHash(plainText, hash) {
//由於hash內就帶有salt資訊所以,直接將內文與hash做compare,若是正確就回傳true
const result = await bcrypto.compare(plainText, hash);
return result;
}
|