1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
68: public class DSAParameters extends AlgorithmParametersSpi
69: {
70: private BigInteger q;
71: private BigInteger p;
72: private BigInteger g;
73:
74:
75: public void engineInit(AlgorithmParameterSpec paramSpec)
76: throws InvalidParameterSpecException
77: {
78: if( paramSpec instanceof DSAParameterSpec ) {
79: DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec;
80: p = dsaParamSpec.getP();
81: q = dsaParamSpec.getQ();
82: g = dsaParamSpec.getG();
83: }
84: else
85: throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
86: }
87:
88: public void engineInit(byte[] params)
89: throws IOException
90: {
91: DERReader in = new DERReader(params);
92: DERValue val = in.read();
93: if (val.getValue() != DER.CONSTRUCTED_VALUE)
94: throw new ASN1ParsingException("badly formed parameters");
95: try
96: {
97: p = (BigInteger) in.read().getValue();
98: q = (BigInteger) in.read().getValue();
99: g = (BigInteger) in.read().getValue();
100: }
101: catch (Exception x)
102: {
103: throw new ASN1ParsingException("badly formed parameters");
104: }
105: }
106:
107: public void engineInit(byte[] params, String format)
108: throws IOException
109: {
110: if( !format.equals("ASN.1") )
111: throw new IOException("Invalid Format: Only accepts ASN.1");
112: engineInit( params );
113: }
114:
115: public AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
116: throws InvalidParameterSpecException
117: {
118: if( paramSpec.isAssignableFrom(DSAParameterSpec.class) )
119: return new DSAParameterSpec(p, q, g);
120: else
121: throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
122: }
123:
124: public byte[] engineGetEncoded()
125: throws IOException
126: {
127: ByteArrayOutputStream bout = new ByteArrayOutputStream();
128: ArrayList seq = new ArrayList(3);
129: seq.add(new DERValue(DER.INTEGER, p));
130: seq.add(new DERValue(DER.INTEGER, q));
131: seq.add(new DERValue(DER.INTEGER, g));
132: DERWriter.write(bout, new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, seq));
133: return bout.toByteArray();
134: }
135:
136:
137: public byte[] engineGetEncoded(String format)
138: throws IOException
139: {
140: if( !format.equals("ASN.1") )
141: throw new IOException("Invalid Format: Only accepts ASN.1");
142: return engineGetEncoded();
143: }
144:
145: public String engineToString()
146: {
147: return ("q: " + q + " p: " + p + " g: " + g);
148: }
149:
150: }