Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 51b0c91659be6e5b36480450cb0827a2bd9a7f98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
 * Copyright (c) 2011 SAP AG
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.console.storage;

import java.security.MessageDigest;
/*******************************************************************************
 * Copyright (c) 2011 SAP AG
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation
 *******************************************************************************/

import java.security.NoSuchAlgorithmException;

/**
 * This class provides utility method for one-way hashing of strings
 * 
 */
public class DigestUtil {
	private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
	private static final String MD5 = "MD5";
	private static final String SHA1 = "SHA1";
	
	/**
	 * Create a one-way hash of an input strings. First a MD5 hash of the input string
	 * is calculated and appended to the string, and then the new string is hashed with SHA1  
	 * 
	 * @param originalText the string to be hashed
	 * @return hashed string
	 * @throws Exception
	 */
	public static String encrypt(String originalText)throws Exception{
		try {
			String password_salt = appendSalt(originalText);
			byte[] sha_digest;

			sha_digest = getDigest(password_salt.getBytes(), SHA1);
			return asHex(sha_digest);
		} catch (NoSuchAlgorithmException e) {
			throw new Exception ("Encryption Failed!");
		}		
	}
	
	private static String appendSalt(String inputPassword) throws NoSuchAlgorithmException{
		byte [] salt = getDigest(inputPassword.getBytes(), MD5);
		return inputPassword + asHex(salt);
	}
	
	//byte array into hexademical string
    private static String asHex(byte[] buf)
    {
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i)
        {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }
    
    //generate digest byte[]  
    private static byte[] getDigest(byte[] inputData, String algorithm) throws NoSuchAlgorithmException { 
    	MessageDigest md = MessageDigest.getInstance(algorithm);
    	md.update(inputData);
        return md.digest();
    }
}

Back to the top