|
| 1 | +/* |
| 2 | + Usage example for the PBKDF2_HMACBuilder class. |
| 3 | +
|
| 4 | + This example shows how to use the Hash library to hash data using the PBKDF2_HMACBuilder class. |
| 5 | + PBKDF2_HMAC (Password-Based Key Derivation Function 2) is a key derivation function that uses a password and a salt to derive a key. |
| 6 | +
|
| 7 | + The PBKDF2_HMACBuilder class takes for arguments: |
| 8 | + - A HashBuilder object to use for the HMAC (SHA1Builder, SHA2Builder, SHA3Builder, etc.) |
| 9 | + - A password string (default: empty) |
| 10 | + - A salt string (default: empty) |
| 11 | + - The number of iterations (default: 1000) |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <SHA1Builder.h> |
| 15 | +#include <SHA2Builder.h> |
| 16 | +#include <PBKDF2_HMACBuilder.h> |
| 17 | + |
| 18 | +void setup() { |
| 19 | + Serial.begin(115200); |
| 20 | + Serial.println("\n\nPBKDF2-HMAC Example"); |
| 21 | + Serial.println("==================="); |
| 22 | + |
| 23 | + // Test 1: Basic PBKDF2-HMAC-SHA1 |
| 24 | + Serial.println("\n1. PBKDF2-HMAC-SHA1 Test (1 iteration)"); |
| 25 | + { |
| 26 | + SHA1Builder sha1; |
| 27 | + PBKDF2_HMACBuilder pbkdf2(&sha1, "password", "salt", 1); |
| 28 | + |
| 29 | + pbkdf2.begin(); |
| 30 | + pbkdf2.calculate(); |
| 31 | + |
| 32 | + Serial.print("Password: "); |
| 33 | + Serial.println("password"); |
| 34 | + Serial.print("Salt: "); |
| 35 | + Serial.println("salt"); |
| 36 | + Serial.print("Iterations: "); |
| 37 | + Serial.println(1); |
| 38 | + Serial.print("Output (hex): "); |
| 39 | + Serial.println(pbkdf2.toString()); |
| 40 | + |
| 41 | + // Expected: 0c60c80f961f0e71f3a9b524af6012062fe037a6 |
| 42 | + String expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; |
| 43 | + String result = pbkdf2.toString(); |
| 44 | + |
| 45 | + if (result.equalsIgnoreCase(expected)) { |
| 46 | + Serial.println("✓ PASS: Output matches expected value"); |
| 47 | + } else { |
| 48 | + Serial.println("✗ FAIL: Output does not match expected value"); |
| 49 | + Serial.print("Expected: "); |
| 50 | + Serial.println(expected); |
| 51 | + Serial.print("Got: "); |
| 52 | + Serial.println(result); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Test 2: PBKDF2-HMAC-SHA1 with more iterations |
| 57 | + Serial.println("\n2. PBKDF2-HMAC-SHA1 Test (1000 iterations)"); |
| 58 | + { |
| 59 | + SHA1Builder sha1; |
| 60 | + PBKDF2_HMACBuilder pbkdf2(&sha1); |
| 61 | + |
| 62 | + const char *password = "password"; |
| 63 | + const char *salt = "salt"; |
| 64 | + |
| 65 | + pbkdf2.begin(); |
| 66 | + pbkdf2.setPassword(password); |
| 67 | + pbkdf2.setSalt(salt); |
| 68 | + pbkdf2.setIterations(1000); |
| 69 | + pbkdf2.calculate(); |
| 70 | + |
| 71 | + Serial.print("Password: "); |
| 72 | + Serial.println(password); |
| 73 | + Serial.print("Salt: "); |
| 74 | + Serial.println(salt); |
| 75 | + Serial.print("Iterations: "); |
| 76 | + Serial.println(1000); |
| 77 | + Serial.print("Output (hex): "); |
| 78 | + Serial.println(pbkdf2.toString()); |
| 79 | + |
| 80 | + // Expected: 6e88be8bad7eae9d9e10aa061224034fed48d03f |
| 81 | + String expected = "6e88be8bad7eae9d9e10aa061224034fed48d03f"; |
| 82 | + String result = pbkdf2.toString(); |
| 83 | + |
| 84 | + if (result.equalsIgnoreCase(expected)) { |
| 85 | + Serial.println("✓ PASS: Output matches expected value"); |
| 86 | + } else { |
| 87 | + Serial.println("✗ FAIL: Output does not match expected value"); |
| 88 | + Serial.print("Expected: "); |
| 89 | + Serial.println(expected); |
| 90 | + Serial.print("Got: "); |
| 91 | + Serial.println(result); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Test 3: PBKDF2-HMAC-SHA256 with different password and salt |
| 96 | + Serial.println("\n3. PBKDF2-HMAC-SHA256 Test"); |
| 97 | + { |
| 98 | + SHA256Builder sha256; |
| 99 | + PBKDF2_HMACBuilder pbkdf2(&sha256, "mySecretPassword", "randomSalt123", 100); |
| 100 | + |
| 101 | + pbkdf2.begin(); |
| 102 | + pbkdf2.calculate(); |
| 103 | + |
| 104 | + Serial.print("Password: "); |
| 105 | + Serial.println("mySecretPassword"); |
| 106 | + Serial.print("Salt: "); |
| 107 | + Serial.println("randomSalt123"); |
| 108 | + Serial.print("Iterations: "); |
| 109 | + Serial.println(100); |
| 110 | + Serial.print("Output (hex): "); |
| 111 | + Serial.println(pbkdf2.toString()); |
| 112 | + |
| 113 | + // Expected: 4ce309e56a37e0a4b9b84b98ed4a94e6c5cd5926cfd3baca3a6dea8c5d7903e8 |
| 114 | + String expected = "4ce309e56a37e0a4b9b84b98ed4a94e6c5cd5926cfd3baca3a6dea8c5d7903e8"; |
| 115 | + String result = pbkdf2.toString(); |
| 116 | + |
| 117 | + if (result.equalsIgnoreCase(expected)) { |
| 118 | + Serial.println("✓ PASS: Output matches expected value"); |
| 119 | + } else { |
| 120 | + Serial.println("✗ FAIL: Output does not match expected value"); |
| 121 | + Serial.print("Expected: "); |
| 122 | + Serial.println(expected); |
| 123 | + Serial.print("Got: "); |
| 124 | + Serial.println(result); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Test 4: PBKDF2-HMAC-SHA1 with byte arrays |
| 129 | + Serial.println("\n4. PBKDF2-HMAC-SHA1 Test (byte arrays)"); |
| 130 | + { |
| 131 | + SHA1Builder sha1; // or any other hash algorithm based on HashBuilder |
| 132 | + PBKDF2_HMACBuilder pbkdf2(&sha1); |
| 133 | + |
| 134 | + uint8_t password[] = {0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64}; // "password" in bytes |
| 135 | + uint8_t salt[] = {0x73, 0x61, 0x6c, 0x74}; // "salt" in bytes |
| 136 | + |
| 137 | + pbkdf2.begin(); |
| 138 | + pbkdf2.setPassword(password, sizeof(password)); |
| 139 | + pbkdf2.setSalt(salt, sizeof(salt)); |
| 140 | + pbkdf2.setIterations(1); |
| 141 | + pbkdf2.calculate(); |
| 142 | + |
| 143 | + Serial.print("Password (bytes): "); |
| 144 | + for (int i = 0; i < sizeof(password); i++) { |
| 145 | + Serial.print((char)password[i]); |
| 146 | + } |
| 147 | + Serial.println(); |
| 148 | + Serial.print("Salt (bytes): "); |
| 149 | + for (int i = 0; i < sizeof(salt); i++) { |
| 150 | + Serial.print((char)salt[i]); |
| 151 | + } |
| 152 | + Serial.println(); |
| 153 | + Serial.print("Iterations: "); |
| 154 | + Serial.println(1); |
| 155 | + Serial.print("Output (hex): "); |
| 156 | + Serial.println(pbkdf2.toString()); |
| 157 | + |
| 158 | + // Expected: 0c60c80f961f0e71f3a9b524af6012062fe037a6 (same as test 1) |
| 159 | + String expected = "0c60c80f961f0e71f3a9b524af6012062fe037a6"; |
| 160 | + String result = pbkdf2.toString(); |
| 161 | + |
| 162 | + if (result.equalsIgnoreCase(expected)) { |
| 163 | + Serial.println("✓ PASS: Output matches expected value"); |
| 164 | + } else { |
| 165 | + Serial.println("✗ FAIL: Output does not match expected value"); |
| 166 | + Serial.print("Expected: "); |
| 167 | + Serial.println(expected); |
| 168 | + Serial.print("Got: "); |
| 169 | + Serial.println(result); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + Serial.println("\nPBKDF2-HMAC tests completed!"); |
| 174 | +} |
| 175 | + |
| 176 | +void loop() { |
| 177 | + // Nothing to do in loop |
| 178 | +} |
0 commit comments