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

Function hmac_new

crates/stdlib/src/hashlib.rs:759–798  ·  view source on GitHub ↗
(args: NewHMACHashArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

757
758 #[pyfunction]
759 fn hmac_new(args: NewHMACHashArgs, vm: &VirtualMachine) -> PyResult<PyHmac> {
760 let digestmod = args
761 .digestmod
762 .into_option()
763 .ok_or_else(|| vm.new_type_error("Missing required parameter 'digestmod'."))?;
764 let name = resolve_digestmod(&digestmod, vm)?;
765
766 let key_buf = args.key.borrow_buf();
767 let msg_data = args.msg.flatten();
768
769 macro_rules! make_hmac {
770 ($hash_ty:ty) => {{
771 let mut mac = <hmac::Hmac<$hash_ty> as Mac>::new_from_slice(&key_buf)
772 .map_err(|_| vm.new_value_error("invalid key length".to_owned()))?;
773 if let Some(ref m) = msg_data {
774 m.with_ref(|bytes| Mac::update(&mut mac, bytes));
775 }
776 Ok(PyHmac {
777 algo_name: name,
778 digest_size: <$hash_ty as OutputSizeUser>::output_size(),
779 block_size: <$hash_ty as BlockSizeUser>::block_size(),
780 ctx: PyRwLock::new(Box::new(TypedHmac(mac))),
781 })
782 }};
783 }
784
785 match name.as_str() {
786 "md5" => make_hmac!(Md5),
787 "sha1" => make_hmac!(Sha1),
788 "sha224" => make_hmac!(Sha224),
789 "sha256" => make_hmac!(Sha256),
790 "sha384" => make_hmac!(Sha384),
791 "sha512" => make_hmac!(Sha512),
792 "sha3_224" => make_hmac!(Sha3_224),
793 "sha3_256" => make_hmac!(Sha3_256),
794 "sha3_384" => make_hmac!(Sha3_384),
795 "sha3_512" => make_hmac!(Sha3_512),
796 _ => Err(unsupported_hash(&name, vm)),
797 }
798 }
799
800 #[pyfunction]
801 fn hmac_digest(args: HmacDigestArgs, vm: &VirtualMachine) -> PyResult<PyBytes> {

Callers

nothing calls this directly

Calls 8

resolve_digestmodFunction · 0.85
unsupported_hashFunction · 0.85
ok_or_elseMethod · 0.80
into_optionMethod · 0.80
ErrClass · 0.50
borrow_bufMethod · 0.45
flattenMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected