Module: Doraemon::AES

Defined in:
lib/doraemon/utils/aes.rb

Constant Summary collapse

TOKEN_KEY =
'idongjia'

Class Method Summary collapse

Class Method Details

.decrypt(key, cipher_text) ⇒ Object

AES 解密



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/doraemon/utils/aes.rb', line 34

def self.decrypt(key, cipher_text)
  result = nil
  begin
    cipher_text = Base64.decode64(cipher_text)
    aes = OpenSSL::Cipher.new("AES-256-ECB")
    aes.decrypt
    aes.key = OpenSSL::Digest::SHA256.new(key).digest
    plain_text = aes.update(cipher_text)
    plain_text << aes.final
    result = plain_text
  rescue Exception => e
    result = nil
  end
  result
end

.decrypt_token(cipher_text) ⇒ Object

解密 token



19
20
21
# File 'lib/doraemon/utils/aes.rb', line 19

def self.decrypt_token(cipher_text)
  decrypt(TOKEN_KEY, cipher_text)
end

.encrypt(key, plain_text) ⇒ Object

AES 加密



24
25
26
27
28
29
30
31
# File 'lib/doraemon/utils/aes.rb', line 24

def self.encrypt(key, plain_text)
  aes = OpenSSL::Cipher.new("AES-256-ECB")
  aes.encrypt
  aes.key = OpenSSL::Digest::SHA256.new(key).digest
  result = aes.update(plain_text)
  result << aes.final
  Base64.encode64(result)
end

.encrypt_token(plain_text) ⇒ Object

生成 token



14
15
16
# File 'lib/doraemon/utils/aes.rb', line 14

def self.encrypt_token(plain_text) 
  encrypt(TOKEN_KEY, plain_text)
end