Package com.google.crypto.tink.internal
Class Util
- java.lang.Object
-
- com.google.crypto.tink.internal.Util
-
public final class Util extends java.lang.ObjectHelper functions used throughout Tink, for Tink internal use only.
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.charset.CharsetUTF_8Android 18-compatible alternative toStandardCharsets.UTF_8.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BytescheckedToBytesFromPrintableAscii(java.lang.String s)Converts a stringsto a correspondingBytesobject.static java.lang.IntegergetAndroidApiLevel()Returns the current Android API level as integer or null if we do not run on Android.static booleanisAndroid()Best-effort checks that this is Android.static booleanisPrefix(byte[] prefix, byte[] complete)Returns true if the first argument is a prefix of the second argument.static intrandKeyId()Returns a non-zero random int which can be used as a key ID in a keyset.static SecretBytesreadIntoSecretBytes(java.io.InputStream input, int length, SecretKeyAccess access)Readslengthnumber of bytes from theinputstream and returns it in aSecretBytesobject.static BytestoBytesFromPrintableAscii(java.lang.String s)Converts a stringsto a correspondingBytesobject.
-
-
-
Method Detail
-
randKeyId
public static int randKeyId()
Returns a non-zero random int which can be used as a key ID in a keyset.
-
toBytesFromPrintableAscii
public static final Bytes toBytesFromPrintableAscii(java.lang.String s)
Converts a stringsto a correspondingBytesobject.The string must contain only printable ASCII characters; calling it in any other way is a considered a bug in Tink. Spaces are not allowed.
- Throws:
TinkBugException- if s contains a character which is not a printable ASCII character or a space.
-
checkedToBytesFromPrintableAscii
public static final Bytes checkedToBytesFromPrintableAscii(java.lang.String s) throws java.security.GeneralSecurityException
Converts a stringsto a correspondingBytesobject.- Throws:
java.security.GeneralSecurityException- if s contains a character which is not a printable ASCII character or a space.
-
isAndroid
public static boolean isAndroid()
Best-effort checks that this is Android.Note: this is more tricky than it seems. For example, using reflection to see if android.os.Build.SDK_INT exists might fail because proguard might break the reflection part. Using build dispatching can also fail if there are issues in the build graph (see cl/510374081).
- Returns:
- true if running on Android.
-
getAndroidApiLevel
@Nullable public static java.lang.Integer getAndroidApiLevel()
Returns the current Android API level as integer or null if we do not run on Android.
-
isPrefix
public static boolean isPrefix(byte[] prefix, byte[] complete)Returns true if the first argument is a prefix of the second argument. Not constant time.
-
readIntoSecretBytes
public static SecretBytes readIntoSecretBytes(java.io.InputStream input, int length, SecretKeyAccess access) throws java.security.GeneralSecurityException
Readslengthnumber of bytes from theinputstream and returns it in aSecretBytesobject.Note that this method will not close the
inputstream.- Throws:
java.security.GeneralSecurityException- when not enough randomness was provided in theinputstream.
-
-