author | Da Risk <da_risk@beem-project.com> |
Sun, 15 Mar 2015 18:03:03 +0100 | |
changeset 1044 | 197a85a35cba |
parent 1015 | src/com/isode/stroke/stringcodecs/Base64BSD.java@63669480c941 |
permissions | -rw-r--r-- |
1015
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
1 |
package com.isode.stroke.stringcodecs; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
2 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
3 |
import java.util.Arrays; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
4 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
5 |
/** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
6 |
* with RFC 2045.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
7 |
* On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
8 |
* on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
9 |
* compared to <code>sun.misc.Encoder()/Decoder()</code>.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
10 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
11 |
* On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
12 |
* about 50% faster for decoding large arrays. This implementation is about twice as fast on very small |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
13 |
* arrays (< 30 bytes). If source/destination is a <code>String</code> this |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
14 |
* version is about three times as fast due to the fact that the Commons Codec result has to be recoded |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
15 |
* to a <code>String</code> from <code>byte[]</code>, which is very expensive.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
16 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
17 |
* This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
18 |
* allocates the resulting array. This produces less garbage and it is possible to handle arrays twice |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
19 |
* as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
20 |
* whether Sun's <code>sun.misc.Encoder()/Decoder()</code> produce temporary arrays but since performance |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
21 |
* is quite low it probably does.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
22 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
23 |
* The encoder produces the same output as the Sun one except that the Sun's encoder appends |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
24 |
* a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
25 |
* length and is probably a side effect. Both are in conformance with RFC 2045 though.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
26 |
* Commons codec seem to always att a trailing line separator.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
27 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
28 |
* <b>Note!</b> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
29 |
* The encode/decode method pairs (types) come in three versions with the <b>exact</b> same algorithm and |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
30 |
* thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
31 |
* format types. The methods not used can simply be commented out.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
32 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
33 |
* There is also a "fast" version of all decode methods that works the same way as the normal ones, but |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
34 |
* har a few demands on the decoded input. Normally though, these fast verions should be used if the source if |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
35 |
* the input is known and it hasn't bee tampered with.<br><br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
36 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
37 |
* If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom . com. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
38 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
39 |
* Licence (BSD): |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
40 |
* ============== |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
41 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
42 |
* Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
43 |
* All rights reserved. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
44 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
45 |
* Redistribution and use in source and binary forms, with or without modification, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
46 |
* are permitted provided that the following conditions are met: |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
47 |
* Redistributions of source code must retain the above copyright notice, this list |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
48 |
* of conditions and the following disclaimer. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
49 |
* Redistributions in binary form must reproduce the above copyright notice, this |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
50 |
* list of conditions and the following disclaimer in the documentation and/or other |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
51 |
* materials provided with the distribution. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
52 |
* Neither the name of the MiG InfoCom AB nor the names of its contributors may be |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
53 |
* used to endorse or promote products derived from this software without specific |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
54 |
* prior written permission. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
55 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
56 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
57 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
58 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
59 |
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
60 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
61 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
62 |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
63 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
64 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
65 |
* OF SUCH DAMAGE. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
66 |
* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
67 |
* @version 2.2 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
68 |
* @author Mikael Grev |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
69 |
* Date: 2004-aug-02 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
70 |
* Time: 11:31:11 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
71 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
72 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
73 |
public class Base64BSD |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
74 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
75 |
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
76 |
private static final int[] IA = new int[256]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
77 |
static { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
78 |
Arrays.fill(IA, -1); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
79 |
for (int i = 0, iS = CA.length; i < iS; i++) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
80 |
IA[CA[i]] = i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
81 |
IA['='] = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
82 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
83 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
84 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
85 |
// * char[] version |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
86 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
87 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
88 |
/** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
89 |
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
90 |
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
91 |
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
92 |
* little faster. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
93 |
* @return A BASE64 encoded array. Never <code>null</code>. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
94 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
95 |
public final static char[] encodeToChar(byte[] sArr, boolean lineSep) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
96 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
97 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
98 |
int sLen = sArr != null ? sArr.length : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
99 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
100 |
return new char[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
101 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
102 |
int eLen = (sLen / 3) * 3; // Length of even 24-bits. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
103 |
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
104 |
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
105 |
char[] dArr = new char[dLen]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
106 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
107 |
// Encode even 24-bits |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
108 |
for (int s = 0, d = 0, cc = 0; s < eLen;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
109 |
// Copy next three bytes into lower 24 bits of int, paying attension to sign. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
110 |
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
111 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
112 |
// Encode the int into four chars |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
113 |
dArr[d++] = CA[(i >>> 18) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
114 |
dArr[d++] = CA[(i >>> 12) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
115 |
dArr[d++] = CA[(i >>> 6) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
116 |
dArr[d++] = CA[i & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
117 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
118 |
// Add optional line separator |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
119 |
if (lineSep && ++cc == 19 && d < dLen - 2) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
120 |
dArr[d++] = '\r'; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
121 |
dArr[d++] = '\n'; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
122 |
cc = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
123 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
124 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
125 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
126 |
// Pad and encode last bits if source isn't even 24 bits. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
127 |
int left = sLen - eLen; // 0 - 2. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
128 |
if (left > 0) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
129 |
// Prepare the int |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
130 |
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
131 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
132 |
// Set last four chars |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
133 |
dArr[dLen - 4] = CA[i >> 12]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
134 |
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
135 |
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '='; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
136 |
dArr[dLen - 1] = '='; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
137 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
138 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
139 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
140 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
141 |
/** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
142 |
* and without line separators. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
143 |
* @param sArr The source array. <code>null</code> or length 0 will return an empty array. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
144 |
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
145 |
* (including '=') isn't divideable by 4. (I.e. definitely corrupted). |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
146 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
147 |
public final static byte[] decode(char[] sArr) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
148 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
149 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
150 |
int sLen = sArr != null ? sArr.length : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
151 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
152 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
153 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
154 |
// Count illegal characters (including '\r', '\n') to know what size the returned array will be, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
155 |
// so we don't have to reallocate & copy it later. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
156 |
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
157 |
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
158 |
if (IA[sArr[i]] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
159 |
sepCnt++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
160 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
161 |
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
162 |
if ((sLen - sepCnt) % 4 != 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
163 |
return null; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
164 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
165 |
int pad = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
166 |
for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
167 |
if (sArr[i] == '=') |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
168 |
pad++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
169 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
170 |
int len = ((sLen - sepCnt) * 6 >> 3) - pad; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
171 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
172 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
173 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
174 |
for (int s = 0, d = 0; d < len;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
175 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
176 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
177 |
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
178 |
int c = IA[sArr[s++]]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
179 |
if (c >= 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
180 |
i |= c << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
181 |
else |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
182 |
j--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
183 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
184 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
185 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
186 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
187 |
dArr[d++]= (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
188 |
if (d < len) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
189 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
190 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
191 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
192 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
193 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
194 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
195 |
/** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
196 |
* fast as {@link #decode(char[])}. The preconditions are:<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
197 |
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
198 |
* + Line separator must be "\r\n", as specified in RFC 2045 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
199 |
* + The array must not contain illegal characters within the encoded string<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
200 |
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
201 |
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
202 |
* @return The decoded array of bytes. May be of length 0. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
203 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
204 |
public final static byte[] decodeFast(char[] sArr) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
205 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
206 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
207 |
int sLen = sArr.length; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
208 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
209 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
210 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
211 |
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
212 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
213 |
// Trim illegal chars from start |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
214 |
while (sIx < eIx && IA[sArr[sIx]] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
215 |
sIx++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
216 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
217 |
// Trim illegal chars from end |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
218 |
while (eIx > 0 && IA[sArr[eIx]] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
219 |
eIx--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
220 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
221 |
// get the padding count (=) (0, 1 or 2) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
222 |
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
223 |
int cCnt = eIx - sIx + 1; // Content count including possible separators |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
224 |
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
225 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
226 |
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
227 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
228 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
229 |
// Decode all but the last 0 - 2 bytes. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
230 |
int d = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
231 |
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
232 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
233 |
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
234 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
235 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
236 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
237 |
dArr[d++] = (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
238 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
239 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
240 |
// If line separator, jump over it. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
241 |
if (sepCnt > 0 && ++cc == 19) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
242 |
sIx += 2; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
243 |
cc = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
244 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
245 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
246 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
247 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
248 |
// Decode last 1-3 bytes (incl '=') into 1-3 bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
249 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
250 |
for (int j = 0; sIx <= eIx - pad; j++) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
251 |
i |= IA[sArr[sIx++]] << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
252 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
253 |
for (int r = 16; d < len; r -= 8) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
254 |
dArr[d++] = (byte) (i >> r); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
255 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
256 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
257 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
258 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
259 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
260 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
261 |
// * byte[] version |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
262 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
263 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
264 |
/** Encodes a raw byte array into a BASE64 <code>byte[]</code> representation i accordance with RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
265 |
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
266 |
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
267 |
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
268 |
* little faster. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
269 |
* @return A BASE64 encoded array. Never <code>null</code>. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
270 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
271 |
public final static byte[] encodeToByte(byte[] sArr, boolean lineSep) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
272 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
273 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
274 |
int sLen = sArr != null ? sArr.length : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
275 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
276 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
277 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
278 |
int eLen = (sLen / 3) * 3; // Length of even 24-bits. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
279 |
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
280 |
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
281 |
byte[] dArr = new byte[dLen]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
282 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
283 |
// Encode even 24-bits |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
284 |
for (int s = 0, d = 0, cc = 0; s < eLen;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
285 |
// Copy next three bytes into lower 24 bits of int, paying attension to sign. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
286 |
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
287 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
288 |
// Encode the int into four chars |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
289 |
dArr[d++] = (byte) CA[(i >>> 18) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
290 |
dArr[d++] = (byte) CA[(i >>> 12) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
291 |
dArr[d++] = (byte) CA[(i >>> 6) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
292 |
dArr[d++] = (byte) CA[i & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
293 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
294 |
// Add optional line separator |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
295 |
if (lineSep && ++cc == 19 && d < dLen - 2) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
296 |
dArr[d++] = '\r'; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
297 |
dArr[d++] = '\n'; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
298 |
cc = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
299 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
300 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
301 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
302 |
// Pad and encode last bits if source isn't an even 24 bits. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
303 |
int left = sLen - eLen; // 0 - 2. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
304 |
if (left > 0) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
305 |
// Prepare the int |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
306 |
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
307 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
308 |
// Set last four chars |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
309 |
dArr[dLen - 4] = (byte) CA[i >> 12]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
310 |
dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
311 |
dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '='; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
312 |
dArr[dLen - 1] = '='; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
313 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
314 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
315 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
316 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
317 |
/** Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
318 |
* and without line separators. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
319 |
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
320 |
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
321 |
* (including '=') isn't divideable by 4. (I.e. definitely corrupted). |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
322 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
323 |
public final static byte[] decode(byte[] sArr) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
324 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
325 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
326 |
int sLen = sArr.length; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
327 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
328 |
// Count illegal characters (including '\r', '\n') to know what size the returned array will be, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
329 |
// so we don't have to reallocate & copy it later. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
330 |
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
331 |
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
332 |
if (IA[sArr[i] & 0xff] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
333 |
sepCnt++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
334 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
335 |
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
336 |
if ((sLen - sepCnt) % 4 != 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
337 |
return null; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
338 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
339 |
int pad = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
340 |
for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
341 |
if (sArr[i] == '=') |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
342 |
pad++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
343 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
344 |
int len = ((sLen - sepCnt) * 6 >> 3) - pad; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
345 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
346 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
347 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
348 |
for (int s = 0, d = 0; d < len;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
349 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
350 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
351 |
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
352 |
int c = IA[sArr[s++] & 0xff]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
353 |
if (c >= 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
354 |
i |= c << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
355 |
else |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
356 |
j--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
357 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
358 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
359 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
360 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
361 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
362 |
dArr[d++]= (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
363 |
if (d < len) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
364 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
365 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
366 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
367 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
368 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
369 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
370 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
371 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
372 |
/** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
373 |
* fast as {@link #decode(byte[])}. The preconditions are:<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
374 |
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
375 |
* + Line separator must be "\r\n", as specified in RFC 2045 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
376 |
* + The array must not contain illegal characters within the encoded string<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
377 |
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
378 |
* @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
379 |
* @return The decoded array of bytes. May be of length 0. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
380 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
381 |
public final static byte[] decodeFast(byte[] sArr) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
382 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
383 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
384 |
int sLen = sArr.length; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
385 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
386 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
387 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
388 |
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
389 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
390 |
// Trim illegal chars from start |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
391 |
while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
392 |
sIx++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
393 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
394 |
// Trim illegal chars from end |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
395 |
while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
396 |
eIx--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
397 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
398 |
// get the padding count (=) (0, 1 or 2) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
399 |
int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
400 |
int cCnt = eIx - sIx + 1; // Content count including possible separators |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
401 |
int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
402 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
403 |
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
404 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
405 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
406 |
// Decode all but the last 0 - 2 bytes. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
407 |
int d = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
408 |
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
409 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
410 |
int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
411 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
412 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
413 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
414 |
dArr[d++] = (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
415 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
416 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
417 |
// If line separator, jump over it. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
418 |
if (sepCnt > 0 && ++cc == 19) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
419 |
sIx += 2; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
420 |
cc = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
421 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
422 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
423 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
424 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
425 |
// Decode last 1-3 bytes (incl '=') into 1-3 bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
426 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
427 |
for (int j = 0; sIx <= eIx - pad; j++) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
428 |
i |= IA[sArr[sIx++]] << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
429 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
430 |
for (int r = 16; d < len; r -= 8) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
431 |
dArr[d++] = (byte) (i >> r); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
432 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
433 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
434 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
435 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
436 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
437 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
438 |
// * String version |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
439 |
// **************************************************************************************** |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
440 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
441 |
/** Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
442 |
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
443 |
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
444 |
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
445 |
* little faster. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
446 |
* @return A BASE64 encoded array. Never <code>null</code>. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
447 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
448 |
public final static String encodeToString(byte[] sArr, boolean lineSep) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
449 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
450 |
// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
451 |
return new String(encodeToChar(sArr, lineSep)); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
452 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
453 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
454 |
/** Decodes a BASE64 encoded <code>String</code>. All illegal characters will be ignored and can handle both strings with |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
455 |
* and without line separators.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
456 |
* <b>Note!</b> It can be up to about 2x the speed to call <code>decode(str.toCharArray())</code> instead. That |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
457 |
* will create a temporary array though. This version will use <code>str.charAt(i)</code> to iterate the string. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
458 |
* @param str The source string. <code>null</code> or length 0 will return an empty array. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
459 |
* @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
460 |
* (including '=') isn't divideable by 4. (I.e. definitely corrupted). |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
461 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
462 |
public final static byte[] decode(String str) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
463 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
464 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
465 |
int sLen = str != null ? str.length() : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
466 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
467 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
468 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
469 |
// Count illegal characters (including '\r', '\n') to know what size the returned array will be, |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
470 |
// so we don't have to reallocate & copy it later. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
471 |
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
472 |
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
473 |
if (IA[str.charAt(i)] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
474 |
sepCnt++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
475 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
476 |
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
477 |
if ((sLen - sepCnt) % 4 != 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
478 |
return null; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
479 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
480 |
// Count '=' at end |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
481 |
int pad = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
482 |
for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
483 |
if (str.charAt(i) == '=') |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
484 |
pad++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
485 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
486 |
int len = ((sLen - sepCnt) * 6 >> 3) - pad; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
487 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
488 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
489 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
490 |
for (int s = 0, d = 0; d < len;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
491 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
492 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
493 |
for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
494 |
int c = IA[str.charAt(s++)]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
495 |
if (c >= 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
496 |
i |= c << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
497 |
else |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
498 |
j--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
499 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
500 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
501 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
502 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
503 |
dArr[d++]= (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
504 |
if (d < len) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
505 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
506 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
507 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
508 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
509 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
510 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
511 |
/** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
512 |
* fast as {@link #decode(String)}. The preconditions are:<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
513 |
* + The array must have a line length of 76 chars OR no line separators at all (one line).<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
514 |
* + Line separator must be "\r\n", as specified in RFC 2045 |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
515 |
* + The array must not contain illegal characters within the encoded string<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
516 |
* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
517 |
* @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
518 |
* @return The decoded array of bytes. May be of length 0. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
519 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
520 |
public final static byte[] decodeFast(String s) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
521 |
{ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
522 |
// Check special case |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
523 |
int sLen = s.length(); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
524 |
if (sLen == 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
525 |
return new byte[0]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
526 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
527 |
int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
528 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
529 |
// Trim illegal chars from start |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
530 |
while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
531 |
sIx++; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
532 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
533 |
// Trim illegal chars from end |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
534 |
while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
535 |
eIx--; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
536 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
537 |
// get the padding count (=) (0, 1 or 2) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
538 |
int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
539 |
int cCnt = eIx - sIx + 1; // Content count including possible separators |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
540 |
int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
541 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
542 |
int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
543 |
byte[] dArr = new byte[len]; // Preallocate byte[] of exact length |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
544 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
545 |
// Decode all but the last 0 - 2 bytes. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
546 |
int d = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
547 |
for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
548 |
// Assemble three bytes into an int from four "valid" characters. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
549 |
int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)]; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
550 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
551 |
// Add the bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
552 |
dArr[d++] = (byte) (i >> 16); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
553 |
dArr[d++] = (byte) (i >> 8); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
554 |
dArr[d++] = (byte) i; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
555 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
556 |
// If line separator, jump over it. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
557 |
if (sepCnt > 0 && ++cc == 19) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
558 |
sIx += 2; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
559 |
cc = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
560 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
561 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
562 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
563 |
if (d < len) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
564 |
// Decode last 1-3 bytes (incl '=') into 1-3 bytes |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
565 |
int i = 0; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
566 |
for (int j = 0; sIx <= eIx - pad; j++) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
567 |
i |= IA[s.charAt(sIx++)] << (18 - j * 6); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
568 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
569 |
for (int r = 16; d < len; r -= 8) |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
570 |
dArr[d++] = (byte) (i >> r); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
571 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
572 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
573 |
return dArr; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
574 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
575 |
} |