Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerDHParams.java « tls « crypto « spongycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa22e629dfd8fd4542bc1792fafa9596fbf627ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.spongycastle.crypto.tls;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;

import org.spongycastle.crypto.params.DHParameters;
import org.spongycastle.crypto.params.DHPublicKeyParameters;

public class ServerDHParams
{
    protected DHPublicKeyParameters publicKey;

    public ServerDHParams(DHPublicKeyParameters publicKey)
    {
        if (publicKey == null)
        {
            throw new IllegalArgumentException("'publicKey' cannot be null");
        }

        this.publicKey = publicKey;
    }

    public DHPublicKeyParameters getPublicKey()
    {
        return publicKey;
    }

    /**
     * Encode this {@link ServerDHParams} to an {@link OutputStream}.
     * 
     * @param output
     *            the {@link OutputStream} to encode to.
     * @throws IOException
     */
    public void encode(OutputStream output) throws IOException
    {
        DHParameters dhParameters = publicKey.getParameters();
        BigInteger Ys = publicKey.getY();

        TlsDHUtils.writeDHParameter(dhParameters.getP(), output);
        TlsDHUtils.writeDHParameter(dhParameters.getG(), output);
        TlsDHUtils.writeDHParameter(Ys, output);
    }

    /**
     * Parse a {@link ServerDHParams} from an {@link InputStream}.
     * 
     * @param input
     *            the {@link InputStream} to parse from.
     * @return a {@link ServerDHParams} object.
     * @throws IOException
     */
    public static ServerDHParams parse(InputStream input) throws IOException
    {
        BigInteger p = TlsDHUtils.readDHParameter(input);
        BigInteger g = TlsDHUtils.readDHParameter(input);
        BigInteger Ys = TlsDHUtils.readDHParameter(input);

        return new ServerDHParams(new DHPublicKeyParameters(Ys, new DHParameters(p, g)));
    }
}