| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | // Package sha1 implements the SHA-1 hash algorithm as defined in RFC 3174. |
| 5 | // SHA-1 is cryptographically broken and should not be used for secure |
| 6 | // applications. |
| 7 | // Based off: https://github.com/golang/go/blob/master/src/crypto/sha1 |
| 8 | // Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01 |
| 9 | module sha1 |
| 10 | |
| 11 | import encoding.binary |
| 12 | |
| 13 | // The size of a SHA-1 checksum in bytes. |
| 14 | pub const size = 20 |
| 15 | // The blocksize of SHA-1 in bytes. |
| 16 | pub const block_size = 64 |
| 17 | |
| 18 | const chunk = 64 |
| 19 | const init0 = u32(0x67452301) |
| 20 | const init1 = u32(0xEFCDAB89) |
| 21 | const init2 = u32(0x98BADCFE) |
| 22 | const init3 = u32(0x10325476) |
| 23 | const init4 = u32(0xC3D2E1F0) |
| 24 | |
| 25 | // digest represents the partial evaluation of a checksum. |
| 26 | struct Digest { |
| 27 | mut: |
| 28 | h []u32 |
| 29 | x []u8 |
| 30 | nx int |
| 31 | len u64 |
| 32 | } |
| 33 | |
| 34 | // free the resources taken by the Digest `d` |
| 35 | @[unsafe] |
| 36 | pub fn (mut d Digest) free() { |
| 37 | $if prealloc { |
| 38 | return |
| 39 | } |
| 40 | unsafe { |
| 41 | d.x.free() |
| 42 | d.h.free() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn (mut d Digest) init() { |
| 47 | d.x = []u8{len: chunk} |
| 48 | d.h = []u32{len: (5)} |
| 49 | d.reset() |
| 50 | } |
| 51 | |
| 52 | // reset the state of the Digest `d` |
| 53 | pub fn (mut d Digest) reset() { |
| 54 | d.h[0] = u32(init0) |
| 55 | d.h[1] = u32(init1) |
| 56 | d.h[2] = u32(init2) |
| 57 | d.h[3] = u32(init3) |
| 58 | d.h[4] = u32(init4) |
| 59 | d.nx = 0 |
| 60 | d.len = 0 |
| 61 | } |
| 62 | |
| 63 | fn (d &Digest) clone() &Digest { |
| 64 | return &Digest{ |
| 65 | ...d |
| 66 | h: d.h.clone() |
| 67 | x: d.x.clone() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // new returns a new Digest (implementing hash.Hash) computing the SHA1 checksum. |
| 72 | pub fn new() &Digest { |
| 73 | mut d := &Digest{} |
| 74 | d.init() |
| 75 | return d |
| 76 | } |
| 77 | |
| 78 | // write writes the contents of `p_` to the internal hash representation. |
| 79 | @[manualfree] |
| 80 | pub fn (mut d Digest) write(p_ []u8) !int { |
| 81 | nn := p_.len |
| 82 | unsafe { |
| 83 | mut p := p_ |
| 84 | d.len += u64(nn) |
| 85 | if d.nx > 0 { |
| 86 | n := copy(mut d.x[d.nx..], p) |
| 87 | d.nx += n |
| 88 | if d.nx == chunk { |
| 89 | block(mut d, d.x) |
| 90 | d.nx = 0 |
| 91 | } |
| 92 | if n >= p.len { |
| 93 | p = [] |
| 94 | } else { |
| 95 | p = p[n..] |
| 96 | } |
| 97 | } |
| 98 | if p.len >= chunk { |
| 99 | n := p.len & ~(chunk - 1) |
| 100 | block(mut d, p[..n]) |
| 101 | if n >= p.len { |
| 102 | p = [] |
| 103 | } else { |
| 104 | p = p[n..] |
| 105 | } |
| 106 | } |
| 107 | if p.len > 0 { |
| 108 | d.nx = copy(mut d.x, p) |
| 109 | } |
| 110 | } |
| 111 | return nn |
| 112 | } |
| 113 | |
| 114 | // sum returns a copy of the generated sum of the bytes in `b_in`. |
| 115 | pub fn (d &Digest) sum(b_in []u8) []u8 { |
| 116 | // Make a copy of d so that caller can keep writing and summing. |
| 117 | mut d0 := d.clone() |
| 118 | hash := d0.checksum() |
| 119 | mut b_out := b_in.clone() |
| 120 | for b in hash { |
| 121 | b_out << b |
| 122 | } |
| 123 | return b_out |
| 124 | } |
| 125 | |
| 126 | // checksum returns the current byte checksum of the `Digest`, |
| 127 | @[direct_array_access] |
| 128 | fn (mut d Digest) checksum() []u8 { |
| 129 | mut len := d.len |
| 130 | // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. |
| 131 | mut tmp := []u8{len: (64)} |
| 132 | tmp[0] = 0x80 |
| 133 | if int(len) % 64 < 56 { |
| 134 | d.write(tmp[..56 - int(len) % 64]) or { panic(err) } |
| 135 | } else { |
| 136 | d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) } |
| 137 | } |
| 138 | // Length in bits. |
| 139 | len <<= 3 |
| 140 | binary.big_endian_put_u64(mut tmp, len) |
| 141 | d.write(tmp[..8]) or { panic(err) } |
| 142 | mut digest := []u8{len: size} |
| 143 | binary.big_endian_put_u32(mut digest, d.h[0]) |
| 144 | binary.big_endian_put_u32(mut digest[4..], d.h[1]) |
| 145 | binary.big_endian_put_u32(mut digest[8..], d.h[2]) |
| 146 | binary.big_endian_put_u32(mut digest[12..], d.h[3]) |
| 147 | binary.big_endian_put_u32(mut digest[16..], d.h[4]) |
| 148 | return digest |
| 149 | } |
| 150 | |
| 151 | // sum returns the SHA-1 checksum of the bytes passed in `data`. |
| 152 | pub fn sum(data []u8) []u8 { |
| 153 | mut d := new() |
| 154 | d.write(data) or { panic(err) } |
| 155 | return d.checksum() |
| 156 | } |
| 157 | |
| 158 | fn block(mut dig Digest, p []u8) { |
| 159 | // For now just use block_generic until we have specific |
| 160 | // architecture optimized versions |
| 161 | block_generic(mut dig, p) |
| 162 | } |
| 163 | |
| 164 | // size returns the size of the checksum in bytes. |
| 165 | pub fn (d &Digest) size() int { |
| 166 | return size |
| 167 | } |
| 168 | |
| 169 | // block_size returns the block size of the checksum in bytes. |
| 170 | pub fn (d &Digest) block_size() int { |
| 171 | return block_size |
| 172 | } |
| 173 | |
| 174 | // hexhash returns a hexadecimal SHA1 hash sum `string` of `s`. |
| 175 | pub fn hexhash(s string) string { |
| 176 | return sum(s.bytes()).hex() |
| 177 | } |
| 178 | |