Skip to content

Commit bb05c6c

Browse files
committed
Use utility method
1 parent 0e15a02 commit bb05c6c

File tree

1 file changed

+3
-23
lines changed

1 file changed

+3
-23
lines changed

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/dh/KeyAgreementSpi.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.bouncycastle.jcajce.spec.DHUParameterSpec;
3838
import org.bouncycastle.jcajce.spec.MQVParameterSpec;
3939
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
40+
import org.bouncycastle.util.BigIntegers;
4041

4142
/**
4243
* Diffie-Hellman key agreement. There's actually a better way of doing this
@@ -104,30 +105,9 @@ protected byte[] bigIntToBytes(
104105
//
105106
int expectedLength = (p.bitLength() + 7) / 8;
106107

107-
byte[] tmp = r.toByteArray();
108-
109-
if (tmp.length == expectedLength)
110-
{
111-
return tmp;
112-
}
113-
114-
if (tmp[0] == 0 && tmp.length == expectedLength + 1)
115-
{
116-
byte[] rv = new byte[tmp.length - 1];
117-
118-
System.arraycopy(tmp, 1, rv, 0, rv.length);
119-
return rv;
120-
}
121-
122-
// tmp must be shorter than expectedLength
123-
// pad to the left with zeros.
124-
byte[] rv = new byte[expectedLength];
125-
126-
System.arraycopy(tmp, 0, rv, rv.length - tmp.length, tmp.length);
127-
128-
return rv;
108+
return BigIntegers.asUnsignedByteArray(expectedLength, r);
129109
}
130-
110+
131111
protected Key engineDoPhase(
132112
Key key,
133113
boolean lastPhase)

0 commit comments

Comments
 (0)