2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-07-15 18:49:01 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
// Package sha1 implements the SHA-1 hash algorithm as defined in RFC 3174.
|
|
|
|
// SHA-1 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/sha1
|
2019-11-14 09:53:05 +03:00
|
|
|
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
2019-07-15 18:49:01 +03:00
|
|
|
module sha1
|
|
|
|
|
|
|
|
import encoding.binary
|
|
|
|
|
2020-12-20 19:28:40 +03:00
|
|
|
pub const (
|
2019-07-15 18:49:01 +03:00
|
|
|
// The size of a SHA-1 checksum in bytes.
|
2020-12-20 19:28:40 +03:00
|
|
|
size = 20
|
2019-07-15 18:49:01 +03:00
|
|
|
// The blocksize of SHA-1 in bytes.
|
2019-10-24 14:48:20 +03:00
|
|
|
block_size = 64
|
2019-07-15 18:49:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-24 14:48:20 +03:00
|
|
|
chunk = 64
|
|
|
|
init0 = 0x67452301
|
2022-11-06 08:22:28 +03:00
|
|
|
init1 = u32(0xEFCDAB89)
|
|
|
|
init2 = u32(0x98BADCFE)
|
2019-10-24 14:48:20 +03:00
|
|
|
init3 = 0x10325476
|
2022-11-06 08:22:28 +03:00
|
|
|
init4 = u32(0xC3D2E1F0)
|
2019-07-15 18:49:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// digest represents the partial evaluation of a checksum.
|
|
|
|
struct Digest {
|
|
|
|
mut:
|
|
|
|
h []u32
|
2022-04-15 15:35:35 +03:00
|
|
|
x []u8
|
2019-07-15 18:49:01 +03:00
|
|
|
nx int
|
|
|
|
len u64
|
|
|
|
}
|
|
|
|
|
2023-01-16 18:30:40 +03:00
|
|
|
// free the resources taken by the Digest `d`
|
|
|
|
[unsafe]
|
|
|
|
pub fn (mut d Digest) free() {
|
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
d.x.free()
|
|
|
|
d.h.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut d Digest) init() {
|
2022-04-15 15:35:35 +03:00
|
|
|
d.x = []u8{len: sha1.chunk}
|
2020-12-20 19:28:40 +03:00
|
|
|
d.h = []u32{len: (5)}
|
2023-01-16 18:30:40 +03:00
|
|
|
d.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the state of the Digest `d`
|
|
|
|
pub fn (mut d Digest) reset() {
|
2021-05-08 13:32:29 +03:00
|
|
|
d.h[0] = u32(sha1.init0)
|
|
|
|
d.h[1] = u32(sha1.init1)
|
|
|
|
d.h[2] = u32(sha1.init2)
|
|
|
|
d.h[3] = u32(sha1.init3)
|
|
|
|
d.h[4] = u32(sha1.init4)
|
2019-07-15 18:49:01 +03:00
|
|
|
d.nx = 0
|
2019-09-26 14:57:31 +03:00
|
|
|
d.len = 0
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:00:15 +03:00
|
|
|
// new returns a new Digest (implementing hash.Hash) computing the SHA1 checksum.
|
2019-07-15 18:49:01 +03:00
|
|
|
pub fn new() &Digest {
|
|
|
|
mut d := &Digest{}
|
2023-01-16 18:30:40 +03:00
|
|
|
d.init()
|
2019-07-15 18:49:01 +03:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// write writes the contents of `p_` to the internal hash representation.
|
2021-01-11 11:03:10 +03:00
|
|
|
[manualfree]
|
2022-10-20 22:14:33 +03:00
|
|
|
pub fn (mut d Digest) write(p_ []u8) !int {
|
2020-12-20 19:28:40 +03:00
|
|
|
nn := p_.len
|
|
|
|
unsafe {
|
|
|
|
mut p := p_
|
|
|
|
d.len += u64(nn)
|
|
|
|
if d.nx > 0 {
|
2022-03-09 21:26:00 +03:00
|
|
|
n := copy(mut d.x[d.nx..], p)
|
2020-12-20 19:28:40 +03:00
|
|
|
d.nx += n
|
2021-05-08 13:32:29 +03:00
|
|
|
if d.nx == sha1.chunk {
|
2020-12-20 19:28:40 +03:00
|
|
|
block(mut d, d.x)
|
|
|
|
d.nx = 0
|
|
|
|
}
|
|
|
|
if n >= p.len {
|
|
|
|
p = []
|
|
|
|
} else {
|
|
|
|
p = p[n..]
|
|
|
|
}
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
if p.len >= sha1.chunk {
|
|
|
|
n := p.len & ~(sha1.chunk - 1)
|
2020-12-20 19:28:40 +03:00
|
|
|
block(mut d, p[..n])
|
|
|
|
if n >= p.len {
|
|
|
|
p = []
|
|
|
|
} else {
|
|
|
|
p = p[n..]
|
|
|
|
}
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
2020-12-20 19:28:40 +03:00
|
|
|
if p.len > 0 {
|
2022-03-09 21:26:00 +03:00
|
|
|
d.nx = copy(mut d.x, p)
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nn
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// sum returns a copy of the generated sum of the bytes in `b_in`.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (d &Digest) sum(b_in []u8) []u8 {
|
2019-07-15 18:49:01 +03:00
|
|
|
// Make a copy of d so that caller can keep writing and summing.
|
|
|
|
mut d0 := *d
|
2019-07-16 15:20:51 +03:00
|
|
|
hash := d0.checksum()
|
2019-09-29 16:44:52 +03:00
|
|
|
mut b_out := b_in.clone()
|
2019-07-15 18:49:01 +03:00
|
|
|
for b in hash {
|
2019-09-29 16:44:52 +03:00
|
|
|
b_out << b
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
2019-09-29 16:44:52 +03:00
|
|
|
return b_out
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 08:56:01 +03:00
|
|
|
// checksum returns the current byte checksum of the `Digest`.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut d Digest) checksum() []u8 {
|
2019-07-15 18:49:01 +03:00
|
|
|
mut len := d.len
|
|
|
|
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
2022-04-15 15:35:35 +03:00
|
|
|
mut tmp := []u8{len: (64)}
|
2019-07-15 18:49:01 +03:00
|
|
|
tmp[0] = 0x80
|
2020-12-20 19:28:40 +03:00
|
|
|
if int(len) % 64 < 56 {
|
2021-03-01 02:18:14 +03:00
|
|
|
d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
|
2019-07-15 18:49:01 +03:00
|
|
|
} else {
|
2021-03-01 02:18:14 +03:00
|
|
|
d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
// Length in bits.
|
2019-09-26 14:57:31 +03:00
|
|
|
len <<= 3
|
2019-08-07 10:53:33 +03:00
|
|
|
binary.big_endian_put_u64(mut tmp, len)
|
2021-03-01 02:18:14 +03:00
|
|
|
d.write(tmp[..8]) or { panic(err) }
|
2022-04-15 15:35:35 +03:00
|
|
|
mut digest := []u8{len: sha1.size}
|
2019-08-07 10:53:33 +03:00
|
|
|
binary.big_endian_put_u32(mut digest, d.h[0])
|
2019-10-27 10:03:15 +03:00
|
|
|
binary.big_endian_put_u32(mut digest[4..], d.h[1])
|
|
|
|
binary.big_endian_put_u32(mut digest[8..], d.h[2])
|
|
|
|
binary.big_endian_put_u32(mut digest[12..], d.h[3])
|
|
|
|
binary.big_endian_put_u32(mut digest[16..], d.h[4])
|
2019-07-15 18:49:01 +03:00
|
|
|
return digest
|
|
|
|
}
|
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// sum returns the SHA-1 checksum of the bytes passed in `data`.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn sum(data []u8) []u8 {
|
2019-07-16 15:20:51 +03:00
|
|
|
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()
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
fn block(mut dig Digest, p []u8) {
|
2019-07-18 11:50:05 +03:00
|
|
|
// For now just use block_generic until we have specific
|
2019-07-17 12:00:15 +03:00
|
|
|
// architecture optimized versions
|
2019-08-07 14:37:07 +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 19:28:40 +03:00
|
|
|
pub fn (d &Digest) size() int {
|
2021-05-08 13:32:29 +03:00
|
|
|
return sha1.size
|
2020-12-20 19:28:40 +03:00
|
|
|
}
|
2019-07-15 18:49:01 +03:00
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// block_size returns the block size of the checksum in bytes.
|
2020-12-20 19:28:40 +03:00
|
|
|
pub fn (d &Digest) block_size() int {
|
2021-05-08 13:32:29 +03:00
|
|
|
return sha1.block_size
|
2020-12-20 19:28:40 +03:00
|
|
|
}
|
2019-09-02 20:22:19 +03:00
|
|
|
|
2021-01-23 15:33:49 +03:00
|
|
|
// hexhash returns a hexadecimal SHA1 hash sum `string` of `s`.
|
2020-12-20 19:28:40 +03:00
|
|
|
pub fn hexhash(s string) string {
|
|
|
|
return sum(s.bytes()).hex()
|
|
|
|
}
|