001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.server.users; 020 021import java.io.UnsupportedEncodingException; 022import java.nio.charset.Charset; 023import java.security.MessageDigest; 024import java.security.NoSuchAlgorithmException; 025 026import org.apache.commons.codec.binary.Base64; 027import org.slf4j.LoggerFactory; 028 029/** 030 * This class provides hashing service to passwords with SHA-1 algoritm 031 * 032 * @author Samuel Campos <samuelcampos@ua.pt> 033 */ 034public class HashService { 035 /** 036 * Encrypt one password with SHA-1 algorithm 037 * 038 * @param plaintext 039 * @return the hash of the password 040 */ 041 public static String getSHA1Hash(String plaintext) { 042 try { 043 MessageDigest md = MessageDigest.getInstance("SHA-1"); 044 md.update(plaintext.getBytes("UTF-8")); 045 byte[] raw = md.digest(); 046 byte[] asbase64 = Base64.encodeBase64(raw); 047 String hash = new String(asbase64, Charset.forName("UTF-8")); 048 return hash; 049 050 } catch (UnsupportedEncodingException|NoSuchAlgorithmException ex) { 051 LoggerFactory.getLogger(HashService.class).error("Failed to encode text", ex); 052 } 053 054 return null; 055 } 056 057}