Skip to content

Commit 525c3b6

Browse files
Ryanbudziq
Ryan
authored andcommitted
add "Sign and verify a message with HMAC digest" example
1 parent c591cfd commit 525c3b6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/basics.md

+41
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [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] |
2020
| [Extract phone numbers from text][ex-phone] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing] |
2121
| [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] |
2223
| [Define and operate on a type represented as a bitfield][ex-bitflags] | [![bitflags-badge]][bitflags] | [![cat-no-std-badge]][cat-no-std] |
2324
| [Access a file randomly using a memory map][ex-random-file-access] | [![memmap-badge]][memmap] | [![cat-filesystem-badge]][cat-filesystem] |
2425
| [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<()> {
861862
# quick_main!(run);
862863
```
863864

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+
864903
[ex-bitflags]: #ex-bitflags
865904
<a name="ex-bitflags"></a>
866905
## Define and operate on a type represented as a bitfield
@@ -996,6 +1035,8 @@ fn main() {
9961035
[`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
9971036
[`digest::Context`]: https://docs.rs/ring/*/ring/digest/struct.Context.html
9981037
[`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
9991040
[`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
10001041
[`File::create`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.create
10011042
[`File::open`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.open

src/intro.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ community. It needs and welcomes help. For details see
3737
| [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] |
3838
| [Extract phone numbers from text][ex-phone] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing] |
3939
| [Calculate the SHA-256 digest of a file][ex-sha-digest] | [![ring-badge]][ring] [![data-encoding-badge]][data-encoding] | [![cat-cryptography-badge]][cat-cryptography] |
40+
| [Sign and verify a message with an HMAC digest][ex-hmac] | [![ring-badge]][ring] | [![cat-cryptography-badge]][cat-cryptography] |
4041
| [Define and operate on a type represented as a bitfield][ex-bitflags] | [![bitflags-badge]][bitflags] | [![cat-no-std-badge]][cat-no-std] |
4142
| [Access a file randomly using a memory map][ex-random-file-access] | [![memmap-badge]][memmap] | [![cat-filesystem-badge]][cat-filesystem] |
4243
| [Check number of logical cpu cores][ex-check-cpu-cores] | [![num_cpus-badge]][num_cpus] | [![cat-hardware-support-badge]][cat-hardware-support] |
@@ -199,6 +200,7 @@ community. It needs and welcomes help. For details see
199200
[ex-semver-prerelease]: app.html#ex-semver-prerelease
200201
[ex-serialize-csv]: encoding.html#ex-serialize-csv
201202
[ex-sha-digest]: basics.html#ex-sha-digest
203+
[ex-hmac]: basics.html#ex-hmac
202204
[ex-std-read-lines]: basics.html#ex-std-read-lines
203205
[ex-tar-compress]: app.html#ex-tar-compress
204206
[ex-tar-decompress]: app.html#ex-tar-decompress

0 commit comments

Comments
 (0)