title: Node.js Hmac SHA256 base64 of string
body: <p>I'm making an app in java and a server with node and as an authentication method I would like to compare two strings. </p>
  
  <p>In java i'm doing this: </p>
  
  <pre><code>try {
      String secret = "secret";
      String message = "Message";
  
      Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
      SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
      sha256_HMAC.init(secret_key);
  
      String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
      System.out.println(hash);
  } catch (Exception e){
      System.out.println("Error");
  }
  </code></pre>
  
  <p>But I'm still pretty new to node.js and I'm trying to figure out how to do the same there. This is what I've got:</p>
  
  <pre><code>var crypto = require('crypto');
  var sha256 = crypto.createHash('HMAC-SHA256').update('Message').digest("base64");
  </code></pre>
  
  <p>How can I make them do the same? I'm still missing the salt in node.js. 
  Suggestions?</p>
  
  <p><strong>EDIT:</strong>
  The answer below helped me find the solution. If other android users has this problem then this code worked for me:</p>
  
  <pre><code>try {
      String secret = "secret";
      String message = "Message";
  
      Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
      SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
      sha256_HMAC.init(secret_key);
      byte[] s53 = sha256_HMAC.doFinal(message.getBytes());
      String hash = Base64.encodeToString(s53, Base64.DEFAULT);
      Log.e("beadict", hash);
  } catch (Exception e){
      System.out.println("Error");
  }
  </code></pre>
  
  <p>And this in node: </p>
  
  <pre><code>var crypto = require('crypto');
  var hash = crypto.createHmac('SHA256', "secret").update("Message").digest('base64');
  </code></pre>