Author Topic: [VB6] Simple Checksumming Method  (Read 12608 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[VB6] Simple Checksumming Method
« on: June 27, 2005, 12:17:07 pm »
I just whipped this up real quick waiting for a spot in the battle grounds. I may use this in JoeChat to make sure you're using the same version as the other person is, etc. You can use this if you ever write a server for something and want to either require your client to be used or have your client present for checksumming. This is the same as CRev() only a lot less complex. See comments for formula format. Untested (written in gedit, not VB).

Code: [Select]
Public Function Checksum(Formula as String, Files() as String)
  ' Used to create a checksum of multiple files based on a Formula
  ' Formula format: Number, Operation, Operation
  ' Formula example: 1+-
  ' Usage example: Checksum("1+^", Array("C:\File1.exe", "C:\File2.txt")
  Dim Ret as Long, I as Integer, I2 as Integer
  Ret = Val(Mid(Formula, 1, 1))
  For i = LBound(Files) To UBound(Files)
    Open Files(I) For Binary Access Read As #1
      For I2 = 1 to LOF(#1)
        sTemp = Space(1)
        Get #1, I2 + 1, sTemp
        If I2 Mod 2 = 0 Then
          Ret = Val(CStr(Ret) & Mid(Formula, 2, 1) & CStr(Asc(sTemp)))
        Else
          Ret = Val(CStr(Ret) & Mid(Formula, 3, 1) & CStr(Asc(sTemp)))
        End If
      Close #1
    Next i2
  Next i
  Checksum = Ret
End Function
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [VB6] Simple Checksumming Method
« Reply #1 on: June 27, 2005, 12:20:03 pm »
Formula Generatior! Again, untested.

Code: [Select]
Public Function MakeForumla() As String
  Operations = "+_^/*"
  Numbers = "123456789"  'Zero is just a mess
  Randomize
  MakeFormula = Mid(Numbers, Int(Rnd * Len(Numbers)) + 1) & Mid(Operations, Int(Rnd * Len(Operations)) + 1) & Mid(Operations, Int(Rnd * Len(Operations)) + 1)
End Function 
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #2 on: June 27, 2005, 04:49:16 pm »
LOF() isn't a number.  You can't use EOF either.  Your random shit sucks.  You need to actually test it and also make sure it works before you release it.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [VB6] Simple Checksumming Method
« Reply #3 on: June 27, 2005, 05:03:09 pm »
Why don't you use CRC32?  That's exactly what it's designed for.  It's generally better to use a tried and tested standard than making your own and guessing at how it can be done. 

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [VB6] Simple Checksumming Method
« Reply #4 on: June 27, 2005, 05:39:06 pm »
Quote
You need to actually test it and also make sure it works before you release it.
I don't test code I don't plan to use. This is just suggestive coding, that I do when I'm bored waiting in lines for the battlegrounds to open up in World of Warcraft. I do this in gedit on cave, which is a Linux box, a PowerPC Linux box. VB6 has no way of existing here thanks to that, and thus it will go untested. Anyone needing to checksum a file can fix it themselves, or when I get the time (JoeMomma is always busy) I'll fix it myself.

Quote
Why don't you use CRC32?  That's exactly what it's designed for.  It's generally better to use a tried and tested standard than making your own and guessing at how it can be done.
I thought about that. On the toilet actually. I remember you once saying that VB6 sucks because it cannot handle overflows and that most hashing requires it. I thought this would be no exception. CRC32 never came to mind, but hashing itself did.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [VB6] Simple Checksumming Method
« Reply #5 on: June 28, 2005, 02:19:57 pm »
Code: [Select]
Private Const CRC32_POLYNOMIAL As Long = &HEDB88320
Private CRC32Table(0 To 255) As Long

Private Sub InitCRC32()
    Dim I As Long, J As Long, K As Long, XorVal As Long
   
    Static CRC32Initialized As Boolean
    If CRC32Initialized Then Exit Sub
    CRC32Initialized = True
   
    For I = 0 To 255
        K = I
       
        For J = 1 To 8
            If K And 1 Then XorVal = CRC32_POLYNOMIAL Else XorVal = 0
            If K < 0 Then K = ((K And &H7FFFFFFF) \ 2) Or &H40000000 Else K = K \ 2
            K = K Xor XorVal
        Next
       
        CRC32Table(I) = K
    Next
End Sub

Private Function CRC32(ByVal Data As String) As Long
    Dim I As Long, J As Long
   
    Call InitCRC32
   
    CRC32 = &HFFFFFFFF
   
    For I = 1 To Len(Data)
        J = CByte(Asc(Mid(Data, I, 1))) Xor (CRC32 And &HFF&)
        If CRC32 < 0 Then CRC32 = ((CRC32 And &H7FFFFFFF) \ &H100&) Or &H800000 Else CRC32 = CRC32 \ &H100&
        CRC32 = CRC32 Xor CRC32Table(J)
    Next
   
    CRC32 = Not CRC32
End Function
And like a fool I believed myself, and thought I was somebody else...

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #6 on: June 28, 2005, 09:02:16 pm »
Quote
You need to actually test it and also make sure it works before you release it.
I don't test code I don't plan to use. This is just suggestive coding, that I do when I'm bored waiting in lines for the battlegrounds to open up in World of Warcraft. I do this in gedit on cave, which is a Linux box, a PowerPC Linux box. VB6 has no way of existing here thanks to that, and thus it will go untested. Anyone needing to checksum a file can fix it themselves, or when I get the time (JoeMomma is always busy) I'll fix it myself.

Quote
Why don't you use CRC32?  That's exactly what it's designed for.  It's generally better to use a tried and tested standard than making your own and guessing at how it can be done.
I thought about that. On the toilet actually. I remember you once saying that VB6 sucks because it cannot handle overflows and that most hashing requires it. I thought this would be no exception. CRC32 never came to mind, but hashing itself did.
VB6 can't handle overloads, there is a difference.  Nothing can handle overflows, that's why they are errors.

overflow:
Code: [Select]
int k = 7829430237407092346273;
Code: [Select]
Const k As Integer = 7829430237407092346273
overload:
Code: [Select]
int blah();
bool blah();
char blah();
void *blah();
int blah(int MOO);
// etc...
Code: [Select]
' OMG WTF?!

trust

  • Guest
Re: [VB6] Simple Checksumming Method
« Reply #7 on: June 29, 2005, 08:46:12 am »
LOF() isn't a number.  You can't use EOF either.  Your random shit sucks.  You need to actually test it and also make sure it works before you release it.

pssh *remembers rabbits packet buffer in sb.net code bank* =P

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #8 on: June 29, 2005, 03:59:01 pm »
At least mine RAN before getting an error and exploding.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [VB6] Simple Checksumming Method
« Reply #9 on: June 29, 2005, 05:11:36 pm »
Quote
You need to actually test it and also make sure it works before you release it.
I don't test code I don't plan to use. This is just suggestive coding, that I do when I'm bored waiting in lines for the battlegrounds to open up in World of Warcraft. I do this in gedit on cave, which is a Linux box, a PowerPC Linux box. VB6 has no way of existing here thanks to that, and thus it will go untested. Anyone needing to checksum a file can fix it themselves, or when I get the time (JoeMomma is always busy) I'll fix it myself.

Quote
Why don't you use CRC32?  That's exactly what it's designed for.  It's generally better to use a tried and tested standard than making your own and guessing at how it can be done.
I thought about that. On the toilet actually. I remember you once saying that VB6 sucks because it cannot handle overflows and that most hashing requires it. I thought this would be no exception. CRC32 never came to mind, but hashing itself did.
VB6 can't handle overloads, there is a difference.  Nothing can handle overflows, that's why they are errors.

overflow:
Code: [Select]
int k = 7829430237407092346273;
Code: [Select]
Const k As Integer = 7829430237407092346273
overload:
Code: [Select]
int blah();
bool blah();
char blah();
void *blah();
int blah(int MOO);
// etc...
Code: [Select]
' OMG WTF?!

You obviously don't know what you're talking about :-P

All languages that I know of (except for VB and BASIC) can handle overflows without an error.  If you reach  (unsigned int32)0xFFFFFFFF, and add one, you'll end up at (unsigned int32)0x00000000.  That's not an error, it's an expected result.  It's used for tons of things such as encryption and hashing. 

Overloading is completly different.  As far as I know, overloading is done in Java, Smalltalk and C++.  It's probably also done in other languages that I don't know, but I don't know them.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [VB6] Simple Checksumming Method
« Reply #10 on: June 29, 2005, 07:46:53 pm »
VB6 can't handle overloads, there is a difference.  Nothing can handle overflows, that's why they are errors.

WTF are you talking about?  Overflows are determined by whether or not a flag was set in the flags register of a CPU (based on Intel architecture).  You can choose whether or not to ignore it in your code.  Ignore it and you won't have an error.

See what iago said.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #11 on: June 29, 2005, 08:04:33 pm »
It seems worthy enough of an error, nonetheless. I mean if you overflow you can overwrite memory, no?
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [VB6] Simple Checksumming Method
« Reply #12 on: June 29, 2005, 08:24:37 pm »
It seems worthy enough of an error, nonetheless. I mean if you overflow you can overwrite memory, no?

No.  I'll follow up in a bit, I'm busy right now

Offline Tuberload

  • Neophyte
  • x86
  • Hero Member
  • *****
  • Posts: 530
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #13 on: June 30, 2005, 12:17:18 pm »
It seems worthy enough of an error, nonetheless. I mean if you overflow you can overwrite memory, no?

No.  I'll follow up in a bit, I'm busy right now

Aren't overflows a big bug in software used to run arbitrary code and such?
I am prepared to be ridiculed for what I believe, are you?

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [VB6] Simple Checksumming Method
« Reply #14 on: June 30, 2005, 02:52:09 pm »
Overflows are where you try to put too much stuff in a small place, thus it overflows.

Example:
You have a long int, and try to put 0xFFFFFFFFF in it, but the max is 0xFFFFFFFF, you will get an overflow.
You have a gallon jug and try to pour two gallons of soda in it, you will get an overflow.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [VB6] Simple Checksumming Method
« Reply #15 on: June 30, 2005, 05:12:38 pm »
It seems worthy enough of an error, nonetheless. I mean if you overflow you can overwrite memory, no?

No.  I'll follow up in a bit, I'm busy right now

Aren't overflows a big bug in software used to run arbitrary code and such?

All right, sorry about that.

Anyways, there are different types of overflows.  In the sense we're talking about, it is an integer overflow.  That is when you add one to the maximum number and end up with 0.  The only type of vulnerability that that can lead to is a heap overflow, which is how Microsoft's JPEG decoder vulnerability came about.  More on that later.

The type of overflow that you're thinking of is an array overflow.  That is, when the programmer declares an array on the stack and then tries to add data to it that goes past the end of the array, it overwrites other data on the stack.  And if you control a program's stack, it's game over.

Now, back to the integer overflow.  This is something that people are never really taught, but that is very, very important.  Here is some innocent looking C code:
Code: [Select]
int size = data[0]; // The first byte is the size, not including headers
unsigned char *buffer = malloc(size + 10); // allocate 10 extra bytes, to include the header
memcpy(buffer, data, 10); // copy the header
memcpy(buffer + 10, data + 10, size); // copy the rest of the data

That looks innocent enough.  But what if the user sends a size of 0xFFFFFFF6?  it will allocate size+10, or 0x00000000 bytes (due to the overflow).  Then it will go ahead and copy the data into the buffer.  Well, we only allocated 0 bytes, so we're overwriteing other data on the heap now.  This is a head overflow, and doesn't always lead to full control of the process but often it's game over.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [VB6] Simple Checksumming Method
« Reply #16 on: June 30, 2005, 09:51:13 pm »
...and what happens to the location in memory if you overflowed the variable...

I'd think the CPU would generate an exception before the memory was written to, no?
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [VB6] Simple Checksumming Method
« Reply #17 on: July 01, 2005, 12:18:59 am »
I'd think the CPU would generate an exception before the memory was written to, no?
Only if you tried to overwrite from a page of unprotected memory to a page of protected memory, AFAIK.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [VB6] Simple Checksumming Method
« Reply #18 on: July 01, 2005, 12:27:41 am »
I'd think the CPU would generate an exception before the memory was written to, no?
Only if you tried to overwrite from a page of unprotected memory to a page of protected memory, AFAIK.

Yeah, you only get an exception if you overwrite memory that you aren't allowed to.