Skip to content

Commit 601ed4f

Browse files
committed
Add ASN1Integer valueOf methods and small constants
1 parent 9fedfe5 commit 601ed4f

175 files changed

Lines changed: 414 additions & 390 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/org/bouncycastle/asn1/ASN1Integer.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ ASN1Primitive fromImplicitPrimitive(DEROctetString octetString)
2020
}
2121
};
2222

23+
private static final ASN1Integer[] SMALL_CONSTANTS = new ASN1Integer[17];
24+
25+
public static final ASN1Integer ZERO;
26+
public static final ASN1Integer ONE;
27+
public static final ASN1Integer TWO;
28+
public static final ASN1Integer THREE;
29+
public static final ASN1Integer FOUR;
30+
public static final ASN1Integer FIVE;
31+
2332
static final int SIGN_EXT_SIGNED = 0xFFFFFFFF;
2433
static final int SIGN_EXT_UNSIGNED = 0xFF;
2534

@@ -76,6 +85,48 @@ public static ASN1Integer getTagged(ASN1TaggedObject taggedObject, boolean decla
7685
return (ASN1Integer)TYPE.getTagged(taggedObject, declaredExplicit);
7786
}
7887

88+
public static ASN1Integer valueOf(int value)
89+
{
90+
if (value >= 0L && value < SMALL_CONSTANTS.length)
91+
return SMALL_CONSTANTS[value];
92+
93+
return new ASN1Integer(value);
94+
}
95+
96+
public static ASN1Integer valueOf(long value)
97+
{
98+
if (value >= 0L && value < SMALL_CONSTANTS.length)
99+
return SMALL_CONSTANTS[(int)value];
100+
101+
return new ASN1Integer(value);
102+
}
103+
104+
static
105+
{
106+
for (int i = 0; i < SMALL_CONSTANTS.length; ++i)
107+
{
108+
SMALL_CONSTANTS[i] = new ASN1Integer(i);
109+
}
110+
111+
ZERO = SMALL_CONSTANTS[0];
112+
ONE = SMALL_CONSTANTS[1];
113+
TWO = SMALL_CONSTANTS[2];
114+
THREE = SMALL_CONSTANTS[3];
115+
FOUR = SMALL_CONSTANTS[4];
116+
FIVE = SMALL_CONSTANTS[5];
117+
}
118+
119+
/**
120+
* Construct an INTEGER from the passed in int value.
121+
*
122+
* @param value the int representing the value desired.
123+
*/
124+
public ASN1Integer(int value)
125+
{
126+
this.bytes = BigInteger.valueOf(value).toByteArray();
127+
this.start = 0;
128+
}
129+
79130
/**
80131
* Construct an INTEGER from the passed in long value.
81132
*

core/src/main/java/org/bouncycastle/asn1/cryptopro/ECGOST3410ParamSetParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ECGOST3410ParamSetParameters(
5151
this.b = new ASN1Integer(b);
5252
this.p = new ASN1Integer(p);
5353
this.q = new ASN1Integer(q);
54-
this.x = new ASN1Integer(x);
54+
this.x = ASN1Integer.valueOf(x);
5555
this.y = new ASN1Integer(y);
5656
}
5757

core/src/main/java/org/bouncycastle/asn1/cryptopro/GOST3410ParamSetParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public ASN1Primitive toASN1Primitive()
9595
{
9696
ASN1EncodableVector v = new ASN1EncodableVector(4);
9797

98-
v.add(new ASN1Integer(keySize));
98+
v.add(ASN1Integer.valueOf(keySize));
9999
v.add(p);
100100
v.add(q);
101101
v.add(a);

core/src/main/java/org/bouncycastle/asn1/nist/KMACwithSHAKE128_params.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public ASN1Primitive toASN1Primitive()
101101

102102
if (outputLength != DEF_LENGTH)
103103
{
104-
v.add(new ASN1Integer(outputLength));
104+
v.add(ASN1Integer.valueOf(outputLength));
105105
}
106106

107107
if (customizationString.length != 0)

core/src/main/java/org/bouncycastle/asn1/nist/KMACwithSHAKE256_params.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public ASN1Primitive toASN1Primitive()
101101

102102
if (outputLength != DEF_LENGTH)
103103
{
104-
v.add(new ASN1Integer(outputLength));
104+
v.add(ASN1Integer.valueOf(outputLength));
105105
}
106106

107107
if (customizationString.length != 0)

core/src/main/java/org/bouncycastle/asn1/ocsp/ResponseData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
public class ResponseData
2727
extends ASN1Object
2828
{
29-
private static final ASN1Integer V1 = new ASN1Integer(0);
30-
29+
private static final ASN1Integer V1 = ASN1Integer.ZERO;
30+
3131
private boolean versionPresent;
3232

3333
private ASN1Integer version;

core/src/main/java/org/bouncycastle/asn1/ocsp/TBSRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class TBSRequest
1616
extends ASN1Object
1717
{
18-
private static final ASN1Integer V1 = new ASN1Integer(0);
18+
private static final ASN1Integer V1 = ASN1Integer.ZERO;
1919

2020
ASN1Integer version;
2121
GeneralName requestorName;

core/src/main/java/org/bouncycastle/asn1/pkcs/CertificationRequestInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class CertificationRequestInfo
3737
extends ASN1Object
3838
{
39-
ASN1Integer version = new ASN1Integer(0);
39+
ASN1Integer version = ASN1Integer.ZERO;
4040
X500Name subject;
4141
SubjectPublicKeyInfo subjectPKInfo;
4242
ASN1Set attributes = null;

core/src/main/java/org/bouncycastle/asn1/pkcs/DHParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public DHParameter(
2525

2626
if (l != 0)
2727
{
28-
this.l = new ASN1Integer(l);
28+
this.l = ASN1Integer.valueOf(l);
2929
}
3030
else
3131
{

core/src/main/java/org/bouncycastle/asn1/pkcs/EncryptedData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ public ASN1OctetString getContent()
102102

103103
public ASN1Primitive toASN1Primitive()
104104
{
105-
return new BERSequence(new ASN1Integer(0), data);
105+
return new BERSequence(ASN1Integer.ZERO, data);
106106
}
107107
}

0 commit comments

Comments
 (0)