Skip to content

Commit 17fb2d3

Browse files
RomanTsisykRomanTsisyk
and
RomanTsisyk
authored
add docs (#8)
* add docs * Save changes on restore branch --------- Co-authored-by: RomanTsisyk <[email protected]>
1 parent 199f59a commit 17fb2d3

File tree

185 files changed

+19598
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+19598
-142
lines changed

.idea/deploymentTargetSelector.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/other.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# **CryptoKit**
22

33
![Purpose](https://img.shields.io/badge/purpose-cryptographic%20library-blue)
4-
![Status](https://img.shields.io/badge/status-active-brightgreen)
5-
![Last Commit](https://img.shields.io/github/last-commit/RomanTsisyk/CryptoKit)
64
![Docs](https://img.shields.io/badge/docs-up--to--date-brightgreen)
75
![OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/projects/1/badge)
86
[![Known Vulnerabilities](https://snyk.io/test/github/RomanTsisyk/CryptoKit/badge.svg)](https://snyk.io/test/github/RomanTsisyk/CryptoKit)
7+
8+
![Status](https://img.shields.io/badge/status-active-brightgreen)
9+
![Last Commit](https://img.shields.io/github/last-commit/RomanTsisyk/CryptoKit)
910
![GitHub stars](https://img.shields.io/github/stars/RomanTsisyk/CryptoKit?style=social)
1011
![Contributors](https://img.shields.io/github/contributors/RomanTsisyk/CryptoKit)
1112

@@ -23,23 +24,28 @@
2324

2425
---
2526

26-
## **Documentation**
27-
Explore the detailed documentation on the [CryptoKit Wiki](https://github.com/RomanTsisyk/CryptoKit/wiki):
27+
## CryptoLib Documentation
28+
29+
CryptoLib provides a comprehensive set of cryptographic operations, and with this release, we have also generated full API documentation to help developers integrate it into their projects seamlessly.
2830

29-
1. **[AES Encryption and Decryption](https://github.com/RomanTsisyk/CryptoKit/wiki/01-AES-Encryption-and-Decryption)**
30-
Secure your data using AES-GCM with Base64 encoding.
31+
The documentation covers the following:
3132

32-
2. **[RSA Encryption and Decryption](https://github.com/RomanTsisyk/CryptoKit/wiki/02.-RSA-Encryption-and-Decryption)**
33-
Encrypt small payloads with RSA-OAEP for secure communication.
33+
- **Key Management**: Generate, retrieve, and delete keys securely from the Keystore.
34+
- **Cryptographic Operations**: Encrypt, decrypt, and sign data with ease.
35+
- **Error Handling**: Detailed error messages and exception handling tailored to cryptographic operations.
36+
- **Usage Examples**: Simple, easy-to-follow examples to get started quickly.
3437

35-
3. **[Digital Signatures](https://github.com/RomanTsisyk/CryptoKit/wiki/03.-Digital-Signatures)**
36-
Sign and verify data for integrity and authenticity.
38+
You can access the full documentation [here](docs), which includes:
3739

38-
4. **[QR Code Integration](https://github.com/RomanTsisyk/CryptoKit/wiki/04.-QR-Code-Integration)**
39-
Generate and decode secure QR codes for data handling.
40+
- Function and method signatures
41+
- Descriptions of parameters and return values
42+
- Exception handling details
43+
- Code examples for various use cases
4044

41-
5. **[Key Management](https://github.com/RomanTsisyk/CryptoKit/wiki/05.-Key-Management)**
42-
Manage, store, and rotate cryptographic keys seamlessly.
45+
## 🚀 Get Started
4346

47+
1. Clone this repository.
48+
2. Check out the `docs/` folder for local documentation.
49+
3. Refer to the API docs for in-depth technical details on each class and method.
4450

4551
---

build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
plugins {
3-
id("com.android.library") version "8.5.2" apply false
4-
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
5-
}
1+
plugins {
2+
id("com.android.library") version "8.5.2" apply false
3+
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
4+
id("org.jetbrains.dokka") version "2.0.0"
5+
}

cryptolib/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id("com.android.library")
33
id("org.jetbrains.kotlin.android")
4+
id("org.jetbrains.dokka")
45
}
56

67
android {
@@ -69,3 +70,4 @@ dependencies {
6970
androidTestImplementation ("androidx.test:core:1.6.1")
7071

7172
}
73+

cryptolib/src/main/java/io/github/romantsisyk/cryptolib/biometrics/BiometricHelper.kt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import javax.crypto.Cipher
88
class BiometricHelper(private val context: Context) {
99

1010
/**
11-
* Authenticates the user using biometrics and optionally decrypts the provided encrypted data.
11+
* Authenticates the user using biometric data (e.g., fingerprint) and optionally decrypts encrypted data after successful authentication.
12+
* Provides success and error callbacks for handling decryption and authentication results.
13+
*
1214
* @param activity The activity where the biometric prompt will be displayed.
13-
* @param encryptedData Data to be decrypted upon successful authentication.
15+
* @param encryptedData The encrypted data to be decrypted upon successful authentication.
1416
* @param title The title displayed on the biometric prompt.
1517
* @param description The description displayed on the biometric prompt.
16-
* @param onSuccess Callback to handle the decrypted data.
17-
* @param onError Callback to handle errors during authentication or decryption.
18-
* @param onAuthenticationError Callback to handle authentication-specific errors.
18+
* @param onSuccess Callback invoked with decrypted data on successful authentication.
19+
* @param onError Callback invoked if any error occurs during decryption or authentication.
20+
* @param onAuthenticationError Callback invoked on authentication errors (e.g., failed fingerprint scan).
1921
*/
2022
fun authenticate(
2123
activity: FragmentActivity,
@@ -30,7 +32,6 @@ class BiometricHelper(private val context: Context) {
3032
.setTitle(title)
3133
.setDescription(description)
3234
.setSubtitle("Log in using your biometrics")
33-
.setDescription("Place your fingerprint on the sensor to authenticate")
3435
.setNegativeButtonText("Cancel")
3536
.build()
3637

@@ -49,56 +50,60 @@ class BiometricHelper(private val context: Context) {
4950
)
5051
}
5152
if (decryptedData != null) {
52-
onSuccess(decryptedData)
53+
onSuccess(decryptedData) // Handle success
5354
} else {
54-
onError(Exception("Decryption returned null"))
55+
onError(Exception("Decryption returned null")) // Handle decryption failure
5556
}
5657
} catch (e: Exception) {
57-
onError(e)
58+
onError(e) // Handle exception
5859
}
5960
}
6061

6162
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
6263
super.onAuthenticationError(errorCode, errString)
63-
onAuthenticationError(errorCode, errString)
64+
onAuthenticationError(errorCode, errString) // Handle authentication error
6465
}
6566

6667
override fun onAuthenticationFailed() {
6768
super.onAuthenticationFailed()
68-
onError(Exception("Authentication failed"))
69+
onError(Exception("Authentication failed")) // Handle failed authentication
6970
}
7071
}
7172
)
7273

73-
val cipher = getCipher()
74+
val cipher = getCipher() // Initialize cipher for decryption
7475
val cryptoObject = BiometricPrompt.CryptoObject(cipher)
7576

76-
biometricPrompt.authenticate(promptInfo, cryptoObject)
77+
biometricPrompt.authenticate(promptInfo, cryptoObject) // Start authentication
7778
}
7879

7980
/**
80-
* Initializes a Cipher object for decryption.
81-
* @return A Cipher initialized with a secret key.
81+
* Initializes a Cipher object for AES decryption.
82+
* The cipher is initialized with the secure key and is used to decrypt data.
83+
*
84+
* @return A Cipher initialized in DECRYPT_MODE.
8285
* @throws IllegalStateException if initialization fails.
8386
*/
8487
private fun getCipher(): Cipher {
8588
return try {
8689
val secretKey = KeyHelper.getKey() // Retrieve the secure key from KeyHelper
87-
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
88-
cipher.init(Cipher.DECRYPT_MODE, secretKey)
90+
val cipher = Cipher.getInstance("AES/GCM/NoPadding") // AES GCM mode
91+
cipher.init(Cipher.DECRYPT_MODE, secretKey) // Initialize cipher for decryption
8992
cipher
9093
} catch (e: Exception) {
91-
throw IllegalStateException("Failed to initialize Cipher", e)
94+
throw IllegalStateException("Failed to initialize Cipher", e) // Handle initialization failure
9295
}
9396
}
9497

9598
/**
96-
* Decrypts the provided data using the Cipher.
99+
* Decrypts the provided encrypted data using the specified Cipher.
100+
* This method is invoked as part of the decryption process after successful authentication.
101+
*
97102
* @param cipher The Cipher used for decryption.
98103
* @param encryptedData The encrypted data to decrypt.
99104
* @return The decrypted data as a ByteArray.
100105
*/
101106
private fun decryptData(cipher: Cipher, encryptedData: ByteArray): ByteArray {
102-
return cipher.doFinal(encryptedData)
107+
return cipher.doFinal(encryptedData) // Perform decryption
103108
}
104-
}
109+
}

cryptolib/src/main/java/io/github/romantsisyk/cryptolib/crypto/aes/AESEncryption.kt

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,66 @@ import javax.crypto.KeyGenerator
88
import javax.crypto.SecretKey
99
import javax.crypto.spec.GCMParameterSpec
1010

11+
/**
12+
* Object responsible for AES encryption and decryption operations using the AES-GCM algorithm.
13+
* Provides methods for encrypting and decrypting data, as well as generating AES keys.
14+
*/
1115
object AESEncryption {
1216

13-
private const val TRANSFORMATION = "AES/GCM/NoPadding"
14-
private const val IV_SIZE = 12 // Recommended IV size for GCM
15-
private const val TAG_SIZE = 128
16-
private const val KEY_SIZE = 256
17+
private const val TRANSFORMATION = "AES/GCM/NoPadding" // AES-GCM algorithm transformation
18+
private const val IV_SIZE = 12 // Recommended IV size for GCM mode
19+
private const val TAG_SIZE = 128 // Tag size for GCM mode (in bits)
20+
private const val KEY_SIZE = 256 // Key size (in bits)
1721

1822
/**
19-
* Encrypts plaintext using AES-GCM.
20-
* Throws CryptoOperationException on failure.
23+
* Encrypts the provided plaintext using AES-GCM encryption.
24+
*
25+
* @param plaintext The data to be encrypted in ByteArray format.
26+
* @param key The AES SecretKey used for encryption.
27+
* @return A Base64-encoded string containing the IV and the ciphertext.
28+
* @throws CryptoOperationException if the encryption process fails.
2129
*/
2230
fun encrypt(plaintext: ByteArray, key: SecretKey): String {
2331
return try {
2432
val cipher = Cipher.getInstance(TRANSFORMATION)
2533

26-
// Generate a random IV
34+
// Generate a random IV (Initialization Vector)
2735
val iv = ByteArray(IV_SIZE)
2836
SecureRandom().nextBytes(iv)
2937
val spec = GCMParameterSpec(TAG_SIZE, iv)
3038

39+
// Initialize the cipher for encryption and perform the encryption
3140
cipher.init(Cipher.ENCRYPT_MODE, key, spec)
3241
val ciphertext = cipher.doFinal(plaintext)
3342

34-
// Prepend IV to ciphertext
43+
// Prepend IV to ciphertext for later use in decryption
3544
val encrypted = iv + ciphertext
3645

37-
// Return as Base64 string
46+
// Return the encrypted data as a Base64 string
3847
Base64.getEncoder().encodeToString(encrypted)
3948
} catch (e: Exception) {
4049
throw CryptoOperationException("Encryption", e)
4150
}
4251
}
4352

4453
/**
45-
* Decrypts ciphertext using AES-GCM.
46-
* Throws CryptoOperationException on failure.
54+
* Decrypts the provided Base64-encoded encrypted data using AES-GCM decryption.
55+
*
56+
* @param encryptedData A Base64-encoded string containing the IV and the ciphertext.
57+
* @param key The AES SecretKey used for decryption.
58+
* @return The decrypted data as a ByteArray.
59+
* @throws CryptoOperationException if the decryption process fails.
4760
*/
4861
fun decrypt(encryptedData: String, key: SecretKey): ByteArray {
4962
return try {
5063
val encryptedBytes = Base64.getDecoder().decode(encryptedData)
5164

52-
// Extract IV and ciphertext
65+
// Extract IV and ciphertext from the encrypted data
5366
val iv = encryptedBytes.copyOfRange(0, IV_SIZE)
5467
val ciphertext = encryptedBytes.copyOfRange(IV_SIZE, encryptedBytes.size)
5568
val spec = GCMParameterSpec(TAG_SIZE, iv)
5669

70+
// Initialize the cipher for decryption and perform the decryption
5771
val cipher = Cipher.getInstance(TRANSFORMATION)
5872
cipher.init(Cipher.DECRYPT_MODE, key, spec)
5973
cipher.doFinal(ciphertext)
@@ -63,7 +77,9 @@ object AESEncryption {
6377
}
6478

6579
/**
66-
* Generates a new AES key.
80+
* Generates a new AES key with the specified key size.
81+
*
82+
* @return A newly generated AES SecretKey.
6783
*/
6884
fun generateKey(): SecretKey {
6985
val keyGenerator = KeyGenerator.getInstance("AES")

0 commit comments

Comments
 (0)