Repository navigation
Expand file tree
/
Copy pathexample_secure_buffer.py
More file actions
69 lines (55 loc) · 1.87 KB
/
Copy pathexample_secure_buffer.py
File metadata and controls
69 lines (55 loc) · 1.87 KB
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
#!/usr/bin/env python3
"""
libzupt - SecureBuffer Example
Shows how to use SecureBuffer for zeroizing sensitive data
"""
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
import zupt
def main():
print("=" * 60)
print("libzupt - SecureBuffer Example")
print("=" * 60)
print()
# Create a SecureBuffer from bytes
print("1. Creating SecureBuffer from bytes...")
secret = b"My secret password123"
buffer = zupt.SecureBuffer(secret)
print(f" Buffer size: {len(buffer)} bytes")
print(f" Buffer content: {buffer.to_string()}")
print()
# Create a SecureBuffer with specific size
print("2. Creating empty SecureBuffer...")
empty_buffer = zupt.SecureBuffer(64)
print(f" Empty buffer size: {len(empty_buffer)} bytes")
print()
# Encrypt using SecureBuffer
print("3. Encrypting with SecureBuffer...")
keygen = zupt.KeyGenerator()
keypair = keygen.generate_keypair()
encryptor = zupt.Encryptor(keypair.public_key)
decryptor = zupt.Decryptor(keypair.secret_key)
ciphertext, enc_header = encryptor.encrypt_secure(buffer)
print(f" Ciphertext size: {len(ciphertext)} bytes")
print()
# Decrypt to SecureBuffer (zeroized on destruction)
print("4. Decrypting to SecureBuffer...")
decrypted_buffer = decryptor.decrypt_secure(ciphertext, enc_header)
print(f" Decrypted buffer size: {len(decrypted_buffer)} bytes")
print(f" Decrypted content: {decrypted_buffer.to_string()}")
print()
# Verify
assert decrypted_buffer.to_bytes() == secret
print("5. Verification: SUCCESS")
print()
# Demonstrate secure wiping
print("6. Securely wiping buffer...")
buffer.zeroize()
print(" Buffer zeroized (content is now zero)")
print()
print("=" * 60)
print("SecureBuffer example passed!")
print("=" * 60)
if __name__ == "__main__":
main()