Module: Ktct::Cryptable
- Included in:
- Baofu, Wangjinshe
- Defined in:
- lib/ktct/cryptable.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #crypt(message, type) ⇒ Object
- #decrypt(message) ⇒ Object
- #encrypt(message) ⇒ Object
- #max_decrypt_block_size ⇒ Object
- #max_encrypt_block_size ⇒ Object
- #modulus_byte_size ⇒ Object
- #reset_key(key_file, passphrase = nil) ⇒ Object
- #ssign(message) ⇒ Object
- #stupid_decrypt(message) ⇒ Object
- #stupid_decrypt_batch(message) ⇒ Object
- #stupid_encrypt(message) ⇒ Object
- #stupid_encrypt_batch(message) ⇒ Object
- #sverify(signature, message) ⇒ Object
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
13 14 15 |
# File 'lib/ktct/cryptable.rb', line 13 def key @key end |
Instance Method Details
#crypt(message, type) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ktct/cryptable.rb', line 47 def crypt(, type) bytes_array = .unpack('C*') input_length = bytes_array.length result, offset, i = "", 0, 0 block_size = send("max_#{type.to_s.split('_').last}_block_size") begin bytes = bytes_array[offset, block_size] result << key.send(type, bytes.pack("C*")) offset = (i += 1) * block_size end while input_length - offset > 0 result end |
#decrypt(message) ⇒ Object
19 20 21 |
# File 'lib/ktct/cryptable.rb', line 19 def decrypt() Base64.strict_decode64(private_decrypt([].pack('H*'))) end |
#encrypt(message) ⇒ Object
15 16 17 |
# File 'lib/ktct/cryptable.rb', line 15 def encrypt() public_encrypt(Base64.strict_encode64()).unpack('H*').first end |
#max_decrypt_block_size ⇒ Object
66 67 68 |
# File 'lib/ktct/cryptable.rb', line 66 def max_decrypt_block_size modulus_byte_size end |
#max_encrypt_block_size ⇒ Object
62 63 64 |
# File 'lib/ktct/cryptable.rb', line 62 def max_encrypt_block_size modulus_byte_size - 11 end |
#modulus_byte_size ⇒ Object
70 71 72 |
# File 'lib/ktct/cryptable.rb', line 70 def modulus_byte_size key.n.to_s(16).size / 2 end |
#reset_key(key_file, passphrase = nil) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/ktct/cryptable.rb', line 74 def reset_key(key_file, passphrase=nil) key_file = File.(key_file) if key_file =~ /pfx/ @key = OpenSSL::PKCS12.new(IO.read(key_file), passphrase).key else @key = OpenSSL::X509::Certificate.new(IO.read(key_file)).public_key end end |
#ssign(message) ⇒ Object
27 28 29 |
# File 'lib/ktct/cryptable.rb', line 27 def ssign() key.sign('sha1', Digest::SHA1.hexdigest()).unpack('H*').first end |
#stupid_decrypt(message) ⇒ Object
31 32 33 |
# File 'lib/ktct/cryptable.rb', line 31 def stupid_decrypt() Base64.decode64(public_decrypt([].pack('H*'))).force_encoding('UTF-8') end |
#stupid_decrypt_batch(message) ⇒ Object
39 40 41 |
# File 'lib/ktct/cryptable.rb', line 39 def stupid_decrypt_batch() Zlib.gunzip(public_decrypt([].pack('H*'))).force_encoding('UTF-8') end |
#stupid_encrypt(message) ⇒ Object
35 36 37 |
# File 'lib/ktct/cryptable.rb', line 35 def stupid_encrypt() private_encrypt(Base64.encode64()).unpack('H*').first end |
#stupid_encrypt_batch(message) ⇒ Object
43 44 45 |
# File 'lib/ktct/cryptable.rb', line 43 def stupid_encrypt_batch() private_encrypt(Zlib.gzip()).unpack('H*').first end |
#sverify(signature, message) ⇒ Object
23 24 25 |
# File 'lib/ktct/cryptable.rb', line 23 def sverify(signature, ) key.verify('sha1', [signature].pack('H*'), Digest::SHA1.hexdigest()) end |