Class Crypto
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 TypeMethodDescriptionstatic org.teavm.jso.typedarrays.ArrayBufferHashes bytes with a named algorithm.static org.teavm.jso.typedarrays.ArrayBufferHashes bytes with a named algorithm.static org.teavm.jso.typedarrays.ArrayBufferHMAC of bytes under a key of bytes.static org.teavm.jso.typedarrays.ArrayBufferHMAC of text under a key.static org.teavm.jso.typedarrays.ArrayBufferhmacSha256(String key, String text) HMAC-SHA256 of text under a key.static org.teavm.jso.typedarrays.ArrayBuffermd5(byte[] bytes) MD5 of bytes.static org.teavm.jso.typedarrays.ArrayBufferMD5 of text, UTF-8 encoded.static org.teavm.jso.typedarrays.ArrayBufferrandom(int count) Cryptographically random bytes.static org.teavm.jso.typedarrays.ArrayBufferrandom32()Cryptographically random 32 bytes.static org.teavm.jso.typedarrays.ArrayBuffersha1(byte[] bytes) SHA-1 of bytes.static org.teavm.jso.typedarrays.ArrayBufferSHA-1 of text, UTF-8 encoded.static org.teavm.jso.typedarrays.ArrayBuffersha256(byte[] bytes) SHA-256 of bytes.static org.teavm.jso.typedarrays.ArrayBufferSHA-256 of text, UTF-8 encoded.static org.teavm.jso.typedarrays.ArrayBuffersha512(byte[] bytes) SHA-512 of bytes.static org.teavm.jso.typedarrays.ArrayBufferSHA-512 of text, UTF-8 encoded.static booleantimingSafeEquals(byte[] left, byte[] right) Compares two byte arrays without leaking which byte differed through timing.static booleantimingSafeEquals(String left, String right) Compares two strings without leaking which byte differed through timing.static Stringuuid()Returns a cryptographically random UUID.
-
Method Details
-
md5
MD5 of text, UTF-8 encoded.Not part of Web Crypto. Cloudflare supports it for
digestonly, 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
SHA-256 of text, UTF-8 encoded.- Parameters:
text- the text- Returns:
- the digest
-
sha512
SHA-512 of text, UTF-8 encoded.- Parameters:
text- the text- Returns:
- the digest
-
sha1
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-512orMD5bytes- the bytes- Returns:
- the digest
-
digest
Hashes bytes with a named algorithm.- Parameters:
algorithm-SHA-1,SHA-256,SHA-384,SHA-512orMD5bytes- 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
HMAC of text under a key.- Parameters:
algorithm-SHA-256or another SHA variant. MD5 is refused herekey- the keytext- the text- Returns:
- the signature
-
hmac
HMAC of bytes under a key of bytes.The form to use for a key that is not text. A key given as a
Stringis UTF-8 encoded, which is wrong for a key that came out of a key derivation or a random generator.- Parameters:
algorithm-SHA-256or another SHA variant. MD5 is refused herekey- the keydata- the bytes to sign- Returns:
- the signature
-
hmacSha256
HMAC-SHA256 of text under a key.- Parameters:
key-Stringkeytext-Stringtext- Returns:
ArrayBuffersignature
-
timingSafeEquals
Compares two strings without leaking which byte differed through timing.An ordinary
equalsreturns 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 stringright- 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 bytesright- the second bytes- Returns:
- whether they match
-
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
-