aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/crypto/sha1
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/old/vlib/crypto/sha1')
-rw-r--r--v_windows/v/old/vlib/crypto/sha1/sha1.v155
-rw-r--r--v_windows/v/old/vlib/crypto/sha1/sha1_test.v8
-rw-r--r--v_windows/v/old/vlib/crypto/sha1/sha1block_generic.v118
3 files changed, 281 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/crypto/sha1/sha1.v b/v_windows/v/old/vlib/crypto/sha1/sha1.v
new file mode 100644
index 0000000..d792eb1
--- /dev/null
+++ b/v_windows/v/old/vlib/crypto/sha1/sha1.v
@@ -0,0 +1,155 @@
+// Copyright (c) 2019-2021 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.
+// Based off: https://github.com/golang/go/blob/master/src/crypto/sha1
+// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
+module sha1
+
+import encoding.binary
+
+pub const (
+ // The size of a SHA-1 checksum in bytes.
+ size = 20
+ // The blocksize of SHA-1 in bytes.
+ block_size = 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 (mut d Digest) reset() {
+ d.x = []byte{len: sha1.chunk}
+ d.h = []u32{len: (5)}
+ 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)
+ d.nx = 0
+ d.len = 0
+}
+
+// new returns a new Digest (implementing hash.Hash) computing the SHA1 checksum.
+pub fn new() &Digest {
+ mut d := &Digest{}
+ d.reset()
+ return d
+}
+
+// write writes the contents of `p_` to the internal hash representation.
+[manualfree]
+pub fn (mut d Digest) write(p_ []byte) ?int {
+ nn := p_.len
+ unsafe {
+ mut p := p_
+ d.len += u64(nn)
+ if d.nx > 0 {
+ n := copy(d.x[d.nx..], p)
+ d.nx += n
+ if d.nx == sha1.chunk {
+ block(mut d, d.x)
+ d.nx = 0
+ }
+ if n >= p.len {
+ p = []
+ } else {
+ p = p[n..]
+ }
+ }
+ if p.len >= sha1.chunk {
+ n := p.len & ~(sha1.chunk - 1)
+ block(mut d, p[..n])
+ if n >= p.len {
+ p = []
+ } else {
+ p = p[n..]
+ }
+ }
+ if p.len > 0 {
+ d.nx = copy(d.x, p)
+ }
+ }
+ return nn
+}
+
+// sum returns a copy of the generated sum of the bytes in `b_in`.
+pub fn (d &Digest) sum(b_in []byte) []byte {
+ // Make a copy of d so that caller can keep writing and summing.
+ mut d0 := *d
+ hash := d0.checksum()
+ mut b_out := b_in.clone()
+ for b in hash {
+ b_out << b
+ }
+ return b_out
+}
+
+// checksum returns the byte checksum of the `Digest`.
+fn (mut d Digest) checksum() []byte {
+ mut len := d.len
+ // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
+ mut tmp := []byte{len: (64)}
+ tmp[0] = 0x80
+ if int(len) % 64 < 56 {
+ d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
+ } else {
+ d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
+ }
+ // Length in bits.
+ len <<= 3
+ binary.big_endian_put_u64(mut tmp, len)
+ d.write(tmp[..8]) or { panic(err) }
+ mut digest := []byte{len: sha1.size}
+ 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])
+ return digest
+}
+
+// sum returns the SHA-1 checksum of the bytes passed in `data`.
+pub fn sum(data []byte) []byte {
+ mut d := new()
+ d.write(data) or { panic(err) }
+ return d.checksum()
+}
+
+fn block(mut dig Digest, p []byte) {
+ // For now just use block_generic until we have specific
+ // architecture optimized versions
+ block_generic(mut dig, p)
+}
+
+// size returns the size of the checksum in bytes.
+pub fn (d &Digest) size() int {
+ return sha1.size
+}
+
+// block_size returns the block size of the checksum in bytes.
+pub fn (d &Digest) block_size() int {
+ return sha1.block_size
+}
+
+// hexhash returns a hexadecimal SHA1 hash sum `string` of `s`.
+pub fn hexhash(s string) string {
+ return sum(s.bytes()).hex()
+}
diff --git a/v_windows/v/old/vlib/crypto/sha1/sha1_test.v b/v_windows/v/old/vlib/crypto/sha1/sha1_test.v
new file mode 100644
index 0000000..b52ea06
--- /dev/null
+++ b/v_windows/v/old/vlib/crypto/sha1/sha1_test.v
@@ -0,0 +1,8 @@
+// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
+// Use of this source code is governed by an MIT license
+// that can be found in the LICENSE file.
+import crypto.sha1
+
+fn test_crypto_sha1() {
+ assert sha1.sum('This is a sha1 checksum.'.bytes()).hex() == 'e100d74442faa5dcd59463b808983c810a8eb5a1'
+}
diff --git a/v_windows/v/old/vlib/crypto/sha1/sha1block_generic.v b/v_windows/v/old/vlib/crypto/sha1/sha1block_generic.v
new file mode 100644
index 0000000..a0dc92c
--- /dev/null
+++ b/v_windows/v/old/vlib/crypto/sha1/sha1block_generic.v
@@ -0,0 +1,118 @@
+// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
+// Use of this source code is governed by an MIT license
+// that can be found in the LICENSE file.
+// This is the generic version with no architecture optimizations.
+// In its own file so that an architecture
+// optimized verision can be substituted
+module sha1
+
+import math.bits
+
+const (
+ _k0 = 0x5A827999
+ _k1 = 0x6ED9EBA1
+ _k2 = 0x8F1BBCDC
+ _k3 = 0xCA62C1D6
+)
+
+fn block_generic(mut dig Digest, p_ []byte) {
+ unsafe {
+ mut p := p_
+ mut w := []u32{len: (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 in 0 .. 16 {
+ j := i * 4
+ w[i] = u32(p[j] << 24) | u32(p[j + 1] << 16) | u32(p[j + 2] << 8) | 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 := b & c | (~b) & d
+ t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._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] = (tmp << 1) | (tmp >> (32 - 1))
+ f := b & c | (~b) & d
+ t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._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] = (tmp << 1) | (tmp >> (32 - 1))
+ f := b ^ c ^ d
+ t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._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] = (tmp << 1) | (tmp >> (32 - 1))
+ f := ((b | c) & d) | (b & c)
+ t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._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] = (tmp << 1) | (tmp >> (32 - 1))
+ f := b ^ c ^ d
+ t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._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 = []
+ } else {
+ p = p[chunk..]
+ }
+ }
+ dig.h[0] = h0
+ dig.h[1] = h1
+ dig.h[2] = h2
+ dig.h[3] = h3
+ dig.h[4] = h4
+ }
+}