Varun Bahl / Mbed 2 deprecated Decryptulator

Dependencies:   BahlDecrypModified CyaSSL mbed nRF51822

Fork of Decryptulator by Mobius IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sfh_mbed.cpp Source File

sfh_mbed.cpp

00001 #include "sfh_mbed.h"
00002 #include "SlidingWindow.h"
00003 #include "LookupTable.h"
00004 
00005 #define LOOKUP_TABLE_SIZE 3000
00006 #define SLIDING_WINDOW_SIZE 300
00007 
00008 //static uint32_t sliding_window_index = 0; 
00009 
00010 
00011 uint32_t hash (const char *data, uint16_t len) {
00012     // This is mostly Paul Hsieh's original code
00013     uint32_t hash, tmp;
00014     int rem;
00015 
00016     if (len <= 0 || data == 0) {
00017         return 0;
00018     }
00019 
00020     hash = len;
00021     rem = len & 3;
00022     len >>= 2;
00023 
00024     /* Main loop */
00025     while (len > 0) {
00026         hash += *((uint16_t *) data);
00027 
00028         /* To make a long story short, the C standard states that the
00029          * shift operator's operands must be promoted to (unsigned) int,
00030          * which is (usually) 32 bits wide on PC and 16 on Arduino. This
00031          * results in different behaviour, since part of the result gets
00032          * truncated on Arduino, so we cast the result to make sure all
00033          * bits are kept.
00034          */
00035         tmp = ((uint32_t) (*((uint16_t *) (data + 2))) << 11) ^ hash;
00036 
00037         hash = (hash << 16) ^ tmp;
00038         data += 2 * sizeof (uint16_t);
00039         hash += hash >> 11;
00040         len--;
00041     }
00042 
00043     /* Handle end cases */
00044     switch (rem) {
00045         case 3:
00046             hash += * ((uint16_t *) data);
00047             hash ^= hash << 16;
00048             hash ^= ((signed char) data[2]) << 18;
00049             hash += hash >> 11;
00050             break;
00051 
00052         case 2:
00053             hash += * ((uint16_t *) data);
00054             hash ^= hash << 11;
00055             hash += hash >> 17;
00056             break;
00057 
00058         case 1:
00059             hash += (signed char) * data;
00060             hash ^= hash << 10;
00061             hash += hash >> 1;
00062     }
00063 
00064     /* Force "avalanching" of final 127 bits */
00065     hash ^= hash << 3;
00066     hash += hash >> 5;
00067     hash ^= hash << 4;
00068     hash += hash >> 17;
00069     hash ^= hash << 25;
00070     hash += hash >> 6;
00071 
00072     return hash;
00073 }