title: Converting Java to Python
body: <p>I was wondering if you guys could help me convert some java code to python. Basically I'm trying to get the same hash output that the java code executes. Currently all I have gotten to (in python) is using the hashlib module converting some string to sha256.</p>
  
  <p>I have tried googling and I have found nothing, friends couldn't help so this is my last resort. Here is the hash outputs I get from python &amp; java. Obviously python being the incorrect hash, and java code being the correct hash.</p>
  
  <p><strong>Java Code Output Hash(correct):</strong></p>
  
  <pre><code>947e238ef26e56d3de2fe2480268c23297d91efe6355b3e5fac0dcf63
  </code></pre>
  
  <p><strong>Python Code Output Hash(incorrect):</strong></p>
  
  <pre><code>88c5f638b023a5740676960747833fc4eacc20697c92e1b46b0384ebf
  </code></pre>
  
  <p>The java code hashes a string which gets split into variables, im stuck on the for loop which I'm not able to convert to python. PLEASE take note I am a noob and not that very experienced, thanks :P.</p>
  
  <p><strong>Java CODE:</strong></p>
  
  <pre><code>    String pass = "6b508/^!zVl?947e238ef26e56d3de2fe2480268c23297d91efe6355b3e5fac0dcf63";
      String secretKey = "Wr79mk1PXFac!#%";
      String loginKey = pass.substring(0, 5);
      String randomKey = pass.substring(5, 13);
      String originalHash = pass.substring(13);
  
      try {
          MessageDigest md = MessageDigest.getInstance("SHA-256");
          md.update((loginKey + randomKey + secretKey).getBytes(StandardCharsets.UTF_8));
  
          //obtain hash
          byte[] hash = md.digest();
          StringBuilder hexString = new StringBuilder();
          for (byte aHash : hash) {
              String hex = Integer.toHexString(0xff &amp; aHash);
              if (hex.length() == 1) hexString.append('0');
              hexString.append(hex);
          }
          String newHash = hexString.toString().substring(0, 56);
          if (newHash.equals(originalHash)) {
              //hash is right
              return true;
          }
  </code></pre>
  
  <p><strong>Python CODE(My attempt to convert):</strong></p>
  
  <pre><code>key = "6b508"
  salt = "Wr79mk1PXFac!#%"
  rand = "/^!zVl?"
  hash = hashlib.sha256(key+rand+salt).hexdigest()
  raw_input(hash[0:57])
  </code></pre>
  
  <p>Any help would be appreciated, thanks.</p>