Skip to content

Commit

Permalink
chore(formatting): lua format the copy/pasta code
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed May 13, 2024
1 parent 43a1ef6 commit 9fd6472
Show file tree
Hide file tree
Showing 29 changed files with 7,896 additions and 3,572 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ node_modules
/.luarocks
wallet.json
.DS_Store
luacov-html
139 changes: 60 additions & 79 deletions contract/src/crypto/cipher/aes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,35 @@ local ZeroPadding = require(".crypto.padding.zero")
local public = {}

local getBlockCipher = function(keyLength)
if keyLength == 128 then
return AES128Cipher
elseif keyLength == 192 then
return AES192Cipher
elseif keyLength == 256 then
return AES256Cipher
elseif keyLength == nil then
return AES128Cipher
else
return nil
end
if keyLength == 128 then
return AES128Cipher
elseif keyLength == 192 then
return AES192Cipher
elseif keyLength == 256 then
return AES256Cipher
elseif keyLength == nil then
return AES128Cipher
else
return nil
end
end

local getMode = function(mode)
if mode == "CBC" then
return CBCMode
elseif mode == "ECB" then
return ECBMode
elseif mode == "CFB" then
return CFBMode
elseif mode == "OFB" then
return OFBMode
elseif mode == "CTR" then
return CTRMode
else
return nil
end
if mode == "CBC" then
return CBCMode
elseif mode == "ECB" then
return ECBMode
elseif mode == "CFB" then
return CFBMode
elseif mode == "OFB" then
return OFBMode
elseif mode == "CTR" then
return CTRMode
else
return nil
end
end


--- Encrypts the given data using AES encryption.
--- @param data string - The data to be encrypted.
--- @param key string - The key to use for encryption.
Expand All @@ -58,40 +57,32 @@ end
--- @param keyLength? number (optional) - The length of the key to use for encryption. Defaults to 128.
--- @returns table - A table containing the encrypted data in bytes, hex, and string formats.
public.encrypt = function(data, key, iv, mode, keyLength)
local d = Array.fromString(data)
local k = Array.fromString(key)
local _iv = iv ~= nil and Array.fromString(iv) or Array.fromHex("00000000000000000000000000000000")

local cipherMode = getMode(mode) or CBCMode
local blockCipher = getBlockCipher(keyLength) or AES128Cipher
local d = Array.fromString(data)
local k = Array.fromString(key)
local _iv = iv ~= nil and Array.fromString(iv) or Array.fromHex("00000000000000000000000000000000")

local cipher = cipherMode.Cipher()
.setKey(k)
.setBlockCipher(blockCipher)
.setPadding(ZeroPadding);
local cipherMode = getMode(mode) or CBCMode
local blockCipher = getBlockCipher(keyLength) or AES128Cipher

local cipher = cipherMode.Cipher().setKey(k).setBlockCipher(blockCipher).setPadding(ZeroPadding)

local cipherOutput = cipher
.init()
.update(Stream.fromArray(_iv))
.update(Stream.fromArray(d))
.finish()
local cipherOutput = cipher.init().update(Stream.fromArray(_iv)).update(Stream.fromArray(d)).finish()

local results = {}
local results = {}

results.asBytes = function()
return cipherOutput.asBytes()
end
results.asBytes = function()
return cipherOutput.asBytes()
end

results.asHex = function()
return cipherOutput.asHex()
end
results.asHex = function()
return cipherOutput.asHex()
end

results.asString = function()
return cipherOutput.asString()
end
results.asString = function()
return cipherOutput.asString()
end

return results
return results
end

--- Decrypts the given data using AES decryption.
Expand All @@ -101,42 +92,32 @@ end
--- @param mode? string (optional) - The mode to use for decryption. Defaults to "CBC".
--- @param keyLength? number (optional) - The length of the key to use for decryption. Defaults to 128.
public.decrypt = function(cipher, key, iv, mode, keyLength)
local cipherText = Array.fromHex(cipher)
local k = Array.fromString(key)
local _iv = iv ~= nil and Array.fromString(iv) or Array.fromHex("00000000000000000000000000000000")

local cipherMode = getMode(mode) or CBCMode
local blockCipher = getBlockCipher(keyLength) or AES128Cipher
local cipherText = Array.fromHex(cipher)
local k = Array.fromString(key)
local _iv = iv ~= nil and Array.fromString(iv) or Array.fromHex("00000000000000000000000000000000")

local cipherMode = getMode(mode) or CBCMode
local blockCipher = getBlockCipher(keyLength) or AES128Cipher

local decipher = cipherMode.Decipher()
.setKey(k)
.setBlockCipher(blockCipher)
.setPadding(ZeroPadding);
local decipher = cipherMode.Decipher().setKey(k).setBlockCipher(blockCipher).setPadding(ZeroPadding)

local plainOutput = decipher.init().update(Stream.fromArray(_iv)).update(Stream.fromArray(cipherText)).finish()

local plainOutput = decipher
.init()
.update(Stream.fromArray(_iv))
.update(Stream.fromArray(cipherText))
.finish()
local results = {}

local results = {}
results.asBytes = function()
return plainOutput.asBytes()
end

results.asBytes = function()
return plainOutput.asBytes()
end
results.asHex = function()
return plainOutput.asHex()
end

results.asHex = function()
return plainOutput.asHex()
end
results.asString = function()
return plainOutput.asString()
end

results.asString = function()
return plainOutput.asString()
end

return results
return results
end


return public
Loading

0 comments on commit 9fd6472

Please sign in to comment.