|
1 | | -The `Crypto` module provides simple, secure, and efficient APIs for generating cryptographic hashes from data. |
2 | | -Currently, it supports three hashing algorithms: **SHA-256**, **SHA-384**, and **SHA-512**. |
| 1 | +The `Crypto` module provides a set of cryptographic utilities for hashing, HMAC authentication, symmetric key generation, and AES-GCM encryption/decryption. It is designed to securely process `Data` instances using industry-standard algorithms. |
3 | 2 |
|
4 | | -The module is designed for use with the `Data` class. |
5 | | -You can easily hash any text, binary data, or file content with just a few lines of code. |
| 3 | +--- |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `Crypto` module enables: |
| 8 | + |
| 9 | +* Hashing data with MD5, SHA-1, SHA-2 family (SHA-224, SHA-256, SHA-384, SHA-512) |
| 10 | +* Creating HMAC digests using a secret key |
| 11 | +* Generating random symmetric encryption keys |
| 12 | +* Encrypting and decrypting data with AES-GCM (authenticated encryption) |
| 13 | + |
| 14 | +All operations are performed on the `Data` type, which encapsulates binary input/output. |
6 | 15 |
|
7 | 16 | --- |
8 | 17 |
|
9 | 18 | ## Functions |
10 | 19 |
|
11 | | -### `sha256(data: Data): string` |
12 | | -Computes the SHA-256 hash of the given `Data` and returns the result as a hexadecimal string. |
| 20 | +### `Crypto.generateSymmetricKey(size?: number): Data` |
| 21 | + |
| 22 | +Generates a new random symmetric key. |
| 23 | + |
| 24 | +* **Parameters:** |
13 | 25 |
|
14 | | -### `sha384(data: Data): string` |
15 | | -Computes the SHA-384 hash of the given `Data` and returns the result as a hexadecimal string. |
| 26 | + * `size` (optional): Key size in bits. Defaults to `256`. |
16 | 27 |
|
17 | | -### `sha512(data: Data): string` |
18 | | -Computes the SHA-512 hash of the given `Data` and returns the result as a hexadecimal string. |
| 28 | +* **Returns:** A `Data` object representing the key. |
| 29 | + |
| 30 | +* **Example:** |
| 31 | + |
| 32 | + ```ts |
| 33 | + const key = Crypto.generateSymmetricKey() // 256-bit key |
| 34 | + ``` |
19 | 35 |
|
20 | 36 | --- |
21 | 37 |
|
22 | | -## Usage Examples |
| 38 | +### Hashing Functions |
23 | 39 |
|
24 | | -### Hash a simple string |
25 | | -```tsx |
26 | | -const data = Data.fromString('Hello, world!') |
27 | | -if (data) { |
28 | | - const hash = Crypto.sha256(data) |
29 | | - console.log('SHA-256 Hash:', hash) |
30 | | -} |
31 | | -``` |
| 40 | +These functions return a cryptographic hash (digest) of the input data. |
| 41 | + |
| 42 | +#### `Crypto.md5(data: Data): Data` |
| 43 | + |
| 44 | +Hashes the input data using the MD5 algorithm (128-bit output). |
| 45 | + |
| 46 | +* **Returns:** A `Data` object containing the MD5 digest. |
| 47 | + |
| 48 | +* **Example:** |
| 49 | + |
| 50 | + ```ts |
| 51 | + const data = Data.fromString("Hello") |
| 52 | + const hash = Crypto.md5(data).toHexString() |
| 53 | + ``` |
32 | 54 |
|
33 | 55 | --- |
34 | 56 |
|
35 | | -### Hash file contents |
36 | | -```tsx |
37 | | -const filePath = '/path/to/file.txt' |
38 | | -const fileData = Data.fromFile(filePath) |
39 | | -if (fileData) { |
40 | | - const hash = Crypto.sha384(fileData) |
41 | | - console.log('SHA-384 File Hash:', hash) |
42 | | -} |
43 | | -``` |
| 57 | +#### `Crypto.sha1(data: Data): Data` |
| 58 | + |
| 59 | +Uses the SHA-1 algorithm (160-bit output). |
| 60 | + |
| 61 | +* **Returns:** A `Data` object. |
44 | 62 |
|
45 | 63 | --- |
46 | 64 |
|
47 | | -### Hash an image (JPEG) |
48 | | -```tsx |
49 | | -// Assume you have a UIImage object |
50 | | -const imageData = Data.fromJPEG(myImage, 0.8) |
51 | | -if (imageData) { |
52 | | - const hash = Crypto.sha512(imageData) |
53 | | - console.log('SHA-512 Image Hash:', hash) |
54 | | -} |
| 65 | +#### `Crypto.sha256(data: Data): Data` |
| 66 | + |
| 67 | +Uses the SHA-256 algorithm (256-bit output). |
| 68 | + |
| 69 | +* **Returns:** A `Data` object. |
| 70 | + |
| 71 | +* **Example:** |
| 72 | + |
| 73 | + ```ts |
| 74 | + const hash = Crypto.sha256(Data.fromString("test")).toHexString() |
| 75 | + ``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +#### `Crypto.sha384(data: Data): Data` |
| 80 | + |
| 81 | +Uses the SHA-384 algorithm (384-bit output). |
| 82 | + |
| 83 | +* **Returns:** A `Data` object. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +#### `Crypto.sha512(data: Data): Data` |
| 88 | + |
| 89 | +Uses the SHA-512 algorithm (512-bit output). |
| 90 | + |
| 91 | +* **Returns:** A `Data` object. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +### HMAC Functions |
| 96 | + |
| 97 | +These functions generate a hash-based message authentication code (HMAC) using a shared secret key. |
| 98 | + |
| 99 | +All return a `Data` object representing the HMAC digest. |
| 100 | + |
| 101 | +* **Parameters:** |
| 102 | + |
| 103 | + * `data`: The message to authenticate (`Data`) |
| 104 | + * `key`: The symmetric key (`Data`) |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +#### `Crypto.hmacMD5(data: Data, key: Data): Data` |
| 109 | + |
| 110 | +Computes HMAC using MD5. |
| 111 | + |
| 112 | +```ts |
| 113 | +const key = Crypto.generateSymmetricKey() |
| 114 | +const hmac = Crypto.hmacMD5(Data.fromString("msg"), key).toHexString() |
55 | 115 | ``` |
56 | 116 |
|
57 | 117 | --- |
58 | 118 |
|
59 | | -### Chain hash multiple steps |
60 | | -```tsx |
61 | | -const original = Data.fromString('First Step') |
62 | | -if (original) { |
63 | | - const firstHash = Crypto.sha256(original) |
64 | | - const secondData = Data.fromString(firstHash) |
65 | | - if (secondData) { |
66 | | - const finalHash = Crypto.sha512(secondData) |
67 | | - console.log('Chained Hash (SHA-512):', finalHash) |
68 | | - } |
| 119 | +#### `Crypto.hmacSHA1(data: Data, key: Data): Data` |
| 120 | + |
| 121 | +Computes HMAC using SHA-1. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +#### `Crypto.hmacSHA224(data: Data, key: Data): Data` |
| 126 | + |
| 127 | +Computes HMAC using SHA-224. |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +#### `Crypto.hmacSHA256(data: Data, key: Data): Data` |
| 132 | + |
| 133 | +Computes HMAC using SHA-256. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +#### `Crypto.hmacSHA384(data: Data, key: Data): Data` |
| 138 | + |
| 139 | +Computes HMAC using SHA-384. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +#### `Crypto.hmacSHA512(data: Data, key: Data): Data` |
| 144 | + |
| 145 | +Computes HMAC using SHA-512. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## AES-GCM Encryption |
| 150 | + |
| 151 | +### `Crypto.encryptAESGCM(data: Data, key: Data, options?: { iv?: Data, aad?: Data }): Data | null` |
| 152 | + |
| 153 | +Encrypts the given data using AES-GCM with the provided key. |
| 154 | + |
| 155 | +* **Parameters:** |
| 156 | + |
| 157 | + * `data`: The plaintext `Data` to encrypt |
| 158 | + * `key`: A `Data` object representing the symmetric key |
| 159 | + * `options` (optional): |
| 160 | + |
| 161 | + * `iv`: Initialization vector (optional). If omitted, a random IV is used internally. |
| 162 | + * `aad`: Additional authenticated data (optional). Used for authentication but not encrypted. |
| 163 | + |
| 164 | +* **Returns:** A `Data` object containing the encrypted ciphertext, or `null` on failure. |
| 165 | + |
| 166 | +* **Example:** |
| 167 | + |
| 168 | + ```ts |
| 169 | + const key = Crypto.generateSymmetricKey() |
| 170 | + const plaintext = Data.fromString("secret message") |
| 171 | + const encrypted = Crypto.encryptAESGCM(plaintext, key) |
| 172 | + ``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +### `Crypto.decryptAESGCM(data: Data, key: Data, aad?: Data): Data | null` |
| 177 | + |
| 178 | +Decrypts AES-GCM-encrypted `Data` using the provided key and optional AAD. |
| 179 | + |
| 180 | +* **Parameters:** |
| 181 | + |
| 182 | + * `data`: The encrypted data (`Data`) |
| 183 | + * `key`: The symmetric key used to encrypt the data |
| 184 | + * `aad` (optional): The additional authenticated data used during encryption (must match exactly) |
| 185 | + |
| 186 | +* **Returns:** The decrypted plaintext as `Data`, or `null` if decryption fails (e.g., tag mismatch or incorrect key). |
| 187 | + |
| 188 | +* **Example:** |
| 189 | + |
| 190 | + ```ts |
| 191 | + const decrypted = Crypto.decryptAESGCM(encrypted, key) |
| 192 | + console.log(decrypted?.toRawString()) |
| 193 | + ``` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## Summary of Algorithms |
| 198 | + |
| 199 | +| Function | Output Size | Use Case | |
| 200 | +| --------- | ------------ | -------------------------- | |
| 201 | +| `md5` | 128 bits | Legacy checksums | |
| 202 | +| `sha1` | 160 bits | Compatibility | |
| 203 | +| `sha256` | 256 bits | General-purpose security | |
| 204 | +| `sha384` | 384 bits | Stronger hashing | |
| 205 | +| `sha512` | 512 bits | High-security requirements | |
| 206 | +| `hmacXXX` | Same as hash | Authentication | |
| 207 | +| `AES-GCM` | variable | Authenticated encryption | |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## Full Example |
| 212 | + |
| 213 | +```ts |
| 214 | +const key = Crypto.generateSymmetricKey() |
| 215 | +const message = Data.fromString("Encrypt me") |
| 216 | +const encrypted = Crypto.encryptAESGCM(message, key) |
| 217 | +const decrypted = encrypted ? Crypto.decryptAESGCM(encrypted, key) : null |
| 218 | + |
| 219 | +if (decrypted) { |
| 220 | + console.log("Decrypted:", decrypted.toRawString()) |
69 | 221 | } |
70 | 222 | ``` |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +## Notes |
| 227 | + |
| 228 | +* All functions require valid `Data` objects. |
| 229 | +* For AES-GCM, if `iv` is omitted, a secure random IV is automatically applied. |
| 230 | +* Encrypted `Data` may include the IV and authentication tag. |
0 commit comments