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:
parent
969f8f1a75
commit
dd5b25a9f2
@ -1,22 +1,18 @@
|
|||||||
// 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.
|
||||||
block_size = 64
|
block_size = 64
|
||||||
)
|
)
|
||||||
@ -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,35 +55,36 @@ pub fn new() &Digest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d Digest) write(p_ []byte) int {
|
pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
mut p := p_
|
nn := p_.len
|
||||||
nn := p.len
|
unsafe {
|
||||||
d.len += u64(nn)
|
mut p := p_
|
||||||
|
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
|
||||||
if d.nx == chunk {
|
if d.nx == chunk {
|
||||||
block(mut d, d.x)
|
block(mut d, d.x)
|
||||||
d.nx = 0
|
d.nx = 0
|
||||||
|
}
|
||||||
|
if n >= p.len {
|
||||||
|
p = []
|
||||||
|
} else {
|
||||||
|
p = p[n..]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if n >= p.len {
|
if p.len >= chunk {
|
||||||
p = []
|
n := p.len & ~(chunk - 1)
|
||||||
} else {
|
block(mut d, p[..n])
|
||||||
p = p[n..]
|
if n >= p.len {
|
||||||
|
p = []
|
||||||
|
} else {
|
||||||
|
p = p[n..]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if p.len > 0 {
|
||||||
if p.len >= chunk {
|
d.nx = copy(d.x, p)
|
||||||
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)
|
|
||||||
}
|
|
||||||
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,107 +16,103 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
fn block_generic(mut dig Digest, p_ []byte) {
|
fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
mut p := p_
|
unsafe {
|
||||||
mut w := []u32{len:(16)}
|
mut p := p_
|
||||||
mut h0 := dig.h[0]
|
mut w := []u32{len: (16)}
|
||||||
mut h1 := dig.h[1]
|
mut h0 := dig.h[0]
|
||||||
mut h2 := dig.h[2]
|
mut h1 := dig.h[1]
|
||||||
mut h3 := dig.h[3]
|
mut h2 := dig.h[2]
|
||||||
mut h4 := dig.h[4]
|
mut h3 := dig.h[3]
|
||||||
for p.len >= chunk {
|
mut h4 := dig.h[4]
|
||||||
// Can interlace the computation of w with the
|
for p.len >= chunk {
|
||||||
// rounds below if needed for speed.
|
// Can interlace the computation of w with the
|
||||||
for i in 0..16 {
|
// rounds below if needed for speed.
|
||||||
j := i * 4
|
for i in 0 .. 16 {
|
||||||
w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3])
|
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..]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
dig.h[0] = h0
|
||||||
mut a := h0
|
dig.h[1] = h1
|
||||||
mut b := h1
|
dig.h[2] = h2
|
||||||
mut c := h2
|
dig.h[3] = h3
|
||||||
mut d := h3
|
dig.h[4] = h4
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -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
Block a user