This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Hash of an entire file?
Started by crazyadam0, 10 April 2014 - 01:40 AMPosted 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
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.
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
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.Posted 10 April 2014 - 08:09 AM
no not quite.So in theory I could justand 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.local f = fs.open("/path","r") local checksum = sha256(textutils.serialize(f.readAll()))
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
Posted 10 April 2014 - 01:28 PM
Nope, not quite…no not quite.So in theory I could justand 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.local f = fs.open("/path","r") local checksum = sha256(textutils.serialize(f.readAll()))
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
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
;)/>Posted 10 April 2014 - 01:46 PM
Shhh… no one is to know, lets keep this our little secret. ;)/>Nope, not quite…
Thanks, I was clearly very distracted when typing this and went muscle memory; I use `h`.
Posted 10 April 2014 - 01:51 PM
Hehe :P/>Shhh… no one is to know, lets keep this our little secret. ;)/>Nope, not quite…
Thanks, I was clearly very distracted when typing this and went muscle memory; I use `h`.
If you want to make it a one-liner, you can use this:
local checksum = sha256(fs.open("path","r").readAll())
Posted 10 April 2014 - 02:33 PM
Except for the lingering file handle. while in the case of ComputerCraft it can be debated acceptable, it is definitely not good practice.If you want to make it a one-liner, you can use this:local checksum = sha256(fs.open("path","r").readAll())
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/>
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.
Posted 10 April 2014 - 11:49 PM
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.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.
Posted 12 April 2014 - 11:45 PM
Uh, Lua doesn't have null-terminated strings as part of the language specification. In fact: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.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.
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'.