# Utility Class

### K Class Overview

* **Static Methods**\
  All methods are static, eliminating the need for object instantiation.
* **Encoding and Decoding**\
  Supports encoding and decoding for multiple formats, including URL, Base64, CSV, JSON, XML, and YAML.
* **AES-256 Encryption/Decryption**\
  Provides robust encryption and decryption using the AES/CBC/PKCS5Padding algorithm. The provided secure key is hashed with SHA-256 to create a 256-bit key. Use K.getRandomBytes(16) to generate the required initialization vector.
* **Data Compression**\
  Enables ZLIB and GZIP compression and decompression for efficient data handling.
* **Hash Generation**\
  Supports generating cryptographic hashes using MD5, SHA-2, or SHA-3 algorithms.
* **DNS Querying**\
  Allows querying of any DNS record type (e.g., MX, A). MX records are returned in priority order for convenience.
* **Thread Management**\
  Includes functionality to introduce delays in thread execution.
* **Environment Information**\
  Retrieves detailed environment data, such as JVM version, IP address, hostname, and more.

### Example Delay Thread

```java
// Wait 1/4 second
K.waitMilliseconds(250);

// Wait 5 minutes
K.waitMinutes(5);
```

### Example Encode/Decode

```java
// Encode Base-64 string
String httpAuth = K.encodeBase64(userName + ':' + password);

// Encode URL
String urlEncoded = K.encodeURL("https://example.com?q=1&p=done");
```

### Example Query DNS Server

```java
// Get DNS SOA record
String[] soaRRs = K.queryDNS("SOA", "google.com");

// Get DNS MX record(s)
String[] mxRRs = K.queryDNS("MX", "yahoo.com");
```

### Example AES-256

```java
String secretKey = "This is the secret key";
		
// Create a simple text file
KFile.writeFile("This is a simple text file", "AES-Text.txt");
		
// Generate and save random AES-256 initialization vector
KFile.writeFile(K.getRandomBytes(16), "AES.iv");
		
// AES-256 Encrypt
byte[] clearText  = KFile.readByteFile("AES-Text.txt");
byte[] initVector = KFile.readByteFile("AES.iv");
byte[] cipherText = K.encryptAES256(clearText, secretKey.getBytes(), initVector);
KFile.writeFile(cipherText, "AES-Text.encrypted");
		
// AES-256 Decrypt
cipherText = KFile.readByteFile("AES-Text.encrypted");
initVector = KFile.readByteFile("AES.iv");
clearText  = K.decryptAES256(cipherText, secretKey.getBytes(), initVector);
		
System.out.println(new String(clearText));
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java-util.k43.ch/examples/utility-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
