title: Signature with HMAC SHA-256 and Base64Encoder
body: <p>I tried a lot for creating signature to access one web service.</p>
  
  <p>They required that I generate a signature with each request.</p>
  
  <p>For generating signature, I have one message say "abc" and one secrete key say "xyz".  According to them my signature should be processed as the following ruby code</p>
  
  <pre><code>require 'base64'
  
  require 'openssl'
  
  secret = "xyz"
  
  request = "abc"
  
  digest = OpenSSL::Digest::Digest.new('sha256')
  
  signature = Base64.encode64(OpenSSL::HMAC.digest(digest, secret, request)).chomp
  </code></pre>
  
  <p><code>signature</code> should be <code>9ZjsfVB3k5nPNLf5he+gfyYaxNWCIJ6J8YcRpxW5GG0=</code> but I am not getting this using Java code which is as below:</p>
  
  <pre><code> SecretKey secretKey = null;    
  
   byte[] keyBytes = keyString.getBytes("UTF-8");     
  
   Mac mac = Mac.getInstance("HMACHSA256");
  
   secretKey = new SecretKeySpec(keyBytes,mac.getAlgorithm());
  
   mac.init(secretKey);   
  
   byte[] text = baseString.getBytes("UTF-8");
  
   //mac.update(digest.digest());
  
   byte[] encodedText = mac.doFinal(text);    
  
   return new String(Base64.encodeBase64(encodedText)).trim();
  </code></pre>
  
  <p>Please help me on this.</p>