Class: IdCipher::IntegerCipher
- Inherits:
-
Object
- Object
- IdCipher::IntegerCipher
- Defined in:
- lib/id_cipher.rb
Instance Method Summary collapse
- #decrypt(eid) ⇒ Object
- #encrypt(id) ⇒ Object
-
#initialize ⇒ IntegerCipher
constructor
this crypto method use ARC4 cipher integer ARC4加密算法优点是速度快, 基于bit位xor的算法 该算法在一定数量后会重复,既存在最大值,超过最大值后无法区分当前是原文还是密文 该算法设计只用于加密正整数,加密范围为整数长整型.
- #key ⇒ Object
- #pack(number) ⇒ Object
- #unpack(crypto_text) ⇒ Object
Constructor Details
#initialize ⇒ IntegerCipher
this crypto method use ARC4 cipher integer ARC4加密算法优点是速度快, 基于bit位xor的算法该算法在一定数量后会重复,既存在最大值,超过最大值后无法区分当前是原文还是密文该算法设计只用于加密正整数,加密范围为整数长整型
30 31 32 |
# File 'lib/id_cipher.rb', line 30 def initialize @cipher = OpenSSL::Cipher::Cipher.new('rc4') end |
Instance Method Details
#decrypt(eid) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/id_cipher.rb', line 55 def decrypt(eid) eid = eid.to_i @cipher.decrypt @cipher.key = key self.unpack @cipher.update(self.pack(eid)) end |
#encrypt(id) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/id_cipher.rb', line 48 def encrypt(id) id = id.to_i @cipher.encrypt @cipher.key = key self.unpack @cipher.update(self.pack(id)) end |
#key ⇒ Object
34 35 36 37 38 |
# File 'lib/id_cipher.rb', line 34 def key (defined? Setting) && Setting.id_cipher_key || IdCipher::KEY || (raise TypeError, 'no cipher key found. you can set key within Settings.yml, id_cipher_key="cipher slat" or refine IdCipher::KEY') end |
#pack(number) ⇒ Object
40 41 42 |
# File 'lib/id_cipher.rb', line 40 def pack(number) [number].pack('L') end |
#unpack(crypto_text) ⇒ Object
44 45 46 |
# File 'lib/id_cipher.rb', line 44 def unpack(crypto_text) crypto_text.unpack('L')[0] end |