@@ -19,7 +19,7 @@ Other language versions:
19
19
- [ Java] ( https://github.com/ecies/java )
20
20
- [ Dart] ( https://github.com/ecies/dart )
21
21
22
- You can also check a web backend demo [ here ] ( https://github.com/ecies/py-demo ) .
22
+ You can also check a [ web backend demo] ( https://github.com/ecies/py-demo ) .
23
23
24
24
## Install
25
25
@@ -29,6 +29,8 @@ Or `pip install 'eciespy[eth]'` to install `eth-keys` as well.
29
29
30
30
## Quick Start
31
31
32
+ ### Secp256k1
33
+
32
34
``` python
33
35
>> > from ecies.keys import PrivateKey
34
36
>> > from ecies import encrypt, decrypt
@@ -39,11 +41,24 @@ Or `pip install 'eciespy[eth]'` to install `eth-keys` as well.
39
41
>> > decrypt(sk_bytes, encrypt(pk_bytes, data)).decode()
40
42
' hello world🌍'
41
43
>> > sk_hex = sk.to_hex() # hex str
42
- >> > pk_hex = sk.public_key.to_hex() # hex str
44
+ >> > pk_hex = sk.public_key.to_hex(True ) # hex str
43
45
>> > decrypt(sk_hex, encrypt(pk_hex, data)).decode()
44
46
' hello world🌍'
45
47
```
46
48
49
+ ### X25519
50
+
51
+ ``` python
52
+ >> > from ecies.keys import PrivateKey
53
+ >> > from ecies import encrypt, decrypt
54
+ >> > from ecies.config import ECIES_CONFIG
55
+ >> > ECIES_CONFIG .elliptic_curve = ' x25519'
56
+ >> > data = ' hello world🌍' .encode()
57
+ >> > sk = PrivateKey(' x25519' )
58
+ >> > decrypt(sk.secret, encrypt(sk.public_key.to_bytes(), data)).decode()
59
+ ' hello world🌍'
60
+ ```
61
+
47
62
Or just use a builtin command ` eciespy ` in your favorite [ command line] ( #command-line-interface ) .
48
63
49
64
## API
@@ -120,26 +135,31 @@ $ rm sk pk data enc_data
120
135
Ephemeral key format in the payload and shared key in the key derivation can be configured as compressed or uncompressed format.
121
136
122
137
``` py
123
- from .consts import COMPRESSED_PUBLIC_KEY_SIZE , UNCOMPRESSED_PUBLIC_KEY_SIZE
124
-
138
+ EllipticCurve = Literal[" secp256k1" , " x25519" ]
125
139
SymmetricAlgorithm = Literal[" aes-256-gcm" , " xchacha20" ]
126
140
NonceLength = Literal[12 , 16 ] # only for aes-256-gcm, xchacha20 will always be 24
127
141
128
142
129
143
@dataclass ()
130
144
class Config :
145
+ elliptic_curve: EllipticCurve = " secp256k1"
131
146
is_ephemeral_key_compressed: bool = False
132
147
is_hkdf_key_compressed: bool = False
133
148
symmetric_algorithm: SymmetricAlgorithm = " aes-256-gcm"
134
149
symmetric_nonce_length: NonceLength = 16
135
150
136
151
@ property
137
152
def ephemeral_key_size (self ):
138
- return (
139
- COMPRESSED_PUBLIC_KEY_SIZE
140
- if self .is_ephemeral_key_compressed
141
- else UNCOMPRESSED_PUBLIC_KEY_SIZE
142
- )
153
+ if self .elliptic_curve == " secp256k1" :
154
+ return (
155
+ COMPRESSED_PUBLIC_KEY_SIZE
156
+ if self .is_ephemeral_key_compressed
157
+ else UNCOMPRESSED_PUBLIC_KEY_SIZE
158
+ )
159
+ elif self .elliptic_curve == " x25519" :
160
+ return CURVE25519_PUBLIC_KEY_SIZE
161
+ else :
162
+ raise NotImplementedError
143
163
144
164
145
165
ECIES_CONFIG = Config()
0 commit comments