pub struct Signatory { /* private fields */ }Expand description
Struct responsible for signing operations.
Implementations§
Source§impl Signatory
impl Signatory
Sourcepub fn new(key: String) -> Signatory
pub fn new(key: String) -> Signatory
Creates a new instance of the Signatory struct with the provided secret key.
§Arguments
key- AStringrepresenting the secret key to be used in signing.
Sourcepub fn generate_sign(
&self,
params: HashMap<String, Value>,
) -> Result<String, Box<dyn Error>>
pub fn generate_sign( &self, params: HashMap<String, Value>, ) -> Result<String, Box<dyn Error>>
Generates a signature from a given HashMap<String, Value>.
Steps:
- Removes the
signfield fromparamsif it exists. - Sorts the remaining keys in ascending order.
- Builds a query string from key-value pairs.
- Appends the secret key to the query string.
- Computes the MD5 hash of the string.
- Converts the hash to an uppercase hexadecimal string and returns it.
§Arguments
params- A mutableHashMap<String, Value>containing the parameters to sign.
§Returns
Returns the generated signature as a Result<String, Box<dyn Error>>.
Sourcepub fn to_string(
&self,
params: HashMap<String, Value>,
) -> Result<String, Box<dyn Error>>
pub fn to_string( &self, params: HashMap<String, Value>, ) -> Result<String, Box<dyn Error>>
Converts a HashMap<String, Value> into a Base64-encoded string.
This function:
- Adds the current timestamp if the
timestampkey does not exist. - Adds the generated signature to
paramsif thesignkey does not exist. - Serializes the
paramsto a JSON string. - Base64 encodes the JSON string and returns it.
§Arguments
params- A mutableHashMap<String, Value>containing the parameters to encode.
§Returns
Returns the Base64 encoded string as Result<String, Box<dyn Error>>.
Sourcepub fn decrypt(
&self,
params: String,
) -> Result<HashMap<String, Value>, Box<dyn Error>>
pub fn decrypt( &self, params: String, ) -> Result<HashMap<String, Value>, Box<dyn Error>>
Decodes a Base64-encoded string into a HashMap<String, Value>.
This function:
- Base64 decodes the input string.
- Converts the resulting bytes into a UTF-8 string.
- Deserializes the string into a
HashMap.
§Arguments
params- A Base64-encoded string representing serializedHashMap<String, Value>.
§Returns
Returns the decoded HashMap<String, Value> as Result<HashMap<String, Value>, Box<dyn Error>>.
Sourcepub fn check_sign(&self, params: HashMap<String, Value>, sign: String) -> bool
pub fn check_sign(&self, params: HashMap<String, Value>, sign: String) -> bool
Verifies the integrity of the provided signature.
This function:
- Regenerates the signature based on the
params. - Compares the regenerated signature with the provided
sign.
§Arguments
params- AHashMap<String, Value>representing the parameters to verify.sign- AStringrepresenting the signature to verify.
§Returns
Returns true if the signature matches, otherwise false.
Auto Trait Implementations§
impl Freeze for Signatory
impl RefUnwindSafe for Signatory
impl Send for Signatory
impl Sync for Signatory
impl Unpin for Signatory
impl UnwindSafe for Signatory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more