From 1b1af8d03818550282efe259be108d5f808ab224 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Wed, 21 Jun 2023 19:26:16 +0800 Subject: [PATCH] Add Java and C++ code for the section hash algorithm (#560) --- codes/cpp/chapter_hashing/built_in_hash.cpp | 29 ++++++++ codes/cpp/chapter_hashing/simple_hash.cpp | 67 +++++++++++++++++++ codes/java/chapter_hashing/built_in_hash.java | 38 +++++++++++ codes/java/chapter_hashing/simple_hash.java | 66 ++++++++++++++++++ codes/python/chapter_hashing/simple_hash.py | 10 +-- docs/chapter_hashing/hash_algorithm.md | 57 ++++++++++++++-- 6 files changed, 256 insertions(+), 11 deletions(-) create mode 100644 codes/cpp/chapter_hashing/built_in_hash.cpp create mode 100644 codes/cpp/chapter_hashing/simple_hash.cpp create mode 100644 codes/java/chapter_hashing/built_in_hash.java create mode 100644 codes/java/chapter_hashing/simple_hash.java diff --git a/codes/cpp/chapter_hashing/built_in_hash.cpp b/codes/cpp/chapter_hashing/built_in_hash.cpp new file mode 100644 index 0000000000..847df40a3a --- /dev/null +++ b/codes/cpp/chapter_hashing/built_in_hash.cpp @@ -0,0 +1,29 @@ +/** + * File: built_in_hash.cpp + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* Driver Code */ +int main() { + int num = 3; + size_t hashNum = hash()(num); + cout << "整数 " << num << " 的哈希值为 " << hashNum << "\n"; + + bool bol = true; + size_t hashBol = hash()(bol); + cout << "布尔量 " << bol << " 的哈希值为 " << hashBol << "\n"; + + double dec = 3.14159; + size_t hashDec = hash()(dec); + cout << "小数 " << dec << " 的哈希值为 " << hashDec << "\n"; + + string str = "Hello 算法"; + size_t hashStr = hash()(str); + cout << "字符串 " << str << " 的哈希值为 " << hashStr << "\n"; + + // 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算 + // 数组、对象的哈希值计算需要自行实现 +} diff --git a/codes/cpp/chapter_hashing/simple_hash.cpp b/codes/cpp/chapter_hashing/simple_hash.cpp new file mode 100644 index 0000000000..f32e131805 --- /dev/null +++ b/codes/cpp/chapter_hashing/simple_hash.cpp @@ -0,0 +1,67 @@ +/** + * File: simple_hash.cpp + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +#include "../utils/common.hpp" + +/* 加法哈希 */ +int addHash(string key) { + long long hash = 0; + const int MODULUS = 1000000007; + for (unsigned char c : key) { + hash = (hash + (int)c) % MODULUS; + } + return (int)hash; +} + +/* 乘法哈希 */ +int mulHash(string key) { + long long hash = 0; + const int MODULUS = 1000000007; + for (unsigned char c : key) { + hash = (31 * hash + (int)c) % MODULUS; + } + return (int)hash; +} + +/* 异或哈希 */ +int xorHash(string key) { + int hash = 0; + const int MODULUS = 1000000007; + for (unsigned char c : key) { + cout<<(int)c<> 28) ^ (int)c) % MODULUS; + } + return (int)hash; +} + +/* Driver Code */ +int main() { + string key = "Hello dsad3241241dsa算123法"; + + int hash = addHash(key); + cout << "加法哈希值为 " << hash << endl; + + hash = mulHash(key); + cout << "乘法哈希值为 " << hash << endl; + + hash = xorHash(key); + cout << "异或哈希值为 " << hash << endl; + + hash = rotHash(key); + cout << "旋转哈希值为 " << hash << endl; + + return 0; +} diff --git a/codes/java/chapter_hashing/built_in_hash.java b/codes/java/chapter_hashing/built_in_hash.java new file mode 100644 index 0000000000..3f7f49d0ca --- /dev/null +++ b/codes/java/chapter_hashing/built_in_hash.java @@ -0,0 +1,38 @@ +/** + * File: built_in_hash.java + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +package chapter_hashing; + +import utils.*; +import java.util.*; + +public class built_in_hash { + public static void main(String[] args) { + int num = 3; + int hashNum = Integer.hashCode(num); + System.out.println("整数 " + num + " 的哈希值为 " + hashNum); + + boolean bol = true; + int hashBol = Boolean.hashCode(bol); + System.out.println("布尔量 " + bol + " 的哈希值为 " + hashBol); + + double dec = 3.14159; + int hashDec = Double.hashCode(dec); + System.out.println("小数 " + dec + " 的哈希值为 " + hashDec); + + String str = "Hello 算法"; + int hashStr = str.hashCode(); + System.out.println("字符串 " + str + " 的哈希值为 " + hashStr); + + Object[] arr = { 12836, "小哈" }; + int hashTup = Arrays.hashCode(arr); + System.out.println("数组 " + Arrays.toString(arr) + " 的哈希值为 " + hashTup); + + ListNode obj = new ListNode(0); + int hashObj = obj.hashCode(); + System.out.println("节点对象 " + obj + " 的哈希值为 " + hashObj); + } +} diff --git a/codes/java/chapter_hashing/simple_hash.java b/codes/java/chapter_hashing/simple_hash.java new file mode 100644 index 0000000000..c2f3970812 --- /dev/null +++ b/codes/java/chapter_hashing/simple_hash.java @@ -0,0 +1,66 @@ +/** + * File: simple_hash.java + * Created Time: 2023-06-21 + * Author: Krahets (krahets@163.com) + */ + +package chapter_hashing; + +public class simple_hash { + /* 加法哈希 */ + static int addHash(String key) { + long hash = 0; + final int MODULUS = 1000000007; + for (char c : key.toCharArray()) { + hash = (hash + (int) c) % MODULUS; + } + return (int) hash; + } + + /* 乘法哈希 */ + static int mulHash(String key) { + long hash = 0; + final int MODULUS = 1000000007; + for (char c : key.toCharArray()) { + hash = (31 * hash + (int) c) % MODULUS; + } + return (int) hash; + } + + /* 异或哈希 */ + static int xorHash(String key) { + int hash = 0; + final int MODULUS = 1000000007; + for (char c : key.toCharArray()) { + System.out.println((int)c); + hash ^= (int) c; + } + return hash & MODULUS; + } + + /* 旋转哈希 */ + static int rotHash(String key) { + long hash = 0; + final int MODULUS = 1000000007; + for (char c : key.toCharArray()) { + hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS; + } + return (int) hash; + } + + public static void main(String[] args) { + String key = "Hello 算法"; + + int hash = addHash(key); + System.out.println("加法哈希值为 " + hash); + + hash = mulHash(key); + System.out.println("乘法哈希值为 " + hash); + + hash = xorHash(key); + System.out.println("异或哈希值为 " + hash); + + hash = rotHash(key); + System.out.println("旋转哈希值为 " + hash); + } +} diff --git a/codes/python/chapter_hashing/simple_hash.py b/codes/python/chapter_hashing/simple_hash.py index d4e420532e..e63c18244b 100644 --- a/codes/python/chapter_hashing/simple_hash.py +++ b/codes/python/chapter_hashing/simple_hash.py @@ -43,14 +43,16 @@ def rot_hash(key: str) -> int: """Driver Code""" if __name__ == "__main__": - hash = add_hash("Hello 算法") + key = "Hello 算法" + + hash = add_hash(key) print(f"加法哈希值为 {hash}") - hash = mul_hash("Hello 算法") + hash = mul_hash(key) print(f"乘法哈希值为 {hash}") - hash = xor_hash("Hello 算法") + hash = xor_hash(key) print(f"异或哈希值为 {hash}") - hash = rot_hash("Hello 算法") + hash = rot_hash(key) print(f"旋转哈希值为 {hash}") diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 68098d7608..295d442557 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -211,13 +211,13 @@ $$ 直至目前,MD5 和 SHA-1 已多次被成功攻击,因此它们被各类安全应用弃用。SHA-2 系列中的 SHA-256 是最安全的哈希算法之一,仍未出现成功的攻击案例,因此常被用在各类安全应用与协议中。SHA-3 相较 SHA-2 的实现开销更低、计算效率更高,但目前使用覆盖度不如 SHA-2 系列。 -| | MD5 | SHA-1 | SHA-2 | SHA-3 | -| -------- | ------------------------------ | -------------------- | ---------------------------- | -------------------- | -| 推出时间 | 1992 | 1995 | 2002 | 2008 | -| 输出长度 | 128 bits | 160 bits | 256 / 512 bits | 224/256/384/512 bits | -| 哈希冲突 | 较多 | 较多 | 很少 | 很少 | -| 安全等级 | 低,已被成功攻击 | 低,已被成功攻击 | 高 | 高 | -| 应用 | 已被弃用,仍用于数据完整性检查 | 已被弃用 | 加密货币交易验证、数字签名等 | 可用于替代 SHA-2 | +| | MD5 | SHA-1 | SHA-2 | SHA-3 | +| -------- | ------------------------------ | ---------------- | ---------------------------- | -------------------- | +| 推出时间 | 1992 | 1995 | 2002 | 2008 | +| 输出长度 | 128 bits | 160 bits | 256 / 512 bits | 224/256/384/512 bits | +| 哈希冲突 | 较多 | 较多 | 很少 | 很少 | +| 安全等级 | 低,已被成功攻击 | 低,已被成功攻击 | 高 | 高 | +| 应用 | 已被弃用,仍用于数据完整性检查 | 已被弃用 | 加密货币交易验证、数字签名等 | 可用于替代 SHA-2 | ## 数据结构的哈希值 @@ -228,16 +228,59 @@ $$ - 元组的哈希值是对其中每一个元素进行哈希,然后将这些哈希值组合起来,得到单一的哈希值。 - 对象的哈希值基于其内存地址生成。通过重写对象的哈希方法,可实现基于内容生成哈希值。 +!!! tip + + 请注意,不同编程语言的内置哈希值计算函数的定义和方法不同。 + === "Java" ```java title="built_in_hash.java" + int num = 3; + int hashNum = Integer.hashCode(num); + // 整数 3 的哈希值为 3 + + boolean bol = true; + int hashBol = Boolean.hashCode(bol); + // 布尔量 true 的哈希值为 1231 + + double dec = 3.14159; + int hashDec = Double.hashCode(dec); + // 小数 3.14159 的哈希值为 -1340954729 + + String str = "Hello 算法"; + int hashStr = str.hashCode(); + // 字符串 Hello 算法 的哈希值为 -727081396 + Object[] arr = { 12836, "小哈" }; + int hashTup = Arrays.hashCode(arr); + // 数组 [12836, 小哈] 的哈希值为 1151158 + + ListNode obj = new ListNode(0); + int hashObj = obj.hashCode(); + // 节点对象 utils.ListNode@7dc5e7b4 的哈希值为 2110121908 ``` === "C++" ```cpp title="built_in_hash.cpp" + int num = 3; + size_t hashNum = hash()(num); + // 整数 3 的哈希值为 3 + + bool bol = true; + size_t hashBol = hash()(bol); + // 布尔量 1 的哈希值为 1 + + double dec = 3.14159; + size_t hashDec = hash()(dec); + // 小数 3.14159 的哈希值为 4614256650576692846 + + string str = "Hello 算法"; + size_t hashStr = hash()(str); + // 字符串 Hello 算法 的哈希值为 15466937326284535026 + // 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算 + // 数组、对象的哈希值计算需要自行实现 ``` === "Python"