Java tutorial
//package com.java2s; import java.security.MessageDigest; import java.util.Formatter; public class Main { private static final String ERROR_SHA1 = "error_sha1"; private static final String UTF_8 = "UTF-8"; public static String toSHA1(String s) { MessageDigest md = null; byte[] sha1hash = null; try { md = MessageDigest.getInstance("SHA-1"); sha1hash = new byte[40]; md.update(s.getBytes(UTF_8), 0, s.length()); } catch (Exception e) { return ERROR_SHA1; } sha1hash = md.digest(); Formatter formatter = new Formatter(); for (byte b : sha1hash) { formatter.format("%02x", b); } return formatter.toString(); } }