Skip to content

Commit c864885

Browse files
authored
Add _blake2 consts (RustPython#8053)
* Add `_blake2` consts * Update `test_hashlib.py` to 3.14.5
1 parent 28addda commit c864885

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

Lib/test/test_hashlib.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ def __init__(self, *args, **kwargs):
127127
algorithms.add(algorithm.lower())
128128

129129
_blake2 = self._conditional_import_module('_blake2')
130+
blake2_hashes = {'blake2b', 'blake2s'}
130131
if _blake2:
131-
algorithms.update({'blake2b', 'blake2s'})
132+
algorithms.update(blake2_hashes)
133+
else:
134+
algorithms.difference_update(blake2_hashes)
132135

133136
self.constructors_to_test = {}
134137
for algorithm in algorithms:
@@ -220,7 +223,12 @@ def test_algorithms_available(self):
220223
# all available algorithms must be loadable, bpo-47101
221224
self.assertNotIn("undefined", hashlib.algorithms_available)
222225
for name in hashlib.algorithms_available:
223-
digest = hashlib.new(name, usedforsecurity=False)
226+
with self.subTest(name):
227+
try:
228+
_ = hashlib.new(name, usedforsecurity=False)
229+
except ValueError as exc:
230+
self.skip_if_blake2_not_builtin(name, exc)
231+
raise
224232

225233
def test_usedforsecurity_true(self):
226234
hashlib.new("sha256", usedforsecurity=True)
@@ -444,6 +452,7 @@ def test_sha3_256_update_over_4gb(self):
444452
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")
445453

446454
@requires_resource('cpu')
455+
@requires_blake2
447456
def test_blake2_update_over_4gb(self):
448457
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
449458
# common loop macro logic.
@@ -734,6 +743,12 @@ def test_case_sha512_3(self):
734743
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
735744
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
736745

746+
def skip_if_blake2_not_builtin(self, name, skip_reason):
747+
# blake2 builtins may be absent if python built with
748+
# a subset of --with-builtin-hashlib-hashes or none.
749+
if "blake2" in name and "blake2" not in builtin_hashes:
750+
self.skipTest(skip_reason)
751+
737752
def check_blake2(self, constructor, salt_size, person_size, key_size,
738753
digest_size, max_offset):
739754
self.assertEqual(constructor.SALT_SIZE, salt_size)

crates/stdlib/src/blake2.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ mod _blake2 {
77
use crate::hashlib::_hashlib::{BlakeHashArgs, local_blake2b, local_blake2s};
88
use crate::vm::{PyPayload, PyResult, VirtualMachine};
99

10+
#[pyattr(name = "_GIL_MINSIZE")]
11+
const GIL_MINSIZE: u16 = 2048;
12+
13+
#[pyattr]
14+
const BLAKE2B_SALT_SIZE: u8 = 16;
15+
16+
#[pyattr]
17+
const BLAKE2B_PERSON_SIZE: u8 = 16;
18+
19+
#[pyattr]
20+
const BLAKE2B_MAX_KEY_SIZE: u8 = 64;
21+
22+
#[pyattr]
23+
const BLAKE2B_MAX_DIGEST_SIZE: u8 = 64;
24+
25+
#[pyattr]
26+
const BLAKE2S_SALT_SIZE: u8 = 8;
27+
28+
#[pyattr]
29+
const BLAKE2S_PERSON_SIZE: u8 = 8;
30+
31+
#[pyattr]
32+
const BLAKE2S_MAX_KEY_SIZE: u8 = 32;
33+
34+
#[pyattr]
35+
const BLAKE2S_MAX_DIGEST_SIZE: u8 = 32;
36+
1037
#[pyfunction]
1138
fn blake2b(args: BlakeHashArgs, vm: &VirtualMachine) -> PyResult {
1239
Ok(local_blake2b(args, vm)?.into_pyobject(vm))

0 commit comments

Comments
 (0)