Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* sha funzona hmac no */
- #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - c) ++b; a += c;
- #include <math.h>
- #include <string.h>
- class sha1 {
- private :
- unsigned long H[5] = {
- 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
- unsigned long k[4] = {
- 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
- unsigned char buffer[64] = {
- 0 };
- unsigned long words[80] = {
- 0 };
- void input(char *);
- void process();
- void transform();
- void final();
- unsigned long shift(unsigned long, int);
- unsigned long hash[5];
- public :
- sha1(char *);
- unsigned long * get_hash();
- void get_string_hash(unsigned char *);
- };
- class hmacSha1 {
- private :
- unsigned long * hash;
- public :
- hmacSha1(char *, char *);
- unsigned long * get_hash();
- };
- hmacSha1::hmacSha1(char * key, char * message) {
- unsigned char * opad, * ipad, * message_sha;
- opad = (unsigned char *)malloc(20 * sizeof(unsigned char));
- ipad = (unsigned char *)malloc((64+strlen(message)) * sizeof(unsigned char));
- message_sha = (unsigned char *)malloc((strlen(message)) * sizeof(unsigned char));
- char hmac_key[64] = {
- 0 };
- unsigned long * temp_hash;
- char temp_sha[20];
- int i;
- if(strlen(key)>64) {
- sha1 sha_obj(key);
- temp_hash = sha_obj.get_hash();
- memcpy(hmac_key, temp_hash, 20);
- }
- if(strlen(key)<64) {
- for(i=0;i<strlen(key);i++) {
- hmac_key[i] = key[i];
- }
- }
- for(i=0;i<strlen(message);i++) {
- message_sha[i] = message[i];
- }
- for(i=0;i<64;i++) {
- ipad[i] = hmac_key[i] ^ 0x5c;
- opad[i] = hmac_key[i] ^ 0x36;
- }
- memcpy(ipad+64, message_sha, strlen(message));
- sha1 iash((char *)ipad);
- for(i=0;i<64+strlen(message);i++) {
- Serial.print(ipad[i], HEX);
- }
- Serial.println();
- temp_hash = iash.get_hash();
- /*memcpy(opad+64, temp_hash, 20);
- sha1 oash((char *)opad);*/
- hash = iash.get_hash();
- }
- unsigned long * hmacSha1::get_hash() {
- return hash;
- }
- void setup() {
- unsigned long * hash;
- unsigned char string[20];
- int i;
- Serial.begin(9600);
- hmacSha1 test("ciao", "ciao");
- hash = test.get_hash();
- for(i=0;i<5;i++) {
- Serial.print(hash[i], HEX);
- }
- }
- void loop() {
- }
- sha1::sha1(char * message) {
- input(message);
- }
- unsigned long * sha1::get_hash() {
- return H;
- }
- void sha1::get_string_hash(unsigned char * string) {
- memcpy(string, H, 20);
- }
- void sha1::input(char * message) {
- int length = strlen(message);
- int i,j;
- unsigned long size64[2]={
- 0 };
- j=0;
- for(i=0; i<length;i++) {
- buffer[j] = message[i];
- /*Serial.print(j);
- Serial.print(" - ");
- Serial.println((char)buffer[j]);*/
- if (j == 63) {
- process();
- memset(buffer,0,64);
- j=0;
- }
- else {
- j++;
- }
- }
- if(j<56) {
- buffer[j] = 0x80;
- j++;
- while(j<56) {
- buffer[j]=0x00;
- j++;
- }
- }
- else {
- buffer[j] = 0x80;
- j++;
- while(j<64) {
- buffer[j]=0x00;
- j++;
- }
- /*for(i=0; i<64;i++) {
- Serial.println(buffer[i],BIN);
- }*/
- process();
- memset(buffer,0,56);
- }
- DBL_INT_ADD(size64[0],size64[1],8 * length);
- buffer[63] = size64[0];
- buffer[62] = size64[0] >> 8;
- buffer[61] = size64[0] >> 16;
- buffer[60] = size64[0] >> 24;
- buffer[59] = size64[1];
- buffer[58] = size64[1] >> 8;
- buffer[57] = size64[1] >> 16;
- buffer[56] = size64[1] >> 24;
- /*for(i=0;i<64;i++) {
- Serial.println(buffer[i], BIN);
- }*/
- process();
- }
- void sha1::process() {
- int i;
- unsigned long a,b,c,d;
- for(i=0;i<16;i++) {
- a = buffer[i*4];
- b = buffer[i*4+1];
- c = buffer[i*4+2];
- d = buffer[i*4+3];
- words[i] = (a << 24) + (b << 16) + (c << 8) + d;
- }
- /*for(i=0;i<16;i++) {
- Serial.println(words[i],BIN);
- }*/
- for(i=16;i<80;i++) {
- words[i] = shift(words[i-3] ^ words[i-8] ^ words[i-14] ^ words[i-16], 1);
- }
- //Serial.println(words[35], BIN);
- transform();
- }
- void sha1::transform() {
- int i;
- unsigned long a, b, c, d, e, t;
- a = H[0];
- b = H[1];
- c = H[2];
- d = H[3];
- e = H[4];
- for (i=0; i < 20; i++) {
- t = shift(a,5) + ((b & c) ^ (~b & d)) + e + k[0] + words[i];
- e = d;
- d = c;
- c = shift(b,30);
- b = a;
- a = t;
- }
- for ( ; i < 40; i++) {
- t = shift(a,5) + (b ^ c ^ d) + e + k[1] + words[i];
- e = d;
- d = c;
- c = shift(b,30);
- b = a;
- a = t;
- }
- for ( ; i < 60; i++) {
- t = shift(a,5) + ((b & c) ^ (b & d) ^ (c & d)) + e + k[2] + words[i];
- e = d;
- d = c;
- c = shift(b,30);
- b = a;
- a = t;
- }
- for ( ; i < 80; i++) {
- t = shift(a,5) + (b ^ c ^ d) + e + k[3] + words[i];
- e = d;
- d = c;
- c = shift(b,30);
- b = a;
- a = t;
- }
- H[0] += a;
- H[1] += b;
- H[2] += c;
- H[3] += d;
- H[4] += e;
- }
- void sha1::final() {
- /*int i;
- for(i=0;i<5;i++) {
- Serial.println(H[i], HEX);
- }*/
- }
- unsigned long sha1::shift(unsigned long word_input, int bits) {
- return (word_input << bits) | ((word_input) >> (32-bits));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement