在部署vue3+element plus + django项目的时候,报错了,Cannot read properties of undefined (reading ‘digest’),也就是调用这个函数的时候报错了:crypto.subtle.digest
主要是加密函数生成摘要的时候,一个函数不能使用,发现是必须是安全的请求才能使用摘要函数digest,所以http是不能使用的,https可以正常使用,其他的几种安全请求是https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features/
- (https, *, *)
- (wss, *, *)
- (*, localhost, *)
- (*, 127/8, *)
- (*, ::1/128, *)
- (file, *, —)
- (chrome-extension, *, —)
https://github.com/RBND-studio/flows-js/issues/186
async sha(str) {
const textEncoder = new TextEncoder();
const message = textEncoder.encode(str);
// Cannot read properties of undefined (reading 'digest')
// https://stackoverflow.com/questions/46670556/how-to-enable-crypto-subtle-for-unsecure-origins-in-chrome/46671627#46671627
// https://github.com/RBND-studio/flows-js/issues/186
const messageDigest = await crypto.subtle.digest('SHA-512', message);
const hexDigest = Array.from(new Uint8Array(messageDigest))
.map((x) => x.toString(16).padStart(2, '0'))
.join('');
// console.log(hexDigest);
return hexDigest;
},