Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit bf0bccb

Browse files
committed
Merge pull request #2 from sochotnicky/master
Fix salt initialization and randomness of PBECipher Thanks
2 parents c3e9403 + 6ab0e38 commit bf0bccb

File tree

1 file changed

+5
-38
lines changed

1 file changed

+5
-38
lines changed

src/main/java/org/sonatype/plexus/components/cipher/PBECipher.java

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,15 @@ public class PBECipher
6363

6464
protected MessageDigest _digester;
6565

66-
protected SecureRandom _secureRandom;
67-
68-
protected boolean _onLinux = false;
66+
private static final SecureRandom _secureRandom = new SecureRandom();
67+
6968
//---------------------------------------------------------------
7069
public PBECipher()
7170
throws PlexusCipherException
7271
{
7372
try
7473
{
7574
_digester = MessageDigest.getInstance( DIGEST_ALG );
76-
77-
if( System.getProperty( "os.name", "blah" ).toLowerCase().indexOf( "linux" ) != -1 )
78-
{
79-
_onLinux = true;
80-
}
81-
82-
if( _onLinux )
83-
{
84-
System.setProperty( "securerandom.source", "file:/dev/./urandom");
85-
}
86-
else
87-
{
88-
_secureRandom = new SecureRandom();
89-
}
90-
9175
}
9276
catch ( NoSuchAlgorithmException e )
9377
{
@@ -96,21 +80,10 @@ public PBECipher()
9680
}
9781
//---------------------------------------------------------------
9882
private byte[] getSalt( final int sz )
99-
throws NoSuchAlgorithmException, NoSuchProviderException
10083
{
101-
byte [] res = null;
102-
103-
if( _secureRandom != null )
104-
{
105-
_secureRandom.setSeed( System.currentTimeMillis() );
106-
res = _secureRandom.generateSeed( sz );
107-
}
108-
else
109-
{
110-
res = new byte[ sz ];
111-
Random r = new Random( System.currentTimeMillis() );
112-
r.nextBytes( res );
113-
}
84+
byte[] res = new byte[ sz ];
85+
86+
_secureRandom.nextBytes( res );
11487

11588
return res;
11689
}
@@ -124,12 +97,6 @@ public String encrypt64( final String clearText, final String password )
12497

12598
byte[] salt = getSalt( SALT_SIZE );
12699

127-
// spin it :)
128-
if( _secureRandom != null )
129-
{
130-
new SecureRandom().nextBytes( salt );
131-
}
132-
133100
Cipher cipher = createCipher( password.getBytes( STRING_ENCODING ), salt, Cipher.ENCRYPT_MODE );
134101

135102
byte [] encryptedBytes = cipher.doFinal( clearBytes );

0 commit comments

Comments
 (0)