| 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)] |