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

tools: make v test-cleancode test everything by default (#10050)

This commit is contained in:
Delyan Angelov
2021-05-08 13:32:29 +03:00
committed by GitHub
parent cba2cb6b9c
commit 8a380f4699
132 changed files with 3230 additions and 3440 deletions

View File

@@ -40,20 +40,20 @@ pub fn new_cipher(key []byte) AesCipher {
// block_size returns the block size of the checksum in bytes.
pub fn (c &AesCipher) block_size() int {
return block_size
return aes.block_size
}
// encrypt encrypts the blocks in `src` to `dst`.
// Please note: `dst` and `src` are both mutable for performance reasons.
pub fn (c &AesCipher) encrypt(mut dst []byte, mut src []byte) {
if src.len < block_size {
if src.len < aes.block_size {
panic('crypto.aes: input not full block')
}
if dst.len < block_size {
if dst.len < aes.block_size {
panic('crypto.aes: output not full block')
}
// if subtle.inexact_overlap(dst[:block_size], src[:block_size]) {
if subtle.inexact_overlap((*dst)[..block_size], (*src)[..block_size]) {
if subtle.inexact_overlap((*dst)[..aes.block_size], (*src)[..aes.block_size]) {
panic('crypto.aes: invalid buffer overlap')
}
// for now use generic version
@@ -63,13 +63,13 @@ pub fn (c &AesCipher) encrypt(mut dst []byte, mut src []byte) {
// decrypt decrypts the blocks in `src` to `dst`.
// Please note: `dst` and `src` are both mutable for performance reasons.
pub fn (c &AesCipher) decrypt(mut dst []byte, mut src []byte) {
if src.len < block_size {
if src.len < aes.block_size {
panic('crypto.aes: input not full block')
}
if dst.len < block_size {
if dst.len < aes.block_size {
panic('crypto.aes: output not full block')
}
if subtle.inexact_overlap((*dst)[..block_size], (*src)[..block_size]) {
if subtle.inexact_overlap((*dst)[..aes.block_size], (*src)[..aes.block_size]) {
panic('crypto.aes: invalid buffer overlap')
}
// for now use generic version

View File

@@ -22,7 +22,6 @@ fn test_crypto_aes() {
mode := aes.new_cbc(block, iv)
cipher_clone := ciphertext.clone()
mode.encrypt_blocks(mut ciphertext, cipher_clone)
assert ciphertext.hex() ==
'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
assert ciphertext.hex() == 'c210459b514668ddc44674885e4979215265a6c44431a248421254ef357a8c2a308a8bddf5623af9df91737562041cf1'
println('ok')
}

View File

@@ -69,14 +69,10 @@ fn encrypt_block_generic(xk []u32, mut dst []byte, src []byte) {
s3 = t3
}
// Last round uses s-box directly and XORs to produce output.
s0 = s_box0[t0 >> 24] << 24 |
s_box0[t1 >> 16 & 0xff] << 16 | u32(s_box0[t2 >> 8 & 0xff] << 8) | s_box0[t3 & u32(0xff)]
s1 = s_box0[t1 >> 24] << 24 |
s_box0[t2 >> 16 & 0xff] << 16 | u32(s_box0[t3 >> 8 & 0xff] << 8) | s_box0[t0 & u32(0xff)]
s2 = s_box0[t2 >> 24] << 24 |
s_box0[t3 >> 16 & 0xff] << 16 | u32(s_box0[t0 >> 8 & 0xff] << 8) | s_box0[t1 & u32(0xff)]
s3 = s_box0[t3 >> 24] << 24 |
s_box0[t0 >> 16 & 0xff] << 16 | u32(s_box0[t1 >> 8 & 0xff] << 8) | s_box0[t2 & u32(0xff)]
s0 = s_box0[t0 >> 24] << 24 | s_box0[t1 >> 16 & 0xff] << 16 | u32(s_box0[t2 >> 8 & 0xff] << 8) | s_box0[t3 & u32(0xff)]
s1 = s_box0[t1 >> 24] << 24 | s_box0[t2 >> 16 & 0xff] << 16 | u32(s_box0[t3 >> 8 & 0xff] << 8) | s_box0[t0 & u32(0xff)]
s2 = s_box0[t2 >> 24] << 24 | s_box0[t3 >> 16 & 0xff] << 16 | u32(s_box0[t0 >> 8 & 0xff] << 8) | s_box0[t1 & u32(0xff)]
s3 = s_box0[t3 >> 24] << 24 | s_box0[t0 >> 16 & 0xff] << 16 | u32(s_box0[t1 >> 8 & 0xff] << 8) | s_box0[t2 & u32(0xff)]
s0 ^= xk[k + 0]
s1 ^= xk[k + 1]
s2 ^= xk[k + 2]
@@ -120,14 +116,10 @@ fn decrypt_block_generic(xk []u32, mut dst []byte, src []byte) {
s3 = t3
}
// Last round uses s-box directly and XORs to produce output.
s0 = u32(s_box1[t0 >> 24]) << 24 |
u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff] << 8) | u32(s_box1[t1 & u32(0xff)])
s1 = u32(s_box1[t1 >> 24]) << 24 |
u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff] << 8) | u32(s_box1[t2 & u32(0xff)])
s2 = u32(s_box1[t2 >> 24]) << 24 |
u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff] << 8) | u32(s_box1[t3 & u32(0xff)])
s3 = u32(s_box1[t3 >> 24]) << 24 |
u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff] << 8) | u32(s_box1[t0 & u32(0xff)])
s0 = u32(s_box1[t0 >> 24]) << 24 | u32(s_box1[t3 >> 16 & 0xff]) << 16 | u32(s_box1[t2 >> 8 & 0xff] << 8) | u32(s_box1[t1 & u32(0xff)])
s1 = u32(s_box1[t1 >> 24]) << 24 | u32(s_box1[t0 >> 16 & 0xff]) << 16 | u32(s_box1[t3 >> 8 & 0xff] << 8) | u32(s_box1[t2 & u32(0xff)])
s2 = u32(s_box1[t2 >> 24]) << 24 | u32(s_box1[t1 >> 16 & 0xff]) << 16 | u32(s_box1[t0 >> 8 & 0xff] << 8) | u32(s_box1[t3 & u32(0xff)])
s3 = u32(s_box1[t3 >> 24]) << 24 | u32(s_box1[t2 >> 16 & 0xff]) << 16 | u32(s_box1[t1 >> 8 & 0xff] << 8) | u32(s_box1[t0 & u32(0xff)])
s0 ^= xk[k + 0]
s1 ^= xk[k + 1]
s2 ^= xk[k + 2]
@@ -141,8 +133,7 @@ fn decrypt_block_generic(xk []u32, mut dst []byte, src []byte) {
// Apply s_box0 to each byte in w.
fn subw(w u32) u32 {
return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff] << 16) | u32(s_box0[w >> 8 &
0xff] << 8) | u32(s_box0[w & u32(0xff)])
return u32(s_box0[w >> 24]) << 24 | u32(s_box0[w >> 16 & 0xff] << 16) | u32(s_box0[w >> 8 & 0xff] << 8) | u32(s_box0[w & u32(0xff)])
}
// Rotate
@@ -184,8 +175,7 @@ fn expand_key_generic(key []byte, mut enc []u32, mut dec []u32) {
for j in 0 .. 4 {
mut x := enc[ei + j]
if i > 0 && i + 4 < n {
x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^
td3[s_box0[x & u32(0xff)]]
x = td0[s_box0[x >> 24]] ^ td1[s_box0[x >> 16 & 0xff]] ^ td2[s_box0[x >> 8 & 0xff]] ^ td3[s_box0[x & u32(0xff)]]
}
dec[i + j] = x
}

View File

@@ -8,8 +8,8 @@ module aes
fn new_cipher_generic(key []byte) AesCipher {
n := key.len + 28
mut c := AesCipher{
enc: []u32{len: (n)}
dec: []u32{len: (n)}
enc: []u32{len: n}
dec: []u32{len: n}
}
expand_key_generic(key, mut c.enc, mut c.dec)
return c

View File

@@ -21,4 +21,3 @@ pub enum Hash {
blake2b_384
blake2b_512
}

View File

@@ -11,7 +11,7 @@ const (
)
// new returns a HMAC byte array, depending on the hash algorithm used.
pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksize int) []byte {
pub fn new(key []byte, data []byte, hash_func fn ([]byte) []byte, blocksize int) []byte {
mut b_key := []byte{}
if key.len <= blocksize {
b_key = key.clone() // TODO: remove .clone() once https://github.com/vlang/v/issues/6604 gets fixed
@@ -19,16 +19,16 @@ pub fn new(key []byte, data []byte, hash_func fn (bytes []byte) []byte, blocksiz
b_key = hash_func(key)
}
if b_key.len < blocksize {
b_key << npad[..blocksize - b_key.len]
b_key << hmac.npad[..blocksize - b_key.len]
}
mut inner := []byte{}
for i, b in ipad[..blocksize] {
for i, b in hmac.ipad[..blocksize] {
inner << b_key[i] ^ b
}
inner << data
inner_hash := hash_func(inner)
mut outer := []byte{cap: b_key.len}
for i, b in opad[..blocksize] {
for i, b in hmac.opad[..blocksize] {
outer << b_key[i] ^ b
}
outer << inner_hash

View File

@@ -21,31 +21,37 @@ import crypto.sha512
// import crypto.blake2b_512
const (
keys = [[byte(0xb), 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb],
'Jefe'.bytes(), [byte(0xAA), 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA], [byte(0x01), 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19],
[byte(0x0c), 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0x0c,
], [byte(0xaa), 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa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0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd],
'Test With Truncation'.bytes(),
'Test Using Larger Than Block-Size Key - Hash Key First'.bytes(),
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'.bytes(),
]
)
@@ -61,8 +67,8 @@ fn test_hmac_md5() {
'6f630fad67cda0ee1fb1f562db3aa53e',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], md5.sum, md5.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], md5.sum, md5.block_size).hex()
assert result == md5_expected_results[i]
}
}
@@ -78,8 +84,8 @@ fn test_hmac_sha1() {
'e8e99d0f45237d786d6bbaa7965c7808bbff1a91',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], sha1.sum, sha1.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], sha1.sum, sha1.block_size).hex()
assert result == sha1_expected_results[i]
}
}
@@ -95,8 +101,8 @@ fn test_hmac_sha224() {
'7358939e58683a448ac5065196d33191a1c1d33d4b8b0304dc60f5e0',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], sha256.sum224, sha256.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], sha256.sum224, sha256.block_size).hex()
assert result == sha224_expected_results[i]
}
}
@@ -112,8 +118,8 @@ fn test_hmac_sha256() {
'6355ac22e890d0a3c8481a5ca4825bc884d3e7a1ff98a2fc2ac7d8e064c3b2e6',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], sha256.sum, sha256.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], sha256.sum, sha256.block_size).hex()
assert result == sha256_expected_results[i]
}
}
@@ -129,8 +135,8 @@ fn test_hmac_sha384() {
'34f065bdedc2487c30a634d9a49cf42116f78bb386ea4d498aea05c0077f05373cfdaa9b59a7b0481bced9e3f55016a9',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], sha512.sum384, sha512.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], sha512.sum384, sha512.block_size).hex()
assert result == sha384_expected_results[i]
}
}
@@ -146,8 +152,8 @@ fn test_hmac_sha512() {
'09441cda584ed2f4d2f5b519c71baf3c79cce19dfc89a548e73b3bb382a9124d6e792b77bf57903ff5858e5d111d15f45d6fd118eea023f28d2eb234ebe62f85',
]
mut result := ''
for i, key in keys {
result = hmac.new(key, data[i], sha512.sum512, sha512.block_size).hex()
for i, key in hmac.keys {
result = hmac.new(key, hmac.data[i], sha512.sum512, sha512.block_size).hex()
assert result == sha512_expected_results[i]
}
}

View File

@@ -10,9 +10,9 @@ module subtle
// corresponding) index. The memory beyond the slice length is ignored.
pub fn any_overlap(x []byte, y []byte) bool {
// NOTE: Remember to come back to this (joe-c)
return x.len > 0 && y.len > 0 && // &x.data[0] <= &y.data[y.len-1] &&
return x.len > 0 && y.len > 0 && // &x.data[0] <= &y.data[y.len-1] &&
// &y.data[0] <= &x.data[x.len-1]
unsafe {&x[0] <= &y[y.len - 1] && &y[0] <= &x[x.len - 1]}
unsafe { &x[0] <= &y[y.len - 1] && &y[0] <= &x[x.len - 1] }
}
// inexact_overlap reports whether x and y share memory at any non-corresponding
@@ -22,7 +22,7 @@ pub fn any_overlap(x []byte, y []byte) bool {
// inexact_overlap can be used to implement the requirements of the crypto/cipher
// AEAD, Block, BlockMode and Stream interfaces.
pub fn inexact_overlap(x []byte, y []byte) bool {
if x.len == 0 || y.len == 0 || unsafe {&x[0] == &y[0]} {
if x.len == 0 || y.len == 0 || unsafe { &x[0] == &y[0] } {
return false
}
return any_overlap(x, y)

View File

@@ -35,11 +35,11 @@ mut:
fn (mut d Digest) reset() {
d.s = []u32{len: (4)}
d.x = []byte{len: (block_size)}
d.s[0] = u32(init0)
d.s[1] = u32(init1)
d.s[2] = u32(init2)
d.s[3] = u32(init3)
d.x = []byte{len: md5.block_size}
d.s[0] = u32(md5.init0)
d.s[1] = u32(md5.init1)
d.s[2] = u32(md5.init2)
d.s[3] = u32(md5.init3)
d.nx = 0
d.len = 0
}
@@ -60,7 +60,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == block_size {
if d.nx == md5.block_size {
block(mut d, d.x)
d.nx = 0
}
@@ -70,8 +70,8 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
p = p[n..]
}
}
if p.len >= block_size {
n := p.len & ~(block_size - 1)
if p.len >= md5.block_size {
n := p.len & ~(md5.block_size - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
@@ -116,7 +116,7 @@ pub fn (mut d Digest) checksum() []byte {
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []byte{len: (size)}
mut digest := []byte{len: md5.size}
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[8..], d.s[2])
@@ -139,12 +139,12 @@ fn block(mut dig Digest, p []byte) {
// size returns the size of the checksum in bytes.
pub fn (d &Digest) size() int {
return size
return md5.size
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
return md5.block_size
}
// hexhash returns a hexadecimal MD5 hash sum `string` of `s`.

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.md5
fn test_crypto_md5() {

View File

@@ -18,7 +18,7 @@ fn block_generic(mut dig Digest, p []byte) {
mut c := dig.s[2]
mut d := dig.s[3]
for i := 0; i <= p.len-block_size; i += block_size {
for i := 0; i <= p.len - block_size; i += block_size {
mut q := p[i..]
q = q[..block_size]
// save current state
@@ -28,94 +28,94 @@ fn block_generic(mut dig Digest, p []byte) {
dd := d
// load input block
x0 := binary.little_endian_u32(q[4*0x0..])
x1 := binary.little_endian_u32(q[4*0x1..])
x2 := binary.little_endian_u32(q[4*0x2..])
x3 := binary.little_endian_u32(q[4*0x3..])
x4 := binary.little_endian_u32(q[4*0x4..])
x5 := binary.little_endian_u32(q[4*0x5..])
x6 := binary.little_endian_u32(q[4*0x6..])
x7 := binary.little_endian_u32(q[4*0x7..])
x8 := binary.little_endian_u32(q[4*0x8..])
x9 := binary.little_endian_u32(q[4*0x9..])
xa := binary.little_endian_u32(q[4*0xa..])
xb := binary.little_endian_u32(q[4*0xb..])
xc := binary.little_endian_u32(q[4*0xc..])
xd := binary.little_endian_u32(q[4*0xd..])
xe := binary.little_endian_u32(q[4*0xe..])
xf := binary.little_endian_u32(q[4*0xf..])
x0 := binary.little_endian_u32(q[4 * 0x0..])
x1 := binary.little_endian_u32(q[4 * 0x1..])
x2 := binary.little_endian_u32(q[4 * 0x2..])
x3 := binary.little_endian_u32(q[4 * 0x3..])
x4 := binary.little_endian_u32(q[4 * 0x4..])
x5 := binary.little_endian_u32(q[4 * 0x5..])
x6 := binary.little_endian_u32(q[4 * 0x6..])
x7 := binary.little_endian_u32(q[4 * 0x7..])
x8 := binary.little_endian_u32(q[4 * 0x8..])
x9 := binary.little_endian_u32(q[4 * 0x9..])
xa := binary.little_endian_u32(q[4 * 0xa..])
xb := binary.little_endian_u32(q[4 * 0xb..])
xc := binary.little_endian_u32(q[4 * 0xc..])
xd := binary.little_endian_u32(q[4 * 0xd..])
xe := binary.little_endian_u32(q[4 * 0xe..])
xf := binary.little_endian_u32(q[4 * 0xf..])
// round 1
a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x0+u32(0xd76aa478), 7)
d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x1+u32(0xe8c7b756), 12)
c = d + bits.rotate_left_32((((a^b)&d)^b)+c+x2+u32(0x242070db), 17)
b = c + bits.rotate_left_32((((d^a)&c)^a)+b+x3+u32(0xc1bdceee), 22)
a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x4+u32(0xf57c0faf), 7)
d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x5+u32(0x4787c62a), 12)
c = d + bits.rotate_left_32((((a^b)&d)^b)+c+x6+u32(0xa8304613), 17)
b = c + bits.rotate_left_32((((d^a)&c)^a)+b+x7+u32(0xfd469501), 22)
a = b + bits.rotate_left_32((((c^d)&b)^d)+a+x8+u32(0x698098d8), 7)
d = a + bits.rotate_left_32((((b^c)&a)^c)+d+x9+u32(0x8b44f7af), 12)
c = d + bits.rotate_left_32((((a^b)&d)^b)+c+xa+u32(0xffff5bb1), 17)
b = c + bits.rotate_left_32((((d^a)&c)^a)+b+xb+u32(0x895cd7be), 22)
a = b + bits.rotate_left_32((((c^d)&b)^d)+a+xc+u32(0x6b901122), 7)
d = a + bits.rotate_left_32((((b^c)&a)^c)+d+xd+u32(0xfd987193), 12)
c = d + bits.rotate_left_32((((a^b)&d)^b)+c+xe+u32(0xa679438e), 17)
b = c + bits.rotate_left_32((((d^a)&c)^a)+b+xf+u32(0x49b40821), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x0 + u32(0xd76aa478), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x1 + u32(0xe8c7b756), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x2 + u32(0x242070db), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x3 + u32(0xc1bdceee), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x4 + u32(0xf57c0faf), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x5 + u32(0x4787c62a), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x6 + u32(0xa8304613), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x7 + u32(0xfd469501), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x8 + u32(0x698098d8), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x9 + u32(0x8b44f7af), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xa + u32(0xffff5bb1), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xb + u32(0x895cd7be), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + xc + u32(0x6b901122), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + xd + u32(0xfd987193), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xe + u32(0xa679438e), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xf + u32(0x49b40821), 22)
// round 2
a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x1+u32(0xf61e2562), 5)
d = a + bits.rotate_left_32((((a^b)&c)^b)+d+x6+u32(0xc040b340), 9)
c = d + bits.rotate_left_32((((d^a)&b)^a)+c+xb+u32(0x265e5a51), 14)
b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x0+u32(0xe9b6c7aa), 20)
a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x5+u32(0xd62f105d), 5)
d = a + bits.rotate_left_32((((a^b)&c)^b)+d+xa+u32(0x02441453), 9)
c = d + bits.rotate_left_32((((d^a)&b)^a)+c+xf+u32(0xd8a1e681), 14)
b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x4+u32(0xe7d3fbc8), 20)
a = b + bits.rotate_left_32((((b^c)&d)^c)+a+x9+u32(0x21e1cde6), 5)
d = a + bits.rotate_left_32((((a^b)&c)^b)+d+xe+u32(0xc33707d6), 9)
c = d + bits.rotate_left_32((((d^a)&b)^a)+c+x3+u32(0xf4d50d87), 14)
b = c + bits.rotate_left_32((((c^d)&a)^d)+b+x8+u32(0x455a14ed), 20)
a = b + bits.rotate_left_32((((b^c)&d)^c)+a+xd+u32(0xa9e3e905), 5)
d = a + bits.rotate_left_32((((a^b)&c)^b)+d+x2+u32(0xfcefa3f8), 9)
c = d + bits.rotate_left_32((((d^a)&b)^a)+c+x7+u32(0x676f02d9), 14)
b = c + bits.rotate_left_32((((c^d)&a)^d)+b+xc+u32(0x8d2a4c8a), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x1 + u32(0xf61e2562), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x6 + u32(0xc040b340), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xb + u32(0x265e5a51), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x0 + u32(0xe9b6c7aa), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x5 + u32(0xd62f105d), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xa + u32(0x02441453), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xf + u32(0xd8a1e681), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x4 + u32(0xe7d3fbc8), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x9 + u32(0x21e1cde6), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xe + u32(0xc33707d6), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x3 + u32(0xf4d50d87), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x8 + u32(0x455a14ed), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + xd + u32(0xa9e3e905), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x2 + u32(0xfcefa3f8), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x7 + u32(0x676f02d9), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + xc + u32(0x8d2a4c8a), 20)
// round 3
a = b + bits.rotate_left_32((b^c^d)+a+x5+u32(0xfffa3942), 4)
d = a + bits.rotate_left_32((a^b^c)+d+x8+u32(0x8771f681), 11)
c = d + bits.rotate_left_32((d^a^b)+c+xb+u32(0x6d9d6122), 16)
b = c + bits.rotate_left_32((c^d^a)+b+xe+u32(0xfde5380c), 23)
a = b + bits.rotate_left_32((b^c^d)+a+x1+u32(0xa4beea44), 4)
d = a + bits.rotate_left_32((a^b^c)+d+x4+u32(0x4bdecfa9), 11)
c = d + bits.rotate_left_32((d^a^b)+c+x7+u32(0xf6bb4b60), 16)
b = c + bits.rotate_left_32((c^d^a)+b+xa+u32(0xbebfbc70), 23)
a = b + bits.rotate_left_32((b^c^d)+a+xd+u32(0x289b7ec6), 4)
d = a + bits.rotate_left_32((a^b^c)+d+x0+u32(0xeaa127fa), 11)
c = d + bits.rotate_left_32((d^a^b)+c+x3+u32(0xd4ef3085), 16)
b = c + bits.rotate_left_32((c^d^a)+b+x6+u32(0x04881d05), 23)
a = b + bits.rotate_left_32((b^c^d)+a+x9+u32(0xd9d4d039), 4)
d = a + bits.rotate_left_32((a^b^c)+d+xc+u32(0xe6db99e5), 11)
c = d + bits.rotate_left_32((d^a^b)+c+xf+u32(0x1fa27cf8), 16)
b = c + bits.rotate_left_32((c^d^a)+b+x2+u32(0xc4ac5665), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x5 + u32(0xfffa3942), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x8 + u32(0x8771f681), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + xb + u32(0x6d9d6122), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + xe + u32(0xfde5380c), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x1 + u32(0xa4beea44), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x4 + u32(0x4bdecfa9), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + x7 + u32(0xf6bb4b60), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + xa + u32(0xbebfbc70), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + xd + u32(0x289b7ec6), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x0 + u32(0xeaa127fa), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + x3 + u32(0xd4ef3085), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + x6 + u32(0x04881d05), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x9 + u32(0xd9d4d039), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + xc + u32(0xe6db99e5), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + xf + u32(0x1fa27cf8), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + x2 + u32(0xc4ac5665), 23)
// round 4
a = b + bits.rotate_left_32((c^(b|~d))+a+x0+u32(0xf4292244), 6)
d = a + bits.rotate_left_32((b^(a|~c))+d+x7+u32(0x432aff97), 10)
c = d + bits.rotate_left_32((a^(d|~b))+c+xe+u32(0xab9423a7), 15)
b = c + bits.rotate_left_32((d^(c|~a))+b+x5+u32(0xfc93a039), 21)
a = b + bits.rotate_left_32((c^(b|~d))+a+xc+u32(0x655b59c3), 6)
d = a + bits.rotate_left_32((b^(a|~c))+d+x3+u32(0x8f0ccc92), 10)
c = d + bits.rotate_left_32((a^(d|~b))+c+xa+u32(0xffeff47d), 15)
b = c + bits.rotate_left_32((d^(c|~a))+b+x1+u32(0x85845dd1), 21)
a = b + bits.rotate_left_32((c^(b|~d))+a+x8+u32(0x6fa87e4f), 6)
d = a + bits.rotate_left_32((b^(a|~c))+d+xf+u32(0xfe2ce6e0), 10)
c = d + bits.rotate_left_32((a^(d|~b))+c+x6+u32(0xa3014314), 15)
b = c + bits.rotate_left_32((d^(c|~a))+b+xd+u32(0x4e0811a1), 21)
a = b + bits.rotate_left_32((c^(b|~d))+a+x4+u32(0xf7537e82), 6)
d = a + bits.rotate_left_32((b^(a|~c))+d+xb+u32(0xbd3af235), 10)
c = d + bits.rotate_left_32((a^(d|~b))+c+x2+u32(0x2ad7d2bb), 15)
b = c + bits.rotate_left_32((d^(c|~a))+b+x9+u32(0xeb86d391), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x0 + u32(0xf4292244), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x7 + u32(0x432aff97), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xe + u32(0xab9423a7), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x5 + u32(0xfc93a039), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + xc + u32(0x655b59c3), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x3 + u32(0x8f0ccc92), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xa + u32(0xffeff47d), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x1 + u32(0x85845dd1), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x8 + u32(0x6fa87e4f), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xf + u32(0xfe2ce6e0), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x6 + u32(0xa3014314), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + xd + u32(0x4e0811a1), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x4 + u32(0xf7537e82), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xb + u32(0xbd3af235), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x2 + u32(0x2ad7d2bb), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x9 + u32(0xeb86d391), 21)
// add saved state
a += aa

View File

@@ -4,6 +4,7 @@
module rand
#include <sys/syscall.h>
const (
read_batch_size = 256
)
@@ -15,7 +16,11 @@ pub fn read(bytes_needed int) ?[]byte {
mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes
for bytes_read < bytes_needed {
batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes }
batch_size := if remaining_bytes > rand.read_batch_size {
rand.read_batch_size
} else {
remaining_bytes
}
rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
if rbytes == -1 {
unsafe { free(buffer) }
@@ -23,12 +28,12 @@ pub fn read(bytes_needed int) ?[]byte {
}
bytes_read += rbytes
}
return unsafe {buffer.vbytes(bytes_needed)}
return unsafe { buffer.vbytes(bytes_needed) }
}
fn getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > read_batch_size {
panic('getrandom() dont request more than $read_batch_size bytes at once.')
if bytes_needed > rand.read_batch_size {
panic('getrandom() dont request more than $rand.read_batch_size bytes at once.')
}
return unsafe { C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) }
}

View File

@@ -6,7 +6,7 @@ module rand
#include <sys/random.h>
fn C.getrandom(p byteptr, n size_t, flags u32) int
fn C.getrandom(p &byte, n size_t, flags u32) int
const (
read_batch_size = 256
@@ -19,7 +19,11 @@ pub fn read(bytes_needed int) ?[]byte {
mut remaining_bytes := bytes_needed
// getrandom syscall wont block if requesting <= 256 bytes
for bytes_read < bytes_needed {
batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes }
batch_size := if remaining_bytes > rand.read_batch_size {
rand.read_batch_size
} else {
remaining_bytes
}
rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) }
if rbytes == -1 {
unsafe { free(buffer) }
@@ -27,12 +31,12 @@ pub fn read(bytes_needed int) ?[]byte {
}
bytes_read += rbytes
}
return unsafe {buffer.vbytes(bytes_needed)}
return unsafe { buffer.vbytes(bytes_needed) }
}
fn v_getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > read_batch_size {
panic('getrandom() dont request more than $read_batch_size bytes at once.')
if bytes_needed > rand.read_batch_size {
panic('getrandom() dont request more than $rand.read_batch_size bytes at once.')
}
return C.getrandom(buffer, bytes_needed, 0)
}

View File

@@ -15,10 +15,10 @@ const (
// read returns an array of `bytes_needed` random bytes read from the OS.
pub fn read(bytes_needed int) ?[]byte {
mut buffer := []byte{ len: bytes_needed }
mut buffer := []byte{len: bytes_needed}
// use bcrypt_use_system_preferred_rng because we passed null as algo
status := C.BCryptGenRandom(0, buffer.data, bytes_needed, bcrypt_use_system_preferred_rng)
if status != status_success {
status := C.BCryptGenRandom(0, buffer.data, bytes_needed, rand.bcrypt_use_system_preferred_rng)
if status != rand.status_success {
return IError(&ReadError{})
}
return buffer

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.rc4
fn test_crypto_rc4() {

View File

@@ -36,13 +36,13 @@ mut:
}
fn (mut d Digest) reset() {
d.x = []byte{len: (chunk)}
d.x = []byte{len: sha1.chunk}
d.h = []u32{len: (5)}
d.h[0] = u32(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
d.h[3] = u32(init3)
d.h[4] = u32(init4)
d.h[0] = u32(sha1.init0)
d.h[1] = u32(sha1.init1)
d.h[2] = u32(sha1.init2)
d.h[3] = u32(sha1.init3)
d.h[4] = u32(sha1.init4)
d.nx = 0
d.len = 0
}
@@ -64,7 +64,7 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == chunk {
if d.nx == sha1.chunk {
block(mut d, d.x)
d.nx = 0
}
@@ -74,8 +74,8 @@ pub fn (mut d Digest) write(p_ []byte) ?int {
p = p[n..]
}
}
if p.len >= chunk {
n := p.len & ~(chunk - 1)
if p.len >= sha1.chunk {
n := p.len & ~(sha1.chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
@@ -117,7 +117,7 @@ fn (mut d Digest) checksum() []byte {
len <<= 3
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp[..8]) or { panic(err) }
mut digest := []byte{len: (size)}
mut digest := []byte{len: sha1.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])
@@ -141,12 +141,12 @@ fn block(mut dig Digest, p []byte) {
// size returns the size of the checksum in bytes.
pub fn (d &Digest) size() int {
return size
return sha1.size
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
return sha1.block_size
}
// hexhash returns a hexadecimal SHA1 hash sum `string` of `s`.

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.sha1
fn test_crypto_sha1() {

View File

@@ -42,7 +42,7 @@ fn block_generic(mut dig Digest, p_ []byte) {
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)
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@@ -51,10 +51,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
i++
}
for i < 20 {
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
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)
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@@ -63,10 +63,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
i++
}
for i < 40 {
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
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)
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k1)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@@ -75,10 +75,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
i++
}
for i < 60 {
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
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)
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k2)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@@ -87,10 +87,10 @@ fn block_generic(mut dig Digest, p_ []byte) {
i++
}
for i < 80 {
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[(i) & 0xf]
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)
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k3)
e = d
d = c
c = bits.rotate_left_32(b, 30)

View File

@@ -50,25 +50,25 @@ mut:
fn (mut d Digest) reset() {
d.h = []u32{len: (8)}
d.x = []byte{len: (chunk)}
d.x = []byte{len: sha256.chunk}
if !d.is224 {
d.h[0] = u32(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
d.h[3] = u32(init3)
d.h[4] = u32(init4)
d.h[5] = u32(init5)
d.h[6] = u32(init6)
d.h[7] = u32(init7)
d.h[0] = u32(sha256.init0)
d.h[1] = u32(sha256.init1)
d.h[2] = u32(sha256.init2)
d.h[3] = u32(sha256.init3)
d.h[4] = u32(sha256.init4)
d.h[5] = u32(sha256.init5)
d.h[6] = u32(sha256.init6)
d.h[7] = u32(sha256.init7)
} else {
d.h[0] = u32(init0_224)
d.h[1] = u32(init1_224)
d.h[2] = u32(init2_224)
d.h[3] = u32(init3_224)
d.h[4] = u32(init4_224)
d.h[5] = u32(init5_224)
d.h[6] = u32(init6_224)
d.h[7] = u32(init7_224)
d.h[0] = u32(sha256.init0_224)
d.h[1] = u32(sha256.init1_224)
d.h[2] = u32(sha256.init2_224)
d.h[3] = u32(sha256.init3_224)
d.h[4] = u32(sha256.init4_224)
d.h[5] = u32(sha256.init5_224)
d.h[6] = u32(sha256.init6_224)
d.h[7] = u32(sha256.init7_224)
}
d.nx = 0
d.len = 0
@@ -98,7 +98,7 @@ fn (mut d Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == chunk {
if d.nx == sha256.chunk {
block(mut d, d.x)
d.nx = 0
}
@@ -108,8 +108,8 @@ fn (mut d Digest) write(p_ []byte) ?int {
p = p[n..]
}
}
if p.len >= chunk {
n := p.len & ~(chunk - 1)
if p.len >= sha256.chunk {
n := p.len & ~(sha256.chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
@@ -130,7 +130,7 @@ pub fn (d &Digest) sum(b_in []byte) []byte {
hash := d0.checksum()
mut b_out := b_in.clone()
if d0.is224 {
for b in hash[..size224] {
for b in hash[..sha256.size224] {
b_out << b
}
} else {
@@ -158,7 +158,7 @@ fn (mut d Digest) checksum() []byte {
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []byte{len: (size)}
mut digest := []byte{len: sha256.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])
@@ -190,8 +190,8 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data) or { panic(err) }
sum := d.checksum()
sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224])
sum224 := []byte{len: sha256.size224}
copy(sum224, sum[..sha256.size224])
return sum224
}
@@ -204,14 +204,14 @@ fn block(mut dig Digest, p []byte) {
// size returns the size of the checksum in bytes.
pub fn (d &Digest) size() int {
if !d.is224 {
return size
return sha256.size
}
return size224
return sha256.size224
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
return sha256.block_size
}
// hexhash returns a hexadecimal SHA256 hash sum `string` of `s`.

View File

@@ -1,12 +1,10 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.sha256
fn test_crypto_sha256() {
assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() ==
'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'
assert sha256.sum('This is a sha256 checksum.'.bytes()).hex() == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'
}
fn test_crypto_sha256_writer() {

View File

@@ -115,9 +115,9 @@ fn block_generic(mut dig Digest, p_ []byte) {
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]
t2 := ((bits.rotate_left_32(a, -2)) ^
(bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) +
((e & f) ^ (~e & g)) + u32(sha256._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
g = f

View File

@@ -72,47 +72,47 @@ mut:
fn (mut d Digest) reset() {
d.h = []u64{len: (8)}
d.x = []byte{len: (chunk)}
d.x = []byte{len: sha512.chunk}
match d.function {
.sha384 {
d.h[0] = init0_384
d.h[1] = init1_384
d.h[2] = init2_384
d.h[3] = init3_384
d.h[4] = init4_384
d.h[5] = init5_384
d.h[6] = init6_384
d.h[7] = init7_384
d.h[0] = sha512.init0_384
d.h[1] = sha512.init1_384
d.h[2] = sha512.init2_384
d.h[3] = sha512.init3_384
d.h[4] = sha512.init4_384
d.h[5] = sha512.init5_384
d.h[6] = sha512.init6_384
d.h[7] = sha512.init7_384
}
.sha512_224 {
d.h[0] = init0_224
d.h[1] = init1_224
d.h[2] = init2_224
d.h[3] = init3_224
d.h[4] = init4_224
d.h[5] = init5_224
d.h[6] = init6_224
d.h[7] = init7_224
d.h[0] = sha512.init0_224
d.h[1] = sha512.init1_224
d.h[2] = sha512.init2_224
d.h[3] = sha512.init3_224
d.h[4] = sha512.init4_224
d.h[5] = sha512.init5_224
d.h[6] = sha512.init6_224
d.h[7] = sha512.init7_224
}
.sha512_256 {
d.h[0] = init0_256
d.h[1] = init1_256
d.h[2] = init2_256
d.h[3] = init3_256
d.h[4] = init4_256
d.h[5] = init5_256
d.h[6] = init6_256
d.h[7] = init7_256
d.h[0] = sha512.init0_256
d.h[1] = sha512.init1_256
d.h[2] = sha512.init2_256
d.h[3] = sha512.init3_256
d.h[4] = sha512.init4_256
d.h[5] = sha512.init5_256
d.h[6] = sha512.init6_256
d.h[7] = sha512.init7_256
}
else {
d.h[0] = init0
d.h[1] = init1
d.h[2] = init2
d.h[3] = init3
d.h[4] = init4
d.h[5] = init5
d.h[6] = init6
d.h[7] = init7
d.h[0] = sha512.init0
d.h[1] = sha512.init1
d.h[2] = sha512.init2
d.h[3] = sha512.init3
d.h[4] = sha512.init4
d.h[5] = sha512.init5
d.h[6] = sha512.init6
d.h[7] = sha512.init7
}
}
d.nx = 0
@@ -157,7 +157,7 @@ fn (mut d Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x[d.nx..], p)
d.nx += n
if d.nx == chunk {
if d.nx == sha512.chunk {
block(mut d, d.x)
d.nx = 0
}
@@ -167,8 +167,8 @@ fn (mut d Digest) write(p_ []byte) ?int {
p = p[n..]
}
}
if p.len >= chunk {
n := p.len & ~(chunk - 1)
if p.len >= sha512.chunk {
n := p.len & ~(sha512.chunk - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
@@ -190,17 +190,17 @@ fn (d &Digest) sum(b_in []byte) []byte {
mut b_out := b_in.clone()
match d0.function {
.sha384 {
for b in hash[..size384] {
for b in hash[..sha512.size384] {
b_out << b
}
}
.sha512_224 {
for b in hash[..size224] {
for b in hash[..sha512.size224] {
b_out << b
}
}
.sha512_256 {
for b in hash[..size256] {
for b in hash[..sha512.size256] {
b_out << b
}
}
@@ -231,7 +231,7 @@ fn (mut d Digest) checksum() []byte {
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []byte{len: (size)}
mut digest := []byte{len: sha512.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])
@@ -257,8 +257,8 @@ pub fn sum384(data []byte) []byte {
mut d := new_digest(.sha384)
d.write(data) or { panic(err) }
sum := d.checksum()
sum384 := []byte{len: (size384)}
copy(sum384, sum[..size384])
sum384 := []byte{len: sha512.size384}
copy(sum384, sum[..sha512.size384])
return sum384
}
@@ -267,8 +267,8 @@ pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224)
d.write(data) or { panic(err) }
sum := d.checksum()
sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224])
sum224 := []byte{len: sha512.size224}
copy(sum224, sum[..sha512.size224])
return sum224
}
@@ -277,8 +277,8 @@ pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256)
d.write(data) or { panic(err) }
sum := d.checksum()
sum256 := []byte{len: (size256)}
copy(sum256, sum[..size256])
sum256 := []byte{len: sha512.size256}
copy(sum256, sum[..sha512.size256])
return sum256
}
@@ -291,16 +291,16 @@ fn block(mut dig Digest, p []byte) {
// size returns the size of the checksum in bytes.
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 sha512.size224 }
.sha512_256 { return sha512.size256 }
.sha384 { return sha512.size384 }
else { return sha512.size }
}
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
return sha512.block_size
}
// hexhash returns a hexadecimal SHA512 hash sum `string` of `s`.