Package org.drasyl.crypto
Class Crypto
- java.lang.Object
-
- org.drasyl.crypto.Crypto
-
public class Crypto extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static SecureRandom
SRND
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
compressedKey(PrivateKey privkey)
static byte[]
compressedKey(PublicKey key)
static KeyPair
generateKeys()
static PrivateKey
getPrivateKeyFromBytes(byte[] privKey)
static PublicKey
getPublicKeyFromBytes(byte[] pubKey)
static KeyPair
makeKeyPair(byte[] compressedPrivate, byte[] compressedPublic)
static org.bouncycastle.jce.interfaces.ECPrivateKey
parseCompressedPrivateKey(byte[] compressedPrivateKey)
static org.bouncycastle.jce.interfaces.ECPublicKey
parseCompressedPublicKey(byte[] compressedPubKey)
static int
randomNumber(int bound)
Generates a random number with the staticSecureRandom
of this class.static String
randomString(int entropy)
Generates a secure random HEX String with the givenentropy
of bytes.static void
sign(PrivateKey key, Signable signable)
Signs the given signable with the Privatekey.static byte[]
signMessage(PrivateKey key, byte[] message)
static boolean
verifySignature(byte[] compressedPublicKey, byte[] message, byte[] signature)
static boolean
verifySignature(PublicKey pubkey, byte[] message, byte[] signature)
static boolean
verifySignature(PublicKey publicKey, Signable content)
-
-
-
Field Detail
-
SRND
public static final SecureRandom SRND
-
-
Method Detail
-
generateKeys
public static KeyPair generateKeys()
-
makeKeyPair
public static KeyPair makeKeyPair(byte[] compressedPrivate, byte[] compressedPublic) throws CryptoException
- Throws:
CryptoException
-
getPublicKeyFromBytes
public static PublicKey getPublicKeyFromBytes(byte[] pubKey) throws CryptoException
- Throws:
CryptoException
-
getPrivateKeyFromBytes
public static PrivateKey getPrivateKeyFromBytes(byte[] privKey) throws CryptoException
- Throws:
CryptoException
-
parseCompressedPublicKey
public static org.bouncycastle.jce.interfaces.ECPublicKey parseCompressedPublicKey(byte[] compressedPubKey) throws CryptoException
- Throws:
CryptoException
-
parseCompressedPrivateKey
public static org.bouncycastle.jce.interfaces.ECPrivateKey parseCompressedPrivateKey(byte[] compressedPrivateKey) throws CryptoException
- Throws:
CryptoException
-
compressedKey
public static byte[] compressedKey(PublicKey key) throws CryptoException
- Throws:
CryptoException
-
compressedKey
public static byte[] compressedKey(PrivateKey privkey) throws CryptoException
- Throws:
CryptoException
-
sign
public static void sign(PrivateKey key, Signable signable) throws CryptoException
Signs the given signable with the Privatekey. This will also put the resulting signature into the Signable object- Parameters:
key
- Key to usesignable
- signature to create- Throws:
CryptoException
- on failure
-
signMessage
public static byte[] signMessage(PrivateKey key, byte[] message) throws CryptoException
- Throws:
CryptoException
-
verifySignature
public static boolean verifySignature(byte[] compressedPublicKey, byte[] message, byte[] signature)
-
verifySignature
public static boolean verifySignature(PublicKey pubkey, byte[] message, byte[] signature)
-
randomString
public static String randomString(int entropy)
Generates a secure random HEX String with the givenentropy
of bytes.Recommendation:
- 4 byte for small sets
- 8 bytes for unique internal strings, e.g. hash tables
- 16 bytes for global uniqueness, e.g. auth token
You can also use the following probability table for the "Birthday problem", as a starting point for a suitable entropy size: Birthday problem probability table
- Parameters:
entropy
- entropy in bytes- Returns:
- a secure random HEX String
-
randomNumber
public static int randomNumber(int bound)
Generates a random number with the staticSecureRandom
of this class. Avoids overhead of generating a new instance ofSecureRandom
.- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- the next pseudorandom, uniformly distributed
int
value between zero (inclusive) andbound
(exclusive) from this random number generator's sequence
-
-