all: minor array fixes
parent
3c210a57f9
commit
50a6976b5e
|
@ -50,8 +50,8 @@ fn main() {
|
||||||
init_settings()
|
init_settings()
|
||||||
// This tool is intended to be launched by the v frontend,
|
// This tool is intended to be launched by the v frontend,
|
||||||
// which provides the path to V inside os.getenv('VEXE')
|
// which provides the path to V inside os.getenv('VEXE')
|
||||||
args := os.args // args are: vpm [options] SUBCOMMAND module names
|
// args are: vpm [options] SUBCOMMAND module names
|
||||||
params := cmdline.only_non_options(args[1..])
|
params := cmdline.only_non_options(os.args[1..])
|
||||||
verbose_println('cli params: $params')
|
verbose_println('cli params: $params')
|
||||||
if params.len < 1 {
|
if params.len < 1 {
|
||||||
vpm_help()
|
vpm_help()
|
||||||
|
@ -217,7 +217,7 @@ fn vpm_install(module_names []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vpm_update(m []string) {
|
fn vpm_update(m []string) {
|
||||||
mut module_names := m
|
mut module_names := m.clone()
|
||||||
if settings.is_help {
|
if settings.is_help {
|
||||||
vhelp.show_topic('update')
|
vhelp.show_topic('update')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
|
@ -26,8 +26,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
args := os.args
|
args_string := os.args[1..].join(' ')
|
||||||
args_string := args[1..].join(' ')
|
|
||||||
v_test_formatting(args_string.all_before('test-fmt'))
|
v_test_formatting(args_string.all_before('test-fmt'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,14 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn test_sorting_simple() {
|
fn test_sorting_simple() {
|
||||||
mut a := unsorted
|
mut a := unsorted.clone()
|
||||||
a.sort()
|
a.sort()
|
||||||
eprintln(' a: $a')
|
eprintln(' a: $a')
|
||||||
assert a == sorted_asc
|
assert a == sorted_asc
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_sorting_with_condition_expression() {
|
fn test_sorting_with_condition_expression() {
|
||||||
mut a := unsorted
|
mut a := unsorted.clone()
|
||||||
a.sort(a > b)
|
a.sort(a > b)
|
||||||
eprintln(' a: $a')
|
eprintln(' a: $a')
|
||||||
assert a == sorted_desc
|
assert a == sorted_desc
|
||||||
|
@ -23,7 +23,7 @@ fn mysort(mut a []int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_sorting_by_passing_a_mut_array_to_a_function() {
|
fn test_sorting_by_passing_a_mut_array_to_a_function() {
|
||||||
mut a := unsorted
|
mut a := unsorted.clone()
|
||||||
mysort(mut a)
|
mysort(mut a)
|
||||||
eprintln(' a: $a')
|
eprintln(' a: $a')
|
||||||
assert a == sorted_asc
|
assert a == sorted_asc
|
||||||
|
|
|
@ -44,6 +44,7 @@ pub fn (x &AesCbc) block_size() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
||||||
|
unsafe {
|
||||||
mut dst := *dst_
|
mut dst := *dst_
|
||||||
mut src := src_
|
mut src := src_
|
||||||
if src.len % x.block_size != 0 {
|
if src.len % x.block_size != 0 {
|
||||||
|
@ -72,6 +73,7 @@ pub fn (x &AesCbc) encrypt_blocks(mut dst_ []byte, src_ []byte) {
|
||||||
// Save the iv for the next crypt_blocks call.
|
// Save the iv for the next crypt_blocks call.
|
||||||
copy(x.iv, iv)
|
copy(x.iv, iv)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) {
|
pub fn (mut x AesCbc) decrypt_blocks(mut dst []byte, src []byte) {
|
||||||
if src.len % x.block_size != 0 {
|
if src.len % x.block_size != 0 {
|
||||||
|
|
|
@ -24,4 +24,5 @@ fn test_crypto_aes() {
|
||||||
mode.encrypt_blocks(mut ciphertext, cipher_clone)
|
mode.encrypt_blocks(mut ciphertext, cipher_clone)
|
||||||
assert ciphertext.hex() ==
|
assert ciphertext.hex() ==
|
||||||
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
|
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
|
||||||
|
println('ok')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
// 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 md5 implements the MD5 hash algorithm as defined in RFC 1321.
|
// Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
|
||||||
|
|
||||||
// MD5 is cryptographically broken and should not be used for secure
|
// MD5 is cryptographically broken and should not be used for secure
|
||||||
// applications.
|
// applications.
|
||||||
|
|
||||||
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
|
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
|
||||||
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
|
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
|
||||||
|
|
||||||
module md5
|
module md5
|
||||||
|
|
||||||
import encoding.binary
|
import encoding.binary
|
||||||
|
@ -56,6 +52,7 @@ pub fn new() &Digest {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut d Digest) write(p_ []byte) int {
|
pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
|
unsafe {
|
||||||
mut p := p_
|
mut p := p_
|
||||||
nn := p.len
|
nn := p.len
|
||||||
d.len += u64(nn)
|
d.len += u64(nn)
|
||||||
|
@ -86,6 +83,7 @@ pub fn (mut d Digest) write(p_ []byte) int {
|
||||||
}
|
}
|
||||||
return nn
|
return nn
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (d &Digest) sum(b_in []byte) []byte {
|
pub fn (d &Digest) sum(b_in []byte) []byte {
|
||||||
// Make a copy of d so that caller can keep writing and summing.
|
// Make a copy of d so that caller can keep writing and summing.
|
||||||
|
@ -110,15 +108,12 @@ pub fn (mut d Digest) checksum() []byte {
|
||||||
pad := ((55 - d.len) % 64) // calculate number of padding bytes
|
pad := ((55 - d.len) % 64) // calculate number of padding bytes
|
||||||
binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
|
binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
|
||||||
d.write(tmp[..1 + pad + 8])
|
d.write(tmp[..1 + pad + 8])
|
||||||
|
|
||||||
// The previous write ensures that a whole number of
|
// The previous write ensures that a whole number of
|
||||||
// blocks (i.e. a multiple of 64 bytes) have been hashed.
|
// blocks (i.e. a multiple of 64 bytes) have been hashed.
|
||||||
if d.nx != 0 {
|
if d.nx != 0 {
|
||||||
panic('d.nx != 0')
|
panic('d.nx != 0')
|
||||||
}
|
}
|
||||||
|
|
||||||
mut digest := []byte{len: (size)}
|
mut digest := []byte{len: (size)}
|
||||||
|
|
||||||
binary.little_endian_put_u32(mut digest, d.s[0])
|
binary.little_endian_put_u32(mut digest, d.s[0])
|
||||||
binary.little_endian_put_u32(mut digest[4..], d.s[1])
|
binary.little_endian_put_u32(mut digest[4..], d.s[1])
|
||||||
binary.little_endian_put_u32(mut digest[8..], d.s[2])
|
binary.little_endian_put_u32(mut digest[8..], d.s[2])
|
||||||
|
@ -139,8 +134,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,13 +1,10 @@
|
||||||
// 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 sha256 implements the SHA224 and SHA256 hash algorithms as defined
|
// Package sha256 implements the SHA224 and SHA256 hash algorithms as defined
|
||||||
// in FIPS 180-4.
|
// in FIPS 180-4.
|
||||||
|
|
||||||
// Based off: https://github.com/golang/go/tree/master/src/crypto/sha256
|
// Based off: https://github.com/golang/go/tree/master/src/crypto/sha256
|
||||||
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
// Last commit: https://github.com/golang/go/commit/3ce865d7a0b88714cc433454ae2370a105210c01
|
||||||
|
|
||||||
module sha256
|
module sha256
|
||||||
|
|
||||||
import encoding.binary
|
import encoding.binary
|
||||||
|
@ -93,6 +90,7 @@ pub fn new224() &Digest {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut d Digest) write(p_ []byte) int {
|
fn (mut d Digest) write(p_ []byte) int {
|
||||||
|
unsafe {
|
||||||
mut p := p_
|
mut p := p_
|
||||||
nn := p.len
|
nn := p.len
|
||||||
d.len += u64(nn)
|
d.len += u64(nn)
|
||||||
|
@ -123,6 +121,7 @@ fn (mut d Digest) write(p_ []byte) int {
|
||||||
}
|
}
|
||||||
return nn
|
return nn
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn (d &Digest) sum(b_in []byte) []byte {
|
fn (d &Digest) sum(b_in []byte) []byte {
|
||||||
// Make a copy of d so that caller can keep writing and summing.
|
// Make a copy of d so that caller can keep writing and summing.
|
||||||
|
@ -151,18 +150,14 @@ fn (mut d Digest) checksum() []byte {
|
||||||
} 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 <<= u64(3)
|
len <<= u64(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])
|
||||||
|
|
||||||
if d.nx != 0 {
|
if d.nx != 0 {
|
||||||
panic('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, 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])
|
||||||
|
@ -173,7 +168,6 @@ fn (mut d Digest) checksum() []byte {
|
||||||
if !d.is224 {
|
if !d.is224 {
|
||||||
binary.big_endian_put_u32(mut digest[28..], d.h[7])
|
binary.big_endian_put_u32(mut digest[28..], d.h[7])
|
||||||
}
|
}
|
||||||
|
|
||||||
return digest
|
return digest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,7 +206,14 @@ pub fn (d &Digest) size() int {
|
||||||
return size224
|
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(s string) string {
|
||||||
pub fn hexhash_224(s string) string { return sum224(s.bytes()).hex() }
|
return sum256(s.bytes()).hex()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hexhash_224(s string) string {
|
||||||
|
return sum224(s.bytes()).hex()
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
// 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.
|
||||||
|
|
||||||
// SHA256 block step.
|
// SHA256 block step.
|
||||||
// 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 sha256
|
module sha256
|
||||||
|
|
||||||
import math.bits
|
import math.bits
|
||||||
|
@ -81,10 +79,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: (64)}
|
mut w := []u32{len: (64)}
|
||||||
|
|
||||||
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]
|
||||||
|
@ -93,7 +90,6 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
mut h5 := dig.h[5]
|
mut h5 := dig.h[5]
|
||||||
mut h6 := dig.h[6]
|
mut h6 := dig.h[6]
|
||||||
mut h7 := dig.h[7]
|
mut h7 := dig.h[7]
|
||||||
|
|
||||||
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.
|
||||||
|
@ -108,7 +104,6 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3)
|
t2 := (bits.rotate_left_32(v2, -7)) ^ (bits.rotate_left_32(v2, -18)) ^ (v2 >> 3)
|
||||||
w[i] = t1 + w[i - 7] + t2 + w[i - 16]
|
w[i] = t1 + w[i - 7] + t2 + w[i - 16]
|
||||||
}
|
}
|
||||||
|
|
||||||
mut a := h0
|
mut a := h0
|
||||||
mut b := h1
|
mut b := h1
|
||||||
mut c := h2
|
mut c := h2
|
||||||
|
@ -117,11 +112,13 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
mut f := h5
|
mut f := h5
|
||||||
mut g := h6
|
mut g := h6
|
||||||
mut h := h7
|
mut h := h7
|
||||||
|
|
||||||
for i in 0 .. 64 {
|
for i in 0 .. 64 {
|
||||||
t1 := h + ((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) + ((e & f) ^ (~e & g)) + u32(_k[i]) + w[i]
|
t1 := h +
|
||||||
t2 := ((bits.rotate_left_32(a, -2)) ^ (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) + ((a & b) ^ (a & c) ^ (b & c))
|
((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) +
|
||||||
|
((e & f) ^ (~e & g)) + u32(_k[i]) + w[i]
|
||||||
|
t2 := ((bits.rotate_left_32(a, -2)) ^
|
||||||
|
(bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) +
|
||||||
|
((a & b) ^ (a & c) ^ (b & c))
|
||||||
h = g
|
h = g
|
||||||
g = f
|
g = f
|
||||||
f = e
|
f = e
|
||||||
|
@ -131,7 +128,6 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
b = a
|
b = a
|
||||||
a = t1 + t2
|
a = t1 + t2
|
||||||
}
|
}
|
||||||
|
|
||||||
h0 += a
|
h0 += a
|
||||||
h1 += b
|
h1 += b
|
||||||
h2 += c
|
h2 += c
|
||||||
|
@ -140,14 +136,12 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
h5 += f
|
h5 += f
|
||||||
h6 += g
|
h6 += g
|
||||||
h7 += h
|
h7 += h
|
||||||
|
|
||||||
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
|
||||||
|
@ -157,3 +151,4 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
dig.h[6] = h6
|
dig.h[6] = h6
|
||||||
dig.h[7] = h7
|
dig.h[7] = h7
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -59,6 +59,7 @@ const (
|
||||||
init6_384 = u64(0xdb0c2e0d64f98fa7)
|
init6_384 = u64(0xdb0c2e0d64f98fa7)
|
||||||
init7_384 = u64(0x47b5481dbefa4fa4)
|
init7_384 = u64(0x47b5481dbefa4fa4)
|
||||||
)
|
)
|
||||||
|
|
||||||
// digest represents the partial evaluation of a checksum.
|
// digest represents the partial evaluation of a checksum.
|
||||||
struct Digest {
|
struct Digest {
|
||||||
mut:
|
mut:
|
||||||
|
@ -112,7 +113,8 @@ fn (mut d Digest) reset() {
|
||||||
d.h[5] = init5
|
d.h[5] = init5
|
||||||
d.h[6] = init6
|
d.h[6] = init6
|
||||||
d.h[7] = init7
|
d.h[7] = init7
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
d.nx = 0
|
d.nx = 0
|
||||||
d.len = 0
|
d.len = 0
|
||||||
}
|
}
|
||||||
|
@ -147,6 +149,7 @@ fn new384() &Digest {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut d Digest) write(p_ []byte) int {
|
fn (mut d Digest) write(p_ []byte) int {
|
||||||
|
unsafe {
|
||||||
mut p := p_
|
mut p := p_
|
||||||
nn := p.len
|
nn := p.len
|
||||||
d.len += u64(nn)
|
d.len += u64(nn)
|
||||||
|
@ -159,8 +162,7 @@ fn (mut d Digest) write(p_ []byte) int {
|
||||||
}
|
}
|
||||||
if n >= p.len {
|
if n >= p.len {
|
||||||
p = []
|
p = []
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
p = p[n..]
|
p = p[n..]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,8 +171,7 @@ fn (mut d Digest) write(p_ []byte) int {
|
||||||
block(mut d, p[..n])
|
block(mut d, p[..n])
|
||||||
if n >= p.len {
|
if n >= p.len {
|
||||||
p = []
|
p = []
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
p = p[n..]
|
p = p[n..]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,6 +180,7 @@ fn (mut d Digest) write(p_ []byte) int {
|
||||||
}
|
}
|
||||||
return nn
|
return nn
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn (d &Digest) sum(b_in []byte) []byte {
|
fn (d &Digest) sum(b_in []byte) []byte {
|
||||||
// Make a copy of d so that caller can keep writing and summing.
|
// Make a copy of d so that caller can keep writing and summing.
|
||||||
|
@ -205,7 +207,8 @@ fn (d &Digest) sum(b_in []byte) []byte {
|
||||||
for b in hash {
|
for b in hash {
|
||||||
b_out << b
|
b_out << b
|
||||||
}
|
}
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
return b_out
|
return b_out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,8 +219,7 @@ fn (mut d Digest) checksum() []byte {
|
||||||
tmp[0] = 0x80
|
tmp[0] = 0x80
|
||||||
if int(len) % 128 < 112 {
|
if int(len) % 128 < 112 {
|
||||||
d.write(tmp[..112 - int(len) % 128])
|
d.write(tmp[..112 - int(len) % 128])
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
d.write(tmp[..128 + 112 - int(len) % 128])
|
d.write(tmp[..128 + 112 - int(len) % 128])
|
||||||
}
|
}
|
||||||
// Length in bits.
|
// Length in bits.
|
||||||
|
@ -287,18 +289,11 @@ fn block(mut dig Digest, p []byte) {
|
||||||
|
|
||||||
pub fn (d &Digest) size() int {
|
pub fn (d &Digest) size() int {
|
||||||
match d.function {
|
match d.function {
|
||||||
.sha512_224 {
|
.sha512_224 { return size224 }
|
||||||
return size224
|
.sha512_256 { return size256 }
|
||||||
|
.sha384 { return size384 }
|
||||||
|
else { return size }
|
||||||
}
|
}
|
||||||
.sha512_256 {
|
|
||||||
return size256
|
|
||||||
}
|
|
||||||
.sha384 {
|
|
||||||
return size384
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return size
|
|
||||||
}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (d &Digest) block_size() int {
|
pub fn (d &Digest) block_size() int {
|
||||||
|
|
|
@ -10,90 +10,30 @@ module sha512
|
||||||
import math.bits
|
import math.bits
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_k = [u64(0x428a2f98d728ae22),
|
_k = [u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
|
||||||
u64(0x7137449123ef65cd),
|
u64(0x3956c25bf348b538), u64(0x59f111f1b605d019), u64(0x923f82a4af194f9b), u64(0xab1c5ed5da6d8118),
|
||||||
u64(0xb5c0fbcfec4d3b2f),
|
u64(0xd807aa98a3030242), u64(0x12835b0145706fbe), u64(0x243185be4ee4b28c), u64(0x550c7dc3d5ffb4e2),
|
||||||
u64(0xe9b5dba58189dbbc),
|
u64(0x72be5d74f27b896f), u64(0x80deb1fe3b1696b1), u64(0x9bdc06a725c71235), u64(0xc19bf174cf692694),
|
||||||
u64(0x3956c25bf348b538),
|
u64(0xe49b69c19ef14ad2), u64(0xefbe4786384f25e3), u64(0x0fc19dc68b8cd5b5), u64(0x240ca1cc77ac9c65),
|
||||||
u64(0x59f111f1b605d019),
|
u64(0x2de92c6f592b0275), u64(0x4a7484aa6ea6e483), u64(0x5cb0a9dcbd41fbd4), u64(0x76f988da831153b5),
|
||||||
u64(0x923f82a4af194f9b),
|
u64(0x983e5152ee66dfab), u64(0xa831c66d2db43210), u64(0xb00327c898fb213f), u64(0xbf597fc7beef0ee4),
|
||||||
u64(0xab1c5ed5da6d8118),
|
u64(0xc6e00bf33da88fc2), u64(0xd5a79147930aa725), u64(0x06ca6351e003826f), u64(0x142929670a0e6e70),
|
||||||
u64(0xd807aa98a3030242),
|
u64(0x27b70a8546d22ffc), u64(0x2e1b21385c26c926), u64(0x4d2c6dfc5ac42aed), u64(0x53380d139d95b3df),
|
||||||
u64(0x12835b0145706fbe),
|
u64(0x650a73548baf63de), u64(0x766a0abb3c77b2a8), u64(0x81c2c92e47edaee6), u64(0x92722c851482353b),
|
||||||
u64(0x243185be4ee4b28c),
|
u64(0xa2bfe8a14cf10364), u64(0xa81a664bbc423001), u64(0xc24b8b70d0f89791), u64(0xc76c51a30654be30),
|
||||||
u64(0x550c7dc3d5ffb4e2),
|
u64(0xd192e819d6ef5218), u64(0xd69906245565a910), u64(0xf40e35855771202a), u64(0x106aa07032bbd1b8),
|
||||||
u64(0x72be5d74f27b896f),
|
u64(0x19a4c116b8d2d0c8), u64(0x1e376c085141ab53), u64(0x2748774cdf8eeb99), u64(0x34b0bcb5e19b48a8),
|
||||||
u64(0x80deb1fe3b1696b1),
|
u64(0x391c0cb3c5c95a63), u64(0x4ed8aa4ae3418acb), u64(0x5b9cca4f7763e373), u64(0x682e6ff3d6b2b8a3),
|
||||||
u64(0x9bdc06a725c71235),
|
u64(0x748f82ee5defb2fc), u64(0x78a5636f43172f60), u64(0x84c87814a1f0ab72), u64(0x8cc702081a6439ec),
|
||||||
u64(0xc19bf174cf692694),
|
u64(0x90befffa23631e28), u64(0xa4506cebde82bde9), u64(0xbef9a3f7b2c67915), u64(0xc67178f2e372532b),
|
||||||
u64(0xe49b69c19ef14ad2),
|
u64(0xca273eceea26619c), u64(0xd186b8c721c0c207), u64(0xeada7dd6cde0eb1e), u64(0xf57d4f7fee6ed178),
|
||||||
u64(0xefbe4786384f25e3),
|
u64(0x06f067aa72176fba), u64(0x0a637dc5a2c898a6), u64(0x113f9804bef90dae), u64(0x1b710b35131c471b),
|
||||||
u64(0x0fc19dc68b8cd5b5),
|
u64(0x28db77f523047d84), u64(0x32caab7b40c72493), u64(0x3c9ebe0a15c9bebc), u64(0x431d67c49c100d4c),
|
||||||
u64(0x240ca1cc77ac9c65),
|
u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817)]
|
||||||
u64(0x2de92c6f592b0275),
|
|
||||||
u64(0x4a7484aa6ea6e483),
|
|
||||||
u64(0x5cb0a9dcbd41fbd4),
|
|
||||||
u64(0x76f988da831153b5),
|
|
||||||
u64(0x983e5152ee66dfab),
|
|
||||||
u64(0xa831c66d2db43210),
|
|
||||||
u64(0xb00327c898fb213f),
|
|
||||||
u64(0xbf597fc7beef0ee4),
|
|
||||||
u64(0xc6e00bf33da88fc2),
|
|
||||||
u64(0xd5a79147930aa725),
|
|
||||||
u64(0x06ca6351e003826f),
|
|
||||||
u64(0x142929670a0e6e70),
|
|
||||||
u64(0x27b70a8546d22ffc),
|
|
||||||
u64(0x2e1b21385c26c926),
|
|
||||||
u64(0x4d2c6dfc5ac42aed),
|
|
||||||
u64(0x53380d139d95b3df),
|
|
||||||
u64(0x650a73548baf63de),
|
|
||||||
u64(0x766a0abb3c77b2a8),
|
|
||||||
u64(0x81c2c92e47edaee6),
|
|
||||||
u64(0x92722c851482353b),
|
|
||||||
u64(0xa2bfe8a14cf10364),
|
|
||||||
u64(0xa81a664bbc423001),
|
|
||||||
u64(0xc24b8b70d0f89791),
|
|
||||||
u64(0xc76c51a30654be30),
|
|
||||||
u64(0xd192e819d6ef5218),
|
|
||||||
u64(0xd69906245565a910),
|
|
||||||
u64(0xf40e35855771202a),
|
|
||||||
u64(0x106aa07032bbd1b8),
|
|
||||||
u64(0x19a4c116b8d2d0c8),
|
|
||||||
u64(0x1e376c085141ab53),
|
|
||||||
u64(0x2748774cdf8eeb99),
|
|
||||||
u64(0x34b0bcb5e19b48a8),
|
|
||||||
u64(0x391c0cb3c5c95a63),
|
|
||||||
u64(0x4ed8aa4ae3418acb),
|
|
||||||
u64(0x5b9cca4f7763e373),
|
|
||||||
u64(0x682e6ff3d6b2b8a3),
|
|
||||||
u64(0x748f82ee5defb2fc),
|
|
||||||
u64(0x78a5636f43172f60),
|
|
||||||
u64(0x84c87814a1f0ab72),
|
|
||||||
u64(0x8cc702081a6439ec),
|
|
||||||
u64(0x90befffa23631e28),
|
|
||||||
u64(0xa4506cebde82bde9),
|
|
||||||
u64(0xbef9a3f7b2c67915),
|
|
||||||
u64(0xc67178f2e372532b),
|
|
||||||
u64(0xca273eceea26619c),
|
|
||||||
u64(0xd186b8c721c0c207),
|
|
||||||
u64(0xeada7dd6cde0eb1e),
|
|
||||||
u64(0xf57d4f7fee6ed178),
|
|
||||||
u64(0x06f067aa72176fba),
|
|
||||||
u64(0x0a637dc5a2c898a6),
|
|
||||||
u64(0x113f9804bef90dae),
|
|
||||||
u64(0x1b710b35131c471b),
|
|
||||||
u64(0x28db77f523047d84),
|
|
||||||
u64(0x32caab7b40c72493),
|
|
||||||
u64(0x3c9ebe0a15c9bebc),
|
|
||||||
u64(0x431d67c49c100d4c),
|
|
||||||
u64(0x4cc5d4becb3e42b6),
|
|
||||||
u64(0x597f299cfc657e2a),
|
|
||||||
u64(0x5fcb6fab3ad6faec),
|
|
||||||
u64(0x6c44198c4a475817),
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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 := []u64{len: (80)}
|
mut w := []u64{len: (80)}
|
||||||
mut h0 := dig.h[0]
|
mut h0 := dig.h[0]
|
||||||
|
@ -107,7 +47,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
for p.len >= chunk {
|
for p.len >= chunk {
|
||||||
for i in 0 .. 16 {
|
for i in 0 .. 16 {
|
||||||
j := i * 8
|
j := i * 8
|
||||||
w[i] = (u64(p[j])<<56) | (u64(p[j + 1])<<48) | (u64(p[j + 2])<<40) | (u64(p[j + 3])<<32) | (u64(p[j + 4])<<24) | (u64(p[j + 5])<<16) | (u64(p[j + 6])<<8) | u64(p[j + 7])
|
w[i] = (u64(p[j]) << 56) |
|
||||||
|
(u64(p[j + 1]) << 48) | (u64(p[j + 2]) << 40) |
|
||||||
|
(u64(p[j + 3]) << 32) | (u64(p[j + 4]) << 24) |
|
||||||
|
(u64(p[j + 5]) << 16) | (u64(p[j + 6]) << 8) | u64(p[j + 7])
|
||||||
}
|
}
|
||||||
for i := 16; i < 80; i++ {
|
for i := 16; i < 80; i++ {
|
||||||
v1 := w[i - 2]
|
v1 := w[i - 2]
|
||||||
|
@ -125,8 +68,11 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
mut g := h6
|
mut g := h6
|
||||||
mut h := h7
|
mut h := h7
|
||||||
for i in 0 .. 80 {
|
for i in 0 .. 80 {
|
||||||
t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i]
|
t1 := h +
|
||||||
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c))
|
(bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) +
|
||||||
|
((e & f) ^ (~e & g)) + _k[i] + w[i]
|
||||||
|
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) +
|
||||||
|
((a & b) ^ (a & c) ^ (b & c))
|
||||||
h = g
|
h = g
|
||||||
g = f
|
g = f
|
||||||
f = e
|
f = e
|
||||||
|
@ -146,8 +92,7 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
h7 += h
|
h7 += h
|
||||||
if chunk >= p.len {
|
if chunk >= p.len {
|
||||||
p = []
|
p = []
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
p = p[chunk..]
|
p = p[chunk..]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,3 +105,4 @@ fn block_generic(mut dig Digest, p_ []byte) {
|
||||||
dig.h[6] = h6
|
dig.h[6] = h6
|
||||||
dig.h[7] = h7
|
dig.h[7] = h7
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2209,11 +2209,11 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
||||||
// TODO replace all c.pref.translated checks with `$if !translated` for performance
|
// TODO replace all c.pref.translated checks with `$if !translated` for performance
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if left_sym.kind == .array &&
|
if left_sym.kind == .array && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign] &&
|
||||||
assign_stmt.op in [.assign, .decl_assign] && right_sym.kind == .array && left is ast.Ident &&
|
right_sym.kind == .array && left is ast.Ident && right is ast.Ident {
|
||||||
right is ast.Ident {
|
|
||||||
// Do not allow `a = b`, only `a = b.clone()`
|
// Do not allow `a = b`, only `a = b.clone()`
|
||||||
c.error('use `array2 = array1.clone()` instead of `array2 = array1`', assign_stmt.pos)
|
c.error('use `array2 = array1.clone()` instead of `array2 = array1` (or use `unsafe`)',
|
||||||
|
assign_stmt.pos)
|
||||||
}
|
}
|
||||||
left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
|
left_is_ptr := left_type.is_ptr() || left_sym.is_pointer()
|
||||||
if left_is_ptr {
|
if left_is_ptr {
|
||||||
|
|
|
@ -9,20 +9,20 @@ import crypto.sha512
|
||||||
import cli { Command }
|
import cli { Command }
|
||||||
|
|
||||||
struct TestAliasInStruct {
|
struct TestAliasInStruct {
|
||||||
time t.Time
|
time Time
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_import() {
|
fn test_import() {
|
||||||
info := l.Level.info
|
info := l.Level.info
|
||||||
assert info == .info
|
assert info == .info
|
||||||
assert term.white('INFO') == white('INFO')
|
assert white('INFO') == white('INFO')
|
||||||
assert os.o_rdonly == os.o_rdonly
|
assert os.o_rdonly == os.o_rdonly
|
||||||
assert t.month_days[0] == t.month_days[0]
|
assert t.month_days[0] == t.month_days[0]
|
||||||
assert sha256.size == sha256.size
|
assert sha256.size == sha256.size
|
||||||
assert math.pi == math.pi
|
assert math.pi == math.pi
|
||||||
assert sha512.size == sha512.size
|
assert sha512.size == sha512.size
|
||||||
assert md5.sum('module'.bytes()).hex() == sum('module'.bytes()).hex()
|
assert sum('module'.bytes()).hex() == sum('module'.bytes()).hex()
|
||||||
assert t.utc().unix_time() == utc().unix_time()
|
assert utc().unix_time() == utc().unix_time()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_imports_array_as_fn_arg() {
|
fn test_imports_array_as_fn_arg() {
|
||||||
|
@ -38,7 +38,7 @@ fn test_imports_array_as_fn_arg() {
|
||||||
|
|
||||||
fn test_alias_in_struct_field() {
|
fn test_alias_in_struct_field() {
|
||||||
a := TestAliasInStruct{
|
a := TestAliasInStruct{
|
||||||
time: t.Time{
|
time: Time{
|
||||||
year: 2020
|
year: 2020
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue