Struct Signatory

Source
pub struct Signatory { /* private fields */ }
Expand description

Struct responsible for signing operations.

Implementations§

Source§

impl Signatory

Source

pub fn new(key: String) -> Signatory

Creates a new instance of the Signatory struct with the provided secret key.

§Arguments
  • key - A String representing the secret key to be used in signing.
Source

pub fn generate_sign( &self, params: HashMap<String, Value>, ) -> Result<String, Box<dyn Error>>

Generates a signature from a given HashMap<String, Value>.

Steps:

  1. Removes the sign field from params if it exists.
  2. Sorts the remaining keys in ascending order.
  3. Builds a query string from key-value pairs.
  4. Appends the secret key to the query string.
  5. Computes the MD5 hash of the string.
  6. Converts the hash to an uppercase hexadecimal string and returns it.
§Arguments
  • params - A mutable HashMap<String, Value> containing the parameters to sign.
§Returns

Returns the generated signature as a Result<String, Box<dyn Error>>.

Source

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:

  1. Adds the current timestamp if the timestamp key does not exist.
  2. Adds the generated signature to params if the sign key does not exist.
  3. Serializes the params to a JSON string.
  4. Base64 encodes the JSON string and returns it.
§Arguments
  • params - A mutable HashMap<String, Value> containing the parameters to encode.
§Returns

Returns the Base64 encoded string as Result<String, Box<dyn Error>>.

Source

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:

  1. Base64 decodes the input string.
  2. Converts the resulting bytes into a UTF-8 string.
  3. Deserializes the string into a HashMap.
§Arguments
  • params - A Base64-encoded string representing serialized HashMap<String, Value>.
§Returns

Returns the decoded HashMap<String, Value> as Result<HashMap<String, Value>, Box<dyn Error>>.

Source

pub fn check_sign(&self, params: HashMap<String, Value>, sign: String) -> bool

Verifies the integrity of the provided signature.

This function:

  1. Regenerates the signature based on the params.
  2. Compares the regenerated signature with the provided sign.
§Arguments
  • params - A HashMap<String, Value> representing the parameters to verify.
  • sign - A String representing the signature to verify.
§Returns

Returns true if the signature matches, otherwise false.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.