Add MD5, CRC-32. Add md5sum, base64 programs. Rename sha256 to sha256sum program. Add recipe.

This commit is contained in:
Kubuxu 2015-05-31 14:16:48 +02:00
parent 657bc7dcdf
commit 029915d595
8 changed files with 105 additions and 14 deletions

View File

@ -28,4 +28,4 @@ event.listen("component_removed", function(_, address)
end
return false -- remove listener
end
end)
end)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
return data

View File

@ -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", ""]]

View File

@ -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")
}
}

View File

@ -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())
}
// ----------------------------------------------------------------------- //