Skip to content

Commit 0c044d7

Browse files
authored
Merge pull request #11 from Commencis/documentation
Add Section about Accessing Generated Keys inside Java Class to README
2 parents 8c2b0d9 + f4a1b3f commit 0c044d7

1 file changed

Lines changed: 46 additions & 5 deletions

File tree

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The Secrets Vault Plugin employs multiple security-enhancing strategies, includi
1111

1212
> Please remember that no client-side security measures are invincible. As a rule of thumb, **storing secrets in a mobile app is not considered best practice**. However, when there's no other option, this method is our best recommendation for concealing them.
1313
14-
# 1) Getting started
14+
# 1) Getting Started
1515

1616
To use the Secrets Vault Plugin in your Android project, follow these steps:
1717

@@ -37,7 +37,7 @@ plugins {
3737
}
3838
```
3939

40-
# 2) Keep secrets in your project
40+
# 2) Keep Secrets in Your Project
4141

4242
To keep secrets in your project, you can add them to a JSON file located in the root folder of the module where you've applied the plugin.
4343
Follow the format below:
@@ -81,7 +81,7 @@ secretsVault {
8181
- The `projectName` parameter is optional and uses the module's name where the plugin is applied by default. You can specify a different project name if needed.
8282
- The `version` parameter is optional. If not provided, a default CMake version will be used.
8383

84-
# 3) Get your secret key in your app
84+
# 3) Get Your Secret Key in Your App
8585
To enable the compilation of C++ files, add these lines in the Module level `build.gradle[.kts]` :
8686
```gradle
8787
android {
@@ -96,12 +96,53 @@ android {
9696
}
9797
```
9898

99+
## Access inside Kotlin
100+
99101
Access your secret key by calling :
100102
```kotlin
101103
val key = MainSecrets().getYourSecretKeyName()
102104
```
103105

104-
# 4) Flavor-specific secrets (Optional)
106+
## Access inside Java
107+
108+
The names of the methods in the generated files (`MainSecrets.kt` & `Secrets.kt`) are replaced with dummy strings such as `a0` on the `JVM` side for extra security. This makes accessing these methods inside `Java` classes a bit tricky since those names are not "readable" and are dependent on the order in `secrets.json` file.
109+
110+
As as solution, a helper/wrapper `Kotlin` class can be created instead of accessing these methods directly inside a `Java` class:
111+
112+
`MySecretsHelper.kt`:
113+
```kotlin
114+
internal class MySecretsHelper {
115+
val secrets = MainSecrets() // Suppose contains "external fun getYourSecretKeyName(): String" with "@JvmName("a0")" annotation.
116+
val mySecret: String get() = secrets.getYourSecretKeyName()
117+
}
118+
```
119+
120+
Inside your `Java` class, instead of doing:
121+
122+
```java
123+
class MySecretsConsumer {
124+
final MainSecrets secrets = new MainSecrets();
125+
126+
void someMethod() {
127+
final String key = secrets.a0();
128+
...
129+
}
130+
}
131+
```
132+
133+
You can do:
134+
```java
135+
class MySecretsConsumer {
136+
final MySecretsHelper secretsHelper = new MySecretsHelper();
137+
138+
void someMethod() {
139+
final String key = secretsHelper.getMySecret();
140+
...
141+
}
142+
}
143+
```
144+
145+
# 4) Flavor-specific Secrets (Optional)
105146
If you are working on multi-flavor projects and have flavor-specific secrets, you need to pass arguments to CMake in your `build.gradle[.kts]` file. Follow the steps below:
106147

107148
```gradle
@@ -207,7 +248,7 @@ android {
207248
```
208249
Remember to replace version in -Dversion=flavorName with the appropriate cmakeArgument from your JSON. The flavorName should correspond to the specific product flavor you're building for.
209250

210-
# 6) Enhance your secrets security (Optional)
251+
# 6) Enhance Your Secrets' Security (Optional)
211252
To enhance the security of your secrets, you can create a custom encoding/decoding algorithm. The secrets will be stored in C++ and further secured by applying your custom encoding algorithm. Additionally, the decoding algorithm will be compiled, making it more challenging for an attacker to reverse-engineer and obtain your keys.
212253

213254
Encode all values in your `secrets.json` file.

0 commit comments

Comments
 (0)