lower/snake case consts & enums

pull/2532/head
joe-conigliaro 2019-10-24 22:48:20 +11:00 committed by Alexander Medvednikov
parent fe17dd9a7e
commit 580abe0de4
20 changed files with 301 additions and 308 deletions

View File

@ -11,8 +11,8 @@ import (
fn test_import() {
assert os.O_RDONLY == os.O_RDONLY &&
t.month_days[0] == t.month_days[0] &&
s2.Size == s2.Size &&
s2.size == s2.size &&
math.pi == math.pi &&
l.INFO == l.INFO &&
s5.Size == s5.Size
s5.size == s5.size
}

View File

@ -13,7 +13,7 @@ import (
const (
// The AES block size in bytes.
BlockSize = 16
block_size = 16
)
// A cipher is an instance of AES encryption using a particular key.
@ -39,17 +39,17 @@ pub fn new_cipher(key []byte) AesCipher {
return new_cipher_generic(key)
}
pub fn (c &AesCipher) block_size() int { return BlockSize }
pub fn (c &AesCipher) block_size() int { return block_size }
pub fn (c &AesCipher) encrypt(dst, src []byte) {
if src.len < BlockSize {
if src.len < block_size {
panic('crypto.aes: input not full block')
}
if dst.len < BlockSize {
if dst.len < block_size {
panic('crypto.aes: output not full block')
}
// if subtle.inexact_overlap(dst[:BlockSize], src[:BlockSize]) {
if subtle.inexact_overlap(dst.left(BlockSize), src.left(BlockSize)) {
// if subtle.inexact_overlap(dst[:block_size], src[:block_size]) {
if subtle.inexact_overlap(dst.left(block_size), src.left(block_size)) {
panic('crypto.aes: invalid buffer overlap')
}
// for now use generic version
@ -57,13 +57,13 @@ pub fn (c &AesCipher) encrypt(dst, src []byte) {
}
pub fn (c &AesCipher) decrypt(dst, src []byte) {
if src.len < BlockSize {
if src.len < block_size {
panic('crypto.aes: input not full block')
}
if dst.len < BlockSize {
if dst.len < block_size {
panic('crypto.aes: output not full block')
}
if subtle.inexact_overlap(dst.left(BlockSize), src.left(BlockSize)) {
if subtle.inexact_overlap(dst.left(block_size), src.left(block_size)) {
panic('crypto.aes: invalid buffer overlap')
}
// for now use generic version

View File

@ -11,13 +11,13 @@ fn test_crypto_aes() {
block := aes.new_cipher(key)
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if ciphertext.len < aes.BlockSize {
if ciphertext.len < aes.block_size {
panic('ciphertext too short')
}
iv := ciphertext.left(aes.BlockSize)
ciphertext = ciphertext.right(aes.BlockSize)
iv := ciphertext.left(aes.block_size)
ciphertext = ciphertext.right(aes.block_size)
// CBC mode always works in whole blocks.
if ciphertext.len%aes.BlockSize != 0 {
if ciphertext.len%aes.block_size != 0 {
panic('ciphertext is not a multiple of the block size')
}
mut mode := aes.new_cbc(block, iv)

View File

@ -64,10 +64,10 @@ fn encrypt_block_generic(xk []u32, dst, src []byte) {
mut t2 := u32(0)
mut t3 := u32(0)
for r := 0; r < nr; r++ {
t0 = xk[k+0] ^ Te0[byte(s0>>24)] ^ Te1[byte(s1>>16)] ^ Te2[byte(s2>>8)] ^ u32(Te3[byte(s3)])
t1 = xk[k+1] ^ Te0[byte(s1>>24)] ^ Te1[byte(s2>>16)] ^ Te2[byte(s3>>8)] ^ u32(Te3[byte(s0)])
t2 = xk[k+2] ^ Te0[byte(s2>>24)] ^ Te1[byte(s3>>16)] ^ Te2[byte(s0>>8)] ^ u32(Te3[byte(s1)])
t3 = xk[k+3] ^ Te0[byte(s3>>24)] ^ Te1[byte(s0>>16)] ^ Te2[byte(s1>>8)] ^ u32(Te3[byte(s2)])
t0 = xk[k+0] ^ te0[byte(s0>>24)] ^ te1[byte(s1>>16)] ^ te2[byte(s2>>8)] ^ u32(te3[byte(s3)])
t1 = xk[k+1] ^ te0[byte(s1>>24)] ^ te1[byte(s2>>16)] ^ te2[byte(s3>>8)] ^ u32(te3[byte(s0)])
t2 = xk[k+2] ^ te0[byte(s2>>24)] ^ te1[byte(s3>>16)] ^ te2[byte(s0>>8)] ^ u32(te3[byte(s1)])
t3 = xk[k+3] ^ te0[byte(s3>>24)] ^ te1[byte(s0>>16)] ^ te2[byte(s1>>8)] ^ u32(te3[byte(s2)])
k += 4
s0 = t0
s1 = t1
@ -76,10 +76,10 @@ fn encrypt_block_generic(xk []u32, dst, src []byte) {
}
// Last round uses s-box directly and XORs to produce output.
s0 = SBox0[t0>>24]<<24 | SBox0[t1>>16&0xff]<<16 | u32(SBox0[t2>>8&0xff]<<8) | SBox0[t3&u32(0xff)]
s1 = SBox0[t1>>24]<<24 | SBox0[t2>>16&0xff]<<16 | u32(SBox0[t3>>8&0xff]<<8) | SBox0[t0&u32(0xff)]
s2 = SBox0[t2>>24]<<24 | SBox0[t3>>16&0xff]<<16 | u32(SBox0[t0>>8&0xff]<<8) | SBox0[t1&u32(0xff)]
s3 = SBox0[t3>>24]<<24 | SBox0[t0>>16&0xff]<<16 | u32(SBox0[t1>>8&0xff]<<8) | SBox0[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]
@ -116,10 +116,10 @@ fn decrypt_block_generic(xk []u32, dst, src []byte) {
mut t2 := u32(0)
mut t3 := u32(0)
for r := 0; r < nr; r++ {
t0 = xk[k+0] ^ Td0[byte(s0>>24)] ^ Td1[byte(s3>>16)] ^ Td2[byte(s2>>8)] ^ u32(Td3[byte(s1)])
t1 = xk[k+1] ^ Td0[byte(s1>>24)] ^ Td1[byte(s0>>16)] ^ Td2[byte(s3>>8)] ^ u32(Td3[byte(s2)])
t2 = xk[k+2] ^ Td0[byte(s2>>24)] ^ Td1[byte(s1>>16)] ^ Td2[byte(s0>>8)] ^ u32(Td3[byte(s3)])
t3 = xk[k+3] ^ Td0[byte(s3>>24)] ^ Td1[byte(s2>>16)] ^ Td2[byte(s1>>8)] ^ u32(Td3[byte(s0)])
t0 = xk[k+0] ^ td0[byte(s0>>24)] ^ td1[byte(s3>>16)] ^ td2[byte(s2>>8)] ^ u32(td3[byte(s1)])
t1 = xk[k+1] ^ td0[byte(s1>>24)] ^ td1[byte(s0>>16)] ^ td2[byte(s3>>8)] ^ u32(td3[byte(s2)])
t2 = xk[k+2] ^ td0[byte(s2>>24)] ^ td1[byte(s1>>16)] ^ td2[byte(s0>>8)] ^ u32(td3[byte(s3)])
t3 = xk[k+3] ^ td0[byte(s3>>24)] ^ td1[byte(s2>>16)] ^ td2[byte(s1>>8)] ^ u32(td3[byte(s0)])
k += 4
s0 = t0
s1 = t1
@ -128,10 +128,10 @@ fn decrypt_block_generic(xk []u32, dst, src []byte) {
}
// Last round uses s-box directly and XORs to produce output.
s0 = u32(SBox1[t0>>24])<<24 | u32(SBox1[t3>>16&0xff])<<16 | u32(SBox1[t2>>8&0xff]<<8) | u32(SBox1[t1&u32(0xff)])
s1 = u32(SBox1[t1>>24])<<24 | u32(SBox1[t0>>16&0xff])<<16 | u32(SBox1[t3>>8&0xff]<<8) | u32(SBox1[t2&u32(0xff)])
s2 = u32(SBox1[t2>>24])<<24 | u32(SBox1[t1>>16&0xff])<<16 | u32(SBox1[t0>>8&0xff]<<8) | u32(SBox1[t3&u32(0xff)])
s3 = u32(SBox1[t3>>24])<<24 | u32(SBox1[t2>>16&0xff])<<16 | u32(SBox1[t1>>8&0xff]<<8) | u32(SBox1[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]
@ -145,12 +145,12 @@ fn decrypt_block_generic(xk []u32, dst, src []byte) {
binary.big_endian_put_u32(mut dst.slice(12, 16), s3)
}
// Apply SBox0 to each byte in w.
// Apply s_box0 to each byte in w.
fn subw(w u32) u32 {
return u32(SBox0[w>>24])<<24 |
u32(SBox0[w>>16&0xff]<<16) |
u32(SBox0[w>>8&0xff]<<8) |
u32(SBox0[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
@ -172,7 +172,7 @@ fn expand_key_generic(key []byte, enc mut []u32, dec mut []u32) {
for i < enc.len {
mut t := enc[i-1]
if i%nk == 0 {
t = subw(rotw(t)) ^ u32(PowX[i/nk-1]) << 24
t = subw(rotw(t)) ^ u32(pow_x[i/nk-1]) << 24
} else if nk > 6 && i%nk == 4 {
t = subw(t)
}
@ -192,7 +192,7 @@ fn expand_key_generic(key []byte, enc mut []u32, dec mut []u32) {
for j := 0; j < 4; j++ {
mut x := enc[ei+j]
if i > 0 && i+4 < n {
x = Td0[SBox0[x>>24]] ^ Td1[SBox0[x>>16&0xff]] ^ Td2[SBox0[x>>8&0xff]] ^ Td3[SBox0[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

@ -23,12 +23,12 @@ module aes
// Reducing mod poly corresponds to binary xor with poly every
// time a 0x100 bit appears.
const (
Poly = int(1<<8) | int(1<<4) | int(1<<3) | int(1<<1) | int(1<<0) // x⁸ + x⁴ + x³ + x + 1
poly = int(1<<8) | int(1<<4) | int(1<<3) | int(1<<1) | int(1<<0) // x⁸ + x⁴ + x³ + x + 1
)
// Powers of x mod poly in GF(2).
const (
PowX = [
pow_x = [
byte(0x01),
0x02,
0x04,
@ -50,7 +50,7 @@ const (
// FIPS-197 Figure 7. S-box substitution values in hexadecimal format.
const (
SBox0 = [
s_box0 = [
byte(0x63), 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
@ -72,7 +72,7 @@ const (
// FIPS-197 Figure 14. Inverse S-box substitution values in hexadecimal format.
const (
SBox1 = [
s_box1 = [
byte(0x52), 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
@ -95,7 +95,7 @@ const (
// Lookup tables for encryption.
const (
Te0 = [
te0 = [
0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b,
@ -129,7 +129,7 @@ const (
0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8,
0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
]
Te1 = [
te1 = [
0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676,
0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0,
@ -163,7 +163,7 @@ const (
0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868,
0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616,
]
Te2 = [
te2 = [
0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76,
0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0,
@ -197,7 +197,7 @@ const (
0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068,
0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16,
]
Te3 = [
te3 = [
0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec,
0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb,
@ -235,7 +235,7 @@ const (
// Lookup tables for decryption.
const (
Td0 = [
td0 = [
0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f,
0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6,
@ -269,7 +269,7 @@ const (
0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541,
0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
]
Td1 = [
td1 = [
0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3,
0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9,
@ -303,7 +303,7 @@ const (
0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95,
0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857,
]
Td2 = [
td2 = [
0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562,
0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3,
@ -337,7 +337,7 @@ const (
0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d,
0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8,
]
Td3 = [
td3 = [
0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5,
0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b,

View File

@ -1,23 +1,23 @@
module crypto
pub enum Hash {
MD4
MD5
SHA1
SHA224
SHA256
SHA384
SHA512
MD5SHA1
RIPEMD160
SHA3_224
SHA3_256
SHA3_384
SHA3_512
SHA512_224
SHA512_256
BLAKE2s_256
BLAKE2b_256
BLAKE2b_384
BLAKE2b_512
md4
md5
sha1
sha224
sha256
sha384
sha512
md5sha1
ripemd160
sha3_224
sha3_256
sha3_384
sha3_512
sha512_224
sha512_256
blake2s_256
blake2b_256
blake2b_384
blake2b_512
}

View File

@ -16,16 +16,16 @@ import encoding.binary
const (
// The size of an MD5 checksum in bytes.
Size = 16
size = 16
// The blocksize of MD5 in bytes.
BlockSize = 64
block_size = 64
)
const (
Init0 = 0x67452301
Init1 = 0xEFCDAB89
Init2 = 0x98BADCFE
Init3 = 0x10325476
init0 = 0x67452301
init1 = 0xEFCDAB89
init2 = 0x98BADCFE
init3 = 0x10325476
)
// Digest represents the partial evaluation of a checksum.
@ -39,11 +39,11 @@ mut:
fn (d mut Digest) reset() {
d.s = [u32(0)].repeat(4)
d.x = [byte(0)].repeat(BlockSize)
d.s[0] = u32(Init0)
d.s[1] = u32(Init1)
d.s[2] = u32(Init2)
d.s[3] = u32(Init3)
d.x = [byte(0)].repeat(block_size)
d.s[0] = u32(init0)
d.s[1] = u32(init1)
d.s[2] = u32(init2)
d.s[3] = u32(init3)
d.nx = 0
d.len = 0
}
@ -62,7 +62,7 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == BlockSize {
if d.nx == block_size {
block(mut d, d.x)
d.nx = 0
}
@ -72,8 +72,8 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
p = p.right(n)
}
}
if p.len >= BlockSize {
n := p.len &~ (BlockSize - 1)
if p.len >= block_size {
n := p.len &~ (block_size - 1)
block(mut d, p.left(n))
if n >= p.len {
p = []byte
@ -117,7 +117,7 @@ pub fn (d mut Digest) checksum() []byte {
panic('d.nx != 0')
}
digest := [byte(0)].repeat(Size)
digest := [byte(0)].repeat(size)
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest.right(4), d.s[1])
@ -139,8 +139,8 @@ fn block(dig mut Digest, p []byte) {
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 BlockSize }
pub fn (d &Digest) block_size() int { return block_size }
pub fn hexhash(s string) string { return sum(s.bytes()).hex() }

View File

@ -20,9 +20,9 @@ fn block_generic(dig mut Digest, p []byte) {
mut c := dig.s[2]
mut d := dig.s[3]
for i := 0; i <= p.len-BlockSize; i += BlockSize {
for i := 0; i <= p.len-block_size; i += block_size {
mut q := p.right(i)
q = q.left(BlockSize)
q = q.left(block_size)
// save current state
aa := a
bb := b

View File

@ -5,7 +5,7 @@
module rand
const (
ReadError = error('crypro.rand.read() error reading random bytes')
read_error = error('crypro.rand.read() error reading random bytes')
)
// NOTE: temp until we have []bytes(buff)

View File

@ -8,40 +8,33 @@ import math
#include <sys/syscall.h>
// const (
// SYS_getrandom = 278 // AArch65
// SYS_getrandom = 384 // ARM
// SYS_getrandom = 355 // x86
// SYS_getrandom = 318 // x86_64
// )
const (
ReadBatchSize = 256
read_batch_size = 256
)
pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed)
mut bytes_read := 0
// getrandom syscall wont block if requesting <= 256 bytes
if bytes_needed > ReadBatchSize {
no_batches := int(math.floor(f64(bytes_needed/ReadBatchSize)))
if bytes_needed > read_batch_size {
no_batches := int(math.floor(f64(bytes_needed/read_batch_size)))
for i:=0; i<no_batches; i++ {
if getrandom(ReadBatchSize, buffer+bytes_read) == -1 {
return ReadError
if getrandom(read_batch_size, buffer+bytes_read) == -1 {
return read_error
}
bytes_read += ReadBatchSize
bytes_read += read_batch_size
}
}
if getrandom(bytes_needed-bytes_read, buffer+bytes_read) == -1 {
return ReadError
return read_error
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}
fn getrandom(bytes_needed int, buffer voidptr) int {
if bytes_needed > ReadBatchSize {
panic('getrandom() dont request more thane $ReadBatchSize bytes at once.')
if bytes_needed > read_batch_size {
panic('getrandom() dont request more thane $read_batch_size bytes at once.')
}
return C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0)
}

View File

@ -12,7 +12,7 @@ pub fn read(bytes_needed int) ?[]byte {
mut buffer := malloc(bytes_needed)
status := C.SecRandomCopyBytes(0, bytes_needed, buffer)
if status != 0 {
return ReadError
return read_error
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}

View File

@ -18,7 +18,7 @@ pub fn read(bytes_needed int) ?[]byte {
// use BCRYPT_USE_SYSTEM_PREFERRED_RNG because we passed null as algo
status := C.BCryptGenRandom(0, buffer, bytes_needed, BCRYPT_USE_SYSTEM_PREFERRED_RNG)
if status != STATUS_SUCCESS {
return ReadError
return read_error
}
return c_array_to_bytes_tmp(bytes_needed, buffer)
}

View File

@ -16,18 +16,18 @@ import encoding.binary
const(
// The size of a SHA-1 checksum in bytes.
Size = 20
size = 20
// The blocksize of SHA-1 in bytes.
BlockSize = 64
block_size = 64
)
const (
Chunk = 64
Init0 = 0x67452301
Init1 = 0xEFCDAB89
Init2 = 0x98BADCFE
Init3 = 0x10325476
Init4 = 0xC3D2E1F0
chunk = 64
init0 = 0x67452301
init1 = 0xEFCDAB89
init2 = 0x98BADCFE
init3 = 0x10325476
init4 = 0xC3D2E1F0
)
// digest represents the partial evaluation of a checksum.
@ -40,13 +40,13 @@ mut:
}
fn (d mut Digest) reset() {
d.x = [byte(0)].repeat(Chunk)
d.x = [byte(0)].repeat(chunk)
d.h = [u32(0)].repeat(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(init0)
d.h[1] = u32(init1)
d.h[2] = u32(init2)
d.h[3] = u32(init3)
d.h[4] = u32(init4)
d.nx = 0
d.len = 0
}
@ -66,7 +66,7 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
if d.nx == chunk {
block(d, d.x)
d.nx = 0
}
@ -76,8 +76,8 @@ pub fn (d mut Digest) write(p_ []byte) ?int {
p = p.right(n)
}
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
if p.len >= chunk {
n := p.len &~ (chunk - 1)
block(d, p.left(n))
if n >= p.len {
p = []byte
@ -120,7 +120,7 @@ fn (d mut Digest) checksum() []byte {
binary.big_endian_put_u64(mut tmp, len)
d.write(tmp.left(8))
mut digest := [byte(0)].repeat(Size)
mut digest := [byte(0)].repeat(size)
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest.right(4), d.h[1])
@ -144,8 +144,8 @@ fn block(dig &Digest, p []byte) {
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 BlockSize }
pub fn (d &Digest) block_size() int { return block_size }
pub fn hexhash(s string) string { return sum(s.bytes()).hex() }

View File

@ -11,10 +11,10 @@ module sha1
import math.bits
const (
_K0 = 0x5A827999
_K1 = 0x6ED9EBA1
_K2 = 0x8F1BBCDC
_K3 = 0xCA62C1D6
_k0 = 0x5A827999
_k1 = 0x6ED9EBA1
_k2 = 0x8F1BBCDC
_k3 = 0xCA62C1D6
)
fn block_generic(dig mut Digest, p_ []byte) {
@ -25,7 +25,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
mut h2 := dig.h[2]
mut h3 := dig.h[3]
mut h4 := dig.h[4]
for p.len >= Chunk {
for p.len >= chunk {
// Can interlace the computation of w with the
// rounds below if needed for speed.
for i := 0; i < 16; i++ {
@ -41,11 +41,11 @@ fn block_generic(dig mut Digest, p_ []byte) {
// Each of the four 20-iteration rounds
// differs only in the computation of f and
// the choice of K (_K0, _K1, etc).
// 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)
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)
@ -57,7 +57,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = tmp<<1 | u32(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(_k0)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@ -69,7 +69,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = tmp<<1 | u32(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(_k1)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@ -81,7 +81,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = tmp<<1 | u32(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(_k2)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@ -93,7 +93,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
w[i&0xf] = tmp<<1 | u32(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(_k3)
e = d
d = c
c = bits.rotate_left_32(b, 30)
@ -108,10 +108,10 @@ fn block_generic(dig mut Digest, p_ []byte) {
h3 += d
h4 += e
if Chunk >= p.len {
if chunk >= p.len {
p = []byte
} else {
p = p.right(Chunk)
p = p.right(chunk)
}
}

View File

@ -14,31 +14,31 @@ import encoding.binary
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.
BlockSize = 64
block_size = 64
)
const (
Chunk = 64
Init0 = 0x6A09E667
Init1 = 0xBB67AE85
Init2 = 0x3C6EF372
Init3 = 0xA54FF53A
Init4 = 0x510E527F
Init5 = 0x9B05688C
Init6 = 0x1F83D9AB
Init7 = 0x5BE0CD19
Init0_224 = 0xC1059ED8
Init1_224 = 0x367CD507
Init2_224 = 0x3070DD17
Init3_224 = 0xF70E5939
Init4_224 = 0xFFC00B31
Init5_224 = 0x68581511
Init6_224 = 0x64F98FA7
Init7_224 = 0xBEFA4FA4
chunk = 64
init0 = 0x6A09E667
init1 = 0xBB67AE85
init2 = 0x3C6EF372
init3 = 0xA54FF53A
init4 = 0x510E527F
init5 = 0x9B05688C
init6 = 0x1F83D9AB
init7 = 0x5BE0CD19
init0_224 = 0xC1059ED8
init1_224 = 0x367CD507
init2_224 = 0x3070DD17
init3_224 = 0xF70E5939
init4_224 = 0xFFC00B31
init5_224 = 0x68581511
init6_224 = 0x64F98FA7
init7_224 = 0xBEFA4FA4
)
// digest represents the partial evaluation of a checksum.
@ -53,25 +53,25 @@ mut:
fn (d mut Digest) reset() {
d.h = [u32(0)].repeat(8)
d.x = [byte(0)].repeat(Chunk)
d.x = [byte(0)].repeat(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(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)
} 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(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.nx = 0
d.len = 0
@ -99,7 +99,7 @@ fn (d mut Digest) write(p_ []byte) ?int {
if d.nx > 0 {
n := copy(d.x.right(d.nx), p)
d.nx += n
if d.nx == Chunk {
if d.nx == chunk {
block(mut d, d.x)
d.nx = 0
}
@ -109,8 +109,8 @@ fn (d mut Digest) write(p_ []byte) ?int {
p = p.right(n)
}
}
if p.len >= Chunk {
n := p.len &~ (Chunk - 1)
if p.len >= chunk {
n := p.len &~ (chunk - 1)
block(mut d, p.left(n))
if n >= p.len {
p = []byte
@ -130,7 +130,7 @@ fn (d &Digest) sum(b_in []byte) []byte {
hash := d0.checksum()
mut b_out := b_in.clone()
if d0.is224 {
for b in hash.left(Size224) {
for b in hash.left(size224) {
b_out << b
}
} else {
@ -161,7 +161,7 @@ fn (d mut Digest) checksum() []byte {
panic('d.nx != 0')
}
digest := [byte(0)].repeat(Size)
digest := [byte(0)].repeat(size)
binary.big_endian_put_u32(mut digest, d.h[0])
binary.big_endian_put_u32(mut digest.right(4), d.h[1])
@ -194,8 +194,8 @@ pub fn sum224(data []byte) []byte {
mut d := new224()
d.write(data)
sum := d.checksum()
mut sum224 := [byte(0)].repeat(Size224)
copy(sum224, sum.left(Size224))
mut sum224 := [byte(0)].repeat(size224)
copy(sum224, sum.left(size224))
return sum224
}
@ -207,12 +207,12 @@ fn block(dig mut Digest, p []byte) {
pub fn (d &Digest) size() int {
if !d.is224 {
return Size
return size
}
return Size224
return size224
}
pub fn (d &Digest) block_size() int { return BlockSize }
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() }

View File

@ -12,7 +12,7 @@ module sha256
import math.bits
const (
_K = [
_k = [
0x428a2f98,
0x71374491,
0xb5c0fbcf,
@ -94,7 +94,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
mut h6 := dig.h[6]
mut h7 := dig.h[7]
for p.len >= Chunk {
for p.len >= chunk {
// Can interlace the computation of w with the
// rounds below if needed for speed.
for i := 0; i < 16; i++ {
@ -119,7 +119,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
mut h := h7
for i := 0; i < 64; i++ {
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 + ((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
@ -141,10 +141,10 @@ fn block_generic(dig mut Digest, p_ []byte) {
h6 += g
h7 += h
if Chunk >= p.len {
if chunk >= p.len {
p = []byte
} else {
p = p.right(Chunk)
p = p.right(chunk)
}
}

View File

@ -16,53 +16,53 @@ import (
)
const (
// 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
// Size256 is the size, in bytes, of a SHA-512/256 checksum.
Size256 = 32
// Size384 is the size, in bytes, of a SHA-384 checksum.
Size384 = 48
// BlockSize is the block size, in bytes, of the SHA-512/224,
// 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
// size256 is the size, in bytes, of a SHA-512/256 checksum.
size256 = 32
// size384 is the size, in bytes, of a SHA-384 checksum.
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.
BlockSize = 128
block_size = 128
)
const (
Chunk = 128
Init0 = 0x6a09e667f3bcc908
Init1 = 0xbb67ae8584caa73b
Init2 = 0x3c6ef372fe94f82b
Init3 = 0xa54ff53a5f1d36f1
Init4 = 0x510e527fade682d1
Init5 = 0x9b05688c2b3e6c1f
Init6 = 0x1f83d9abfb41bd6b
Init7 = 0x5be0cd19137e2179
Init0_224 = 0x8c3d37c819544da2
Init1_224 = 0x73e1996689dcd4d6
Init2_224 = 0x1dfab7ae32ff9c82
Init3_224 = 0x679dd514582f9fcf
Init4_224 = 0x0f6d2b697bd44da8
Init5_224 = 0x77e36f7304c48942
Init6_224 = 0x3f9d85a86a1d36c8
Init7_224 = 0x1112e6ad91d692a1
Init0_256 = 0x22312194fc2bf72c
Init1_256 = 0x9f555fa3c84c64c2
Init2_256 = 0x2393b86b6f53b151
Init3_256 = 0x963877195940eabd
Init4_256 = 0x96283ee2a88effe3
Init5_256 = 0xbe5e1e2553863992
Init6_256 = 0x2b0199fc2c85b8aa
Init7_256 = 0x0eb72ddc81c52ca2
Init0_384 = 0xcbbb9d5dc1059ed8
Init1_384 = 0x629a292a367cd507
Init2_384 = 0x9159015a3070dd17
Init3_384 = 0x152fecd8f70e5939
Init4_384 = 0x67332667ffc00b31
Init5_384 = 0x8eb44a8768581511
Init6_384 = 0xdb0c2e0d64f98fa7
Init7_384 = 0x47b5481dbefa4fa4
init0 = 0x6a09e667f3bcc908
init1 = 0xbb67ae8584caa73b
init2 = 0x3c6ef372fe94f82b
init3 = 0xa54ff53a5f1d36f1
init4 = 0x510e527fade682d1
init5 = 0x9b05688c2b3e6c1f
init6 = 0x1f83d9abfb41bd6b
init7 = 0x5be0cd19137e2179
init0_224 = 0x8c3d37c819544da2
init1_224 = 0x73e1996689dcd4d6
init2_224 = 0x1dfab7ae32ff9c82
init3_224 = 0x679dd514582f9fcf
init4_224 = 0x0f6d2b697bd44da8
init5_224 = 0x77e36f7304c48942
init6_224 = 0x3f9d85a86a1d36c8
init7_224 = 0x1112e6ad91d692a1
init0_256 = 0x22312194fc2bf72c
init1_256 = 0x9f555fa3c84c64c2
init2_256 = 0x2393b86b6f53b151
init3_256 = 0x963877195940eabd
init4_256 = 0x96283ee2a88effe3
init5_256 = 0xbe5e1e2553863992
init6_256 = 0x2b0199fc2c85b8aa
init7_256 = 0x0eb72ddc81c52ca2
init0_384 = 0xcbbb9d5dc1059ed8
init1_384 = 0x629a292a367cd507
init2_384 = 0x9159015a3070dd17
init3_384 = 0x152fecd8f70e5939
init4_384 = 0x67332667ffc00b31
init5_384 = 0x8eb44a8768581511
init6_384 = 0xdb0c2e0d64f98fa7
init7_384 = 0x47b5481dbefa4fa4
)
// digest represents the partial evaluation of a checksum.
@ -79,42 +79,42 @@ fn (d mut Digest) reset() {
d.h = [u64(0)].repeat(8)
d.x = [byte(0)].repeat(Chunk)
switch d.function {
case crypto.Hash.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
case crypto.Hash.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
case crypto.Hash.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
case crypto.Hash.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
case crypto.Hash.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
case crypto.Hash.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
default:
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] = 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.nx = 0
d.len = 0
@ -129,22 +129,22 @@ fn new_digest(hash crypto.Hash) &Digest {
// new returns a new Digest (implementing hash.Hash) computing the SHA-512 checksum.
pub fn new() &Digest {
return new_digest(crypto.Hash.SHA512)
return new_digest(crypto.Hash.sha512)
}
// new512_224 returns a new Digest (implementing hash.Hash) computing the SHA-512/224 checksum.
fn new512_224() &Digest {
return new_digest(crypto.Hash.SHA512_224)
return new_digest(crypto.Hash.sha512_224)
}
// new512_256 returns a new Digest (implementing hash.Hash) computing the SHA-512/256 checksum.
fn new512_256() &Digest {
return new_digest(crypto.Hash.SHA512_256)
return new_digest(crypto.Hash.sha512_256)
}
// new384 returns a new Digest (implementing hash.Hash) computing the SHA-384 checksum.
fn new384() &Digest {
return new_digest(crypto.Hash.SHA384)
return new_digest(crypto.Hash.sha384)
}
fn (d mut Digest) write(p_ []byte) ?int {
@ -185,16 +185,16 @@ fn (d mut Digest) sum(b_in []byte) []byte {
hash := d0.checksum()
mut b_out := b_in.clone()
switch d0.function {
case crypto.Hash.SHA384:
for b in hash.left(Size384) {
case crypto.Hash.sha384:
for b in hash.left(size384) {
b_out << b
}
case crypto.Hash.SHA512_224:
for b in hash.left(Size224) {
case crypto.Hash.sha512_224:
for b in hash.left(size224) {
b_out << b
}
case crypto.Hash.SHA512_256:
for b in hash.left(Size256) {
case crypto.Hash.sha512_256:
for b in hash.left(size256) {
b_out << b
}
default:
@ -228,7 +228,7 @@ fn (d mut Digest) checksum() []byte {
panic('d.nx != 0')
}
mut digest := [byte(0)].repeat(Size)
mut digest := [byte(0)].repeat(size)
binary.big_endian_put_u64(mut digest, d.h[0])
binary.big_endian_put_u64(mut digest.right(8), d.h[1])
@ -236,7 +236,7 @@ fn (d mut Digest) checksum() []byte {
binary.big_endian_put_u64(mut digest.right(24), d.h[3])
binary.big_endian_put_u64(mut digest.right(32), d.h[4])
binary.big_endian_put_u64(mut digest.right(40), d.h[5])
if d.function != crypto.Hash.SHA384 {
if d.function != crypto.Hash.sha384 {
binary.big_endian_put_u64(mut digest.right(48), d.h[6])
binary.big_endian_put_u64(mut digest.right(56), d.h[7])
}
@ -246,38 +246,38 @@ fn (d mut Digest) checksum() []byte {
// sum512 returns the SHA512 checksum of the data.
pub fn sum512(data []byte) []byte {
mut d := new_digest(crypto.Hash.SHA512)
mut d := new_digest(crypto.Hash.sha512)
d.write(data)
return d.checksum()
}
// sum384 returns the SHA384 checksum of the data.
pub fn sum384(data []byte) []byte {
mut d := new_digest(crypto.Hash.SHA384)
mut d := new_digest(crypto.Hash.sha384)
d.write(data)
sum := d.checksum()
mut sum384 := [byte(0)].repeat(Size384)
copy(sum384, sum.left(Size384))
mut sum384 := [byte(0)].repeat(size384)
copy(sum384, sum.left(size384))
return sum384
}
// sum512_224 returns the Sum512/224 checksum of the data.
pub fn sum512_224(data []byte) []byte {
mut d := new_digest(crypto.Hash.SHA512_224)
mut d := new_digest(crypto.Hash.sha512_224)
d.write(data)
sum := d.checksum()
mut sum224 := [byte(0)].repeat(Size224)
copy(sum224, sum.left(Size224))
mut sum224 := [byte(0)].repeat(size224)
copy(sum224, sum.left(size224))
return sum224
}
// Sum512_256 returns the Sum512/256 checksum of the data.
pub fn sum512_256(data []byte) []byte {
mut d := new_digest(crypto.Hash.SHA512_256)
mut d := new_digest(crypto.Hash.sha512_256)
d.write(data)
sum := d.checksum()
mut sum256 := [byte(0)].repeat(Size256)
copy(sum256, sum.left(Size256))
mut sum256 := [byte(0)].repeat(size256)
copy(sum256, sum.left(size256))
return sum256
}
@ -289,18 +289,18 @@ fn block(dig mut Digest, p []byte) {
pub fn (d &Digest) size() int {
switch d.function {
case crypto.Hash.SHA512_224:
return Size224
case crypto.Hash.SHA512_256:
return Size256
case crypto.Hash.SHA384:
return Size384
case crypto.Hash.sha512_224:
return size224
case crypto.Hash.sha512_256:
return size256
case crypto.Hash.sha384:
return size384
default:
return Size
return size
}
}
pub fn (d &Digest) block_size() int { return BlockSize }
pub fn (d &Digest) block_size() int { return block_size }
pub fn hexhash(s string) string { return sum512(s.bytes()).hex() }
pub fn hexhash_384(s string) string { return sum384(s.bytes()).hex() }

View File

@ -12,7 +12,7 @@ module sha512
import math.bits
const(
_K = [
_k = [
0x428a2f98d728ae22,
0x7137449123ef65cd,
0xb5c0fbcfec4d3b2f,
@ -135,7 +135,7 @@ fn block_generic(dig mut Digest, p_ []byte) {
mut h := h7
for i := 0; i < 80; i++ {
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 + (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

View File

@ -8,14 +8,14 @@ module crc32
// polynomials
const (
IEEE = 0xedb88320
Castagnoli = 0x82f63b78
Koopman = 0xeb31d82e
ieee = 0xedb88320
castagnoli = 0x82f63b78
koopman = 0xeb31d82e
)
// The size of a CRC-32 checksum in bytes.
const (
Size = 4
size = 4
)
struct Crc32 {
@ -56,8 +56,8 @@ pub fn new(poly int) &Crc32 {
return c
}
// calculate crc32 using IEEE
// calculate crc32 using ieee
pub fn sum(b []byte) u32 {
mut c := new(IEEE)
mut c := new(ieee)
return c.sum32(b)
}

View File

@ -7,7 +7,7 @@ fn test_hash_crc32() {
assert sum1.hex() == '0x483f8cf0'
c := crc32.new(crc32.IEEE)
c := crc32.new(crc32.ieee)
b2 := 'testing crc32 again'.bytes()
sum2 := c.checksum(b2)
assert sum2 == u32(1420327025)