0

As of right now, I am in the process of creating a signature for OAuth 1.0 for Twitter.
Im following this.
I've ran into a small problem at the end.

They have

84 2B 52 99 88 7E 88 7602 12 A0 56 AC 4E C2 EE 16 26 B5 49

And they manage to create

hCtSmYh+iHYCEqBWrE7C7hYmtUk=

Their explanation is to Encode the "binary string" to base64.

I saw in this stackoverflow question that i basically need to decode hex and then encode base64 to get from

84 2B 52 99 88 7E 88 7602 12 A0 56 AC 4E C2 EE 16 26 B5 49

To

hCtSmYh+iHYCEqBWrE7C7hYmtUk=

And it worked in the online calculator, so now I need to implement it to javascript, the Base64 encode works correctly and looks like that:

Buffer.from("ExampleString").toString('base64')

However the Hex Decode seems to be the problem I got the solution from here.
This is my Hex Decoder

String.prototype.hexDecode = function(){
  var j;
  var hexes = this.match(/.{1,4}/g) || [];
  var back = "";
  for(j = 0; j<hexes.length; j++) {
      back += String.fromCharCode(parseInt(hexes[j], 16));
  }

  return back;
}

I get a different string when I use "my" Decoder and the online calculator, any idea?

DavidGetter
  • 349
  • 3
  • 12

1 Answers1

0

answer found here

function oAuthSignature(base_string, signing_key) {
    var signature = hmac_sha1(base_string, signing_key);
    return percentEncode(signature);
};
var jsSHA = require('jssha');
function hmac_sha1(string, secret) {
    let shaObj = new jsSHA("SHA-1", "TEXT");
    shaObj.setHMACKey(secret, "TEXT");
    shaObj.update(string);
    let hmac = shaObj.getHMAC("B64");
    return hmac;
};
function percentEncode(str) {
  return encodeURIComponent(str).replace(/[!*()']/g, (character) => {
    return '%' + character.charCodeAt(0).toString(16);
  });
};

If someone is willing to explain the answer, feel free because i'd love to understand it.

DavidGetter
  • 349
  • 3
  • 12