001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.codec.digest; 018 019import java.security.SecureRandom; 020import java.util.concurrent.ThreadLocalRandom; 021 022import org.apache.commons.codec.Charsets; 023 024/** 025 * GNU libc crypt(3) compatible hash method. 026 * <p> 027 * See {@link #crypt(String, String)} for further details. 028 * <p> 029 * This class is immutable and thread-safe. 030 * 031 * @since 1.7 032 */ 033public class Crypt { 034 035 /** 036 * Encrypts a password in a crypt(3) compatible way. 037 * <p> 038 * A random salt and the default algorithm (currently SHA-512) are used. See {@link #crypt(String, String)} for 039 * details. 040 * </p> 041 * <p> 042 * A salt is generated for you using {@link ThreadLocalRandom}; for more secure salts consider using 043 * {@link SecureRandom} to generate your own salts and calling {@link #crypt(byte[], String)}. 044 * </p> 045 * 046 * @param keyBytes 047 * plaintext password 048 * @return hash value 049 * @throws IllegalArgumentException 050 * when a {@link java.security.NoSuchAlgorithmException} is caught. 051 */ 052 public static String crypt(final byte[] keyBytes) { 053 return crypt(keyBytes, null); 054 } 055 056 /** 057 * Encrypts a password in a crypt(3) compatible way. 058 * <p> 059 * If no salt is provided, a random salt and the default algorithm (currently SHA-512) will be used. See 060 * {@link #crypt(String, String)} for details. 061 * 062 * @param keyBytes 063 * plaintext password 064 * @param salt 065 * real salt value without prefix or "rounds=". The salt may be null, 066 * in which case a salt is generated for you using {@link ThreadLocalRandom}; 067 * for more secure salts consider using {@link SecureRandom} to 068 * generate your own salts. 069 * @return hash value 070 * @throws IllegalArgumentException 071 * if the salt does not match the allowed pattern 072 * @throws IllegalArgumentException 073 * when a {@link java.security.NoSuchAlgorithmException} is caught. 074 */ 075 public static String crypt(final byte[] keyBytes, final String salt) { 076 if (salt == null) { 077 return Sha2Crypt.sha512Crypt(keyBytes); 078 } else if (salt.startsWith(Sha2Crypt.SHA512_PREFIX)) { 079 return Sha2Crypt.sha512Crypt(keyBytes, salt); 080 } else if (salt.startsWith(Sha2Crypt.SHA256_PREFIX)) { 081 return Sha2Crypt.sha256Crypt(keyBytes, salt); 082 } else if (salt.startsWith(Md5Crypt.MD5_PREFIX)) { 083 return Md5Crypt.md5Crypt(keyBytes, salt); 084 } else { 085 return UnixCrypt.crypt(keyBytes, salt); 086 } 087 } 088 089 /** 090 * Calculates the digest using the strongest crypt(3) algorithm. 091 * <p> 092 * A random salt and the default algorithm (currently SHA-512) are used. 093 * </p> 094 * <p> 095 * A salt is generated for you using {@link ThreadLocalRandom}; for more secure salts consider using 096 * {@link SecureRandom} to generate your own salts and calling {@link #crypt(String, String)}. 097 * </p> 098 * 099 * @see #crypt(String, String) 100 * @param key 101 * plaintext password 102 * @return hash value 103 * @throws IllegalArgumentException 104 * when a {@link java.security.NoSuchAlgorithmException} is caught. 105 */ 106 public static String crypt(final String key) { 107 return crypt(key, null); 108 } 109 110 /** 111 * Encrypts a password in a crypt(3) compatible way. 112 * <p> 113 * The exact algorithm depends on the format of the salt string: 114 * <ul> 115 * <li>SHA-512 salts start with {@code $6$} and are up to 16 chars long. 116 * <li>SHA-256 salts start with {@code $5$} and are up to 16 chars long 117 * <li>MD5 salts start with {@code $1$} and are up to 8 chars long 118 * <li>DES, the traditional UnixCrypt algorithm is used with only 2 chars 119 * <li>Only the first 8 chars of the passwords are used in the DES algorithm! 120 * </ul> 121 * The magic strings {@code "$apr1$"} and {@code "$2a$"} are not recognized by this method as its output should be 122 * identical with that of the libc implementation. 123 * <p> 124 * The rest of the salt string is drawn from the set {@code [a-zA-Z0-9./]} and is cut at the maximum length of if a 125 * {@code "$"} sign is encountered. It is therefore valid to enter a complete hash value as salt to e.g. verify a 126 * password with: 127 * 128 * <pre> 129 * storedPwd.equals(crypt(enteredPwd, storedPwd)) 130 * </pre> 131 * <p> 132 * The resulting string starts with the marker string ({@code $n$}), where n is the same as the input salt. 133 * The salt is then appended, followed by a {@code "$"} sign. 134 * This is followed by the actual hash value. 135 * For DES the string only contains the salt and actual hash. 136 * The total length is dependent on the algorithm used: 137 * <ul> 138 * <li>SHA-512: 106 chars 139 * <li>SHA-256: 63 chars 140 * <li>MD5: 34 chars 141 * <li>DES: 13 chars 142 * </ul> 143 * <p> 144 * Example: 145 * 146 * <pre> 147 * crypt("secret", "$1$xxxx") => "$1$xxxx$aMkevjfEIpa35Bh3G4bAc." 148 * crypt("secret", "xx") => "xxWAum7tHdIUw" 149 * </pre> 150 * <p> 151 * This method comes in a variation that accepts a byte[] array to support input strings that are not encoded in 152 * UTF-8 but e.g. in ISO-8859-1 where equal characters result in different byte values. 153 * 154 * @see "The man page of the libc crypt (3) function." 155 * @param key 156 * plaintext password as entered by the used 157 * @param salt 158 * real salt value without prefix or "rounds=". The salt may be null, in which case a 159 * salt is generated for you using {@link ThreadLocalRandom}; for more secure salts 160 * consider using {@link SecureRandom} to generate your own salts. 161 * @return hash value, i.e. encrypted password including the salt string 162 * @throws IllegalArgumentException 163 * if the salt does not match the allowed pattern 164 * @throws IllegalArgumentException 165 * when a {@link java.security.NoSuchAlgorithmException} is caught. * 166 */ 167 public static String crypt(final String key, final String salt) { 168 return crypt(key.getBytes(Charsets.UTF_8), salt); 169 } 170}