Module: Ktct::Cryptable

Included in:
Baofu, Wangjinshe
Defined in:
lib/ktct/cryptable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

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(message, type)
  bytes_array = message.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(message)
  Base64.strict_decode64(private_decrypt([message].pack('H*')))
end

#encrypt(message) ⇒ Object



15
16
17
# File 'lib/ktct/cryptable.rb', line 15

def encrypt(message)
  public_encrypt(Base64.strict_encode64(message)).unpack('H*').first
end

#max_decrypt_block_sizeObject



66
67
68
# File 'lib/ktct/cryptable.rb', line 66

def max_decrypt_block_size
  modulus_byte_size
end

#max_encrypt_block_sizeObject



62
63
64
# File 'lib/ktct/cryptable.rb', line 62

def max_encrypt_block_size
  modulus_byte_size - 11
end

#modulus_byte_sizeObject



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.expand_path(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(message)
  key.sign('sha1', Digest::SHA1.hexdigest(message)).unpack('H*').first
end

#stupid_decrypt(message) ⇒ Object



31
32
33
# File 'lib/ktct/cryptable.rb', line 31

def stupid_decrypt(message)
  Base64.decode64(public_decrypt([message].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(message)
  Zlib.gunzip(public_decrypt([message].pack('H*'))).force_encoding('UTF-8')
end

#stupid_encrypt(message) ⇒ Object



35
36
37
# File 'lib/ktct/cryptable.rb', line 35

def stupid_encrypt(message)
  private_encrypt(Base64.encode64(message)).unpack('H*').first
end

#stupid_encrypt_batch(message) ⇒ Object



43
44
45
# File 'lib/ktct/cryptable.rb', line 43

def stupid_encrypt_batch(message)
  private_encrypt(Zlib.gzip(message)).unpack('H*').first
end

#sverify(signature, message) ⇒ Object



23
24
25
# File 'lib/ktct/cryptable.rb', line 23

def sverify(signature, message)
  key.verify('sha1', [signature].pack('H*'), Digest::SHA1.hexdigest(message))
end