> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.adid.dev/llms.txt.
> For full documentation content, see https://docs.adid.dev/llms-full.txt.

# SDK — Crypto Helpers

Source: `packages/sdk/src/crypto.ts`. Re-exported at the package root for convenience.

```ts
import {
  base58Encode, base58Decode,
  base64UrlEncode, base64UrlDecode,
  bytesToHex, hexToBytes,
  generateNonce, sha256,
  ed25519Sign, ed25519Verify, generateEd25519KeyPair,
  secp256k1Sign, secp256k1Verify, generateSecp256k1KeyPair,
} from '@infinia/ida-sdk';
```

| Function                                      | Signature                                         | Purpose                                                                                   |
| --------------------------------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `base58Encode(data)`                          | `(Uint8Array) => string`                          | Bitcoin-alphabet Base58                                                                   |
| `base58Decode(str)`                           | `(string) => Uint8Array`                          | Inverse; throws on invalid char                                                           |
| `base64UrlEncode(data)`                       | `(Uint8Array \| string) => string`                | URL-safe, no padding                                                                      |
| `base64UrlDecode(str)`                        | `(string) => Uint8Array`                          |                                                                                           |
| `bytesToHex(bytes)`                           | `(Uint8Array) => string`                          | Lower-case hex                                                                            |
| `hexToBytes(hex)`                             | `(string) => Uint8Array`                          | Throws on odd length                                                                      |
| `generateNonce(length?)`                      | `(number=32) => string`                           | Returns hex; uses `crypto.getRandomValues`                                                |
| `sha256(data)`                                | `(Uint8Array \| string) => Promise<Uint8Array>`   | Web Crypto                                                                                |
| `generateEd25519KeyPair()`                    | `() => Promise<{ publicKey, privateKey }>`        | Web Crypto Ed25519 (with HMAC-SHA-256 fallback when unavailable; see `crypto.ts:206-216`) |
| `ed25519Sign(privateKey, data)`               | `(Uint8Array, Uint8Array) => Promise<Uint8Array>` |                                                                                           |
| `ed25519Verify(publicKey, data, signature)`   | `(Uint8Array×3) => Promise<boolean>`              |                                                                                           |
| `generateSecp256k1KeyPair()`                  | `() => { publicKey, privateKey }`                 | Compressed pubkey via `@noble/secp256k1`                                                  |
| `secp256k1Sign(privateKey, data)`             | `(Uint8Array, Uint8Array) => Promise<Uint8Array>` | SHA-256 + ECDSA, compact bytes                                                            |
| `secp256k1Verify(publicKey, data, signature)` | `(Uint8Array×3) => Promise<boolean>`              |                                                                                           |

> **Important:** The Ed25519 fallback path (when Web Crypto's Ed25519 algorithm is unavailable in the runtime) **silently switches to HMAC-SHA-256** for compatibility with very old browsers. Verifiers MUST NOT trust signatures produced via this fallback. In production, ensure your runtime supports Ed25519 (Node 20+, Chrome 116+, Safari 17+, Firefox 130+).