Skip to content

Commit cf254de

Browse files
authored
Merge pull request #78 from yotamofek/master
digest 0.10, hmac 0.12
2 parents 370ff38 + 47e8fbb commit cf254de

File tree

10 files changed

+70
-46
lines changed

10 files changed

+70
-46
lines changed

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ features = ["openssl"]
1616

1717
[dependencies]
1818
base64 = "0.13"
19-
crypto-mac = "0.11"
20-
digest = "0.9"
21-
hmac = "0.11"
22-
sha2 = "0.9"
19+
crypto-common = "0.1"
20+
digest = "0.10"
21+
hmac = { version = "0.12", features = ["reset"] }
22+
sha2 = "0.10"
2323
serde = { version = "1.0", features = ["derive"] }
2424
serde_json = "1.0"
2525

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Claims can be any `serde::Serialize` type, usually derived with
2020
`serde_derive`.
2121

2222
```rust
23-
use hmac::{Hmac, NewMac};
23+
use hmac::{Hmac, Mac};
2424
use jwt::SignWithKey;
2525
use sha2::Sha256;
2626
use std::collections::BTreeMap;
@@ -40,7 +40,7 @@ Claims can be any `serde::Deserialize` type, usually derived with
4040
`serde_derive`.
4141

4242
```rust
43-
use hmac::{Hmac, NewMac};
43+
use hmac::{Hmac, Mac};
4444
use jwt::VerifyWithKey;
4545
use sha2::Sha256;
4646
use std::collections::BTreeMap;
@@ -64,7 +64,7 @@ fields, but any type that implements `JoseHeader` can be used.
6464
Both header and claims have to implement `serde::Serialize`.
6565

6666
```rust
67-
use hmac::{Hmac, NewMac};
67+
use hmac::{Hmac, Mac};
6868
use jwt::{AlgorithmType, Header, SignWithKey, Token};
6969
use sha2::Sha384;
7070
use std::collections::BTreeMap;
@@ -87,7 +87,7 @@ assert_eq!(token.as_str(), "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJzb21lb25lIn0.WM_WnPU
8787
Both header and claims have to implement `serde::Deserialize`.
8888

8989
```rust
90-
use hmac::{Hmac, NewMac};
90+
use hmac::{Hmac, Mac};
9191
use jwt::{AlgorithmType, Header, Token, VerifyWithKey};
9292
use sha2::Sha384;
9393
use std::collections::BTreeMap;
@@ -114,7 +114,7 @@ For the trait `VerifyWithStore`, the key id from the deserialized header will be
114114
to use.
115115

116116
```rust
117-
use hmac::{Hmac, NewMac};
117+
use hmac::{Hmac, Mac};
118118
use jwt::{Header, SignWithStore, Token, VerifyWithStore};
119119
use sha2::Sha512;
120120
use std::collections::BTreeMap;

examples/custom_claims.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hmac::{Hmac, NewMac};
1+
use hmac::{Hmac, Mac};
22
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
33
use serde::{Deserialize, Serialize};
44
use sha2::Sha256;

examples/hs256.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hmac::{Hmac, NewMac};
1+
use hmac::{Hmac, Mac};
22
use jwt::{RegisteredClaims, SignWithKey, VerifyWithKey};
33
use sha2::Sha256;
44

src/algorithm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! module. The `none` algorithm is explicitly not supported.
55
//! ## Examples
66
//! ```
7-
//! use hmac::{Hmac, NewMac};
7+
//! use hmac::{Hmac, Mac};
88
//! use sha2::Sha256;
99
//!
1010
//! let hs256_key: Hmac<Sha256> = Hmac::new_from_slice(b"some-secret").unwrap();

src/algorithm/rust_crypto.rs

+40-17
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
//! According to that organization, only hmac is safely implemented at the
33
//! moment.
44
5-
use crypto_mac::Mac;
6-
use digest::generic_array::ArrayLength;
7-
use digest::{BlockInput, FixedOutput, Reset, Update};
5+
use digest::core_api::{CoreProxy, FixedOutputCore};
6+
use digest::{
7+
block_buffer::Eager,
8+
consts::U256,
9+
core_api::{BlockSizeUser, BufferKindUser},
10+
generic_array::typenum::{IsLess, Le, NonZero},
11+
HashMarker, Mac,
12+
};
813
use hmac::Hmac;
914

1015
use crate::algorithm::{AlgorithmType, SigningAlgorithm, VerifyingAlgorithm};
@@ -34,16 +39,22 @@ type_level_algorithm_type!(sha2::Sha512, AlgorithmType::Hs512);
3439

3540
impl<D> SigningAlgorithm for Hmac<D>
3641
where
37-
D: Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType,
38-
D::BlockSize: ArrayLength<u8>,
39-
D::OutputSize: ArrayLength<u8>,
42+
D: CoreProxy + TypeLevelAlgorithmType,
43+
D::Core: HashMarker
44+
+ BufferKindUser<BufferKind = Eager>
45+
+ FixedOutputCore
46+
+ digest::Reset
47+
+ Default
48+
+ Clone,
49+
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
50+
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
4051
{
4152
fn algorithm_type(&self) -> AlgorithmType {
4253
D::algorithm_type()
4354
}
4455

4556
fn sign(&self, header: &str, claims: &str) -> Result<String, Error> {
46-
let hmac = get_hmac_with_data(&self, header, claims);
57+
let hmac = get_hmac_with_data(self, header, claims);
4758
let mac_result = hmac.finalize();
4859
let code = mac_result.into_bytes();
4960
Ok(base64::encode_config(&code, base64::URL_SAFE_NO_PAD))
@@ -52,26 +63,38 @@ where
5263

5364
impl<D> VerifyingAlgorithm for Hmac<D>
5465
where
55-
D: Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType,
56-
D::BlockSize: ArrayLength<u8>,
57-
D::OutputSize: ArrayLength<u8>,
66+
D: CoreProxy + TypeLevelAlgorithmType,
67+
D::Core: HashMarker
68+
+ BufferKindUser<BufferKind = Eager>
69+
+ FixedOutputCore
70+
+ digest::Reset
71+
+ Default
72+
+ Clone,
73+
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
74+
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
5875
{
5976
fn algorithm_type(&self) -> AlgorithmType {
6077
D::algorithm_type()
6178
}
6279

6380
fn verify_bytes(&self, header: &str, claims: &str, signature: &[u8]) -> Result<bool, Error> {
6481
let hmac = get_hmac_with_data(self, header, claims);
65-
hmac.verify(&signature)?;
82+
hmac.verify_slice(signature)?;
6683
Ok(true)
6784
}
6885
}
6986

7087
fn get_hmac_with_data<D>(hmac: &Hmac<D>, header: &str, claims: &str) -> Hmac<D>
7188
where
72-
D: Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType,
73-
D::BlockSize: ArrayLength<u8>,
74-
D::OutputSize: ArrayLength<u8>,
89+
D: CoreProxy,
90+
D::Core: HashMarker
91+
+ BufferKindUser<BufferKind = Eager>
92+
+ FixedOutputCore
93+
+ digest::Reset
94+
+ Default
95+
+ Clone,
96+
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
97+
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
7598
{
7699
let mut hmac = hmac.clone();
77100
hmac.reset();
@@ -85,7 +108,7 @@ where
85108
mod tests {
86109
use crate::algorithm::{SigningAlgorithm, VerifyingAlgorithm};
87110
use crate::error::Error;
88-
use crypto_mac::NewMac;
111+
use digest::Mac;
89112
use hmac::Hmac;
90113
use sha2::Sha256;
91114

@@ -96,7 +119,7 @@ mod tests {
96119
let expected_signature = "TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
97120

98121
let signer: Hmac<Sha256> = Hmac::new_from_slice(b"secret")?;
99-
let computed_signature = SigningAlgorithm::sign(&signer, &header, &claims)?;
122+
let computed_signature = SigningAlgorithm::sign(&signer, header, claims)?;
100123

101124
assert_eq!(computed_signature, expected_signature);
102125
Ok(())
@@ -110,7 +133,7 @@ mod tests {
110133

111134
let verifier: Hmac<Sha256> = Hmac::new_from_slice(b"secret")?;
112135
assert!(VerifyingAlgorithm::verify(
113-
&verifier, &header, &claims, &signature
136+
&verifier, header, claims, signature
114137
)?);
115138
Ok(())
116139
}

src/error.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::fmt;
22
use std::string::FromUtf8Error;
33

44
use base64::DecodeError;
5-
use crypto_mac::{InvalidKeyLength, MacError};
5+
use crypto_common::InvalidLength;
6+
use digest::MacError;
67
use serde_json::Error as JsonError;
78

89
use self::Error::*;
@@ -21,7 +22,7 @@ pub enum Error {
2122
NoKeyWithKeyId(String),
2223
NoSignatureComponent,
2324
RustCryptoMac(MacError),
24-
RustCryptoMacKeyLength(InvalidKeyLength),
25+
RustCryptoMacKeyLength(InvalidLength),
2526
TooManyComponents,
2627
Utf8(FromUtf8Error),
2728
#[cfg(feature = "openssl")]
@@ -69,6 +70,6 @@ error_wrap!(DecodeError, Base64);
6970
error_wrap!(JsonError, Json);
7071
error_wrap!(FromUtf8Error, Utf8);
7172
error_wrap!(MacError, RustCryptoMac);
72-
error_wrap!(InvalidKeyLength, RustCryptoMacKeyLength);
73+
error_wrap!(InvalidLength, RustCryptoMacKeyLength);
7374
#[cfg(feature = "openssl")]
7475
error_wrap!(openssl::error::ErrorStack, Error::OpenSsl);

src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Claims can be any `serde::Serialize` type, usually derived with
66
//! `serde_derive`.
77
//! ```rust
8-
//! use hmac::{Hmac, NewMac};
8+
//! use hmac::{Hmac, Mac};
99
//! use jwt::SignWithKey;
1010
//! use sha2::Sha256;
1111
//! use std::collections::BTreeMap;
@@ -25,7 +25,7 @@
2525
//! Claims can be any `serde::Deserialize` type, usually derived with
2626
//! `serde_derive`.
2727
//! ```rust
28-
//! use hmac::{Hmac, NewMac};
28+
//! use hmac::{Hmac, Mac};
2929
//! use jwt::VerifyWithKey;
3030
//! use sha2::Sha256;
3131
//! use std::collections::BTreeMap;
@@ -47,7 +47,7 @@
4747
//! #### Signing
4848
//! Both header and claims have to implement `serde::Serialize`.
4949
//! ```rust
50-
//! use hmac::{Hmac, NewMac};
50+
//! use hmac::{Hmac, Mac};
5151
//! use jwt::{AlgorithmType, Header, SignWithKey, Token};
5252
//! use sha2::Sha384;
5353
//! use std::collections::BTreeMap;
@@ -70,7 +70,7 @@
7070
//! #### Verification
7171
//! Both header and claims have to implement `serde::Deserialize`.
7272
//! ```rust
73-
//! use hmac::{Hmac, NewMac};
73+
//! use hmac::{Hmac, Mac};
7474
//! use jwt::{AlgorithmType, Header, Token, VerifyWithKey};
7575
//! use sha2::Sha384;
7676
//! use std::collections::BTreeMap;
@@ -144,9 +144,9 @@ impl<H, C, S> Token<H, C, S> {
144144
}
145145
}
146146

147-
impl<H, C, S> Into<(H, C)> for Token<H, C, S> {
148-
fn into(self) -> (H, C) {
149-
(self.header, self.claims)
147+
impl<H, C, S> From<Token<H, C, S>> for (H, C) {
148+
fn from(token: Token<H, C, S>) -> Self {
149+
(token.header, token.claims)
150150
}
151151
}
152152

@@ -195,7 +195,7 @@ mod tests {
195195
use crate::Claims;
196196
use crate::Token;
197197
use hmac::Hmac;
198-
use hmac::NewMac;
198+
use hmac::Mac;
199199
use sha2::Sha256;
200200

201201
#[test]

src/token/signed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ impl<'a, H, C> Token<H, C, Signed> {
131131
}
132132
}
133133

134-
impl<H, C> Into<String> for Token<H, C, Signed> {
135-
fn into(self) -> String {
136-
self.signature.token_string
134+
impl<H, C> From<Token<H, C, Signed>> for String {
135+
fn from(token: Token<H, C, Signed>) -> Self {
136+
token.signature.token_string
137137
}
138138
}
139139

140140
#[cfg(test)]
141141
mod tests {
142142
use std::collections::BTreeMap;
143143

144-
use hmac::{Hmac, NewMac};
144+
use hmac::{Hmac, Mac};
145145
use serde::Serialize;
146146
use sha2::{Sha256, Sha512};
147147

src/token/verified.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149
use std::collections::{BTreeMap, HashMap};
150150
use std::iter::FromIterator;
151151

152-
use hmac::{Hmac, NewMac};
152+
use hmac::{Hmac, Mac};
153153
use serde::Deserialize;
154154
use sha2::{Sha256, Sha512};
155155

@@ -247,7 +247,7 @@ mod tests {
247247

248248
// Header {"alg":"HS512","kid":"second_key"}
249249
// Claims {"name":"Jane Doe"}
250-
const JANE_DOE_SECOND_KEY_TOKEN: &'static str = "eyJhbGciOiJIUzUxMiIsImtpZCI6InNlY29uZF9rZXkifQ.eyJuYW1lIjoiSmFuZSBEb2UifQ.t2ON5s8DDb2hefBIWAe0jaEcp-T7b2Wevmj0kKJ8BFxKNQURHpdh4IA-wbmBmqtiCnqTGoRdqK45hhW0AOtz0A";
250+
const JANE_DOE_SECOND_KEY_TOKEN: &str = "eyJhbGciOiJIUzUxMiIsImtpZCI6InNlY29uZF9rZXkifQ.eyJuYW1lIjoiSmFuZSBEb2UifQ.t2ON5s8DDb2hefBIWAe0jaEcp-T7b2Wevmj0kKJ8BFxKNQURHpdh4IA-wbmBmqtiCnqTGoRdqK45hhW0AOtz0A";
251251

252252
#[test]
253253
pub fn verify_claims_with_b_tree_map() -> Result<(), Error> {

0 commit comments

Comments
 (0)