diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/util/Util.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/util/Util.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java index ffd3106..a524953 100644 --- a/src/main/java/org/openslx/imagemaster/util/Util.java +++ b/src/main/java/org/openslx/imagemaster/util/Util.java @@ -1,6 +1,11 @@ package org.openslx.imagemaster.util; import java.io.File; +import java.math.BigInteger; +import java.security.Key; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.util.Arrays; import java.util.Random; import org.apache.log4j.Logger; @@ -120,6 +125,21 @@ public class Util } } + /** + * Tries to parse a bigint. Returns null on error. + * + * @param s The strig to parse + * @return The parsed bigint + */ + public static BigInteger tryToParseBigInt( String s ) + { + try { + return new BigInteger( s ); + } catch ( NumberFormatException e ) { + return null; + } + } + public static int getNumberOfBlocks( long fileSize, int blockSize ) { int blocks = (int) ( fileSize / blockSize ); @@ -136,4 +156,32 @@ public class Util return fileName; } + /** + * Checks whether the two given keys are equal. Works for + * public and private keys. + * + * @param k1 first key + * @param k2 second key + * @return true if equal + */ + public static boolean keysEqual( Key k1, Key k2 ) + { + if ( k1 instanceof RSAPublicKey && k2 instanceof RSAPublicKey ) + { + RSAPublicKey rsa1 = (RSAPublicKey)k1; + RSAPublicKey rsa2 = (RSAPublicKey)k2; + return rsa1.getModulus().equals( rsa2.getModulus() ) + && rsa1.getPublicExponent().equals( rsa2.getPublicExponent() ); + } + + if ( k1 instanceof RSAPrivateKey && k2 instanceof RSAPrivateKey ) + { + RSAPrivateKey rsa1 = (RSAPrivateKey)k1; + RSAPrivateKey rsa2 = (RSAPrivateKey)k2; + return rsa1.getModulus().equals( rsa2.getModulus() ) + && rsa1.getPrivateExponent().equals( rsa2.getPrivateExponent() ); + } + return Arrays.equals( k1.getEncoded(), k2.getEncoded() ); + } + } |