all: minor array fixes

This commit is contained in:
Alexander Medvednikov 2020-12-20 16:08:56 +01:00
parent 3c210a57f9
commit 50a6976b5e
12 changed files with 357 additions and 417 deletions

View file

@ -1,22 +1,18 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
// MD5 is cryptographically broken and should not be used for secure
// applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
module md5
import encoding.binary
pub const (
// The size of an MD5 checksum in bytes.
size = 16
size = 16
// The blocksize of MD5 in bytes.
block_size = 64
)
@ -38,9 +34,9 @@ mut:
}
fn (mut d Digest) reset() {
d.s = []u32{len:(4)}
d.x = []byte{len:(block_size)}
d.s[0] = u32(init0)
d.s = []u32{len: (4)}
d.x = []byte{len: (block_size)}
d.s[0] = u32(init0)
d.s[1] = u32(init1)
d.s[2] = u32(init2)
d.s[3] = u32(init3)
@ -56,35 +52,37 @@ pub fn new() &Digest {
}
pub fn (mut d Digest) write(p_ []byte) int {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == block_size {
block(mut d, d.x)
d.nx = 0
unsafe {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == block_size {
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
if n >= p.len {
p = []
} else {
p = p[n..]
if p.len >= block_size {
n := p.len & ~(block_size - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
}
if p.len >= block_size {
n := p.len &~ (block_size - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
}
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
}
pub fn (d &Digest) sum(b_in []byte) []byte {
@ -105,20 +103,17 @@ pub fn (mut d Digest) checksum() []byte {
//
// 1 byte end marker :: 0-63 padding bytes :: 8 byte length
// tmp := [1 + 63 + 8]byte{0x80}
mut tmp := []byte{len:(1 + 63 + 8)}
mut tmp := []byte{len: (1 + 63 + 8)}
tmp[0] = 0x80
pad := ((55 - d.len) % 64) // calculate number of padding bytes
binary.little_endian_put_u64(mut tmp[1+pad..], d.len<<3) // append length in bits
d.write(tmp[..1+pad+8])
binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
d.write(tmp[..1 + pad + 8])
// The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed.
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []byte{len:(size)}
mut digest := []byte{len: (size)}
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest[4..], d.s[1])
binary.little_endian_put_u32(mut digest[8..], d.s[2])
@ -134,13 +129,19 @@ pub fn sum(data []byte) []byte {
}
fn block(mut dig Digest, p []byte) {
// For now just use block_generic until we have specific
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(mut dig, p)
block_generic(mut dig, p)
}
pub fn (d &Digest) size() int { return size }
pub fn (d &Digest) size() int {
return size
}
pub fn (d &Digest) block_size() int { return block_size }
pub fn (d &Digest) block_size() int {
return block_size
}
pub fn hexhash(s string) string { return sum(s.bytes()).hex() }
pub fn hexhash(s string) string {
return sum(s.bytes()).hex()
}