11 "github.com/bytom/vapor/crypto/ed25519"
14 const format = "Private-key-format: v1.3\n"
16 // PrivateKeyString converts a PrivateKey to a string. This string has the same
17 // format as the private-key-file of BIND9 (Private-key-format: v1.3).
18 // It needs some info from the key (the algorithm), so its a method of the DNSKEY
19 // It supports rsa.PrivateKey, ecdsa.PrivateKey and dsa.PrivateKey
20 func (r *DNSKEY) PrivateKeyString(p crypto.PrivateKey) string {
21 algorithm := strconv.Itoa(int(r.Algorithm))
22 algorithm += " (" + AlgorithmToString[r.Algorithm] + ")"
24 switch p := p.(type) {
26 modulus := toBase64(p.PublicKey.N.Bytes())
27 e := big.NewInt(int64(p.PublicKey.E))
28 publicExponent := toBase64(e.Bytes())
29 privateExponent := toBase64(p.D.Bytes())
30 prime1 := toBase64(p.Primes[0].Bytes())
31 prime2 := toBase64(p.Primes[1].Bytes())
32 // Calculate Exponent1/2 and Coefficient as per: http://en.wikipedia.org/wiki/RSA#Using_the_Chinese_remainder_algorithm
33 // and from: http://code.google.com/p/go/issues/detail?id=987
35 p1 := big.NewInt(0).Sub(p.Primes[0], one)
36 q1 := big.NewInt(0).Sub(p.Primes[1], one)
37 exp1 := big.NewInt(0).Mod(p.D, p1)
38 exp2 := big.NewInt(0).Mod(p.D, q1)
39 coeff := big.NewInt(0).ModInverse(p.Primes[1], p.Primes[0])
41 exponent1 := toBase64(exp1.Bytes())
42 exponent2 := toBase64(exp2.Bytes())
43 coefficient := toBase64(coeff.Bytes())
46 "Algorithm: " + algorithm + "\n" +
47 "Modulus: " + modulus + "\n" +
48 "PublicExponent: " + publicExponent + "\n" +
49 "PrivateExponent: " + privateExponent + "\n" +
50 "Prime1: " + prime1 + "\n" +
51 "Prime2: " + prime2 + "\n" +
52 "Exponent1: " + exponent1 + "\n" +
53 "Exponent2: " + exponent2 + "\n" +
54 "Coefficient: " + coefficient + "\n"
56 case *ecdsa.PrivateKey:
64 private := toBase64(intToBytes(p.D, intlen))
66 "Algorithm: " + algorithm + "\n" +
67 "PrivateKey: " + private + "\n"
70 T := divRoundUp(divRoundUp(p.PublicKey.Parameters.G.BitLen(), 8)-64, 8)
71 prime := toBase64(intToBytes(p.PublicKey.Parameters.P, 64+T*8))
72 subprime := toBase64(intToBytes(p.PublicKey.Parameters.Q, 20))
73 base := toBase64(intToBytes(p.PublicKey.Parameters.G, 64+T*8))
74 priv := toBase64(intToBytes(p.X, 20))
75 pub := toBase64(intToBytes(p.PublicKey.Y, 64+T*8))
77 "Algorithm: " + algorithm + "\n" +
78 "Prime(p): " + prime + "\n" +
79 "Subprime(q): " + subprime + "\n" +
80 "Base(g): " + base + "\n" +
81 "Private_value(x): " + priv + "\n" +
82 "Public_value(y): " + pub + "\n"
84 case ed25519.PrivateKey:
85 private := toBase64(p.Seed())
87 "Algorithm: " + algorithm + "\n" +
88 "PrivateKey: " + private + "\n"