3 This is a minimal, lightweight crypto library for the ESP8266 IOT device. It
4 provides the following functions:
11 The SHA256 and AES implementations are based upon the implementations in axTLS
12 except ported to the ESP8266 Arduino platform, credit to Cameron Rich for the
19 The following snippet demonstrates how to compute the SHA256 HMAC authentication
22 /* Include the crypto library into your project */
25 /* The length of the key we will use for this HMAC */
26 /* The key can be of any length, 16 and 32 are common */
30 byte key[KEY_LENGTH] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
32 /* Create the HMAC instance with our key */
33 SHA256HMAC hmac(key, KEY_LENGTH);
35 /* Update the HMAC with just a plain string (null terminated) */
36 hmac.doUpdate("Hello World");
38 /* And or with a string and length */
39 const char *goodbye = "GoodBye World";
40 hmac.doUpdate(goodbye, strlen(goodbye));
42 /* And or with a binary message */
43 byte message[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
44 hmac.doUpdate(message, sizeof(message));
46 /* Finish the HMAC calculation and return the authentication code */
47 byte authCode[SHA256HMAC_SIZE];
48 hmac.doFinal(authCode);
50 /* authCode now contains our 32 byte authentication code */
51 for (byte i; i < SHA256HMAC_SIZE; i++)
53 Serial.print(authCode[i], HEX);
58 The following snippet demonstrates how to compute the SHA256 hash of a message.
60 /* Create a SHA256 hash */
63 /* Update the hash with your message, as many times as you like */
64 const char *hello = "Hello World";
65 hasher.doUpdate(hello, strlen(hello));
67 /* Update the hash with just a plain string*/
68 hasher.doUpdate("Goodbye World");
70 /* Update the hash with a binary message */
71 byte message[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
72 hasher.doUpdate(message, sizeof(message));
74 /* Compute the final hash */
75 byte hash[SHA256_SIZE];
78 /* hash now contains our 32 byte hash */
79 for (byte i; i < SHA256_SIZE; i++)
81 Serial.print(hash[i], HEX);
87 Copyright (c) 2016, Chris Ellis, with portions derived from axTLS
90 Redistribution and use in source and binary forms, with or without
91 modification, are permitted provided that the following conditions are met:
93 1. Redistributions of source code must retain the above copyright notice, this
94 list of conditions and the following disclaimer.
95 2. Redistributions in binary form must reproduce the above copyright notice,
96 this list of conditions and the following disclaimer in the documentation
97 and/or other materials provided with the distribution.
99 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
100 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
101 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
102 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
103 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
104 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
105 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
106 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
108 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116 Copyright (c) Chris Ellis 2016