44
55use anyhow:: Context ;
66use base64:: { decode, encode} ;
7- use minisign:: { sign, KeyPair as KP , SecretKeyBox } ;
7+ use minisign:: { sign, KeyPair as KP , SecretKey , SecretKeyBox , SignatureBox } ;
88use std:: {
9- env:: var_os,
109 fs:: { self , File , OpenOptions } ,
1110 io:: { BufReader , BufWriter , Write } ,
1211 path:: { Path , PathBuf } ,
@@ -101,22 +100,11 @@ where
101100}
102101
103102/// Sign files
104- pub fn sign_file < P > (
105- private_key : String ,
106- password : Option < String > ,
107- bin_path : P ,
108- ) -> crate :: Result < ( PathBuf , String ) >
103+ pub fn sign_file < P > ( secret_key : & SecretKey , bin_path : P ) -> crate :: Result < ( PathBuf , SignatureBox ) >
109104where
110105 P : AsRef < Path > ,
111106{
112107 let bin_path = bin_path. as_ref ( ) ;
113- let decoded_secret = decode_key ( private_key) ?;
114- let sk_box = SecretKeyBox :: from_string ( & decoded_secret)
115- . with_context ( || "failed to load updater private key" ) ?;
116- let sk = sk_box
117- . into_secret_key ( password)
118- . with_context ( || "incorrect updater private key password" ) ?;
119-
120108 // We need to append .sig at the end it's where the signature will be stored
121109 let mut extension = bin_path. extension ( ) . unwrap ( ) . to_os_string ( ) ;
122110 extension. push ( ".sig" ) ;
@@ -134,7 +122,7 @@ where
134122
135123 let signature_box = sign (
136124 None ,
137- & sk ,
125+ secret_key ,
138126 data_reader,
139127 Some ( trusted_comment. as_str ( ) ) ,
140128 Some ( "signature from tauri secret key" ) ,
@@ -143,34 +131,18 @@ where
143131 let encoded_signature = encode ( & signature_box. to_string ( ) ) ;
144132 signature_box_writer. write_all ( encoded_signature. as_bytes ( ) ) ?;
145133 signature_box_writer. flush ( ) ?;
146- Ok ( ( fs:: canonicalize ( & signature_path) ?, encoded_signature ) )
134+ Ok ( ( fs:: canonicalize ( & signature_path) ?, signature_box ) )
147135}
148136
149- /// Sign files using the TAURI_KEY_PASSWORD and TAURI_PRIVATE_KEY environment variables
150- pub fn sign_file_from_env_variables < P > ( path_to_sign : P ) -> crate :: Result < ( PathBuf , String ) >
151- where
152- P : AsRef < Path > ,
153- {
154- // if no password provided we set empty string
155- let password_string =
156- var_os ( "TAURI_KEY_PASSWORD" ) . map ( |value| value. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
157- // get the private key
158- if let Some ( private_key) = var_os ( "TAURI_PRIVATE_KEY" ) {
159- // check if this file exist..
160- let mut private_key_string = String :: from ( private_key. to_str ( ) . unwrap ( ) ) ;
161- let pk_dir = Path :: new ( & private_key_string) ;
162- // Check if user provided a path or a key
163- // We validate if the path exist or no.
164- if pk_dir. exists ( ) {
165- // read file content as use it as private key
166- private_key_string = read_key_from_file ( pk_dir) ?;
167- }
168- // sign our file
169- sign_file ( private_key_string, password_string, path_to_sign)
170- } else {
171- // reject if we don't have the private key
172- Err ( anyhow:: anyhow!( "A public key has been found, but no private key. Make sure to set `TAURI_PRIVATE_KEY` environment variable." ) )
173- }
137+ /// Gets the updater secret key from the given private key and password.
138+ pub fn secret_key ( private_key : String , password : Option < String > ) -> crate :: Result < SecretKey > {
139+ let decoded_secret = decode_key ( private_key) ?;
140+ let sk_box = SecretKeyBox :: from_string ( & decoded_secret)
141+ . with_context ( || "failed to load updater private key" ) ?;
142+ let sk = sk_box
143+ . into_secret_key ( password)
144+ . with_context ( || "incorrect updater private key password" ) ?;
145+ Ok ( sk)
174146}
175147
176148fn unix_timestamp ( ) -> u64 {
0 commit comments