Here you can find the source of sha1Hash(String input)
Parameter | Description |
---|---|
input | a parameter |
public static String sha1Hash(String input)
//package com.java2s; /*//from ww w . j a v a 2 s .c o m * Copyright (c) 2016. Sten Martinez * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { private static MessageDigest _sha1Digest = null; /** * TODO: move to better algorithm? * @param input * @return */ public static String sha1Hash(String input) { if (_sha1Digest == null) { try { _sha1Digest = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { // this will not fail. e.printStackTrace(); return null; } } byte[] digest = _sha1Digest.digest(input.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } }