fn generate_sas_token(
        iot_hub_name: &str,
        key_name: &str,
        private_key: &str,
        expires_in_seconds: i64,
    ) -> azure_core::Result<String> {
        type HmacSHA256 = Hmac<Sha256>;
        let expiry_date = chrono::Utc::now() + chrono::Duration::seconds(expires_in_seconds);
        let expiry_date_seconds = expiry_date.timestamp();
        let data = format!(
            "{}.azure-devices.net\n{}",
            iot_hub_name, &expiry_date_seconds
        );

        let key = decode(private_key).with_context(ErrorKind::Other, || {
            format!("failed to decode the given private key: {private_key}")
        })?;

        let mut hmac = HmacSHA256::new_from_slice(key.as_ref())
            .with_context(ErrorKind::Other, || {
                format!("failed to use the given private key for the hashing algorithm: {key:?}")
            })?;

        hmac.update(data.as_bytes());
        let result = hmac.finalize();
        let sas_token: &str = &encode_config(&result.into_bytes(), base64::STANDARD);

        let encoded: String = url::form_urlencoded::Serializer::new(String::new())
            .append_pair("sr", &format!("{}.azure-devices.net", iot_hub_name))
            .append_pair("sig", sas_token)
            .append_pair("skn", key_name)
            .append_pair("se", &expiry_date_seconds.to_string())
            .finish();

        Ok(format!("SharedAccessSignature {}", encoded))
    }
/// Generate a new SAS token to use for authentication with IoT Hub
Generate a new SAS token to use for authentication with IoT Hub
Generate a new SAS token to use for authentication with IoT Hub
fn generate_sas_token(
        iot_hub_name: &str,
        key_name: &str,
        private_key: &str,
        expires_in_seconds: i64,
    ) -> azure_core::Result<String> {
        type HmacSHA256 = Hmac<Sha256>;
        let expiry_date = chrono::Utc::now() + chrono::Duration::seconds(expires_in_seconds);
        let expiry_date_seconds = expiry_date.timestamp();
        let data = format!(
            "{}.azure-devices.net\n{}",
            iot_hub_name, &expiry_date_seconds
        );
        let key = decode(private_key).with_context(ErrorKind::Other, || {
            format!("failed to decode the given private key: {private_key}")
        })?;
        let mut hmac = HmacSHA256::new_from_slice(key.as_ref())
            .with_context(ErrorKind::Other, || {
                format!("failed to use the given private key for the hashing algorithm: {key:?}")
            })?;
        hmac.update(data.as_bytes());
        let result = hmac.finalize();
        let sas_token: &str = &encode_config(&result.into_bytes(), base64::STANDARD);
        let encoded: String = url::form_urlencoded::Serializer::new(String::new())
            .append_pair("sr", &format!("{}.azure-devices.net", iot_hub_name))
            .append_pair("sig", sas_token)
            .append_pair("skn", key_name)
            .append_pair("se", &expiry_date_seconds.to_string())
            .finish();
        Ok(format!("SharedAccessSignature {}", encoded))
    }
fn generate_sas_token ( iot_hub_name : & str , key_name : & str , private_key : & str , expires_in_seconds : i64 , ) -> azure_core :: Result < String > { type HmacSHA256 = Hmac < Sha256 > ; let expiry_date = chrono :: Utc :: now ( ) + chrono :: Duration :: seconds ( expires_in_seconds ) ; let expiry_date_seconds = expiry_date . timestamp ( ) ; let data = format ! ( " \n " iot_hub_name expiry_date_seconds ) ; let key = decode ( private_key ) . with_context ( ErrorKind :: Other , | | { format ! ( " " ) } ) ? ; let mut hmac = HmacSHA256 :: new_from_slice ( key . as_ref ( ) ) . with_context ( ErrorKind :: Other , | | { format ! ( " " ) } ) ? ; hmac . update ( data . as_bytes ( ) ) ; let result = hmac . finalize ( ) ; let sas_token : & str = & encode_config ( & result . into_bytes ( ) , base64 :: STANDARD ) ; let encoded : String = url :: form_urlencoded :: Serializer :: new ( String :: new ( ) ) . append_pair ( " " , & format ! ( " " iot_hub_name ) ) . append_pair ( " " , sas_token ) . append_pair ( " " , key_name ) . append_pair ( " " , & expiry_date_seconds . to_string ( ) ) . finish ( ) ; Ok ( format ! ( " " encoded ) ) }
Generate a new SAS token to use for authentication with IoT Hub
Generate a new SAS token to use for authentication with IoT Hub
iot_hub_name
&str
key_name
&str
private_key
&str
expires_in_seconds
i64