mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
crypto: add .free() and .reset() methods to reduce memory leaks with -autofree (#16992)
* Fix unsafe pointer I was compile vab with '-prod' and it was needed to fix and it is of course warning * Add files via upload * reduce memory leak s sha512 * add method .free() and .reset() for some
This commit is contained in:
@@ -33,9 +33,23 @@ mut:
|
||||
len u64
|
||||
}
|
||||
|
||||
fn (mut d Digest) reset() {
|
||||
// free the resources taken by the Digest `d`
|
||||
[unsafe]
|
||||
pub fn (mut d Digest) free() {
|
||||
$if prealloc {
|
||||
return
|
||||
}
|
||||
unsafe { d.x.free() }
|
||||
}
|
||||
|
||||
fn (mut d Digest) init() {
|
||||
d.s = []u32{len: (4)}
|
||||
d.x = []u8{len: md5.block_size}
|
||||
d.reset()
|
||||
}
|
||||
|
||||
// reset the state of the Digest `d`
|
||||
pub fn (mut d Digest) reset() {
|
||||
d.s[0] = u32(md5.init0)
|
||||
d.s[1] = u32(md5.init1)
|
||||
d.s[2] = u32(md5.init2)
|
||||
@@ -47,7 +61,7 @@ fn (mut d Digest) reset() {
|
||||
// new returns a new Digest (implementing hash.Hash) computing the MD5 checksum.
|
||||
pub fn new() &Digest {
|
||||
mut d := &Digest{}
|
||||
d.reset()
|
||||
d.init()
|
||||
return d
|
||||
}
|
||||
|
||||
|
||||
@@ -6,3 +6,23 @@ import crypto.md5
|
||||
fn test_crypto_md5() {
|
||||
assert md5.sum('this is a md5 checksum.'.bytes()).hex() == '6fb421ff99036547655984da12973431'
|
||||
}
|
||||
|
||||
fn test_crypto_md5_writer() {
|
||||
mut digest := md5.new()
|
||||
digest.write('this is a'.bytes()) or { assert false }
|
||||
digest.write(' md5 checksum.'.bytes()) or { assert false }
|
||||
sum := digest.sum([])
|
||||
assert sum.hex() == '6fb421ff99036547655984da12973431'
|
||||
}
|
||||
|
||||
fn test_crypto_md5_writer_reset() {
|
||||
mut digest := md5.new()
|
||||
digest.write('this is a'.bytes()) or { assert false }
|
||||
digest.write(' md5 checksum.'.bytes()) or { assert false }
|
||||
_ = digest.sum([])
|
||||
digest.reset()
|
||||
digest.write('this is a'.bytes()) or { assert false }
|
||||
digest.write(' md5 checksum.'.bytes()) or { assert false }
|
||||
sum := digest.sum([])
|
||||
assert sum.hex() == '6fb421ff99036547655984da12973431'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user