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
4 changed files with 22 additions and 22 deletions

View File

@@ -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()
}