Class: Huami

Inherits:
Object
  • Object
show all
Defined in:
lib/huami.rb

Constant Summary collapse

STR1 =
"snow"
STR2 =
"kise"
STR3 =
"sunlovesnow1990090127xykab"

Class Method Summary collapse

Class Method Details

.huami(key, password) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/huami.rb', line 11

def self.huami(key, password)
  md5one    = md5_hmac(key, password)
  md5two    = md5_hmac(STR1, md5one)
  md5three  = md5_hmac(STR2, md5one)

  # 转换大小写
  rule    = md5three.split('')
  source  = md5two.split('')
  for i in (0..31)
    if STR3.include? rule[i]
      source[i] = source[i].upcase
    end
  end

  # 保证首字符为字母
  if is_digit(source[0])
    code16 = "K" + source[1..15].join()
  else
    code16 = source[0..15].join()
  end

  return code16
end

.is_digit(string) ⇒ Object



40
41
42
# File 'lib/huami.rb', line 40

def self.is_digit(string)
  return string.strip =~ /^[0-9]$/
end

.md5_hmac(key, password) ⇒ Object



35
36
37
38
# File 'lib/huami.rb', line 35

def self.md5_hmac(key, password)
  digest = OpenSSL::Digest::Digest.new('md5')
  return OpenSSL::HMAC.hexdigest(digest, key, password)
end