Class Crypto

java.lang.Object
dev.gmitch215.bytebox.builtin.Crypto

public final class Crypto extends Object
The platform's Web Crypto, exposed as itself.

Hashing and signing run in the runtime rather than in the binary, so a digest costs no wasm bytes and runs at native speed. Every method here blocks, because Web Crypto is asynchronous.

 String digest = Text.toHex(Crypto.sha256("hello world!"));
Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.teavm.jso.typedarrays.ArrayBuffer
    digest(String algorithm, byte[] bytes)
    Hashes bytes with a named algorithm.
    static org.teavm.jso.typedarrays.ArrayBuffer
    digest(String algorithm, org.teavm.jso.typedarrays.ArrayBuffer bytes)
    Hashes bytes with a named algorithm.
    static org.teavm.jso.typedarrays.ArrayBuffer
    hmac(String algorithm, byte[] key, byte[] data)
    HMAC of bytes under a key of bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    hmac(String algorithm, String key, String text)
    HMAC of text under a key.
    static org.teavm.jso.typedarrays.ArrayBuffer
    hmacSha256(String key, String text)
    HMAC-SHA256 of text under a key.
    static org.teavm.jso.typedarrays.ArrayBuffer
    md5(byte[] bytes)
    MD5 of bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    md5(String text)
    MD5 of text, UTF-8 encoded.
    static org.teavm.jso.typedarrays.ArrayBuffer
    random(int count)
    Cryptographically random bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    Cryptographically random 32 bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha1(byte[] bytes)
    SHA-1 of bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha1(String text)
    SHA-1 of text, UTF-8 encoded.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha256(byte[] bytes)
    SHA-256 of bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha256(String text)
    SHA-256 of text, UTF-8 encoded.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha512(byte[] bytes)
    SHA-512 of bytes.
    static org.teavm.jso.typedarrays.ArrayBuffer
    sha512(String text)
    SHA-512 of text, UTF-8 encoded.
    static boolean
    timingSafeEquals(byte[] left, byte[] right)
    Compares two byte arrays without leaking which byte differed through timing.
    static boolean
    Compares two strings without leaking which byte differed through timing.
    static String
    Returns a cryptographically random UUID.

    Methods inherited from class java.lang.Object

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

    • md5

      public static org.teavm.jso.typedarrays.ArrayBuffer md5(String text)
      MD5 of text, UTF-8 encoded.

      Not part of Web Crypto. Cloudflare supports it for digest only, for talking to systems that will not change, and states plainly that it is weak. Nothing security-bearing should rest on it, and it is not available as an HMAC hash.

      Parameters:
      text - the text
      Returns:
      the digest
    • sha256

      public static org.teavm.jso.typedarrays.ArrayBuffer sha256(String text)
      SHA-256 of text, UTF-8 encoded.
      Parameters:
      text - the text
      Returns:
      the digest
    • sha512

      public static org.teavm.jso.typedarrays.ArrayBuffer sha512(String text)
      SHA-512 of text, UTF-8 encoded.
      Parameters:
      text - the text
      Returns:
      the digest
    • sha1

      public static org.teavm.jso.typedarrays.ArrayBuffer sha1(String text)
      SHA-1 of text, UTF-8 encoded.

      Broken for anything adversarial. Present because two things still need it: an interoperable serialVersionUID, which the serialization specification fixes to SHA-1, and older protocols that will not change.

      Parameters:
      text - the text
      Returns:
      the digest
    • digest

      public static org.teavm.jso.typedarrays.ArrayBuffer digest(String algorithm, org.teavm.jso.typedarrays.ArrayBuffer bytes)
      Hashes bytes with a named algorithm.
      Parameters:
      algorithm - SHA-1, SHA-256, SHA-384, SHA-512 or MD5
      bytes - the bytes
      Returns:
      the digest
    • digest

      public static org.teavm.jso.typedarrays.ArrayBuffer digest(String algorithm, byte[] bytes)
      Hashes bytes with a named algorithm.
      Parameters:
      algorithm - SHA-1, SHA-256, SHA-384, SHA-512 or MD5
      bytes - the bytes
      Returns:
      the digest
    • md5

      public static org.teavm.jso.typedarrays.ArrayBuffer md5(byte[] bytes)
      MD5 of bytes.
      Parameters:
      bytes - the bytes
      Returns:
      the digest
    • sha1

      public static org.teavm.jso.typedarrays.ArrayBuffer sha1(byte[] bytes)
      SHA-1 of bytes.
      Parameters:
      bytes - the bytes
      Returns:
      the digest
    • sha256

      public static org.teavm.jso.typedarrays.ArrayBuffer sha256(byte[] bytes)
      SHA-256 of bytes.
      Parameters:
      bytes - the bytes
      Returns:
      the digest
    • sha512

      public static org.teavm.jso.typedarrays.ArrayBuffer sha512(byte[] bytes)
      SHA-512 of bytes.
      Parameters:
      bytes - the bytes
      Returns:
      the digest
    • hmac

      public static org.teavm.jso.typedarrays.ArrayBuffer hmac(String algorithm, String key, String text)
      HMAC of text under a key.
      Parameters:
      algorithm - SHA-256 or another SHA variant. MD5 is refused here
      key - the key
      text - the text
      Returns:
      the signature
    • hmac

      public static org.teavm.jso.typedarrays.ArrayBuffer hmac(String algorithm, byte[] key, byte[] data)
      HMAC of bytes under a key of bytes.

      The form to use for a key that is not text. A key given as a String is UTF-8 encoded, which is wrong for a key that came out of a key derivation or a random generator.

      Parameters:
      algorithm - SHA-256 or another SHA variant. MD5 is refused here
      key - the key
      data - the bytes to sign
      Returns:
      the signature
    • hmacSha256

      public static org.teavm.jso.typedarrays.ArrayBuffer hmacSha256(String key, String text)
      HMAC-SHA256 of text under a key.
      Parameters:
      key - String key
      text - String text
      Returns:
      ArrayBuffer signature
    • timingSafeEquals

      public static boolean timingSafeEquals(String left, String right)
      Compares two strings without leaking which byte differed through timing.

      An ordinary equals returns as soon as it finds a difference, which tells an attacker how much of a guess was right. This does not.

      Parameters:
      left - the first string
      right - the second string
      Returns:
      whether they match
    • timingSafeEquals

      public static boolean timingSafeEquals(byte[] left, byte[] right)
      Compares two byte arrays without leaking which byte differed through timing.
      Parameters:
      left - the first bytes
      right - the second bytes
      Returns:
      whether they match
    • uuid

      public static String uuid()
      Returns a cryptographically random UUID.
      Returns:
      a cryptographically random UUID
    • random

      public static org.teavm.jso.typedarrays.ArrayBuffer random(int count)
      Cryptographically random bytes.
      Parameters:
      count - how many
      Returns:
      the bytes
    • random32

      public static org.teavm.jso.typedarrays.ArrayBuffer random32()
      Cryptographically random 32 bytes.
      Returns:
      the bytes