MCPcopy Create free account
hub / github.com/echQoQ/RustSL / process

Function process

encrypt_lib/ecc.py:16–52  ·  view source on GitHub ↗
(data, args)

Source from the content-addressed store, hash-verified

14 return sha.digest()
15
16def process(data, args):
17 # Generate ECC private key A
18 priv_a = ec.generate_private_key(ec.SECP256R1())
19
20 # Generate random ECC private key B
21 priv_b = ec.generate_private_key(ec.SECP256R1())
22 pub_b = priv_b.public_key()
23
24 # Perform ECDH: shared_secret = priv_a.exchange(ec.ECDH(), pub_b)
25 shared_secret = priv_a.exchange(ec.ECDH(), pub_b)
26
27 # Derive AES key using HKDF
28 hkdf = HKDF(
29 algorithm=hashes.SHA256(),
30 length=32,
31 salt=None,
32 info=b'',
33 )
34 aes_key = hkdf.derive(shared_secret)
35
36 # Generate nonce
37 nonce = os.urandom(12) # AES-GCM uses 12-byte nonce
38
39 # Encrypt data
40 aesgcm = AESGCM(aes_key)
41 ciphertext = aesgcm.encrypt(nonce, data, None)
42
43 # Serialize keys
44 priv_a_bytes = priv_a.private_numbers().private_value.to_bytes(32, 'big')
45 pub_b_bytes = pub_b.public_bytes(
46 encoding=serialization.Encoding.X962,
47 format=serialization.PublicFormat.CompressedPoint
48 ) # 33 bytes: 0x02/0x03 + x
49
50 # Final output: priv_a (32) + pub_b (33) + nonce (12) + ciphertext
51 final = priv_a_bytes + pub_b_bytes + nonce + ciphertext
52 return final

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected