Module: MD5Utils

Defined in:
lib/k3cloud/utils/md5_utils.rb

Class Method Summary collapse

Class Method Details

.bytes_to_hex(bytes) ⇒ Object



32
33
34
# File 'lib/k3cloud/utils/md5_utils.rb', line 32

def self.bytes_to_hex(bytes)
  bytes.map { |byte| byte.to_s(16).rjust(2, "0") }.join
end

.encrypt(data_str) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/k3cloud/utils/md5_utils.rb', line 7

def self.encrypt(data_str)
  m = Digest::MD5.new
  m.update(data_str.encode("UTF-8"))
  s = m.digest
  result = ""

  s.each_byte do |byte|
    result += (byte & 0xFF | -256).to_s(16)[6..-1]
  end

  result
rescue StandardError => e
  e.backtrace
  ""
end

.hash_mac(data, secret) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/k3cloud/utils/md5_utils.rb', line 23

def self.hash_mac(data, secret)
  kd_mac = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), secret, data)
  hex_string = bytes_to_hex(kd_mac.bytes)
  Base64.strict_encode64(hex_string)
rescue StandardError => e
  e.backtrace
  nil
end