From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
To: <email@hidden>
Subject: Re: Encryption Code Problem
Date: Tue, 3 Jun 2003 15:32:48 -0500
I wrote:
>Jonathan Fleming, email@hidden, wrote:
>>><http://www.wodeveloper.com/omniLists/webobjects-dev/2002/July/msg00157.htm
>>>l>
>>
>>Thanks for this Jonathan but that link seems to be dead. Is there an
>>alternative way of obtaining this?
>
>That link worked for me this AM, and works for me now. I'll include the
>code here.
OK, I found my version of this code on my disk. I prefered lower-case hex
characters, and I believe Raphael's original encode() implementation
reversed the hex output. Anyway, here's my version. It's been through
extensive unit tests (grin).
// HexCoder.java
//
// Copyright (C) 2001 Petite Abeille. All Rights Reserved.
// This class is hereby released for all uses. No warranties whatsoever.
//
// by Petite Abeille <email@hidden>
//
// Sat Sep 8 2001: Created.
import org.apache.log4j.Logger;
public class HexCoder {
private static Logger log = Logger.getLogger( HexCoder.class );
private static final char[] Map = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
public static String encode(byte[] someBytes) {
if (someBytes != null) {
StringBuffer aBuffer = new StringBuffer();
int length = someBytes.length;
for (int i = 0; i < length; i++) {
byte aByte = someBytes[i];
aBuffer.append(HexCoder.Map[((aByte >> 4) & 0x0f)]);
aByte &= 0x0f;
aBuffer.append(HexCoder.Map[aByte]);
}
return aBuffer.toString();
}
throw new IllegalArgumentException("HexCoder.decode: null bytes.");
}
public static byte[] decode(String aString) {
if (aString != null) {
int aLength = aString.length();
byte[] someBytes = new byte[aLength / 2];
int anIndex = 0;
int anotherIndex = aLength / 2;
char[] someChars = aString.toCharArray();
while (anIndex < aLength) {
char upperChar = someChars[anIndex];
char lowerChar = someChars[anIndex + 1];
someBytes[--anotherIndex] =
(byte) ((HexCoder.charToNibble(upperChar) << 4)
+ HexCoder.charToNibble(lowerChar));
anIndex += 2;
}
return someBytes;
}
throw new IllegalArgumentException("HexCoder.decode: null string.");
}
private static int charToNibble(char aChar) {
if ((aChar >= '0') && (aChar <= '9')) {
return aChar - '0';
}
if ((aChar >= 'A') && (aChar <= 'F')) {
return aChar - 'A' + 10;
}
if ((aChar >= 'a') && (aChar <= 'f')) {
return aChar - 'a' + 10;
}
return 0;
}
};
.......................................................
Jonathan 'Wolf' Rentzsch http://rentzsch.com
Red Shed Software http://redshed.net (847) 584-7465
PGP: b2af 1a09 f881 ebde c9d6 c4d2 c04f a3c0 3ec5 d5f2
"better" necessarily means "different"
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.