![JAR search and dependency download from the Maven repository](/logo.png)
org.glassfish.security.common.SSHA Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2019] [Payara Foundation and/or its affiliates]
package org.glassfish.security.common;
import static java.lang.System.arraycopy;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import com.sun.enterprise.util.i18n.StringManager;
/**
* Util class for salted SHA processing.
*
*
* Salted SHA (aka SSHA) is computed as follows:
* result = {SSHA}BASE64(SHA(password,salt),salt)
*
*
* Methods are also provided to return partial results, such as SHA( password , salt) without Base64 encoding.
*
*/
public class SSHA {
private static final String SSHA_TAG = "{SSHA}";
private static final String SSHA_256_TAG = "{SSHA256}";
private static final String algoSHA = "SHA";
private static final String algoSHA256 = "SHA-256";
public static final String defaultAlgo = algoSHA256;
// TODO V3 need to check if second arg is correct
private static StringManager sm = StringManager.getManager(SSHA.class);
/**
* Compute a salted SHA hash.
*
* @param salt Salt bytes.
* @param password Password bytes.
* @return Byte array of length 20 bytes containing hash result.
* @throws IllegalArgumentException Thrown if there is an error.
*
*/
public static byte[] compute(byte[] salt, byte[] password, String algo) throws IllegalArgumentException {
byte[] buff = new byte[password.length + salt.length];
System.arraycopy(password, 0, buff, 0, password.length);
System.arraycopy(salt, 0, buff, password.length, salt.length);
byte[] hash = null;
boolean isSHA = false;
if (algoSHA.equals(algo)) {
isSHA = true;
}
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algo);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
assert (md != null);
md.reset();
hash = md.digest(buff);
if (!isSHA) {
for (int i = 2; i <= 100; i++) {
md.reset();
md.update(hash);
hash = md.digest();
}
}
if (isSHA) {
assert (hash.length == 20); // SHA output is 20 bytes
} else {
assert (hash.length == 32); // SHA-256 output is 32 bytes
}
return hash;
}
/**
* Perform encoding of salt and computed hash.
*
* @param salt Salt bytes.
* @param hash Result of prior compute() operation.
* @return String Encoded string, as described in class documentation.
*
*/
public static String encode(byte[] salt, byte[] hash, String algo) {
boolean isSHA = false;
if (algoSHA.equals(algo)) {
isSHA = true;
}
int resultLength = 32;
if (isSHA) {
resultLength = 20;
}
byte[] res = new byte[resultLength + salt.length];
arraycopy(hash, 0, res, 0, resultLength);
arraycopy(salt, 0, res, resultLength, salt.length);
String encoded = new String(Base64.getMimeEncoder().encode(res), UTF_8);
String out = SSHA_256_TAG + encoded;
if (isSHA) {
out = SSHA_TAG + encoded;
}
return out;
}
/**
* Verifies a password.
*
*
* The given password is verified against the provided encoded SSHA result string.
*
* @param encoded Encoded SSHA value (e.g. output of computeAndEncode())
* @param password Password bytes of the password to verify.
* @returns True if given password matches encoded SSHA.
* @throws IllegalArgumentException Thrown if there is an error.
*
*/
public static boolean verify(String encoded, byte[] password) throws IllegalArgumentException {
byte[] hash = new byte[20];
String algo = algoSHA256;
if (encoded.startsWith(SSHA_TAG)) {
algo = algoSHA;
}
byte[] salt = decode(encoded, hash, algo);
return verify(salt, hash, password, algo);
}
/**
* Verifies a password.
*
*
* The given password is verified against the provided salt and hash buffers.
*
* @param salt Salt bytes used in the hash result.
* @param hash Hash result to compare against.
* @param password Password bytes of the password to verify.
* @returns True if given password matches encoded SSHA.
* @throws IllegalArgumentException Thrown if there is an error.
*
*/
public static boolean verify(byte[] salt, byte[] hash, byte[] password, String algo) throws IllegalArgumentException {
byte[] newHash = compute(salt, password, algo);
return Arrays.equals(hash, newHash);
}
/**
* Decodes an encoded SSHA string.
*
* @param encoded Encoded SSHA value (e.g. output of computeAndEncode())
* @param hashResult A byte array which must contain 20 elements. Upon succesful return from method, it will be filled
* by the hash value decoded from the given SSHA string. Existing values are not used and will be overwritten.
* @returns Byte array containing the salt obtained from the encoded SSHA string.
* @throws IllegalArgumentException Thrown if there is an error.
*
*/
public static byte[] decode(String encoded, byte[] hashResult, String algo) throws IllegalArgumentException {
boolean isSHA = false;
if (algoSHA.equals(algo)) {
isSHA = true;
}
if (!encoded.startsWith(SSHA_TAG) && !encoded.startsWith(SSHA_256_TAG)) {
throw new IllegalArgumentException(sm.getString("ssha.badformat", encoded));
}
String ssha = encoded.substring(SSHA_256_TAG.length());
if (isSHA) {
ssha = encoded.substring(SSHA_TAG.length());
}
byte[] result = Base64.getMimeDecoder().decode(ssha);
int resultLength = 32;
if (isSHA) {
resultLength = 20;
}
byte[] salt = new byte[result.length - resultLength];
arraycopy(result, 0, hashResult, 0, resultLength);
arraycopy(result, resultLength, salt, 0, result.length - resultLength);
return salt;
}
}