rustexamples/dkim_verify.rs -rw-r--r-- 1.9 KiB
1/*
2 * Copyright (c) 2020-2023, Stalwart Labs Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 * option. This file may not be copied, modified, or distributed
8 * except according to those terms.
9 */
10
11use mail_auth::{AuthenticatedMessage, DkimResult, Resolver};
12
13const TEST_MESSAGE: &str = r#"DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed;
14d=football.example.com; i=@football.example.com;
15q=dns/txt; s=brisbane; t=1528637909; h=from : to :
16subject : date : message-id : from : subject : date;
17bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
18b=/gCrinpcQOoIfuHNQIbq4pgh9kyIK3AQUdt9OdqQehSwhEIug4D11Bus
19Fa3bT3FY5OsU7ZbnKELq+eXdp1Q1Dw==
20DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
21d=football.example.com; i=@football.example.com;
22q=dns/txt; s=test; t=1528637909; h=from : to : subject :
23date : message-id : from : subject : date;
24bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
25b=F45dVWDfMbQDGHJFlXUNB2HKfbCeLRyhDXgFpEL8GwpsRe0IeIixNTe3
26DhCVlUrSjV4BwcVcOF6+FF3Zo9Rpo1tFOeS9mPYQTnGdaSGsgeefOsk2Jz
27dA+L10TeYt9BgDfQNZtKdN1WO//KgIqXP7OdEFE4LjFYNcUxZQ4FADY+8=
28From: Joe SixPack <joe@football.example.com>
29To: Suzie Q <suzie@shopping.example.net>
30Subject: Is dinner ready?
31Date: Fri, 11 Jul 2003 21:00:37 -0700 (PDT)
32Message-ID: <20030712040037.46341.5F8J@football.example.com>
33
34Hi.
35
36We lost the game. Are you hungry yet?
37
38Joe."#;
39
40#[tokio::main]
41async fn main() {
42 // Create a resolver using Cloudflare DNS
43 let resolver = Resolver::new_cloudflare_tls().unwrap();
44
45 // Parse message
46 let authenticated_message = AuthenticatedMessage::parse(TEST_MESSAGE.as_bytes()).unwrap();
47
48 // Validate signature
49 let result = resolver.verify_dkim(&authenticated_message).await;
50
51 // Make sure all signatures passed verification
52 assert!(result.iter().all(|s| s.result() == &DkimResult::Pass));
53}