Skip to content

Commit e7a9925

Browse files
authored
perf: reduce allocation for string (#49)
1 parent fdc8cd2 commit e7a9925

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

core/lib/src/parser/signature.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ use sha2::Sha256;
3131
/// // NG
3232
/// }
3333
/// ```
34-
pub fn validate_signature(channel_secret: String, signature: String, body: String) -> bool {
34+
pub fn validate_signature(channel_secret: &str, signature: &str, body: &str) -> bool {
3535
type HmacSha256 = Hmac<Sha256>;
3636

3737
let mut mac = HmacSha256::new_from_slice(channel_secret.as_bytes())
3838
.expect("HMAC can take key of any size");
3939
mac.update(body.as_bytes());
4040
let mut buf = String::new();
41-
general_purpose::STANDARD.encode_string(&mac.finalize().into_bytes().to_vec(), &mut buf);
41+
general_purpose::STANDARD.encode_string(mac.finalize().into_bytes(), &mut buf);
4242
buf == signature
4343
}

examples/actix_web_example/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn callback(signature: Signature, bytes: web::Bytes) -> Result<HttpRespons
2626

2727
let body: &str = &String::from_utf8(bytes.to_vec()).unwrap();
2828

29-
if !validate_signature(channel_secret.to_string(), signature.key, body.to_string()) {
29+
if !validate_signature(channel_secret, &signature.key, body) {
3030
return Err(ErrorBadRequest("x-line-signature is invalid."));
3131
}
3232

examples/rocket_example/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn world(signature: Signature, body: String) -> (Status, &'static str) {
2626
println!("{signature:#?}");
2727
println!("{body:#?}");
2828

29-
if !validate_signature(channel_secret.to_string(), signature.key, body.clone()) {
29+
if !validate_signature(channel_secret, &signature.key, &body) {
3030
return (Status::BadRequest, "x-line-signature is invalid.");
3131
}
3232

0 commit comments

Comments
 (0)