1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ci: fix sha1.v, sha1block_generic.v and szip.v

This commit is contained in:
Delyan Angelov 2020-12-20 18:28:40 +02:00
parent 969f8f1a75
commit dd5b25a9f2
3 changed files with 142 additions and 152 deletions

View File

@ -1,22 +1,18 @@
// 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
pub const(
pub const (
// The size of a SHA-1 checksum in bytes.
size = 20
size = 20
// The blocksize of SHA-1 in bytes.
block_size = 64
)
@ -40,8 +36,8 @@ mut:
}
fn (mut d Digest) reset() {
d.x = []byte{len:(chunk)}
d.h = []u32{len:(5)}
d.x = []byte{len: (chunk)}
d.h = []u32{len: (5)}
d.h[0] = u32(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
@ -59,35 +55,36 @@ pub fn new() &Digest {
}
pub fn (mut d Digest) write(p_ []byte) int {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == chunk {
block(mut d, d.x)
d.nx = 0
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 == chunk {
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
if n >= p.len {
p = []
} else {
p = p[n..]
if p.len >= chunk {
n := p.len & ~(chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
}
if p.len >= chunk {
n := p.len &~ (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)
}
}
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
}
@ -105,29 +102,23 @@ pub fn (d &Digest) sum(b_in []byte) []byte {
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)}
mut tmp := []byte{len: (64)}
tmp[0] = 0x80
if int(len)%64 < 56 {
d.write(tmp[..56-int(len)%64])
if int(len) % 64 < 56 {
d.write(tmp[..56 - int(len) % 64])
} else {
d.write(tmp[..64+56-int(len)%64])
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)}
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()
}

View File

@ -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,107 +16,103 @@ const (
)
fn block_generic(mut dig Digest, p_ []byte) {
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])
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(_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(_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(_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(_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(_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..]
}
}
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(_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(_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(_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(_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(_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
}
dig.h[0] = h0
dig.h[1] = h1
dig.h[2] = h2
dig.h[3] = h3
dig.h[4] = h4
}

View File

@ -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')
}