forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cbf6c7
commit d9dd17f
Showing
11 changed files
with
2,629 additions
and
2,489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
[toolchain] | ||
channel = "1.54.0" | ||
components = [ "rustfmt" ] | ||
targets = [ "wasm32-unknown-unknown" ] | ||
profile = "minimal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use derive_more::{AsRef, From, Into}; | ||
|
||
#[derive(Clone)] | ||
pub struct Hmac { | ||
digest: crate::digest::Context, | ||
outer_key: Box<[u8]>, | ||
inner_key: Box<[u8]>, | ||
} | ||
|
||
impl Hmac { | ||
pub fn new( | ||
digest_algorithm: &str, | ||
secret_key: &[u8], | ||
) -> Result<Self, &'static str> { | ||
let mut digest = crate::digest::Context::new(digest_algorithm)?; | ||
|
||
assert!( | ||
digest.input_block_length() > digest.output_length(), | ||
"Digest algorithm is not compatible with HMAC. This should never happen." | ||
); | ||
|
||
let mut padded_key: Box<[u8]> = | ||
vec![0; digest.input_block_length()].to_owned().into(); | ||
|
||
if secret_key.len() <= digest.input_block_length() { | ||
padded_key.copy_from_slice(secret_key); | ||
} else { | ||
digest.update(secret_key); | ||
let digested_key = digest.digest_and_reset(None)?; | ||
padded_key.copy_from_slice(&digested_key); | ||
} | ||
|
||
let outer_key: Box<[u8]> = | ||
padded_key.iter().map(|byte| byte ^ 0b0101_1100).collect(); | ||
let inner_key: Box<[u8]> = | ||
padded_key.iter().map(|byte| byte ^ 0b0011_0110).collect(); | ||
|
||
digest.update(&inner_key); | ||
|
||
Ok(Hmac { | ||
digest, | ||
inner_key, | ||
outer_key, | ||
}) | ||
} | ||
|
||
pub fn algorithm_name(&self) -> impl AsRef<str> { | ||
return format!("HMAC-{}", self.digest.algorithm_name().as_ref()); | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.digest.reset(); | ||
self.digest.update(&self.inner_key); | ||
} | ||
|
||
pub fn update(&mut self, data: &[u8]) { | ||
self.digest.update(data) | ||
} | ||
|
||
pub fn digest(&self) -> Result<Box<[u8]>, &'static str> { | ||
self.clone().digest_and_drop() | ||
} | ||
|
||
pub fn digest_and_reset(&mut self) -> Result<Box<[u8]>, &'static str> { | ||
let inner_digest = self.digest.digest_and_reset(None)?; | ||
self.digest.update(&self.outer_key); | ||
self.digest.update(&inner_digest); | ||
let digest = self.digest.digest_and_reset(None)?; | ||
self.digest.update(&self.inner_key); | ||
Ok(digest) | ||
} | ||
|
||
pub fn digest_and_drop(mut self) -> Result<Box<[u8]>, &'static str> { | ||
let inner_digest = self.digest.digest_and_reset(None)?; | ||
self.digest.update(&self.outer_key); | ||
self.digest.update(&inner_digest); | ||
self.digest.digest_and_drop(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters