crossbundle_tools/commands/android/common/
gen_key.rs

1use android_tools::java_tools::{Key, KeyAlgorithm, Keytool};
2use std::path::PathBuf;
3
4/// Generates keystore with default configuration. You can manage configuration with
5/// options
6pub fn gen_key(
7    sign_key_path: Option<PathBuf>,
8    sign_key_pass: Option<String>,
9    sign_key_alias: Option<String>,
10) -> crate::error::Result<Key> {
11    let key = if let Some(key_path) = sign_key_path {
12        let aab_key = Key {
13            key_path,
14            key_pass: sign_key_pass.unwrap(),
15            key_alias: sign_key_alias.unwrap(),
16        };
17        if aab_key.key_path.exists() {
18            aab_key
19        } else {
20            Keytool::new()
21                .genkeypair(true)
22                .v(true)
23                .keystore(&aab_key.key_path)
24                .alias(&aab_key.key_alias)
25                .keypass(&aab_key.key_pass)
26                .storepass(&aab_key.key_pass)
27                .dname(&["CN=Android Debug,O=Android,C=US".to_owned()])
28                .keyalg(KeyAlgorithm::RSA)
29                .keysize(2048)
30                .validity(10000)
31                .run()?
32                // This will never panic because of AabKey always returned if help flag not set
33                .unwrap()
34        }
35    } else {
36        let aab_key = Key::new_default()?;
37        if aab_key.key_path.exists() {
38            aab_key
39        } else {
40            Keytool::new()
41                .genkeypair(true)
42                .v(true)
43                .keystore(&aab_key.key_path)
44                .alias(&aab_key.key_alias)
45                .keypass(&aab_key.key_pass)
46                .storepass(&aab_key.key_pass)
47                .dname(&["CN=Android Debug,O=Android,C=US".to_owned()])
48                .keyalg(KeyAlgorithm::RSA)
49                .keysize(2048)
50                .validity(10000)
51                .run()?
52                // This will never panic because of AabKey always returned if help flag not set
53                .unwrap()
54        }
55    };
56    Ok(key)
57}