001package com.bitbucket.thinbus.srp6.js;
002
003import static com.nimbusds.srp6.BigIntegerUtils.fromHex;
004import static com.nimbusds.srp6.BigIntegerUtils.toHex;
005
006import java.math.BigInteger;
007import java.security.MessageDigest;
008
009import com.nimbusds.srp6.XRoutine;
010
011public class HexHashedXRoutine implements XRoutine {
012        /**
013         * Computes the password key 'x'.
014         *
015         * @param digest
016         *            The hash function 'H'.
017         * @param salt
018         *            The salt 's'. This is considered a mandatory argument in
019         *            computation of 'x'. Must not be {@code null} or empty.
020         * @param username
021         *            The user identity 'I'. Must not be {@code null} or empty.
022         * @param password
023         *            The user password 'P'. This is considered a mandatory argument
024         *            in the computation of 'x'. Must not be {@code null} or empty.
025         *
026         * @return The resulting 'x' value.
027         */
028        @Override
029        public BigInteger computeX(MessageDigest digest, byte[] salt,
030                        byte[] username, byte[] password) {
031                final String i = new String(username, HexHashedRoutines.utf8);
032                final String p = new String(password, HexHashedRoutines.utf8);
033                final String s = toHex(new BigInteger(1, salt));
034
035                if (i == null || i.trim().isEmpty())
036                        throw new IllegalArgumentException(
037                                        "The user identity 'I' must not be null or empty");
038
039                if (p == null || p.trim().isEmpty())
040                        throw new IllegalArgumentException(
041                                        "The user password 'P' must not be null or empty");
042
043                if (s == null || s.trim().isEmpty())
044                        throw new IllegalArgumentException(
045                                        "The user salt 's' must not be null or empty");
046
047                final String x = HexHashedRoutines.hashCredentials(digest, s, i, p);
048                final BigInteger X = fromHex(x);
049                return X;
050        }
051
052}