package aalg.crypto;

import java.math.BigInteger;
import aalg.algebra.IMonoid;
import aalg.algebra.IElement;
import aalg.algebra.SubGroupDefinition;

/**
 * Diffie-Hellman key exchange protocol.
 * @author Ing. Petr Ležák
 *
 * @param <T> type of monoid elements
 */
public class DHKeyExchange<T extends IElement> {
	
	private final SubGroupDefinition<T> subGroupDefinition;
	
	/**
	 * Creates DH protocol.
	 * @param subGroupDefinition definition of monoid subgroup
	 */
	public DHKeyExchange(final SubGroupDefinition<T> subGroupDefinition) {
		this.subGroupDefinition = subGroupDefinition;
	}
	
	public T calculateSharedKey (final DlPublicKey<T> oppositePublicKey, final DlPrivateKey ownPrivateKey) {
		return subGroupDefinition.getGroup().repeatOperation(oppositePublicKey.getKey(), ownPrivateKey.getKey());
	}

}
