-3
我使用https://github.com/DaGenix/rust-crypto並有一些奇怪的錯誤:在加密的參數類型不匹配:: HMAC :: HMAC ::新
extern crate crypto;
use crypto::sha2::{Sha256, Sha384, Sha512};
use crypto::hmac::Hmac;
use crypto::digest::Digest;
use crypto::mac::Mac;
enum MyType {
Type1,
Type2,
Type3
}
//......
let mut hmac = Hmac::new(match myType {
MyType::Type1 => Sha256::new(),
MyType::Type2 => Sha384::new(),
MyType::Type3 => Sha512::new()
}, my_secret.to_string().as_bytes()
);
的錯誤是:
error: match arms have incompatible types:
expected `crypto::sha2::Sha256`,
found `crypto::sha2::Sha384`
(expected struct `crypto::sha2::Sha256`,
found struct `crypto::sha2::Sha384`) [E0308]
let mut hmac = Hmac::new(match algorithm {
MyType::Type1 => Sha256::new(),
MyType::Type2 => Sha384::new(),
MyType::Type3 => Sha512::new(),
_ => panic!()
}, secret.to_string().as_bytes()
note: match arm with an incompatible type
MyType::Type2 => Sha384::new(),
^~~~~~~~~~~~~
help: methods from traits can only be called if the trait is implemented and in scope; the following traits define a method `input`, perhaps you need to implement one of them:
help: candidate #1: `crypto::cryptoutil::FixedBuffer`
help: candidate #2: `crypto::digest::Digest`
help: candidate #3: `crypto::mac::Mac`
help: methods from traits can only be called if the trait is implemented and in scope; the following traits define a method `result`, perhaps you need to implement one of them:
help: candidate #1: `crypto::digest::Digest`
help: candidate #2: `crypto::mac::Mac`
這是爲什麼? Sha256::new()
是否與Sha384::new()
和Sha512::new()
具有相同的類型?
**否。它們是非常明顯不同的類型,如果你看源代碼,不要指望編譯器告訴你它們是不同的類型。** - 它們都**實現Digest和Hmac :: new()'接受的特性'Digest'也是。他們怎麼可能是一個不同的類型? –
https://github.com/DaGenix/rust-crypto/blob/master/src/sha2.rs –
@AlexanderSupertramp:他們實現共同的特質是無關緊要的:他們是不同的類型。所有的內置整數實現'Int',但它們仍然是不同的類型。 「Digest」特徵只是與多種不同類型進行交互的通用界面。 –