Class SubtleUtil


  • public final class SubtleUtil
    extends java.lang.Object
    Helper methods.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int androidApiLevel()
      Deprecated.
      Please reimplement this method in your code instead.
      static java.math.BigInteger bytes2Integer​(byte[] bs)
      Converts an unsigned, big-endian encoded byte array to a non-negative integer (https://tools.ietf.org/html/rfc8017#section-4.1).
      static byte[] integer2Bytes​(java.math.BigInteger num, int intendedLength)
      Converts a nonnegative integer to a byte array of a specified length (https://tools.ietf.org/html/rfc8017#section-4.2).
      static boolean isAndroid()
      Best-effort checks that this is Android.
      static byte[] mgf1​(byte[] mgfSeed, int maskLen, Enums.HashType mgfHash)
      Computes MGF1 as defined at https://tools.ietf.org/html/rfc8017#appendix-B.2.1.
      static void putAsUnsigedInt​(java.nio.ByteBuffer buffer, long value)
      Inserts value as unsigned into into buffer.
      static java.lang.String toDigestAlgo​(Enums.HashType hash)
      Returns the digest algorithm name corresponding to a hash type.
      static java.lang.String toEcdsaAlgo​(Enums.HashType hash)
      Returns the Ecdsa algorithm name corresponding to a hash type.
      static java.lang.String toRsaSsaPkcs1Algo​(Enums.HashType hash)
      Returns the RSA SSA (Signature with Appendix) PKCS1 algorithm name corresponding to a hash type.
      • Methods inherited from class java.lang.Object

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

      • toEcdsaAlgo

        public static java.lang.String toEcdsaAlgo​(Enums.HashType hash)
                                            throws java.security.GeneralSecurityException
        Returns the Ecdsa algorithm name corresponding to a hash type.
        Parameters:
        hash - the hash type
        Returns:
        the JCE's Ecdsa algorithm name for the hash.
        Throws:
        java.security.GeneralSecurityException - if hash is not supported or is not safe for digital signature.
      • toRsaSsaPkcs1Algo

        public static java.lang.String toRsaSsaPkcs1Algo​(Enums.HashType hash)
                                                  throws java.security.GeneralSecurityException
        Returns the RSA SSA (Signature with Appendix) PKCS1 algorithm name corresponding to a hash type.
        Parameters:
        hash - the hash type.
        Returns:
        the JCE's RSA SSA PKCS1 algorithm name for the hash.
        Throws:
        java.security.GeneralSecurityException - if hash is not supported or is not safe for digital signature.
      • toDigestAlgo

        public static java.lang.String toDigestAlgo​(Enums.HashType hash)
                                             throws java.security.GeneralSecurityException
        Returns the digest algorithm name corresponding to a hash type.
        Parameters:
        hash - the hash type.
        Returns:
        theh JCE's hash algorithm name.
        Throws:
        java.security.GeneralSecurityException - if hash is not supported.
      • isAndroid

        public static boolean isAndroid()
        Best-effort checks that this is Android.
        Returns:
        true if running on Android.
      • androidApiLevel

        @Deprecated
        public static int androidApiLevel()
        Deprecated.
        Please reimplement this method in your code instead. From within Tink, use Util.getAndroidApiLevel directly.
        Returns the Android API level or -1 if Tink isn't running on Android.
      • bytes2Integer

        public static java.math.BigInteger bytes2Integer​(byte[] bs)
        Converts an unsigned, big-endian encoded byte array to a non-negative integer (https://tools.ietf.org/html/rfc8017#section-4.1).
        Parameters:
        bs - the byte array to be converted to integer.
        Returns:
        the corresponding integer.
      • integer2Bytes

        public static byte[] integer2Bytes​(java.math.BigInteger num,
                                           int intendedLength)
                                    throws java.security.GeneralSecurityException
        Converts a nonnegative integer to a byte array of a specified length (https://tools.ietf.org/html/rfc8017#section-4.2).
        Parameters:
        num - nonnegative integer to be converted.
        intendedLength - intended length of the resulting integer.
        Returns:
        the corresponding byte array of length intendedLength.
        Throws:
        java.security.GeneralSecurityException
      • mgf1

        public static byte[] mgf1​(byte[] mgfSeed,
                                  int maskLen,
                                  Enums.HashType mgfHash)
                           throws java.security.GeneralSecurityException
        Computes MGF1 as defined at https://tools.ietf.org/html/rfc8017#appendix-B.2.1.
        Throws:
        java.security.GeneralSecurityException
      • putAsUnsigedInt

        public static void putAsUnsigedInt​(java.nio.ByteBuffer buffer,
                                           long value)
                                    throws java.security.GeneralSecurityException
        Inserts value as unsigned into into buffer.

        @throws GeneralSecurityException if not 0 <= value < 2^32.

        Throws:
        java.security.GeneralSecurityException