Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add _sha2 module and openssl_md_meth_names to _hashlib for Python 3.1…
…4 compatibility

Co-authored-by: moreal <26626194+moreal@users.noreply.github.com>
  • Loading branch information
Copilot and moreal committed Feb 6, 2026
commit a03e9aeb847afe128056ef3199f2967c8dbfd760
26 changes: 26 additions & 0 deletions crates/stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,30 @@ pub mod _hashlib {
}
}
}

#[pyattr(once)]
fn openssl_md_meth_names(vm: &VirtualMachine) -> PyObjectRef {
// List of all hash algorithms that are available
// This is used by hashlib.py to populate algorithms_available
let names = vec![
"md5",
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
"sha3_224",
"sha3_256",
"sha3_384",
"sha3_512",
"shake_128",
"shake_256",
"blake2b",
"blake2s",
];
let names_iter = names.into_iter().map(|name| vm.ctx.new_str(name).into());
rustpython_vm::builtins::PyFrozenSet::from_iter(vm, names_iter)
.expect("Creating openssl_md_meth_names frozen set must succeed")
.to_pyobject(vm)
}
}
2 changes: 2 additions & 0 deletions crates/stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod blake2;
mod hashlib;
mod md5;
mod sha1;
mod sha2;
mod sha256;
mod sha3;
mod sha512;
Expand Down Expand Up @@ -157,6 +158,7 @@ pub fn stdlib_module_defs(ctx: &Context) -> Vec<&'static builtins::PyModuleDef>
#[cfg(any(unix, windows, target_os = "wasi"))]
select::module_def(ctx),
sha1::module_def(ctx),
sha2::module_def(ctx),
sha256::module_def(ctx),
sha3::module_def(ctx),
sha512::module_def(ctx),
Expand Down
33 changes: 33 additions & 0 deletions crates/stdlib/src/sha2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[pymodule]
mod _sha2 {
use crate::hashlib::_hashlib::{HashArgs, local_sha224, local_sha256, local_sha384, local_sha512};
use crate::vm::{Py, PyPayload, PyResult, VirtualMachine, builtins::PyModule};

#[pyfunction]
fn sha224(args: HashArgs, vm: &VirtualMachine) -> PyResult {
Ok(local_sha224(args).into_pyobject(vm))
}

#[pyfunction]
fn sha256(args: HashArgs, vm: &VirtualMachine) -> PyResult {
Ok(local_sha256(args).into_pyobject(vm))
}

#[pyfunction]
fn sha384(args: HashArgs, vm: &VirtualMachine) -> PyResult {
Ok(local_sha384(args).into_pyobject(vm))
}

#[pyfunction]
fn sha512(args: HashArgs, vm: &VirtualMachine) -> PyResult {
Ok(local_sha512(args).into_pyobject(vm))
}

pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py<PyModule>) -> PyResult<()> {
let _ = vm.import("_hashlib", 0);
__module_exec(vm, module);
Ok(())
}
}

pub(crate) use _sha2::module_def;