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

@ -1,22 +1,19 @@
// 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 sha256 implements the SHA224 and SHA256 hash algorithms as defined
// in FIPS 180-4.
// Based off: https://github.com/golang/go/tree/master/src/crypto/sha256
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
module sha256
import encoding.binary
pub const (
// The size of a SHA256 checksum in bytes.
size = 32
size = 32
// The size of a SHA224 checksum in bytes.
size224 = 28
size224 = 28
// The blocksize of SHA256 and SHA224 in bytes.
block_size = 64
)
@ -52,8 +49,8 @@ mut:
}
fn (mut d Digest) reset() {
d.h = []u32{len:(8)}
d.x = []byte{len:(chunk)}
d.h = []u32{len: (8)}
d.x = []byte{len: (chunk)}
if !d.is224 {
d.h[0] = u32(init0)
d.h[1] = u32(init1)
@ -93,35 +90,37 @@ pub fn new224() &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 = []
} 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)
}
return nn
}
if p.len > 0 {
d.nx = copy(d.x, p)
}
return nn
}
fn (d &Digest) sum(b_in []byte) []byte {
@ -144,25 +143,21 @@ 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 <<= u64(3)
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp[..8])
if d.nx != 0 {
panic('d.nx != 0')
}
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])
@ -173,7 +168,6 @@ fn (mut d Digest) checksum() []byte {
if !d.is224 {
binary.big_endian_put_u32(mut digest[28..], d.h[7])
}
return digest
}
@ -194,7 +188,7 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data)
sum := d.checksum()
sum224 := []byte{len:(size224)}
sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224])
return sum224
}
@ -212,7 +206,14 @@ pub fn (d &Digest) size() int {
return size224
}
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 sum256(s.bytes()).hex() }
pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() }
pub fn hexhash(s string) string {
return sum256(s.bytes()).hex()
}
pub fn hexhash_224(s string) string {
return sum224(s.bytes()).hex()
}