这个十年前就有结论的问题每隔一段时间都会有人讨论。结论是不管什么环境,前端提交 hash 后的口令总是好的,防的不是中途的嗅探者,而是脱库后的破解者。前端 hash 越耗时,脱库后跑字典越慢。
至于前端 hash 也不用自己捣鼓 js/wasm 这些,主流浏览器早已内置 PBKDF2 算法,较新的 CPU 都有相应的硬件加速,比自己实现可以快很多倍。
演示:
const username = new TextEncoder().encode('alice') const password = new TextEncoder().encode('hello1234') // 重复 1000 万次 SHA256 const pbkdfOpts = { name: 'PBKDF2', hash: 'SHA-256', salt: username, iterations: 1e7, } async function pbkdf2(pwd, opts, bits) { const baseKey = await crypto.subtle.importKey('raw', pwd, 'PBKDF2', false, ['deriveBits']) const buf = await crypto.subtle.deriveBits(opts, baseKey, bits) return new Uint8Array(buf) } const dk = await pbkdf2(password, pbkdfOpts, 256) // 注册/登录提交 dk 即可,无需提交 password console.log(dk) 

