From 9c586e7e92d978d79f398dca4cc1666948aa2f60 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 16 Jul 2019 22:20:51 +1000 Subject: [PATCH] crypto md5 --- vlib/crypto/md5/md5.v | 143 ++++++++++++++++++ vlib/crypto/md5/md5_test.v | 9 ++ vlib/crypto/md5/md5block_generic.v | 129 ++++++++++++++++ vlib/crypto/sha1/sha1.v | 18 +-- vlib/crypto/sha1/sha1_test.v | 6 +- .../sha1/{sha1block.v => sha1block_generic.v} | 10 +- 6 files changed, 302 insertions(+), 13 deletions(-) create mode 100644 vlib/crypto/md5/md5.v create mode 100644 vlib/crypto/md5/md5_test.v create mode 100644 vlib/crypto/md5/md5block_generic.v rename vlib/crypto/sha1/{sha1block.v => sha1block_generic.v} (89%) diff --git a/vlib/crypto/md5/md5.v b/vlib/crypto/md5/md5.v new file mode 100644 index 0000000000..0d170253d6 --- /dev/null +++ b/vlib/crypto/md5/md5.v @@ -0,0 +1,143 @@ +// 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 md5 implements the MD5 hash algorithm as defined in RFC 1321. + +// MD5 is cryptographically broken and should not be used for secure +// applications. + +// Adapted from: https://github.com/golang/go/blob/master/src/crypto/md5 + +module md5 + +import math +import encoding.binary + +const ( + // The size of an MD5 checksum in bytes. + Size = 16 + // The blocksize of MD5 in bytes. + BlockSize = 64 +) + +const ( + Init0 = 0x67452301 + Init1 = 0xEFCDAB89 + Init2 = 0x98BADCFE + Init3 = 0x10325476 +) + +// Digest represents the partial evaluation of a checksum. +struct Digest { +mut: + s []u32 + x []byte + nx int + len u64 +} + +fn (d mut Digest) reset() { + d.s = [u32(0); 4] + d.x = [byte(0); BlockSize] + d.s[0] = u32(Init0) + d.s[1] = u32(Init1) + d.s[2] = u32(Init2) + d.s[3] = u32(Init3) + d.nx = 0 + d.len = u64(0) +} + +// New returns a new hash.Hash computing the MD5 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= p.len { + p = []byte + } else { + p = p.right(n) + } + } + if p.len >= BlockSize { + n := p.len &~ (BlockSize - 1) + block_generic(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= p.len { @@ -79,7 +80,7 @@ pub fn (d mut Digest) write(p []byte) ?int { } if p.len >= Chunk { n := p.len &~ (Chunk - 1) - block(d, p.left(n)) + block_generic(d, p.left(n)) if n >= p.len { p = []byte } else { @@ -98,14 +99,14 @@ pub fn (d mut Digest) write(p []byte) ?int { 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() + hash := d0.checksum() for b in hash { b_in << b } return *b_in } -fn (d mut Digest) check_sum() []byte { +fn (d mut Digest) checksum() []byte { mut len := d.len // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. mut tmp := [byte(0); 64] @@ -136,10 +137,9 @@ fn (d mut Digest) check_sum() []byte { // Sum returns the SHA-1 checksum of the data. pub fn sum(data []byte) []byte { - mut d := Digest{} - d.reset() + mut d := new() d.write(data) - return d.check_sum() + return d.checksum() } pub fn (d &Digest) size() int { return Size } diff --git a/vlib/crypto/sha1/sha1_test.v b/vlib/crypto/sha1/sha1_test.v index 1e39923d6b..3e95cb6525 100644 --- a/vlib/crypto/sha1/sha1_test.v +++ b/vlib/crypto/sha1/sha1_test.v @@ -1,5 +1,9 @@ +// 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. + import crypto.sha1 fn test_crypto_sha1() { - assert sha1.sum('This is a sha1 hash.'.bytes()).hex() == '6FF5FA4D5166D5C2576FE56ED1EC2D5AB0FDF936' + assert sha1.sum('This is a sha1 checksum.'.bytes()).hex() == 'E100D74442FAA5DCD59463B808983C810A8EB5A1' } diff --git a/vlib/crypto/sha1/sha1block.v b/vlib/crypto/sha1/sha1block_generic.v similarity index 89% rename from vlib/crypto/sha1/sha1block.v rename to vlib/crypto/sha1/sha1block_generic.v index fb10bfc6f6..c96b8fb6e3 100644 --- a/vlib/crypto/sha1/sha1block.v +++ b/vlib/crypto/sha1/sha1block_generic.v @@ -1,3 +1,9 @@ +// 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. + +// This is a generic implementation with no arch optimizations + module sha1 import math.bits @@ -9,7 +15,7 @@ const ( _K3 = 0xCA62C1D6 ) -fn block(dig &Digest, p []byte) { +fn block_generic(dig &Digest, p []byte) { mut w := [u32(0); 16] mut h0 := dig.h[0] mut h1 := dig.h[1] @@ -73,13 +79,11 @@ fn block(dig &Digest, p []byte) { w[i&0xf] = 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 {