Class TokenUtils
java.lang.Object
io.getlime.security.powerauth.crypto.lib.util.TokenUtils
Class used for computing PowerAuth Token digests.
- Author:
- Petr Dvorak, petr@wultra.com
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]computeTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret) Compute the digest of provided token information using given token secret.byte[]convertTokenTimestamp(long timestamp) Helper method to convert provided timestamp into bytes (using the string representation of the timestamp), for the purpose of token timestamping.Generate random token ID.byte[]Generate random token nonce, 16 random bytes.byte[]Generate random token secret, 16 random bytes.byte[]Helper method to get current timestamp for the purpose of token timestamping, encoded as bytes from the String representation of timestamp.
The timestamp conversion works like this: Long timestamp is converted to String and then, bytes of the String are extracted usign the UTF-8 encoding.
Code:String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8);booleanvalidateTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret, byte[] tokenDigest) Validate provided token digest for given input data and provided token secret.
-
Constructor Details
-
TokenUtils
public TokenUtils()
-
-
Method Details
-
generateTokenId
Generate random token ID. Use UUID format.- Returns:
- Random token ID.
-
generateTokenSecret
Generate random token secret, 16 random bytes.- Returns:
- Random token secret.
- Throws:
CryptoProviderException- In case key cryptography provider is incorrectly initialized.
-
generateTokenNonce
Generate random token nonce, 16 random bytes.- Returns:
- Random token nonce.
- Throws:
CryptoProviderException- In case key cryptography provider is incorrectly initialized.
-
generateTokenTimestamp
public byte[] generateTokenTimestamp()Helper method to get current timestamp for the purpose of token timestamping, encoded as bytes from the String representation of timestamp.
The timestamp conversion works like this: Long timestamp is converted to String and then, bytes of the String are extracted usign the UTF-8 encoding.
Code:String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8);- Returns:
- Current timestamp in milliseconds.
-
convertTokenTimestamp
public byte[] convertTokenTimestamp(long timestamp) Helper method to convert provided timestamp into bytes (using the string representation of the timestamp), for the purpose of token timestamping.- Parameters:
timestamp- Timestamp to be converted.- Returns:
- Provided timestamp in milliseconds converted as bytes.
-
computeTokenDigest
public byte[] computeTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret) throws GenericCryptoException, CryptoProviderException Compute the digest of provided token information using given token secret.- Parameters:
nonce- Token nonce, 16 random bytes.timestamp- Token timestamp, Unix timestamp format encoded as bytes (string representation).version- Protocol version.tokenSecret- Token secret, 16 random bytes.- Returns:
- Token digest computed using provided data bytes with given token secret.
- Throws:
GenericCryptoException- In case digest computation fails.CryptoProviderException- In case cryptography provider is incorrectly initialized.
-
validateTokenDigest
public boolean validateTokenDigest(byte[] nonce, byte[] timestamp, String version, byte[] tokenSecret, byte[] tokenDigest) throws GenericCryptoException, CryptoProviderException Validate provided token digest for given input data and provided token secret.- Parameters:
nonce- Token nonce, 16 random bytes.timestamp- Token timestamp, Unix timestamp format encoded as bytes (string representation).version- Protocol version.tokenSecret- Token secret, 16 random bytes.tokenDigest- Token digest, 32 bytes to be validated.- Returns:
- Token digest computed using provided data bytes with given token secret.
- Throws:
GenericCryptoException- In case digest computation fails.CryptoProviderException- In case cryptography provider is incorrectly initialized.
-