Module: Paytm::EncryptionNewPG

Defined in:
lib/paytm/encryption_new_pg.rb

Class Method Summary collapse

Class Method Details

.new_pg_checksum(params, key, salt_length = 4) ⇒ Object

function returns checksum of given key value pairs ### accepts a hash with key value pairs ### calculates sha256 checksum of given values ###



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/paytm/encryption_new_pg.rb', line 133

def self.new_pg_checksum(params, key, salt_length = 4)
  if params.class != Hash
    return false
  end
  if key.empty?
    return false
  end
  salt = new_pg_generate_salt(salt_length)
  keys = params.keys
  str = nil
  keys = keys.sort
  keys.each do |k|
    if str.nil?
      str = params[k].to_s
      next
    end
    str = str + '|'  + params[k].to_s
  end
  str = str + '|' + salt
  check_sum = Digest::SHA256.hexdigest(str)
  check_sum = check_sum + salt
  ### encrypting checksum ###
  check_sum = new_pg_encrypt_variable(check_sum, key)
  return check_sum.gsub("\n",'')
end

.new_pg_checksum_by_str(paramstr, key, salt_length = 4) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/paytm/encryption_new_pg.rb', line 160

def self.new_pg_checksum_by_str(paramstr, key, salt_length = 4)

  salt = new_pg_generate_salt(salt_length)

  str = nil

  str = paramstr + '|' + salt
  check_sum = Digest::SHA256.hexdigest(str)
  check_sum = check_sum + salt
  ### encrypting checksum ###
  check_sum = new_pg_encrypt_variable(check_sum, key)
  return check_sum.gsub("\n",'')
end

.new_pg_decrypt(params) ⇒ Object

function returns dictionary of decrypted data ### accepts a dictionary with data and key to decrypt with ### can accept multiple key value pairs in the dictionary ###



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/paytm/encryption_new_pg.rb', line 69

def self.new_pg_decrypt(params)
  if (params.class != Hash) || (params.keys == [])
    return false
  end
  if !params.has_key?(:key)
    return false
  end
  decrypted_data = Hash[]
  key = params.delete(:key)
  keys = params.keys
  aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  begin
    keys.each do |k|
      data = params[k]
      aes.decrypt
      aes.key = key
      aes.iv = '@@@@&&&&####$$$$'
      decrypted_k = Base64.decode64(k.to_s)
      decrypted_k = aes.update(decrypted_k.to_s) + aes.final
      if data.empty?
        decrypted_data[decrypted_k] = ""
        next
      end
      aes.decrypt
      aes.key = key
      aes.iv = '@@@@&&&&####$$$$'
      data = Base64.decode64(data)
      decrypted_data[decrypted_k] = aes.update(data) + aes.final
    end
  rescue Exception => e
    return false
  end
  return decrypted_data
end

.new_pg_decrypt_variable(data, key) ⇒ Object

function returns a single decrypted value ### input data -> value to be decrypted ### key -> key to use for decryption ###



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/paytm/encryption_new_pg.rb', line 108

def self.new_pg_decrypt_variable(data, key)
  aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes.decrypt
  aes.key = key
  aes.iv = '@@@@&&&&####$$$$'
  decrypted_data = nil
  begin
    decrypted_data = Base64.decode64(data.to_s)
    decrypted_data = aes.update(decrypted_data) + aes.final
  rescue Exception => e
    return false
  end
  return decrypted_data
end

.new_pg_encrypt(params) ⇒ Object

function returns dictionary of encrypted data ### accepts a dictionary with data and key to encrypt with ### can accept multiple key value pairs in the dictionary ###



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/paytm/encryption_new_pg.rb', line 16

def self.new_pg_encrypt(params)
  if (params.class != Hash) || (params.keys == [])
    return false
  end
  if !params.has_key?(:key)
    return false
  end
  encrypted_data = Hash[]
  key = params.delete(:key)
  keys = params.keys
  aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  begin
    keys.each do |k|
      data = params[k]
      aes.encrypt
      aes.key = key
      aes.iv = '@@@@&&&&####$$$$'
      encrypted_k = aes.update(k.to_s) + aes.final
      encrypted_k = Base64.encode64(encrypted_k.to_s)
      aes.encrypt
      aes.key = key
      aes.iv = '@@@@&&&&####$$$$'
      encrypted_data[encrypted_k] = aes.update(data.to_s) + aes.final
      encrypted_data[encrypted_k] = Base64.encode64(encrypted_data[encrypted_k])
    end
  rescue Exception => e
    return false
  end
  return encrypted_data
end

.new_pg_encrypt_variable(data, key) ⇒ Object

function returns a single encrypted value ### input data -> value to be encrypted ### key -> key to use for encryption ###



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/paytm/encryption_new_pg.rb', line 50

def self.new_pg_encrypt_variable(data, key)
  aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes.encrypt
  aes.key = key
  aes.iv = '@@@@&&&&####$$$$'
  encrypted_data = nil
  begin
    encrypted_data = aes.update(data.to_s) + aes.final
    encrypted_data = Base64.encode64(encrypted_data)
  rescue Exception => e
    return false
  end
  return encrypted_data
end

.new_pg_generate_salt(length) ⇒ Object



124
125
126
127
# File 'lib/paytm/encryption_new_pg.rb', line 124

def self.new_pg_generate_salt(length)
  salt = SecureRandom.urlsafe_base64(length*(3.0/4.0))
  return salt.to_s
end

.new_pg_verify_checksum(params, check_sum, key, salt_length = 4) ⇒ Object

function returns checksum of given key value pairs (must contain the :checksum key) ### accepts a hash with key value pairs ### calculates sha256 checksum of given values ### returns true if checksum is consistent ### returns false in case of inconsistency ###



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/paytm/encryption_new_pg.rb', line 180

def self.new_pg_verify_checksum(params, check_sum, key, salt_length = 4)

  if params.class != Hash
    return false
  end

  if key.empty?
    return false
  end

  if check_sum.nil? || check_sum.empty?
    return false
  end

  generated_check_sum = nil
  check_sum = new_pg_decrypt_variable(check_sum, key)

  if check_sum == false
    return false
  end
  begin
    salt = check_sum[(check_sum.length-salt_length), (check_sum.length)]
    keys = params.keys
    str = nil
    keys = keys.sort
    keys.each do |k|
      if str.nil?
        str = params[k].to_s
        next
      end
      str = str + '|' + params[k].to_s
    end
    str = str + '|' + salt
    generated_check_sum = Digest::SHA256.hexdigest(str)
    generated_check_sum = generated_check_sum + salt
  rescue Exception => e
    return false
  end

  if check_sum == generated_check_sum
    return true
  else
    return false
  end
end