Module: EncryptionNewPG

Defined in:
lib/paytm/merchant/encryption_new_p_g.rb

Overview

New PG Encryption ##########################################

Instance Method Summary collapse

Instance Method Details

#new_pg_checksum(paytmparams, 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 ###



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 137

def new_pg_checksum(paytmparams, key, salt_length = 4)
  if paytmparams.class != Hash
    return false
  end
  if key.empty?
    return false
  end
  salt = new_pg_generate_salt(salt_length)
  keys = paytmparams.keys
  str = nil
  keys = keys.sort
  keys.each do |k|
    if str.nil?
      str = paytmparams[k].to_s
      next
    end
    str = str + '|'  + paytmparams[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
end

#new_pg_checksum_by_String(strdata, key, salt_length = 4) ⇒ Object

function returns checksum of given String ### accepts a hash with String ### calculates sha256 checksum of given values ###



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 166

def new_pg_checksum_by_String(strdata, key, salt_length = 4)
  if strdata.empty?
    return false
  end
  if key.empty?
    return false
  end
  salt = new_pg_generate_salt(salt_length)
  str = nil
  str = strdata + '|' + 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
end

#new_pg_decrypt(paytmparams) ⇒ 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 ###



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
103
104
105
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 71

def new_pg_decrypt(paytmparams)
  if (paytmparams.class != Hash) || (paytmparams.keys == [])
    return false
  end
  if !paytmparams.has_key?(:key)
    return false
  end
  decrypted_data = Hash[]
  key = paytmparams.delete(:key)
  keys = paytmparams.keys
  ##aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes = OpenSSL::Cipher::AES.new('128-CBC')
  begin
    keys.each do |k|
      data = paytmparams[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 ###



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 111

def new_pg_decrypt_variable(data, key)
  ##aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes = OpenSSL::Cipher::AES.new('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(paytmparams) ⇒ 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
46
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 16

def new_pg_encrypt(paytmparams)
  if (paytmparams.class != Hash) || (paytmparams.keys == [])
    return false
  end
  if !paytmparams.has_key?(:key)
    return false
  end
  encrypted_data = Hash[]
  key = paytmparams.delete(:key)
  keys = paytmparams.keys
  ###aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes = OpenSSL::Cipher::AES.new('128-CBC')
  begin
    keys.each do |k|
      data = paytmparams[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 ###



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 51

def new_pg_encrypt_variable(data, key)
  ##aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes = OpenSSL::Cipher::AES.new('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



128
129
130
131
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 128

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

#new_pg_refund_checksum(paytmparams, 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 ###



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
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 186

def new_pg_refund_checksum(paytmparams, key, salt_length = 4)
  keys = paytmparams.keys
  keys.each do |k|
    if ! paytmparams[k].empty?
      #if params[k].to_s.include? "REFUND"
      unless paytmparams[k].to_s.include? "|"
          next
      end
      paytmparams[k] = paytmparams[k]
    end
  end
  if paytmparams.class != Hash
    return false
  end
  if key.empty?
    return false
  end
  salt = new_pg_generate_salt(salt_length)
  keys = paytmparams.keys
  str = nil
  keys = keys.sort
  keys.each do |k|
    if str.nil?
      str = paytmparams[k].to_s
      next
    end
    str = str + '|'  + paytmparams[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
end

#new_pg_verify_checksum(paytmparams, 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 ###



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 262

def new_pg_verify_checksum(paytmparams, check_sum, key, salt_length = 4)
  if paytmparams.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 = paytmparams.keys
    str = nil
    keys = keys.sort
    keys.each do |k|
      if str.nil?
        str = paytmparams[k].to_s
        next
      end
      str = str + '|' + paytmparams[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

#new_pg_verify_checksum_by_String(strdata, 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 ###



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/paytm/merchant/encryption_new_p_g.rb', line 227

def new_pg_verify_checksum_by_String(strdata, check_sum, key, salt_length = 4)
  if strdata.empty?
    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)]
    str = strdata + '|' + 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