Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add hashlib hasher repr #5833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ def check_blocksize_name(self, name, block_size=0, digest_size=0,
# split for sha3_512 / _sha3.sha3 object
self.assertIn(name.split("_")[0], repr(m))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_blocksize_name(self):
self.check_blocksize_name('md5', 64, 16)
self.check_blocksize_name('sha1', 64, 20)
Expand Down Expand Up @@ -500,8 +498,6 @@ def test_extra_sha3(self):
self.check_sha3('shake_128', 256, 1344, b'\x1f')
self.check_sha3('shake_256', 512, 1088, b'\x1f')

# TODO: RUSTPYTHON implement all blake2 params
@unittest.expectedFailure
@requires_blake2
def test_blocksize_name_blake2(self):
self.check_blocksize_name('blake2b', 128, 64)
Expand Down
14 changes: 12 additions & 2 deletions stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ pub(crate) use _hashlib::make_module;
pub mod _hashlib {
use crate::common::lock::PyRwLock;
use crate::vm::{
PyObjectRef, PyPayload, PyResult, VirtualMachine,
Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyBytes, PyStrRef, PyTypeRef},
convert::ToPyObject,
function::{ArgBytesLike, ArgStrOrBytesLike, FuncArgs, OptionalArg},
protocol::PyBuffer,
types::Representable,
};
use blake2::{Blake2b512, Blake2s256};
use digest::{DynDigest, core_api::BlockSizeUser};
Expand Down Expand Up @@ -96,7 +97,7 @@ pub mod _hashlib {
}
}

#[pyclass]
#[pyclass(with(Representable))]
impl PyHasher {
fn new(name: &str, d: HashWrapper) -> Self {
PyHasher {
Expand Down Expand Up @@ -146,6 +147,15 @@ pub mod _hashlib {
}
}

impl Representable for PyHasher {
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
Ok(format!(
"<{} _hashlib.HASH object @ {:#x}>",
zelf.name, zelf as *const _ as usize
))
}
}

#[pyattr]
#[pyclass(module = "_hashlib", name = "HASHXOF")]
#[derive(PyPayload)]
Expand Down
Loading