You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
/*
|
|
* @Author: donghao donghao@supervision.ltd
|
|
* @Date: 2024-04-18 10:46:41
|
|
* @LastEditors: donghao donghao@supervision.ltd
|
|
* @LastEditTime: 2024-04-18 10:47:57
|
|
* @FilePath: \general-ai-manage\mock\utils\mockHash.ts
|
|
* @Description: 哈希值
|
|
*/
|
|
// 生成随机字符串
|
|
export function generateRandomString(length) {
|
|
let result = '';
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
const charactersLength = characters.length;
|
|
for (let i = 0; i < length; i++) {
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// 生成哈希值
|
|
export function generateHash(str) {
|
|
let hash = 0;
|
|
if (str.length == 0) return hash;
|
|
for (let i = 0; i < str.length; i++) {
|
|
const char = str.charCodeAt(i);
|
|
hash = (hash << 5) - hash + char;
|
|
hash = hash & hash; // Convert to 32bit integer
|
|
}
|
|
return Math.abs(hash).toString(16).slice(-4);
|
|
}
|
|
|
|
// 生成随机字符串
|
|
// const randomString = generateRandomString(10);
|
|
|
|
// // 生成哈希值
|
|
// const hash = generateHash(randomString);
|
|
|
|
// console.log(hash); // 输出一个随机的 16 位哈希值
|