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

crypto: make digest implement io.Writer (#8975)

This commit is contained in:
zakuro 2021-02-26 15:24:47 +09:00 committed by GitHub
parent d81b6e7805
commit bc0507590e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 22 deletions

View File

@ -52,7 +52,7 @@ pub fn new() &Digest {
} }
// write writes the contents of `p_` to the internal hash representation. // write writes the contents of `p_` to the internal hash representation.
pub fn (mut d Digest) write(p_ []byte) int { pub fn (mut d Digest) write(p_ []byte) ?int {
unsafe { unsafe {
mut p := p_ mut p := p_
nn := p.len nn := p.len
@ -110,7 +110,7 @@ pub fn (mut d Digest) checksum() []byte {
tmp[0] = 0x80 tmp[0] = 0x80
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]) or { panic(err) }
// 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 {
@ -127,7 +127,7 @@ pub fn (mut d Digest) checksum() []byte {
// sum returns the MD5 checksum of the data. // sum returns the MD5 checksum of the data.
pub fn sum(data []byte) []byte { pub fn sum(data []byte) []byte {
mut d := new() mut d := new()
d.write(data) d.write(data) or { panic(err) }
return d.checksum() return d.checksum()
} }

View File

@ -56,7 +56,7 @@ pub fn new() &Digest {
// write writes the contents of `p_` to the internal hash representation. // write writes the contents of `p_` to the internal hash representation.
[manualfree] [manualfree]
pub fn (mut d Digest) write(p_ []byte) int { pub fn (mut d Digest) write(p_ []byte) ?int {
nn := p_.len nn := p_.len
unsafe { unsafe {
mut p := p_ mut p := p_
@ -109,14 +109,14 @@ fn (mut d Digest) checksum() []byte {
mut tmp := []byte{len: (64)} mut tmp := []byte{len: (64)}
tmp[0] = 0x80 tmp[0] = 0x80
if int(len) % 64 < 56 { if int(len) % 64 < 56 {
d.write(tmp[..56 - int(len) % 64]) d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
} else { } else {
d.write(tmp[..64 + 56 - int(len) % 64]) d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
} }
// Length in bits. // Length in bits.
len <<= 3 len <<= 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]) or { panic(err) }
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])
@ -129,7 +129,7 @@ fn (mut d Digest) checksum() []byte {
// sum returns the SHA-1 checksum of the bytes passed in `data`. // sum returns the SHA-1 checksum of the bytes passed in `data`.
pub fn sum(data []byte) []byte { pub fn sum(data []byte) []byte {
mut d := new() mut d := new()
d.write(data) d.write(data) or { panic(err) }
return d.checksum() return d.checksum()
} }

View File

@ -90,7 +90,7 @@ pub fn new224() &Digest {
} }
// write writes the contents of `p_` to the internal hash representation. // write writes the contents of `p_` to the internal hash representation.
fn (mut d Digest) write(p_ []byte) int { fn (mut d Digest) write(p_ []byte) ?int {
unsafe { unsafe {
mut p := p_ mut p := p_
nn := p.len nn := p.len
@ -147,14 +147,14 @@ fn (mut d Digest) checksum() []byte {
mut tmp := []byte{len: (64)} mut tmp := []byte{len: (64)}
tmp[0] = 0x80 tmp[0] = 0x80
if int(len) % 64 < 56 { if int(len) % 64 < 56 {
d.write(tmp[..56 - int(len) % 64]) d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
} else { } else {
d.write(tmp[..64 + 56 - int(len) % 64]) d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
} }
// 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]) or { panic(err) }
if d.nx != 0 { if d.nx != 0 {
panic('d.nx != 0') panic('d.nx != 0')
} }
@ -181,14 +181,14 @@ pub fn sum(data []byte) []byte {
// sum256 returns the SHA256 checksum of the data. // sum256 returns the SHA256 checksum of the data.
pub fn sum256(data []byte) []byte { pub fn sum256(data []byte) []byte {
mut d := new() mut d := new()
d.write(data) d.write(data) or { panic(err) }
return d.checksum() return d.checksum()
} }
// sum224 returns the SHA224 checksum of the data. // sum224 returns the SHA224 checksum of the data.
pub fn sum224(data []byte) []byte { pub fn sum224(data []byte) []byte {
mut d := new224() mut d := new224()
d.write(data) d.write(data) or { panic(err) }
sum := d.checksum() sum := d.checksum()
sum224 := []byte{len: (size224)} sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224]) copy(sum224, sum[..size224])

View File

@ -149,7 +149,7 @@ fn new384() &Digest {
} }
// write writes the contents of `p_` to the internal hash representation. // write writes the contents of `p_` to the internal hash representation.
fn (mut d Digest) write(p_ []byte) int { fn (mut d Digest) write(p_ []byte) ?int {
unsafe { unsafe {
mut p := p_ mut p := p_
nn := p.len nn := p.len
@ -219,15 +219,15 @@ fn (mut d Digest) checksum() []byte {
mut tmp := []byte{len: (128)} mut tmp := []byte{len: (128)}
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]) or { panic(err) }
} else { } else {
d.write(tmp[..128 + 112 - int(len) % 128]) d.write(tmp[..128 + 112 - int(len) % 128]) or { panic(err) }
} }
// Length in bits. // Length in bits.
len <<= u64(3) len <<= u64(3)
binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64 binary.big_endian_put_u64(mut tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(mut tmp[8..], len) binary.big_endian_put_u64(mut tmp[8..], len)
d.write(tmp[..16]) d.write(tmp[..16]) or { panic(err) }
if d.nx != 0 { if d.nx != 0 {
panic('d.nx != 0') panic('d.nx != 0')
} }
@ -248,14 +248,14 @@ fn (mut d Digest) checksum() []byte {
// sum512 returns the SHA512 checksum of the data. // sum512 returns the SHA512 checksum of the data.
pub fn sum512(data []byte) []byte { pub fn sum512(data []byte) []byte {
mut d := new_digest(.sha512) mut d := new_digest(.sha512)
d.write(data) d.write(data) or { panic(err) }
return d.checksum() return d.checksum()
} }
// sum384 returns the SHA384 checksum of the data. // sum384 returns the SHA384 checksum of the data.
pub fn sum384(data []byte) []byte { pub fn sum384(data []byte) []byte {
mut d := new_digest(.sha384) mut d := new_digest(.sha384)
d.write(data) d.write(data) or { panic(err) }
sum := d.checksum() sum := d.checksum()
sum384 := []byte{len: (size384)} sum384 := []byte{len: (size384)}
copy(sum384, sum[..size384]) copy(sum384, sum[..size384])
@ -265,7 +265,7 @@ pub fn sum384(data []byte) []byte {
// sum512_224 returns the Sum512/224 checksum of the data. // sum512_224 returns the Sum512/224 checksum of the data.
pub fn sum512_224(data []byte) []byte { pub fn sum512_224(data []byte) []byte {
mut d := new_digest(.sha512_224) mut d := new_digest(.sha512_224)
d.write(data) d.write(data) or { panic(err) }
sum := d.checksum() sum := d.checksum()
sum224 := []byte{len: (size224)} sum224 := []byte{len: (size224)}
copy(sum224, sum[..size224]) copy(sum224, sum[..size224])
@ -275,7 +275,7 @@ pub fn sum512_224(data []byte) []byte {
// sum512_256 returns the Sum512/256 checksum of the data. // sum512_256 returns the Sum512/256 checksum of the data.
pub fn sum512_256(data []byte) []byte { pub fn sum512_256(data []byte) []byte {
mut d := new_digest(.sha512_256) mut d := new_digest(.sha512_256)
d.write(data) d.write(data) or { panic(err) }
sum := d.checksum() sum := d.checksum()
sum256 := []byte{len: (size256)} sum256 := []byte{len: (size256)}
copy(sum256, sum[..size256]) copy(sum256, sum[..size256])