SHA
关于 SHA 哈希算法
安全哈希算法(英语:Secure Hash Algorithm,缩写为 SHA)由美国国家安全局(NSA)所设计,并由美国国家标准与技术研究院(NIST)发布的一系列加密哈希算法,SHA 家族系列包括:
- SHA-0:1993 年发布,当时称做安全散列标准(Secure Hash Standard),由于未公开的重大缺陷,发布之后很快就被 NSA 撤回,并被稍作修改的 SHA-1 版本取代;
- SHA-1:1995 年发布,但在 2010 年以后已经不被大多数的加密场景所接受,2017 年荷兰密码学研究小组 CWI 和 Google 正式宣布攻破了 SHA-1;
- SHA-2:2001 年发布,包括 SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256,SHA-2 目前没有出现明显的弱点,虽然至今尚未出现对 SHA-2 有效的攻击,但它的算法跟 SHA-1 基本上仍然相似;
- SHA-3:2015 年发布,包括 SHA3-224、SHA3-256、SHA3-384、SHA3-512、SHAKE128、SHAKE256,由于 MD5 被成功的破解,以及 SHA-0 和 SHA-1 出现理论上破解的方法,NIST 需要一个与之前算法不同的、可替换的加密散列算法,也就是现在的 SHA-3。
# Python 3.0+
import hashlib
def sha_hash(hash_data, hash_algorithm):
sha_object = hashlib.new(hash_algorithm)
sha_object.update(hash_data.encode())
sha_result = sha_object.hexdigest()
return sha_result
def sha_hash_shake128(hash_data, hash_length):
sha_object = hashlib.shake_128()
sha_object.update(hash_data.encode())
sha_result = sha_object.digest(hash_length).hex()
return sha_result
def sha_hash_shake256(hash_data, hash_length):
sha_object = hashlib.shake_256()
sha_object.update(hash_data.encode())
sha_result = sha_object.digest(hash_length).hex()
return sha_result
data = "spiderapi.cn - 虫术"
print("SHA-1 值: ", sha_hash(data, "sha1")) # 53189bf70db84f4ced0f6c691dffe67c19632b19
print("SHA-224 值: ", sha_hash(data, "sha224")) # c1aabab47820be476620ba4ee445a20f937803023142d28d68e6e369
print("SHA-256 值: ", sha_hash(data, "sha256")) # a9b9db2c59053c7843d59218bf9f9067f5803031bece0fe6853c858532ef6d3c
print("SHA-384 值: ", sha_hash(data, "sha384")) # c984e64888d99ecd27b8761293286fc2bee56a3537f79bf9cbf7652ff28310f388e4d5f94d7832f9691ea34e5eec66c7
print("SHA-512 值: ", sha_hash(data, "sha512")) # 233a2eb1a25db75a9f32d46c99432fe7c4321719f3405530c4f94fa6624d890b5a1fa3f31f672a9c679eb3eb5b867edf13967246ecd737103e95fd63929c593e
print("SHA3-224 值: ", sha_hash(data, "sha3_224")) # d6ad2d97cec0c2377bf20e98a1f464d5157860aa840e5733bdbe12dd
print("SHA3-256 值: ", sha_hash(data, "sha3_256")) # 63dd351dcd0ba81682119d93991e0bf6b58d503e60dcb3747222921ec83955a5
print("SHA3-384 值: ", sha_hash(data, "sha3_384")) # ee7ad38dcda87bfddb5acaa021a4abc46cc1f0c631967bd7aee657850fffaf43c342a3c9194fcb1e4694c670e04f9a80
print("SHA3-512 值: ", sha_hash(data, "sha3_512")) # d376726ba1c4708653ae44445d057382d6df05e20538e27a6f98b1247b5077dc4d81cd159738aaf2409eaadc64263c7b5fef94f1345df777cbf03fd700154816
# 获取比特长度为 16 的哈希值,比特长度必须是 8 的倍数,且不超过 2^256-1
print("SHAKE128 值: ", sha_hash_shake128(data, 16)) # b5b1a0f24b4b35e097020a2dc3c8a2e5
print("SHAKE256 值: ", sha_hash_shake256(data, 16)) # 18f5334b2b2ae60e07459da26d97a0fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# JavaScript Node.js ECMAScript 5.1+
// crypto 不支持 SHAKE128、SHAKE256,请使用方法二
var crypto = require("crypto");
function shaHash(hashData, hashAlgorithm) {
var sha = crypto.createHash(hashAlgorithm);
sha.update(hashData);
return sha.digest("hex");
}
var data = "spiderapi.cn - 虫术";
console.log("SHA-1 值: ", shaHash(data, "sha1")) // 53189bf70db84f4ced0f6c691dffe67c19632b19
console.log("SHA-224 值: ", shaHash(data, "sha224")) // c1aabab47820be476620ba4ee445a20f937803023142d28d68e6e369
console.log("SHA-256 值: ", shaHash(data, "sha256")) // a9b9db2c59053c7843d59218bf9f9067f5803031bece0fe6853c858532ef6d3c
console.log("SHA-384 值: ", shaHash(data, "sha384")) // c984e64888d99ecd27b8761293286fc2bee56a3537f79bf9cbf7652ff28310f388e4d5f94d7832f9691ea34e5eec66c7
console.log("SHA-512 值: ", shaHash(data, "sha512")) // 233a2eb1a25db75a9f32d46c99432fe7c4321719f3405530c4f94fa6624d890b5a1fa3f31f672a9c679eb3eb5b867edf13967246ecd737103e95fd63929c593e
console.log("SHA3-224 值: ", shaHash(data, "sha3-224")) // d6ad2d97cec0c2377bf20e98a1f464d5157860aa840e5733bdbe12dd
console.log("SHA3-256 值: ", shaHash(data, "sha3-256")) // 63dd351dcd0ba81682119d93991e0bf6b58d503e60dcb3747222921ec83955a5
console.log("SHA3-384 值: ", shaHash(data, "sha3-384")) // ee7ad38dcda87bfddb5acaa021a4abc46cc1f0c631967bd7aee657850fffaf43c342a3c9194fcb1e4694c670e04f9a80
console.log("SHA3-512 值: ", shaHash(data, "sha3-512")) // d376726ba1c4708653ae44445d057382d6df05e20538e27a6f98b1247b5077dc4d81cd159738aaf2409eaadc64263c7b5fef94f1345df777cbf03fd700154816
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 安装依赖 npm install js-sha3
// 仅支持 SHA3 系列算法
var jsSHA3 = require("js-sha3");
var data = "spiderapi.cn - 虫术";
console.log("SHA3-224 值: ", jsSHA3.sha3_224(data)) // d6ad2d97cec0c2377bf20e98a1f464d5157860aa840e5733bdbe12dd
console.log("SHA3-256 值: ", jsSHA3.sha3_256(data)) // 63dd351dcd0ba81682119d93991e0bf6b58d503e60dcb3747222921ec83955a5
console.log("SHA3-384 值: ", jsSHA3.sha3_384(data)) // ee7ad38dcda87bfddb5acaa021a4abc46cc1f0c631967bd7aee657850fffaf43c342a3c9194fcb1e4694c670e04f9a80
console.log("SHA3-512 值: ", jsSHA3.sha3_512(data)) // d376726ba1c4708653ae44445d057382d6df05e20538e27a6f98b1247b5077dc4d81cd159738aaf2409eaadc64263c7b5fef94f1345df777cbf03fd700154816
console.log("SHAKE128 值: ", jsSHA3.shake128(data, 128)) // b5b1a0f24b4b35e097020a2dc3c8a2e5
console.log("SHAKE256 值: ", jsSHA3.shake256(data, 128)) // 18f5334b2b2ae60e07459da26d97a0fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
// 安装依赖 npm install crypto-js
// SHA3 系列算法推荐使用方法一和方法二
var CryptoJS = require("crypto-js");
var data = "spiderapi.cn - 虫术";
console.log("SHA-1 值: ", CryptoJS.SHA1(data).toString()) // 53189bf70db84f4ced0f6c691dffe67c19632b19
console.log("SHA-224 值: ", CryptoJS.SHA224(data).toString()) // c1aabab47820be476620ba4ee445a20f937803023142d28d68e6e369
console.log("SHA-256 值: ", CryptoJS.SHA256(data).toString()) // a9b9db2c59053c7843d59218bf9f9067f5803031bece0fe6853c858532ef6d3c
console.log("SHA-384 值: ", CryptoJS.SHA384(data).toString()) // c984e64888d99ecd27b8761293286fc2bee56a3537f79bf9cbf7652ff28310f388e4d5f94d7832f9691ea34e5eec66c7
console.log("SHA-512 值: ", CryptoJS.SHA512(data).toString()) // 233a2eb1a25db75a9f32d46c99432fe7c4321719f3405530c4f94fa6624d890b5a1fa3f31f672a9c679eb3eb5b867edf13967246ecd737103e95fd63929c593e
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
// Make sure to add code blocks to your code group
# Golang 1.0+
// 安装依赖:go get golang.org/x/crypto
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"golang.org/x/crypto/sha3"
)
func shaHash(hashData, hashAlgorithm string) string {
switch hashAlgorithm {
case "sha1":
hash := sha1.New()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha224":
hash := sha256.New224()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha384":
hash := sha512.New384()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha256":
hash := sha256.New()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha512":
hash := sha512.New()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha3-224":
hash := sha3.New224()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha3-256":
hash := sha3.New256()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha3-384":
hash := sha3.New384()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
case "sha3-512":
hash := sha3.New512()
hash.Write([]byte(hashData))
return hex.EncodeToString(hash.Sum(nil))
default:
panic("Unsupported sha algorithm!")
}
}
func shaHashShake(hashData, hashAlgorithm string, hashLength int) string {
switch hashAlgorithm {
case "shake128":
hash := sha3.NewShake128()
_, err := hash.Write([]byte(hashData))
if err != nil {
return ""
}
hashValue := make([]byte, hashLength)
_, err = hash.Read(hashValue)
if err != nil {
return ""
}
return hex.EncodeToString(hashValue)
case "shake256":
hash := sha3.NewShake256()
_, err := hash.Write([]byte(hashData))
if err != nil {
return ""
}
hashValue := make([]byte, hashLength)
_, err = hash.Read(hashValue)
if err != nil {
return ""
}
return hex.EncodeToString(hashValue)
default:
panic("Unsupported sha algorithm!")
}
}
func main() {
data := "spiderapi.cn - 虫术"
fmt.Println("SHA-1 值: ", shaHash(data, "sha1")) // 53189bf70db84f4ced0f6c691dffe67c19632b19
fmt.Println("SHA-224 值: ", shaHash(data, "sha224")) // c1aabab47820be476620ba4ee445a20f937803023142d28d68e6e369
fmt.Println("SHA-256 值: ", shaHash(data, "sha256")) // a9b9db2c59053c7843d59218bf9f9067f5803031bece0fe6853c858532ef6d3c
fmt.Println("SHA-384 值: ", shaHash(data, "sha384")) // c984e64888d99ecd27b8761293286fc2bee56a3537f79bf9cbf7652ff28310f388e4d5f94d7832f9691ea34e5eec66c7
fmt.Println("SHA-512 值: ", shaHash(data, "sha512")) // 233a2eb1a25db75a9f32d46c99432fe7c4321719f3405530c4f94fa6624d890b5a1fa3f31f672a9c679eb3eb5b867edf13967246ecd737103e95fd63929c593e
fmt.Println("SHA3-224 值: ", shaHash(data, "sha3-224")) // d6ad2d97cec0c2377bf20e98a1f464d5157860aa840e5733bdbe12dd
fmt.Println("SHA3-256 值: ", shaHash(data, "sha3-256")) // 63dd351dcd0ba81682119d93991e0bf6b58d503e60dcb3747222921ec83955a5
fmt.Println("SHA3-384 值: ", shaHash(data, "sha3-384")) // ee7ad38dcda87bfddb5acaa021a4abc46cc1f0c631967bd7aee657850fffaf43c342a3c9194fcb1e4694c670e04f9a80
fmt.Println("SHA3-512 值: ", shaHash(data, "sha3-512")) // d376726ba1c4708653ae44445d057382d6df05e20538e27a6f98b1247b5077dc4d81cd159738aaf2409eaadc64263c7b5fef94f1345df777cbf03fd700154816
// 获取比特长度为 16 的哈希值,比特长度必须是 8 的倍数,且不超过 2^256-1
fmt.Println("SHAKE128 值: ", shaHashShake(data, "shake128", 16)) // b5b1a0f24b4b35e097020a2dc3c8a2e5
fmt.Println("SHAKE256 值: ", shaHashShake(data, "shake256", 16)) // 18f5334b2b2ae60e07459da26d97a0fc
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 主要特征
SHAKE 算法结果的大小并不是固定的,其他算法特征如下:
- SHA-1:字母(a-f)和数字(0-9)混合,固定位数 40 位;
- SHA-224/SHA3-224:字母(a-f)和数字(0-9)混合,固定位数 56 位;
- SHA-256/SHA3-256:字母(a-f)和数字(0-9)混合,固定位数 64 位;
- SHA-384/SHA3-384:字母(a-f)和数字(0-9)混合,固定位数 96 位;
- SHA-512/SHA3-512:字母(a-f)和数字(0-9)混合,固定位数 128 位。
示例:
编码类型 | 示例 |
---|---|
明文 | 123456 |
SHA-1 | 7c4a8d09ca3762af61e59520943dc26494f8941b |
SHA-224 | f8cdb04495ded47615258f9dc6a3f4707fd2405434fefc3cbf4ef4e6 |
SHA-256 | 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 |
SHA3-224 | 6be790258b73da9441099c4cb6aeec1f0c883152dd74e7581b70a648 |
SHA3-256 | d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e |
# 在线工具
帮助我们改善此页 (opens new window)
上次更新: 2024/10/17, 09:54:53