-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed format and cleaned up examples. Added JacHash example.
- Loading branch information
1 parent
6bb6eb9
commit a0986b3
Showing
9 changed files
with
66 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use Text; | ||
|
||
func main () { | ||
JacHash hash = new JacHash (); | ||
println (hash.computeHash ("apple".toList ())); | ||
} | ||
|
||
MAX_LENGTH = 16; | ||
FILLER_BYTE = 0xF; | ||
|
||
class JacHash { | ||
func new () {} | ||
func computeHash (bytes : list) { | ||
this.init (); | ||
bytes = this.pad (bytes); | ||
result = []; | ||
foreach (i in range (0, bytes.length)) | ||
this.x += bytes [i]; | ||
StringBuilder sb = new StringBuilder (); | ||
for (int i = 0; i < bytes.length; i++) | ||
bytes [i % MAX_LENGTH] = this.next (bytes [i].toInt ()); | ||
for (int i = 0; i < MAX_LENGTH; i++) | ||
sb.appendFormat ("{0:x2}", bytes [i].toInt ()); | ||
|
||
return sb.toString (); | ||
} | ||
|
||
func next (bl : int) : int { | ||
this.a = this.shiftLeft(bl, this.x); | ||
this.b = (this.b ^ bl) - this.x; | ||
this.c = (this.a + this.b) & this.x; | ||
this.d ^= this.x - this.b; | ||
this.x ^= this.d; | ||
return (this.a * this.c + this.b - this.x * this.d ^ bl) & 0xFF; | ||
} | ||
|
||
func shiftLeft (b : int, bits : int) : int { | ||
return b << bits | b >> 32 - bits; | ||
} | ||
|
||
func pad (bytes : list) : list { | ||
for (int i = bytes.length; i < MAX_LENGTH; i++) | ||
bytes.add (FILLER_BYTE); | ||
return bytes; | ||
} | ||
|
||
func init () { | ||
this.a = 0x6B87 & 0xFF; | ||
this.b = 0x7F43 & 0xFF; | ||
this.c = 0xA4Ad & 0xFF; | ||
this.d = 0xDC3F & 0xFF; | ||
this.x = 0; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters