Module: K3cloud::MD5Utils
- Defined in:
- lib/k3cloud/utils/md5_utils.rb
Constant Summary collapse
- UNSECURE_KEY =
"0054f397c6234378b09ca7d3e5debce7"
Class Method Summary collapse
- .bytes_to_hex(bytes) ⇒ Object
-
.encrypt(data_str) ⇒ Object
返回 32 位小写十六进制 MD5。原来的逐字节位运算 hack 在 Ruby 下会得到
nil, 且被 rescue 吞掉返回空串,故直接使用 Digest::MD5.hexdigest。. - .hash_mac(data, secret) ⇒ Object
Class Method Details
.bytes_to_hex(bytes) ⇒ Object
24 25 26 |
# File 'lib/k3cloud/utils/md5_utils.rb', line 24 def self.bytes_to_hex(bytes) bytes.map { |byte| byte.to_s(16).rjust(2, "0") }.join end |
.encrypt(data_str) ⇒ Object
返回 32 位小写十六进制 MD5。原来的逐字节位运算 hack 在 Ruby 下会得到 nil,
且被 rescue 吞掉返回空串,故直接使用 Digest::MD5.hexdigest。
12 13 14 |
# File 'lib/k3cloud/utils/md5_utils.rb', line 12 def self.encrypt(data_str) Digest::MD5.hexdigest(data_str.encode("UTF-8")) end |
.hash_mac(data, secret) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/k3cloud/utils/md5_utils.rb', line 16 def self.hash_mac(data, secret) raise ArgumentError, "secret is required for HMAC" if secret.nil? || secret.to_s.empty? kd_mac = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), secret, data) hex_string = bytes_to_hex(kd_mac.bytes) Base64.strict_encode64(hex_string) end |