Twitter with OAuth Example.\\ see also http://www.soramimi.jp/twicpp/index.html

Dependencies:   mbed HTTPClient NTPClient_NetServices EthernetNetIf

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hash.cpp Source File

hash.cpp

00001 
00002 #include "oauth.h" // base64 encode fn's.
00003 #include "sha1.h"
00004 
00005 #include <string.h>
00006 
00007 void hmac_sha1(unsigned char const *key, size_t keylen, unsigned char const *in, size_t inlen, unsigned char *resbuf)
00008 {
00009     struct SHA1Context inner;
00010     struct SHA1Context outer;
00011     unsigned char tmpkey[20];
00012     unsigned char digest[20];
00013     unsigned char block[64];
00014 
00015     const int IPAD = 0x36;
00016     const int OPAD = 0x5c;
00017 
00018     if (keylen > 64) {
00019         struct SHA1Context keyhash;
00020         SHA1Reset(&keyhash);
00021         SHA1Input(&keyhash, key, keylen);
00022         SHA1Result(&keyhash, tmpkey);
00023         key = tmpkey;
00024         keylen = 20;
00025     }
00026 
00027     for (size_t i = 0; i < sizeof(block); i++) {
00028         block[i] = IPAD ^ (i < keylen ? key[i] : 0);
00029     }
00030     SHA1Reset(&inner);
00031     SHA1Input(&inner, block, 64);
00032     SHA1Input(&inner, in, inlen);
00033     SHA1Result(&inner, digest);
00034 
00035     for (size_t i = 0; i < sizeof(block); i++) {
00036         block[i] = OPAD ^ (i < keylen ? key[i] : 0);
00037     }
00038     SHA1Reset(&outer);
00039     SHA1Input(&outer, block, 64);
00040     SHA1Input(&outer, digest, 20);
00041     SHA1Result(&outer, resbuf);
00042 }
00043 
00044 
00045 std::string oauth_sign_hmac_sha1(const char *m, const char *k)
00046 {
00047     return oauth_sign_hmac_sha1_raw(m, strlen(m), k, strlen(k));
00048 }
00049 
00050 std::string oauth_sign_hmac_sha1_raw(const char *m, const size_t ml, const char *k, const size_t kl)
00051 {
00052     unsigned char result[20];
00053     hmac_sha1((unsigned char const *)k, kl, (unsigned char const *)m, ml, result);
00054     return oauth_encode_base64(result, 20);
00055 }
00056