Hash随机函数

236 阅读1分钟

Hash随机函数:

createHash(hashLength: number): string {
        hashLength = hashLength || 16;
        const hashArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        const hashBox = [];
        const hashAll = hashArr.length;
        for (let i = 0; i < hashLength; i++) {
            let hashItem;
            if (Math.floor(Math.random() * hashAll) > hashAll / 2) {
                hashItem = hashArr[Math.floor(Math.random() * hashAll)];
            }
            else {
                hashItem = hashArr[Math.floor(Math.random() * hashAll)].toLocaleUpperCase();
            }
            hashBox.push(hashItem);
        }
        return hashBox.join('');
    }