// 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= 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