# Utils

`com.jsmacrosce.jsmacros.api.library.FUtils`

**Since:** 1.8.4

Standalone utilities for hashing and Base64 conversion, guessing chat sender names, and
rejecting null values.  
```js
// hashing, sha-256 in hex by default
Chat.log(Utils.hashString("hello"));  
// the same digest in base64, which is the form a lot of APIs ask for
Chat.log(Utils.hashString("hello", "sha256", true));  

// a base64 round trip
const encoded = Utils.encode("hello");  
Chat.log(`${encoded} decodes back to ${Utils.decode(encoded)}`);  

// pulling the sender's name back out of a chat line
const name = Utils.guessName("Notch whispers to you: meet me at spawn");  
Chat.log(name === null ? "no name could be guessed" : `that was ${name}`);  
```

Accessible in scripts via the global `Utils` variable.

## Methods

### decode {#com.jsmacrosce.jsmacros.api.library.FUtils_decode-String-}

```js
decode(String message): String
```

Base64-decodes the string and converts the decoded bytes to text using the platform's
default charset.

**Parameters:**
- `message`: the message to decode

**Returns:** the decoded message.

**Since:** 1.8.4

### encode {#com.jsmacrosce.jsmacros.api.library.FUtils_encode-String-}

```js
encode(String message): String
```

Base64-encodes the string's bytes, using the platform's default charset to convert the
string to bytes and the encoded bytes back to text.

**Parameters:**
- `message`: the message to encode

**Returns:** the encoded message.

**Since:** 1.8.4

### guessName {#com.jsmacrosce.jsmacros.api.library.FUtils_guessName-String-}

```js
guessName(String text): String
```

Guesses the sender name from a chat message; the heuristic may not fit every server's format.

**Parameters:**
- `text`: the text to check

**Returns:** the name of the sender or `null` if it couldn't be guessed.

**Since:** 1.8.4

### guessNameAndRoles {#com.jsmacrosce.jsmacros.api.library.FUtils_guessNameAndRoles-String-}

```js
guessNameAndRoles(String text): List<String>
```

Guesses a sender name and any extracted role or prefix fragments. It examines at most the
first 60 characters; whisper-form messages yield only the name. This is a heuristic, not a
chat-format parser, and input the heuristic cannot recognize produces an empty list. When
the returned list contains a name, it is the first element.

**Parameters:**
- `text`: the text to check

**Returns:** a list of names, titles and roles of the sender or an empty list if it couldn't be
guessed.

**Since:** 1.8.4

### hashString {#com.jsmacrosce.jsmacros.api.library.FUtils_hashString-String-}

```js
hashString(String message): String
```

Hashes the message with SHA-256 and returns lowercase hexadecimal.

**Parameters:**
- `message`: the message to hash

**Returns:** the hashed message.

**Since:** 1.8.4

### hashString {#com.jsmacrosce.jsmacros.api.library.FUtils_hashString-String-String-}

```js
hashString(String message, String algorithm): String
```

Hashes with the selected algorithm and returns lowercase hexadecimal. Algorithm names are
case-sensitive; an unrecognized name returns the input unchanged, including `null`.  
Recognized algorithms do not accept a `null` message.

**Parameters:**
- `message`: the message to hash
- `algorithm`: sha1 | sha256 | sha384 | sha512 | md2 | md5

**Returns:** the digest as lowercase hexadecimal, or the original message if the algorithm is
        unrecognized

**Since:** 1.8.4

### hashString {#com.jsmacrosce.jsmacros.api.library.FUtils_hashString-String-String-Boolean-}

```js
hashString(String message, String algorithm, Boolean base64): String
```

Hashes with the selected algorithm, returning lowercase hexadecimal or Base64 of the raw
digest bytes when `base64` is `true`. An unrecognized algorithm returns the
input unchanged; recognized algorithms do not accept a `null` message.

**Parameters:**
- `message`: the message to hash
- `algorithm`: sha1 | sha256 | sha384 | sha512 | md2 | md5
- `base64`: encode the result in base64

**Returns:** the digest in the selected encoding, or the original message if the algorithm is
        unrecognized

**Since:** 1.9.1

### requireNonNull {#com.jsmacrosce.jsmacros.api.library.FUtils_requireNonNull-T-}

```js
requireNonNull(T obj): T
```

Returns the reference if non-null; otherwise throws [NullPointerException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/NullPointerException.html) without a
detail message. Use the two-argument overload to provide one.

**Parameters:**
- `obj`: the object reference to check for nullity

**Type Parameters:**
- `T`: the reference type

**Returns:** `obj` if not `null`

**Since:** 1.9.1

### requireNonNull {#com.jsmacrosce.jsmacros.api.library.FUtils_requireNonNull-T-String-}

```js
requireNonNull(T obj, String message): T
```

Returns `obj` if it is non-null; otherwise throws a
[NullPointerException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/NullPointerException.html) with `message` as its detail message.

**Parameters:**
- `obj`: the object reference to check for nullity
- `message`: detail message for the [NullPointerException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/NullPointerException.html) if `obj` is `null`

**Type Parameters:**
- `T`: the reference type

**Returns:** `obj` if not `null`

**Since:** 1.9.1
