title: Go Hmac SHA1 generates hash different from Hmac SHA1 in Java
body: <p>I'm just starting to learn Go and I'm trying to rewrite my existing small application from Java to Go. </p>
  
  <p>I need to create Base64 hash of input string with key using Hmac SHA1 algorithm.</p>
  
  <p><strong>My Java code:</strong></p>
  
  <pre><code>private String getSignedBody(String input, String key) {
      String result = "";
      try {
          SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
          Mac mac = Mac.getInstance("HmacSHA1");
          mac.init(signingKey);
          byte[] rawHmac = mac.doFinal(input.getBytes("UTF-8"));
          result = Base64.encodeToString(rawHmac, false);
      } catch (Exception e) {
          Logger.error("Failed to generate signature: " + e.getMessage());
      }
      return result;
  }
  </code></pre>
  
  <p><strong>My Go code:</strong></p>
  
  <pre><code>func GetSignature(input, key string) string {
      key_for_sign := []byte(key)
      h := hmac.New(sha1.New, key_for_sign)
      h.Write([]byte(input))
      return base64.StdEncoding.EncodeToString(h.Sum(nil))
  }
  </code></pre>
  
  <p>The problem is that Go code generates output that is not expected. For example, for input string <code>"qwerty"</code> and key <code>"key"</code> Java output will be <code>RiD1vimxoaouU3VB1sVmchwhfhg=</code> and Go output will be <code>9Cuw7rAY671Fl65yE3EexgdghD8=</code>. </p>
  
  <p>Where did I make mistakes in the Go code?</p>