Created
May 5, 2020 00:40
-
-
Save QQBoxy/68db236deabb751948d6d6496b6dfb93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shortUrl32 = (url) => { | |
const _alphabet = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; // 排除相似字 01OI // 共 32 個字 | |
const _base = _alphabet.length - 1; | |
const hash = CryptoJS.MD5(url).toString(); | |
const hashLen = hash.length; | |
let i = 0; | |
let j = 0; | |
let codes = []; | |
// 生成的 32 個位數,使用 8位數 x 4組 = 32 位數 | |
for (i = 0; i < 4; i++) { | |
const sub = hash.substr(8 * i, 8); | |
// 32 個位元,僅使用 30 個位元 | |
let hex = 0x3fffffff & parseInt(sub, 16); | |
let code = ''; | |
for (j = 0; j < 6; j++) { | |
const num = _base & hex; | |
code += _alphabet[num]; | |
hex = hex >> 5; | |
} | |
codes.push(code); | |
} | |
return codes; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment