@@ -66,22 +66,31 @@ impl Drop for InnerKey {
6666
6767/// CNG private key wrapper
6868#[ derive( Clone , Debug ) ]
69- pub struct NCryptKey ( Arc < InnerKey > ) ;
69+ pub struct NCryptKey {
70+ inner : Arc < InnerKey > ,
71+ silent : bool ,
72+ }
7073
7174impl NCryptKey {
7275 /// Create an owned instance which frees the underlying handle automatically
7376 pub fn new_owned ( handle : NCRYPT_KEY_HANDLE ) -> Self {
74- NCryptKey ( Arc :: new ( InnerKey :: Owned ( handle) ) )
77+ NCryptKey {
78+ inner : Arc :: new ( InnerKey :: Owned ( handle) ) ,
79+ silent : true ,
80+ }
7581 }
7682
7783 /// Create a borrowed instance which doesn't free the key handle
7884 pub fn new_borrowed ( handle : NCRYPT_KEY_HANDLE ) -> Self {
79- NCryptKey ( Arc :: new ( InnerKey :: Borrowed ( handle) ) )
85+ NCryptKey {
86+ inner : Arc :: new ( InnerKey :: Borrowed ( handle) ) ,
87+ silent : true ,
88+ }
8089 }
8190
8291 /// Return an inner CNG key handle
8392 pub fn inner ( & self ) -> NCRYPT_KEY_HANDLE {
84- self . 0 . inner ( )
93+ self . inner . inner ( )
8594 }
8695
8796 fn get_string_property ( & self , property : PCWSTR ) -> Result < String > {
@@ -160,6 +169,11 @@ impl NCryptKey {
160169 CngError :: from_hresult ( result)
161170 }
162171
172+ /// Enable or disable silent key operations without prompting the user. The default value is 'true'.
173+ pub fn set_silent ( & mut self , silent : bool ) {
174+ self . silent = silent;
175+ }
176+
163177 /// Sign a given digest with this key. The `hash` slice must be 32, 48 or 64 bytes long.
164178 pub fn sign ( & self , hash : & [ u8 ] , padding : SignaturePadding ) -> Result < Vec < u8 > > {
165179 unsafe {
@@ -189,6 +203,7 @@ impl NCryptKey {
189203 } ;
190204
191205 let mut result = 0 ;
206+ let dwflags = flag | if self . silent { NCRYPT_SILENT_FLAG } else { 0 } ;
192207
193208 CngError :: from_hresult ( NCryptSignHash (
194209 self . inner ( ) ,
@@ -198,7 +213,7 @@ impl NCryptKey {
198213 ptr:: null_mut ( ) ,
199214 0 ,
200215 & mut result,
201- NCRYPT_SILENT_FLAG | flag ,
216+ dwflags ,
202217 ) ) ?;
203218
204219 let mut signature = vec ! [ 0u8 ; result as usize ] ;
@@ -211,7 +226,7 @@ impl NCryptKey {
211226 signature. as_mut_ptr ( ) ,
212227 signature. len ( ) as u32 ,
213228 & mut result,
214- NCRYPT_SILENT_FLAG | flag ,
229+ dwflags ,
215230 ) ) ?;
216231
217232 Ok ( signature)
0 commit comments