org.kapott.cryptalgs.PBKDF2 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbci4j-core Show documentation
Show all versions of hbci4j-core Show documentation
HBCI4j - Home Banking Computer Interface for Java
/* $Id: PBKDF2.java,v 1.1 2011/05/04 22:37:59 willuhn Exp $
This file is part of CryptAlgs4Java
Copyright (C) 2001-2010 Stefan Palme
CryptAlgs4Java 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.
CryptAlgs4Java 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.kapott.cryptalgs;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
// TODO: implement some SPI interface?
public class PBKDF2
{
public static byte[] deriveKey(byte[] salt, long nof_iterations, byte[] pass, int dkLen, String algname)
{
try {
SecretKeySpec keyspec=new SecretKeySpec(pass,algname);
Mac mac=Mac.getInstance(algname);
mac.init(keyspec);
// see pkcs #5 v2.0 (RDH-2) bzw. pkcs #5 v2.1 (RDH-10)
int hLen=mac.getMacLength();
byte[] dk=new byte[dkLen];
int l=((dkLen-1)/hLen)+1;
int r=dkLen - (l-1)*hLen;
// System.out.println("deriveKey: hLen="+hLen+"; l="+l+"; r="+r);
byte[] t=new byte[hLen];
for (long i=0;i>24)&0xFF);
u[salt.length+1]=(byte)(((i+1)>>16)&0xFF);
u[salt.length+2]=(byte)(((i+1)>>8)&0xFF);
u[salt.length+3]=(byte)(((i+1)>>0)&0xFF);
for (int j=0;j>3)+1)<<3;
byte diff=(byte)(newlen-len);
byte[] output=new byte[newlen];
System.arraycopy(input,0, output,0, len);
for (int i=len;i