Package org.drasyl.crypto
Class Crypto
java.lang.Object
org.drasyl.crypto.Crypto
Util class that provides cryptography functions for drasyl.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 KeyPairGenerates an asymmetric curve key pair for signing.static PrivateKeygetPrivateKeyFromBytes(byte[] privKey) Generates an asymmetric curve private key from the given bytes.static PublicKeygetPublicKeyFromBytes(byte[] pubKey) Generates an asymmetric curve public key from the given bytes.static KeyPairmakeKeyPair(byte[] compressedPrivate, byte[] compressedPublic) Generates an asymmetric curve key pair from the given compressed private and public key.static org.bouncycastle.jce.interfaces.ECPrivateKeyparseCompressedPrivateKey(byte[] compressedPrivateKey) Generates an asymmetric curve private key from the given compressed private key.static org.bouncycastle.jce.interfaces.ECPublicKeyparseCompressedPublicKey(byte[] compressedPubKey) Generates an asymmetric curve public key from the given compressed public key.static intrandomNumber(int bound) Generates a random number with the staticSecureRandomof this class.static StringrandomString(int entropy) Generates a secure random HEX String with the givenentropyof bytes.static voidsign(PrivateKey key, Signable signable) Signs the given signable with the PrivateKey.static byte[]signMessage(PrivateKey key, byte[] message) Creates signature from the given message with the PrivateKey.static booleanverifySignature(byte[] compressedPublicKey, byte[] message, byte[] signature) Verifies the signature of the given message with the signature and compressed public key.static booleanverifySignature(PublicKey pubkey, byte[] message, byte[] signature) Verify the signature of the given message with the signature and public key.static booleanverifySignature(PublicKey publicKey, Signable content) Verify the signature of the given Signable object with the public key.
-
Field Details
-
SRND
-
-
Method Details
-
generateKeys
Generates an asymmetric curve key pair for signing.- Returns:
- asymmetric key pair
-
makeKeyPair
public static KeyPair makeKeyPair(byte[] compressedPrivate, byte[] compressedPublic) throws CryptoException Generates an asymmetric curve key pair from the given compressed private and public key.- Parameters:
compressedPrivate- compressed private keycompressedPublic- compressed public key- Returns:
- asymmetric curve key pair
- Throws:
CryptoException- if key pair could not be generated
-
getPublicKeyFromBytes
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
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
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
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
-
sign
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
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(byte[] compressedPublicKey, byte[] message, byte[] signature) Verifies the signature of the given message with the signature and compressed public key.- Parameters:
compressedPublicKey- the compressed public keymessage- the message to verifysignature- the signature of the message- Returns:
- if the message is valid or not
-
verifySignature
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
-
verifySignature
Verify the signature of the given Signable object with the public key.- Parameters:
publicKey- the public keycontent- the Signable object- Returns:
- if the content is valid or not
-
randomString
Generates a secure random HEX String with the givenentropyof 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 staticSecureRandomof 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
intvalue between zero (inclusive) andbound(exclusive) from this random number generator's sequence
-