diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java b/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java new file mode 100644 index 00000000..3fff65a6 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/util/encoders/HexTranslator.java @@ -0,0 +1,87 @@ +package org.bouncycastle.util.encoders; + +/** + * Converters for going from hex to binary and back. Note: this class assumes ASCII processing. + */ +public class HexTranslator + implements Translator +{ + private static final byte[] hexTable = + { + (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', + (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' + }; + + /** + * size of the output block on encoding produced by getDecodedBlockSize() + * bytes. + */ + public int getEncodedBlockSize() + { + return 2; + } + + public int encode( + byte[] in, + int inOff, + int length, + byte[] out, + int outOff) + { + for (int i = 0, j = 0; i < length; i++, j += 2) + { + out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f]; + out[outOff + j + 1] = hexTable[in[inOff] & 0x0f]; + + inOff++; + } + + return length * 2; + } + + /** + * size of the output block on decoding produced by getEncodedBlockSize() + * bytes. + */ + public int getDecodedBlockSize() + { + return 1; + } + + public int decode( + byte[] in, + int inOff, + int length, + byte[] out, + int outOff) + { + int halfLength = length / 2; + byte left, right; + for (int i = 0; i < halfLength; i++) + { + left = in[inOff + i * 2]; + right = in[inOff + i * 2 + 1]; + + if (left < (byte)'a') + { + out[outOff] = (byte)((left - '0') << 4); + } + else + { + out[outOff] = (byte)((left - 'a' + 10) << 4); + } + if (right < (byte)'a') + { + out[outOff] += (byte)(right - '0'); + } + else + { + out[outOff] += (byte)(right - 'a' + 10); + } + + outOff++; + } + + return halfLength; + } +} |