author | Da Risk <da_risk@beem-project.com> |
Sun, 13 Jan 2013 20:11:53 +0100 | |
changeset 1015 | 63669480c941 |
child 1018 | 8daca77fabc1 |
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 |
/* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, Isode Limited, London, England. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
3 |
* All rights reserved. |
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 |
/* |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
6 |
* Copyright (c) 2010, Remko Tron�on. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
7 |
* All rights reserved. |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
8 |
*/ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
9 |
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
|
10 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
11 |
import com.isode.stroke.base.ByteArray; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
12 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
13 |
public class HMACSHA1 { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
14 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
15 |
private static final int B = 64; |
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 |
public static ByteArray getResult(ByteArray key, ByteArray data) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
18 |
assert key.getSize() <= B; |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
19 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
20 |
/* And an assert that does something */ |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
21 |
if (key.getSize() > B) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
22 |
throw new IllegalStateException("Invalid key size."); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
23 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
24 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
25 |
// Create the padded key |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
26 |
ByteArray paddedKey = new ByteArray(key); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
27 |
for (int i = key.getSize(); i < B; ++i) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
28 |
paddedKey.append((byte) 0x0); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
29 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
30 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
31 |
// Create the first value |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
32 |
ByteArray x = new ByteArray(paddedKey); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
33 |
byte[] xInner = x.getData(); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
34 |
for (int i = 0; i < xInner.length; ++i) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
35 |
xInner[i] ^= 0x36; |
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 |
x.append(data); |
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 |
// Create the second value |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
40 |
ByteArray y = new ByteArray(paddedKey); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
41 |
byte[] yInner = y.getData(); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
42 |
for (int i = 0; i < yInner.length; ++i) { |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
43 |
yInner[i] ^= 0x5c; |
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 |
y.append(SHA1.getHash(x)); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
46 |
|
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
47 |
return SHA1.getHash(y); |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
48 |
} |
63669480c941
Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff
changeset
|
49 |
} |