Created
February 28, 2019 19:12
-
-
Save scottchiefbaker/641f69eb64bbda2809c03300c58c3266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/rweather/arduinolibs/tree/master/libraries/Crypto | |
#include <SHA256.h> | |
#define HASH_SIZE 32 // Bytes | |
#define LED_PIN 2 // LED pin to blink while we crunch SHA256 | |
#define debug 0 | |
// SHA256 via C++ char strings (much faster than Arduino Strings) | |
char *sha256hex(const char *input, char *outhex) { | |
SHA256 hash; | |
uint8_t output[HASH_SIZE]; // Raw byte array of the sha256 output | |
strcpy(outhex,""); | |
hash.reset(); | |
hash.update(input, strlen(input)); | |
hash.finalize(output, HASH_SIZE); | |
for (int i = 0; i < HASH_SIZE; i++) { | |
uint8_t c = output[i]; | |
sprintf(eos(outhex), "%02x", c); | |
} | |
return outhex; | |
} | |
// Return a string (*much* slower than C strings) | |
String sha256hex(const String input) { | |
char tmp[65]; | |
sha256hex(input.c_str(),tmp); | |
String output_hex = tmp; | |
return output_hex; | |
} | |
char *random_string(char *ret) { | |
char words[][6] = { | |
"apple", "world", "smart", "phone", "toons", "yearn", "write", "vixen", "unite", "tutor", "swing", "roman", | |
"rosey", "quote", "psych", "overy", "novel", "muggy", "lumpy", "knots", "joker", "ingot", "hunch", "guide", | |
"furry", "elves", "dizzy", "cubed", "bumby", "atone", "magic", "smell", "speak", "glass", "board", "elbow", | |
}; | |
int total = sizeof(words) / sizeof(words[0]); | |
strcpy(ret,""); | |
// Word #1 | |
strcat(ret,words[random(0,total)]); | |
// Random numbers in the middle | |
char num[3]; | |
itoa(random(0,99),num,10); | |
strcat(ret,num); | |
// Word #2 | |
strcat(ret,words[random(0,total)]); | |
return ret; | |
} | |
char *eos(char str[]) { | |
return (str) + strlen(str); | |
} | |
/////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////// | |
long int start = 0; | |
int count = 0; | |
char buf[100]; | |
void setup() { | |
Serial.begin(115200); | |
delay(500); | |
Serial.print("Starting hash tests\r\n"); | |
start = millis(); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LOW); | |
} | |
void loop() { | |
char input[15] = ""; | |
random_string(input); | |
digitalWrite(LED_PIN, HIGH); | |
char output_hex[65] = ""; | |
sha256hex(input,output_hex); | |
digitalWrite(LED_PIN, LOW); | |
if (debug) { | |
sprintf(buf, "SHA256: %-12s = %s\r\n", input, output_hex); | |
Serial.print(buf); | |
} | |
count++; | |
// Output some stats every 10 seconds | |
if ((millis() - start) > 10000) { | |
sprintf(buf, "Crunched %d SHA256 sums in 10 seconds\r\n",count); | |
Serial.print(buf); | |
// Reset the variables for a new loop | |
count = 0; | |
start = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment