File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 48
48
is224 bool // mark if this digest is SHA-224
49
49
}
50
50
51
- fn (mut d Digest) reset () {
51
+ // free the resources taken by the Digest `d`
52
+ [unsafe ]
53
+ pub fn (mut d Digest) free () {
54
+ $if prealloc {
55
+ return
56
+ }
57
+ unsafe {
58
+ d.x.free ()
59
+ d.h.free ()
60
+ }
61
+ }
62
+
63
+ fn (mut d Digest) init () {
52
64
d.h = []u32 {len: (8 )}
53
65
d.x = []u8 {len: sha256 .chunk}
66
+ }
67
+
68
+ // reset the state of the Digest `d`
69
+ pub fn (mut d Digest) reset () {
54
70
if ! d.is224 {
55
71
d.h[0 ] = u32 (sha256 .init0 )
56
72
d.h[1 ] = u32 (sha256 .init1 )
@@ -77,6 +93,7 @@ fn (mut d Digest) reset() {
77
93
// new returns a new Digest (implementing hash.Hash) computing the SHA256 checksum.
78
94
pub fn new () & Digest {
79
95
mut d := & Digest{}
96
+ d.init ()
80
97
d.reset ()
81
98
return d
82
99
}
@@ -85,6 +102,7 @@ pub fn new() &Digest {
85
102
pub fn new224 () & Digest {
86
103
mut d := & Digest{}
87
104
d.is224 = true
105
+ d.init ()
88
106
d.reset ()
89
107
return d
90
108
}
Original file line number Diff line number Diff line change @@ -14,3 +14,15 @@ fn test_crypto_sha256_writer() {
14
14
sum := digest.sum ([])
15
15
assert sum.hex () == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'
16
16
}
17
+
18
+ fn test_crypto_sha256_writer_reset () {
19
+ mut digest := sha256 .new ()
20
+ digest.write ('This is a' .bytes ()) or { assert false }
21
+ digest.write (' sha256 checksum.' .bytes ()) or { assert false }
22
+ _ = digest.sum ([])
23
+ digest.reset ()
24
+ digest.write ('This is a' .bytes ()) or { assert false }
25
+ digest.write (' sha256 checksum.' .bytes ()) or { assert false }
26
+ sum := digest.sum ([])
27
+ assert sum.hex () == 'dc7163299659529eae29683eb1ffec50d6c8fc7275ecb10c145fde0e125b8727'
28
+ }
You can’t perform that action at this time.
0 commit comments