Shinichi Fuchita / Twitter

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

Dependencies:   mbed HTTPClient NTPClient_NetServices EthernetNetIf

Committer:
soramimi
Date:
Thu Nov 15 00:51:45 2012 +0000
Revision:
1:c3f74457cad4
Twitter API address changed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
soramimi0:7ddb56bfde0c 1
soramimi0:7ddb56bfde0c 2#include "oauth.h" // base64 encode fn's.
soramimi0:7ddb56bfde0c 3#include "sha1.h"
soramimi0:7ddb56bfde0c 4
soramimi0:7ddb56bfde0c 5#include <string.h>
soramimi0:7ddb56bfde0c 6
soramimi0:7ddb56bfde0c 7void hmac_sha1(unsigned char const *key, size_t keylen, unsigned char const *in, size_t inlen, unsigned char *resbuf)
soramimi0:7ddb56bfde0c 8{
soramimi0:7ddb56bfde0c 9 struct SHA1Context inner;
soramimi0:7ddb56bfde0c 10 struct SHA1Context outer;
soramimi0:7ddb56bfde0c 11 unsigned char tmpkey[20];
soramimi0:7ddb56bfde0c 12 unsigned char digest[20];
soramimi0:7ddb56bfde0c 13 unsigned char block[64];
soramimi0:7ddb56bfde0c 14
soramimi0:7ddb56bfde0c 15 const int IPAD = 0x36;
soramimi0:7ddb56bfde0c 16 const int OPAD = 0x5c;
soramimi0:7ddb56bfde0c 17
soramimi0:7ddb56bfde0c 18 if (keylen > 64) {
soramimi0:7ddb56bfde0c 19 struct SHA1Context keyhash;
soramimi0:7ddb56bfde0c 20 SHA1Reset(&keyhash);
soramimi0:7ddb56bfde0c 21 SHA1Input(&keyhash, key, keylen);
soramimi0:7ddb56bfde0c 22 SHA1Result(&keyhash, tmpkey);
soramimi0:7ddb56bfde0c 23 key = tmpkey;
soramimi0:7ddb56bfde0c 24 keylen = 20;
soramimi0:7ddb56bfde0c 25 }
soramimi0:7ddb56bfde0c 26
soramimi0:7ddb56bfde0c 27 for (size_t i = 0; i < sizeof(block); i++) {
soramimi0:7ddb56bfde0c 28 block[i] = IPAD ^ (i < keylen ? key[i] : 0);
soramimi0:7ddb56bfde0c 29 }
soramimi0:7ddb56bfde0c 30 SHA1Reset(&inner);
soramimi0:7ddb56bfde0c 31 SHA1Input(&inner, block, 64);
soramimi0:7ddb56bfde0c 32 SHA1Input(&inner, in, inlen);
soramimi0:7ddb56bfde0c 33 SHA1Result(&inner, digest);
soramimi0:7ddb56bfde0c 34
soramimi0:7ddb56bfde0c 35 for (size_t i = 0; i < sizeof(block); i++) {
soramimi0:7ddb56bfde0c 36 block[i] = OPAD ^ (i < keylen ? key[i] : 0);
soramimi0:7ddb56bfde0c 37 }
soramimi0:7ddb56bfde0c 38 SHA1Reset(&outer);
soramimi0:7ddb56bfde0c 39 SHA1Input(&outer, block, 64);
soramimi0:7ddb56bfde0c 40 SHA1Input(&outer, digest, 20);
soramimi0:7ddb56bfde0c 41 SHA1Result(&outer, resbuf);
soramimi0:7ddb56bfde0c 42}
soramimi0:7ddb56bfde0c 43
soramimi0:7ddb56bfde0c 44
soramimi0:7ddb56bfde0c 45std::string oauth_sign_hmac_sha1(const char *m, const char *k)
soramimi0:7ddb56bfde0c 46{
soramimi0:7ddb56bfde0c 47 return oauth_sign_hmac_sha1_raw(m, strlen(m), k, strlen(k));
soramimi0:7ddb56bfde0c 48}
soramimi0:7ddb56bfde0c 49
soramimi0:7ddb56bfde0c 50std::string oauth_sign_hmac_sha1_raw(const char *m, const size_t ml, const char *k, const size_t kl)
soramimi0:7ddb56bfde0c 51{
soramimi0:7ddb56bfde0c 52 unsigned char result[20];
soramimi0:7ddb56bfde0c 53 hmac_sha1((unsigned char const *)k, kl, (unsigned char const *)m, ml, result);
soramimi0:7ddb56bfde0c 54 return oauth_encode_base64(result, 20);
soramimi0:7ddb56bfde0c 55}
soramimi0:7ddb56bfde0c 56