|
19 | 19 | | [Replace all occurrences of one text pattern with another pattern.][ex-regex-replace-named] | [![regex-badge]][regex] [![lazy_static-badge]][lazy_static] | [![cat-text-processing-badge]][cat-text-processing] |
|
20 | 20 | | [Extract phone numbers from text][ex-phone] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing] |
|
21 | 21 | | [Calculate the SHA-256 digest of a file][ex-sha-digest] | [![ring-badge]][ring] [![data-encoding-badge]][data-encoding] | [![cat-cryptography-badge]][cat-cryptography] |
|
| 22 | +| [Sign and verify a message with an HMAC digest][ex-hmac] | [![ring-badge]][ring] | [![cat-cryptography-badge]][cat-cryptography] | |
22 | 23 | | [Define and operate on a type represented as a bitfield][ex-bitflags] | [![bitflags-badge]][bitflags] | [![cat-no-std-badge]][cat-no-std] |
|
23 | 24 | | [Access a file randomly using a memory map][ex-random-file-access] | [![memmap-badge]][memmap] | [![cat-filesystem-badge]][cat-filesystem] |
|
24 | 25 | | [Check number of logical cpu cores][ex-check-cpu-cores] | [![num_cpus-badge]][num_cpus] | [![cat-hardware-support-badge]][cat-hardware-support] |
|
@@ -861,6 +862,44 @@ fn run() -> Result<()> {
|
861 | 862 | # quick_main!(run);
|
862 | 863 | ```
|
863 | 864 |
|
| 865 | +[ex-hmac]: #ex-hmac |
| 866 | +<a name="ex-hmac"></a> |
| 867 | +## Sign and verify a message with HMAC digest |
| 868 | + |
| 869 | +[![ring-badge]][ring] [![cat-cryptography-badge]][cat-cryptography] |
| 870 | + |
| 871 | +Uses [`ring::hmac`] to creates a [`hmac::Signature`] of a string then verifies the signiture is correct. |
| 872 | + |
| 873 | +```rust |
| 874 | +# #[macro_use] |
| 875 | +# extern crate error_chain; |
| 876 | +extern crate ring; |
| 877 | +# |
| 878 | +# error_chain! { |
| 879 | +# foreign_links { |
| 880 | +# Ring(ring::error::Unspecified); |
| 881 | +# } |
| 882 | +# } |
| 883 | + |
| 884 | +use ring::{digest, hmac, rand}; |
| 885 | +use ring::rand::SecureRandom; |
| 886 | + |
| 887 | +fn run() -> Result<()> { |
| 888 | + let mut key_value = [0u8; 48]; |
| 889 | + let rng = rand::SystemRandom::new(); |
| 890 | + rng.fill(&mut key_value)?; |
| 891 | + let key = hmac::SigningKey::new(&digest::SHA256, &key_value); |
| 892 | + |
| 893 | + let message = "Legitimate and important message."; |
| 894 | + let signature = hmac::sign(&key, message.as_bytes()); |
| 895 | + hmac::verify_with_own_key(&key, message.as_bytes(), signature.as_ref())?; |
| 896 | + |
| 897 | + Ok(()) |
| 898 | +} |
| 899 | +# |
| 900 | +# quick_main!(run); |
| 901 | +``` |
| 902 | + |
864 | 903 | [ex-bitflags]: #ex-bitflags
|
865 | 904 | <a name="ex-bitflags"></a>
|
866 | 905 | ## Define and operate on a type represented as a bitfield
|
@@ -996,6 +1035,8 @@ fn main() {
|
996 | 1035 | [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
|
997 | 1036 | [`digest::Context`]: https://docs.rs/ring/*/ring/digest/struct.Context.html
|
998 | 1037 | [`digest::Digest`]: https://docs.rs/ring/*/ring/digest/struct.Digest.html
|
| 1038 | +[`ring::hmac`]: https://docs.rs/ring/*/ring/hmac/ |
| 1039 | +[`hmac::Signature`]: https://docs.rs/ring/*/ring/hmac/struct.Signature.html |
999 | 1040 | [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
|
1000 | 1041 | [`File::create`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.create
|
1001 | 1042 | [`File::open`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.open
|
|
0 commit comments