1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/crypto/sha1/sha1.v

152 lines
3.0 KiB
V
Raw Normal View History

2020-02-03 07:00:36 +03:00
// Copyright (c) 2019-2020 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.
2019-07-16 15:20:51 +03:00
2019-07-15 18:49:01 +03:00
// SHA-1 is cryptographically broken and should not be used for secure
// applications.
2019-07-16 15:20:51 +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-16 15:20:51 +03:00
2019-07-15 18:49:01 +03:00
module sha1
import encoding.binary
pub const(
2019-07-15 18:49:01 +03:00
// The size of a SHA-1 checksum in bytes.
2019-10-24 14:48:20 +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
init1 = 0xEFCDAB89
init2 = 0x98BADCFE
init3 = 0x10325476
init4 = 0xC3D2E1F0
2019-07-15 18:49:01 +03:00
)
// digest represents the partial evaluation of a checksum.
struct Digest {
mut:
h []u32
x []byte
nx int
len u64
}
2020-05-17 14:51:18 +03:00
fn (mut d Digest) reset() {
d.x = []byte{len:(chunk)}
d.h = []u32{len:(5)}
2019-10-24 14:48:20 +03:00
d.h[0] = u32(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
d.h[3] = u32(init3)
d.h[4] = u32(init4)
2019-07-15 18:49:01 +03:00
d.nx = 0
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{}
d.reset()
return d
}
2020-05-17 14:51:18 +03:00
pub fn (mut d Digest) write(p_ []byte) int {
2019-08-07 14:37:07 +03:00
mut p := p_
2019-07-15 18:49:01 +03:00
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
2019-07-15 18:49:01 +03:00
d.nx += n
2019-10-24 14:48:20 +03:00
if d.nx == chunk {
2019-07-17 12:00:15 +03:00
block(d, d.x)
2019-07-15 18:49:01 +03:00
d.nx = 0
}
if n >= p.len {
2019-11-14 09:53:05 +03:00
p = []
2019-07-15 18:49:01 +03:00
} else {
p = p[n..]
2019-07-15 18:49:01 +03:00
}
}
2019-10-24 14:48:20 +03:00
if p.len >= chunk {
n := p.len &~ (chunk - 1)
block(d, p[..n])
2019-07-15 18:49:01 +03:00
if n >= p.len {
2019-11-14 09:53:05 +03:00
p = []
2019-07-15 18:49:01 +03:00
} else {
p = p[n..]
2019-07-15 18:49:01 +03:00
}
}
if p.len > 0 {
2019-07-29 17:33:35 +03:00
d.nx = copy(d.x, p)
2019-07-15 18:49:01 +03:00
}
return nn
}
2019-09-29 16:44:52 +03:00
pub fn (d &Digest) sum(b_in []byte) []byte {
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
}
2020-05-17 14:51:18 +03:00
fn (mut d Digest) checksum() []byte {
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.
mut tmp := []byte{len:(64)}
2019-07-15 18:49:01 +03:00
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp[..56-int(len)%64])
2019-07-15 18:49:01 +03:00
} else {
d.write(tmp[..64+56-int(len)%64])
2019-07-15 18:49:01 +03:00
}
// Length in bits.
len <<= 3
2019-08-07 10:53:33 +03:00
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp[..8])
2019-07-15 18:49:01 +03:00
mut digest := []byte{len:(size)}
2019-07-15 18:49:01 +03:00
2019-08-07 10:53:33 +03:00
binary.big_endian_put_u32(mut digest, d.h[0])
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
}
// Sum returns the SHA-1 checksum of the data.
pub fn sum(data []byte) []byte {
2019-07-16 15:20:51 +03:00
mut d := new()
2019-08-07 14:37:07 +03:00
d.write(data)
2019-07-16 15:20:51 +03:00
return d.checksum()
2019-07-15 18:49:01 +03:00
}
2019-07-17 12:00:15 +03:00
fn block(dig &Digest, p []byte) {
// 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
}
2019-10-24 14:48:20 +03:00
pub fn (d &Digest) size() int { return size }
2019-07-15 18:49:01 +03:00
2019-10-24 14:48:20 +03:00
pub fn (d &Digest) block_size() int { return block_size }
2019-09-02 20:22:19 +03:00
pub fn hexhash(s string) string { return sum(s.bytes()).hex() }