This commit is contained in:
Gabriele C
2016-05-04 17:26:58 +02:00
parent 39aaef8f8a
commit 5ceabe2146
16 changed files with 57 additions and 62 deletions
@@ -1,71 +1,62 @@
package fr.xephi.authme.security.pbkdf2;
/*
* Free auxiliary functions. Copyright 2007, 2014, Matthias Gärtner
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
* <p>
* Free auxiliary functions. Copyright (c) 2007 Matthias G&auml;rtner
* </p>
* <p>
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
* </p>
* <p>
* This library 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 Lesser General Public License for more
* details.
* </p>
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* </p>
* <p>
* For Details, see <a
* href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
* >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
* </p>
* Free auxiliary functions
*
* @author Matthias G&auml;rtner
* @version 1.0
*/
public class BinTools {
private static final String HEX_CHARS = "0123456789ABCDEF";
private BinTools() {
}
public static final String hex = "0123456789ABCDEF";
/**
* Simple binary-to-hexadecimal conversion.
*
* @param b Input bytes. May be <code>null</code>.
*
* @param b
* Input bytes. May be <code>null</code>.
* @return Hexadecimal representation of b. Uppercase A-F, two characters
* per byte. Empty string on <code>null</code> input.
* per byte. Empty string on <code>null</code> input.
*/
public static String bin2hex(final byte[] b) {
if (b == null) {
return "";
}
StringBuffer stringBuffer = new StringBuffer(2 * b.length);
for (byte aB : b) {
int v = (256 + aB) % 256;
stringBuffer.append(HEX_CHARS.charAt((v / 16) & 15));
stringBuffer.append(HEX_CHARS.charAt((v % 16) & 15));
StringBuffer sb = new StringBuffer(2 * b.length);
for (int i = 0; i < b.length; i++) {
int v = (256 + b[i]) % 256;
sb.append(hex.charAt((v / 16) & 15));
sb.append(hex.charAt((v % 16) & 15));
}
return stringBuffer.toString();
return sb.toString();
}
/**
* Convert hex string to array of bytes.
*
* @param s String containing hexadecimal digits. May be <code>null</code>.
* On odd length leading zero will be assumed.
*
* @param s
* String containing hexadecimal digits. May be <code>null</code>
* . On odd length leading zero will be assumed.
* @return Array on bytes, non-<code>null</code>.
* @throws IllegalArgumentException when string contains non-hex character
* @throws IllegalArgumentException
* when string contains non-hex character
*/
public static byte[] hex2bin(final String s) {
String m = s;
@@ -88,10 +79,11 @@ public class BinTools {
/**
* Convert hex digit to numerical value.
*
* @param c 0-9, a-f, A-F allowed.
*
* @param c
* 0-9, a-f, A-F allowd.
* @return 0-15
* @throws IllegalArgumentException on non-hex character
* @throws IllegalArgumentException
* on non-hex character
*/
public static int hex2bin(char c) {
if (c >= '0' && c <= '9') {
@@ -106,9 +98,7 @@ public class BinTools {
throw new IllegalArgumentException("Input string may only contain hex digits, but found '" + c + "'");
}
// Note ljacqu 20160313: This appears to be a test method that was present in the third-party source.
// We can keep it for troubleshooting in the future.
private static void testUtils() {
public static void main(String[] args) {
byte b[] = new byte[256];
byte bb = 0;
for (int i = 0; i < 256; i++) {