Package org.drasyl.crypto
Class Crypto
- java.lang.Object
-
- org.drasyl.crypto.Crypto
-
public class Crypto extends Object
Util class that provides cryptography functions for drasyl.
-
-
Field Summary
Fields Modifier and Type Field Description static int
COMPRESSED_KEY_LENGTH
static SecureRandom
CSPRNG
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
compressedKey(PrivateKey privkey)
Generates an asymmetric, compressed curve private key from the given private key.static byte[]
compressedKey(PublicKey key)
Generates an asymmetric, compressed curve public key from the given public key.static KeyPair
generateKeys()
Generates an asymmetric curve key pair for signing.static PrivateKey
getPrivateKeyFromBytes(byte[] privKey)
Generates an asymmetric curve private key from the given bytes.static PublicKey
getPublicKeyFromBytes(byte[] pubKey)
Generates an asymmetric curve public key from the given bytes.static org.bouncycastle.jce.interfaces.ECPrivateKey
parseCompressedPrivateKey(byte[] compressedPrivateKey)
Generates an asymmetric curve private key from the given compressed private key.static org.bouncycastle.jce.interfaces.ECPublicKey
parseCompressedPublicKey(byte[] compressedPubKey)
Generates an asymmetric curve public key from the given compressed public key.static byte[]
randomBytes(int entropy)
Generates a secure random bytes with the givenentropy
.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 byte[]
signMessage(PrivateKey key, byte[] message)
Creates signature from the given message with the PrivateKey.static boolean
verifySignature(PublicKey pubkey, byte[] message, byte[] signature)
Verify the signature of the given message with the signature and public key.
-
-
-
Field Detail
-
CSPRNG
public static final SecureRandom CSPRNG
-
COMPRESSED_KEY_LENGTH
public static final int COMPRESSED_KEY_LENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
generateKeys
public static KeyPair generateKeys()
Generates an asymmetric curve key pair for signing.- Returns:
- asymmetric key pair
-
getPublicKeyFromBytes
public static PublicKey getPublicKeyFromBytes(byte[] pubKey) throws CryptoException
Generates an asymmetric curve public key from the given bytes.- Parameters:
pubKey
- public key as byte array- Returns:
- asymmetric curve public key
- Throws:
CryptoException
- if public key could not be generated
-
getPrivateKeyFromBytes
public static PrivateKey getPrivateKeyFromBytes(byte[] privKey) throws CryptoException
Generates an asymmetric curve private key from the given bytes.- Parameters:
privKey
- private key as byte array- Returns:
- asymmetric curve private key
- Throws:
CryptoException
- if private key could not be generated
-
parseCompressedPublicKey
public static org.bouncycastle.jce.interfaces.ECPublicKey parseCompressedPublicKey(byte[] compressedPubKey) throws CryptoException
Generates an asymmetric curve public key from the given compressed public key.- Parameters:
compressedPubKey
- compressed public key- Returns:
- asymmetric curve public key
- Throws:
CryptoException
- if public key could not be generated
-
parseCompressedPrivateKey
public static org.bouncycastle.jce.interfaces.ECPrivateKey parseCompressedPrivateKey(byte[] compressedPrivateKey) throws CryptoException
Generates an asymmetric curve private key from the given compressed private key.- Parameters:
compressedPrivateKey
- compressed private key- Returns:
- asymmetric curve private key
- Throws:
CryptoException
- if private key could not be generated
-
compressedKey
public static byte[] compressedKey(PublicKey key) throws CryptoException
Generates an asymmetric, compressed curve public key from the given public key.- Parameters:
key
- the public key- Returns:
- compressed public key
- Throws:
CryptoException
- if the public key was not in ECPublicKey format
-
compressedKey
public static byte[] compressedKey(PrivateKey privkey) throws CryptoException
Generates an asymmetric, compressed curve private key from the given private key.- Parameters:
privkey
- the private key- Returns:
- compressed private key
- Throws:
CryptoException
- if the public key was not in ECPrivateKey format
-
signMessage
public static byte[] signMessage(PrivateKey key, byte[] message) throws CryptoException
Creates signature from the given message with the PrivateKey.- Parameters:
key
- Key to usemessage
- message to sign- Throws:
CryptoException
- on failure
-
verifySignature
public static boolean verifySignature(PublicKey pubkey, byte[] message, byte[] signature)
Verify the signature of the given message with the signature and public key.- Parameters:
pubkey
- the public keymessage
- the message to verifysignature
- the signature of the message- Returns:
- if the message is valid or not
-
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
-
randomBytes
public static byte[] randomBytes(int entropy)
Generates a secure random bytes with the givenentropy
.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 bytes
-
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
-
-