ci: fix sha1.v, sha1block_generic.v and szip.v
parent
969f8f1a75
commit
dd5b25a9f2
|
@ -1,15 +1,11 @@
|
|||
// Copyright (c) 2019-2020 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
|
||||
|
@ -59,10 +55,10 @@ pub fn new() &Digest {
|
|||
}
|
||||
|
||||
pub fn (mut d Digest) write(p_ []byte) int {
|
||||
nn := p_.len
|
||||
unsafe {
|
||||
mut p := p_
|
||||
nn := p.len
|
||||
d.len += u64(nn)
|
||||
|
||||
if d.nx > 0 {
|
||||
n := copy(d.x[d.nx..], p)
|
||||
d.nx += n
|
||||
|
@ -88,6 +84,7 @@ pub fn (mut d Digest) write(p_ []byte) int {
|
|||
if p.len > 0 {
|
||||
d.nx = copy(d.x, p)
|
||||
}
|
||||
}
|
||||
return nn
|
||||
}
|
||||
|
||||
|
@ -106,28 +103,22 @@ 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])
|
||||
} else {
|
||||
d.write(tmp[..64 + 56 - int(len) % 64])
|
||||
}
|
||||
|
||||
// Length in bits.
|
||||
len <<= 3
|
||||
binary.big_endian_put_u64(mut tmp, len)
|
||||
d.write(tmp[..8])
|
||||
|
||||
mut digest := []byte{len: (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
|
||||
}
|
||||
|
||||
|
@ -144,8 +135,14 @@ fn block(mut dig Digest, p []byte) {
|
|||
block_generic(mut dig, p)
|
||||
}
|
||||
|
||||
pub fn (d &Digest) size() int { return size }
|
||||
pub fn (d &Digest) size() int {
|
||||
return size
|
||||
}
|
||||
|
||||
pub fn (d &Digest) block_size() int { return block_size }
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return block_size
|
||||
}
|
||||
|
||||
pub fn hexhash(s string) string { return sum(s.bytes()).hex() }
|
||||
pub fn hexhash(s string) string {
|
||||
return sum(s.bytes()).hex()
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
// Copyright (c) 2019-2020 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
|
||||
|
@ -18,6 +16,7 @@ const (
|
|||
)
|
||||
|
||||
fn block_generic(mut dig Digest, p_ []byte) {
|
||||
unsafe {
|
||||
mut p := p_
|
||||
mut w := []u32{len: (16)}
|
||||
mut h0 := dig.h[0]
|
||||
|
@ -32,13 +31,11 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
|||
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).
|
||||
|
@ -101,24 +98,21 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -209,8 +209,7 @@ pub fn (mut zentry Zip) write_entry(data []byte) ? {
|
|||
if (data[0] & 0xff) == -1 {
|
||||
return error('szip: cannot write entry')
|
||||
}
|
||||
buf := data // alias of data
|
||||
res := C.zip_entry_write(zentry, buf.data, buf.len)
|
||||
res := C.zip_entry_write(zentry, data.data, data.len)
|
||||
if res != 0 {
|
||||
return error('szip: failed to write entry')
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue