Here you can find the source of generateSaltedPassword(final String password, byte[] salt, int iterationsCount, String hmacName)
Parameter | Description |
---|---|
password | Clear form password, i.e. what user typed |
salt | Salt to be used |
iterationsCount | Iterations for 'salting' |
hmacName | HMAC to be used |
Parameter | Description |
---|---|
InvalidKeyException | if internal error occur while working with SecretKeySpec |
NoSuchAlgorithmException | if hmacName is not supported by the java |
public static byte[] generateSaltedPassword(final String password, byte[] salt, int iterationsCount, String hmacName) throws InvalidKeyException, NoSuchAlgorithmException
//package com.java2s; /*/* www . ja va 2s . c o m*/ * Copyright 2016 Ognyan Bankov * <p> * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class Main { private static final byte[] INT_1 = new byte[] { 0, 0, 0, 1 }; /** * Generates salted password. * * @param password Clear form password, i.e. what user typed * @param salt Salt to be used * @param iterationsCount Iterations for 'salting' * @param hmacName HMAC to be used * @return salted password * @throws InvalidKeyException if internal error occur while working with SecretKeySpec * @throws NoSuchAlgorithmException if hmacName is not supported by the java */ public static byte[] generateSaltedPassword(final String password, byte[] salt, int iterationsCount, String hmacName) throws InvalidKeyException, NoSuchAlgorithmException { Mac mac = createHmac(password.getBytes(StandardCharsets.US_ASCII), hmacName); mac.update(salt); mac.update(INT_1); byte[] result = mac.doFinal(); byte[] previous = null; for (int i = 1; i < iterationsCount; i++) { mac.update(previous != null ? previous : result); previous = mac.doFinal(); for (int x = 0; x < result.length; x++) { result[x] ^= previous[x]; } } return result; } /** * Creates HMAC * * @param keyBytes key * @param hmacName HMAC name * @return Mac * @throws InvalidKeyException if internal error occur while working with SecretKeySpec * @throws NoSuchAlgorithmException if hmacName is not supported by the java */ public static Mac createHmac(final byte[] keyBytes, String hmacName) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec key = new SecretKeySpec(keyBytes, hmacName); Mac mac = Mac.getInstance(hmacName); mac.init(key); return mac; } }