Skip to content
Snippets Groups Projects
Commit 41a25b6e authored by Dorababu A's avatar Dorababu A
Browse files

Compare sha256 hash with Crypto lib

parent 32493833
No related branches found
No related tags found
No related merge requests found
#include <Arduino.h>
#include <sha256.h>
#include <DFRobot_ID809.h>
// use serial3 as interface with fingerprint
// sensor .
#define FPSerial Serial3
// define buillt_in led
#define BUILTIN_LED 13
// Collect the fingerprint 3 times
#define COLLECT_NUMBER 3
#define SHA256_SIZE 32
/*------Funciotion dclarations----------*/
// function to blink blitin_led
// to make sure that code is working
void blink_led();
/*------Global variables--------------*/
DFRobot_ID809 fingerprint;
byte hash[SHA256_SIZE];
uint8_t ID, i, ret;
uint8_t temp[1008];
void compute_hash() {
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, temp, sizeof(temp));
sha256_final(&ctx, hash);
// SHA256 hasher;
// hasher.doUpdate(temp, sizeof(temp));
// hasher.doFinal(hash);
}
void setup() {
// config led_pin as output
pinMode(BUILTIN_LED, OUTPUT);
// use serial port to print the baud rate
Serial.begin(9600);
FPSerial.begin(115200);
fingerprint.begin(FPSerial);
while (fingerprint.isConnected() == false) {
Serial.println("Communication with the device failed");
delay(1000);
}
// fingerprint.getTemplate(1, temp);
// for(uint8_t i=0; i<1008; i++) {
// Serial.print(i);
// Serial.print(": ");
// Serial.println(temp[i]);
// delay(100);
// }
// compute_hash();
// Serial.println("------------------------");
// for (uint8_t i = 0; i < SHA256_SIZE; i++) {
//
// if (hash[i] < 0x10) {
// Serial.print('0');
// }
// Serial.print(hash[i], HEX);
// Serial.print(" ");
// }
// Serial.println("------------------------");
}
void loop() {
uint8_t ret = 0;
/*Set fingerprint LED ring mode, color, and number of blinks
Can be set as follows:
Parameter 1:<LEDMode>
eBreathing eFastBlink eKeepsOn eNormalClose
eFadeIn eFadeOut eSlowBlink
Parameter 2:<LEDColor>
eLEDGreen eLEDRed eLEDYellow eLEDBlue
eLEDCyan eLEDMagenta eLEDWhite
Parameter 3:<number of blinks> 0 represents blinking all the time
This parameter will only be valid in mode eBreathing, eFastBlink, eSlowBlink
*/
fingerprint.ctrlLED(/*LEDMode = */ fingerprint.eBreathing,
/*LEDColor = */ fingerprint.eLEDBlue,
/*blinkCount = */ 0);
Serial.println("Please press down your finger");
/*Capture fingerprint image, Disable the collection timeout function
If succeed return 0, otherwise return ERR_ID809
*/
if ((fingerprint.collectionFingerprint(/*timeout=*/0)) != ERR_ID809) {
/*Set fingerprint LED ring to quick blink in yellow 3 times*/
fingerprint.ctrlLED(/*LEDMode = */ fingerprint.eFastBlink,
/*LEDColor = */ fingerprint.eLEDYellow,
/*blinkCount = */ 3);
Serial.println("Capturing succeeds");
Serial.println("Please release your finger");
/*Wait for finger to release
Return 1 when finger is detected, otherwise return 0
*/
while (fingerprint.detectFinger())
;
/*Compare the captured fingerprint with all the fingerprints in the
fingerprint library Return fingerprint ID(1-80) if succeed, return 0 when
failed
*/
ret = fingerprint.search();
/*Compare the captured fingerprint with a fingerprint of specific ID
Return fingerprint ID(1-80) if succeed, return 0 when failed
*/
// ret = fingerprint.verify(/*Fingerprint ID = */1);
if (ret != 0) {
/*Set fingerprint LED ring to always ON in green */
fingerprint.ctrlLED(/*LEDMode = */ fingerprint.eKeepsOn,
/*LEDColor = */ fingerprint.eLEDGreen,
/*blinkCount = */ 0);
Serial.print("Matching succeeds,ID=");
Serial.println(ret);
fingerprint.getTemplate(ret, temp);
// for(uint8_t i=0; i<1008; i++) {
// Serial.print(i);
// Serial.print(": ");
// Serial.println(temp[i]);
// delay(100);
// }
compute_hash();
Serial.println("--------------------");
for (uint8_t i = 0; i < SHA256_SIZE; i++) {
if (hash[i] < 0x10) {
Serial.print('0');
}
Serial.print(hash[i], HEX);
Serial.print(" ");
}
Serial.println("---------------------");
for (uint16_t i = 0; i < 1008; i++) {
Serial.print(temp[i], HEX);
Serial.print(" ");
}
} else {
/*Set fingerprint LED ring to always ON in red*/
fingerprint.ctrlLED(/*LEDMode = */ fingerprint.eKeepsOn,
/*LEDColor = */ fingerprint.eLEDRed,
/*blinkCount = */ 0);
Serial.println("Matching fails");
}
} else {
Serial.println("Capturing fails");
/*Get error code information*/
// desc = fingerprint.getErrorDescription();
// Serial.println(desc);
}
Serial.println("-----------------------------");
delay(1000);
/*Set the fingerprint LED ring as blue breathing light*/
fingerprint.ctrlLED(/*LEDMode = */ fingerprint.eBreathing,
/*LEDColor = */ fingerprint.eLEDBlue,
/*blinkCount = */ 0);
blink_led();
}
void blink_led() {
digitalWrite(BUILTIN_LED, HIGH);
delay(500);
digitalWrite(BUILTIN_LED, LOW);
delay(500);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment