1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /*
20  * The apr_md5_encode() routine in the APR project's apr_md5.c file uses much
21  * code obtained from the FreeBSD 3.0 MD5 crypt() function, which is licenced
22  * as follows:
23  * ----------------------------------------------------------------------------
24  * "THE BEER-WARE LICENSE" (Revision 42):
25  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
26  * can do whatever you want with this stuff. If we meet some day, and you think
27  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
28  * ----------------------------------------------------------------------------
29  */
30 module hunt.shiro.codec.H64;
31 
32 
33 /**
34  * Codec for <a href="http://en.wikipedia.org/wiki/Crypt_(Unix)">Unix Crypt</a>-style encoding.  While similar to
35  * Base64, it is not compatible with Base64.
36  * <p/>
37  * This implementation is based on encoding algorithms found in the Apache Portable Runtime library's
38  * <a href="http://svn.apache.org/viewvc/apr/apr/trunk/crypto/apr_md5.c?revision=HEAD&view=markup">apr_md5.c</a>
39  * implementation for its {@code crypt}-style support.  The APR team in turn received inspiration for its encoding
40  * implementation based on FreeBSD 3.0's {@code /usr/src/lib/libcrypt/crypt.c} implementation.  The
41  * accompanying license headers have been retained at the top of this source file.
42  * <p/>
43  * This file and all that it contains is ASL 2.0 compatible.
44  *
45  * @since 1.2
46  */
47 class H64 {
48 
49     // private static final char[] itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
50 
51     // private static short toShort(byte b) {
52     //     return (short) (b & 0xff);
53     // }
54 
55     // private static int toInt(byte[] bytes, int offset, int numBytes) {
56     //     if (numBytes < 1 || numBytes > 4) {
57     //         throw new IllegalArgumentException("numBytes must be between 1 and 4.");
58     //     }
59     //     int val = toShort(bytes[offset]); //1st byte
60     //     for (int i = 1; i < numBytes; i++) { //any remaining bytes:
61     //         short s = toShort(bytes[offset + i]);
62     //         switch (i) {
63     //             case 1: val |= s << 8; break;
64     //             case 2: val |= s << 16; break;
65     //             case 3: val |= s << 24; break;
66     //         }
67     //     }
68     //     return val;
69     // }
70 
71     // /**
72     //  * Appends the specified character into the buffer, rethrowing any encountered
73     //  * {@link IOException} as an {@link IllegalStateException} (since this method is used for internal
74     //  * implementation needs and we only ever use StringBuilders, we should never encounter an IOException).
75     //  *
76     //  * @param buf the buffer to append to
77     //  * @param c   the character to append.
78     //  */
79     // private static void append(Appendable buf, char c) {
80     //     try {
81     //         buf.append(c);
82     //     } catch (IOException e) {
83     //         throw new IllegalStateException("Unable to append character to internal buffer.", e);
84     //     }
85     // }
86 
87     // /**
88     //  * Encodes the specified integer to {@code numChars} H64-compatible characters and appends them into {@code buf}.
89     //  *
90     //  * @param value    the integer to encode to H64-compatible characters
91     //  * @param buf      the output buffer
92     //  * @param numChars the number of characters the value should be converted to.  3, 2 or 1.
93     //  */
94     // private static void encodeAndAppend(int value, Appendable buf, int numChars) {
95     //     for (int i = 0; i < numChars; i++) {
96     //         append(buf, itoa64[value & 0x3f]);
97     //         value >>= 6;
98     //     }
99     // }
100 
101     // /**
102     //  * Encodes the specified bytes to an {@code H64}-encoded String.
103     //  *
104     //  * @param bytes
105     //  * @return
106     //  */
107     // static String encodeToString(byte[] bytes) {
108     //     if (bytes is null || bytes.length == 0) return null;
109 
110     //     StringBuilder buf = new StringBuilder();
111 
112     //     int length = bytes.length;
113     //     int remainder = length % 3;
114     //     int i = 0; //starting byte
115     //     int last3ByteIndex = length - remainder; //last byte whose index is a multiple of 3
116 
117     //     for(; i < last3ByteIndex; i += 3) {
118     //         int twentyFourBit = toInt(bytes, i, 3);
119     //         encodeAndAppend(twentyFourBit, buf, 4);
120     //     }
121     //     if (remainder > 0) {
122     //         //one or two bytes that we still need to encode:
123     //         int a = toInt(bytes, i, remainder);
124     //         encodeAndAppend(a, buf, remainder + 1);
125     //     }
126     //     return buf.toString();
127     // }
128 }