Skip to content

Commit

Permalink
fix advanced options for password-hashing
Browse files Browse the repository at this point in the history
and allow raising scrypt ram usage past OpenSSL's default 32 MiB
  • Loading branch information
9001 committed Nov 15, 2024
1 parent fc3bbb7 commit 1f17752
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions copyparty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,9 @@ def get_sects():
use argon2id with timecost 3, 256 MiB, 4 threads, version 19 (0x13/v1.3)
\033[36m--ah-alg scrypt\033[0m # which is the same as:
\033[36m--ah-alg scrypt,13,2,8,4\033[0m
use scrypt with cost 2**13, 2 iterations, blocksize 8, 4 threads
\033[36m--ah-alg scrypt,13,2,8,4,32\033[0m
use scrypt with cost 2**13, 2 iterations, blocksize 8, 4 threads,
and allow using up to 32 MiB RAM (ram=cost*blksz roughly)
\033[36m--ah-alg sha2\033[0m # which is the same as:
\033[36m--ah-alg sha2,424242\033[0m
Expand Down
18 changes: 10 additions & 8 deletions copyparty/pwhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ class PWHash(object):
def __init__(self, args: argparse.Namespace):
self.args = args

try:
alg, ac = args.ah_alg.split(",")
except:
alg = args.ah_alg
ac = {}

zsl = args.ah_alg.split(",")
alg = zsl[0]
if alg == "none":
alg = ""

self.alg = alg
self.ac = ac
self.ac = zsl[1:]
if not alg:
self.on = False
self.hash = unicode
Expand Down Expand Up @@ -90,17 +86,23 @@ def _gen_scrypt(self, plain: str) -> str:
its = 2
blksz = 8
para = 4
ramcap = 0 # openssl 1.1 = 32 MiB
try:
cost = 2 << int(self.ac[0])
its = int(self.ac[1])
blksz = int(self.ac[2])
para = int(self.ac[3])
ramcap = int(self.ac[4]) * 1024 * 1024
except:
pass

cfg = {"salt": self.salt, "n": cost, "r": blksz, "p": para, "dklen": 24}
if ramcap:
cfg["maxmem"] = ramcap

ret = plain.encode("utf-8")
for _ in range(its):
ret = hashlib.scrypt(ret, salt=self.salt, n=cost, r=blksz, p=para, dklen=24)
ret = hashlib.scrypt(ret, **cfg)

return "+" + base64.urlsafe_b64encode(ret).decode("utf-8")

Expand Down

0 comments on commit 1f17752

Please sign in to comment.