KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > encoders > HexTranslator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.encoders;
21
22 /**
23  * Converters for going from hex to binary and back. Note: this class assumes ASCII processing.
24  */

25 public class HexTranslator
26 {
27     private static final byte[] hexTable =
28         {
29             (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
30             (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
31         };
32
33     /**
34      * size of the output block on encoding produced by getDecodedBlockSize()
35      * bytes.
36      */

37     public int getEncodedBlockSize()
38     {
39         return 2;
40     }
41
42     public int encode(
43         byte[] in,
44         int inOff,
45         int length,
46         byte[] out,
47         int outOff)
48     {
49         for (int i = 0, j = 0; i < length; i++, j += 2)
50         {
51             out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f];
52             out[outOff + j + 1] = hexTable[in[inOff] & 0x0f];
53
54             inOff++;
55         }
56
57         return length * 2;
58     }
59
60     /**
61      * size of the output block on decoding produced by getEncodedBlockSize()
62      * bytes.
63      */

64     public int getDecodedBlockSize()
65     {
66         return 1;
67     }
68
69     public int decode(
70         byte[] in,
71         int inOff,
72         int length,
73         byte[] out,
74         int outOff)
75     {
76         int halfLength = length / 2;
77         byte left, right;
78         for (int i = 0; i < halfLength; i++)
79         {
80             left = in[inOff + i * 2];
81             right = in[inOff + i * 2 + 1];
82
83             if (left < (byte)'a')
84             {
85                 out[outOff] = (byte)((left - '0') << 4);
86             }
87             else
88             {
89                 out[outOff] = (byte)((left - 'a' + 10) << 4);
90             }
91             if (right < (byte)'a')
92             {
93                 out[outOff] += (byte)(right - '0');
94             }
95             else
96             {
97                 out[outOff] += (byte)(right - 'a' + 10);
98             }
99
100             outOff++;
101         }
102
103         return halfLength;
104     }
105 }
106
Popular Tags