title: Crypto.js SHA1 vs MessageDigest java SHA1: why different results?
body: <p>I need to encrypt a string. The initial version was written in Java, but it needs to be rewritten in javascript now. But i have different results.
  Here is the code in java:</p>
  
  <hr>
  
  <p>Java Version:</p>
  
  <pre><code>private static String getEncrypt(String input, String salt) throws NoSuchAlgorithmException {
          byte[] raw_salt = Base64.getDecoder().decode(salt);
          byte[] raw_data = input.getBytes(StandardCharsets.UTF_8);
          byte[] test_data = new byte[raw_salt.length + raw_data.length];
          MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
          System.arraycopy(raw_salt, 0, test_data, 0, raw_salt.length);
          System.arraycopy(raw_data, 0, test_data, raw_salt.length, raw_data.length);
          mdTemp.update(test_data);
          byte[] bytes = mdTemp.digest();
          return new String(Base64.getEncoder().encode(bytes));
      }
  input: 123456789
  salt: pMm6kWsoWjR18sWKnoG4Az==
  output: 6u/VAXS9ZKmLEbHw/OZ1AVarth4=
  </code></pre>
  
  <hr>
  
  <p>JS Version: (use crypto.js)</p>
  
  <pre><code>const crypto = require("crypto");
  
  function getEncrypt(input, salt){
     const sha1 = crypto.createHash('sha1');
     const beforeCrypto = salt + input;
     const afterCrypto = sha1.update(beforeCrypto).digest('base64');
     return afterCrypto;
  }
  
  input: 123456789
  salt: pMm6kWsoWjR18sWKnoG4Az==
  output: ie/3j+92nxvcNT5i+3WUJbWsEAg=
  </code></pre>
  
  <hr>
  
  <p>The <code>MessageDigest</code> method in java requires me to enter input in <code>byte[]</code> format. While in javascript, I use ·string· type input.
  They also use <code>salt</code> to make the encryption more safe, but it brings more different that i cannot rewrite it in javascript.
  I try many ways to solve it. But I still cannot get the same result.</p>