package com.jscape.inet.ssh.protocol.v2.marshaling.keys;

import com.jscape.inet.ssh.protocol.v2.messages.PublicKeyAlgorithm;
import com.jscape.util.crypto.CryptoServicesProvider;

import java.security.Provider;

/**
 * @author Alex Usun
 */
public class PublicKeyCodecs {

    public static PublicKeyCodec init(PublicKeyCodec codec) {
        return init(codec, CryptoServicesProvider.instance());
    }

    public static PublicKeyCodec init(PublicKeyCodec codec, String provider) {
        for (PublicKeyAlgorithm value : PublicKeyAlgorithm.values()) {
            switch (value.keyAlgorithm) {
                case "RSA":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, RsaPublicKeyCodec.codecFor(provider)));
                    break;
                case "DSA":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, DsaPublicKeyCodec.codecFor(provider)));
                    break;
                case "EC":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, EcPublicKeyCodec.codecFor(provider)));
                    break;
                default:
                    break;
            }
        }

        return codec;
    }

    public static PublicKeyCodec init(PublicKeyCodec codec, Provider provider) {
        for (PublicKeyAlgorithm value : PublicKeyAlgorithm.values()) {
            switch (value.keyAlgorithm) {
                case "RSA":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, RsaPublicKeyCodec.codecFor(provider)));
                    break;
                case "DSA":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, DsaPublicKeyCodec.codecFor(provider)));
                    break;
                case "EC":
                    codec.set(new PublicKeyCodec.Entry(value.identifier, EcPublicKeyCodec.codecFor(provider)));
                    break;
                default:
                    break;
            }
        }

        return codec;
    }

    private PublicKeyCodecs() {
        // empty
    }
}
