Skip to content

Commit f361855

Browse files
committed
Added method to set signature size
1 parent f75c436 commit f361855

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/main/java/org/redline_rpm/Builder.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ public class Builder {
8989
protected final Entry< byte[]> immutable = ( Entry< byte[]>) format.getHeader().addEntry( HEADERIMMUTABLE, 16);
9090

9191
protected Contents contents = new Contents();
92-
protected File privateKeyRingFile;
93-
protected String privateKeyId;
94-
protected String privateKeyPassphrase;
95-
protected PGPPrivateKey privateKey;
92+
protected File privateKeyRingFile;
93+
protected String privateKeyId;
94+
protected String privateKeyPassphrase;
95+
protected PGPPrivateKey privateKey;
96+
protected int signatureSize = 287;
9697

9798
/**
9899
* Initializes the builder and sets some required fields to known values.
@@ -1220,6 +1221,15 @@ public void setPrivateKeyPassphrase( String privateKeyPassphrase ) {
12201221
public void setPrivateKey( PGPPrivateKey privateKey ) {
12211222
this.privateKey = privateKey;
12221223
}
1224+
1225+
/**
1226+
* Sets the signature size for generating keys with different RSA headers.
1227+
* This should only be used if the exact size of the header is known.
1228+
* @param signatureSize the signature size
1229+
*/
1230+
public void setSignatureSize( int signatureSize ) {
1231+
this.signatureSize = signatureSize;
1232+
}
12231233

12241234
/**
12251235
* Generates an RPM with a standard name consisting of the RPM package name, version, release,
@@ -1398,9 +1408,9 @@ public void build( final FileChannel original) throws NoSuchAlgorithmException,
13981408

13991409
protected SignatureGenerator createSignatureGenerator() {
14001410
if (privateKey != null) {
1401-
return new SignatureGenerator( privateKey );
1411+
return new SignatureGenerator( privateKey, signatureSize );
14021412
}
1403-
return new SignatureGenerator( privateKeyRingFile, privateKeyId, privateKeyPassphrase);
1413+
return new SignatureGenerator( privateKeyRingFile, privateKeyId, privateKeyPassphrase, signatureSize );
14041414
}
14051415

14061416
protected byte[] getSignature( final int count) {

src/main/java/org/redline_rpm/SignatureGenerator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
public class SignatureGenerator {
3232

33-
protected static final int SIGNATURE_SIZE = 287;
33+
protected int signatureSize;
3434
protected final boolean enabled;
3535
protected Entry< byte[]> headerOnlyRSAEntry;
3636
protected Entry< byte[]> headerAndPayloadPGPEntry;
@@ -39,12 +39,14 @@ public class SignatureGenerator {
3939
protected Key< byte[]> headerAndPayloadKey = null;
4040
private Logger logger = getLogger( SignatureGenerator.class.getName());
4141

42-
public SignatureGenerator( PGPPrivateKey privateKey ) {
42+
public SignatureGenerator( PGPPrivateKey privateKey, int signatureSize ) {
4343
this.privateKey = privateKey;
4444
this.enabled = privateKey != null;
45+
this.signatureSize = signatureSize > 0 ? signatureSize : 287;
4546
}
4647

47-
public SignatureGenerator( File privateKeyRingFile, String privateKeyId, String privateKeyPassphrase ) {
48+
public SignatureGenerator( File privateKeyRingFile, String privateKeyId, String privateKeyPassphrase, int signatureSize ) {
49+
this.signatureSize = signatureSize > 0 ? signatureSize : 287;
4850
if ( privateKeyRingFile != null ) {
4951
PGPSecretKeyRingCollection keyRings = readKeyRings( privateKeyRingFile );
5052
PGPSecretKey secretKey = findMatchingSecretKey( keyRings, privateKeyId );
@@ -63,8 +65,8 @@ public SignatureGenerator( File privateKeyRingFile, String privateKeyId, String
6365
@SuppressWarnings("unchecked")
6466
public void prepare( Signature signature ) {
6567
if ( enabled ) {
66-
headerOnlyRSAEntry = ( Entry< byte[]> ) signature.addEntry( RSAHEADER, SIGNATURE_SIZE );
67-
headerAndPayloadPGPEntry = ( Entry< byte[]> ) signature.addEntry( LEGACY_PGP, SIGNATURE_SIZE );
68+
headerOnlyRSAEntry = ( Entry< byte[]> ) signature.addEntry( RSAHEADER, signatureSize );
69+
headerAndPayloadPGPEntry = ( Entry< byte[]> ) signature.addEntry( LEGACY_PGP, signatureSize );
6870
}
6971
}
7072

src/test/java/org/redline_rpm/SignatureGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public class SignatureGeneratorTest extends TestBase {
1010

1111
@Test
1212
public void testReadingFirstKey() throws Exception {
13-
SignatureGenerator generator = new SignatureGenerator( new File( getFileResource( "/pgp/secring.gpg" ) ), null, "redline" );
13+
SignatureGenerator generator = new SignatureGenerator( new File( getFileResource( "/pgp/secring.gpg" ) ), null, "redline", 287 );
1414
assertTrue( generator.isEnabled() );
1515
}
1616

1717
@Test
1818
public void testFindByKey() throws Exception {
19-
SignatureGenerator generator = new SignatureGenerator( new File( getFileResource( "/pgp/secring.gpg" )), "5A186608", "redline" );
19+
SignatureGenerator generator = new SignatureGenerator( new File( getFileResource( "/pgp/secring.gpg" )), "5A186608", "redline", 287 );
2020
assertTrue( generator.isEnabled() );
2121
}
2222
}

0 commit comments

Comments
 (0)