Author: painter Date: Wed Nov 7 16:01:57 2018 New Revision: 1846051 URL: http://svn.apache.org/viewvc?rev=1846051&view=rev Log: Update junit, clean up hexconverter
Modified: turbine/fulcrum/trunk/yaafi-crypto/pom.xml turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/HexConverter.java turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml Modified: turbine/fulcrum/trunk/yaafi-crypto/pom.xml URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/pom.xml?rev=1846051&r1=1846050&r2=1846051&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/pom.xml (original) +++ turbine/fulcrum/trunk/yaafi-crypto/pom.xml Wed Nov 7 16:01:57 2018 @@ -56,7 +56,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.12</version> <scope>test</scope> </dependency> </dependencies> Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/HexConverter.java URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/HexConverter.java?rev=1846051&r1=1846050&r2=1846051&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/HexConverter.java (original) +++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/HexConverter.java Wed Nov 7 16:01:57 2018 @@ -19,29 +19,17 @@ package org.apache.fulcrum.jce.crypto; * under the License. */ - /** * Helper class to for HEX conversion. * - * The code uses parts from Markus Hahn's Blowfish library found at - * http://blowfishj.sourceforge.net/ - * - * @author <a href="mailto:siegfried.goes...@it20one.at">Siegfried Goeschl </a> + * @author <a href="mailto:pain...@apache.org">Jeffery Painter</a> + * @author <a href="mailto:siegfried.goes...@it20one.at">Siegfried Goeschl</a> * @author <a href="mailto:maa...@earthlink.net">Markus Hahn</a> */ public final class HexConverter { /** - * Table for byte to hex conversion - */ - final private static char[] HEXTAB = - { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' - }; - - /** * Converts a byte array to a hex string. * * @param data the byte array @@ -49,126 +37,36 @@ public final class HexConverter */ public static String toString( byte[] data ) { - return bytesToHexStr(data, 0, data.length); + return bytesToHexStr(data); } /** * Converts a hex string into a byte[] * - * @param data the hex string + * @param sHex the hex string * @return the byte[] */ - - public static byte[] toBytes( String data ) - { - byte[] result = new byte[data.length()/2]; - hexStrToBytes( data, result, 0, 0, result.length ); - return result; - } - + public static byte[] toBytes(String sHex) { + int len = sHex.length(); + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) ((Character.digit(sHex.charAt(i), 16) << 4) + + Character.digit(sHex.charAt(i+1), 16)); + } + return data; + } + /** * Converts a byte array to a hex string. * @param data the byte array - * @param nOfs start index where to get the bytes - * @param nLen number of bytes to convert * @return the hex string */ - private static String bytesToHexStr( - byte[] data, - int nOfs, - int nLen) + private static String bytesToHexStr( byte[] data ) { - StringBuilder sbuf; - - sbuf = new StringBuilder(); - sbuf.setLength(nLen << 1); - - int nPos = 0; - int nC = nOfs + nLen; - - while (nOfs < nC) - { - sbuf.setCharAt(nPos++, HEXTAB[(data[nOfs ] >> 4) & 0x0f]); - sbuf.setCharAt(nPos++, HEXTAB[ data[nOfs++] & 0x0f]); - } - + StringBuilder sbuf = new StringBuilder(); + for ( byte b : data ) + sbuf.append( String.format("%02x", b ) ); return sbuf.toString(); } - /** - * Converts a hex string back into a byte array (invalid codes will be - * skipped). - * @param sHex hex string - * @param data the target array - * @param nSrcOfs from which character in the string the conversion should - * begin, remember that (nSrcPos modulo 2) should equals 0 normally - * @param nDstOfs to store the bytes from which position in the array - * @param nLen number of bytes to extract - * @return number of extracted bytes - */ - private static int hexStrToBytes( - String sHex, - byte[] data, - int nSrcOfs, - int nDstOfs, - int nLen) - { - int nI, nJ, nStrLen, nAvailBytes, nDstOfsBak; - byte bActByte; - boolean blConvertOK; - - // check for correct ranges - - nStrLen = sHex.length(); - - nAvailBytes = (nStrLen - nSrcOfs) >> 1; - if (nAvailBytes < nLen) - { - nLen = nAvailBytes; - } - - int nOutputCapacity = data.length - nDstOfs; - if (nLen > nOutputCapacity) - { - nLen = nOutputCapacity; - } - - // convert now - - nDstOfsBak = nDstOfs; - - for (nI = 0; nI < nLen; nI++) - { - bActByte = 0; - blConvertOK = true; - - for (nJ = 0; nJ < 2; nJ++) - { - bActByte <<= 4; - char cActChar = sHex.charAt(nSrcOfs++); - - if ((cActChar >= 'a') && (cActChar <= 'f')) - { - bActByte |= (byte) (cActChar - 'a') + 10; - } - else - { - if ((cActChar >= '0') && (cActChar <= '9')) - { - bActByte |= (byte) (cActChar - '0'); - } - else - { - blConvertOK = false; - } - } - } - if (blConvertOK) - { - data[nDstOfs++] = bActByte; - } - } - - return (nDstOfs - nDstOfsBak); - } } Modified: turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml?rev=1846051&r1=1846050&r2=1846051&view=diff ============================================================================== --- turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml (original) +++ turbine/fulcrum/trunk/yaafi-crypto/xdocs/changes.xml Wed Nov 7 16:01:57 2018 @@ -26,12 +26,18 @@ <body> <release version="1.0.7" date="as in SVN"> <action dev="painter" type="update"> + Simplify the HexConverter code, more Java 8 friendly + </action> + <action dev="painter" type="update"> Update parent pom to Turbine 5 + </action> + <action dev="painter" type="update"> Remove references to StringBuffer + </action> + <action dev="painter" type="update"> Fix missing license from rat report </action> </release> - <release version="1.0.6" date="as in SVN"> <action dev="sgoeschl" type="update"> Using the official fulcrum-parent-1 pom.