Commit d2cdcfc1 authored by Russ Cox's avatar Russ Cox

casify hash

R=r
DELTA=235  (6 added, 26 deleted, 203 changed)
OCL=22907
CL=22938
parent e83c85ac
......@@ -19,8 +19,10 @@ export type Digest struct {
n int;
}
const Mod = 65521;
const Maxiter = 5552; // max mod-free iterations before would overflow uint32
const (
_Mod = 65521;
_MaxIter = 5552; // max mod-free iterations before would overflow uint32
)
export func NewDigest() *Digest {
return &Digest{1, 0, 0};
......@@ -32,9 +34,9 @@ func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
a += uint32(p[i]);
b += a;
n++;
if n == Maxiter {
a %= Mod;
b %= Mod;
if n == _MaxIter {
a %= _Mod;
b %= _Mod;
n = 0;
}
}
......@@ -44,9 +46,9 @@ func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
func (d *Digest) Sum32() uint32 {
a, b := d.a, d.b;
if a >= Mod || b >= Mod {
a %= Mod;
b %= Mod;
if a >= _Mod || b >= _Mod {
a %= _Mod;
b %= _Mod;
}
return b<<16 | a;
}
......
......@@ -10,43 +10,43 @@ import (
"testing";
)
type Adler32Test struct {
type _Adler32Test struct {
out uint32;
in string;
}
var golden = []Adler32Test {
Adler32Test{ 0x1, "" },
Adler32Test{ 0x620062, "a" },
Adler32Test{ 0x12600c4, "ab" },
Adler32Test{ 0x24d0127, "abc" },
Adler32Test{ 0x3d8018b, "abcd" },
Adler32Test{ 0x5c801f0, "abcde" },
Adler32Test{ 0x81e0256, "abcdef" },
Adler32Test{ 0xadb02bd, "abcdefg" },
Adler32Test{ 0xe000325, "abcdefgh" },
Adler32Test{ 0x118e038e, "abcdefghi" },
Adler32Test{ 0x158603f8, "abcdefghij" },
Adler32Test{ 0x3f090f02, "Discard medicine more than two years old." },
Adler32Test{ 0x46d81477, "He who has a shady past knows that nice guys finish last." },
Adler32Test{ 0x40ee0ee1, "I wouldn't marry him with a ten foot pole." },
Adler32Test{ 0x16661315, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
Adler32Test{ 0x5b2e1480, "The days of the digital watch are numbered. -Tom Stoppard" },
Adler32Test{ 0x8c3c09ea, "Nepal premier won't resign." },
Adler32Test{ 0x45ac18fd, "For every action there is an equal and opposite government program." },
Adler32Test{ 0x53c61462, "His money is twice tainted: 'taint yours and 'taint mine." },
Adler32Test{ 0x7e511e63, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
Adler32Test{ 0xe4801a6a, "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
Adler32Test{ 0x61b507df, "size: a.out: bad magic" },
Adler32Test{ 0xb8631171, "The major problem is with sendmail. -Mark Horton" },
Adler32Test{ 0x8b5e1904, "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
Adler32Test{ 0x7cc6102b, "If the enemy is within range, then so are you." },
Adler32Test{ 0x700318e7, "It's well we cannot hear the screams/That we create in others' dreams." },
Adler32Test{ 0x1e601747, "You remind me of a TV show, but that's all right: I watch it anyway." },
Adler32Test{ 0xb55b0b09, "C is as portable as Stonehedge!!" },
Adler32Test{ 0x39111dd0, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
Adler32Test{ 0x91dd304f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
Adler32Test{ 0x2e5d1316, "How can you write a big system without C++? -Paul Glick" },
var golden = []_Adler32Test {
_Adler32Test{ 0x1, "" },
_Adler32Test{ 0x620062, "a" },
_Adler32Test{ 0x12600c4, "ab" },
_Adler32Test{ 0x24d0127, "abc" },
_Adler32Test{ 0x3d8018b, "abcd" },
_Adler32Test{ 0x5c801f0, "abcde" },
_Adler32Test{ 0x81e0256, "abcdef" },
_Adler32Test{ 0xadb02bd, "abcdefg" },
_Adler32Test{ 0xe000325, "abcdefgh" },
_Adler32Test{ 0x118e038e, "abcdefghi" },
_Adler32Test{ 0x158603f8, "abcdefghij" },
_Adler32Test{ 0x3f090f02, "Discard medicine more than two years old." },
_Adler32Test{ 0x46d81477, "He who has a shady past knows that nice guys finish last." },
_Adler32Test{ 0x40ee0ee1, "I wouldn't marry him with a ten foot pole." },
_Adler32Test{ 0x16661315, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
_Adler32Test{ 0x5b2e1480, "The days of the digital watch are numbered. -Tom Stoppard" },
_Adler32Test{ 0x8c3c09ea, "Nepal premier won't resign." },
_Adler32Test{ 0x45ac18fd, "For every action there is an equal and opposite government program." },
_Adler32Test{ 0x53c61462, "His money is twice tainted: 'taint yours and 'taint mine." },
_Adler32Test{ 0x7e511e63, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
_Adler32Test{ 0xe4801a6a, "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
_Adler32Test{ 0x61b507df, "size: a.out: bad magic" },
_Adler32Test{ 0xb8631171, "The major problem is with sendmail. -Mark Horton" },
_Adler32Test{ 0x8b5e1904, "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
_Adler32Test{ 0x7cc6102b, "If the enemy is within range, then so are you." },
_Adler32Test{ 0x700318e7, "It's well we cannot hear the screams/That we create in others' dreams." },
_Adler32Test{ 0x1e601747, "You remind me of a TV show, but that's all right: I watch it anyway." },
_Adler32Test{ 0xb55b0b09, "C is as portable as Stonehedge!!" },
_Adler32Test{ 0x39111dd0, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
_Adler32Test{ 0x91dd304f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
_Adler32Test{ 0x2e5d1316, "How can you write a big system without C++? -Paul Glick" },
}
export func TestGolden(t *testing.T) {
......
......@@ -44,7 +44,7 @@ export func MakeTable(poly uint32) Table {
return t;
}
export var ieee = MakeTable(IEEE);
export var IEEETable = MakeTable(IEEE);
export type Digest struct {
crc uint32;
......@@ -56,7 +56,7 @@ export func NewDigest(tab Table) *Digest {
}
export func NewIEEEDigest() *Digest {
return NewDigest(ieee);
return NewDigest(IEEETable);
}
func (d *Digest) Write(p []byte) (n int, err *os.Error) {
......
......@@ -10,43 +10,43 @@ import (
"testing";
)
type Crc32Test struct {
type _Crc32Test struct {
out uint32;
in string;
}
var golden = []Crc32Test {
Crc32Test{ 0x0, "" },
Crc32Test{ 0xe8b7be43, "a" },
Crc32Test{ 0x9e83486d, "ab" },
Crc32Test{ 0x352441c2, "abc" },
Crc32Test{ 0xed82cd11, "abcd" },
Crc32Test{ 0x8587d865, "abcde" },
Crc32Test{ 0x4b8e39ef, "abcdef" },
Crc32Test{ 0x312a6aa6, "abcdefg" },
Crc32Test{ 0xaeef2a50, "abcdefgh" },
Crc32Test{ 0x8da988af, "abcdefghi" },
Crc32Test{ 0x3981703a, "abcdefghij" },
Crc32Test{ 0x6b9cdfe7, "Discard medicine more than two years old." },
Crc32Test{ 0xc90ef73f, "He who has a shady past knows that nice guys finish last." },
Crc32Test{ 0xb902341f, "I wouldn't marry him with a ten foot pole." },
Crc32Test{ 0x42080e8, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
Crc32Test{ 0x154c6d11, "The days of the digital watch are numbered. -Tom Stoppard" },
Crc32Test{ 0x4c418325, "Nepal premier won't resign." },
Crc32Test{ 0x33955150, "For every action there is an equal and opposite government program." },
Crc32Test{ 0x26216a4b, "His money is twice tainted: 'taint yours and 'taint mine." },
Crc32Test{ 0x1abbe45e, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
Crc32Test{ 0xc89a94f7, "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
Crc32Test{ 0xab3abe14, "size: a.out: bad magic" },
Crc32Test{ 0xbab102b6, "The major problem is with sendmail. -Mark Horton" },
Crc32Test{ 0x999149d7, "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
Crc32Test{ 0x6d52a33c, "If the enemy is within range, then so are you." },
Crc32Test{ 0x90631e8d, "It's well we cannot hear the screams/That we create in others' dreams." },
Crc32Test{ 0x78309130, "You remind me of a TV show, but that's all right: I watch it anyway." },
Crc32Test{ 0x7d0a377f, "C is as portable as Stonehedge!!" },
Crc32Test{ 0x8c79fd79, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
Crc32Test{ 0xa20b7167, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
Crc32Test{ 0x8e0bb443, "How can you write a big system without C++? -Paul Glick" },
var golden = []_Crc32Test {
_Crc32Test{ 0x0, "" },
_Crc32Test{ 0xe8b7be43, "a" },
_Crc32Test{ 0x9e83486d, "ab" },
_Crc32Test{ 0x352441c2, "abc" },
_Crc32Test{ 0xed82cd11, "abcd" },
_Crc32Test{ 0x8587d865, "abcde" },
_Crc32Test{ 0x4b8e39ef, "abcdef" },
_Crc32Test{ 0x312a6aa6, "abcdefg" },
_Crc32Test{ 0xaeef2a50, "abcdefgh" },
_Crc32Test{ 0x8da988af, "abcdefghi" },
_Crc32Test{ 0x3981703a, "abcdefghij" },
_Crc32Test{ 0x6b9cdfe7, "Discard medicine more than two years old." },
_Crc32Test{ 0xc90ef73f, "He who has a shady past knows that nice guys finish last." },
_Crc32Test{ 0xb902341f, "I wouldn't marry him with a ten foot pole." },
_Crc32Test{ 0x42080e8, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
_Crc32Test{ 0x154c6d11, "The days of the digital watch are numbered. -Tom Stoppard" },
_Crc32Test{ 0x4c418325, "Nepal premier won't resign." },
_Crc32Test{ 0x33955150, "For every action there is an equal and opposite government program." },
_Crc32Test{ 0x26216a4b, "His money is twice tainted: 'taint yours and 'taint mine." },
_Crc32Test{ 0x1abbe45e, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
_Crc32Test{ 0xc89a94f7, "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
_Crc32Test{ 0xab3abe14, "size: a.out: bad magic" },
_Crc32Test{ 0xbab102b6, "The major problem is with sendmail. -Mark Horton" },
_Crc32Test{ 0x999149d7, "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
_Crc32Test{ 0x6d52a33c, "If the enemy is within range, then so are you." },
_Crc32Test{ 0x90631e8d, "It's well we cannot hear the screams/That we create in others' dreams." },
_Crc32Test{ 0x78309130, "You remind me of a TV show, but that's all right: I watch it anyway." },
_Crc32Test{ 0x7d0a377f, "C is as portable as Stonehedge!!" },
_Crc32Test{ 0x8c79fd79, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
_Crc32Test{ 0xa20b7167, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
_Crc32Test{ 0x8e0bb443, "How can you write a big system without C++? -Paul Glick" },
}
export func TestGolden(t *testing.T) {
......
......@@ -8,54 +8,52 @@ package md5
import "os"
package const (
Chunk = 64
)
const (
A = 0x67452301;
B = 0xEFCDAB89;
C = 0x98BADCFE;
D = 0x10325476;
_Chunk = 64;
_Init0 = 0x67452301;
_Init1 = 0xEFCDAB89;
_Init2 = 0x98BADCFE;
_Init3 = 0x10325476;
)
export type Digest struct {
s [4]uint32;
x [Chunk]byte;
x [_Chunk]byte;
nx int;
len uint64;
}
export func NewDigest() *Digest {
d := new(Digest);
d.s[0] = A;
d.s[1] = B;
d.s[2] = C;
d.s[3] = D;
d.s[0] = _Init0;
d.s[1] = _Init1;
d.s[2] = _Init2;
d.s[3] = _Init3;
return d;
}
package func Block(dig *Digest, p []byte) int
func _Block(dig *Digest, p []byte) int
func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
nn = len(p);
d.len += uint64(nn);
if d.nx > 0 {
n := len(p);
if n > Chunk-d.nx {
n = Chunk-d.nx;
if n > _Chunk-d.nx {
n = _Chunk-d.nx;
}
for i := 0; i < n; i++ {
d.x[d.nx+i] = p[i];
}
d.nx += n;
if d.nx == Chunk {
Block(d, d.x);
if d.nx == _Chunk {
_Block(d, d.x);
d.nx = 0;
}
p = p[n:len(p)];
}
n := Block(d, p);
n := _Block(d, p);
p = p[n:len(p)];
if len(p) > 0 {
for i := 0; i < len(p); i++ {
......
......@@ -5,58 +5,49 @@
package md5
import (
"md5";
"fmt";
"io";
"md5";
"testing";
)
type Md5Test struct {
type md5Test struct {
out string;
in string;
}
var golden = []Md5Test {
Md5Test{ "d41d8cd98f00b204e9800998ecf8427e", "" },
Md5Test{ "0cc175b9c0f1b6a831c399e269772661", "a" },
Md5Test{ "187ef4436122d1cc2f40dc2b92f0eba0", "ab" },
Md5Test{ "900150983cd24fb0d6963f7d28e17f72", "abc" },
Md5Test{ "e2fc714c4727ee9395f324cd2e7f331f", "abcd" },
Md5Test{ "ab56b4d92b40713acc5af89985d4b786", "abcde" },
Md5Test{ "e80b5017098950fc58aad83c8c14978e", "abcdef" },
Md5Test{ "7ac66c0f148de9519b8bd264312c4d64", "abcdefg" },
Md5Test{ "e8dc4081b13434b45189a720b77b6818", "abcdefgh" },
Md5Test{ "8aa99b1f439ff71293e95357bac6fd94", "abcdefghi" },
Md5Test{ "a925576942e94b2ef57a066101b48876", "abcdefghij" },
Md5Test{ "d747fc1719c7eacb84058196cfe56d57", "Discard medicine more than two years old." },
Md5Test{ "bff2dcb37ef3a44ba43ab144768ca837", "He who has a shady past knows that nice guys finish last." },
Md5Test{ "0441015ecb54a7342d017ed1bcfdbea5", "I wouldn't marry him with a ten foot pole." },
Md5Test{ "9e3cac8e9e9757a60c3ea391130d3689", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
Md5Test{ "a0f04459b031f916a59a35cc482dc039", "The days of the digital watch are numbered. -Tom Stoppard" },
Md5Test{ "e7a48e0fe884faf31475d2a04b1362cc", "Nepal premier won't resign." },
Md5Test{ "637d2fe925c07c113800509964fb0e06", "For every action there is an equal and opposite government program." },
Md5Test{ "834a8d18d5c6562119cf4c7f5086cb71", "His money is twice tainted: 'taint yours and 'taint mine." },
Md5Test{ "de3a4d2fd6c73ec2db2abad23b444281", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
Md5Test{ "acf203f997e2cf74ea3aff86985aefaf", "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
Md5Test{ "e1c1384cb4d2221dfdd7c795a4222c9a", "size: a.out: bad magic" },
Md5Test{ "c90f3ddecc54f34228c063d7525bf644", "The major problem is with sendmail. -Mark Horton" },
Md5Test{ "cdf7ab6c1fd49bd9933c43f3ea5af185", "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
Md5Test{ "83bc85234942fc883c063cbd7f0ad5d0", "If the enemy is within range, then so are you." },
Md5Test{ "277cbe255686b48dd7e8f389394d9299", "It's well we cannot hear the screams/That we create in others' dreams." },
Md5Test{ "fd3fb0a7ffb8af16603f3d3af98f8e1f", "You remind me of a TV show, but that's all right: I watch it anyway." },
Md5Test{ "469b13a78ebf297ecda64d4723655154", "C is as portable as Stonehedge!!" },
Md5Test{ "63eb3a2f466410104731c4b037600110", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
Md5Test{ "72c2ed7592debca1c90fc0100f931a2f", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
Md5Test{ "132f7619d33b523b1d9e5bd8e0928355", "How can you write a big system without C++? -Paul Glick" },
}
func Hex(p []byte) string {
s := "";
for i := 0; i < len(p); i++ {
v := p[i];
s += string("0123456789abcdef"[v>>4]);
s += string("0123456789abcdef"[v&15]);
}
return s;
var golden = []md5Test {
md5Test{ "d41d8cd98f00b204e9800998ecf8427e", "" },
md5Test{ "0cc175b9c0f1b6a831c399e269772661", "a" },
md5Test{ "187ef4436122d1cc2f40dc2b92f0eba0", "ab" },
md5Test{ "900150983cd24fb0d6963f7d28e17f72", "abc" },
md5Test{ "e2fc714c4727ee9395f324cd2e7f331f", "abcd" },
md5Test{ "ab56b4d92b40713acc5af89985d4b786", "abcde" },
md5Test{ "e80b5017098950fc58aad83c8c14978e", "abcdef" },
md5Test{ "7ac66c0f148de9519b8bd264312c4d64", "abcdefg" },
md5Test{ "e8dc4081b13434b45189a720b77b6818", "abcdefgh" },
md5Test{ "8aa99b1f439ff71293e95357bac6fd94", "abcdefghi" },
md5Test{ "a925576942e94b2ef57a066101b48876", "abcdefghij" },
md5Test{ "d747fc1719c7eacb84058196cfe56d57", "Discard medicine more than two years old." },
md5Test{ "bff2dcb37ef3a44ba43ab144768ca837", "He who has a shady past knows that nice guys finish last." },
md5Test{ "0441015ecb54a7342d017ed1bcfdbea5", "I wouldn't marry him with a ten foot pole." },
md5Test{ "9e3cac8e9e9757a60c3ea391130d3689", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
md5Test{ "a0f04459b031f916a59a35cc482dc039", "The days of the digital watch are numbered. -Tom Stoppard" },
md5Test{ "e7a48e0fe884faf31475d2a04b1362cc", "Nepal premier won't resign." },
md5Test{ "637d2fe925c07c113800509964fb0e06", "For every action there is an equal and opposite government program." },
md5Test{ "834a8d18d5c6562119cf4c7f5086cb71", "His money is twice tainted: 'taint yours and 'taint mine." },
md5Test{ "de3a4d2fd6c73ec2db2abad23b444281", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
md5Test{ "acf203f997e2cf74ea3aff86985aefaf", "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
md5Test{ "e1c1384cb4d2221dfdd7c795a4222c9a", "size: a.out: bad magic" },
md5Test{ "c90f3ddecc54f34228c063d7525bf644", "The major problem is with sendmail. -Mark Horton" },
md5Test{ "cdf7ab6c1fd49bd9933c43f3ea5af185", "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
md5Test{ "83bc85234942fc883c063cbd7f0ad5d0", "If the enemy is within range, then so are you." },
md5Test{ "277cbe255686b48dd7e8f389394d9299", "It's well we cannot hear the screams/That we create in others' dreams." },
md5Test{ "fd3fb0a7ffb8af16603f3d3af98f8e1f", "You remind me of a TV show, but that's all right: I watch it anyway." },
md5Test{ "469b13a78ebf297ecda64d4723655154", "C is as portable as Stonehedge!!" },
md5Test{ "63eb3a2f466410104731c4b037600110", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
md5Test{ "72c2ed7592debca1c90fc0100f931a2f", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
md5Test{ "132f7619d33b523b1d9e5bd8e0928355", "How can you write a big system without C++? -Paul Glick" },
}
export func TestGolden(t *testing.T) {
......@@ -64,7 +55,7 @@ export func TestGolden(t *testing.T) {
g := golden[i];
c := NewDigest();
io.WriteString(c, g.in);
s := Hex(c.Sum());
s := fmt.Sprintf("%x", c.Sum());
if s != g.out {
t.Errorf("md5(%s) = %s want %s", g.in, s, g.out);
t.FailNow();
......
......@@ -10,8 +10,8 @@ package md5
import "md5"
// T[i] = int((1<<32) * abs(sin(i+1 radians))).
var T = []uint32 {
// table[i] = int((1<<32) * abs(sin(i+1 radians))).
var table = []uint32 {
// round 1
0xd76aa478,
0xe8c7b756,
......@@ -90,14 +90,14 @@ var shift2 = []uint { 5, 9, 14, 20 };
var shift3 = []uint { 4, 11, 16, 23 };
var shift4 = []uint { 6, 10, 15, 21 };
package func Block(dig *Digest, p []byte) int {
func _Block(dig *Digest, p []byte) int {
a := dig.s[0];
b := dig.s[1];
c := dig.s[2];
d := dig.s[3];
n := 0;
var X [16]uint32;
for len(p) >= Chunk {
for len(p) >= _Chunk {
aa, bb, cc, dd := a, b, c, d;
for i := 0; i < 16; i++ {
......@@ -119,7 +119,7 @@ package func Block(dig *Digest, p []byte) int {
t := i;
s := shift1[i%4];
f := ((c ^ d) & b) ^ d;
a += f + X[x] + T[t];
a += f + X[x] + table[t];
a = a<<s | a>>(32-s);
a += b;
a, b, c, d = d, a, b, c;
......@@ -131,7 +131,7 @@ package func Block(dig *Digest, p []byte) int {
t := 16+i;
s := shift2[i%4];
g := ((b ^ c) & d) ^ c;
a += g + X[x] + T[t];
a += g + X[x] + table[t];
a = a<<s | a>>(32-s);
a += b;
a, b, c, d = d, a, b, c;
......@@ -143,7 +143,7 @@ package func Block(dig *Digest, p []byte) int {
t := 32+i;
s := shift3[i%4];
h := b ^ c ^ d;
a += h + X[x] + T[t];
a += h + X[x] + table[t];
a = a<<s | a>>(32-s);
a += b;
a, b, c, d = d, a, b, c;
......@@ -155,7 +155,7 @@ package func Block(dig *Digest, p []byte) int {
s := shift4[i%4];
t := 48+i;
ii := c ^ (b | ^d);
a += ii + X[x] + T[t];
a += ii + X[x] + table[t];
a = a<<s | a>>(32-s);
a += b;
a, b, c, d = d, a, b, c;
......@@ -166,8 +166,8 @@ package func Block(dig *Digest, p []byte) int {
c += cc;
d += dd;
p = p[Chunk:len(p)];
n += Chunk;
p = p[_Chunk:len(p)];
n += _Chunk;
}
dig.s[0] = a;
......
......@@ -8,56 +8,54 @@ package sha1
import "os"
package const (
Chunk = 64;
)
const (
H0 = 0x67452301;
H1 = 0xEFCDAB89;
H2 = 0x98BADCFE;
H3 = 0x10325476;
H4 = 0xC3D2E1F0;
_Chunk = 64;
_Init0 = 0x67452301;
_Init1 = 0xEFCDAB89;
_Init2 = 0x98BADCFE;
_Init3 = 0x10325476;
_Init4 = 0xC3D2E1F0;
)
export type Digest struct {
h [5]uint32;
x [Chunk]byte;
x [_Chunk]byte;
nx int;
len uint64;
}
export func NewDigest() *Digest {
d := new(Digest);
d.h[0] = H0;
d.h[1] = H1;
d.h[2] = H2;
d.h[3] = H3;
d.h[4] = H4;
d.h[0] = _Init0;
d.h[1] = _Init1;
d.h[2] = _Init2;
d.h[3] = _Init3;
d.h[4] = _Init4;
return d;
}
package func Block(dig *Digest, p []byte) int
func _Block(dig *Digest, p []byte) int
func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
nn = len(p);
d.len += uint64(nn);
if d.nx > 0 {
n := len(p);
if n > Chunk-d.nx {
n = Chunk-d.nx;
if n > _Chunk-d.nx {
n = _Chunk-d.nx;
}
for i := 0; i < n; i++ {
d.x[d.nx+i] = p[i];
}
d.nx += n;
if d.nx == Chunk {
Block(d, d.x);
if d.nx == _Chunk {
_Block(d, d.x);
d.nx = 0;
}
p = p[n:len(p)];
}
n := Block(d, p);
n := _Block(d, p);
p = p[n:len(p)];
if len(p) > 0 {
for i := 0; i < len(p); i++ {
......
......@@ -7,58 +7,49 @@
package sha1
import (
"sha1";
"fmt";
"io";
"sha1";
"testing";
)
type Sha1Test struct {
type sha1Test struct {
out string;
in string;
}
var golden = []Sha1Test {
Sha1Test{ "da39a3ee5e6b4b0d3255bfef95601890afd80709", "" },
Sha1Test{ "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a" },
Sha1Test{ "da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab" },
Sha1Test{ "a9993e364706816aba3e25717850c26c9cd0d89d", "abc" },
Sha1Test{ "81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd" },
Sha1Test{ "03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde" },
Sha1Test{ "1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef" },
Sha1Test{ "2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg" },
Sha1Test{ "425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh" },
Sha1Test{ "c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi" },
Sha1Test{ "d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij" },
Sha1Test{ "ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old." },
Sha1Test{ "e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last." },
Sha1Test{ "45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole." },
Sha1Test{ "55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
Sha1Test{ "b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered. -Tom Stoppard" },
Sha1Test{ "c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign." },
Sha1Test{ "6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program." },
Sha1Test{ "597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine." },
Sha1Test{ "6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
Sha1Test{ "514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
Sha1Test{ "c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size: a.out: bad magic" },
Sha1Test{ "74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail. -Mark Horton" },
Sha1Test{ "0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
Sha1Test{ "3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you." },
Sha1Test{ "410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams." },
Sha1Test{ "841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway." },
Sha1Test{ "163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!" },
Sha1Test{ "32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
Sha1Test{ "0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
Sha1Test{ "6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++? -Paul Glick" },
}
func Hex(p []byte) string {
s := "";
for i := 0; i < len(p); i++ {
v := p[i];
s += string("0123456789abcdef"[v>>4]);
s += string("0123456789abcdef"[v&15]);
}
return s;
var golden = []sha1Test {
sha1Test{ "da39a3ee5e6b4b0d3255bfef95601890afd80709", "" },
sha1Test{ "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a" },
sha1Test{ "da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab" },
sha1Test{ "a9993e364706816aba3e25717850c26c9cd0d89d", "abc" },
sha1Test{ "81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd" },
sha1Test{ "03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde" },
sha1Test{ "1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef" },
sha1Test{ "2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg" },
sha1Test{ "425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh" },
sha1Test{ "c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi" },
sha1Test{ "d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij" },
sha1Test{ "ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old." },
sha1Test{ "e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last." },
sha1Test{ "45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole." },
sha1Test{ "55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" },
sha1Test{ "b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered. -Tom Stoppard" },
sha1Test{ "c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign." },
sha1Test{ "6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program." },
sha1Test{ "597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine." },
sha1Test{ "6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" },
sha1Test{ "514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek" },
sha1Test{ "c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size: a.out: bad magic" },
sha1Test{ "74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail. -Mark Horton" },
sha1Test{ "0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world. CCFestoon" },
sha1Test{ "3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you." },
sha1Test{ "410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams." },
sha1Test{ "841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway." },
sha1Test{ "163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!" },
sha1Test{ "32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" },
sha1Test{ "0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" },
sha1Test{ "6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++? -Paul Glick" },
}
export func TestGolden(t *testing.T) {
......@@ -66,7 +57,7 @@ export func TestGolden(t *testing.T) {
g := golden[i];
c := NewDigest();
io.WriteString(c, g.in);
s := Hex(c.Sum());
s := fmt.Sprintf("%x", c.Sum());
if s != g.out {
t.Errorf("sha1(%s) = %s want %s", g.in, s, g.out);
t.FailNow();
......
......@@ -11,18 +11,18 @@ package sha1
import "sha1"
const (
K0 = 0x5A827999;
K1 = 0x6ED9EBA1;
K2 = 0x8F1BBCDC;
K3 = 0xCA62C1D6;
_K0 = 0x5A827999;
_K1 = 0x6ED9EBA1;
_K2 = 0x8F1BBCDC;
_K3 = 0xCA62C1D6;
)
package func Block(dig *Digest, p []byte) int {
func _Block(dig *Digest, p []byte) int {
var w [80]uint32;
n := 0;
h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4];
for len(p) >= Chunk {
for len(p) >= _Chunk {
// Can interlace the computation of w with the
// rounds below if needed for speed.
for i := 0; i < 16; i++ {
......@@ -41,33 +41,33 @@ package func Block(dig *Digest, p []byte) int {
// Each of the four 20-iteration rounds
// differs only in the computation of f and
// the choice of K (K0, K1, etc).
// the choice of K (_K0, _K1, etc).
for i := 0; i < 20; i++ {
f := b&c | (^b)&d;
a5 := a<<5 | a>>(32-5);
b30 := b<<30 | b>>(32-30);
t := a5 + f + e + w[i] + K0;
t := a5 + f + e + w[i] + _K0;
a, b, c, d, e = t, a, b30, c, d;
}
for i := 20; i < 40; i++ {
f := b ^ c ^ d;
a5 := a<<5 | a>>(32-5);
b30 := b<<30 | b>>(32-30);
t := a5 + f + e + w[i] + K1;
t := a5 + f + e + w[i] + _K1;
a, b, c, d, e = t, a, b30, c, d;
}
for i := 40; i < 60; i++ {
f := b&c | b&d | c&d;
a5 := a<<5 | a>>(32-5);
b30 := b<<30 | b>>(32-30);
t := a5 + f + e + w[i] + K2;
t := a5 + f + e + w[i] + _K2;
a, b, c, d, e = t, a, b30, c, d;
}
for i := 60; i < 80; i++ {
f := b ^ c ^ d;
a5 := a<<5 | a>>(32-5);
b30 := b<<30 | b>>(32-30);
t := a5 + f + e + w[i] + K3;
t := a5 + f + e + w[i] + _K3;
a, b, c, d, e = t, a, b30, c, d;
}
......@@ -77,8 +77,8 @@ package func Block(dig *Digest, p []byte) int {
h3 += d;
h4 += e;
p = p[Chunk:len(p)];
n += Chunk;
p = p[_Chunk:len(p)];
n += _Chunk;
}
dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment