package dns

import (
	
	
	
	
	
	
)

const format = "Private-key-format: v1.3\n"

var bigIntOne = big.NewInt(1)

// PrivateKeyString converts a PrivateKey to a string. This string has the same
// format as the private-key-file of BIND9 (Private-key-format: v1.3).
// It needs some info from the key (the algorithm), so its a method of the DNSKEY.
// It supports *rsa.PrivateKey, *ecdsa.PrivateKey and ed25519.PrivateKey.
func ( *DNSKEY) ( crypto.PrivateKey) string {
	 := strconv.Itoa(int(.Algorithm))
	 += " (" + AlgorithmToString[.Algorithm] + ")"

	switch p := .(type) {
	case *rsa.PrivateKey:
		 := toBase64(.PublicKey.N.Bytes())
		 := big.NewInt(int64(.PublicKey.E))
		 := toBase64(.Bytes())
		 := toBase64(.D.Bytes())
		 := toBase64(.Primes[0].Bytes())
		 := toBase64(.Primes[1].Bytes())
		// Calculate Exponent1/2 and Coefficient as per: http://en.wikipedia.org/wiki/RSA#Using_the_Chinese_remainder_algorithm
		// and from: http://code.google.com/p/go/issues/detail?id=987
		 := new(big.Int).Sub(.Primes[0], bigIntOne)
		 := new(big.Int).Sub(.Primes[1], bigIntOne)
		 := new(big.Int).Mod(.D, )
		 := new(big.Int).Mod(.D, )
		 := new(big.Int).ModInverse(.Primes[1], .Primes[0])

		 := toBase64(.Bytes())
		 := toBase64(.Bytes())
		 := toBase64(.Bytes())

		return format +
			"Algorithm: " +  + "\n" +
			"Modulus: " +  + "\n" +
			"PublicExponent: " +  + "\n" +
			"PrivateExponent: " +  + "\n" +
			"Prime1: " +  + "\n" +
			"Prime2: " +  + "\n" +
			"Exponent1: " +  + "\n" +
			"Exponent2: " +  + "\n" +
			"Coefficient: " +  + "\n"

	case *ecdsa.PrivateKey:
		var  int
		switch .Algorithm {
		case ECDSAP256SHA256:
			 = 32
		case ECDSAP384SHA384:
			 = 48
		}
		 := toBase64(intToBytes(.D, ))
		return format +
			"Algorithm: " +  + "\n" +
			"PrivateKey: " +  + "\n"

	case ed25519.PrivateKey:
		 := toBase64(.Seed())
		return format +
			"Algorithm: " +  + "\n" +
			"PrivateKey: " +  + "\n"

	default:
		return ""
	}
}