HASH 04: SHA-256
This example shows how to calculate SHA-256 message digest to provide INTEGRITY to the sending of information. Both data must be sent over the network: MESSAGE + HASH_MESSAGE

Required Materials

1 x Waspmote 1 x Battery

Notes

- The battery has to be connected. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------Waspmote HASH_04 SHA-256 Message Digest--------
3
*
4
* This example shows how to calculate SHA-256 message digest to
5
* provide INTEGRITY to the sending of information. Both data
6
* must be sent over the network: MESSAGE + HASH_MESSAGE
7
*
8
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
9
* http://www.libelium.com
10
*
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation, either version 2 of the License, or
14
* (at your option) any later version.
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
20
*
21
* You should have received a copy of the GNU General Public License
22
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
*
24
* Version: 3.0
25
* Design: David Gascón
26
* Implementation: Alvaro Gonzalez, Yuri Carmona
27
*/
28
29
#include "WaspHash.h"
30
31
32
// Original message
33
char message[] = "Libelium";
34
35
// Variable to store the hash message
36
uint8_t hash_message_sha256[32];
37
38
39
void setup()
40
{
41
USB.ON();
42
USB.println(F("HASH_04 example"));
43
USB.println(F("- SHA-256 algorithm"));
44
USB.println(F("- Message Digest length of 256 bits (32 Bytes)"));
45
USB.println();
46
47
USB.print(F("Original message: "));
48
USB.println(message);
49
}
50
51
void loop()
52
{
53
54
// Calculate hash of original message with SHA-256 Algorithm
55
HASH.sha( SHA256, hash_message_sha256, (uint8_t*)message, strlen(message)*8);
56
57
// Printing message digest SHA-256
58
HASH.printMessageDigest("SHA-256:", hash_message_sha256, 32);
59
60
delay(5000);
61
}
Copied!

Output

1
H#
2
HASH_04 example
3
- SHA-256 algorithm
4
- Message Digest length of 256 bits (32 Bytes)
5
6
Original message: Libelium
7
SHA-256:"D22B6FDCC90777469FF9815F7C0E0026DD857B32466CF8B4B6B23308CB6533EE"
Copied!
Last modified 1yr ago