import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import passwordderivebytes.PasswordDeriveBytes;
import sun.misc.BASE64Encoder;
public class GeraToken {
public static void main(String[] args) {
new GeraToken().TestaHash();
}
private String getHash(String inputText, String password, String salt)
{
byte[] saltBytes;
String encryptData = null;
try
{
saltBytes = salt.getBytes("UTF8");
byte[] data = inputText.getBytes(Charset.forName("UTF-16LE"));
// Por favor a baixar a JAR que acompanha o manual
PasswordDeriveBytes p2 = new PasswordDeriveBytes(password, saltBytes, "SHA1", 1);
byte []keyBytes = p2.GetBytes(16);
saltBytes = p2.GetBytes(16);
Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "Rijndael");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(saltBytes));
byte[] finalData = cipher.doFinal(data);
encryptData = new BASE64Encoder().encode(finalData);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
return encryptData;
}
public String GetUTCFormatada()
{
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("ddMMyyyyHHmm");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = dateFormatGmt.format(new Date());
return result;
}
//Testa implementação
public void TestaHash()
{
String token = "FFDB921BC60F44C685859FD4D9FAA9A9";
String inputText = GetUTCFormatada();
String data = getHash(inputText, token, (token.length()+"") );
System.out.println(data);
}
}
|