# zyjnicemoe/sm-crypto-utils architecture

> Architecture diagram of the GitHub repository zyjnicemoe/sm-crypto-utils, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zyjnicemoe/sm-crypto-utils
- Repository: https://github.com/zyjnicemoe/sm-crypto-utils

## Overview

##### Project type and purpose
- This is a Java/Maven library (“sm-crypto-utils”) providing utility classes for the Chinese national cryptographic algorithms SM2, SM3, and SM4.
- It exposes a public API (SM2.java, SM3.java, SM4.java) and wraps BouncyCastle as its cryptographic provider.

##### File structure
- Top-level:
- pom.xml — Maven coordinates and dependencies
- build_release.sh, build_snapshots.sh — release pipeline scripts
- src/main/java/cn/zyjblogs/crypto:
- SmException.java — common exception type
- sm2/… — SM2 keypair generation, encrypt/decrypt, signature & verification classes
- sm3/SM3.java — digest/hash operations
- sm4/SM4.java — symmetric key gen, encrypt/decrypt operations

##### Review the README for insights
- Confirms SM2, SM3, SM4 usage examples.
- Notes reliance on BC (“BC库”) under the hood.
- Shows Maven dependency snippet—illustrates how clients integrate.

a. Main components:
- Client Application (any Java project that consumes the library)
- sm-crypto-utils Library API
– Package: crypto.sm2
– Package: crypto.sm3
– Package: crypto.sm4
– Common: SmException
- External Crypto Provider: BouncyCastle
- Build & Release Pipeline
– Maven (pom.xml)
– Shell scripts (build_release.sh, build_snapshots.sh)

b. Relationships & data flow:
1. Client invokes API methods (e.g., SM2.encrypt(), SM3.digest(), SM4.decrypt()).
2. API classes call into BouncyCastle provider classes for low-level crypto operations.
3. API returns results or throws SmException on errors.
4. Build scripts invoke Maven to compile, test, package, and publish snapshots/releases to a repository.

c. Architectural patterns & design principles:
- Modular packaging: separate packages for each algorithm.
- Utility/static-method pattern: no application server or stateful components.
- Exception abstraction: SmException wraps underlying provider errors.
- Dependency inversion: high-level API depends on BC through interfaces provided by the BouncyCastle library.

d. Technologies/frameworks/libraries:
- Java 8+
- Apache Maven
- BouncyCastle (bcprov)
- Shell scripting (bash) for CI/CD

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Application
    ClientApp["Client Application"]

    %% SM-Crypto-Utils Library API
    subgraph "SM-Crypto-Utils Library API"
        subgraph "crypto.sm2"
            DirSM2["crypto.sm2"]:::api
            SM2["SM2.java"]:::api
            SM2Eng["SM2EngineExtend.java"]:::api
            SM2Key["SM2KeyPair.java"]:::api
        end
        subgraph "crypto.sm3"
            SM3["SM3.java"]:::api
        end
        subgraph "crypto.sm4"
            SM4["SM4.java"]:::api
        end
        SmEx["SmException.java"]:::utility
    end

    %% External Dependency
    BCP["BouncyCastle Provider"]:::external

    %% Build & Release Pipeline
    subgraph "Build & Release Pipeline"
        POM["pom.xml"]:::build
        Snap["build_snapshots.sh"]:::build
        Rel["build_release.sh"]:::build
    end

    %% Relationships: Client to API
    ClientApp -->|"encrypt, decrypt, generateKey"| SM2
    ClientApp -->|"digest"| SM3
    ClientApp -->|"encrypt, decrypt"| SM4

    %% API to BouncyCastle
    SM2 -->|"calls BC"| BCP
    SM3 -->|"calls BC"| BCP
    SM4 -->|"calls BC"| BCP

    %% API to Exception
    SM2 -->|"throws SmException"| SmEx
    SM3 -->|"throws SmException"| SmEx
    SM4 -->|"throws SmException"| SmEx

    %% Build Pipeline to Library
    POM -->|"configures build"| SM2
    POM --> SM3
    POM --> SM4
    Snap -->|"build & publish snapshots"| SM2
    Rel -->|"build & release"| SM2

    %% Click Events
    click DirSM2 "https://github.com/zyjnicemoe/sm-crypto-utils/tree/master/src/main/java/cn/zyjblogs/crypto/sm2/"
    click SM2 "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/sm2/SM2.java"
    click SM2Eng "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/sm2/SM2EngineExtend.java"
    click SM2Key "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/sm2/SM2KeyPair.java"
    click SM3 "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/sm3/SM3.java"
    click SM4 "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/sm4/SM4.java"
    click SmEx "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/src/main/java/cn/zyjblogs/crypto/SmException.java"
    click POM "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/pom.xml"
    click Snap "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/build_snapshots.sh"
    click Rel "https://github.com/zyjnicemoe/sm-crypto-utils/blob/master/build_release.sh"

    %% Styles
    classDef api fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef external fill:#90EE90,stroke:#000,stroke-width:1px
    classDef build fill:#FFA500,stroke:#000,stroke-width:1px
    classDef utility fill:#D3D3D3,stroke:#000,stroke-width:1px

    class DirSM2,SM2,SM2Eng,SM2Key,SM3,SM4 api
    class BCP external
    class POM,Snap,Rel build
    class SmEx utility
```
