MCPcopy Index your code
hub / github.com/RustPython/RustPython / compare_digest

Function compare_digest

crates/stdlib/src/hashlib.rs:721–745  ·  view source on GitHub ↗
(
        a: ArgStrOrBytesLike,
        b: ArgStrOrBytesLike,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

719
720 #[pyfunction]
721 fn compare_digest(
722 a: ArgStrOrBytesLike,
723 b: ArgStrOrBytesLike,
724 vm: &VirtualMachine,
725 ) -> PyResult<bool> {
726 use constant_time_eq::constant_time_eq;
727
728 match (&a, &b) {
729 (ArgStrOrBytesLike::Str(a), ArgStrOrBytesLike::Str(b)) => {
730 if !a.isascii() || !b.isascii() {
731 return Err(vm.new_type_error(
732 "comparing strings with non-ASCII characters is not supported",
733 ));
734 }
735 Ok(constant_time_eq(a.as_bytes(), b.as_bytes()))
736 }
737 (ArgStrOrBytesLike::Buf(a), ArgStrOrBytesLike::Buf(b)) => {
738 Ok(a.with_ref(|a| b.with_ref(|b| constant_time_eq(a, b))))
739 }
740 _ => Err(vm.new_type_error(format!(
741 "a bytes-like object is required, not '{}'",
742 b.as_object().class().name()
743 ))),
744 }
745 }
746
747 #[derive(FromArgs, Debug)]
748 #[allow(unused)]

Callers

nothing calls this directly

Calls 4

with_refMethod · 0.80
ErrClass · 0.50
isasciiMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected