ciphern 0.2.1

Enterprise-grade cryptographic library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (c) 2025 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! Ciphern Crypto Library - Python API Examples
//!
//! This module provides examples of using Ciphern from Python via PyO3.
//!
//! # Python Usage
//!
//! ```python
//! from ciphern import Ciphern, KeyManager, Cipher, Algorithm, Hash
//!
//! # Initialize the library
//! Ciphern.init()
//!
//! # Create a key manager
//! key_manager = KeyManager()
//!
//! # Generate a key
//! key_id = key_manager.generate_key(Algorithm.AES_256_GCM)
//!
//! # Create a cipher and encrypt data
//! cipher = Cipher(Algorithm.AES_256_GCM)
//! plaintext = b"Hello, Ciphern!"
//! ciphertext = cipher.encrypt(key_manager, key_id, plaintext)
//!
//! # Decrypt the data
//! decrypted = cipher.decrypt(key_manager, key_id, ciphertext)
//!
//! # Verify
//! assert plaintext == decrypted
//! print("Python encryption successful!")
//! ```

use std::result::Result;

/// Python AES-256-GCM Encryption Example
///
/// Demonstrates using Ciphern from Python with AES-256-GCM encryption.
pub fn python_aes_example() {
    println!(
        r#"
Python AES-256-GCM Example
==========================

from ciphern import Ciphern, KeyManager, Cipher, Algorithm

# Initialize the library
Ciphern.init()

# Create a key manager
key_manager = KeyManager()

# Generate a key
key_id = key_manager.generate_key(Algorithm.AES_256_GCM)
print("Generated Key ID: {{}}".format(key_id))

# Create a cipher
cipher = Cipher(Algorithm.AES_256_GCM)

# Encrypt data
plaintext = b"Hello, Ciphern from Python!"
ciphertext = cipher.encrypt(key_manager, key_id, plaintext)
print("Ciphertext length: {{}} bytes".format(len(ciphertext)))

# Decrypt data
decrypted = cipher.decrypt(key_manager, key_id, ciphertext)
print("Decrypted: {{}}".format(decrypted.decode()))

# Verify
assert plaintext == decrypted
print("Python AES-256-GCM encryption successful!")
"#
    );
}

/// Python Digital Signature Example
///
/// Demonstrates digital signatures from Python.
pub fn python_signature_example() {
    println!(
        r#"
Python Digital Signature Example
================================

from ciphern import Ciphern, KeyManager, Signer, Algorithm

# Initialize the library
Ciphern.init()

# Create a key manager
key_manager = KeyManager()

# Generate an Ed25519 key pair
key_id = key_manager.generate_key(Algorithm.ED_25519)
print("Generated Key ID: {{}}".format(key_id))

# Create a signer
signer = Signer(Algorithm.ED_25519)

# Sign a message
message = b"Message to sign"
signature = signer.sign(key_manager, key_id, message)
print("Signature length: {{}} bytes".format(len(signature)))

# Verify the signature
is_valid = signer.verify(key_manager, key_id, message, signature)
print("Signature valid: {{}}".format(is_valid))

assert is_valid
print("Python digital signature successful!")
"#
    );
}

/// Python SM4 Encryption Example
///
/// Demonstrates SM4 encryption (Chinese national standard) from Python.
pub fn python_sm4_example() {
    println!(
        r#"
Python SM4-GCM Example (Chinese National Standard)
===================================================

from ciphern import Ciphern, KeyManager, Cipher, Algorithm

# Initialize the library
Ciphern.init()

# Create a key manager
key_manager = KeyManager()

# Generate an SM4 key
key_id = key_manager.generate_key(Algorithm.SM4_GCM)
print("Generated SM4 Key ID: {{}}".format(key_id))

# Create a cipher
cipher = Cipher(Algorithm.SM4_GCM)

# Encrypt data
plaintext = b"Hello, SM4 encryption from Python!"
ciphertext = cipher.encrypt(key_manager, key_id, plaintext)
print("Ciphertext length: {{}} bytes".format(len(ciphertext)))

# Decrypt data
decrypted = cipher.decrypt(key_manager, key_id, ciphertext)
print("Decrypted: {{}}".format(decrypted.decode()))

# Verify
assert plaintext == decrypted
print("Python SM4-GCM encryption successful!")
"#
    );
}

/// Python Key Management Example
///
/// Demonstrates key management from Python.
pub fn python_key_management_example() {
    println!(
        r#"
Python Key Management Example
=============================

from ciphern import Ciphern, KeyManager, Algorithm, KeyState

# Initialize the library
Ciphern.init()

# Create a key manager
key_manager = KeyManager()

# Generate multiple keys
key_id_1 = key_manager.generate_key(Algorithm.AES_256_GCM)
key_id_2 = key_manager.generate_key(Algorithm.SM4_GCM)
key_id_3 = key_manager.generate_key(Algorithm.ED_25519)

print("Generated 3 keys:")
print("  1. {{}}".format(key_id_1))
print("  2. {{}}".format(key_id_2))
print("  3. {{}}".format(key_id_3))

# Get key state
state = key_manager.get_key_state(key_id_1)
print("Key 1 state: {{}}".format(state))

# Rotate key
key_manager.rotate_key(key_id_1)
print("Key 1 rotated successfully")

# Deprecate key
key_manager.deprecate_key(key_id_2)
print("Key 2 deprecated successfully")

# Destroy key
key_manager.destroy_key(key_id_3)
print("Key 3 destroyed successfully")

print("Python key management successful!")
"#
    );
}

/// Python Hash Operations Example
///
/// Demonstrates hash operations from Python.
pub fn python_hash_example() {
    println!(
        r#"
Python Hash Operations Example
==============================

from ciphern import Ciphern, Hash

# Initialize the library
Ciphern.init()

# Data to hash
data = b"Hello, Ciphern!"

# Compute SHA-256 hash
sha256 = Hash.sha256(data)
print("SHA-256: {{}}...".format(sha256.hex()[:32]))

# Compute SHA-512 hash
sha512 = Hash.sha512(data)
print("SHA-512: {{}}...".format(sha512.hex()[:32]))

# Compute SM3 hash (Chinese national standard)
sm3 = Hash.sm3(data)
print("SM3: {{}}...".format(sm3.hex()[:32]))

# Compute BLAKE3 hash (high performance)
blake3 = Hash.blake3(data)
print("BLAKE3: {{}}...".format(blake3.hex()[:32]))

print("Python hash operations successful!")
"#
    );
}

/// Python Random Generation Example
///
/// Demonstrates secure random generation from Python.
pub fn python_random_example() {
    println!(
        r#"
Python Random Generation Example
================================

from ciphern import Ciphern, Random

# Initialize the library
Ciphern.init()

# Generate random bytes
random_bytes = Random.bytes(32)
print("Random bytes: {{}}...".format(random_bytes.hex()[:32]))

# Generate a UUID v4
uuid = Random.uuid_v4()
print("UUID v4: {{}}".format(uuid))

# Generate random number in range
random_int = Random.randint(1, 1000)
print("Random int (1-1000): {{}}".format(random_int))

print("Python random generation successful!")
"#
    );
}

/// Python Complete Example
///
/// A complete Python example demonstrating multiple features.
pub fn python_complete_example() {
    println!(
        r#"
Python Complete Example
=======================

from ciphern import Ciphern, KeyManager, Cipher, Signer, Algorithm, Hash, Random

def main():
    # Initialize
    Ciphern.init()
    print("Ciphern initialized")

    # Key management
    key_manager = KeyManager()
    aes_key = key_manager.generate_key(Algorithm.AES_256_GCM)
    sign_key = key_manager.generate_key(Algorithm.ED_25519)
    print("Keys generated: {{}}..., {{}}...".format(aes_key[:8], sign_key[:8]))

    # Encryption
    cipher = Cipher(Algorithm.AES_256_GCM)
    plaintext = b"Secret message from Python!"
    ciphertext = cipher.encrypt(key_manager, aes_key, plaintext)
    print("Data encrypted: {{}} bytes".format(len(ciphertext)))

    # Decryption
    decrypted = cipher.decrypt(key_manager, aes_key, ciphertext)
    print("Data decrypted: {{}}".format(decrypted.decode()))

    # Digital signature
    signer = Signer(Algorithm.ED_25519)
    message = b"Message to sign"
    signature = signer.sign(key_manager, sign_key, message)
    print("Message signed: {{}} bytes".format(len(signature)))

    # Verify signature
    is_valid = signer.verify(key_manager, sign_key, message, signature)
    print("Signature valid: {{}}".format(is_valid))

    # Hash operations
    hash_result = Hash.sha256(plaintext)
    print("SHA-256: {{}}...".format(hash_result.hex()[:32]))

    # Random generation
    random_data = Random.bytes(16)
    print("Random bytes: {{}}".format(random_data.hex()))

    # Key lifecycle
    key_manager.rotate_key(aes_key)
    print("Key rotated")

    key_manager.deprecate_key(aes_key)
    print("Key deprecated")

    key_manager.destroy_key(aes_key)
    print("Key destroyed")

    print("\nAll operations completed successfully!")

if __name__ == "__main__":
    main()
"#
    );
}

/// Python Web Application Example
///
/// Example of using Ciphern in a Python web application.
pub fn python_web_example() {
    println!(
        r#"
Python Web Application Example (FastAPI)
========================================

from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
from ciphern import Ciphern, KeyManager, Cipher, Algorithm
from typing import Optional

app = FastAPI()

# Initialize Ciphern on startup
@app.on_event("startup")
async def startup():
    Ciphern.init()
    # In production, load keys from secure storage

# Dependency for key manager
def get_key_manager() -> KeyManager:
    return KeyManager()

class EncryptRequest(BaseModel):
    plaintext: str
    algorithm: str = "AES_256_GCM"

class EncryptResponse(BaseModel):
    ciphertext: str
    key_id: str

@app.post("/encrypt", response_model=EncryptResponse)
async def encrypt(
    request: EncryptRequest,
    key_manager: KeyManager = Depends(get_key_manager)
):
    try:
        algo = Algorithm[request.algorithm]
        cipher = Cipher(algo)
        key_id = key_manager.generate_key(algo)

        plaintext_bytes = request.plaintext.encode()
        ciphertext = cipher.encrypt(key_manager, key_id, plaintext_bytes)

        return EncryptResponse(
            ciphertext=ciphertext.hex(),
            key_id=key_id
        )
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@app.post("/decrypt")
async def decrypt(
    ciphertext: str,
    key_id: str,
    algorithm: str,
    key_manager: KeyManager = Depends(get_key_manager)
):
    try:
        algo = Algorithm[algorithm]
        cipher = Cipher(algo)

        ciphertext_bytes = bytes.fromhex(ciphertext)
        decrypted = cipher.decrypt(key_manager, key_id, ciphertext_bytes)

        return {{"plaintext": decrypted.decode()}}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

# Run with: uvicorn main:app --reload
"#
    );
}

pub fn run_all() -> Result<(), Box<dyn std::error::Error>> {
    python_aes_example();
    python_signature_example();
    python_hash_example();
    python_key_management_example();
    python_web_example();
    Ok(())
}

fn main() {
    if let Err(e) = run_all() {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}