update client key encryption

This commit is contained in:
Thein Lin Kyaw
2023-08-17 15:53:13 +06:30
parent d2b9a9e61f
commit 019902872b
3 changed files with 80 additions and 76 deletions

View File

@@ -1,15 +1,15 @@
class BatchLineItem < ApplicationRecord
belongs_to :batch
attr_accessor :secret_token
attr_accessor :location
attr_accessor :product_type
belongs_to :batch
attr_accessor :secret_token
attr_accessor :location
attr_accessor :product_type
require 'digest/md5'
CARD = "Card"
WRISTBAND = "Wristband"
ACCOUNT_CARD = "Account Card"
def self.generate_account_no(location_code)
super_merchant = "101"
account_type='1'
@@ -20,12 +20,12 @@ class BatchLineItem < ApplicationRecord
account_no = super_merchant+account_type+location+random_account_no
find_account_no = BatchLineItem.find_by_asset_identity(account_no)
if !find_account_no.nil?
if !find_account_no.nil?
o = [('0'..'9')].map { |i| i.to_a }.flatten
random_account_no = (0...9).map { o[rand(o.length)] }.join
account_no = super_merchant+account_type+location+random_account_no
account_no = super_merchant+account_type+location+random_account_no
end
return account_no
return account_no
end
def self.generate_security_code
@@ -33,11 +33,11 @@ class BatchLineItem < ApplicationRecord
security_code = (0...8).map { o[rand(o.length)] }.join
find_account_no = BatchLineItem.find_by_security_code(security_code)
if !find_account_no.nil?
if !find_account_no.nil?
o = [('0'..'8')].map { |i| i.to_a }.flatten
security_code = (0...8).map { o[rand(o.length)] }.join
security_code = (0...8).map { o[rand(o.length)] }.join
end
return security_code
return security_code
end
def self.generate_barcode
@@ -45,12 +45,12 @@ class BatchLineItem < ApplicationRecord
# barcode = (0...8).map { o[rand(o.length)] }.join
barcode= rand(1_000_000_0..9_999_999_9)
find_account_no = BatchLineItem.find_by_barcode(barcode)
if !find_account_no.nil?
if !find_account_no.nil?
# o = [('0'..'9')].map { |i| i.to_a }.flatten
# barcode = (0...8).map { o[rand(o.length)] }.join
barcode = rand(1_000_000_0..9_999_999_9)
# barcode = (0...8).map { o[rand(o.length)] }.join
barcode = rand(1_000_000_0..9_999_999_9)
end
return barcode
return barcode
end
def self.generate_serial_no(client_id)
find_lookup=Lookup.find_by_name('generate_serial_no')
@@ -58,52 +58,52 @@ class BatchLineItem < ApplicationRecord
max_value=find_lookup.max_value
max_value=max_value +1
prefix=find_lookup.prefix
max_length=find_lookup.max_length
sufix_len=max_length-prefix.length
sufix_str="0" * sufix_len
value_len= max_value.to_s.length
max_length=find_lookup.max_length
sufix_len=max_length-prefix.length
sufix_str="0" * sufix_len
value_len= max_value.to_s.length
start=0
ends=sufix_len-value_len-1
sufix_str= sufix_str[start..ends]
prefix_len=prefix.to_s.length
client_len= client_id.to_s.length
ends= prefix_len -client_len -1
ends= prefix_len -client_len -1
prefix=prefix[start..ends]
prefix_str=prefix.to_s + client_id.to_s
serial_no=prefix_str.to_s+sufix_str+max_value.to_s
return serial_no
serial_no=prefix_str.to_s+sufix_str+max_value.to_s
return serial_no
end
end
def self.to_csv(client_id,location_code)
encrypt_key=""
find_client=Client.find_by_id(client_id)
find_client=Client.find_by_id(client_id)
if !find_client.nil?
encrypt_key=find_client.secrect_key
encrypt_key=find_client.secrect_key
end
attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |encoder|
if encoder.product_type_id ==1
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
csv << attributes
all.each do |encoder|
if encoder.product_type_id ==1
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
elsif encoder.product_type_id == 3
attributes = attributes+ %w{security_code}
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s+"&security_code="+encoder.security_code.to_s
else
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
end
else
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
end
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encoder.secret_token=encryptd_data
encoder.location=location_code
encoder.location=location_code
csv << attributes.map{ |attr| encoder.send(attr)}
end
end
@@ -111,50 +111,47 @@ class BatchLineItem < ApplicationRecord
def self.to_csv_seller(client_id,location_code)
encrypt_key=""
find_client=Client.find_by_id(client_id)
find_client=Client.find_by_id(client_id)
if !find_client.nil?
encrypt_key=find_client.secrect_key
encrypt_key=find_client.secrect_key
end
attributes = %w{serial_no asset_identity batch_id manufacture_uid asset_type secret_token location barcode}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |encoder|
if encoder.product_type_id ==1
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
csv << attributes
all.each do |encoder|
if encoder.product_type_id ==1
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s
elsif encoder.product_type_id == 3
attributes = attributes+ %w{security_code}
str="account_no="+encoder.asset_identity.to_s+"&manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s+"&security_code="+encoder.security_code.to_s
else
else
str="manufacture_uid="+encoder.manufacture_uid.to_s+"&serial_no="+encoder.serial_no.to_s+"&barcode="+encoder.barcode.to_s
end
end
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encryptd_data=BatchLineItem.encrypted(str,encrypt_key)
encoder.secret_token=encryptd_data
encoder.location=location_code
encoder.location=location_code
csv << attributes.map{ |attr| encoder.send(attr)}
end
end
end
def self.encrypted(message,encrypt_key)
cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
def self.encrypted(message,encrypt_key)
cipher = OpenSSL::Cipher.new("aes-256-cbc")
cipher.encrypt
key = Digest::SHA1.hexdigest(encrypt_key)
iv =encrypt_key
cipher.key = encrypt_key
cipher.iv = encrypt_key
cipher.iv = encrypt_key[0,16]
encrypted = cipher.update(message)
encrypted << cipher.final
encrypted=Base64.encode64(encrypted)
return encrypted
return encrypted
end
def self.create_product(asset_identity,serial_no,batch_id,manufacture_uid,card_type,security_code=nil)
batchLineItem=BatchLineItem.new
@@ -175,7 +172,7 @@ class BatchLineItem < ApplicationRecord
max_serail_no=lookup.max_value
lookup.max_value=max_serail_no.to_i+1
lookup.save
card_qty=batch.qty_processing.to_i
success_qty=batch.qty_success
@@ -190,15 +187,15 @@ class BatchLineItem < ApplicationRecord
batch.save
@result=false,'Error occurs in registration encoder!'
end
end
end
end
def self.get_array
arr=Array.new
arr=Array.new
(1..100).each do |i|
str= SecureRandom.hex
uid=str[0..13]
str= SecureRandom.hex
uid=str[0..13]
arr.push(uid)
end
@arr=arr
end
end
@arr=arr
end
end