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

all: minor array fixes

This commit is contained in:
Alexander Medvednikov
2020-12-20 16:08:56 +01:00
parent 3c210a57f9
commit 50a6976b5e
12 changed files with 357 additions and 417 deletions

View File

@ -11,29 +11,29 @@ import crypto
import encoding.binary
pub const (
// size is the size, in bytes, of a SHA-512 checksum.
size = 64
// size is the size, in bytes, of a SHA-512 checksum.
size = 64
// size224 is the size, in bytes, of a SHA-512/224 checksum.
size224 = 28
size224 = 28
// size256 is the size, in bytes, of a SHA-512/256 checksum.
size256 = 32
size256 = 32
// size384 is the size, in bytes, of a SHA-384 checksum.
size384 = 48
size384 = 48
// block_size is the block size, in bytes, of the SHA-512/224,
// SHA-512/256, SHA-384 and SHA-512 hash functions.
block_size = 128
)
const (
chunk = 128
init0 = u64(0x6a09e667f3bcc908)
init1 = u64(0xbb67ae8584caa73b)
init2 = u64(0x3c6ef372fe94f82b)
init3 = u64(0xa54ff53a5f1d36f1)
init4 = u64(0x510e527fade682d1)
init5 = u64(0x9b05688c2b3e6c1f)
init6 = u64(0x1f83d9abfb41bd6b)
init7 = u64(0x5be0cd19137e2179)
chunk = 128
init0 = u64(0x6a09e667f3bcc908)
init1 = u64(0xbb67ae8584caa73b)
init2 = u64(0x3c6ef372fe94f82b)
init3 = u64(0xa54ff53a5f1d36f1)
init4 = u64(0x510e527fade682d1)
init5 = u64(0x9b05688c2b3e6c1f)
init6 = u64(0x1f83d9abfb41bd6b)
init7 = u64(0x5be0cd19137e2179)
init0_224 = u64(0x8c3d37c819544da2)
init1_224 = u64(0x73e1996689dcd4d6)
init2_224 = u64(0x1dfab7ae32ff9c82)
@ -59,6 +59,7 @@ const (
init6_384 = u64(0xdb0c2e0d64f98fa7)
init7_384 = u64(0x47b5481dbefa4fa4)
)
// digest represents the partial evaluation of a checksum.
struct Digest {
mut:
@ -70,8 +71,8 @@ mut:
}
fn (mut d Digest) reset() {
d.h = []u64{len:(8)}
d.x = []byte{len:(chunk)}
d.h = []u64{len: (8)}
d.x = []byte{len: (chunk)}
match d.function {
.sha384 {
d.h[0] = init0_384
@ -112,7 +113,8 @@ fn (mut d Digest) reset() {
d.h[5] = init5
d.h[6] = init6
d.h[7] = init7
}}
}
}
d.nx = 0
d.len = 0
}
@ -147,37 +149,37 @@ fn new384() &Digest {
}
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
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
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 = []
if p.len >= chunk {
n := p.len & ~(chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
else {
p = p[n..]
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
}
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)
}
return nn
}
fn (d &Digest) sum(b_in []byte) []byte {
@ -205,19 +207,19 @@ fn (d &Digest) sum(b_in []byte) []byte {
for b in hash {
b_out << b
}
}}
}
}
return b_out
}
fn (mut d Digest) checksum() []byte {
// Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
mut len := d.len
mut tmp := []byte{len:(128)}
mut tmp := []byte{len: (128)}
tmp[0] = 0x80
if int(len) % 128 < 112 {
d.write(tmp[..112 - int(len) % 128])
}
else {
} else {
d.write(tmp[..128 + 112 - int(len) % 128])
}
// Length in bits.
@ -228,7 +230,7 @@ fn (mut d Digest) checksum() []byte {
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []byte{len:(size)}
mut digest := []byte{len: (size)}
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest[8..], d.h[1])
binary.big_endian_put_u64(mut digest[16..], d.h[2])
@ -254,7 +256,7 @@ pub fn sum384(data []byte) []byte {
mut d := new_digest(.sha384)
d.write(data)
sum := d.checksum()
sum384 := []byte{len:(size384)}
sum384 := []byte{len: (size384)}
copy(sum384, sum[..size384])
return sum384
}
@ -264,7 +266,7 @@ pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224)
d.write(data)
sum := d.checksum()
sum224 := []byte{len:(size224)}
sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224])
return sum224
}
@ -274,7 +276,7 @@ pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256)
d.write(data)
sum := d.checksum()
sum256 := []byte{len:(size256)}
sum256 := []byte{len: (size256)}
copy(sum256, sum[..size256])
return sum256
}
@ -287,18 +289,11 @@ fn block(mut dig Digest, p []byte) {
pub fn (d &Digest) size() int {
match d.function {
.sha512_224 {
return size224
}
.sha512_256 {
return size256
}
.sha384 {
return size384
}
else {
return size
}}
.sha512_224 { return size224 }
.sha512_256 { return size256 }
.sha384 { return size384 }
else { return size }
}
}
pub fn (d &Digest) block_size() int {