This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
crazyadam0's profile picture

Hash of an entire file?

Started by crazyadam0, 10 April 2014 - 01:40 AM
crazyadam0 #1
Posted 10 April 2014 - 03:40 AM
A while back GravityScore made a program that computes the sha256 hash of a string. This is good for using to store a salted password but what if you want to use it for creating a checksum to verify data integrity, in other words see if a file has been modified by a malicious program or a person who knows what the edit command does :P/>. I thought this would make for a really good anti-virus and auto updater by taking the checksum of all executable files used in an operating system and comparing them to a list downloaded by maybe pastebin. The problem there is that the function only accepts strings and i believe there is a character limit in lua as with probably every language. This would be very easily exceeded by man programs. Is there a way of taking the hash of an ENTIRE FILE or is this the wrong way to go about what i'm trying to do.
Edited on 10 April 2014 - 01:41 AM
theoriginalbit #2
Posted 10 April 2014 - 05:20 AM
well according to the Java — ComputerCraft uses LuaJ which is a Java implementation of Lua — spec the max size String is Integer.MAX_VALUE, or 231-1 characters in length. If you manage to have a file read into a string and its larger than 2,147,483,647 then you may want to consider refactoring your code so its smaller!

So in summary, yes, you can use GravityScores SHA256 hashing function; although there's probably better things you could use.
Edited on 10 April 2014 - 03:21 AM
crazyadam0 #3
Posted 10 April 2014 - 07:57 AM
So in theory I could just

local f = fs.open("/path","r")
local checksum = sha256(textutils.serialize(f.readAll()))
and it would give me a value? I will test it tomorrow, it's almost midnight where I live and I should be off to bed.
theoriginalbit #4
Posted 10 April 2014 - 08:09 AM
So in theory I could just

local f = fs.open("/path","r")
local checksum = sha256(textutils.serialize(f.readAll()))
and it would give me a value? I will test it tomorrow, it's almost midnight where I live and I should be off to bed.
no not quite.

local f = fs.open( '/path', 'r' )
local checksum = sha256( f.readAll() ) --# contents are a string, not a table, no need to serialise
f.close() --# don't forget to close
Edited on 10 April 2014 - 11:45 AM
apemanzilla #5
Posted 10 April 2014 - 01:28 PM
So in theory I could just

local f = fs.open("/path","r")
local checksum = sha256(textutils.serialize(f.readAll()))
and it would give me a value? I will test it tomorrow, it's almost midnight where I live and I should be off to bed.
no not quite.

local h = fs.open( '/path', 'r' )
local checksum = sha256( f.readAll() ) --# contents are a string, not a table, no need to serialise
f.close() --# don't forget to close
Nope, not quite…

local f = fs.open( '/path', 'r' ) --# you defined it as 'h' instead of 'f'
local checksum = sha256( f.readAll() ) --# contents are a string, not a table, no need to serialise
f.close() --# don't forget to close
;)/>
theoriginalbit #6
Posted 10 April 2014 - 01:46 PM
Nope, not quite…
Shhh… no one is to know, lets keep this our little secret. ;)/>
Thanks, I was clearly very distracted when typing this and went muscle memory; I use `h`.
apemanzilla #7
Posted 10 April 2014 - 01:51 PM
Nope, not quite…
Shhh… no one is to know, lets keep this our little secret. ;)/>
Thanks, I was clearly very distracted when typing this and went muscle memory; I use `h`.
Hehe :P/>
If you want to make it a one-liner, you can use this:

local checksum = sha256(fs.open("path","r").readAll())
theoriginalbit #8
Posted 10 April 2014 - 02:33 PM
If you want to make it a one-liner, you can use this:

local checksum = sha256(fs.open("path","r").readAll())
Except for the lingering file handle. while in the case of ComputerCraft it can be debated acceptable, it is definitely not good practice.
CometWolf #9
Posted 10 April 2014 - 02:33 PM
I've exprimented with onelining the fs api in the past, you're kinda screwed since you can't close the file afterwards :P/>
crazyadam0 #10
Posted 10 April 2014 - 11:12 PM
Well, thanks for the help guys, I'm just glad that there isn't a 256 char limit to strings like SOME languages I know. Very glad.
theoriginalbit #11
Posted 10 April 2014 - 11:49 PM
Well, thanks for the help guys, I'm just glad that there isn't a 256 char limit to strings like SOME languages I know. Very glad.
Which languages are those? I know of none. I do know Pascal / Delphi, SQL, and a few others have a max string length of 255 chars, but I'm unaware of any language that is 256. All languages that I know of use null terminated strings, meaning you only get 255 chars due to the last char being null.
AgentE382 #12
Posted 12 April 2014 - 11:45 PM
Well, thanks for the help guys, I'm just glad that there isn't a 256 char limit to strings like SOME languages I know. Very glad.
Which languages are those? I know of none. I do know Pascal / Delphi, SQL, and a few others have a max string length of 255 chars, but I'm unaware of any language that is 256. All languages that I know of use null terminated strings, meaning you only get 255 chars due to the last char being null.
Uh, Lua doesn't have null-terminated strings as part of the language specification. In fact:
Lua 5.1 Refernce Manual, Section 2.1 said:
Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.