java - How can I generate an SHA1 in android like most sites and programs do? -


http://www.sha1-online.com/ , http://passwordsgenerator.net/sha1-hash-generator/ , http://www.miraclesalad.com/webtools/sha1.php

-all return 40bd001563085fc35165329ea1ff5c5ecbdbbeef sha-1 of 123

however, code:

public static string sha1(string clearstring) {     try     {         messagedigest messagedigest = messagedigest.getinstance("sha-1");         messagedigest.update(clearstring.getbytes("utf-8"));         byte[] bytes = messagedigest.digest();         stringbuilder buffer = new stringbuilder();         (byte b : bytes)         {             buffer.append(integer.tostring((b & 0xff) + 0x100, 16).substring(1));         }         return buffer.tostring();     }     catch (exception ignored)     {         ignored.printstacktrace();         return null;     } } 

and many other codes i've tried, different. above code get: 9c9598069a40434b500d862e1a13ab9d5a969fc8 123.

seeing sites use same algorithm, seems i'm doing wrong.

looks problem in way converting string.

public static string bytearraytostring(byte[] bytes) {     stringbuilder buffer = new stringbuilder();     (byte b : bytes) {         buffer.append(string.format(locale.getdefault(), "%02x", b));     }     return buffer.tostring(); }  public static string sha1(string clearstring) {     try {         messagedigest messagedigest = messagedigest.getinstance("sha-1");         messagedigest.update(clearstring.getbytes("utf-8"));         return bytearraytostring(messagedigest.digest());     } catch (exception ignored) {         ignored.printstacktrace();         return null;     } } 

so sha1("123") result in : 40bd001563085fc35165329ea1ff5c5ecbdbbeef


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -