@@ -2,6 +2,8 @@ module ecdsa
22
33import encoding.hex
44import crypto.pem
5+ import crypto.sha1
6+ import crypto.sha512
57
68// This material wss generated with https://emn178.github.io/online-tools/ecdsa/key-generator
79// with curve SECG secp384r1 aka NIST P-384
@@ -126,3 +128,68 @@ UHhnmmVRraSwrVkPdYIeXhH/Ob4+8OLcwrQBMv4RXsD1GVFsgkvEYDTEb/vnMA==
126128 return
127129 }
128130}
131+
132+ fn test_key_signing_verifying_with_custom_hash () ! {
133+ // privatekey_sample was P-384 key
134+ pvkey := privkey_from_string (privatekey_sample)!
135+ // public key part
136+ pbkey := pvkey.public_key ()!
137+ pbk := pubkey_from_string (public_key_sample)!
138+
139+ // lets sign the message with default hash, ie, sha384
140+ signature := pvkey.sign (message_tobe_signed)!
141+ verified := pbkey.verify (message_tobe_signed, signature)!
142+ assert verified == true
143+
144+ // Use the bigger custom hash
145+ opt0 := SignerOpts{
146+ hash_config: .with_custom_hash
147+ allow_custom_hash: true
148+ custom_hash: sha512 .new ()
149+ }
150+ // online-generated signature with sha512 digest with the same params from https://emn178.github.io/online-tools/ecdsa/sign/
151+ online_sign0 := hex.decode ('3066023100b54b479b64961481074c4200a9dec83fb8a42bb7db53cf97f1da131504a058ead85d0a9e4e32be14098bc9b4d1a5a8dd023100f9c7de178a286329103f684d1eab1ccfe359c65a41a1459d7f535b703c57048f25931b1670ab4ec7a812d94c69063522' )!
152+ // library generated signature
153+ sign0 := pvkey.sign (message_tobe_signed, opt0 )!
154+ v00 := pbkey.verify (message_tobe_signed, sign0 , opt0 )!
155+ // this own signature should assert into true
156+ assert v00 == true
157+
158+ // verify online-generated signature
159+ v01 := pbkey.verify (message_tobe_signed, online_sign0 , opt0 )!
160+ assert v01 == true
161+
162+ // with public_key_sample key
163+ v02 := pbk.verify (message_tobe_signed, sign0 , opt0 )!
164+ assert v02 == true
165+ v03 := pbk.verify (message_tobe_signed, online_sign0 , opt0 )!
166+ assert v03 == true
167+
168+ // Use smaller custom hash
169+ opt1 := SignerOpts{
170+ hash_config: .with_custom_hash
171+ allow_custom_hash: true
172+ allow_smaller_size: true
173+ custom_hash: sha1 .new ()
174+ }
175+ // online-generated signature with SHA1 digest
176+ online_sign1 := hex.decode ('306602310084299d8a70bf512c25cd2b79ae36509572f2bd6f198baeee074683578a70b4af8008e1cf451a2df1a887cf43daff4eea023100dceb267fe5037025c2af9f37911e05a36cbe666dd90fd6904020b5db056e86f25f9439a0ccb443d113b174cab6e2ad61' )!
177+ // library generated signature
178+ sign1 := pvkey.sign (message_tobe_signed, opt1 )!
179+ verified1 := pbkey.verify (message_tobe_signed, sign1 , opt1 )!
180+ // this own signature should assert into true
181+ assert verified1 == true
182+ // verify online-generated signature
183+ verified11 := pbkey.verify (message_tobe_signed, online_sign1 , opt1 )!
184+ assert verified11 == true
185+
186+ // verify with public_key_sample key
187+ v11 := pbk.verify (message_tobe_signed, sign1 , opt1 )!
188+ assert v11 == true
189+ v12 := pbk.verify (message_tobe_signed, online_sign1 , opt1 )!
190+ assert v12 == true
191+
192+ pvkey.free ()
193+ pbkey.free ()
194+ pbk.free ()
195+ }
0 commit comments