Class Util


  • public final class Util
    extends java.lang.Object
    Helper functions used throughout Tink, for Tink internal use only.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.nio.charset.Charset UTF_8
      Android 18-compatible alternative to StandardCharsets.UTF_8.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Bytes checkedToBytesFromPrintableAscii​(java.lang.String s)
      Converts a string s to a corresponding Bytes object.
      static java.lang.Integer getAndroidApiLevel()
      Returns the current Android API level as integer or null if we do not run on Android.
      static boolean isAndroid()
      Best-effort checks that this is Android.
      static boolean isPrefix​(byte[] prefix, byte[] complete)
      Returns true if the first argument is a prefix of the second argument.
      static int randKeyId()
      Returns a non-zero random int which can be used as a key ID in a keyset.
      static SecretBytes readIntoSecretBytes​(java.io.InputStream input, int length, SecretKeyAccess access)
      Reads length number of bytes from the input stream and returns it in a SecretBytes object.
      static Bytes toBytesFromPrintableAscii​(java.lang.String s)
      Converts a string s to a corresponding Bytes object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UTF_8

        public static final java.nio.charset.Charset UTF_8
        Android 18-compatible alternative to StandardCharsets.UTF_8.
    • 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 string s to a corresponding Bytes object.

        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 string s to a corresponding Bytes object.
        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
        Reads length number of bytes from the input stream and returns it in a SecretBytes object.

        Note that this method will not close the input stream.

        Throws:
        java.security.GeneralSecurityException - when not enough randomness was provided in the input stream.