src/com/isode/stroke/stringcodecs/HMACSHA1.java
author Da Risk <da_risk@beem-project.com>
Sun, 15 Mar 2015 17:28:04 +0100
changeset 1039 7d6f2526244a
parent 1018 8daca77fabc1
permissions -rw-r--r--
Use latest gradle
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
/*
1018
8daca77fabc1 fix Remko's currly part of name
Jiří Pinkava <j-pi@seznam.cz>
parents: 1015
diff changeset
     6
 * Copyright (c) 2010, Remko Tronçon.
1015
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
}