Skip to content

add Hash library with support for sha1 #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ void ESP8266WebServer::_handleRequest() {

const char* ESP8266WebServer::_responseCodeToString(int code) {
switch (code) {
case 101: return "Switching Protocols";
case 200: return "OK";
case 403: return "Forbidden";
case 404: return "Not found";
case 500: return "Fail";
default: return "";
Expand Down
32 changes: 32 additions & 0 deletions hardware/esp8266com/esp8266/libraries/Hash/examples/sha1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* simple demo to show sha1 calculation
*/
#include <Arduino.h>
#include <Hash.h>

void setup() {
Serial.begin(921600);
}

void loop() {

// usage as String
// SHA1:a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

Serial.print("SHA1:");
Serial.println(sha1("abc"));

// usage as ptr
// SHA1:a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
uint8_t hash[20];
sha1("abc", &hash[0]);

Serial.print("SHA1:");
for(uint16_t i = 0; i < 20; i++) {
Serial.printf("%02x", hash[i]);
}
Serial.println();

delay(1000);
}

8 changes: 8 additions & 0 deletions hardware/esp8266com/esp8266/libraries/Hash/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name=Hash
version=1.0
author=Markus Sattler
maintainer=Markus Sattler
sentence=Generate Hash from data
paragraph=
url=
architectures=esp8266
117 changes: 117 additions & 0 deletions hardware/esp8266com/esp8266/libraries/Hash/src/Hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* @file Hash.cpp
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the esp8266 core for Arduino environment.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include <Arduino.h>

#include "Hash.h"

extern "C" {
#include "sha1/sha1.h"
}

/**
* create a sha1 hash from data
* @param data uint8_t *
* @param size uint32_t
* @param hash uint8_t[20]
*/
void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {

SHA1_CTX ctx;

#ifdef DEBUG_SHA1
os_printf("DATA:");
for(uint16_t i = 0; i < size; i++) {
os_printf("%02X", data[i]);
}
os_printf("\n");
os_printf("DATA:");
for(uint16_t i = 0; i < size; i++) {
os_printf("%c", data[i]);
}
os_printf("\n");
#endif

SHA1Init(&ctx);
SHA1Update(&ctx, data, size);
SHA1Final(hash, &ctx);

#ifdef DEBUG_SHA1
os_printf("SHA1:");
for(uint16_t i = 0; i < 20; i++) {
os_printf("%02X", hash[i]);
}
os_printf("\n\n");
#endif
}

void sha1(char * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
}

void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
}

void sha1(const char * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
}

void sha1(String data, uint8_t hash[20]) {
sha1(data.c_str(), data.length(), hash);
}

String sha1(uint8_t* data, uint32_t size) {
uint8_t hash[20];
String hashStr = "";

sha1(&data[0], size, &hash[0]);

for(uint16_t i = 0; i < 20; i++) {
String hex = String(hash[i], HEX);
if(hex.length() < 2) {
hex = "0" + hex;
}
hashStr += hex;
}

return hashStr;
}

String sha1(char* data, uint32_t size) {
return sha1((uint8_t*) data, size);
}

String sha1(const uint8_t* data, uint32_t size) {
return sha1((uint8_t*) data, size);
}

String sha1(const char* data, uint32_t size) {
return sha1((uint8_t*) data, size);
}

String sha1(String data) {
return sha1(data.c_str(), data.length());
}

42 changes: 42 additions & 0 deletions hardware/esp8266com/esp8266/libraries/Hash/src/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file Hash.h
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the esp8266 core for Arduino environment.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifndef HASH_H_
#define HASH_H_

//#define DEBUG_SHA1

void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]);
void sha1(char * data, uint32_t size, uint8_t hash[20]);
void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]);
void sha1(const char * data, uint32_t size, uint8_t hash[20]);
void sha1(String data, uint8_t hash[20]);

String sha1(uint8_t* data, uint32_t size);
String sha1(char* data, uint32_t size);
String sha1(const uint8_t* data, uint32_t size);
String sha1(const char* data, uint32_t size);
String sha1(String data);

#endif /* HASH_H_ */
Loading