blob: 710ad53ec1aa3131732de5eb385f8e0ee6e16697 (
plain)
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
80
81
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util;
import java.io.*;
import java.security.*;
public class Sha1Crypto
{
/**
* Encodes the given text with the SHA-1 algorithm.
*
* @param text the text to encode
* @return the encoded text
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String encode(String text)
throws NoSuchAlgorithmException,
UnsupportedEncodingException
{
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] sha1hash;
messageDigest.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = messageDigest.digest();
return convertToHex(sha1hash);
}
/**
* Encodes the given text with the SHA-1 algorithm.
*
* @param byteArray the byte array to encode
* @return the encoded text
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String encode(byte[] byteArray)
throws NoSuchAlgorithmException,
UnsupportedEncodingException
{
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] sha1hash;
messageDigest.update(byteArray);
sha1hash = messageDigest.digest();
return convertToHex(sha1hash);
}
/**
* Converts the given byte data into Hex string.
*
* @param data the byte array to convert
* @return the Hex string representation of the given byte array
*/
private static String convertToHex(byte[] data)
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++)
{
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do
{
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
}
while(two_halfs++ < 1);
}
return buf.toString();
}
}
|