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:
parent
d81b6e7805
commit
bc0507590e
@ -52,7 +52,7 @@ pub fn new() &Digest {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
mut p := p_
|
||||
nn := p.len
|
||||
@ -110,7 +110,7 @@ pub fn (mut d Digest) checksum() []byte {
|
||||
tmp[0] = 0x80
|
||||
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
|
||||
d.write(tmp[..1 + pad + 8])
|
||||
d.write(tmp[..1 + pad + 8]) or { panic(err) }
|
||||
// The previous write ensures that a whole number of
|
||||
// blocks (i.e. a multiple of 64 bytes) have been hashed.
|
||||
if d.nx != 0 {
|
||||
@ -127,7 +127,7 @@ pub fn (mut d Digest) checksum() []byte {
|
||||
// sum returns the MD5 checksum of the data.
|
||||
pub fn sum(data []byte) []byte {
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,7 @@ pub fn new() &Digest {
|
||||
|
||||
// write writes the contents of `p_` to the internal hash representation.
|
||||
[manualfree]
|
||||
pub fn (mut d Digest) write(p_ []byte) int {
|
||||
pub fn (mut d Digest) write(p_ []byte) ?int {
|
||||
nn := p_.len
|
||||
unsafe {
|
||||
mut p := p_
|
||||
@ -109,14 +109,14 @@ fn (mut d Digest) checksum() []byte {
|
||||
mut tmp := []byte{len: (64)}
|
||||
tmp[0] = 0x80
|
||||
if int(len) % 64 < 56 {
|
||||
d.write(tmp[..56 - int(len) % 64])
|
||||
d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
|
||||
} else {
|
||||
d.write(tmp[..64 + 56 - int(len) % 64])
|
||||
d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
|
||||
}
|
||||
// Length in bits.
|
||||
len <<= 3
|
||||
binary.big_endian_put_u64(mut tmp, len)
|
||||
d.write(tmp[..8])
|
||||
d.write(tmp[..8]) or { panic(err) }
|
||||
mut digest := []byte{len: (size)}
|
||||
binary.big_endian_put_u32(mut digest, d.h[0])
|
||||
binary.big_endian_put_u32(mut digest[4..], d.h[1])
|
||||
@ -129,7 +129,7 @@ fn (mut d Digest) checksum() []byte {
|
||||
// sum returns the SHA-1 checksum of the bytes passed in `data`.
|
||||
pub fn sum(data []byte) []byte {
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ pub fn new224() &Digest {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
mut p := p_
|
||||
nn := p.len
|
||||
@ -147,14 +147,14 @@ fn (mut d Digest) checksum() []byte {
|
||||
mut tmp := []byte{len: (64)}
|
||||
tmp[0] = 0x80
|
||||
if int(len) % 64 < 56 {
|
||||
d.write(tmp[..56 - int(len) % 64])
|
||||
d.write(tmp[..56 - int(len) % 64]) or { panic(err) }
|
||||
} else {
|
||||
d.write(tmp[..64 + 56 - int(len) % 64])
|
||||
d.write(tmp[..64 + 56 - int(len) % 64]) or { panic(err) }
|
||||
}
|
||||
// Length in bits.
|
||||
len <<= u64(3)
|
||||
binary.big_endian_put_u64(mut tmp, len)
|
||||
d.write(tmp[..8])
|
||||
d.write(tmp[..8]) or { panic(err) }
|
||||
if 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.
|
||||
pub fn sum256(data []byte) []byte {
|
||||
mut d := new()
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
// sum224 returns the SHA224 checksum of the data.
|
||||
pub fn sum224(data []byte) []byte {
|
||||
mut d := new224()
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum()
|
||||
sum224 := []byte{len: (size224)}
|
||||
copy(sum224, sum[..size224])
|
||||
|
@ -149,7 +149,7 @@ fn new384() &Digest {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
mut p := p_
|
||||
nn := p.len
|
||||
@ -219,15 +219,15 @@ fn (mut d Digest) checksum() []byte {
|
||||
mut tmp := []byte{len: (128)}
|
||||
tmp[0] = 0x80
|
||||
if int(len) % 128 < 112 {
|
||||
d.write(tmp[..112 - int(len) % 128])
|
||||
d.write(tmp[..112 - int(len) % 128]) or { panic(err) }
|
||||
} else {
|
||||
d.write(tmp[..128 + 112 - int(len) % 128])
|
||||
d.write(tmp[..128 + 112 - int(len) % 128]) or { panic(err) }
|
||||
}
|
||||
// Length in bits.
|
||||
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[8..], len)
|
||||
d.write(tmp[..16])
|
||||
d.write(tmp[..16]) or { panic(err) }
|
||||
if 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.
|
||||
pub fn sum512(data []byte) []byte {
|
||||
mut d := new_digest(.sha512)
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum()
|
||||
}
|
||||
|
||||
// sum384 returns the SHA384 checksum of the data.
|
||||
pub fn sum384(data []byte) []byte {
|
||||
mut d := new_digest(.sha384)
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum()
|
||||
sum384 := []byte{len: (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.
|
||||
pub fn sum512_224(data []byte) []byte {
|
||||
mut d := new_digest(.sha512_224)
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum()
|
||||
sum224 := []byte{len: (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.
|
||||
pub fn sum512_256(data []byte) []byte {
|
||||
mut d := new_digest(.sha512_256)
|
||||
d.write(data)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum()
|
||||
sum256 := []byte{len: (size256)}
|
||||
copy(sum256, sum[..size256])
|
||||
|
Loading…
Reference in New Issue
Block a user