Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Apr 22, 2024
1 parent 1b1a427 commit 1dbb87f
Showing 1 changed file with 49 additions and 56 deletions.
105 changes: 49 additions & 56 deletions tiger/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ var (
)

type digest struct {
s [3]uint64
x [BlockSize]byte
nx int
length uint64
s [3]uint64
x [BlockSize]byte
nx int
len uint64

hs int
ver int
Expand All @@ -29,117 +29,110 @@ type digest struct {
// newDigest returns a new hash.Hash computing the Tiger hash value
func newDigest(hs int, ver int) *digest {
d := new(digest)
d.Reset()
d.hs = hs
d.ver = ver
d.Reset()

return d
}

func (this *digest) Size() int {
return this.hs
func (d *digest) Size() int {
return d.hs
}

func (this *digest) BlockSize() int {
func (d *digest) BlockSize() int {
return BlockSize
}

func (this *digest) Reset() {
this.s = initH
this.nx = 0
this.length = 0
func (d *digest) Reset() {
d.s = initH
d.nx = 0
d.len = 0
}

func (this *digest) Write(p []byte) (length int, err error) {
length = len(p)
func (d *digest) Write(p []byte) (nn int, err error) {
nn = len(p)

this.length += uint64(length)
d.len += uint64(nn)

if this.nx > 0 {
n := len(p)
if n > BlockSize-this.nx {
n = BlockSize - this.nx
}
plen := len(p)

copy(this.x[this.nx:this.nx+n], p[:n])
this.nx += n
var xx int
for d.nx + plen >= BlockSize {
xx = BlockSize - d.nx

if this.nx == BlockSize {
this.compress(this.x[:BlockSize])
this.nx = 0
}
copy(d.x[d.nx:], p)

p = p[n:]
}
d.compress(d.x[:])

for len(p) >= BlockSize {
this.compress(p[:BlockSize])
p = p[BlockSize:]
plen -= xx
p = p[xx:]
d.nx = 0
}

if len(p) > 0 {
this.nx = copy(this.x[:], p)
}
copy(d.x[d.nx:], p)
d.nx += plen

return
}

func (this *digest) Sum(in []byte) []byte {
func (d *digest) Sum(in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
d0 := *this
d0 := *d
hash := d0.checkSum()
return append(in, hash[:]...)
}

func (this *digest) checkSum() []byte {
func (d *digest) checkSum() []byte {
var tmp [64]byte

if this.ver == 1 {
if d.ver == 1 {
tmp[0] = 0x01
} else {
tmp[0] = 0x80
}

length := this.length
length := d.len

size := length & 0x3f
if size < 56 {
this.Write(tmp[:56-size])
d.Write(tmp[:56-size])
} else {
this.Write(tmp[:64+56-size])
d.Write(tmp[:64+56-size])
}

length <<= 3
for i := uint(0); i < 8; i++ {
tmp[i] = byte(length >> (8 * i))
}

this.Write(tmp[:8])
d.Write(tmp[:8])

for i := uint(0); i < 8; i++ {
tmp[i] = byte(this.s[0] >> (8 * i))
tmp[i+8] = byte(this.s[1] >> (8 * i))
tmp[i+16] = byte(this.s[2] >> (8 * i))
tmp[i] = byte(d.s[0] >> (8 * i))
tmp[i+8] = byte(d.s[1] >> (8 * i))
tmp[i+16] = byte(d.s[2] >> (8 * i))
}

return tmp[:this.hs]
return tmp[:d.hs]
}

func (this *digest) compress(data []byte) {
a := this.s[0]
b := this.s[1]
c := this.s[2]
func (d *digest) compress(data []byte) {
a := d.s[0]
b := d.s[1]
c := d.s[2]

x := bytesToUint64s(data)

this.s[0], this.s[1], this.s[2] = pass(this.s[0], this.s[1], this.s[2], x, 5)
d.s[0], d.s[1], d.s[2] = pass(d.s[0], d.s[1], d.s[2], x, 5)

keySchedule(x)
this.s[2], this.s[0], this.s[1] = pass(this.s[2], this.s[0], this.s[1], x, 7)
d.s[2], d.s[0], d.s[1] = pass(d.s[2], d.s[0], d.s[1], x, 7)

keySchedule(x)
this.s[1], this.s[2], this.s[0] = pass(this.s[1], this.s[2], this.s[0], x, 9)
d.s[1], d.s[2], d.s[0] = pass(d.s[1], d.s[2], d.s[0], x, 9)

this.s[0] ^= a
this.s[1] -= b
this.s[2] += c
d.s[0] ^= a
d.s[1] -= b
d.s[2] += c
}

0 comments on commit 1dbb87f

Please sign in to comment.