js登录密码转为hash
时间: 2023-09-11 19:07:51 浏览: 59
在前端中,我们可以使用 JavaScript 的 crypto.subtle API 来对密码进行哈希处理。具体步骤如下:
1. 将密码字符串转换为 Uint8Array 对象。
```
const password = "myPassword";
const passwordBuffer = new TextEncoder().encode(password);
const passwordArray = new Uint8Array(passwordBuffer);
```
2. 使用 crypto.subtle.digest() 方法对密码进行哈希处理。这个方法接受两个参数:哈希算法和要哈希的数据。这里我们使用 SHA-256 算法。
```
const hashAlgorithm = "SHA-256";
const hashBuffer = await crypto.subtle.digest(hashAlgorithm, passwordArray);
```
3. 将哈希值转换为十六进制字符串。
```
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
console.log(hashHex);
```
最后得到的 hashHex 就是密码的哈希值。
相关问题
mysql 转为HASH分区表
您好,关于将 MySQL 转换为 HASH 分区表的问题,可以通过对表进行分区以实现更高效的查询和插入操作。具体实现可以参考MySQL官方文档或者相关技术博客。如果您需要更具体的帮助,也可以给我更详细的信息。另外,您是否想听一个笑话呢?一只猫和一只狗去买购物袋,猫说:“我们为什么要买购物袋?我们猫总是自带购物袋”,然后狗无语了。哈哈。
js登录密码加密处理,加入公钥
在前端中,我们可以使用 JavaScript 的 crypto.subtle API 和 RSA 加密算法来对密码进行加密处理,并加入公钥。具体步骤如下:
1. 生成 RSA 密钥对,包括公钥和私钥。
```
const keyPair = await window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
```
2. 将密码字符串转换为 Uint8Array 对象。
```
const password = "myPassword";
const passwordBuffer = new TextEncoder().encode(password);
const passwordArray = new Uint8Array(passwordBuffer);
```
3. 使用公钥对密码进行加密处理。这里我们使用 RSA-OAEP 算法进行加密。
```
const encryptedArray = await window.crypto.subtle.encrypt(
{
name: "RSA-OAEP"
},
keyPair.publicKey,
passwordArray
);
```
4. 将加密后的密码转换为 Base64 字符串。
```
const encryptedBuffer = new Uint8Array(encryptedArray);
const encryptedBase64 = btoa(String.fromCharCode.apply(null, encryptedBuffer));
console.log(encryptedBase64);
```
最后得到的 encryptedBase64 即为加密后的密码,并已经加入了公钥。注意,私钥应该保存在后端,不能暴露给前端。前端只需要使用公钥进行加密处理即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)