Skip to content
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

Make sources compatible with esp8266 platform #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions Sha/sha1.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#ifdef ESP8266
#include <pgmspace.h>
#else
#include <avr/io.h>
#include <avr/pgmspace.h>
#endif
#include "sha1.h"

#define SHA1_K0 0x5a827999
Expand Down Expand Up @@ -72,9 +76,12 @@ void Sha1Class::addUncounted(uint8_t data) {
}
}

void Sha1Class::write(uint8_t data) {
WRITE_RET_TYPE Sha1Class::write(uint8_t data) {
++byteCount;
addUncounted(data);
#ifdef ESP8266
return 1;
#endif
}

void Sha1Class::pad() {
Expand All @@ -99,7 +106,7 @@ void Sha1Class::pad() {
uint8_t* Sha1Class::result(void) {
// Pad to complete the last block
pad();

// Swap byte order back
for (int i=0; i<5; i++) {
uint32_t a,b;
Expand All @@ -110,7 +117,7 @@ uint8_t* Sha1Class::result(void) {
b|=a>>24;
state.w[i]=b;
}

// Return pointer to hash (20 characters)
return state.b;
}
Expand Down
10 changes: 8 additions & 2 deletions Sha/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#define HASH_LENGTH 20
#define BLOCK_LENGTH 64

#ifdef ESP8266
#define WRITE_RET_TYPE size_t
#else
#define WRITE_RET_TYPE void
#endif

union _buffer {
uint8_t b[BLOCK_LENGTH];
uint32_t w[BLOCK_LENGTH/4];
Expand All @@ -23,7 +29,7 @@ class Sha1Class : public Print
void initHmac(const uint8_t* secret, int secretLength);
uint8_t* result(void);
uint8_t* resultHmac(void);
virtual void write(uint8_t);
virtual WRITE_RET_TYPE write(uint8_t);
using Print::write;
private:
void pad();
Expand All @@ -36,7 +42,7 @@ class Sha1Class : public Print
uint32_t byteCount;
uint8_t keyBuffer[BLOCK_LENGTH];
uint8_t innerHash[HASH_LENGTH];

};
extern Sha1Class Sha1;

Expand Down
21 changes: 14 additions & 7 deletions Sha/sha256.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#ifdef ESP8266
#include <pgmspace.h>
#else
#include <avr/io.h>
#include <avr/pgmspace.h>
#endif
#include "sha256.h"

uint32_t sha256K[] PROGMEM = {
Expand Down Expand Up @@ -49,7 +53,7 @@ void Sha256Class::hashBlock() {
f=state.w[5];
g=state.w[6];
h=state.w[7];

for (i=0; i<64; i++) {
if (i>=16) {
t1 = buffer.w[i&15] + buffer.w[(i-7)&15];
Expand Down Expand Up @@ -87,9 +91,12 @@ void Sha256Class::addUncounted(uint8_t data) {
}
}

void Sha256Class::write(uint8_t data) {
WRITE_RET_TYPE Sha256Class::write(uint8_t data) {
++byteCount;
addUncounted(data);
#ifdef ESP8266
return 1;
#endif
}

void Sha256Class::pad() {
Expand All @@ -114,7 +121,7 @@ void Sha256Class::pad() {
uint8_t* Sha256Class::result(void) {
// Pad to complete the last block
pad();

// Swap byte order back
for (int i=0; i<8; i++) {
uint32_t a,b;
Expand All @@ -125,7 +132,7 @@ uint8_t* Sha256Class::result(void) {
b|=a>>24;
state.w[i]=b;
}

// Return pointer to hash (20 characters)
return state.b;
}
Expand Down
8 changes: 7 additions & 1 deletion Sha/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#define HASH_LENGTH 32
#define BLOCK_LENGTH 64

#ifdef ESP8266
#define WRITE_RET_TYPE size_t
#else
#define WRITE_RET_TYPE void
#endif

union _buffer {
uint8_t b[BLOCK_LENGTH];
uint32_t w[BLOCK_LENGTH/4];
Expand All @@ -23,7 +29,7 @@ class Sha256Class : public Print
void initHmac(const uint8_t* secret, int secretLength);
uint8_t* result(void);
uint8_t* resultHmac(void);
virtual void write(uint8_t);
virtual WRITE_RET_TYPE write(uint8_t);
using Print::write;
private:
void pad();
Expand Down