title: Hmac sha1 and base 64 encoding in dart
body: <p>I am trying to produce oauth_signature to use Fatsecret API, but getting an invalid signature error.</p>
  
  <p>To create oauth_signature value the Documentation says:</p>
  
  <blockquote>
    <p>Use the HMAC-SHA1 signature algorithm as defined by the [RFC2104] to sign the request where text is the Signature Base String and key is the concatenated values of the Consumer Secret and Access Secret separated by a '&amp;' character (show '&amp;' even if Access Secret is empty as some methods do not require an Access Token).<br>
    The calculated digest octet string, first base64-encoded per [RFC2045], then escaped using the [RFC3986] percent-encoding (%xx) mechanism is the oauth_signature.</p>
  </blockquote>
  
  <p>Following code is used to generate the signature</p>
  
  <pre><code>String _generateSignature(String method, String url, Map&lt;String, String&gt; params) {
  // sort the parameters
  var sortedParams = SplayTreeMap.from(params);
  
  // Concatenate the sortedParams with '&amp;'
  String concatenatedParams = sortedParams.keys.map((key) {
    return '$key=${sortedParams[key]}';
  }).join('&amp;');
  
  // encode the sorted and concatenated params string
  var encodedParams = Uri.encodeComponent(concatenatedParams);
  
  var encodedUrl = Uri.encodeComponent(url);
  
  String baseString = '$method&amp;$encodedUrl&amp;$encodedParams';
  print(baseString);
  
  String signingKey = '${Uri.encodeComponent(SHARED_SECRET)}&amp;';
  
  var hmac = Hmac(sha1, signingKey.codeUnits);
  
  return base64Encode(hmac.convert(baseString.codeUnits).bytes);
  }
  </code></pre>
  
  <p>When I make API call, it returns</p>
  
  <pre><code>11-12 09:52:45.924 15525-15612/com.example.delete I/flutter: {error: {code: 8, message: Invalid signature: oauth_signature 'y81+JIzX/P+xNqOCYLgbrMtDV2I='}}
  </code></pre>
  
  <p>Please help me, guys...I am stuck at this for weeks!</p>