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

sha1 implementation + helper funcs

This commit is contained in:
joe-conigliaro
2019-07-16 01:49:01 +10:00
committed by Alexander Medvednikov
parent 37aff9b107
commit a7529b7b05
9 changed files with 454 additions and 3 deletions

147
vlib/crypto/sha1/sha1.v Normal file
View File

@@ -0,0 +1,147 @@
// Copyright (c) 2019 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 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.
// Adapted from: https://github.com/golang/go/blob/master/src/crypto/sha1
module sha1
import math
import encoding.binary
const(
// The size of a SHA-1 checksum in bytes.
Size = 20
// The blocksize of SHA-1 in bytes.
BlockSize = 64
)
const (
Chunk = 64
Init0 = 0x67452301
Init1 = 0xEFCDAB89
Init2 = 0x98BADCFE
Init3 = 0x10325476
Init4 = 0xC3D2E1F0
)
// digest represents the partial evaluation of a checksum.
struct Digest {
mut:
h []u32
x []byte
nx int
len u64
}
fn (d mut Digest) reset() {
d.x = [byte(0); Chunk]
d.h = [u32(0); 5]
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)
d.nx = 0
d.len = u64(0)
}
// New returns a new Digest (implementing hash.Hash) computing the SHA1 checksum.
pub fn new() &Digest {
mut d := &Digest{}
d.reset()
return d
}
pub fn (d mut Digest) write(p []byte) ?int {
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<n; i++ {
d.x.set(i+d.nx, p[i])
}
d.nx += n
if d.nx == Chunk {
block(d, d.x)
d.nx = 0
}
if n >= p.len {
p = []byte
} else {
p = p.right(n)
}
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
block(d, p.left(n))
if n >= p.len {
p = []byte
} else {
p = p.right(n)
}
}
if p.len > 0 {
d.nx = int(math.min(f64(d.x.len), f64(p.len)))
for i:=0; i<d.nx; i++ {
d.x.set(i, p[i])
}
}
return nn
}
pub fn (d &Digest) sum(b_in mut []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
mut d0 := *d
hash := d0.check_sum()
for b in hash {
b_in << b
}
return *b_in
}
fn (d mut Digest) check_sum() []byte {
mut len := d.len
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
mut tmp := [byte(0); 64]
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp.left(56-int(len)%64))
} else {
d.write(tmp.left(64+56-int(len)%64))
}
// Length in bits.
len <<= u64(3)
binary.big_endian_put_u64(tmp, len)
d.write(tmp.left(8))
mut digest := [byte(0); Size]
binary.big_endian_put_u32(digest, d.h[0])
binary.big_endian_put_u32(digest.right(4), d.h[1])
binary.big_endian_put_u32(digest.right(8), d.h[2])
binary.big_endian_put_u32(digest.right(12), d.h[3])
binary.big_endian_put_u32(digest.right(16), d.h[4])
return digest
}
// Sum returns the SHA-1 checksum of the data.
pub fn sum(data []byte) []byte {
mut d := Digest{}
d.reset()
d.write(data)
return d.check_sum()
}
pub fn (d &Digest) size() int { return Size }
pub fn (d &Digest) block_size() int { return BlockSize }

View File

@@ -0,0 +1,5 @@
import crypto.sha1
fn test_crypto_sha1() {
assert sha1.sum('This is a sha1 hash.'.bytes()).hex() == '6FF5FA4D5166D5C2576FE56ED1EC2D5AB0FDF936'
}

View File

@@ -0,0 +1,117 @@
module sha1
import math.bits
const (
_K0 = 0x5A827999
_K1 = 0x6ED9EBA1
_K2 = 0x8F1BBCDC
_K3 = 0xCA62C1D6
)
fn block(dig &Digest, p []byte) {
mut w := [u32(0); 16]
mut h0 := dig.h[0]
mut h1 := dig.h[1]
mut h2 := dig.h[2]
mut h3 := dig.h[3]
mut h4 := dig.h[4]
for p.len >= Chunk {
// Can interlace the computation of w with the
// rounds below if needed for speed.
for i := 0; i < 16; i++ {
j := i * 4
w[i] = u32(u32(p[j])<<u32(24)) | u32(u32(p[j+1])<<u32(16)) | u32(u32(p[j+2])<<u32(8)) | u32(u32(p[j+3]))
}
mut a := h0
mut b := h1
mut c := h2
mut d := h3
mut e := h4
// Each of the four 20-iteration rounds
// differs only in the computation of f and
// the choice of K (_K0, _K1, etc).
mut i := 0
for i < 16 {
f := u32(b&c | (~b)&d)
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K0)
e = d
d = c
c = bits.rotate_left_32(b, 30)
b = a
a = t
i++
}
for i < 20 {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = u32(tmp<<u32(1)) | u32(tmp>>u32(32-1))
f := b&c | (~b)&d
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K0)
e = d
d = c
c = bits.rotate_left_32(b, 30)
b = a
a = t
i++
}
for i < 40 {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = u32(tmp<<u32(1)) | u32(tmp>>u32(32-1))
f := b ^ c ^ d
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K1)
e = d
d = c
c = bits.rotate_left_32(b, 30)
b = a
a = t
i++
}
for i < 60 {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = u32(tmp<<u32(1)) | u32(tmp>>u32(32-1))
f := ((b | c) & d) | (b & c)
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K2)
e = d
d = c
c = bits.rotate_left_32(b, 30)
b = a
a = t
i++
}
for i < 80 {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = u32(tmp<<u32(1)) | u32(tmp>>u32(32-1))
f := b ^ c ^ d
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_K3)
e = d
d = c
c = bits.rotate_left_32(b, 30)
b = a
a = t
i++
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
if Chunk >= p.len {
p = []byte
} else {
p = p.right(Chunk)
}
}
dig.h[0] = h0
dig.h[1] = h1
dig.h[2] = h2
dig.h[3] = h3
dig.h[4] = h4
}