如何在Crystal中生成带盐的128位非MD5哈希?
Hey there! Let's tackle your questions step by step and give you concrete code to generate that salted 128-bit hash you need.
1. Does Crystal support SHA256?
Absolutely! Newer versions of Crystal (1.0 and above) include SHA256 support directly in the standard library via the Digest module—no extra dependencies needed for basic use. If you're stuck on an older version, you can still access SHA256 through Crystal's OpenSSL bindings (more on that below).
2. Can Crystal call the OpenSSL library?
You bet. Crystal has first-class integration with OpenSSL through its standard library's OpenSSL module, which wraps your system's OpenSSL installation. This gives you access to all the cryptographic functions OpenSSL offers, including SHA256, SHA512, and beyond.
3. Does this impose system requirements?
The only hard requirement is that your target system has the OpenSSL library installed. This is standard on almost all Linux distributions, macOS, and BSD systems. For Windows, you'll need to install OpenSSL (via Chocolatey, the official installer, or WSL) and ensure it's available in your system path.
Generating a Salted 128-bit Hash
Let's cover two practical approaches to meet your needs:
Approach 1: SHA256 + Truncate to 128-bit (with Salt)
If you want to use SHA256 and truncate it to 128 bits, here's a clean implementation using the standard library:
require "digest" def salted_128bit_sha256(input : String, salt : String) : String # Combine input and salt (adjust concatenation logic if your use case needs it) combined_data = input + salt # Generate full SHA256 hash as a hex string full_hash = Digest::SHA256.hexdigest(combined_data) # Truncate to first 32 hex characters (equivalent to 128 bits) full_hash[0, 32] end # Example usage input_string = "your_target_data" existing_salt = "your_predefined_salt" result_hash = salted_128bit_sha256(input_string, existing_salt) puts "Salted 128-bit SHA256 hash: #{result_hash}"
If you prefer direct OpenSSL control for more advanced scenarios, use this version:
require "openssl" def salted_128bit_sha256_openssl(input : String, salt : String) : String digest = OpenSSL::Digest.new("SHA256") digest.update(input) digest.update(salt) full_hash = digest.final.hexstring full_hash[0, 32] end # Example usage input_string = "your_target_data" existing_salt = "your_predefined_salt" puts "Salted 128-bit hash (OpenSSL): #{salted_128bit_sha256_openssl(input_string, existing_salt)}"
Approach 2: Replicate MurmurHash3 128-bit Behavior (with Salt)
If you want to match the Python mmh3 library's behavior directly, use a dedicated MurmurHash3 Crystal shard. First add the dependency to your shard.yml:
dependencies: murmurhash3: github: kostya/murmurhash3
Run shards install, then implement the hashing with salt:
require "murmurhash3" def salted_128bit_murmur3(input : String, salt : String) : String # Combine input and salt before hashing combined_data = input + salt # Generate 128-bit MurmurHash3 (returns two 64-bit integers) high_bits, low_bits = MurmurHash3.x64_128(combined_data) # Convert to a 32-character hex string (128 bits total) "%016x%016x" % [high_bits, low_bits] end # Example usage input_string = "your_target_data" existing_salt = "your_predefined_salt" puts "Salted 128-bit MurmurHash3: #{salted_128bit_murmur3(input_string, existing_salt)}"
This will give you behavior nearly identical to the Python mmh3 library, with your custom salt applied.
内容的提问来源于stack exchange,提问作者madcrazydrumma

