src/com/isode/stroke/stringcodecs/PBKDF2.java
author Da Risk <da_risk@beem-project.com>
Sun, 15 Mar 2015 17:28:04 +0100
changeset 1043 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 PBKDF2 {
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
    public static ByteArray encode(ByteArray password, ByteArray salt, int iterations) {
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    16
        ByteArray u = HMACSHA1.getResult(password, ByteArray.plus(salt, new ByteArray("\0\0\0\1")));
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    17
        ByteArray result = new ByteArray(u);
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    18
        byte[] resultData = result.getData();
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    19
        int i = 1;
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    20
        while (i < iterations) {
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    21
            u = HMACSHA1.getResult(password, u);
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    22
            for (int j = 0; j < u.getSize(); ++j) {
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    23
                resultData[j] ^= u.getData()[j];
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
            ++i;
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    26
        }
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    27
        return result;
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    28
    }
63669480c941 Add an implementation of the SCRAM-SHA-! SASL mechanism.
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    29
}