1 package com.atlassian.seraph.cookie;
2
3
4
5
6
7
8 public class InsecureCookieEncoder implements CookieEncoder
9 {
10
11
12 private static final char DELIMITER = 0x13;
13
14
15
16 private final static int ENCODE_XORMASK = 0x5A;
17 private final static char ENCODE_CHAR_OFFSET1 = 'C';
18 private final static char ENCODE_CHAR_OFFSET2 = 'i';
19
20 public String encodePasswordCookie(final String username, final String password, final String encoding)
21 {
22 final StringBuffer buf = new StringBuffer();
23 if ((username != null) && (password != null))
24 {
25 final char offset1 = ((encoding != null) && (encoding.length() > 1)) ? encoding.charAt(1) : InsecureCookieEncoder.ENCODE_CHAR_OFFSET1;
26 final char offset2 = ((encoding != null) && (encoding.length() > 2)) ? encoding.charAt(2) : InsecureCookieEncoder.ENCODE_CHAR_OFFSET2;
27
28 final byte[] bytes = (username + InsecureCookieEncoder.DELIMITER + password).getBytes();
29 int b;
30
31 for (int n = 0; n < bytes.length; n++)
32 {
33 b = bytes[n] ^ (InsecureCookieEncoder.ENCODE_XORMASK + n);
34 buf.append((char) (offset1 + (b & 0x0F)));
35 buf.append((char) (offset2 + ((b >> 4) & 0x0F)));
36 }
37 }
38 return buf.toString();
39 }
40
41 public String[] decodePasswordCookie(String cookieVal, final String encoding)
42 {
43
44 if ((cookieVal == null) || (cookieVal.length() <= 0))
45 {
46 return null;
47 }
48
49 final char offset1 = ((encoding != null) && (encoding.length() > 1)) ? encoding.charAt(1) : InsecureCookieEncoder.ENCODE_CHAR_OFFSET1;
50 final char offset2 = ((encoding != null) && (encoding.length() > 2)) ? encoding.charAt(2) : InsecureCookieEncoder.ENCODE_CHAR_OFFSET2;
51
52
53 final char[] chars = cookieVal.toCharArray();
54 final byte[] bytes = new byte[chars.length / 2];
55 int b;
56 for (int n = 0, m = 0; n < bytes.length; n++)
57 {
58 b = chars[m++] - offset1;
59 b |= (chars[m++] - offset2) << 4;
60 bytes[n] = (byte) (b ^ (InsecureCookieEncoder.ENCODE_XORMASK + n));
61 }
62 cookieVal = new String(bytes);
63 final int pos = cookieVal.indexOf(InsecureCookieEncoder.DELIMITER);
64 final String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
65 final String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);
66
67 return new String[] { username, password };
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public String encodePasswordCookie(final String username, final String password)
84 {
85 return encodePasswordCookie(username, password, new String(new char[] { InsecureCookieEncoder.DELIMITER, InsecureCookieEncoder.ENCODE_CHAR_OFFSET1, InsecureCookieEncoder.ENCODE_CHAR_OFFSET2 }));
86 }
87
88
89
90
91
92
93
94
95
96
97 public String[] decodePasswordCookie(final String cookieVal)
98 {
99 return decodePasswordCookie(cookieVal, new String(new char[] { InsecureCookieEncoder.DELIMITER, InsecureCookieEncoder.ENCODE_CHAR_OFFSET1, InsecureCookieEncoder.ENCODE_CHAR_OFFSET2 }));
100 }
101 }