# hectorm/otpauth architecture

> Architecture diagram of the GitHub repository hectorm/otpauth, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hectorm/otpauth
- Repository: https://github.com/hectorm/otpauth

## Overview

##### Main Components

a) Core Components:
- HOTP Module (RFC 4226 implementation)
- TOTP Module (RFC 6238 implementation)
- Secret Management Module
- URI Handler Module

b) Internal Components:
- Crypto Operations Layer
- Encoding Layer
- Global Scope Handler

**┌─ Public API Layer ─────────────────────┐**
- HOTP │ TOTP │ Secret │ URI │ Version  │
- ┬────────────────────┘
- ↓
**┌─ Internal Layer ─────────────────────────────────┐**
- ┌─ Crypto ─────┐    ┌─ Encoding ─────────┐      │
- HMAC Digest  │    │ Base32             │      │
- Random Bytes │    │ Hex                │      │
- Timing Safe  │    │ Latin1             │      │
- ┘    │ UTF8               │      │
- UINT               │      │

- ┘

##### Key Elements to Highlight

a) Platform Support:
- Compatibility with different environments (Node.js, Deno, Bun, Browsers)
- Distribution formats (ESM, UMD, CJS)

b) Security Components:
- Cryptographic operations
- Secure random generation flow
- Validation mechanisms

##### Specific Implementation Details

**┌─ TOTP Class ──────────┐**
- issuer             │
- algorithm          │
- digits             │
- period             │
- secret             │
- ┘

##### Data Flow Indicators

- Secret generation flow
- Token generation process
- Validation flow
- URI encoding/decoding paths

##### URI handling for compatibility with standard authenticator apps

This will provide a comprehensive view of the library's architecture while highlighting its security-focused nature and broad platform support.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Public_API
        HOTP["HOTP Module"]
        TOTP["TOTP Module"]
        Secret["Secret Management"]
        URI["URI Handler"]
        Version["Version Management"]
    end

    subgraph Internal_Layer
        subgraph Crypto_Ops
            HMAC["HMAC Digest"]
            Random["Random Bytes"]
            Timing["Timing Safe Equal"]
        end

        subgraph Encoding_Ops
            Base32["Base32 Encoding"]
            Hex["Hex Encoding"]
            Latin1["Latin1 Encoding"]
            UTF8["UTF8 Encoding"]
            UINT["UINT Encoding"]
        end

        Global["Global Scope Handler"]
    end

    %% Relationships
    HOTP --> Crypto_Ops
    HOTP --> Encoding_Ops
    TOTP --> HOTP
    TOTP --> Encoding_Ops
    Secret --> Random
    Secret --> Encoding_Ops
    URI --> Encoding_Ops
    
    %% All components interact with Global Scope
    HOTP --> Global
    TOTP --> Global
    Secret --> Global
    URI --> Global
    
    %% Component click events
    click HOTP "https://github.com/hectorm/otpauth/blob/master/src/hotp.js"
    click TOTP "https://github.com/hectorm/otpauth/blob/master/src/totp.js"
    click Secret "https://github.com/hectorm/otpauth/blob/master/src/secret.js"
    click URI "https://github.com/hectorm/otpauth/blob/master/src/uri.js"
    click Version "https://github.com/hectorm/otpauth/blob/master/src/version.js"
    click HMAC "https://github.com/hectorm/otpauth/blob/master/src/internal/crypto/hmac-digest.js"
    click Random "https://github.com/hectorm/otpauth/blob/master/src/internal/crypto/random-bytes.js"
    click Timing "https://github.com/hectorm/otpauth/blob/master/src/internal/crypto/timing-safe-equal.js"
    click Base32 "https://github.com/hectorm/otpauth/blob/master/src/internal/encoding/base32.js"
    click Hex "https://github.com/hectorm/otpauth/blob/master/src/internal/encoding/hex.js"
    click Latin1 "https://github.com/hectorm/otpauth/blob/master/src/internal/encoding/latin1.js"
    click UTF8 "https://github.com/hectorm/otpauth/blob/master/src/internal/encoding/utf8.js"
    click UINT "https://github.com/hectorm/otpauth/blob/master/src/internal/encoding/uint.js"
    click Global "https://github.com/hectorm/otpauth/blob/master/src/internal/global-scope.js"

    %% Styling
    classDef public fill:#2374ab,stroke:#000,stroke-width:2px,color:#fff
    classDef crypto fill:#2a9d8f,stroke:#000,stroke-width:2px,color:#fff
    classDef encoding fill:#e9c46a,stroke:#000,stroke-width:2px,color:#000
    classDef internal fill:#grey,stroke:#000,stroke-width:2px,color:#000

    class HOTP,TOTP,Secret,URI,Version public
    class HMAC,Random,Timing crypto
    class Base32,Hex,Latin1,UTF8,UINT encoding
    class Global internal
```
