/*
 * Copyright (c) 2009-2010 David Grant
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jscep.transaction;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

import org.jscep.util.HexUtil;


/**
 * This class represents the SCEP <code>transactionID</code> attribute.
 * 
 * @author David Grant
 */
public final class TransactionId {
	private static final AtomicLong ID_SOURCE = new AtomicLong();
	private final byte[] id;
	
	public TransactionId(byte[] id) {
		this.id = id;
	}
	
	private TransactionId(PublicKey pubKey, String digestAlgorithm) {
    	MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance(digestAlgorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        id = HexUtil.toHex(digest.digest(pubKey.getEncoded()));
	}
	
	private TransactionId() {
		id = Long.toHexString(ID_SOURCE.getAndIncrement()).getBytes();
	}
	
	public byte[] getBytes() {
		return id;
	}
	
	@Override
	public boolean equals(Object o) {
		TransactionId transId = (TransactionId) o;
		
		return Arrays.equals(transId.getBytes(), getBytes());
	}
	
	/**
	 * Creates a new Transaction Id
	 * <p>
	 * Each call to this method will return the same transaction ID for the same parameters.
	 * 
	 * @return the new Transaction Id
	 */
	public static TransactionId createTransactionId(PublicKey pubKey, String digestAlgorithm) {
		return new TransactionId(pubKey, digestAlgorithm);
	}
	
	/**
	 * Creates a new Transaction Id
	 * <p>
	 * Each call to this method will return a different transaction ID.
	 * 
	 * @return the new Transaction Id
	 */
	public static TransactionId createTransactionId() {
		return new TransactionId();
	}
	
	@Override
	public String toString() {
		return new String(id);
	}
}

@@ -1752,17 +1752,10 @@
 gest
- = null
 ;%0A
+
     
@@ -2145,163 +2145,9 @@
 %7D%0A%09%0A
-%09@Override%0A%09public boolean equals(Object o) %7B%0A%09%09TransactionId transId = (TransactionId) o;%0A%09%09%0A%09%09return Arrays.equals(transId.getBytes(), getBytes());%0A%09%7D%0A%09
 %0A
+
 %09/**
@@ -2277,25 +2277,103 @@
 ameters.%0A%09 *
- 
+%0A     * @param pubKey public key%0A     * @param digestAlgorithm digest algorithm
 %0A%09 * @return
@@ -2864,13 +2864,404 @@
 ng(id);%0A
-
 %09%7D
+%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) return true;%0A        if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A        TransactionId that = (TransactionId) o;%0A%0A        if (!Arrays.equals(id, that.id)) return false;%0A%0A        return true;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return id != null ? Arrays.hashCode(id) : 0;%0A    %7D
 %0A%7D%0A

Change equals method.