ci: fix sha1.v, sha1block_generic.v and szip.v
parent
969f8f1a75
commit
dd5b25a9f2
|
@ -1,20 +1,16 @@
|
||||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package sha1 implements the SHA-1 hash algorithm as defined in RFC 3174.
|
// 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
|
// SHA-1 is cryptographically broken and should not be used for secure
|
||||||
// applications.
|
// applications.
|
||||||
|
|
||||||
// Based off: https://github.com/golang/go/blob/master/src/crypto/sha1
|
// Based off: https://github.com/golang/go/blob/master/src/crypto/sha1
|
||||||
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
||||||
|
|
||||||
module sha1
|
module sha1
|
||||||
|
|
||||||
import encoding.binary
|
import encoding.binary
|
||||||
|
|
||||||
pub const(
|
pub const (
|
||||||
// The size of a SHA-1 checksum in bytes.
|
// The size of a SHA-1 checksum in bytes.
|
||||||
size = 20
|
size = 20
|
||||||
// The blocksize of SHA-1 in bytes.
|
// The blocksize of SHA-1 in bytes.
|
||||||
|
@ -40,8 +36,8 @@ mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut d Digest) reset() {
|
fn (mut d Digest) reset() {
|
||||||
d.x = []byte{len:(chunk)}
|
d.x = []byte{len: (chunk)}
|
||||||
d.h = []u32{len:(5)}
|
d.h = []u32{len: (5)}
|
||||||
d.h[0] = u32(init0)
|
d.h[0] = u32(init0)
|
||||||
d.h[1] = u32(init1)
|
d.h[1] = u32(init1)
|
||||||
d.h[2] = u32(init2)
|
d.h[2] = u32(init2)
|
||||||
|
@ -59,10 +55,10 @@ pub fn new() &Digest {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d Digest) write(p_ []byte) int {
|
pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
|
nn := p_.len
|
||||||
|
unsafe {
|
||||||
mut p := p_
|
mut p := p_
|
||||||
nn := p.len
|
|
||||||
d.len += u64(nn)
|
d.len += u64(nn)
|
||||||
|
|
||||||
if d.nx > 0 {
|
if d.nx > 0 {
|
||||||
n := copy(d.x[d.nx..], p)
|
n := copy(d.x[d.nx..], p)
|
||||||
d.nx += n
|
d.nx += n
|
||||||
|
@ -77,7 +73,7 @@ pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.len >= chunk {
|
if p.len >= chunk {
|
||||||
n := p.len &~ (chunk - 1)
|
n := p.len & ~(chunk - 1)
|
||||||
block(mut d, p[..n])
|
block(mut d, p[..n])
|
||||||
if n >= p.len {
|
if n >= p.len {
|
||||||
p = []
|
p = []
|
||||||
|
@ -88,6 +84,7 @@ pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
if p.len > 0 {
|
if p.len > 0 {
|
||||||
d.nx = copy(d.x, p)
|
d.nx = copy(d.x, p)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nn
|
return nn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,29 +102,23 @@ pub fn (d &Digest) sum(b_in []byte) []byte {
|
||||||
fn (mut d Digest) checksum() []byte {
|
fn (mut d Digest) checksum() []byte {
|
||||||
mut len := d.len
|
mut len := d.len
|
||||||
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
|
||||||
mut tmp := []byte{len:(64)}
|
mut tmp := []byte{len: (64)}
|
||||||
|
|
||||||
tmp[0] = 0x80
|
tmp[0] = 0x80
|
||||||
|
if int(len) % 64 < 56 {
|
||||||
if int(len)%64 < 56 {
|
d.write(tmp[..56 - int(len) % 64])
|
||||||
d.write(tmp[..56-int(len)%64])
|
|
||||||
} else {
|
} else {
|
||||||
d.write(tmp[..64+56-int(len)%64])
|
d.write(tmp[..64 + 56 - int(len) % 64])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Length in bits.
|
// Length in bits.
|
||||||
len <<= 3
|
len <<= 3
|
||||||
binary.big_endian_put_u64(mut tmp, len)
|
binary.big_endian_put_u64(mut tmp, len)
|
||||||
d.write(tmp[..8])
|
d.write(tmp[..8])
|
||||||
|
mut digest := []byte{len: (size)}
|
||||||
mut digest := []byte{len:(size)}
|
|
||||||
|
|
||||||
binary.big_endian_put_u32(mut digest, d.h[0])
|
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[4..], d.h[1])
|
||||||
binary.big_endian_put_u32(mut digest[8..], d.h[2])
|
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[12..], d.h[3])
|
||||||
binary.big_endian_put_u32(mut digest[16..], d.h[4])
|
binary.big_endian_put_u32(mut digest[16..], d.h[4])
|
||||||
|
|
||||||
return digest
|
return digest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,8 +135,14 @@ fn block(mut dig Digest, p []byte) {
|
||||||
block_generic(mut dig, p)
|
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.
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
// This is the generic version with no architecture optimizations.
|
// This is the generic version with no architecture optimizations.
|
||||||
// In its own file so that an architecture
|
// In its own file so that an architecture
|
||||||
// optimized verision can be substituted
|
// optimized verision can be substituted
|
||||||
|
|
||||||
module sha1
|
module sha1
|
||||||
|
|
||||||
import math.bits
|
import math.bits
|
||||||
|
@ -18,8 +16,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn block_generic(mut dig Digest, p_ []byte) {
|
fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
|
unsafe {
|
||||||
mut p := p_
|
mut p := p_
|
||||||
mut w := []u32{len:(16)}
|
mut w := []u32{len: (16)}
|
||||||
mut h0 := dig.h[0]
|
mut h0 := dig.h[0]
|
||||||
mut h1 := dig.h[1]
|
mut h1 := dig.h[1]
|
||||||
mut h2 := dig.h[2]
|
mut h2 := dig.h[2]
|
||||||
|
@ -28,24 +27,22 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
for p.len >= chunk {
|
for p.len >= chunk {
|
||||||
// Can interlace the computation of w with the
|
// Can interlace the computation of w with the
|
||||||
// rounds below if needed for speed.
|
// rounds below if needed for speed.
|
||||||
for i in 0..16 {
|
for i in 0 .. 16 {
|
||||||
j := i * 4
|
j := i * 4
|
||||||
w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3])
|
w[i] = u32(p[j] << 24) | u32(p[j + 1] << 16) | u32(p[j + 2] << 8) | u32(p[j + 3])
|
||||||
}
|
}
|
||||||
|
|
||||||
mut a := h0
|
mut a := h0
|
||||||
mut b := h1
|
mut b := h1
|
||||||
mut c := h2
|
mut c := h2
|
||||||
mut d := h3
|
mut d := h3
|
||||||
mut e := h4
|
mut e := h4
|
||||||
|
|
||||||
// Each of the four 20-iteration rounds
|
// Each of the four 20-iteration rounds
|
||||||
// differs only in the computation of f and
|
// differs only in the computation of f and
|
||||||
// the choice of K (_k0, _k1, etc).
|
// the choice of K (_k0, _k1, etc).
|
||||||
mut i := 0
|
mut i := 0
|
||||||
for i < 16 {
|
for i < 16 {
|
||||||
f := b&c | (~b)&d
|
f := b & c | (~b) & d
|
||||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k0)
|
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0)
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
c = bits.rotate_left_32(b, 30)
|
c = bits.rotate_left_32(b, 30)
|
||||||
|
@ -54,10 +51,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
for i < 20 {
|
for i < 20 {
|
||||||
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
|
||||||
w[i&0xf] = (tmp<<1) | (tmp>>(32-1))
|
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||||
f := b&c | (~b)&d
|
f := b & c | (~b) & d
|
||||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k0)
|
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0)
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
c = bits.rotate_left_32(b, 30)
|
c = bits.rotate_left_32(b, 30)
|
||||||
|
@ -66,10 +63,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
for i < 40 {
|
for i < 40 {
|
||||||
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
|
||||||
w[i&0xf] = (tmp<<1) | (tmp>>(32-1))
|
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||||
f := b ^ c ^ d
|
f := b ^ c ^ d
|
||||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k1)
|
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k1)
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
c = bits.rotate_left_32(b, 30)
|
c = bits.rotate_left_32(b, 30)
|
||||||
|
@ -78,10 +75,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
for i < 60 {
|
for i < 60 {
|
||||||
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
|
||||||
w[i&0xf] = (tmp<<1) | (tmp>>(32-1))
|
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||||
f := ((b | c) & d) | (b & c)
|
f := ((b | c) & d) | (b & c)
|
||||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k2)
|
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k2)
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
c = bits.rotate_left_32(b, 30)
|
c = bits.rotate_left_32(b, 30)
|
||||||
|
@ -90,10 +87,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
for i < 80 {
|
for i < 80 {
|
||||||
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
|
||||||
w[i&0xf] = (tmp<<1) | (tmp>>(32-1))
|
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||||
f := b ^ c ^ d
|
f := b ^ c ^ d
|
||||||
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k3)
|
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k3)
|
||||||
e = d
|
e = d
|
||||||
d = c
|
d = c
|
||||||
c = bits.rotate_left_32(b, 30)
|
c = bits.rotate_left_32(b, 30)
|
||||||
|
@ -101,24 +98,21 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
a = t
|
a = t
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
h0 += a
|
h0 += a
|
||||||
h1 += b
|
h1 += b
|
||||||
h2 += c
|
h2 += c
|
||||||
h3 += d
|
h3 += d
|
||||||
h4 += e
|
h4 += e
|
||||||
|
|
||||||
if chunk >= p.len {
|
if chunk >= p.len {
|
||||||
p = []
|
p = []
|
||||||
} else {
|
} else {
|
||||||
p = p[chunk..]
|
p = p[chunk..]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dig.h[0] = h0
|
dig.h[0] = h0
|
||||||
dig.h[1] = h1
|
dig.h[1] = h1
|
||||||
dig.h[2] = h2
|
dig.h[2] = h2
|
||||||
dig.h[3] = h3
|
dig.h[3] = h3
|
||||||
dig.h[4] = h4
|
dig.h[4] = h4
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -209,8 +209,7 @@ pub fn (mut zentry Zip) write_entry(data []byte) ? {
|
||||||
if (data[0] & 0xff) == -1 {
|
if (data[0] & 0xff) == -1 {
|
||||||
return error('szip: cannot write entry')
|
return error('szip: cannot write entry')
|
||||||
}
|
}
|
||||||
buf := data // alias of data
|
res := C.zip_entry_write(zentry, data.data, data.len)
|
||||||
res := C.zip_entry_write(zentry, buf.data, buf.len)
|
|
||||||
if res != 0 {
|
if res != 0 {
|
||||||
return error('szip: failed to write entry')
|
return error('szip: failed to write entry')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue