|
| 1 | +/** |
| 2 | + * @file Hash.cpp |
| 3 | + * @date 20.05.2015 |
| 4 | + * @author Markus Sattler |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Markus Sattler. All rights reserved. |
| 7 | + * This file is part of the esp8266 core for Arduino environment. |
| 8 | + * |
| 9 | + * This library is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 2.1 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This library is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with this library; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include <Arduino.h> |
| 26 | + |
| 27 | +#include "Hash.h" |
| 28 | + |
| 29 | +extern "C" { |
| 30 | +#include "sha1/sha1.h" |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * create a sha1 hash from data |
| 35 | + * @param data uint8_t * |
| 36 | + * @param size uint32_t |
| 37 | + * @param hash uint8_t[20] |
| 38 | + */ |
| 39 | +void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) { |
| 40 | + |
| 41 | + SHA1_CTX ctx; |
| 42 | + |
| 43 | +#ifdef DEBUG_SHA1 |
| 44 | + os_printf("DATA:"); |
| 45 | + for(uint16_t i = 0; i < size; i++) { |
| 46 | + os_printf("%02X", data[i]); |
| 47 | + } |
| 48 | + os_printf("\n"); |
| 49 | + os_printf("DATA:"); |
| 50 | + for(uint16_t i = 0; i < size; i++) { |
| 51 | + os_printf("%c", data[i]); |
| 52 | + } |
| 53 | + os_printf("\n"); |
| 54 | +#endif |
| 55 | + |
| 56 | + SHA1Init(&ctx); |
| 57 | + SHA1Update(&ctx, data, size); |
| 58 | + SHA1Final(hash, &ctx); |
| 59 | + |
| 60 | +#ifdef DEBUG_SHA1 |
| 61 | + os_printf("SHA1:"); |
| 62 | + for(uint16_t i = 0; i < 20; i++) { |
| 63 | + os_printf("%02X", hash[i]); |
| 64 | + } |
| 65 | + os_printf("\n\n"); |
| 66 | +#endif |
| 67 | +} |
| 68 | + |
| 69 | +void sha1(char * data, uint32_t size, uint8_t hash[20]) { |
| 70 | + sha1((uint8_t *) data, size, hash); |
| 71 | +} |
| 72 | + |
| 73 | +void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]) { |
| 74 | + sha1((uint8_t *) data, size, hash); |
| 75 | +} |
| 76 | + |
| 77 | +void sha1(const char * data, uint32_t size, uint8_t hash[20]) { |
| 78 | + sha1((uint8_t *) data, size, hash); |
| 79 | +} |
| 80 | + |
| 81 | +void sha1(String data, uint8_t hash[20]) { |
| 82 | + sha1(data.c_str(), data.length(), hash); |
| 83 | +} |
| 84 | + |
| 85 | +String sha1(uint8_t* data, uint32_t size) { |
| 86 | + uint8_t hash[20]; |
| 87 | + String hashStr = ""; |
| 88 | + |
| 89 | + sha1(&data[0], size, &hash[0]); |
| 90 | + |
| 91 | + for(uint16_t i = 0; i < 20; i++) { |
| 92 | + String hex = String(hash[i], HEX); |
| 93 | + if(hex.length() < 2) { |
| 94 | + hex = "0" + hex; |
| 95 | + } |
| 96 | + hashStr += hex; |
| 97 | + } |
| 98 | + |
| 99 | + return hashStr; |
| 100 | +} |
| 101 | + |
| 102 | +String sha1(char* data, uint32_t size) { |
| 103 | + return sha1((uint8_t*) data, size); |
| 104 | +} |
| 105 | + |
| 106 | +String sha1(const uint8_t* data, uint32_t size) { |
| 107 | + return sha1((uint8_t*) data, size); |
| 108 | +} |
| 109 | + |
| 110 | +String sha1(const char* data, uint32_t size) { |
| 111 | + return sha1((uint8_t*) data, size); |
| 112 | +} |
| 113 | + |
| 114 | +String sha1(String data) { |
| 115 | + return sha1(data.c_str(), data.length()); |
| 116 | +} |
| 117 | + |
0 commit comments