Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static byte[] wa_pbkdf2(byte[] password, byte[] salt)
            throws InvalidKeySpecException, NoSuchAlgorithmException {
        /*
         * 2 iterations, 20 keylength
         */

        byte[] last = new byte[salt.length + 4];
        System.arraycopy(salt, 0, last, 0, salt.length);
        last[last.length - 1] = 1;
        byte[] xorsum = hmacSha1(last, password);
        last = xorsum.clone();

        for (int i = 1; i < 2; i++) {
            last = hmacSha1(last, password);

            for (int j = 0; j < xorsum.length; j++)
                xorsum[j] = (byte) ((xorsum[j] & 0xFF) ^ (last[j] & 0xFF));
        }

        byte[] ret = new byte[20];
        System.arraycopy(xorsum, 0, ret, 0, 20);
        return ret;
    }

    private static byte[] hmacSha1(byte[] value, byte[] key) {
        try {
            SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
            return mac.doFinal(value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}