2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-07-16 15:20:51 +03:00
|
|
|
// 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.
|
2019-08-02 07:37:19 +03:00
|
|
|
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
|
|
|
|
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
|
2019-07-16 15:20:51 +03:00
|
|
|
module md5
|
|
|
|
|
|
|
|
import encoding.binary
|
|
|
|
|
2019-12-06 15:24:53 +03:00
|
|
|
pub const (
|
2019-07-16 15:20:51 +03:00
|
|
|
// The size of an MD5 checksum in bytes.
|
2020-12-20 18:08:56 +03:00
|
|
|
size = 16
|
2019-07-16 15:20:51 +03:00
|
|
|
// The blocksize of MD5 in bytes.
|
2019-10-24 14:48:20 +03:00
|
|
|
block_size = 64
|
2019-07-16 15:20:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-24 14:48:20 +03:00
|
|
|
init0 = 0x67452301
|
|
|
|
init1 = 0xEFCDAB89
|
|
|
|
init2 = 0x98BADCFE
|
|
|
|
init3 = 0x10325476
|
2019-07-16 15:20:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Digest represents the partial evaluation of a checksum.
|
|
|
|
struct Digest {
|
|
|
|
mut:
|
|
|
|
s []u32
|
|
|
|
x []byte
|
|
|
|
nx int
|
|
|
|
len u64
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut d Digest) reset() {
|
2020-12-20 18:08:56 +03:00
|
|
|
d.s = []u32{len: (4)}
|
|
|
|
d.x = []byte{len: (block_size)}
|
|
|
|
d.s[0] = u32(init0)
|
2019-10-24 14:48:20 +03:00
|
|
|
d.s[1] = u32(init1)
|
|
|
|
d.s[2] = u32(init2)
|
|
|
|
d.s[3] = u32(init3)
|
2019-07-16 15:20:51 +03:00
|
|
|
d.nx = 0
|
2019-09-26 14:57:31 +03:00
|
|
|
d.len = 0
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
|
|
|
|
2019-07-18 11:50:05 +03:00
|
|
|
// new returns a new Digest (implementing hash.Hash) computing the MD5 checksum.
|
2019-09-02 20:22:19 +03:00
|
|
|
pub fn new() &Digest {
|
2019-07-16 15:20:51 +03:00
|
|
|
mut d := &Digest{}
|
|
|
|
d.reset()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// write writes the contents of `p_` to the internal hash representation.
|
2021-02-26 09:24:47 +03:00
|
|
|
pub fn (mut d Digest) write(p_ []byte) ?int {
|
2020-12-20 18:08:56 +03:00
|
|
|
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..]
|
|
|
|
}
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
2020-12-20 18:08:56 +03:00
|
|
|
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..]
|
|
|
|
}
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
2020-12-20 18:08:56 +03:00
|
|
|
if p.len > 0 {
|
|
|
|
d.nx = copy(d.x, p)
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
2020-12-20 18:08:56 +03:00
|
|
|
return nn
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// sum returns the md5 sum of the bytes in `b_in`.
|
|
|
|
pub fn (d &Digest) sum(b_in []byte) []byte {
|
2019-07-16 15:20:51 +03:00
|
|
|
// Make a copy of d so that caller can keep writing and summing.
|
|
|
|
mut d0 := *d
|
|
|
|
hash := d0.checksum()
|
2019-09-29 16:44:52 +03:00
|
|
|
mut b_out := b_in.clone()
|
2019-07-16 15:20:51 +03:00
|
|
|
for b in hash {
|
2019-09-29 16:44:52 +03:00
|
|
|
b_out << b
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
2019-09-29 16:44:52 +03:00
|
|
|
return b_out
|
2019-07-16 15:20:51 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// checksum returns the byte checksum of the `Digest`.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut d Digest) checksum() []byte {
|
2019-07-16 15:20:51 +03:00
|
|
|
// Append 0x80 to the end of the message and then append zeros
|
|
|
|
// until the length is a multiple of 56 bytes. Finally append
|
|
|
|
// 8 bytes representing the message length in bits.
|
|
|
|
//
|
|
|
|
// 1 byte end marker :: 0-63 padding bytes :: 8 byte length
|
|
|
|
// tmp := [1 + 63 + 8]byte{0x80}
|
2020-12-20 18:08:56 +03:00
|
|
|
mut tmp := []byte{len: (1 + 63 + 8)}
|
2019-07-16 15:20:51 +03:00
|
|
|
tmp[0] = 0x80
|
2020-05-24 22:07:32 +03:00
|
|
|
pad := ((55 - d.len) % 64) // calculate number of padding bytes
|
2020-12-20 18:08:56 +03:00
|
|
|
binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
|
2021-03-01 02:18:14 +03:00
|
|
|
d.write(tmp[..1 + pad + 8]) or { panic(err) }
|
2019-07-16 15:20:51 +03:00
|
|
|
// 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')
|
|
|
|
}
|
2020-12-20 18:08:56 +03:00
|
|
|
mut digest := []byte{len: (size)}
|
2019-08-07 09:19:27 +03:00
|
|
|
binary.little_endian_put_u32(mut digest, d.s[0])
|
2019-10-27 10:03:15 +03:00
|
|
|
binary.little_endian_put_u32(mut digest[4..], d.s[1])
|
|
|
|
binary.little_endian_put_u32(mut digest[8..], d.s[2])
|
|
|
|
binary.little_endian_put_u32(mut digest[12..], d.s[3])
|
2019-07-16 15:20:51 +03:00
|
|
|
return digest
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:00:15 +03:00
|
|
|
// sum returns the MD5 checksum of the data.
|
2019-07-16 15:20:51 +03:00
|
|
|
pub fn sum(data []byte) []byte {
|
|
|
|
mut d := new()
|
2021-03-01 02:18:14 +03:00
|
|
|
d.write(data) or { panic(err) }
|
2019-07-16 15:20:51 +03:00
|
|
|
return d.checksum()
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:35:40 +03:00
|
|
|
fn block(mut dig Digest, p []byte) {
|
2020-12-20 18:08:56 +03:00
|
|
|
// For now just use block_generic until we have specific
|
2019-07-17 12:00:15 +03:00
|
|
|
// architecture optimized versions
|
2020-12-20 18:08:56 +03:00
|
|
|
block_generic(mut dig, p)
|
2019-07-17 12:00:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// size returns the size of the checksum in bytes.
|
2020-12-20 18:08:56 +03:00
|
|
|
pub fn (d &Digest) size() int {
|
|
|
|
return size
|
|
|
|
}
|
2019-07-16 15:20:51 +03:00
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// block_size returns the block size of the checksum in bytes.
|
2020-12-20 18:08:56 +03:00
|
|
|
pub fn (d &Digest) block_size() int {
|
|
|
|
return block_size
|
|
|
|
}
|
2019-09-02 20:22:19 +03:00
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// hexhash returns a hexadecimal MD5 hash sum `string` of `s`.
|
|
|
|
// Example: assert md5.hexhash('V') == '5206560a306a2e085a437fd258eb57ce'
|
2020-12-20 18:08:56 +03:00
|
|
|
pub fn hexhash(s string) string {
|
|
|
|
return sum(s.bytes()).hex()
|
|
|
|
}
|