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

@@ -26,9 +26,8 @@ class ClientsController < ApplicationController
def create def create
@client = Client.new(client_params) @client = Client.new(client_params)
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
key=cipher.random_key secrect_key = cipher.random_key
secrect_key= Base64.encode64(key) @client.secrect_key = secrect_key
@client.secrect_key=secrect_key
respond_to do |format| respond_to do |format|
if @client.save if @client.save

View File

@@ -140,14 +140,11 @@ class BatchLineItem < ApplicationRecord
end end
end end
def self.encrypted(message,encrypt_key) def self.encrypted(message,encrypt_key)
cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc") cipher = OpenSSL::Cipher.new("aes-256-cbc")
cipher.encrypt cipher.encrypt
key = Digest::SHA1.hexdigest(encrypt_key)
iv =encrypt_key
cipher.key = encrypt_key cipher.key = encrypt_key
cipher.iv = encrypt_key cipher.iv = encrypt_key[0,16]
encrypted = cipher.update(message) encrypted = cipher.update(message)
encrypted << cipher.final encrypted << cipher.final

View File

@@ -3,4 +3,12 @@ class Client < ApplicationRecord
validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create
validates :location_code ,presence: { message: "Please enter client location code." } validates :location_code ,presence: { message: "Please enter client location code." }
validates :location_code, :uniqueness => {:message =>"This location code is already taken." } ,on: :create validates :location_code, :uniqueness => {:message =>"This location code is already taken." } ,on: :create
def secrect_key
Base64.decode64(super)
end
def secrect_key=(value)
super(Base64.encode64(value))
end
end end