Skip to content

Commit 8385a2c

Browse files
author
gefeili
committed
Refactor on IssuerKeyID, bcpg.sig.Utils. Add comments to PGPKeyEncryptionMethodGenerator, make iv in SecretKeyPacket is copied when processing. Refactor on SExpression.
1 parent e6b4ba8 commit 8385a2c

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

pg/src/main/java/org/bouncycastle/bcpg/SecretKeyPacket.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55

6+
import org.bouncycastle.util.Arrays;
67
import org.bouncycastle.util.io.Streams;
78

89
/**
@@ -208,7 +209,7 @@ public SecretKeyPacket(
208209
this.aeadAlgorithm = aeadAlgorithm;
209210
this.s2kUsage = s2kUsage;
210211
this.s2k = s2k;
211-
this.iv = iv;
212+
this.iv = Arrays.clone(iv);
212213
this.secKeyData = secKeyData;
213214

214215
if (s2k != null && s2k.getType() == S2K.ARGON_2 && s2kUsage != USAGE_AEAD)
@@ -242,7 +243,7 @@ public int getS2KUsage()
242243

243244
public byte[] getIV()
244245
{
245-
return iv;
246+
return Arrays.clone(iv);
246247
}
247248

248249
public S2K getS2K()

pg/src/main/java/org/bouncycastle/bcpg/sig/IssuerKeyID.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bouncycastle.bcpg.SignatureSubpacket;
44
import org.bouncycastle.bcpg.SignatureSubpacketTags;
5+
import org.bouncycastle.util.Pack;
56

67
/**
78
* packet giving the issuer key ID.
@@ -12,18 +13,7 @@ public class IssuerKeyID
1213
protected static byte[] keyIDToBytes(
1314
long keyId)
1415
{
15-
byte[] data = new byte[8];
16-
17-
data[0] = (byte)(keyId >> 56);
18-
data[1] = (byte)(keyId >> 48);
19-
data[2] = (byte)(keyId >> 40);
20-
data[3] = (byte)(keyId >> 32);
21-
data[4] = (byte)(keyId >> 24);
22-
data[5] = (byte)(keyId >> 16);
23-
data[6] = (byte)(keyId >> 8);
24-
data[7] = (byte)keyId;
25-
26-
return data;
16+
return Pack.longToBigEndian(keyId);
2717
}
2818

2919
public IssuerKeyID(
@@ -43,9 +33,6 @@ public IssuerKeyID(
4333

4434
public long getKeyID()
4535
{
46-
long keyID = ((long)(data[0] & 0xff) << 56) | ((long)(data[1] & 0xff) << 48) | ((long)(data[2] & 0xff) << 40) | ((long)(data[3] & 0xff) << 32)
47-
| ((long)(data[4] & 0xff) << 24) | ((data[5] & 0xff) << 16) | ((data[6] & 0xff) << 8) | (data[7] & 0xff);
48-
49-
return keyID;
36+
return Pack.bigEndianToLong(data, 0);
5037
}
5138
}

pg/src/main/java/org/bouncycastle/bcpg/sig/Utils.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.bcpg.sig;
22

3+
import org.bouncycastle.util.Pack;
4+
35
class Utils
46
{
57
/**
@@ -55,19 +57,11 @@ static long timeFromBytes(byte[] bytes)
5557
throw new IllegalStateException("Byte array has unexpected length. Expected length 4, got " + bytes.length);
5658
}
5759

58-
return ((long)(bytes[0] & 0xff) << 24)
59-
| ((bytes[1] & 0xff) << 16)
60-
| ((bytes[2] & 0xff) << 8)
61-
| (bytes[3] & 0xff);
60+
return Pack.bigEndianToInt(bytes, 0);
6261
}
6362

6463
static byte[] timeToBytes(long t)
6564
{
66-
byte[] data = new byte[4];
67-
data[0] = (byte)(t >> 24);
68-
data[1] = (byte)(t >> 16);
69-
data[2] = (byte)(t >> 8);
70-
data[3] = (byte)t;
71-
return data;
65+
return Pack.intToBigEndian((int)t);
7266
}
7367
}

pg/src/main/java/org/bouncycastle/gpg/SExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private static SExpression parseExpression(InputStream src, SExpression expr, By
165165
else if (c == '#')
166166
{
167167
consumeUntilSkipWhiteSpace(src, accumulator, '#');
168-
expr.addValue(Hex.decode(Strings.fromByteArray(accumulator.toByteArray())));
168+
expr.addValue(Hex.decode(accumulator.toByteArray()));
169169
}
170170
else if (c == '"')
171171
{

pg/src/main/java/org/bouncycastle/openpgp/operator/PGPKeyEncryptionMethodGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.openpgp.operator;
22

33
import org.bouncycastle.bcpg.ContainedPacket;
4+
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
45
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
56
import org.bouncycastle.openpgp.PGPException;
67

@@ -9,6 +10,14 @@
910
*/
1011
public abstract class PGPKeyEncryptionMethodGenerator
1112
{
13+
/**
14+
* Generates a packet encoding the details of this encryption method.
15+
*
16+
* @param encAlgorithm the {@link SymmetricKeyAlgorithmTags encryption algorithm} being used
17+
* @param sessionInfo session data generated by the encrypted data generator.
18+
* @return a packet encoding the provided information and the configuration of this instance.
19+
* @throws PGPException if an error occurs constructing the packet.
20+
*/
1221
public abstract ContainedPacket generate(int encAlgorithm, byte[] sessionInfo)
1322
throws PGPException;
1423

0 commit comments

Comments
 (0)