Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 4a531f9

Browse files
committed
Use Crypto counter class for CTR
1 parent 3d3a469 commit 4a531f9

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

ethereum/keys.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from sha3 import sha3_256
2727
from Crypto.Cipher import AES
2828
from Crypto.Hash import SHA256
29+
from Crypto.Util import Counter
2930

3031
# TODO: make it compatible!
3132

@@ -46,23 +47,15 @@
4647

4748
def aes_ctr_encrypt(text, key, params):
4849
iv = big_endian_to_int(decode_hex(params["iv"]))
49-
o = [0]
50-
51-
def ctr():
52-
o[0] += 1
53-
return zpad(int_to_big_endian(((o[0] - 1) + iv) % 2**128), 16)
50+
ctr = Counter.new(128, initial_value=iv, allow_wraparound=True)
5451
mode = AES.MODE_CTR
5552
encryptor = AES.new(key, mode, counter=ctr)
5653
return encryptor.encrypt(text)
5754

5855

5956
def aes_ctr_decrypt(text, key, params):
6057
iv = big_endian_to_int(decode_hex(params["iv"]))
61-
o = [0]
62-
63-
def ctr():
64-
o[0] += 1
65-
return zpad(int_to_big_endian(((o[0] - 1) + iv) % 2**128), 16)
58+
ctr = Counter.new(128, initial_value=iv, allow_wraparound=True)
6659
mode = AES.MODE_CTR
6760
encryptor = AES.new(key, mode, counter=ctr)
6861
return encryptor.decrypt(text)

0 commit comments

Comments
 (0)