From 029915d5956b1ac69570fec4e1d4b64f4aab9cac Mon Sep 17 00:00:00 2001 From: Kubuxu Date: Sun, 31 May 2015 14:16:48 +0200 Subject: [PATCH] Add MD5, CRC-32. Add md5sum, base64 programs. Rename sha256 to sha256sum program. Add recipe. --- .../lua/component/data/.autorun.lua | 2 +- .../lua/component/data/bin/base64.lua | 42 +++++++++++++++++++ .../lua/component/data/bin/md5sum.lua | 27 ++++++++++++ .../data/bin/{sha256.lua => sha256sum.lua} | 2 +- .../lua/component/data/lib/data.lua | 13 +++++- .../opencomputers/recipes/default.recipes | 4 ++ .../scala/li/cil/oc/common/init/Items.scala | 5 ++- .../li/cil/oc/server/component/DataCard.scala | 24 +++++++---- 8 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 src/main/resources/assets/opencomputers/lua/component/data/bin/base64.lua create mode 100644 src/main/resources/assets/opencomputers/lua/component/data/bin/md5sum.lua rename src/main/resources/assets/opencomputers/lua/component/data/bin/{sha256.lua => sha256sum.lua} (86%) diff --git a/src/main/resources/assets/opencomputers/lua/component/data/.autorun.lua b/src/main/resources/assets/opencomputers/lua/component/data/.autorun.lua index 9aff88263..f83416f02 100644 --- a/src/main/resources/assets/opencomputers/lua/component/data/.autorun.lua +++ b/src/main/resources/assets/opencomputers/lua/component/data/.autorun.lua @@ -28,4 +28,4 @@ event.listen("component_removed", function(_, address) end return false -- remove listener end -end) \ No newline at end of file +end) diff --git a/src/main/resources/assets/opencomputers/lua/component/data/bin/base64.lua b/src/main/resources/assets/opencomputers/lua/component/data/bin/base64.lua new file mode 100644 index 000000000..155dba981 --- /dev/null +++ b/src/main/resources/assets/opencomputers/lua/component/data/bin/base64.lua @@ -0,0 +1,42 @@ +local shell = require("shell") +local data = require("data") + + +local args, parms = shell.parse(...) +if parms.h or parms.help then + io.stderr:write("See: man base64" .. "\n") + os.exit(true) +end +local encodingfun = nil +local encode +if parms.d or parms.decode then + encodingfun = data.decode64 + encode = false +else + encodingfun = data.encode64 + encode = true +end + +if #args == 0 then + repeat + local read = io.read(encode and 3 or 4) + if read then + io.write(encodingfun(read)) + end + until not read +else + for i = 1, #args do + local file, reason = io.open(shell.resolve(args[i])) + if not file then + io.stderr:write(tostring(reason) .. "\n") + os.exit(false) + end + repeat + local line = file:read(encode and 3 or 4) + if line then + io.write(encodingfun(line)) + end + until not line + file:close() + end +end diff --git a/src/main/resources/assets/opencomputers/lua/component/data/bin/md5sum.lua b/src/main/resources/assets/opencomputers/lua/component/data/bin/md5sum.lua new file mode 100644 index 000000000..6a8a874af --- /dev/null +++ b/src/main/resources/assets/opencomputers/lua/component/data/bin/md5sum.lua @@ -0,0 +1,27 @@ +local shell = require("shell") +local data = require("data") + +local args = shell.parse(...) +if #args == 0 then + local read = "" + repeat + local current = io.read("*a") + read = read .. current + until current ~= "" + io.write(data.toHex(data.md5(read))) +else + for i = 1, #args do + local read = "" + local file, reason = io.open(shell.resolve(args[i])) + if not file then + io.stderr:write(tostring(reason) .. "\n") + os.exit(false) + end + repeat + local current = file:read("*a") + read = read .. current + until current ~= "" + file:close() + io.write(data.toHex(data.md5(read)) .. "\t".. args[i]) + end +end diff --git a/src/main/resources/assets/opencomputers/lua/component/data/bin/sha256.lua b/src/main/resources/assets/opencomputers/lua/component/data/bin/sha256sum.lua similarity index 86% rename from src/main/resources/assets/opencomputers/lua/component/data/bin/sha256.lua rename to src/main/resources/assets/opencomputers/lua/component/data/bin/sha256sum.lua index d31506197..777e2b67c 100644 --- a/src/main/resources/assets/opencomputers/lua/component/data/bin/sha256.lua +++ b/src/main/resources/assets/opencomputers/lua/component/data/bin/sha256sum.lua @@ -22,6 +22,6 @@ else read = read .. current until current ~= "" file:close() - io.write(args[i].. "\t".. data.toHex(data.sha256(read))) + io.write(data.toHex(data.sha256(read)) .. "\t".. args[i]) end end diff --git a/src/main/resources/assets/opencomputers/lua/component/data/lib/data.lua b/src/main/resources/assets/opencomputers/lua/component/data/lib/data.lua index 2afc0f0e4..f5cf7cb75 100644 --- a/src/main/resources/assets/opencomputers/lua/component/data/lib/data.lua +++ b/src/main/resources/assets/opencomputers/lua/component/data/lib/data.lua @@ -32,6 +32,17 @@ function data.sha256(data) return component.data.sha256(data) end +-- Returns raw/binary MD5 hash of data. Common form of presenting SHA is hexadecimal string, see data.toHex. +function data.md5(data) + return component.data.md5(data) +end + +-- Returns raw/binary CRC-32 hash of data. Common form of presenting SHA is hexadecimal string, see data.toHex. +function data.crc32(data) + return component.data.crc32(data) +end + + -- Applies DEFLATE compression. function data.deflate(data) return component.data.deflate(data) @@ -42,4 +53,4 @@ function data.inflate(data) return component.data.inflate(data) end -return data \ No newline at end of file +return data diff --git a/src/main/resources/assets/opencomputers/recipes/default.recipes b/src/main/resources/assets/opencomputers/recipes/default.recipes index 2c70a1ff1..c4c8eff71 100644 --- a/src/main/resources/assets/opencomputers/recipes/default.recipes +++ b/src/main/resources/assets/opencomputers/recipes/default.recipes @@ -144,6 +144,10 @@ hdd3 { ["oc:materialCircuitBoardPrinted", "oc:materialDisk", craftingPiston] ["oc:circuitChip3", "oc:materialDisk", diamond]] } +dataCard { + input: [[nuggetIron, "oc:materialALU", "oc:circuitChip1"] + ["", "oc:materialCard", ""]] +} graphicsCard1 { input: [["oc:circuitChip1", "oc:materialALU", "oc:ram1"] ["", "oc:materialCard", ""]] diff --git a/src/main/scala/li/cil/oc/common/init/Items.scala b/src/main/scala/li/cil/oc/common/init/Items.scala index d5d324422..d50e1ec0d 100644 --- a/src/main/scala/li/cil/oc/common/init/Items.scala +++ b/src/main/scala/li/cil/oc/common/init/Items.scala @@ -14,7 +14,8 @@ import li.cil.oc.common.Loot import li.cil.oc.common.Tier import li.cil.oc.common.block.SimpleBlock import li.cil.oc.common.item -import li.cil.oc.common.item.{DataCard, Delegator, UpgradeLeash} +import li.cil.oc.common.item.Delegator +import li.cil.oc.common.item.UpgradeLeash import li.cil.oc.common.item.data.DroneData import li.cil.oc.common.item.data.HoverBootsData import li.cil.oc.common.item.data.MicrocontrollerData @@ -539,6 +540,6 @@ object Items extends ItemAPI { registerItem(new item.APU(multi, Tier.Three), Constants.ItemName.APUCreative) // 1.5.13 - Recipes.addSubItem(new DataCard(multi), Constants.ItemName.DataCard, "oc:dataCard") + Recipes.addSubItem(new item.DataCard(multi), Constants.ItemName.DataCard, "oc:dataCard") } } diff --git a/src/main/scala/li/cil/oc/server/component/DataCard.scala b/src/main/scala/li/cil/oc/server/component/DataCard.scala index b366ee40c..83e60a6c6 100644 --- a/src/main/scala/li/cil/oc/server/component/DataCard.scala +++ b/src/main/scala/li/cil/oc/server/component/DataCard.scala @@ -3,6 +3,7 @@ package li.cil.oc.server.component import java.security.MessageDigest import java.util.zip.{InflaterOutputStream, DeflaterOutputStream} +import com.google.common.hash.Hashing import li.cil.oc.util.ExtendedNBT._ import li.cil.oc.{Settings, OpenComputers, api} import li.cil.oc.api.machine.{Arguments, Context, Callback} @@ -21,17 +22,17 @@ class DataCard extends prefab.ManagedEnvironment{ val romData = Option(api.FileSystem.asManagedEnvironment(api.FileSystem. fromClass(OpenComputers.getClass, Settings.resourceDomain, "lua/component/data"), "data")) - @Callback(direct = true, doc = """function(data:string):string -- Applies base64 encoding to the data.""") + @Callback(direct = true, doc = """function(data:string):string -- Applies base64 encoding to the data.""", limit = 32) def encode64(context: Context, args: Arguments): Array[AnyRef] = { result(Base64.encodeBase64(args.checkByteArray(0))) } - @Callback(direct = true, doc = """function(data:string):string -- Applies base64 decoding to the data.""") + @Callback(direct = true, doc = """function(data:string):string -- Applies base64 decoding to the data.""", limit = 32) def decode64(context: Context, args: Arguments): Array[AnyRef] = { result(Base64.decodeBase64(args.checkByteArray(0))) } - @Callback(direct = true, doc = """function(data:string):string -- Applies deflate compression to the data.""") + @Callback(direct = true, doc = """function(data:string):string -- Applies deflate compression to the data.""", limit = 6) def deflate(context: Context, args: Arguments): Array[AnyRef] = { val baos = new ByteArrayOutputStream(512) val deos = new DeflaterOutputStream(baos) @@ -40,7 +41,7 @@ class DataCard extends prefab.ManagedEnvironment{ result(baos.toByteArray) } - @Callback(direct = true, doc = """function(data:string):string -- Applies inflate decompression to the data.""") + @Callback(direct = true, doc = """function(data:string):string -- Applies inflate decompression to the data.""", limit = 6) def inflate(context: Context, args: Arguments): Array[AnyRef] = { val baos = new ByteArrayOutputStream(512) val inos = new InflaterOutputStream(baos) @@ -49,14 +50,19 @@ class DataCard extends prefab.ManagedEnvironment{ result(baos.toByteArray) } - @Callback(direct = true, doc = """function(data:string):string -- Computes SHA2-256 hash of the data.""") + @Callback(direct = true, doc = """function(data:string):string -- Computes SHA2-256 hash of the data. Result is in binary format.""", limit = 32) def sha256(context: Context, args: Arguments): Array[AnyRef] = { - result(Helper.sha256(args.checkByteArray(0))) + result(Hashing.sha256().hashBytes(args.checkByteArray(0)).asBytes()) } - private object Helper { - val sha256asher = MessageDigest.getInstance("SHA-256") - def sha256(data: Array[Byte]) = sha256asher.clone.asInstanceOf[MessageDigest].digest(data) + @Callback(direct = true, doc = """function(data:string):string -- Computes MD5 hash of the data. Result is in binary format""", limit = 32) + def md5(context: Context, args: Arguments): Array[AnyRef] = { + result(Hashing.md5().hashBytes(args.checkByteArray(0)).asBytes()) + } + + @Callback(direct = true, doc = """function(data:string):string -- Computes CRC-32 hash of the data. Result is in binary format""", limit = 32) + def crc32(context: Context, args: Arguments): Array[AnyRef] = { + result(Hashing.crc32().hashBytes(args.checkByteArray(0)).asBytes()) } // ----------------------------------------------------------------------- //