diff --git a/.github/workflows/nuget_pre.yml b/.github/workflows/nuget_pre.yml index 96303e6..b0b93fc 100644 --- a/.github/workflows/nuget_pre.yml +++ b/.github/workflows/nuget_pre.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/nuget_prod.yml b/.github/workflows/nuget_prod.yml index 999568a..d158e4a 100644 --- a/.github/workflows/nuget_prod.yml +++ b/.github/workflows/nuget_prod.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 0802805..9a45b93 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -13,11 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: - dotnet-version: 6.0.x + dotnet-version: | + 8.0.x + 10.0.x - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.idea/.idea.EasyTool/.idea/.gitignore b/.idea/.idea.EasyTool/.idea/.gitignore new file mode 100644 index 0000000..2e97252 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/.gitignore @@ -0,0 +1,15 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# Rider 忽略的文件 +/projectSettingsUpdater.xml +/.idea.EasyTool.iml +/contentModel.xml +/modules.xml +# 已忽略包含查询文件的默认文件夹 +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/.idea/.idea.EasyTool/.idea/encodings.xml b/.idea/.idea.EasyTool/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.EasyTool/.idea/indexLayout.xml b/.idea/.idea.EasyTool/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.EasyTool/.idea/vcs.xml b/.idea/.idea.EasyTool/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.EasyTool/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.spec-workflow/templates/design-template.md b/.spec-workflow/templates/design-template.md new file mode 100644 index 0000000..1295d7b --- /dev/null +++ b/.spec-workflow/templates/design-template.md @@ -0,0 +1,96 @@ +# Design Document + +## Overview + +[High-level description of the feature and its place in the overall system] + +## Steering Document Alignment + +### Technical Standards (tech.md) +[How the design follows documented technical patterns and standards] + +### Project Structure (structure.md) +[How the implementation will follow project organization conventions] + +## Code Reuse Analysis +[What existing code will be leveraged, extended, or integrated with this feature] + +### Existing Components to Leverage +- **[Component/Utility Name]**: [How it will be used] +- **[Service/Helper Name]**: [How it will be extended] + +### Integration Points +- **[Existing System/API]**: [How the new feature will integrate] +- **[Database/Storage]**: [How data will connect to existing schemas] + +## Architecture + +[Describe the overall architecture and design patterns used] + +### Modular Design Principles +- **Single File Responsibility**: Each file should handle one specific concern or domain +- **Component Isolation**: Create small, focused components rather than large monolithic files +- **Service Layer Separation**: Separate data access, business logic, and presentation layers +- **Utility Modularity**: Break utilities into focused, single-purpose modules + +```mermaid +graph TD + A[Component A] --> B[Component B] + B --> C[Component C] +``` + +## Components and Interfaces + +### Component 1 +- **Purpose:** [What this component does] +- **Interfaces:** [Public methods/APIs] +- **Dependencies:** [What it depends on] +- **Reuses:** [Existing components/utilities it builds upon] + +### Component 2 +- **Purpose:** [What this component does] +- **Interfaces:** [Public methods/APIs] +- **Dependencies:** [What it depends on] +- **Reuses:** [Existing components/utilities it builds upon] + +## Data Models + +### Model 1 +``` +[Define the structure of Model1 in your language] +- id: [unique identifier type] +- name: [string/text type] +- [Additional properties as needed] +``` + +### Model 2 +``` +[Define the structure of Model2 in your language] +- id: [unique identifier type] +- [Additional properties as needed] +``` + +## Error Handling + +### Error Scenarios +1. **Scenario 1:** [Description] + - **Handling:** [How to handle] + - **User Impact:** [What user sees] + +2. **Scenario 2:** [Description] + - **Handling:** [How to handle] + - **User Impact:** [What user sees] + +## Testing Strategy + +### Unit Testing +- [Unit testing approach] +- [Key components to test] + +### Integration Testing +- [Integration testing approach] +- [Key flows to test] + +### End-to-End Testing +- [E2E testing approach] +- [User scenarios to test] diff --git a/.spec-workflow/templates/product-template.md b/.spec-workflow/templates/product-template.md new file mode 100644 index 0000000..82e60de --- /dev/null +++ b/.spec-workflow/templates/product-template.md @@ -0,0 +1,51 @@ +# Product Overview + +## Product Purpose +[Describe the core purpose of this product/project. What problem does it solve?] + +## Target Users +[Who are the primary users of this product? What are their needs and pain points?] + +## Key Features +[List the main features that deliver value to users] + +1. **Feature 1**: [Description] +2. **Feature 2**: [Description] +3. **Feature 3**: [Description] + +## Business Objectives +[What are the business goals this product aims to achieve?] + +- [Objective 1] +- [Objective 2] +- [Objective 3] + +## Success Metrics +[How will we measure the success of this product?] + +- [Metric 1]: [Target] +- [Metric 2]: [Target] +- [Metric 3]: [Target] + +## Product Principles +[Core principles that guide product decisions] + +1. **[Principle 1]**: [Explanation] +2. **[Principle 2]**: [Explanation] +3. **[Principle 3]**: [Explanation] + +## Monitoring & Visibility (if applicable) +[How do users track progress and monitor the system?] + +- **Dashboard Type**: [e.g., Web-based, CLI, Desktop app] +- **Real-time Updates**: [e.g., WebSocket, polling, push notifications] +- **Key Metrics Displayed**: [What information is most important to surface] +- **Sharing Capabilities**: [e.g., read-only links, exports, reports] + +## Future Vision +[Where do we see this product evolving in the future?] + +### Potential Enhancements +- **Remote Access**: [e.g., Tunnel features for sharing dashboards with stakeholders] +- **Analytics**: [e.g., Historical trends, performance metrics] +- **Collaboration**: [e.g., Multi-user support, commenting] diff --git a/.spec-workflow/templates/requirements-template.md b/.spec-workflow/templates/requirements-template.md new file mode 100644 index 0000000..1c80ca0 --- /dev/null +++ b/.spec-workflow/templates/requirements-template.md @@ -0,0 +1,50 @@ +# Requirements Document + +## Introduction + +[Provide a brief overview of the feature, its purpose, and its value to users] + +## Alignment with Product Vision + +[Explain how this feature supports the goals outlined in product.md] + +## Requirements + +### Requirement 1 + +**User Story:** As a [role], I want [feature], so that [benefit] + +#### Acceptance Criteria + +1. WHEN [event] THEN [system] SHALL [response] +2. IF [precondition] THEN [system] SHALL [response] +3. WHEN [event] AND [condition] THEN [system] SHALL [response] + +### Requirement 2 + +**User Story:** As a [role], I want [feature], so that [benefit] + +#### Acceptance Criteria + +1. WHEN [event] THEN [system] SHALL [response] +2. IF [precondition] THEN [system] SHALL [response] + +## Non-Functional Requirements + +### Code Architecture and Modularity +- **Single Responsibility Principle**: Each file should have a single, well-defined purpose +- **Modular Design**: Components, utilities, and services should be isolated and reusable +- **Dependency Management**: Minimize interdependencies between modules +- **Clear Interfaces**: Define clean contracts between components and layers + +### Performance +- [Performance requirements] + +### Security +- [Security requirements] + +### Reliability +- [Reliability requirements] + +### Usability +- [Usability requirements] diff --git a/.spec-workflow/templates/structure-template.md b/.spec-workflow/templates/structure-template.md new file mode 100644 index 0000000..1ab1fbc --- /dev/null +++ b/.spec-workflow/templates/structure-template.md @@ -0,0 +1,145 @@ +# Project Structure + +## Directory Organization + +``` +[Define your project's directory structure. Examples below - adapt to your project type] + +Example for a library/package: +project-root/ +├── src/ # Source code +├── tests/ # Test files +├── docs/ # Documentation +├── examples/ # Usage examples +└── [build/dist/out] # Build output + +Example for an application: +project-root/ +├── [src/app/lib] # Main source code +├── [assets/resources] # Static resources +├── [config/settings] # Configuration +├── [scripts/tools] # Build/utility scripts +└── [tests/spec] # Test files + +Common patterns: +- Group by feature/module +- Group by layer (UI, business logic, data) +- Group by type (models, controllers, views) +- Flat structure for simple projects +``` + +## Naming Conventions + +### Files +- **Components/Modules**: [e.g., `PascalCase`, `snake_case`, `kebab-case`] +- **Services/Handlers**: [e.g., `UserService`, `user_service`, `user-service`] +- **Utilities/Helpers**: [e.g., `dateUtils`, `date_utils`, `date-utils`] +- **Tests**: [e.g., `[filename]_test`, `[filename].test`, `[filename]Test`] + +### Code +- **Classes/Types**: [e.g., `PascalCase`, `CamelCase`, `snake_case`] +- **Functions/Methods**: [e.g., `camelCase`, `snake_case`, `PascalCase`] +- **Constants**: [e.g., `UPPER_SNAKE_CASE`, `SCREAMING_CASE`, `PascalCase`] +- **Variables**: [e.g., `camelCase`, `snake_case`, `lowercase`] + +## Import Patterns + +### Import Order +1. External dependencies +2. Internal modules +3. Relative imports +4. Style imports + +### Module/Package Organization +``` +[Describe your project's import/include patterns] +Examples: +- Absolute imports from project root +- Relative imports within modules +- Package/namespace organization +- Dependency management approach +``` + +## Code Structure Patterns + +[Define common patterns for organizing code within files. Below are examples - choose what applies to your project] + +### Module/Class Organization +``` +Example patterns: +1. Imports/includes/dependencies +2. Constants and configuration +3. Type/interface definitions +4. Main implementation +5. Helper/utility functions +6. Exports/public API +``` + +### Function/Method Organization +``` +Example patterns: +- Input validation first +- Core logic in the middle +- Error handling throughout +- Clear return points +``` + +### File Organization Principles +``` +Choose what works for your project: +- One class/module per file +- Related functionality grouped together +- Public API at the top/bottom +- Implementation details hidden +``` + +## Code Organization Principles + +1. **Single Responsibility**: Each file should have one clear purpose +2. **Modularity**: Code should be organized into reusable modules +3. **Testability**: Structure code to be easily testable +4. **Consistency**: Follow patterns established in the codebase + +## Module Boundaries +[Define how different parts of your project interact and maintain separation of concerns] + +Examples of boundary patterns: +- **Core vs Plugins**: Core functionality vs extensible plugins +- **Public API vs Internal**: What's exposed vs implementation details +- **Platform-specific vs Cross-platform**: OS-specific code isolation +- **Stable vs Experimental**: Production code vs experimental features +- **Dependencies direction**: Which modules can depend on which + +## Code Size Guidelines +[Define your project's guidelines for file and function sizes] + +Suggested guidelines: +- **File size**: [Define maximum lines per file] +- **Function/Method size**: [Define maximum lines per function] +- **Class/Module complexity**: [Define complexity limits] +- **Nesting depth**: [Maximum nesting levels] + +## Dashboard/Monitoring Structure (if applicable) +[How dashboard or monitoring components are organized] + +### Example Structure: +``` +src/ +└── dashboard/ # Self-contained dashboard subsystem + ├── server/ # Backend server components + ├── client/ # Frontend assets + ├── shared/ # Shared types/utilities + └── public/ # Static assets +``` + +### Separation of Concerns +- Dashboard isolated from core business logic +- Own CLI entry point for independent operation +- Minimal dependencies on main application +- Can be disabled without affecting core functionality + +## Documentation Standards +- All public APIs must have documentation +- Complex logic should include inline comments +- README files for major modules +- Follow language-specific documentation conventions diff --git a/.spec-workflow/templates/tasks-template.md b/.spec-workflow/templates/tasks-template.md new file mode 100644 index 0000000..be461de --- /dev/null +++ b/.spec-workflow/templates/tasks-template.md @@ -0,0 +1,139 @@ +# Tasks Document + +- [ ] 1. Create core interfaces in src/types/feature.ts + - File: src/types/feature.ts + - Define TypeScript interfaces for feature data structures + - Extend existing base interfaces from base.ts + - Purpose: Establish type safety for feature implementation + - _Leverage: src/types/base.ts_ + - _Requirements: 1.1_ + - _Prompt: Role: TypeScript Developer specializing in type systems and interfaces | Task: Create comprehensive TypeScript interfaces for the feature data structures following requirements 1.1, extending existing base interfaces from src/types/base.ts | Restrictions: Do not modify existing base interfaces, maintain backward compatibility, follow project naming conventions | Success: All interfaces compile without errors, proper inheritance from base types, full type coverage for feature requirements_ + +- [ ] 2. Create base model class in src/models/FeatureModel.ts + - File: src/models/FeatureModel.ts + - Implement base model extending BaseModel class + - Add validation methods using existing validation utilities + - Purpose: Provide data layer foundation for feature + - _Leverage: src/models/BaseModel.ts, src/utils/validation.ts_ + - _Requirements: 2.1_ + - _Prompt: Role: Backend Developer with expertise in Node.js and data modeling | Task: Create a base model class extending BaseModel and implementing validation following requirement 2.1, leveraging existing patterns from src/models/BaseModel.ts and src/utils/validation.ts | Restrictions: Must follow existing model patterns, do not bypass validation utilities, maintain consistent error handling | Success: Model extends BaseModel correctly, validation methods implemented and tested, follows project architecture patterns_ + +- [ ] 3. Add specific model methods to FeatureModel.ts + - File: src/models/FeatureModel.ts (continue from task 2) + - Implement create, update, delete methods + - Add relationship handling for foreign keys + - Purpose: Complete model functionality for CRUD operations + - _Leverage: src/models/BaseModel.ts_ + - _Requirements: 2.2, 2.3_ + - _Prompt: Role: Backend Developer with expertise in ORM and database operations | Task: Implement CRUD methods and relationship handling in FeatureModel.ts following requirements 2.2 and 2.3, extending patterns from src/models/BaseModel.ts | Restrictions: Must maintain transaction integrity, follow existing relationship patterns, do not duplicate base model functionality | Success: All CRUD operations work correctly, relationships are properly handled, database operations are atomic and efficient_ + +- [ ] 4. Create model unit tests in tests/models/FeatureModel.test.ts + - File: tests/models/FeatureModel.test.ts + - Write tests for model validation and CRUD methods + - Use existing test utilities and fixtures + - Purpose: Ensure model reliability and catch regressions + - _Leverage: tests/helpers/testUtils.ts, tests/fixtures/data.ts_ + - _Requirements: 2.1, 2.2_ + - _Prompt: Role: QA Engineer with expertise in unit testing and Jest/Mocha frameworks | Task: Create comprehensive unit tests for FeatureModel validation and CRUD methods covering requirements 2.1 and 2.2, using existing test utilities from tests/helpers/testUtils.ts and fixtures from tests/fixtures/data.ts | Restrictions: Must test both success and failure scenarios, do not test external dependencies directly, maintain test isolation | Success: All model methods are tested with good coverage, edge cases covered, tests run independently and consistently_ + +- [ ] 5. Create service interface in src/services/IFeatureService.ts + - File: src/services/IFeatureService.ts + - Define service contract with method signatures + - Extend base service interface patterns + - Purpose: Establish service layer contract for dependency injection + - _Leverage: src/services/IBaseService.ts_ + - _Requirements: 3.1_ + - _Prompt: Role: Software Architect specializing in service-oriented architecture and TypeScript interfaces | Task: Design service interface contract following requirement 3.1, extending base service patterns from src/services/IBaseService.ts for dependency injection | Restrictions: Must maintain interface segregation principle, do not expose internal implementation details, ensure contract compatibility with DI container | Success: Interface is well-defined with clear method signatures, extends base service appropriately, supports all required service operations_ + +- [ ] 6. Implement feature service in src/services/FeatureService.ts + - File: src/services/FeatureService.ts + - Create concrete service implementation using FeatureModel + - Add error handling with existing error utilities + - Purpose: Provide business logic layer for feature operations + - _Leverage: src/services/BaseService.ts, src/utils/errorHandler.ts, src/models/FeatureModel.ts_ + - _Requirements: 3.2_ + - _Prompt: Role: Backend Developer with expertise in service layer architecture and business logic | Task: Implement concrete FeatureService following requirement 3.2, using FeatureModel and extending BaseService patterns with proper error handling from src/utils/errorHandler.ts | Restrictions: Must implement interface contract exactly, do not bypass model validation, maintain separation of concerns from data layer | Success: Service implements all interface methods correctly, robust error handling implemented, business logic is well-encapsulated and testable_ + +- [ ] 7. Add service dependency injection in src/utils/di.ts + - File: src/utils/di.ts (modify existing) + - Register FeatureService in dependency injection container + - Configure service lifetime and dependencies + - Purpose: Enable service injection throughout application + - _Leverage: existing DI configuration in src/utils/di.ts_ + - _Requirements: 3.1_ + - _Prompt: Role: DevOps Engineer with expertise in dependency injection and IoC containers | Task: Register FeatureService in DI container following requirement 3.1, configuring appropriate lifetime and dependencies using existing patterns from src/utils/di.ts | Restrictions: Must follow existing DI container patterns, do not create circular dependencies, maintain service resolution efficiency | Success: FeatureService is properly registered and resolvable, dependencies are correctly configured, service lifetime is appropriate for use case_ + +- [ ] 8. Create service unit tests in tests/services/FeatureService.test.ts + - File: tests/services/FeatureService.test.ts + - Write tests for service methods with mocked dependencies + - Test error handling scenarios + - Purpose: Ensure service reliability and proper error handling + - _Leverage: tests/helpers/testUtils.ts, tests/mocks/modelMocks.ts_ + - _Requirements: 3.2, 3.3_ + - _Prompt: Role: QA Engineer with expertise in service testing and mocking frameworks | Task: Create comprehensive unit tests for FeatureService methods covering requirements 3.2 and 3.3, using mocked dependencies from tests/mocks/modelMocks.ts and test utilities | Restrictions: Must mock all external dependencies, test business logic in isolation, do not test framework code | Success: All service methods tested with proper mocking, error scenarios covered, tests verify business logic correctness and error handling_ + +- [ ] 4. Create API endpoints + - Design API structure + - _Leverage: src/api/baseApi.ts, src/utils/apiUtils.ts_ + - _Requirements: 4.0_ + - _Prompt: Role: API Architect specializing in RESTful design and Express.js | Task: Design comprehensive API structure following requirement 4.0, leveraging existing patterns from src/api/baseApi.ts and utilities from src/utils/apiUtils.ts | Restrictions: Must follow REST conventions, maintain API versioning compatibility, do not expose internal data structures directly | Success: API structure is well-designed and documented, follows existing patterns, supports all required operations with proper HTTP methods and status codes_ + +- [ ] 4.1 Set up routing and middleware + - Configure application routes + - Add authentication middleware + - Set up error handling middleware + - _Leverage: src/middleware/auth.ts, src/middleware/errorHandler.ts_ + - _Requirements: 4.1_ + - _Prompt: Role: Backend Developer with expertise in Express.js middleware and routing | Task: Configure application routes and middleware following requirement 4.1, integrating authentication from src/middleware/auth.ts and error handling from src/middleware/errorHandler.ts | Restrictions: Must maintain middleware order, do not bypass security middleware, ensure proper error propagation | Success: Routes are properly configured with correct middleware chain, authentication works correctly, errors are handled gracefully throughout the request lifecycle_ + +- [ ] 4.2 Implement CRUD endpoints + - Create API endpoints + - Add request validation + - Write API integration tests + - _Leverage: src/controllers/BaseController.ts, src/utils/validation.ts_ + - _Requirements: 4.2, 4.3_ + - _Prompt: Role: Full-stack Developer with expertise in API development and validation | Task: Implement CRUD endpoints following requirements 4.2 and 4.3, extending BaseController patterns and using validation utilities from src/utils/validation.ts | Restrictions: Must validate all inputs, follow existing controller patterns, ensure proper HTTP status codes and responses | Success: All CRUD operations work correctly, request validation prevents invalid data, integration tests pass and cover all endpoints_ + +- [ ] 5. Add frontend components + - Plan component architecture + - _Leverage: src/components/BaseComponent.tsx, src/styles/theme.ts_ + - _Requirements: 5.0_ + - _Prompt: Role: Frontend Architect with expertise in React component design and architecture | Task: Plan comprehensive component architecture following requirement 5.0, leveraging base patterns from src/components/BaseComponent.tsx and theme system from src/styles/theme.ts | Restrictions: Must follow existing component patterns, maintain design system consistency, ensure component reusability | Success: Architecture is well-planned and documented, components are properly organized, follows existing patterns and theme system_ + +- [ ] 5.1 Create base UI components + - Set up component structure + - Implement reusable components + - Add styling and theming + - _Leverage: src/components/BaseComponent.tsx, src/styles/theme.ts_ + - _Requirements: 5.1_ + - _Prompt: Role: Frontend Developer specializing in React and component architecture | Task: Create reusable UI components following requirement 5.1, extending BaseComponent patterns and using existing theme system from src/styles/theme.ts | Restrictions: Must use existing theme variables, follow component composition patterns, ensure accessibility compliance | Success: Components are reusable and properly themed, follow existing architecture, accessible and responsive_ + +- [ ] 5.2 Implement feature-specific components + - Create feature components + - Add state management + - Connect to API endpoints + - _Leverage: src/hooks/useApi.ts, src/components/BaseComponent.tsx_ + - _Requirements: 5.2, 5.3_ + - _Prompt: Role: React Developer with expertise in state management and API integration | Task: Implement feature-specific components following requirements 5.2 and 5.3, using API hooks from src/hooks/useApi.ts and extending BaseComponent patterns | Restrictions: Must use existing state management patterns, handle loading and error states properly, maintain component performance | Success: Components are fully functional with proper state management, API integration works smoothly, user experience is responsive and intuitive_ + +- [ ] 6. Integration and testing + - Plan integration approach + - _Leverage: src/utils/integrationUtils.ts, tests/helpers/testUtils.ts_ + - _Requirements: 6.0_ + - _Prompt: Role: Integration Engineer with expertise in system integration and testing strategies | Task: Plan comprehensive integration approach following requirement 6.0, leveraging integration utilities from src/utils/integrationUtils.ts and test helpers | Restrictions: Must consider all system components, ensure proper test coverage, maintain integration test reliability | Success: Integration plan is comprehensive and feasible, all system components work together correctly, integration points are well-tested_ + +- [ ] 6.1 Write end-to-end tests + - Set up E2E testing framework + - Write user journey tests + - Add test automation + - _Leverage: tests/helpers/testUtils.ts, tests/fixtures/data.ts_ + - _Requirements: All_ + - _Prompt: Role: QA Automation Engineer with expertise in E2E testing and test frameworks like Cypress or Playwright | Task: Implement comprehensive end-to-end tests covering all requirements, setting up testing framework and user journey tests using test utilities and fixtures | Restrictions: Must test real user workflows, ensure tests are maintainable and reliable, do not test implementation details | Success: E2E tests cover all critical user journeys, tests run reliably in CI/CD pipeline, user experience is validated from end-to-end_ + +- [ ] 6.2 Final integration and cleanup + - Integrate all components + - Fix any integration issues + - Clean up code and documentation + - _Leverage: src/utils/cleanup.ts, docs/templates/_ + - _Requirements: All_ + - _Prompt: Role: Senior Developer with expertise in code quality and system integration | Task: Complete final integration of all components and perform comprehensive cleanup covering all requirements, using cleanup utilities and documentation templates | Restrictions: Must not break existing functionality, ensure code quality standards are met, maintain documentation consistency | Success: All components are fully integrated and working together, code is clean and well-documented, system meets all requirements and quality standards_ diff --git a/.spec-workflow/templates/tech-template.md b/.spec-workflow/templates/tech-template.md new file mode 100644 index 0000000..57cd538 --- /dev/null +++ b/.spec-workflow/templates/tech-template.md @@ -0,0 +1,99 @@ +# Technology Stack + +## Project Type +[Describe what kind of project this is: web application, CLI tool, desktop application, mobile app, library, API service, embedded system, game, etc.] + +## Core Technologies + +### Primary Language(s) +- **Language**: [e.g., Python 3.11, Go 1.21, TypeScript, Rust, C++] +- **Runtime/Compiler**: [if applicable] +- **Language-specific tools**: [package managers, build tools, etc.] + +### Key Dependencies/Libraries +[List the main libraries and frameworks your project depends on] +- **[Library/Framework name]**: [Purpose and version] +- **[Library/Framework name]**: [Purpose and version] + +### Application Architecture +[Describe how your application is structured - this could be MVC, event-driven, plugin-based, client-server, standalone, microservices, monolithic, etc.] + +### Data Storage (if applicable) +- **Primary storage**: [e.g., PostgreSQL, files, in-memory, cloud storage] +- **Caching**: [e.g., Redis, in-memory, disk cache] +- **Data formats**: [e.g., JSON, Protocol Buffers, XML, binary] + +### External Integrations (if applicable) +- **APIs**: [External services you integrate with] +- **Protocols**: [e.g., HTTP/REST, gRPC, WebSocket, TCP/IP] +- **Authentication**: [e.g., OAuth, API keys, certificates] + +### Monitoring & Dashboard Technologies (if applicable) +- **Dashboard Framework**: [e.g., React, Vue, vanilla JS, terminal UI] +- **Real-time Communication**: [e.g., WebSocket, Server-Sent Events, polling] +- **Visualization Libraries**: [e.g., Chart.js, D3, terminal graphs] +- **State Management**: [e.g., Redux, Vuex, file system as source of truth] + +## Development Environment + +### Build & Development Tools +- **Build System**: [e.g., Make, CMake, Gradle, npm scripts, cargo] +- **Package Management**: [e.g., pip, npm, cargo, go mod, apt, brew] +- **Development workflow**: [e.g., hot reload, watch mode, REPL] + +### Code Quality Tools +- **Static Analysis**: [Tools for code quality and correctness] +- **Formatting**: [Code style enforcement tools] +- **Testing Framework**: [Unit, integration, and/or end-to-end testing tools] +- **Documentation**: [Documentation generation tools] + +### Version Control & Collaboration +- **VCS**: [e.g., Git, Mercurial, SVN] +- **Branching Strategy**: [e.g., Git Flow, GitHub Flow, trunk-based] +- **Code Review Process**: [How code reviews are conducted] + +### Dashboard Development (if applicable) +- **Live Reload**: [e.g., Hot module replacement, file watchers] +- **Port Management**: [e.g., Dynamic allocation, configurable ports] +- **Multi-Instance Support**: [e.g., Running multiple dashboards simultaneously] + +## Deployment & Distribution (if applicable) +- **Target Platform(s)**: [Where/how the project runs: cloud, on-premise, desktop, mobile, embedded] +- **Distribution Method**: [How users get your software: download, package manager, app store, SaaS] +- **Installation Requirements**: [Prerequisites, system requirements] +- **Update Mechanism**: [How updates are delivered] + +## Technical Requirements & Constraints + +### Performance Requirements +- [e.g., response time, throughput, memory usage, startup time] +- [Specific benchmarks or targets] + +### Compatibility Requirements +- **Platform Support**: [Operating systems, architectures, versions] +- **Dependency Versions**: [Minimum/maximum versions of dependencies] +- **Standards Compliance**: [Industry standards, protocols, specifications] + +### Security & Compliance +- **Security Requirements**: [Authentication, encryption, data protection] +- **Compliance Standards**: [GDPR, HIPAA, SOC2, etc. if applicable] +- **Threat Model**: [Key security considerations] + +### Scalability & Reliability +- **Expected Load**: [Users, requests, data volume] +- **Availability Requirements**: [Uptime targets, disaster recovery] +- **Growth Projections**: [How the system needs to scale] + +## Technical Decisions & Rationale +[Document key architectural and technology choices] + +### Decision Log +1. **[Technology/Pattern Choice]**: [Why this was chosen, alternatives considered] +2. **[Architecture Decision]**: [Rationale, trade-offs accepted] +3. **[Tool/Library Selection]**: [Reasoning, evaluation criteria] + +## Known Limitations +[Document any technical debt, limitations, or areas for improvement] + +- [Limitation 1]: [Impact and potential future solutions] +- [Limitation 2]: [Why it exists and when it might be addressed] diff --git a/.spec-workflow/user-templates/README.md b/.spec-workflow/user-templates/README.md new file mode 100644 index 0000000..ad36a48 --- /dev/null +++ b/.spec-workflow/user-templates/README.md @@ -0,0 +1,64 @@ +# User Templates + +This directory allows you to create custom templates that override the default Spec Workflow templates. + +## How to Use Custom Templates + +1. **Create your custom template file** in this directory with the exact same name as the default template you want to override: + - `requirements-template.md` - Override requirements document template + - `design-template.md` - Override design document template + - `tasks-template.md` - Override tasks document template + - `product-template.md` - Override product steering template + - `tech-template.md` - Override tech steering template + - `structure-template.md` - Override structure steering template + +2. **Template Loading Priority**: + - The system first checks this `user-templates/` directory + - If a matching template is found here, it will be used + - Otherwise, the default template from `templates/` will be used + +## Example Custom Template + +To create a custom requirements template: + +1. Create a file named `requirements-template.md` in this directory +2. Add your custom structure, for example: + +```markdown +# Requirements Document + +## Executive Summary +[Your custom section] + +## Business Requirements +[Your custom structure] + +## Technical Requirements +[Your custom fields] + +## Custom Sections +[Add any sections specific to your workflow] +``` + +## Template Variables + +Templates can include placeholders that will be replaced when documents are created: +- `{{projectName}}` - The name of your project +- `{{featureName}}` - The name of the feature being specified +- `{{date}}` - The current date +- `{{author}}` - The document author + +## Best Practices + +1. **Start from defaults**: Copy a default template from `../templates/` as a starting point +2. **Keep structure consistent**: Maintain similar section headers for tool compatibility +3. **Document changes**: Add comments explaining why sections were added/modified +4. **Version control**: Track your custom templates in version control +5. **Test thoroughly**: Ensure custom templates work with the spec workflow tools + +## Notes + +- Custom templates are project-specific and not included in the package distribution +- The `templates/` directory contains the default templates which are updated with each version +- Your custom templates in this directory are preserved during updates +- If a custom template has errors, the system will fall back to the default template diff --git a/EasyTool.Core/AICategory/OpenAIClient.cs b/EasyTool.Core/AICategory/OpenAIClient.cs new file mode 100644 index 0000000..453a339 --- /dev/null +++ b/EasyTool.Core/AICategory/OpenAIClient.cs @@ -0,0 +1,527 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.AICategory +{ + /// + /// OpenAI API 工具类 + /// 提供 GPT、DALL-E、Whisper 等 AI 服务的集成 + /// + public class OpenAIClient + { + private readonly string _apiKey; + private readonly string _baseUrl; + private readonly HttpClient _httpClient; + + /// + /// 创建 OpenAI 客户端 + /// + /// API Key + /// API 基础 URL(默认 OpenAI 官方) + public OpenAIClient(string apiKey, string? baseUrl = null) + { + _apiKey = apiKey; + _baseUrl = baseUrl ?? "https://api.openai.com/v1"; + _httpClient = new HttpClient + { + Timeout = TimeSpan.FromMinutes(5) + }; + _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + } + + #region Chat Completions + + /// + /// 发送聊天请求 + /// + /// 消息列表 + /// 模型名称 + /// 温度(0-2) + /// 最大令牌数 + /// 取消令牌 + /// 响应结果 + public async Task ChatAsync(List messages, string model = "gpt-3.5-turbo", double temperature = 0.7, int? maxTokens = null, CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["messages"] = messages, + ["temperature"] = temperature + }; + + if (maxTokens.HasValue) + requestBody["max_tokens"] = maxTokens.Value; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/chat/completions", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + return JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }) ?? throw new OpenAIException("无法解析响应"); + } + + /// + /// 发送简单聊天请求 + /// + /// 提示词 + /// 模型名称 + /// 温度 + /// 取消令牌 + /// 响应文本 + public async Task ChatSimpleAsync(string prompt, string model = "gpt-3.5-turbo", double temperature = 0.7, CancellationToken cancellationToken = default) + { + var messages = new List + { + new() { Role = "user", Content = prompt } + }; + + var response = await ChatAsync(messages, model, temperature, cancellationToken: cancellationToken); + return response.Choices[0].Message.Content; + } + + /// + /// 流式聊天请求 + /// + public async IAsyncEnumerable ChatStreamAsync(List messages, string model = "gpt-3.5-turbo", double temperature = 0.7, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["messages"] = messages, + ["temperature"] = temperature, + ["stream"] = true + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var request = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/chat/completions") + { + Content = content + }; + + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + response.EnsureSuccessStatusCode(); + + using var stream = await ReadContentAsStreamAsync(response.Content); + using var reader = new StreamReader(stream); + + while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested) + { + var line = await reader.ReadLineAsync(); + if (string.IsNullOrEmpty(line) || !line.StartsWith("data: ")) + continue; + + var data = line.Substring(6); + if (data == "[DONE]") + break; + + var chunkResponse = JsonSerializer.Deserialize(data, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + if (chunkResponse?.Choices?[0]?.Delta?.Content != null) + { + yield return chunkResponse.Choices[0].Delta.Content; + } + } + } + + #endregion + + #region Embeddings + + /// + /// 获取文本嵌入向量 + /// + /// 文本 + /// 模型名称 + /// 取消令牌 + /// 嵌入向量 + public async Task GetEmbeddingAsync(string text, string model = "text-embedding-ada-002", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = text + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/embeddings", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var embeddingResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + return embeddingResponse?.Data?[0]?.Embedding ?? Array.Empty(); + } + + /// + /// 批量获取嵌入向量 + /// + public async Task> GetEmbeddingsAsync(List texts, string model = "text-embedding-ada-002", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = texts + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/embeddings", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var embeddingResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + var result = new List(); + if (embeddingResponse?.Data != null) + { + foreach (var item in embeddingResponse.Data) + { + result.Add(item.Embedding ?? Array.Empty()); + } + } + + return result; + } + + #endregion + + #region Image Generation + + /// + /// 生成图像 + /// + /// 提示词 + /// 尺寸(256x256, 512x512, 1024x1024) + /// 生成数量 + /// 取消令牌 + /// 图像 URL 列表 + public async Task> GenerateImageAsync(string prompt, string size = "1024x1024", int n = 1, CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["prompt"] = prompt, + ["size"] = size, + ["n"] = n + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/images/generations", content, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var imageResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + var result = new List(); + if (imageResponse?.Data != null) + { + foreach (var item in imageResponse.Data) + { + if (!string.IsNullOrEmpty(item.Url)) + result.Add(item.Url); + } + } + + return result; + } + + #endregion + + #region Audio + + /// + /// 语音转文字 + /// + /// 音频文件路径 + /// 模型名称 + /// 语言(如 "zh", "en") + /// 取消令牌 + /// 转录文本 + public async Task TranscribeAsync(string audioFilePath, string model = "whisper-1", string? language = null, CancellationToken cancellationToken = default) + { + using var formContent = new MultipartFormDataContent(); + formContent.Add(new StreamContent(File.OpenRead(audioFilePath)), "file", Path.GetFileName(audioFilePath)); + formContent.Add(new StringContent(model), "model"); + + if (!string.IsNullOrEmpty(language)) + formContent.Add(new StringContent(language), "language"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/audio/transcriptions", formContent, cancellationToken); + var responseJson = await ReadContentAsStringAsync(response.Content); + + if (!response.IsSuccessStatusCode) + { + throw new OpenAIException($"API 请求失败: {response.StatusCode}", responseJson); + } + + var transcriptionResponse = JsonSerializer.Deserialize(responseJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + + return transcriptionResponse?.Text ?? string.Empty; + } + + /// + /// 文字转语音 + /// + /// 文本 + /// 输出文件路径 + /// 模型名称 + /// 声音(alloy, echo, fable, onyx, nova, shimmer) + /// 取消令牌 + /// 是否成功 + public async Task TextToSpeechAsync(string text, string outputFilePath, string model = "tts-1", string voice = "alloy", CancellationToken cancellationToken = default) + { + var requestBody = new Dictionary + { + ["model"] = model, + ["input"] = text, + ["voice"] = voice + }; + + var json = JsonSerializer.Serialize(requestBody); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync($"{_baseUrl}/audio/speech", content, cancellationToken); + + if (!response.IsSuccessStatusCode) + { + var errorJson = await ReadContentAsStringAsync(response.Content); + throw new OpenAIException($"API 请求失败: {response.StatusCode}", errorJson); + } + + var audioData = await ReadContentAsByteArrayAsync(response.Content); + await File.WriteAllBytesAsync(outputFilePath, audioData, cancellationToken); + + return true; + } + + #endregion + + #region Helper Methods + + private static async Task ReadContentAsStringAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsStringAsync(); +#else + return await content.ReadAsStringAsync(default); +#endif + } + + private static async Task ReadContentAsStreamAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsStreamAsync(); +#else + return await content.ReadAsStreamAsync(default); +#endif + } + + private static async Task ReadContentAsByteArrayAsync(HttpContent content) + { +#if NETSTANDARD2_1 + return await content.ReadAsByteArrayAsync(); +#else + return await content.ReadAsByteArrayAsync(default); +#endif + } + + #endregion + } + + #region 数据模型 + + /// + /// 聊天消息 + /// + public class ChatMessage + { + /// + /// 角色(system, user, assistant) + /// + public string Role { get; set; } = string.Empty; + + /// + /// 内容 + /// + public string Content { get; set; } = string.Empty; + } + + /// + /// 聊天响应 + /// + public class ChatResponse + { + /// + /// 响应 ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 选择列表 + /// + public List Choices { get; set; } = new(); + + /// + /// 使用情况 + /// + public UsageInfo? Usage { get; set; } + } + + /// + /// 聊天选择 + /// + public class ChatChoice + { + /// + /// 索引 + /// + public int Index { get; set; } + + /// + /// 消息 + /// + public ChatMessage Message { get; set; } = new(); + + /// + /// 结束原因 + /// + public string? FinishReason { get; set; } + } + + /// + /// 流式响应 + /// + public class ChatStreamResponse + { + public List? Choices { get; set; } + } + + /// + /// 流式选择 + /// + public class ChatStreamChoice + { + public ChatStreamDelta? Delta { get; set; } + } + + /// + /// 流式增量 + /// + public class ChatStreamDelta + { + public string? Content { get; set; } + } + + /// + /// 使用情况 + /// + public class UsageInfo + { + public int PromptTokens { get; set; } + public int CompletionTokens { get; set; } + public int TotalTokens { get; set; } + } + + /// + /// 嵌入响应 + /// + public class EmbeddingResponse + { + public List? Data { get; set; } + } + + /// + /// 嵌入数据 + /// + public class EmbeddingData + { + public float[]? Embedding { get; set; } + } + + /// + /// 图像响应 + /// + public class ImageResponse + { + public List? Data { get; set; } + } + + /// + /// 图像数据 + /// + public class ImageData + { + public string? Url { get; set; } + } + + /// + /// 转录响应 + /// + public class TranscriptionResponse + { + public string? Text { get; set; } + } + + /// + /// OpenAI 异常 + /// + public class OpenAIException : Exception + { + public string? ResponseJson { get; } + + public OpenAIException(string message, string? responseJson = null) : base(message) + { + ResponseJson = responseJson; + } + } + + #endregion +} \ No newline at end of file diff --git a/EasyTool.Core/AICategory/PromptBuilder.cs b/EasyTool.Core/AICategory/PromptBuilder.cs new file mode 100644 index 0000000..64743af --- /dev/null +++ b/EasyTool.Core/AICategory/PromptBuilder.cs @@ -0,0 +1,315 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.AICategory +{ + /// + /// 提示词构建器 + /// 提供构建和管理 AI 提示词的工具 + /// + public class PromptBuilder + { + private readonly StringBuilder _systemPrompt = new(); + private readonly List _examples = new(); + private readonly List _context = new(); + private readonly List _constraints = new(); + private string? _task; + private string? _outputFormat; + + /// + /// 设置系统提示词 + /// + /// 系统提示词 + /// 当前实例 + public PromptBuilder SetSystemPrompt(string systemPrompt) + { + _systemPrompt.Clear(); + _systemPrompt.Append(systemPrompt); + return this; + } + + /// + /// 添加系统提示词 + /// + /// 文本 + /// 当前实例 + public PromptBuilder AddSystemPrompt(string text) + { + _systemPrompt.AppendLine(text); + return this; + } + + /// + /// 设置任务描述 + /// + /// 任务描述 + /// 当前实例 + public PromptBuilder SetTask(string task) + { + _task = task; + return this; + } + + /// + /// 添加示例 + /// + /// 输入示例 + /// 输出示例 + /// 当前实例 + public PromptBuilder AddExample(string input, string output) + { + _examples.Add($"输入: {input}\n输出: {output}"); + return this; + } + + /// + /// 添加上下文 + /// + /// 上下文内容 + /// 当前实例 + public PromptBuilder AddContext(string context) + { + _context.Add(context); + return this; + } + + /// + /// 添加约束条件 + /// + /// 约束条件 + /// 当前实例 + public PromptBuilder AddConstraint(string constraint) + { + _constraints.Add(constraint); + return this; + } + + /// + /// 设置输出格式 + /// + /// 格式描述 + /// 当前实例 + public PromptBuilder SetOutputFormat(string format) + { + _outputFormat = format; + return this; + } + + /// + /// 设置 JSON 输出格式 + /// + /// JSON Schema 或示例 + /// 当前实例 + public PromptBuilder SetJsonOutput(string? schema = null) + { + if (!string.IsNullOrEmpty(schema)) + { + _outputFormat = $"请以 JSON 格式输出,格式如下:\n{schema}"; + } + else + { + _outputFormat = "请以有效的 JSON 格式输出,不要添加任何其他文本或解释。"; + } + return this; + } + + /// + /// 构建最终提示词 + /// + /// 构建的提示词 + public string Build() + { + var result = new StringBuilder(); + + // 系统提示词 + if (_systemPrompt.Length > 0) + { + result.AppendLine(_systemPrompt.ToString()); + result.AppendLine(); + } + + // 任务描述 + if (!string.IsNullOrEmpty(_task)) + { + result.AppendLine("## 任务"); + result.AppendLine(_task); + result.AppendLine(); + } + + // 上下文 + if (_context.Count > 0) + { + result.AppendLine("## 上下文"); + foreach (var ctx in _context) + { + result.AppendLine(ctx); + } + result.AppendLine(); + } + + // 示例 + if (_examples.Count > 0) + { + result.AppendLine("## 示例"); + foreach (var example in _examples) + { + result.AppendLine(example); + result.AppendLine(); + } + } + + // 约束条件 + if (_constraints.Count > 0) + { + result.AppendLine("## 约束条件"); + foreach (var constraint in _constraints) + { + result.AppendLine($"- {constraint}"); + } + result.AppendLine(); + } + + // 输出格式 + if (!string.IsNullOrEmpty(_outputFormat)) + { + result.AppendLine("## 输出格式"); + result.AppendLine(_outputFormat); + } + + return result.ToString().Trim(); + } + + /// + /// 构建消息列表 + /// + /// 用户输入 + /// 消息列表 + public List BuildMessages(string userInput) + { + var messages = new List(); + + // 系统消息 + var systemPrompt = Build(); + if (!string.IsNullOrEmpty(systemPrompt)) + { + messages.Add(new ChatMessage { Role = "system", Content = systemPrompt }); + } + + // 用户消息 + messages.Add(new ChatMessage { Role = "user", Content = userInput }); + + return messages; + } + + /// + /// 清空所有内容 + /// + /// 当前实例 + public PromptBuilder Clear() + { + _systemPrompt.Clear(); + _examples.Clear(); + _context.Clear(); + _constraints.Clear(); + _task = null; + _outputFormat = null; + return this; + } + } + + /// + /// 常用提示词模板 + /// + public static class PromptTemplates + { + /// + /// 代码审查提示词 + /// + public static string CodeReview(string code, string? language = null) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位经验丰富的代码审查专家。") + .SetTask("审查以下代码并提供改进建议。") + .AddConstraint("关注代码质量、性能、安全性") + .AddConstraint("提供具体的改进建议") + .AddConstraint("指出潜在的问题和风险"); + + if (!string.IsNullOrEmpty(language)) + { + builder.AddContext($"编程语言: {language}"); + } + + builder.SetOutputFormat("请按以下格式输出:\n1. 问题列表\n2. 改进建议\n3. 重构后的代码(如有必要)"); + + var messages = builder.BuildMessages($"待审查的代码:\n```\n{code}\n```"); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 翻译提示词 + /// + public static string Translate(string text, string sourceLanguage, string targetLanguage) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位专业的翻译专家,精通多种语言。") + .SetTask($"将以下文本从{sourceLanguage}翻译成{targetLanguage}。") + .AddConstraint("保持原文的语气和风格") + .AddConstraint("确保翻译准确、自然、流畅") + .AddConstraint("保留专业术语的准确性"); + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 摘要生成提示词 + /// + public static string Summarize(string text, int? maxLength = null) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位专业的文本摘要专家。") + .SetTask("请为以下文本生成简洁的摘要。") + .AddConstraint("保留关键信息和要点") + .AddConstraint("语言简洁明了"); + + if (maxLength.HasValue) + { + builder.AddConstraint($"摘要长度不超过{maxLength.Value}字"); + } + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 数据提取提示词 + /// + public static string ExtractData(string text, string[] fields) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位数据提取专家。") + .SetTask("从以下文本中提取指定的数据字段。") + .SetJsonOutput($"{{\"fields\": [{string.Join(", ", fields)}]}}"); + + var messages = builder.BuildMessages(text); + return messages[0].Content + "\n\n" + messages[1].Content; + } + + /// + /// 问答提示词 + /// + public static string QuestionAnswer(string context, string question) + { + var builder = new PromptBuilder() + .AddSystemPrompt("你是一位知识渊博的助手,根据给定的上下文回答问题。") + .SetTask("根据提供的上下文回答用户的问题。") + .AddConstraint("只根据上下文内容回答") + .AddConstraint("如果上下文中没有相关信息,请明确说明") + .AddContext($"上下文:\n{context}"); + + var messages = builder.BuildMessages(question); + return messages[0].Content + "\n\n" + messages[1].Content; + } + } +} diff --git a/EasyTool.Core/AICategory/VectorSimilarity.cs b/EasyTool.Core/AICategory/VectorSimilarity.cs new file mode 100644 index 0000000..b9eb150 --- /dev/null +++ b/EasyTool.Core/AICategory/VectorSimilarity.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.AICategory +{ + /// + /// 向量相似度计算工具 + /// 用于计算嵌入向量之间的相似度 + /// + public static class VectorSimilarity + { + /// + /// 计算余弦相似度 + /// + /// 向量1 + /// 向量2 + /// 相似度(-1到1) + public static double CosineSimilarity(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double dotProduct = 0; + double magnitude1 = 0; + double magnitude2 = 0; + + for (int i = 0; i < vector1.Length; i++) + { + dotProduct += vector1[i] * vector2[i]; + magnitude1 += vector1[i] * vector1[i]; + magnitude2 += vector2[i] * vector2[i]; + } + + magnitude1 = Math.Sqrt(magnitude1); + magnitude2 = Math.Sqrt(magnitude2); + + if (magnitude1 == 0 || magnitude2 == 0) + return 0; + + return dotProduct / (magnitude1 * magnitude2); + } + + /// + /// 计算欧几里得距离 + /// + /// 向量1 + /// 向量2 + /// 距离 + public static double EuclideanDistance(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double sum = 0; + for (int i = 0; i < vector1.Length; i++) + { + var diff = vector1[i] - vector2[i]; + sum += diff * diff; + } + + return Math.Sqrt(sum); + } + + /// + /// 计算点积 + /// + /// 向量1 + /// 向量2 + /// 点积 + public static double DotProduct(float[] vector1, float[] vector2) + { + if (vector1.Length != vector2.Length) + throw new ArgumentException("向量长度必须相同"); + + double sum = 0; + for (int i = 0; i < vector1.Length; i++) + { + sum += vector1[i] * vector2[i]; + } + + return sum; + } + + /// + /// 归一化向量 + /// + /// 向量 + /// 归一化后的向量 + public static float[] Normalize(float[] vector) + { + double magnitude = 0; + for (int i = 0; i < vector.Length; i++) + { + magnitude += vector[i] * vector[i]; + } + + magnitude = Math.Sqrt(magnitude); + if (magnitude == 0) + return new float[vector.Length]; + + var result = new float[vector.Length]; + for (int i = 0; i < vector.Length; i++) + { + result[i] = (float)(vector[i] / magnitude); + } + + return result; + } + + /// + /// 查找最相似的向量 + /// + /// 查询向量 + /// 候选向量列表 + /// 返回数量 + /// 最相似向量的索引和相似度 + public static List<(int Index, double Similarity)> FindMostSimilar(float[] query, List candidates, int topK = 5) + { + var similarities = new List<(int Index, double Similarity)>(); + + for (int i = 0; i < candidates.Count; i++) + { + var similarity = CosineSimilarity(query, candidates[i]); + similarities.Add((i, similarity)); + } + + return similarities + .OrderByDescending(x => x.Similarity) + .Take(topK) + .ToList(); + } + } + + /// + /// 简单的向量存储 + /// 用于存储和检索嵌入向量 + /// + public class VectorStore + { + private readonly List _items = new(); + + /// + /// 添加向量 + /// + /// ID + /// 向量 + /// 元数据 + public void Add(string id, float[] vector, Dictionary? metadata = null) + { + _items.Add(new VectorItem + { + Id = id, + Vector = vector, + Metadata = metadata ?? new Dictionary() + }); + } + + /// + /// 批量添加向量 + /// + public void AddRange(IEnumerable<(string Id, float[] Vector, Dictionary? Metadata)> items) + { + foreach (var item in items) + { + Add(item.Id, item.Vector, item.Metadata); + } + } + + /// + /// 搜索相似向量 + /// + /// 查询向量 + /// 返回数量 + /// 最小相似度 + /// 搜索结果 + public List Search(float[] query, int topK = 5, double minScore = 0) + { + var results = new List(); + + foreach (var item in _items) + { + var score = VectorSimilarity.CosineSimilarity(query, item.Vector); + if (score >= minScore) + { + results.Add(new VectorSearchResult + { + Id = item.Id, + Score = score, + Metadata = item.Metadata + }); + } + } + + return results + .OrderByDescending(x => x.Score) + .Take(topK) + .ToList(); + } + + /// + /// 删除向量 + /// + /// ID + /// 是否删除成功 + public bool Remove(string id) + { + var item = _items.FirstOrDefault(x => x.Id == id); + if (item != null) + { + _items.Remove(item); + return true; + } + return false; + } + + /// + /// 清空所有向量 + /// + public void Clear() + { + _items.Clear(); + } + + /// + /// 获取向量数量 + /// + public int Count => _items.Count; + + /// + /// 获取所有 ID + /// + public IEnumerable GetAllIds() => _items.Select(x => x.Id); + } + + /// + /// 向量项 + /// + internal class VectorItem + { + public string Id { get; set; } = string.Empty; + public float[] Vector { get; set; } = Array.Empty(); + public Dictionary Metadata { get; set; } = new(); + } + + /// + /// 向量搜索结果 + /// + public class VectorSearchResult + { + /// + /// ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 相似度分数 + /// + public double Score { get; set; } + + /// + /// 元数据 + /// + public Dictionary Metadata { get; set; } = new(); + } +} diff --git a/EasyTool.Core/BusinessCategory/BankCardUtil.cs b/EasyTool.Core/BusinessCategory/BankCardUtil.cs new file mode 100644 index 0000000..19e6c57 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/BankCardUtil.cs @@ -0,0 +1,491 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 银行卡类型枚举 + /// + public enum BankType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 借记卡 + /// + Debit = 1, + + /// + /// 信用卡 + /// + Credit = 2 + } + + /// + /// 银行信息 + /// + public class BankInfo + { + /// + /// 银行名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 卡类型 + /// + public BankType Type { get; set; } + + /// + /// 银行缩写代码 + /// + public string Code { get; set; } = string.Empty; + } + + /// + /// 银行卡工具类 + /// + public static class BankCardUtil + { + #region 常量与私有字段 + + /// + /// 银行卡号正则表达式(13-19位数字) + /// + private static readonly Regex BankCardRegex = new(@"^\d{13,19}$", RegexOptions.Compiled); + + /// + /// 银行BIN码映射(前6位 -> 银行信息) + /// 注:此处仅包含部分常见银行BIN码,实际应用中应使用完整的BIN码库 + /// + private static readonly Dictionary BinCodeMapping = new() + { + // 工商银行 + { "622202", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622203", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622204", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622205", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622206", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622207", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622208", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622209", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622210", new BankInfo { Name = "中国工商银行", Type = BankType.Debit, Code = "ICBC" } }, + { "622588", new BankInfo { Name = "中国工商银行", Type = BankType.Credit, Code = "ICBC" } }, + + // 农业银行 + { "622848", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622849", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622845", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622846", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622847", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622821", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622822", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622823", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622824", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + { "622825", new BankInfo { Name = "中国农业银行", Type = BankType.Debit, Code = "ABC" } }, + + // 中国银行 + { "621660", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621661", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621662", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621663", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621665", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621666", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621667", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621668", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + { "621669", new BankInfo { Name = "中国银行", Type = BankType.Debit, Code = "BOC" } }, + + // 建设银行 + { "622700", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "622707", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "622708", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "621081", new BankInfo { Name = "中国建设银行", Type = BankType.Debit, Code = "CCB" } }, + { "436742", new BankInfo { Name = "中国建设银行", Type = BankType.Credit, Code = "CCB" } }, + { "436745", new BankInfo { Name = "中国建设银行", Type = BankType.Credit, Code = "CCB" } }, + + // 交通银行 + { "622260", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622261", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622262", new BankInfo { Name = "交通银行", Type = BankType.Debit, Code = "BOCOM" } }, + { "622521", new BankInfo { Name = "交通银行", Type = BankType.Credit, Code = "BOCOM" } }, + { "622522", new BankInfo { Name = "交通银行", Type = BankType.Credit, Code = "BOCOM" } }, + + // 招商银行 + { "622580", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622581", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622582", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + { "622588", new BankInfo { Name = "招商银行", Type = BankType.Credit, Code = "CMB" } }, + { "622589", new BankInfo { Name = "招商银行", Type = BankType.Credit, Code = "CMB" } }, + { "621286", new BankInfo { Name = "招商银行", Type = BankType.Debit, Code = "CMB" } }, + + // 浦发银行 + { "622518", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + { "622519", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + { "622520", new BankInfo { Name = "浦发银行", Type = BankType.Credit, Code = "SPDB" } }, + { "621228", new BankInfo { Name = "浦发银行", Type = BankType.Debit, Code = "SPDB" } }, + + // 民生银行 + { "622615", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622617", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622618", new BankInfo { Name = "民生银行", Type = BankType.Debit, Code = "CMBC" } }, + { "622620", new BankInfo { Name = "民生银行", Type = BankType.Credit, Code = "CMBC" } }, + { "622622", new BankInfo { Name = "民生银行", Type = BankType.Credit, Code = "CMBC" } }, + + // 兴业银行 + { "622909", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622910", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622911", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622912", new BankInfo { Name = "兴业银行", Type = BankType.Debit, Code = "CIB" } }, + { "622918", new BankInfo { Name = "兴业银行", Type = BankType.Credit, Code = "CIB" } }, + + // 中信银行 + { "622690", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622691", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622692", new BankInfo { Name = "中信银行", Type = BankType.Debit, Code = "CITIC" } }, + { "622696", new BankInfo { Name = "中信银行", Type = BankType.Credit, Code = "CITIC" } }, + { "622698", new BankInfo { Name = "中信银行", Type = BankType.Credit, Code = "CITIC" } }, + + // 光大银行 + { "622655", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622656", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622657", new BankInfo { Name = "光大银行", Type = BankType.Debit, Code = "CEB" } }, + { "622658", new BankInfo { Name = "光大银行", Type = BankType.Credit, Code = "CEB" } }, + { "622685", new BankInfo { Name = "光大银行", Type = BankType.Credit, Code = "CEB" } }, + + // 平安银行 + { "622155", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622156", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622157", new BankInfo { Name = "平安银行", Type = BankType.Debit, Code = "PAB" } }, + { "622525", new BankInfo { Name = "平安银行", Type = BankType.Credit, Code = "PAB" } }, + { "622526", new BankInfo { Name = "平安银行", Type = BankType.Credit, Code = "PAB" } }, + + // 华夏银行 + { "622630", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + { "622631", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + { "622632", new BankInfo { Name = "华夏银行", Type = BankType.Debit, Code = "HXB" } }, + + // 广发银行 + { "622568", new BankInfo { Name = "广发银行", Type = BankType.Debit, Code = "CGB" } }, + { "622569", new BankInfo { Name = "广发银行", Type = BankType.Debit, Code = "CGB" } }, + { "622570", new BankInfo { Name = "广发银行", Type = BankType.Credit, Code = "CGB" } }, + { "622575", new BankInfo { Name = "广发银行", Type = BankType.Credit, Code = "CGB" } }, + + // 邮储银行 + { "622150", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622151", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622180", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622181", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + { "622188", new BankInfo { Name = "邮储银行", Type = BankType.Debit, Code = "PSBC" } }, + + // 北京银行 + { "622309", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + { "622310", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + { "622311", new BankInfo { Name = "北京银行", Type = BankType.Debit, Code = "BJBANK" } }, + + // 上海银行 + { "622462", new BankInfo { Name = "上海银行", Type = BankType.Debit, Code = "SHBANK" } }, + { "622463", new BankInfo { Name = "上海银行", Type = BankType.Debit, Code = "SHBANK" } }, + }; + + #endregion + + #region 验证方法 + + /// + /// 验证银行卡号是否有效(格式 + Luhn校验) + /// + /// 银行卡号 + /// 是否有效 + public static bool IsValid(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return false; + } + + return ValidateLuhn(cardNumber); + } + + /// + /// 仅验证银行卡号格式(不包含Luhn校验) + /// + /// 银行卡号 + /// 格式是否有效 + public static bool IsValidFormat(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + return BankCardRegex.IsMatch(cardNumber); + } + + /// + /// 使用Luhn算法验证银行卡号 + /// + /// 银行卡号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + int sum = 0; + int length = cardNumber.Length; + bool isEvenPosition = false; + + // 从右向左遍历 + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(cardNumber[i])) + { + return false; + } + + int digit = cardNumber[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return sum % 10 == 0; + } + + /// + /// 计算Luhn校验位 + /// + /// 不含校验位的银行卡号 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateLuhnCheckDigit(string? cardNumberWithoutCheckDigit) + { + if (string.IsNullOrWhiteSpace(cardNumberWithoutCheckDigit)) + { + return -1; + } + + // 在末尾添加一个临时校验位0 + string tempCardNumber = cardNumberWithoutCheckDigit + "0"; + int sum = 0; + int length = tempCardNumber.Length; + bool isEvenPosition = false; + + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(tempCardNumber[i])) + { + return -1; + } + + int digit = tempCardNumber[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 银行信息查询 + + /// + /// 获取银行信息 + /// + /// 银行卡号 + /// 银行信息,未找到返回null + public static BankInfo? GetBankInfo(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber) || cardNumber.Length < 6) + { + return null; + } + + // 尝试匹配6位BIN码 + string bin6 = cardNumber.Substring(0, 6); + if (BinCodeMapping.TryGetValue(bin6, out BankInfo? info)) + { + return info; + } + + // 尝试匹配5位BIN码 + if (cardNumber.Length >= 5) + { + string bin5 = cardNumber.Substring(0, 5); + if (BinCodeMapping.TryGetValue(bin5, out info)) + { + return info; + } + } + + // 尝试匹配4位BIN码 + if (cardNumber.Length >= 4) + { + string bin4 = cardNumber.Substring(0, 4); + if (BinCodeMapping.TryGetValue(bin4, out info)) + { + return info; + } + } + + return null; + } + + /// + /// 获取银行名称 + /// + /// 银行卡号 + /// 银行名称 + public static string? GetBankName(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Name; + } + + /// + /// 获取银行缩写代码 + /// + /// 银行卡号 + /// 银行缩写代码 + public static string? GetBankCode(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Code; + } + + /// + /// 获取卡类型 + /// + /// 银行卡号 + /// 卡类型 + public static BankType GetBankType(string? cardNumber) + { + return GetBankInfo(cardNumber)?.Type ?? BankType.Unknown; + } + + /// + /// 判断是否为借记卡 + /// + /// 银行卡号 + /// 是否为借记卡 + public static bool IsDebitCard(string? cardNumber) + { + return GetBankType(cardNumber) == BankType.Debit; + } + + /// + /// 判断是否为信用卡 + /// + /// 银行卡号 + /// 是否为信用卡 + public static bool IsCreditCard(string? cardNumber) + { + return GetBankType(cardNumber) == BankType.Credit; + } + + /// + /// 获取BIN码(前6位) + /// + /// 银行卡号 + /// BIN码 + public static string? GetBinCode(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber) || cardNumber.Length < 6) + { + return null; + } + + return cardNumber.Substring(0, 6); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化银行卡号(每4位一组,空格分隔) + /// + /// 银行卡号 + /// 格式化后的卡号 + public static string? Format(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return null; + } + + // 移除非数字字符 + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + + if (!IsValidFormat(cleaned)) + { + return null; + } + + // 每4位分组 + var groups = new List(); + for (int i = 0; i < cleaned.Length; i += 4) + { + int length = Math.Min(4, cleaned.Length - i); + groups.Add(cleaned.Substring(i, length)); + } + + return string.Join(" ", groups); + } + + /// + /// 银行卡号脱敏:6222****5678 + /// + /// 银行卡号 + /// 脱敏后的卡号 + public static string? Mask(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return null; + } + + // 移除非数字字符 + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + + if (cleaned.Length < 8) + { + return null; + } + + int prefixLength = 4; + int suffixLength = 4; + + string prefix = cleaned.Substring(0, prefixLength); + string suffix = cleaned.Substring(cleaned.Length - suffixLength, suffixLength); + int maskLength = cleaned.Length - prefixLength - suffixLength; + + return prefix + new string('*', maskLength) + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/BarcodeUtil.cs b/EasyTool.Core/BusinessCategory/BarcodeUtil.cs new file mode 100644 index 0000000..e541ba5 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/BarcodeUtil.cs @@ -0,0 +1,651 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 条形码类型枚举 + /// + public enum BarcodeType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// EAN-13(13位国际商品条码) + /// + EAN13 = 1, + + /// + /// EAN-8(8位商品条码) + /// + EAN8 = 2, + + /// + /// UPC-A(12位北美商品条码) + /// + UPCA = 3, + + /// + /// UPC-E(6位压缩商品条码) + /// + UPCE = 4, + + /// + /// ITF-14(14位物流包装条码) + /// + ITF14 = 5, + + /// + /// Code128(可变长度工业条码) + /// + Code128 = 6 + } + + /// + /// 条形码工具类 + /// + public static class BarcodeUtil + { + #region 常量与私有字段 + + /// + /// EAN-13正则表达式 + /// + private static readonly Regex EAN13Regex = new(@"^\d{13}$", RegexOptions.Compiled); + + /// + /// EAN-8正则表达式 + /// + private static readonly Regex EAN8Regex = new(@"^\d{8}$", RegexOptions.Compiled); + + /// + /// UPC-A正则表达式 + /// + private static readonly Regex UPCARegex = new(@"^\d{12}$", RegexOptions.Compiled); + + /// + /// UPC-E正则表达式 + /// + private static readonly Regex UPCERegex = new(@"^\d{6}$", RegexOptions.Compiled); + + /// + /// ITF-14正则表达式 + /// + private static readonly Regex ITF14Regex = new(@"^\d{14}$", RegexOptions.Compiled); + + /// + /// 国家代码(GS1前缀)与地区映射 + /// + private static readonly (string Prefix, string Region)[] Gs1PrefixMap = + { + ("000", "美国/加拿大"), ("001", "美国/加拿大"), ("019", "美国/加拿大"), + ("020", "店内码"), ("029", "店内码"), + ("030", "美国/加拿大"), ("039", "美国/加拿大"), + ("040", "店内码"), ("049", "店内码"), + ("050", "优惠券"), ("099", "优惠券"), + ("100", "美国/加拿大"), ("139", "美国/加拿大"), + ("200", "店内码"), ("299", "店内码"), + ("300", "法国"), ("379", "法国"), + ("380", "保加利亚"), + ("383", "斯洛文尼亚"), + ("385", "克罗地亚"), + ("387", "波黑"), + ("400", "德国"), ("440", "德国"), + ("450", "日本"), ("459", "日本"), + ("460", "俄罗斯"), ("469", "俄罗斯"), + ("470", "吉尔吉斯斯坦"), + ("471", "台湾"), + ("474", "爱沙尼亚"), + ("475", "拉脱维亚"), + ("476", "阿塞拜疆"), + ("477", "立陶宛"), + ("478", "乌兹别克斯坦"), + ("479", "斯里兰卡"), + ("480", "菲律宾"), + ("481", "白俄罗斯"), + ("482", "乌克兰"), + ("483", "土库曼斯坦"), + ("484", "摩尔多瓦"), + ("485", "亚美尼亚"), + ("486", "格鲁吉亚"), + ("487", "哈萨克斯坦"), + ("488", "塔吉克斯坦"), + ("489", "香港"), + ("490", "日本"), ("499", "日本"), + ("500", "英国"), ("509", "英国"), + ("520", "希腊"), + ("528", "黎巴嫩"), + ("529", "塞浦路斯"), + ("530", "阿尔巴尼亚"), + ("531", "马其顿"), + ("535", "马耳他"), + ("539", "爱尔兰"), + ("540", "比利时/卢森堡"), ("549", "比利时/卢森堡"), + ("560", "葡萄牙"), + ("569", "冰岛"), + ("570", "丹麦"), ("579", "丹麦"), + ("590", "波兰"), + ("594", "罗马尼亚"), + ("599", "匈牙利"), + ("600", "南非"), ("601", "南非"), + ("603", "加纳"), + ("604", "塞内加尔"), + ("608", "巴林"), + ("609", "毛里求斯"), + ("611", "摩洛哥"), + ("613", "阿尔及利亚"), + ("615", "尼日利亚"), + ("616", "肯尼亚"), + ("618", "科特迪瓦"), + ("619", "突尼斯"), + ("621", "叙利亚"), + ("622", "埃及"), + ("624", "利比亚"), + ("625", "约旦"), + ("626", "伊朗"), + ("627", "科威特"), + ("628", "沙特阿拉伯"), + ("629", "阿联酋"), + ("640", "芬兰"), ("649", "芬兰"), + ("690", "中国"), ("699", "中国"), + ("700", "挪威"), ("709", "挪威"), + ("729", "以色列"), + ("730", "瑞典"), ("739", "瑞典"), + ("740", "危地马拉"), + ("741", "萨尔瓦多"), + ("742", "洪都拉斯"), + ("743", "尼加拉瓜"), + ("744", "哥斯达黎加"), + ("745", "巴拿马"), + ("746", "多米尼加"), + ("750", "墨西哥"), + ("754", "加拿大"), ("755", "加拿大"), + ("759", "委内瑞拉"), + ("760", "瑞士"), ("769", "瑞士"), + ("770", "哥伦比亚"), + ("773", "乌拉圭"), + ("775", "秘鲁"), + ("777", "玻利维亚"), + ("779", "阿根廷"), + ("780", "智利"), + ("784", "巴拉圭"), + ("786", "厄瓜多尔"), + ("789", "巴西"), ("790", "巴西"), + ("800", "意大利"), ("839", "意大利"), + ("840", "美国"), ("849", "美国"), + ("850", "古巴"), + ("858", "斯洛伐克"), + ("859", "捷克"), + ("860", "塞尔维亚"), + ("865", "蒙古"), + ("867", "朝鲜"), + ("868", "土耳其"), ("869", "土耳其"), + ("870", "荷兰"), ("879", "荷兰"), + ("880", "韩国"), + ("884", "柬埔寨"), + ("885", "泰国"), + ("888", "新加坡"), + ("890", "印度"), + ("893", "越南"), + ("896", "巴基斯坦"), + ("899", "印度尼西亚"), + ("900", "奥地利"), ("919", "奥地利"), + ("930", "澳大利亚"), ("939", "澳大利亚"), + ("940", "新西兰"), ("949", "新西兰"), + ("950", "国际组织"), + ("951", "国际组织"), + ("955", "马来西亚"), + ("958", "澳门") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证条形码是否有效(自动识别类型) + /// + /// 条形码 + /// 是否有效 + public static bool IsValid(string? barcode) + { + return IsValidEAN13(barcode) || IsValidEAN8(barcode) || + IsValidUPCA(barcode) || IsValidUPCE(barcode) || + IsValidITF14(barcode); + } + + /// + /// 验证EAN-13条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidEAN13(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !EAN13Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 13); + } + + /// + /// 验证EAN-8条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidEAN8(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !EAN8Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 8); + } + + /// + /// 验证UPC-A条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidUPCA(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !UPCARegex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 12); + } + + /// + /// 验证UPC-E条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidUPCE(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !UPCERegex.IsMatch(barcode)) + { + return false; + } + + // UPC-E需要展开为UPC-A后验证 + string? expanded = ExpandUPCE(barcode); + return expanded != null && IsValidUPCA(expanded); + } + + /// + /// 验证ITF-14条形码是否有效 + /// + /// 条形码 + /// 是否有效 + public static bool IsValidITF14(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || !ITF14Regex.IsMatch(barcode)) + { + return false; + } + + return ValidateChecksum(barcode, 14); + } + + /// + /// 验证校验位 + /// + private static bool ValidateChecksum(string barcode, int length) + { + int sum = 0; + for (int i = 0; i < length - 1; i++) + { + int digit = barcode[i] - '0'; + // 从右向左,偶数位权重为3,奇数位权重为1 + int weight = ((length - 1 - i) % 2 == 1) ? 3 : 1; + sum += digit * weight; + } + + int checkDigit = (10 - (sum % 10)) % 10; + return checkDigit == (barcode[length - 1] - '0'); + } + + /// + /// 计算校验位 + /// + /// 不含校验位的条形码 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateCheckDigit(string? barcodeWithoutCheck) + { + if (string.IsNullOrWhiteSpace(barcodeWithoutCheck)) + { + return -1; + } + + int length = barcodeWithoutCheck.Length; + int sum = 0; + for (int i = 0; i < length; i++) + { + if (!char.IsDigit(barcodeWithoutCheck[i])) + { + return -1; + } + int digit = barcodeWithoutCheck[i] - '0'; + // 从右向左,偶数位权重为3 + int weight = ((length - i) % 2 == 0) ? 3 : 1; + sum += digit * weight; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 类型识别 + + /// + /// 获取条形码类型 + /// + /// 条形码 + /// 条形码类型 + public static BarcodeType GetBarcodeType(string? barcode) + { + if (IsValidEAN13(barcode)) return BarcodeType.EAN13; + if (IsValidEAN8(barcode)) return BarcodeType.EAN8; + if (IsValidUPCA(barcode)) return BarcodeType.UPCA; + if (IsValidUPCE(barcode)) return BarcodeType.UPCE; + if (IsValidITF14(barcode)) return BarcodeType.ITF14; + return BarcodeType.Unknown; + } + + /// + /// 获取条形码类型名称 + /// + /// 条形码类型 + /// 类型名称 + public static string GetBarcodeTypeName(BarcodeType type) + { + return type switch + { + BarcodeType.EAN13 => "EAN-13", + BarcodeType.EAN8 => "EAN-8", + BarcodeType.UPCA => "UPC-A", + BarcodeType.UPCE => "UPC-E", + BarcodeType.ITF14 => "ITF-14", + BarcodeType.Code128 => "Code 128", + _ => "未知" + }; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国家/地区(根据GS1前缀) + /// + /// 条形码 + /// 国家/地区名称 + public static string? GetRegion(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || barcode.Length < 3) + { + return null; + } + + string prefix3 = barcode.Substring(0, 3); + string prefix2 = barcode.Substring(0, 2); + string prefix1 = barcode.Substring(0, 1); + + // 先匹配3位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix3) + { + return mapping.Region; + } + } + + // 再匹配2位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix2) + { + return mapping.Region; + } + } + + // 最后匹配1位前缀 + foreach (var mapping in Gs1PrefixMap) + { + if (mapping.Prefix == prefix1) + { + return mapping.Region; + } + } + + return null; + } + + /// + /// 判断是否为中国商品条码 + /// + /// 条形码 + /// 是否为中国商品条码 + public static bool IsChinaBarcode(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode) || barcode.Length < 3) + { + return false; + } + + string prefix = barcode.Substring(0, 3); + return prefix.CompareTo("690") >= 0 && prefix.CompareTo("699") <= 0; + } + + /// + /// 获取厂商识别代码(EAN-13的前7-9位) + /// + /// 条形码 + /// 厂商识别代码 + public static string? GetManufacturerCode(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + // EAN-13:前缀(2-3位) + 厂商代码(4-5位) + 商品代码(5位) + 校验位 + // 简化处理:返回前8位(不含校验位) + return barcode!.Substring(0, 8); + } + + /// + /// 获取商品项目代码(EAN-13的第9-12位) + /// + /// 条形码 + /// 商品项目代码 + public static string? GetProductCode(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + return barcode!.Substring(8, 4); + } + + #endregion + + #region 转换方法 + + /// + /// 将UPC-E转换为UPC-A + /// + /// UPC-E条形码 + /// UPC-A条形码,转换失败返回null + public static string? ExpandUPCE(string? upce) + { + if (string.IsNullOrWhiteSpace(upce) || upce.Length != 6 || !UPCERegex.IsMatch(upce)) + { + return null; + } + + char lastDigit = upce[5]; + string result; + + switch (lastDigit) + { + case '0': + result = upce[0] + upce[1].ToString() + "00000" + upce[2] + upce[3] + upce[4]; + break; + case '1': + result = upce[0] + upce[1].ToString() + "10000" + upce[2] + upce[3] + upce[4]; + break; + case '2': + result = upce[0] + upce[1].ToString() + "20000" + upce[2] + upce[3] + upce[4]; + break; + case '3': + result = upce[0] + upce[1].ToString() + upce[2] + "00000" + upce[3] + upce[4]; + break; + case '4': + result = upce[0] + upce[1].ToString() + upce[2] + upce[3] + "00000" + upce[4]; + break; + default: + result = upce[0] + upce[1].ToString() + upce[2] + upce[3] + upce[4] + "0000" + lastDigit; + break; + } + + // 添加系统字符(0)和计算校验位 + string fullCode = "0" + result; + int checkDigit = CalculateCheckDigit(fullCode); + return checkDigit >= 0 ? fullCode + checkDigit : null; + } + + /// + /// 将UPC-A转换为EAN-13 + /// + /// UPC-A条形码 + /// EAN-13条形码 + public static string? ConvertUPCAToEAN13(string? upca) + { + if (!IsValidUPCA(upca)) + { + return null; + } + + return "0" + upca; + } + + /// + /// 将EAN-13转换为EAN-8(仅当适用于短码时) + /// + /// EAN-13条形码 + /// EAN-8条形码,不适用返回null + public static string? ConvertEAN13ToEAN8(string? ean13) + { + if (!IsValidEAN13(ean13)) + { + return null; + } + + // 只有特定前缀的EAN-13才能转换为EAN-8 + // 简化处理:仅支持部分转换 + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化条形码(去除非数字字符) + /// + /// 条形码 + /// 格式化后的条形码 + public static string? Normalize(string? barcode) + { + if (string.IsNullOrWhiteSpace(barcode)) + { + return null; + } + + string cleaned = Regex.Replace(barcode, @"\D", ""); + return cleaned.Length >= 6 ? cleaned : null; + } + + /// + /// 格式化EAN-13(X-XXXXXX-XXXXX-X) + /// + /// 条形码 + /// 格式化后的条形码 + public static string? FormatEAN13(string? barcode) + { + if (!IsValidEAN13(barcode)) + { + return null; + } + + return $"{barcode![0]}-{barcode.Substring(1, 6)}-{barcode.Substring(7, 5)}-{barcode[12]}"; + } + + /// + /// 条形码脱敏:69****1234 + /// + /// 条形码 + /// 脱敏后的条形码 + public static string? Mask(string? barcode) + { + string? normalized = Normalize(barcode); + if (normalized == null || normalized.Length < 6) + { + return null; + } + + int len = normalized.Length; + int prefixLen = Math.Min(2, len / 3); + int suffixLen = Math.Min(4, len / 3); + + return normalized.Substring(0, prefixLen) + + new string('*', len - prefixLen - suffixLen) + + normalized.Substring(len - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机EAN-13条形码(仅供测试使用) + /// + /// 前缀(可选,默认690-中国) + /// EAN-13条形码 + public static string GenerateRandomEAN13(string? prefix = null) + { + string pre = prefix ?? "690"; + while (pre.Length < 12) + { + pre += MathCategory.RandomUtil.RandomInt(0, 10).ToString(); + } + + pre = pre.Substring(0, 12); + int checkDigit = CalculateCheckDigit(pre); + return pre + checkDigit; + } + + /// + /// 生成随机ITF-14条形码(仅供测试使用) + /// + /// ITF-14条形码 + public static string GenerateRandomITF14() + { + string code = MathCategory.RandomUtil.RandomDigitString(13); + int checkDigit = CalculateCheckDigit(code); + return code + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/CreditCardUtil.cs b/EasyTool.Core/BusinessCategory/CreditCardUtil.cs new file mode 100644 index 0000000..47cf073 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/CreditCardUtil.cs @@ -0,0 +1,484 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 国际信用卡类型枚举 + /// + public enum CreditCardType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// Visa + /// + Visa = 1, + + /// + /// MasterCard + /// + MasterCard = 2, + + /// + /// American Express + /// + Amex = 3, + + /// + /// Discover + /// + Discover = 4, + + /// + /// JCB + /// + JCB = 5, + + /// + /// Diners Club + /// + DinersClub = 6, + + /// + /// UnionPay(银联) + /// + UnionPay = 7, + + /// + /// Maestro + /// + Maestro = 8 + } + + /// + /// 国际信用卡信息 + /// + public class CreditCardInfo + { + /// + /// 卡类型 + /// + public CreditCardType Type { get; set; } + + /// + /// 卡名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 发卡组织 + /// + public string Issuer { get; set; } = string.Empty; + } + + /// + /// 国际信用卡工具类 + /// + public static class CreditCardUtil + { + #region 常量与私有字段 + + /// + /// 信用卡号正则表达式(13-19位数字) + /// + private static readonly Regex CardNumberRegex = new(@"^\d{13,19}$", RegexOptions.Compiled); + + /// + /// 卡类型识别规则(前缀 -> 卡类型) + /// + private static readonly (string Prefix, CreditCardType Type, string Name, string Issuer)[] CardTypeRules = + { + // Visa: 4开头,13或16位 + ("4", CreditCardType.Visa, "Visa", "Visa International"), + + // MasterCard: 51-55, 2221-2720开头,16位 + ("51", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("52", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("53", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("54", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("55", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2221", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2222", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2223", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2224", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2225", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2226", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2227", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2228", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2229", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("223", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("224", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("225", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("226", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("227", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("228", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("229", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("23", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("24", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("25", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("26", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("270", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("271", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + ("2720", CreditCardType.MasterCard, "MasterCard", "MasterCard Worldwide"), + + // American Express: 34或37开头,15位 + ("34", CreditCardType.Amex, "American Express", "American Express Company"), + ("37", CreditCardType.Amex, "American Express", "American Express Company"), + + // Discover: 6011, 622126-622925, 644-649, 65开头,16位 + ("6011", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("65", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("644", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("645", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("646", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("647", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("648", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("649", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622126", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622127", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622128", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622129", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62213", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62214", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62215", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62216", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62217", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62218", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62219", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6222", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6223", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6224", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6225", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6226", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6227", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("6228", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62290", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("62291", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622920", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622921", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622922", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622923", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622924", CreditCardType.Discover, "Discover", "Discover Financial Services"), + ("622925", CreditCardType.Discover, "Discover", "Discover Financial Services"), + + // JCB: 3528-3589开头,16位 + ("3528", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("3529", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("353", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("354", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("355", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("356", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("357", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + ("358", CreditCardType.JCB, "JCB", "JCB Co., Ltd."), + + // Diners Club: 300-305, 309, 36, 38-39开头,14位 + ("300", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("301", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("302", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("303", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("304", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("305", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("309", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("36", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("38", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + ("39", CreditCardType.DinersClub, "Diners Club", "Diners Club International"), + + // UnionPay: 62开头,16-19位 + ("62", CreditCardType.UnionPay, "UnionPay", "China UnionPay"), + + // Maestro: 5018, 5020, 5038, 5893, 6304, 6759, 6761-6763开头,12-19位 + ("5018", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5020", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5038", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("5893", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6304", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6759", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6761", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6762", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide"), + ("6763", CreditCardType.Maestro, "Maestro", "MasterCard Worldwide") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证信用卡号是否有效(格式 + Luhn校验) + /// + /// 信用卡号 + /// 是否有效 + public static bool IsValid(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return false; + } + + return ValidateLuhn(cardNumber!); + } + + /// + /// 验证信用卡号格式 + /// + /// 信用卡号 + /// 格式是否正确 + public static bool IsValidFormat(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + return CardNumberRegex.IsMatch(cleaned); + } + + /// + /// 使用Luhn算法验证信用卡号 + /// + /// 信用卡号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? cardNumber) + { + if (string.IsNullOrWhiteSpace(cardNumber)) + { + return false; + } + + string cleaned = Regex.Replace(cardNumber, @"\D", ""); + int sum = 0; + int length = cleaned.Length; + bool isEvenPosition = false; + + for (int i = length - 1; i >= 0; i--) + { + if (!char.IsDigit(cleaned[i])) + { + return false; + } + + int digit = cleaned[i] - '0'; + + if (isEvenPosition) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + isEvenPosition = !isEvenPosition; + } + + return sum % 10 == 0; + } + + #endregion + + #region 类型识别 + + /// + /// 获取信用卡类型 + /// + /// 信用卡号 + /// 信用卡类型 + public static CreditCardType GetCardType(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return CreditCardType.Unknown; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + // 从最长前缀开始匹配 + for (int len = 6; len >= 1; len--) + { + if (cleaned.Length < len) continue; + + string prefix = cleaned.Substring(0, len); + foreach (var rule in CardTypeRules) + { + if (rule.Prefix == prefix) + { + return rule.Type; + } + } + } + + return CreditCardType.Unknown; + } + + /// + /// 获取信用卡信息 + /// + /// 信用卡号 + /// 信用卡信息 + public static CreditCardInfo? GetCardInfo(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + for (int len = 6; len >= 1; len--) + { + if (cleaned.Length < len) continue; + + string prefix = cleaned.Substring(0, len); + foreach (var rule in CardTypeRules) + { + if (rule.Prefix == prefix) + { + return new CreditCardInfo + { + Type = rule.Type, + Name = rule.Name, + Issuer = rule.Issuer + }; + } + } + } + + return null; + } + + /// + /// 获取信用卡类型名称 + /// + /// 信用卡号 + /// 类型名称 + public static string? GetCardTypeName(string? cardNumber) + { + return GetCardInfo(cardNumber)?.Name; + } + + /// + /// 判断是否为Visa卡 + /// + public static bool IsVisa(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Visa; + + /// + /// 判断是否为MasterCard + /// + public static bool IsMasterCard(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.MasterCard; + + /// + /// 判断是否为American Express + /// + public static bool IsAmex(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Amex; + + /// + /// 判断是否为Discover + /// + public static bool IsDiscover(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.Discover; + + /// + /// 判断是否为JCB + /// + public static bool IsJCB(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.JCB; + + /// + /// 判断是否为银联 + /// + public static bool IsUnionPay(string? cardNumber) => GetCardType(cardNumber) == CreditCardType.UnionPay; + + #endregion + + #region 格式化方法 + + /// + /// 格式化信用卡号(每4位一组) + /// + /// 信用卡号 + /// 格式化后的卡号 + public static string? Format(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + var groups = new List(); + + for (int i = 0; i < cleaned.Length; i += 4) + { + int len = Math.Min(4, cleaned.Length - i); + groups.Add(cleaned.Substring(i, len)); + } + + return string.Join(" ", groups); + } + + /// + /// 格式化信用卡号(根据卡类型自动选择格式) + /// + /// 信用卡号 + /// 格式化后的卡号 + public static string? FormatByType(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + CreditCardType type = GetCardType(cleaned); + + // Amex特殊格式:4-6-5 + if (type == CreditCardType.Amex && cleaned.Length == 15) + { + return $"{cleaned.Substring(0, 4)} {cleaned.Substring(4, 6)} {cleaned.Substring(10, 5)}"; + } + + // 默认4位一组 + return Format(cleaned); + } + + /// + /// 信用卡号脱敏:**** **** **** 1234 + /// + /// 信用卡号 + /// 脱敏后的卡号 + public static string? Mask(string? cardNumber) + { + if (!IsValidFormat(cardNumber)) + { + return null; + } + + string cleaned = Regex.Replace(cardNumber!, @"\D", ""); + + if (cleaned.Length < 8) + { + return null; + } + + int suffixLen = 4; + string suffix = cleaned.Substring(cleaned.Length - suffixLen); + int maskLen = cleaned.Length - suffixLen; + string masked = new string('*', maskLen); + + // 格式化输出 + CreditCardType type = GetCardType(cleaned); + if (type == CreditCardType.Amex && cleaned.Length == 15) + { + return $"**** ****** {suffix}"; + } + + return $"**** **** **** {suffix}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs new file mode 100644 index 0000000..ad7f1a5 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/CreditCodeUtil.cs @@ -0,0 +1,210 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 统一社会信用代码工具类 + /// 用于验证和处理中国大陆企业的统一社会信用代码 + /// + public static class CreditCodeUtil + { + /// + /// 统一社会信用代码长度 + /// + private const int CreditCodeLength = 18; + + /// + /// 统一社会信用代码字符集(不包含I、O、Z、S、V) + /// + private const string CreditCodeChars = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + + /// + /// 校验码权重 + /// + private static readonly int[] Weights = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 }; + + /// + /// 验证统一社会信用代码是否有效 + /// + /// 统一社会信用代码 + /// 是否有效 + public static bool IsValid(string? creditCode) + { + if (string.IsNullOrWhiteSpace(creditCode)) + return false; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + // 检查长度 + if (creditCode.Length != CreditCodeLength) + return false; + + // 检查字符是否合法 + foreach (var c in creditCode) + { + if (!CreditCodeChars.Contains(c)) + return false; + } + + // 验证校验码 + return ValidateCheckCode(creditCode); + } + + /// + /// 获取统一社会信用代码的类型信息 + /// + /// 统一社会信用代码 + /// 类型信息 + public static CreditCodeType? GetType(string? creditCode) + { + if (string.IsNullOrWhiteSpace(creditCode)) + return null; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + if (creditCode.Length != CreditCodeLength) + return null; + + var typeCode = creditCode[0]; + return typeCode switch + { + '1' => CreditCodeType.Institution, + '5' => CreditCodeType.Enterprise, + '9' => CreditCodeType.Other, + 'Y' => CreditCodeType.IndividualBusiness, + _ => null + }; + } + + /// + /// 获取登记管理部门 + /// + /// 统一社会信用代码 + /// 登记管理部门 + public static string? GetRegistrationAuthority(string? creditCode) + { + if (string.IsNullOrWhiteSpace(creditCode)) + return null; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + if (creditCode.Length < 2) + return null; + + var code = creditCode.Substring(0, 2); + return code switch + { + "11" => "工商行政管理", + "12" => "工商行政管理(个体工商户)", + "13" => "工商行政管理(农民专业合作社)", + "19" => "工商行政管理(其他)", + "21" => "机构编制", + "31" => "外交", + "32" => "文化", + "33" => "教育", + "34" => "卫生", + "35" => "体育", + "36" => "新闻出版", + "37" => "宗教事务", + "41" => "司法行政(律师)", + "42" => "司法行政(公证)", + "43" => "司法行政(基层法律服务)", + "44" => "司法行政(司法鉴定)", + "51" => "民政", + "52" => "民政(社会组织)", + "53" => "民政(基金会)", + "54" => "民政(民办非企业单位)", + "61" => "旅游", + "62" => "文物", + "71" => "工会", + "81" => "公安", + "91" => "其他", + "A1" => "全国人大", + "A2" => "全国政协", + "A3" => "人民法院", + "A4" => "人民检察院", + "A9" => "其他", + "N1" => "军事", + "N2" => "武警", + _ => "未知" + }; + } + + /// + /// 生成校验码 + /// + /// 前17位代码 + /// 校验码 + public static char GenerateCheckCode(string creditCode17) + { + if (string.IsNullOrEmpty(creditCode17) || creditCode17.Length != 17) + throw new ArgumentException("输入必须为17位"); + + int sum = 0; + for (int i = 0; i < 17; i++) + { + var value = CreditCodeChars.IndexOf(char.ToUpperInvariant(creditCode17[i])); + if (value < 0) + throw new ArgumentException($"第{i + 1}位字符无效"); + + sum += value * Weights[i]; + } + + var mod = 31 - sum % 31; + return mod == 31 ? '0' : CreditCodeChars[mod]; + } + + private static bool ValidateCheckCode(string creditCode) + { + var expectedCheckCode = GenerateCheckCode(creditCode.Substring(0, 17)); + return creditCode[17] == expectedCheckCode; + } + + /// + /// 格式化统一社会信用代码(添加分隔符) + /// + /// 统一社会信用代码 + /// 分隔符 + /// 格式化后的代码 + public static string Format(string? creditCode, string separator = "-") + { + if (string.IsNullOrWhiteSpace(creditCode)) + return string.Empty; + + creditCode = creditCode.Trim().ToUpperInvariant(); + + if (creditCode.Length != CreditCodeLength) + return creditCode; + + // 格式:XXXXXX-XXXX-XXXX-XXXX + return $"{creditCode.Substring(0, 6)}{separator}{creditCode.Substring(6, 4)}{separator}{creditCode.Substring(10, 4)}{separator}{creditCode.Substring(14, 4)}"; + } + } + + /// + /// 统一社会信用代码类型 + /// + public enum CreditCodeType + { + /// + /// 机构 + /// + Institution, + + /// + /// 企业 + /// + Enterprise, + + /// + /// 其他 + /// + Other, + + /// + /// 个体工商户 + /// + IndividualBusiness + } +} diff --git a/EasyTool.Core/BusinessCategory/DomainUtil.cs b/EasyTool.Core/BusinessCategory/DomainUtil.cs new file mode 100644 index 0000000..7e7c929 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/DomainUtil.cs @@ -0,0 +1,456 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 域名工具类 + /// + public static class DomainUtil + { + #region 常量与私有字段 + + /// + /// 域名正则表达式 + /// + private static readonly Regex DomainRegex = new( + @"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$", + RegexOptions.Compiled); + + /// + /// IDN(国际化域名)正则 + /// + private static readonly Regex IdnRegex = new( + @"^(?:[a-zA-Z0-9\u4e00-\u9fa5](?:[a-zA-Z0-9-\u4e00-\u9fa5]{0,61}[a-zA-Z0-9\u4e00-\u9fa5])?\.)+[a-zA-Z\u4e00-\u9fa5]{2,}$", + RegexOptions.Compiled); + + /// + /// 顶级域名(TLD)与类型映射 + /// + private static readonly Dictionary TldTypeMap = new(StringComparer.OrdinalIgnoreCase) + { + // 通用顶级域名 + { "com", "商业机构" }, { "net", "网络服务商" }, { "org", "非营利组织" }, + { "edu", "教育机构" }, { "gov", "政府机构" }, { "mil", "军事机构" }, + { "int", "国际组织" }, { "info", "信息服务" }, { "biz", "商业" }, + { "name", "个人" }, { "pro", "专业人士" }, { "museum", "博物馆" }, + { "coop", "合作社" }, { "aero", "航空" }, { "xxx", "成人内容" }, + { "xyz", "通用" }, { "top", "通用" }, { "vip", "VIP" }, + { "site", "网站" }, { "online", "在线" }, { "store", "商店" }, + { "tech", "科技" }, { "fun", "娱乐" }, { "club", "俱乐部" }, + { "shop", "购物" }, { "ltd", "有限公司" }, { "work", "工作" }, + + // 国家/地区顶级域名 + { "cn", "中国" }, { "hk", "香港" }, { "tw", "台湾" }, { "mo", "澳门" }, + { "jp", "日本" }, { "kr", "韩国" }, { "sg", "新加坡" }, { "my", "马来西亚" }, + { "th", "泰国" }, { "vn", "越南" }, { "ph", "菲律宾" }, { "id", "印度尼西亚" }, + { "in", "印度" }, { "pk", "巴基斯坦" }, { "au", "澳大利亚" }, { "nz", "新西兰" }, + { "us", "美国" }, { "ca", "加拿大" }, { "mx", "墨西哥" }, { "br", "巴西" }, + { "uk", "英国" }, { "de", "德国" }, { "fr", "法国" }, { "it", "意大利" }, + { "es", "西班牙" }, { "nl", "荷兰" }, { "be", "比利时" }, { "ch", "瑞士" }, + { "at", "奥地利" }, { "se", "瑞典" }, { "no", "挪威" }, { "dk", "丹麦" }, + { "fi", "芬兰" }, { "ru", "俄罗斯" }, { "pl", "波兰" }, { "cz", "捷克" }, + { "ua", "乌克兰" }, { "tr", "土耳其" }, { "sa", "沙特" }, { "ae", "阿联酋" }, + { "il", "以色列" }, { "za", "南非" }, { "eg", "埃及" }, { "ng", "尼日利亚" }, + { "ke", "肯尼亚" }, { "ar", "阿根廷" }, { "cl", "智利" }, { "co", "哥伦比亚" }, + + // 中国二级域名 + { "com.cn", "中国商业" }, { "net.cn", "中国网络" }, { "org.cn", "中国组织" }, + { "gov.cn", "中国政府" }, { "edu.cn", "中国教育" }, { "ac.cn", "中国科研" }, + { "mil.cn", "中国军事" }, { "bj.cn", "北京" }, { "sh.cn", "上海" }, + { "tj.cn", "天津" }, { "cq.cn", "重庆" }, { "he.cn", "河北" }, + { "sx.cn", "山西" }, { "nm.cn", "内蒙古" }, { "ln.cn", "辽宁" }, + { "jl.cn", "吉林" }, { "hl.cn", "黑龙江" }, { "js.cn", "江苏" }, + { "zj.cn", "浙江" }, { "ah.cn", "安徽" }, { "fj.cn", "福建" }, + { "jx.cn", "江西" }, { "sd.cn", "山东" }, { "ha.cn", "河南" }, + { "hb.cn", "湖北" }, { "hn.cn", "湖南" }, { "gd.cn", "广东" }, + { "gx.cn", "广西" }, { "hi.cn", "海南" }, { "sc.cn", "四川" }, + { "gz.cn", "贵州" }, { "yn.cn", "云南" }, { "xz.cn", "西藏" }, + { "sn.cn", "陕西" }, { "gs.cn", "甘肃" }, { "qh.cn", "青海" }, + { "nx.cn", "宁夏" }, { "xj.cn", "新疆" } + }; + + /// + /// 常见二级域名服务 + /// + private static readonly Dictionary WellKnownDomains = new(StringComparer.OrdinalIgnoreCase) + { + { "baidu.com", "百度" }, { "qq.com", "腾讯" }, { "taobao.com", "淘宝" }, + { "tmall.com", "天猫" }, { "jd.com", "京东" }, { "alipay.com", "支付宝" }, + { "alibaba.com", "阿里巴巴" }, { "aliyun.com", "阿里云" }, + { "tencent.com", "腾讯" }, { "weixin.com", "微信" }, { "wechat.com", "微信" }, + { "douyin.com", "抖音" }, { "tiktok.com", "TikTok" }, { "bytedance.com", "字节跳动" }, + { "meituan.com", "美团" }, { "dianping.com", "大众点评" }, + { "didichuxing.com", "滴滴" }, { "xiaojukeji.com", "滴滴" }, + { "sohu.com", "搜狐" }, { "sina.com.cn", "新浪" }, { "weibo.com", "微博" }, + { "163.com", "网易" }, { "126.com", "网易" }, { "yeah.net", "网易" }, + { "zhihu.com", "知乎" }, { "csdn.net", "CSDN" }, + { "bilibili.com", "哔哩哔哩" }, { "acfun.cn", "AcFun" }, + { "youku.com", "优酷" }, { "iqiyi.com", "爱奇艺" }, { "v.qq.com", "腾讯视频" }, + { "github.com", "GitHub" }, { "gitlab.com", "GitLab" }, { "gitee.com", "Gitee" }, + { "google.com", "Google" }, { "youtube.com", "YouTube" }, { "gmail.com", "Gmail" }, + { "facebook.com", "Facebook" }, { "instagram.com", "Instagram" }, { "whatsapp.com", "WhatsApp" }, + { "twitter.com", "Twitter" }, { "x.com", "X" }, + { "linkedin.com", "LinkedIn" }, { "microsoft.com", "Microsoft" }, + { "apple.com", "Apple" }, { "amazon.com", "Amazon" }, { "aws.amazon.com", "AWS" }, + { "cloudflare.com", "Cloudflare" }, { "nginx.com", "NGINX" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证域名是否有效 + /// + /// 域名 + /// 是否有效 + public static bool IsValid(string? domain) + { + if (string.IsNullOrWhiteSpace(domain)) + { + return false; + } + + // 域名总长度不超过253字符 + if (domain.Length > 253) + { + return false; + } + + string lower = domain.ToLower().Trim(); + + // 检查是否为IDN + if (IdnRegex.IsMatch(lower)) + { + return true; + } + + return DomainRegex.IsMatch(lower); + } + + /// + /// 验证是否为国际顶级域名 + /// + /// 域名 + /// 是否为国际域名 + public static bool IsInternationalDomain(string? domain) + { + if (!IsValid(domain)) + { + return false; + } + + string tld = GetTLD(domain); + if (tld == null) return false; + + // 常见国际顶级域名 + string[] internationalTlds = { "com", "net", "org", "edu", "gov", "mil", "int", "info", "biz", "name", "pro" }; + foreach (var itld in internationalTlds) + { + if (tld.Equals(itld, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + /// + /// 验证是否为中国域名 + /// + /// 域名 + /// 是否为中国域名 + public static bool IsChinaDomain(string? domain) + { + if (!IsValid(domain)) + { + return false; + } + + string tld = GetTLD(domain); + return tld?.Equals("cn", StringComparison.OrdinalIgnoreCase) == true || + domain!.EndsWith(".com.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".net.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".org.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".gov.cn", StringComparison.OrdinalIgnoreCase) || + domain.EndsWith(".edu.cn", StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 信息提取 + + /// + /// 获取顶级域名(TLD) + /// + /// 域名 + /// 顶级域名 + public static string? GetTLD(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 检查是否为双后缀(如.com.cn) + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld)) + { + return possibleDoubleTld; + } + } + + return parts[^1]; + } + + /// + /// 获取顶级域名类型/归属 + /// + /// 域名 + /// 顶级域名类型 + public static string? GetTLDType(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + // 先检查双后缀 + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.TryGetValue(possibleDoubleTld, out string? type)) + { + return type; + } + } + + string tld = GetTLD(domain); + if (tld != null && TldTypeMap.TryGetValue(tld, out string? tldType)) + { + return tldType; + } + + return null; + } + + /// + /// 获取二级域名 + /// + /// 域名 + /// 二级域名 + public static string? GetSecondLevelDomain(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 处理双后缀 + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld) && parts.Length >= 4) + { + return parts[^3] + "." + possibleDoubleTld; + } + } + + return parts[^2] + "." + parts[^1]; + } + + /// + /// 获取子域名前缀 + /// + /// 域名 + /// 子域名前缀(如www、mail等) + public static string? GetSubdomain(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string[] parts = domain!.ToLower().Split('.'); + + // 计算主域名部分的长度 + int mainDomainParts = 2; + if (parts.Length >= 3) + { + string possibleDoubleTld = parts[^2] + "." + parts[^1]; + if (TldTypeMap.ContainsKey(possibleDoubleTld)) + { + mainDomainParts = 3; + } + } + + if (parts.Length <= mainDomainParts) + { + return null; // 无子域名 + } + + // 返回除主域名外的部分 + return string.Join(".", parts, 0, parts.Length - mainDomainParts); + } + + /// + /// 获取主域名(不含子域名) + /// + /// 域名 + /// 主域名 + public static string? GetMainDomain(string? domain) + { + string? sld = GetSecondLevelDomain(domain); + return sld; + } + + /// + /// 获取已知服务名称 + /// + /// 域名 + /// 服务名称 + public static string? GetServiceName(string? domain) + { + if (!IsValid(domain)) + { + return null; + } + + string mainDomain = GetMainDomain(domain)?.ToLower() ?? ""; + + foreach (var kvp in WellKnownDomains) + { + if (mainDomain.Equals(kvp.Key, StringComparison.OrdinalIgnoreCase) || + domain!.EndsWith("." + kvp.Key, StringComparison.OrdinalIgnoreCase)) + { + return kvp.Value; + } + } + + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化域名(转小写,去除协议和路径) + /// + /// 域名 + /// 格式化后的域名 + public static string? Normalize(string? domain) + { + if (string.IsNullOrWhiteSpace(domain)) + { + return null; + } + + string cleaned = domain.ToLower().Trim(); + + // 去除协议 + if (cleaned.StartsWith("http://")) + { + cleaned = cleaned.Substring(7); + } + else if (cleaned.StartsWith("https://")) + { + cleaned = cleaned.Substring(8); + } + + // 去除路径 + int slashIndex = cleaned.IndexOf('/'); + if (slashIndex > 0) + { + cleaned = cleaned.Substring(0, slashIndex); + } + + // 去除端口 + int colonIndex = cleaned.LastIndexOf(':'); + if (colonIndex > 0) + { + cleaned = cleaned.Substring(0, colonIndex); + } + + return IsValid(cleaned) ? cleaned : null; + } + + /// + /// 域名脱敏:e*****.com + /// + /// 域名 + /// 脱敏后的域名 + public static string? Mask(string? domain) + { + string? normalized = Normalize(domain); + if (normalized == null) + { + return null; + } + + string[] parts = normalized.Split('.'); + if (parts.Length < 2) + { + return null; + } + + // 脱敏主域名部分 + string mainPart = parts[0]; + if (mainPart.Length <= 2) + { + parts[0] = mainPart[0] + "*"; + } + else + { + parts[0] = mainPart[0] + new string('*', mainPart.Length - 2) + mainPart[^1]; + } + + return string.Join(".", parts); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机域名(仅供测试使用) + /// + /// 顶级域名(可选,默认.com) + /// 随机域名 + public static string GenerateRandom(string? tld = null) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + string suffix = tld ?? "com"; + + // 生成随机主域名(6-12位) + int length = MathCategory.RandomUtil.RandomInt(6, 13); + string main = ""; + for (int i = 0; i < length; i++) + { + main += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + return main + "." + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs b/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs new file mode 100644 index 0000000..b4b834c --- /dev/null +++ b/EasyTool.Core/BusinessCategory/DrivingLicenseUtil.cs @@ -0,0 +1,319 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 驾驶证号工具类 + /// + public static class DrivingLicenseUtil + { + #region 常量与私有字段 + + /// + /// 驾驶证号正则表达式(18位,与身份证号格式相同) + /// + private static readonly Regex LicenseRegex = new( + @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 档案编号正则表达式(12位数字) + /// + private static readonly Regex FileNumberRegex = new(@"^\d{12}$", RegexOptions.Compiled); + + /// + /// 驾驶证校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 驾驶证校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 准驾车型映射 + /// + private static readonly (string Code, string Name, string Description)[] VehicleClassMap = + { + ("A1", "大型客车", "可驾驶A3、B1、B2、C1、C2、C3、C4、M"), + ("A2", "牵引车", "可驾驶B1、B2、C1、C2、C3、C4、M"), + ("A3", "城市公交车", "可驾驶C1、C2、C3、C4"), + ("B1", "中型客车", "可驾驶C1、C2、C3、C4、M"), + ("B2", "大型货车", "可驾驶C1、C2、C3、C4、M"), + ("C1", "小型汽车", "可驾驶C2、C3、C4"), + ("C2", "小型自动挡汽车", "仅限自动挡小型汽车"), + ("C3", "低速载货汽车", "可驾驶C4"), + ("C4", "三轮汽车", ""), + ("C5", "残疾人专用小型自动挡汽车", ""), + ("C6", "轻型牵引挂车", "需C1或C2以上驾照增驾"), + ("D", "普通三轮摩托车", "可驾驶E、F"), + ("E", "普通二轮摩托车", "可驾驶F"), + ("F", "轻便摩托车", ""), + ("G", "拖拉机", ""), + ("H", "轮式自行机械", ""), + ("M", "轮式自行机械车", ""), + ("N", "无轨电车", ""), + ("P", "有轨电车", "") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证驾驶证号是否有效 + /// + /// 驾驶证号 + /// 是否有效 + public static bool IsValid(string? licenseNumber) + { + if (string.IsNullOrWhiteSpace(licenseNumber)) + { + return false; + } + + if (!LicenseRegex.IsMatch(licenseNumber)) + { + return false; + } + + // 验证日期有效性 + if (!IsValidDate(licenseNumber.Substring(6, 8))) + { + return false; + } + + // 验证校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (licenseNumber[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + char actualCheckCode = char.ToUpper(licenseNumber[17]); + + return expectedCheckCode == actualCheckCode; + } + + /// + /// 验证档案编号是否有效 + /// + /// 档案编号 + /// 是否有效 + public static bool IsValidFileNumber(string? fileNumber) + { + if (string.IsNullOrWhiteSpace(fileNumber)) + { + return false; + } + + return FileNumberRegex.IsMatch(fileNumber); + } + + #endregion + + #region 信息提取 + + /// + /// 获取出生日期 + /// + /// 驾驶证号 + /// 出生日期 + public static DateTime? GetBirthday(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + int year = int.Parse(licenseNumber!.Substring(6, 4)); + int month = int.Parse(licenseNumber.Substring(10, 2)); + int day = int.Parse(licenseNumber.Substring(12, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取性别(1男2女) + /// + /// 驾驶证号 + /// 性别代码 + public static int? GetGender(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + int genderDigit = licenseNumber![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 驾驶证号 + /// 性别 + public static string? GetGenderString(string? licenseNumber) + { + int? gender = GetGender(licenseNumber); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取行政区划代码 + /// + /// 驾驶证号 + /// 行政区划代码 + public static string? GetAreaCode(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + return licenseNumber!.Substring(0, 6); + } + + /// + /// 判断驾驶证号是否与身份证号一致 + /// + /// 驾驶证号 + /// 身份证号 + /// 是否一致 + public static bool MatchesIdCard(string? licenseNumber, string? idCard) + { + if (!IsValid(licenseNumber) || !IdCardUtil.IsValid18(idCard)) + { + return false; + } + + return licenseNumber!.Equals(idCard!, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 准驾车型 + + /// + /// 获取准驾车型信息 + /// + /// 准驾车型代码 + /// 车型信息 + public static (string Name, string Description)? GetVehicleClassInfo(string? vehicleClass) + { + if (string.IsNullOrWhiteSpace(vehicleClass)) + { + return null; + } + + foreach (var info in VehicleClassMap) + { + if (info.Code.Equals(vehicleClass, StringComparison.OrdinalIgnoreCase)) + { + return (info.Name, info.Description); + } + } + + return null; + } + + /// + /// 获取准驾车型名称 + /// + /// 准驾车型代码 + /// 车型名称 + public static string? GetVehicleClassName(string? vehicleClass) + { + var info = GetVehicleClassInfo(vehicleClass); + return info?.Name; + } + + /// + /// 验证准驾车型代码是否有效 + /// + /// 准驾车型代码 + /// 是否有效 + public static bool IsValidVehicleClass(string? vehicleClass) + { + return GetVehicleClassInfo(vehicleClass) != null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化驾驶证号(转大写) + /// + /// 驾驶证号 + /// 格式化后的驾驶证号 + public static string? Normalize(string? licenseNumber) + { + if (string.IsNullOrWhiteSpace(licenseNumber)) + { + return null; + } + + string upper = licenseNumber.ToUpper().Trim(); + return upper.Length == 18 && LicenseRegex.IsMatch(upper) ? upper : null; + } + + /// + /// 驾驶证号脱敏:110***********1234 + /// + /// 驾驶证号 + /// 脱敏后的驾驶证号 + public static string? Mask(string? licenseNumber) + { + if (!IsValid(licenseNumber)) + { + return null; + } + + return licenseNumber!.Substring(0, 3) + "***********" + licenseNumber.Substring(14); + } + + #endregion + + #region 私有方法 + + /// + /// 验证日期字符串是否有效 + /// + private static bool IsValidDate(string dateStr) + { + if (dateStr.Length != 8) + { + return false; + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + if (year < 1900 || year > DateTime.Now.Year) + { + return false; + } + + if (month < 1 || month > 12) + { + return false; + } + + int maxDay = DateTime.DaysInMonth(year, month); + return day >= 1 && day <= maxDay; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/EmailUtil.cs b/EasyTool.Core/BusinessCategory/EmailUtil.cs new file mode 100644 index 0000000..903a7f3 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/EmailUtil.cs @@ -0,0 +1,518 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 邮箱服务提供商枚举 + /// + public enum EmailProvider + { + /// + /// 未知服务商 + /// + Unknown = 0, + + /// + /// QQ邮箱 + /// + QQ = 1, + + /// + /// 网易163邮箱 + /// + NetEase163 = 2, + + /// + /// 网易126邮箱 + /// + NetEase126 = 3, + + /// + /// 网易yeah邮箱 + /// + NetEaseYeah = 4, + + /// + /// 新浪邮箱 + /// + Sina = 5, + + /// + /// 搜狐邮箱 + /// + Sohu = 6, + + /// + /// Gmail + /// + Gmail = 7, + + /// + /// Outlook/Hotmail + /// + Outlook = 8, + + /// + /// Yahoo + /// + Yahoo = 9, + + /// + /// iCloud + /// + ICloud = 10, + + /// + /// 阿里云邮箱 + /// + Aliyun = 11, + + /// + /// 企业邮箱 + /// + Enterprise = 12 + } + + /// + /// 邮箱工具类 + /// + public static class EmailUtil + { + #region 常量与私有字段 + + /// + /// 邮箱正则表达式(标准格式) + /// + private static readonly Regex EmailRegex = new Regex( + @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", + RegexOptions.Compiled); + + /// + /// 简单邮箱正则表达式(用于快速验证) + /// + private static readonly Regex SimpleEmailRegex = new Regex( + @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", + RegexOptions.Compiled); + + /// + /// 邮箱服务商域名映射 + /// + private static readonly Dictionary ProviderDomainMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + // QQ邮箱 + { "qq.com", EmailProvider.QQ }, + { "foxmail.com", EmailProvider.QQ }, + { "vip.qq.com", EmailProvider.QQ }, + + // 网易邮箱 + { "163.com", EmailProvider.NetEase163 }, + { "vip.163.com", EmailProvider.NetEase163 }, + { "126.com", EmailProvider.NetEase126 }, + { "vip.126.com", EmailProvider.NetEase126 }, + { "yeah.net", EmailProvider.NetEaseYeah }, + + // 新浪邮箱 + { "sina.com", EmailProvider.Sina }, + { "sina.cn", EmailProvider.Sina }, + { "vip.sina.com", EmailProvider.Sina }, + + // 搜狐邮箱 + { "sohu.com", EmailProvider.Sohu }, + { "vip.sohu.com", EmailProvider.Sohu }, + + // Gmail + { "gmail.com", EmailProvider.Gmail }, + { "googlemail.com", EmailProvider.Gmail }, + + // Outlook/Hotmail + { "outlook.com", EmailProvider.Outlook }, + { "hotmail.com", EmailProvider.Outlook }, + { "live.com", EmailProvider.Outlook }, + { "msn.com", EmailProvider.Outlook }, + + // Yahoo + { "yahoo.com", EmailProvider.Yahoo }, + { "yahoo.cn", EmailProvider.Yahoo }, + { "yahoo.com.cn", EmailProvider.Yahoo }, + { "yahoo.co.jp", EmailProvider.Yahoo }, + { "ymail.com", EmailProvider.Yahoo }, + + // iCloud + { "icloud.com", EmailProvider.ICloud }, + { "me.com", EmailProvider.ICloud }, + { "mac.com", EmailProvider.ICloud }, + + // 阿里云邮箱 + { "aliyun.com", EmailProvider.Aliyun }, + { "aliyuncs.com", EmailProvider.Aliyun } + }; + + /// + /// 常见企业邮箱域名 + /// + private static readonly HashSet EnterpriseDomains = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "exmail.qq.com", // 腾讯企业邮 + "qiye.163.com", // 网易企业邮 + "qiye.aliyun.com", // 阿里企业邮 + "corp.sina.com", // 新浪企业邮 + }; + + #endregion + + #region 验证方法 + + /// + /// 验证邮箱格式是否有效(标准验证) + /// + /// 邮箱地址 + /// 是否有效 + public static bool IsValid(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + // 长度检查(RFC 5321规定最大254字符) + if (email.Length > 254) + { + return false; + } + + return EmailRegex.IsMatch(email); + } + + /// + /// 快速验证邮箱格式(简单验证,性能更好) + /// + /// 邮箱地址 + /// 是否有效 + public static bool IsValidQuick(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + if (email.Length > 254) + { + return false; + } + + return SimpleEmailRegex.IsMatch(email); + } + + /// + /// 验证邮箱格式并规范化 + /// + /// 邮箱地址 + /// 规范化后的邮箱地址,无效返回null + public static string? Normalize(string? email) + { + if (!IsValid(email)) + { + return null; + } + + // 转小写,去除首尾空格 + return email!.Trim().ToLower(); + } + + #endregion + + #region 解析方法 + + /// + /// 获取邮箱用户名(@之前的部分) + /// + /// 邮箱地址 + /// 用户名,无效邮箱返回null + public static string? GetUsername(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(0, atIndex); + } + + /// + /// 获取邮箱域名(@之后的部分) + /// + /// 邮箱地址 + /// 域名,无效邮箱返回null + public static string? GetDomain(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(atIndex + 1).ToLower(); + } + + /// + /// 获取邮箱顶级域名 + /// + /// 邮箱地址 + /// 顶级域名(如.com、.cn),无效邮箱返回null + public static string? GetTopLevelDomain(string? email) + { + string? domain = GetDomain(email); + if (domain == null) + { + return null; + } + + int lastDotIndex = domain.LastIndexOf('.'); + if (lastDotIndex < 0) + { + return null; + } + + return domain.Substring(lastDotIndex); + } + + #endregion + + #region 服务商识别 + + /// + /// 获取邮箱服务商 + /// + /// 邮箱地址 + /// 邮箱服务商枚举 + public static EmailProvider GetProvider(string? email) + { + string? domain = GetDomain(email); + if (domain == null) + { + return EmailProvider.Unknown; + } + + // 检查企业邮箱 + if (EnterpriseDomains.Contains(domain)) + { + return EmailProvider.Enterprise; + } + + // 检查已知服务商 + if (ProviderDomainMap.TryGetValue(domain, out EmailProvider provider)) + { + return provider; + } + + // 检查子域名(如 vip.qq.com) + foreach (var kvp in ProviderDomainMap) + { + if (domain.EndsWith("." + kvp.Key, StringComparison.OrdinalIgnoreCase)) + { + return kvp.Value; + } + } + + return EmailProvider.Unknown; + } + + /// + /// 获取邮箱服务商名称 + /// + /// 邮箱地址 + /// 服务商名称 + public static string? GetProviderName(string? email) + { + EmailProvider provider = GetProvider(email); + return provider switch + { + EmailProvider.QQ => "QQ邮箱", + EmailProvider.NetEase163 => "163邮箱", + EmailProvider.NetEase126 => "126邮箱", + EmailProvider.NetEaseYeah => "Yeah邮箱", + EmailProvider.Sina => "新浪邮箱", + EmailProvider.Sohu => "搜狐邮箱", + EmailProvider.Gmail => "Gmail", + EmailProvider.Outlook => "Outlook", + EmailProvider.Yahoo => "Yahoo邮箱", + EmailProvider.ICloud => "iCloud", + EmailProvider.Aliyun => "阿里云邮箱", + EmailProvider.Enterprise => "企业邮箱", + _ => null + }; + } + + /// + /// 判断是否为企业邮箱 + /// + /// 邮箱地址 + /// 是否为企业邮箱 + public static bool IsEnterpriseEmail(string? email) + { + EmailProvider provider = GetProvider(email); + + // 已知企业邮箱域名 + if (provider == EmailProvider.Enterprise) + { + return true; + } + + // 未知服务商可能是企业邮箱 + if (provider == EmailProvider.Unknown) + { + string? domain = GetDomain(email); + // 排除常见个人邮箱域名后的其他域名可能是企业邮箱 + return domain != null && !IsCommonPublicDomain(domain); + } + + return false; + } + + /// + /// 判断是否为常见公共邮箱域名 + /// + private static bool IsCommonPublicDomain(string domain) + { + return ProviderDomainMap.ContainsKey(domain); + } + + #endregion + + #region 格式化方法 + + /// + /// 邮箱脱敏:t***@qq.com + /// + /// 邮箱地址 + /// 脱敏后的邮箱地址 + public static string? Mask(string? email) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + string username = email.Substring(0, atIndex); + string domain = email.Substring(atIndex); + + if (username.Length <= 1) + { + return "*" + domain; + } + else if (username.Length <= 3) + { + return username[0] + new string('*', username.Length - 1) + domain; + } + else + { + return username.Substring(0, 2) + new string('*', username.Length - 2) + domain; + } + } + + /// + /// 邮箱脱敏(自定义脱敏字符数) + /// + /// 邮箱地址 + /// 用户名可见字符数 + /// 脱敏后的邮箱地址 + public static string? Mask(string? email, int visibleChars) + { + if (!IsValidQuick(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + string username = email.Substring(0, atIndex); + string domain = email.Substring(atIndex); + + if (visibleChars <= 0) + { + return new string('*', Math.Min(username.Length, 3)) + domain; + } + + if (visibleChars >= username.Length) + { + return email; + } + + return username.Substring(0, visibleChars) + new string('*', username.Length - visibleChars) + domain; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机邮箱(仅供测试使用) + /// + /// 邮箱服务商(可选,默认随机) + /// 随机邮箱地址 + public static string GenerateRandom(EmailProvider? provider = null) + { + string username = GenerateRandomUsername(8); + string domain; + + if (provider.HasValue && provider.Value != EmailProvider.Unknown && provider.Value != EmailProvider.Enterprise) + { + domain = GetDomainByProvider(provider.Value); + } + else + { + // 随机选择一个服务商 + var providers = new[] { EmailProvider.QQ, EmailProvider.NetEase163, EmailProvider.Gmail, EmailProvider.Outlook }; + var randomProvider = EasyTool.MathCategory.RandomUtil.GetRandomElement(providers); + domain = GetDomainByProvider(randomProvider); + } + + return username + "@" + domain; + } + + #endregion + + #region 私有方法 + + /// + /// 根据服务商获取域名 + /// + private static string GetDomainByProvider(EmailProvider provider) + { + return provider switch + { + EmailProvider.QQ => "qq.com", + EmailProvider.NetEase163 => "163.com", + EmailProvider.NetEase126 => "126.com", + EmailProvider.NetEaseYeah => "yeah.net", + EmailProvider.Sina => "sina.com", + EmailProvider.Sohu => "sohu.com", + EmailProvider.Gmail => "gmail.com", + EmailProvider.Outlook => "outlook.com", + EmailProvider.Yahoo => "yahoo.com", + EmailProvider.ICloud => "icloud.com", + EmailProvider.Aliyun => "aliyun.com", + _ => "example.com" + }; + } + + /// + /// 生成随机用户名 + /// + private static string GenerateRandomUsername(int length) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + var sb = new System.Text.StringBuilder(length); + for (int i = 0; i < length; i++) + { + sb.Append(EasyTool.MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray())); + } + return sb.ToString(); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs b/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs new file mode 100644 index 0000000..142e842 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ForeignerIdUtil.cs @@ -0,0 +1,368 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 外国人永久居留身份证工具类 + /// + public static class ForeignerIdUtil + { + #region 常量与私有字段 + + /// + /// 外国人永久居留身份证正则表达式(15位) + /// + private static readonly Regex ForeignerId15Regex = new( + @"^[A-Z]{3}\d{12}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 新版外国人永久居留身份证正则表达式(18位) + /// 格式与普通身份证相同,但用于外国人 + /// + private static readonly Regex ForeignerId18Regex = new( + @"^[A-Z]\d{17}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 校验码权重(18位版本) + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表(18位版本) + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 国籍代码映射(部分常见国家) + /// + private static readonly Dictionary NationalityMap = new(StringComparer.OrdinalIgnoreCase) + { + { "USA", "美国" }, { "GBR", "英国" }, { "JPN", "日本" }, { "KOR", "韩国" }, + { "DEU", "德国" }, { "FRA", "法国" }, { "ITA", "意大利" }, { "ESP", "西班牙" }, + { "CAN", "加拿大" }, { "AUS", "澳大利亚" }, { "NZL", "新西兰" }, { "RUS", "俄罗斯" }, + { "IND", "印度" }, { "THA", "泰国" }, { "VNM", "越南" }, { "MYS", "马来西亚" }, + { "SGP", "新加坡" }, { "IDN", "印度尼西亚" }, { "PHL", "菲律宾" }, { "MMR", "缅甸" }, + { "PAK", "巴基斯坦" }, { "BGD", "孟加拉国" }, { "BRA", "巴西" }, { "MEX", "墨西哥" }, + { "ZAF", "南非" }, { "EGY", "埃及" }, { "NGA", "尼日利亚" }, { "KEN", "肯尼亚" }, + { "CHN", "中国" }, { "HKG", "香港" }, { "MAC", "澳门" }, { "TWN", "台湾" } + }; + + /// + /// 省份代码映射 + /// + private static readonly Dictionary ProvinceCodeMap = new() + { + { "11", "北京" }, { "12", "天津" }, { "13", "河北" }, { "14", "山西" }, + { "15", "内蒙古" }, { "21", "辽宁" }, { "22", "吉林" }, { "23", "黑龙江" }, + { "31", "上海" }, { "32", "江苏" }, { "33", "浙江" }, { "34", "安徽" }, + { "35", "福建" }, { "36", "江西" }, { "37", "山东" }, { "41", "河南" }, + { "42", "湖北" }, { "43", "湖南" }, { "44", "广东" }, { "45", "广西" }, + { "46", "海南" }, { "50", "重庆" }, { "51", "四川" }, { "52", "贵州" }, + { "53", "云南" }, { "54", "西藏" }, { "61", "陕西" }, { "62", "甘肃" }, + { "63", "青海" }, { "64", "宁夏" }, { "65", "新疆" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证外国人永久居留身份证是否有效 + /// + /// 外国人永久居留身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 15位格式(旧版) + if (cleaned.Length == 15 && ForeignerId15Regex.IsMatch(cleaned)) + { + return true; + } + + // 18位格式(新版) + if (cleaned.Length == 18 && ForeignerId18Regex.IsMatch(cleaned)) + { + return ValidateCheckDigit18(cleaned); + } + + return false; + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 外国人永久居留身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return ForeignerId15Regex.IsMatch(cleaned) || ForeignerId18Regex.IsMatch(cleaned); + } + + /// + /// 验证是否为15位格式 + /// + /// 外国人永久居留身份证号 + /// 是否为15位格式 + public static bool Is15Digit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return ForeignerId15Regex.IsMatch(idCard.ToUpper().Trim()); + } + + /// + /// 验证是否为18位格式 + /// + /// 外国人永久居留身份证号 + /// 是否为18位格式 + public static bool Is18Digit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return cleaned.Length == 18 && ForeignerId18Regex.IsMatch(cleaned); + } + + /// + /// 验证18位校验码 + /// + private static bool ValidateCheckDigit18(string idCard) + { + if (idCard.Length != 18) + { + return false; + } + + // 字母转换(A=10, B=11, ..., Z=35) + char firstChar = char.ToUpper(idCard[0]); + int firstValue; + if (firstChar >= 'A' && firstChar <= 'Z') + { + firstValue = firstChar - 'A' + 10; + } + else + { + return false; + } + + // 计算加权和 + int sum = 0; + + // 第一位字母的权重处理 + sum += (firstValue / 10) * Weights[0]; + sum += (firstValue % 10) * Weights[1]; + + // 数字部分 + for (int i = 1; i < 17; i++) + { + if (!char.IsDigit(idCard[i])) + { + return false; + } + sum += (idCard[i] - '0') * Weights[i + 1]; + } + + // 计算校验码 + char expectedCheck = CheckCodes[sum % 11]; + return char.ToUpper(idCard[17]) == expectedCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国籍代码(15位格式前3位) + /// + /// 外国人永久居留身份证号 + /// 国籍代码 + public static string? GetNationalityCode(string? idCard) + { + if (Is15Digit(idCard)) + { + return idCard!.Substring(0, 3).ToUpper(); + } + + return null; + } + + /// + /// 获取国籍名称 + /// + /// 外国人永久居留身份证号 + /// 国籍名称 + public static string? GetNationality(string? idCard) + { + string? code = GetNationalityCode(idCard); + if (code == null) + { + return null; + } + + return NationalityMap.TryGetValue(code, out string? nationality) ? nationality : code; + } + + /// + /// 获取省份代码(18位格式的第2-3位) + /// + /// 外国人永久居留身份证号 + /// 省份代码 + public static string? GetProvinceCode(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + return idCard!.Substring(1, 2); + } + + /// + /// 获取省份名称 + /// + /// 外国人永久居留身份证号 + /// 省份名称 + public static string? GetProvince(string? idCard) + { + string? code = GetProvinceCode(idCard); + if (code == null) + { + return null; + } + + return ProvinceCodeMap.TryGetValue(code, out string? province) ? province : null; + } + + /// + /// 获取出生日期(18位格式) + /// + /// 外国人永久居留身份证号 + /// 出生日期 + public static DateTime? GetBirthday(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + string cleaned = idCard!.Substring(1); // 去掉首字母 + int year = int.Parse(cleaned.Substring(5, 4)); + int month = int.Parse(cleaned.Substring(9, 2)); + int day = int.Parse(cleaned.Substring(11, 2)); + + try + { + return new DateTime(year, month, day); + } + catch + { + return null; + } + } + + /// + /// 获取性别(18位格式) + /// + /// 外国人永久居留身份证号 + /// 性别(1男2女) + public static int? GetGender(string? idCard) + { + if (!Is18Digit(idCard)) + { + return null; + } + + int genderDigit = idCard![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 外国人永久居留身份证号 + /// 性别 + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化外国人永久居留身份证(统一大写) + /// + /// 外国人永久居留身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 外国人永久居留身份证脱敏 + /// 15位:USA********* + /// 18位:A110**********1 + /// + /// 外国人永久居留身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + + if (cleaned.Length == 15) + { + return cleaned.Substring(0, 3) + "***********" + cleaned.Substring(14); + } + + if (cleaned.Length == 18) + { + return cleaned.Substring(0, 4) + "***********" + cleaned.Substring(15); + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs b/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs new file mode 100644 index 0000000..295bbbb --- /dev/null +++ b/EasyTool.Core/BusinessCategory/HKIdCardUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 香港身份证工具类 + /// + public static class HKIdCardUtil + { + #region 常量与私有字段 + + /// + /// 香港身份证正则表达式 + /// 格式:1-2个英文字母 + 6位数字 + 括号内1位校验码 + /// 例如:A123456(7), AB123456(7) + /// + private static readonly Regex HKIdCardRegex = new( + @"^[A-Z]{1,2}\d{6}\([\dA]\)$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 香港身份证前缀与含义映射 + /// + private static readonly string[] PrefixMeanings = new string[] + { + "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "Y", "Z" + }; + + /// + /// 首字母对应的数字值(A=1, B=2, ..., Z=26) + /// + private static int GetLetterValue(char letter) + { + return char.ToUpper(letter) - 'A' + 1; + } + + #endregion + + #region 验证方法 + + /// + /// 验证香港身份证是否有效 + /// + /// 香港身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 检查格式 + if (!HKIdCardRegex.IsMatch(cleaned)) + { + return false; + } + + // 验证校验码 + return ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 香港身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return HKIdCardRegex.IsMatch(idCard.ToUpper().Trim()); + } + + /// + /// 验证校验码 + /// + private static bool ValidateCheckDigit(string idCard) + { + // 提取校验码(括号内的字符) + int parenStart = idCard.IndexOf('('); + int parenEnd = idCard.IndexOf(')'); + if (parenStart < 0 || parenEnd < 0 || parenEnd <= parenStart) + { + return false; + } + + string checkChar = idCard.Substring(parenStart + 1, parenEnd - parenStart - 1); + if (checkChar.Length != 1) + { + return false; + } + + // 计算校验码 + char? expectedCheck = CalculateCheckDigit(idCard); + if (expectedCheck == null) + { + return false; + } + + return char.ToUpper(checkChar[0]) == expectedCheck.Value; + } + + /// + /// 计算校验码 + /// + /// 香港身份证号(含括号格式) + /// 校验码字符 + public static char? CalculateCheckDigit(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return null; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 提取字母部分和数字部分 + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + if (digitStart < 0) + { + return null; + } + + string letters = cleaned.Substring(0, digitStart); + string digits = cleaned.Substring(digitStart, 6); + + // 计算加权和 + int sum = 0; + int weight = 9 - (2 - letters.Length); // 根据字母数量调整起始权重 + + // 如果只有一个字母,第一位按36处理(相当于前面有一个空位,值为36) + if (letters.Length == 1) + { + sum += 36 * 9; + sum += GetLetterValue(letters[0]) * 8; + } + else if (letters.Length == 2) + { + sum += GetLetterValue(letters[0]) * 9; + sum += GetLetterValue(letters[1]) * 8; + } + else + { + return null; + } + + // 数字部分权重为7到2 + int[] digitWeights = { 7, 6, 5, 4, 3, 2 }; + for (int i = 0; i < 6; i++) + { + sum += (digits[i] - '0') * digitWeights[i]; + } + + // 计算校验码 + int remainder = sum % 11; + int checkValue; + + if (remainder == 0) + { + checkValue = 0; + } + else + { + checkValue = 11 - remainder; + } + + // 返回校验码字符 + if (checkValue == 10) + { + return 'A'; + } + else + { + return (char)('0' + checkValue); + } + } + + #endregion + + #region 信息提取 + + /// + /// 获取身份证前缀字母 + /// + /// 香港身份证号 + /// 前缀字母 + public static string? GetPrefix(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + return digitStart > 0 ? cleaned.Substring(0, digitStart) : null; + } + + /// + /// 获取数字部分(6位) + /// + /// 香港身份证号 + /// 6位数字 + public static string? GetDigitPart(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int digitStart = -1; + for (int i = 0; i < cleaned.Length; i++) + { + if (char.IsDigit(cleaned[i])) + { + digitStart = i; + break; + } + } + + return digitStart >= 0 ? cleaned.Substring(digitStart, 6) : null; + } + + /// + /// 获取校验码 + /// + /// 香港身份证号 + /// 校验码字符 + public static char? GetCheckDigit(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int parenStart = cleaned.IndexOf('('); + int parenEnd = cleaned.IndexOf(')'); + + if (parenStart < 0 || parenEnd < 0 || parenEnd <= parenStart) + { + return null; + } + + return cleaned[parenStart + 1]; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化香港身份证(统一大写,带括号) + /// + /// 香港身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 格式化为标准格式(确保括号正确) + /// + /// 香港身份证号 + /// 标准格式的身份证号 + public static string? Format(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + return cleaned; + } + + /// + /// 香港身份证脱敏:A12***(7) + /// + /// 香港身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + int parenStart = cleaned.IndexOf('('); + + if (parenStart < 7) + { + return null; + } + + // 保留前缀+2位数字,中间用*替代,保留校验码 + string prefix = cleaned.Substring(0, parenStart - 4); + string suffix = cleaned.Substring(parenStart); + + return prefix + "****" + suffix; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机香港身份证号(仅供测试使用) + /// + /// 前缀字母(可选,默认随机) + /// 香港身份证号 + public static string GenerateRandom(string? prefix = null) + { + const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + + // 前缀 + string prefixLetters; + if (string.IsNullOrEmpty(prefix)) + { + int letterCount = MathCategory.RandomUtil.RandomInt(1, 3); + prefixLetters = ""; + for (int i = 0; i < letterCount; i++) + { + prefixLetters += MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()); + } + } + else + { + prefixLetters = prefix.ToUpper(); + } + + // 6位数字 + string numberPart = ""; + for (int i = 0; i < 6; i++) + { + numberPart += MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray()); + } + + // 计算校验码 + string tempId = prefixLetters + numberPart + "(0)"; + char? checkDigit = CalculateCheckDigit(tempId); + + return $"{prefixLetters}{numberPart}({checkDigit ?? '0'})"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ICCIDUtil.cs b/EasyTool.Core/BusinessCategory/ICCIDUtil.cs new file mode 100644 index 0000000..0c5ba59 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ICCIDUtil.cs @@ -0,0 +1,412 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// SIM卡ICCID工具类 + /// ICCID (Integrated Circuit Card Identifier) 是SIM卡的唯一识别号 + /// + public static class ICCIDUtil + { + #region 常量与私有字段 + + /// + /// ICCID正则表达式(19-20位数字) + /// + private static readonly Regex ICCIDRegex = new( + @"^\d{19,20}$", + RegexOptions.Compiled); + + /// + /// 移动国家代码(MCC)映射 + /// + private static readonly Dictionary MccMap = new() + { + { "460", "中国" }, + { "001", "美国" }, + { "004", "阿富汗" }, + { "208", "法国" }, + { "234", "英国" }, + { "262", "德国" }, + { "310", "美国" }, + { "440", "日本" }, + { "450", "韩国" }, + { "505", "澳大利亚" }, + { "530", "新西兰" }, + { "724", "巴西" } + }; + + /// + /// 中国移动网络代码(MNC)映射 + /// + private static readonly Dictionary ChinaMncMap = new() + { + { "00", "中国移动" }, + { "02", "中国移动" }, + { "04", "中国移动" }, + { "07", "中国移动" }, + { "08", "中国移动" }, + { "01", "中国联通" }, + { "06", "中国联通" }, + { "09", "中国联通" }, + { "03", "中国电信" }, + { "05", "中国电信" }, + { "11", "中国电信" }, + { "15", "中国广电" } + }; + + /// + /// Luhn算法校验码权重 + /// + private static readonly int[] LuhnWeights = { 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 }; + + #endregion + + #region 验证方法 + + /// + /// 验证ICCID是否有效 + /// + /// ICCID号 + /// 是否有效 + public static bool IsValid(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return false; + } + + return ValidateLuhn(iccid!); + } + + /// + /// 验证ICCID格式 + /// + /// ICCID号 + /// 格式是否正确 + public static bool IsValidFormat(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return false; + } + + string cleaned = iccid.Trim(); + return ICCIDRegex.IsMatch(cleaned); + } + + /// + /// 使用Luhn算法验证ICCID + /// + /// ICCID号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return false; + } + + string cleaned = iccid.Trim(); + int sum = 0; + int length = cleaned.Length; + + // 从右向左,第1位是校验位 + for (int i = length - 2; i >= 0; i--) + { + if (!char.IsDigit(cleaned[i])) + { + return false; + } + + int digit = cleaned[i] - '0'; + int weightIndex = (length - 2 - i); + int multiplier = (weightIndex % 2 == 0) ? 2 : 1; + + digit *= multiplier; + if (digit > 9) + { + digit -= 9; + } + + sum += digit; + } + + int checkDigit = (10 - (sum % 10)) % 10; + int actualCheck = cleaned[length - 1] - '0'; + + return checkDigit == actualCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取移动国家代码(MCC,前3位) + /// + /// ICCID号 + /// 移动国家代码 + public static string? GetMCC(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid!.Substring(0, 3); + } + + /// + /// 获取国家名称 + /// + /// ICCID号 + /// 国家名称 + public static string? GetCountry(string? iccid) + { + string? mcc = GetMCC(iccid); + if (mcc == null) + { + return null; + } + + return MccMap.TryGetValue(mcc, out string? country) ? country : null; + } + + /// + /// 获取移动网络代码(MNC,第4-5位) + /// + /// ICCID号 + /// 移动网络代码 + public static string? GetMNC(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid!.Substring(3, 2); + } + + /// + /// 获取运营商名称(仅支持中国运营商) + /// + /// ICCID号 + /// 运营商名称 + public static string? GetCarrier(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string? mcc = GetMCC(iccid); + if (mcc != "460") + { + return null; // 非中国卡 + } + + string mnc = iccid!.Substring(3, 2); + return ChinaMncMap.TryGetValue(mnc, out string? carrier) ? carrier : null; + } + + /// + /// 判断是否为中国移动 + /// + public static bool IsChinaMobile(string? iccid) => GetCarrier(iccid) == "中国移动"; + + /// + /// 判断是否为中国联通 + /// + public static bool IsChinaUnicom(string? iccid) => GetCarrier(iccid) == "中国联通"; + + /// + /// 判断是否为中国电信 + /// + public static bool IsChinaTelecom(string? iccid) => GetCarrier(iccid) == "中国电信"; + + /// + /// 判断是否为中国广电 + /// + public static bool IsChinaBroadnet(string? iccid) => GetCarrier(iccid) == "中国广电"; + + /// + /// 获取发卡省份代码(第9-10位) + /// + /// ICCID号 + /// 省份代码 + public static string? GetProvinceCode(string? iccid) + { + if (!IsValidFormat(iccid) || iccid!.Length < 10) + { + return null; + } + + return iccid.Substring(8, 2); + } + + /// + /// 获取序列号(第11-19位,不含校验位) + /// + /// ICCID号 + /// 序列号 + public static string? GetSerialNumber(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + int length = iccid!.Length; + return iccid.Substring(10, length - 11); + } + + /// + /// 获取校验位(最后一位) + /// + /// ICCID号 + /// 校验位 + public static int? GetCheckDigit(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return iccid![iccid.Length - 1] - '0'; + } + + /// + /// 解析ICCID结构 + /// + /// ICCID号 + /// ICCID结构信息 + public static ICCDInfo? Parse(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + return new ICCDInfo + { + MCC = GetMCC(iccid), + Country = GetCountry(iccid), + MNC = GetMNC(iccid), + Carrier = GetCarrier(iccid), + ProvinceCode = GetProvinceCode(iccid), + SerialNumber = GetSerialNumber(iccid), + CheckDigit = GetCheckDigit(iccid) + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化ICCID(去除空格和分隔符) + /// + /// ICCID号 + /// 格式化后的ICCID + public static string? Normalize(string? iccid) + { + if (string.IsNullOrWhiteSpace(iccid)) + { + return null; + } + + string cleaned = iccid.Trim(); + return ICCIDRegex.IsMatch(cleaned) ? cleaned : null; + } + + /// + /// 格式化为易读格式:898600 00 00 1234567890 + /// + /// ICCID号 + /// 格式化后的ICCID + public static string? Format(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string cleaned = iccid!.Trim(); + if (cleaned.Length == 19) + { + return $"{cleaned.Substring(0, 6)} {cleaned.Substring(6, 2)} {cleaned.Substring(8, 2)} {cleaned.Substring(10)}"; + } + else if (cleaned.Length == 20) + { + return $"{cleaned.Substring(0, 6)} {cleaned.Substring(6, 2)} {cleaned.Substring(8, 3)} {cleaned.Substring(11)}"; + } + + return cleaned; + } + + /// + /// ICCID脱敏:898600****7890 + /// + /// ICCID号 + /// 脱敏后的ICCID + public static string? Mask(string? iccid) + { + if (!IsValidFormat(iccid)) + { + return null; + } + + string cleaned = iccid!.Trim(); + int length = cleaned.Length; + + // 保留前6位和后4位 + return cleaned.Substring(0, 6) + new string('*', length - 10) + cleaned.Substring(length - 4); + } + + #endregion + } + + /// + /// ICCID结构信息 + /// + public class ICCDInfo + { + /// + /// 移动国家代码(MCC) + /// + public string? MCC { get; set; } + + /// + /// 国家名称 + /// + public string? Country { get; set; } + + /// + /// 移动网络代码(MNC) + /// + public string? MNC { get; set; } + + /// + /// 运营商名称 + /// + public string? Carrier { get; set; } + + /// + /// 省份代码 + /// + public string? ProvinceCode { get; set; } + + /// + /// 序列号 + /// + public string? SerialNumber { get; set; } + + /// + /// 校验位 + /// + public int? CheckDigit { get; set; } + } +} diff --git a/EasyTool.Core/BusinessCategory/IMEIUtil.cs b/EasyTool.Core/BusinessCategory/IMEIUtil.cs new file mode 100644 index 0000000..7d88853 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IMEIUtil.cs @@ -0,0 +1,347 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// IMEI(国际移动设备识别号)工具类 + /// + public static class IMEIUtil + { + #region 常量与私有字段 + + /// + /// IMEI正则表达式(15位数字) + /// + private static readonly Regex IMEIRegex = new(@"^\d{15}$", RegexOptions.Compiled); + + /// + /// IMEI SV正则表达式(16位数字,含软件版本) + /// + private static readonly Regex IMEISvRegex = new(@"^\d{16}$", RegexOptions.Compiled); + + /// + /// TAC(类型分配码)与制造商映射(部分) + /// + private static readonly (string Prefix, string Manufacturer)[] TacPrefixMap = + { + ("01", "Apple"), + ("35", "Samsung"), + ("86", "Samsung"), + ("01", "Nokia"), + ("35", "Nokia"), + ("352", "Sony"), + ("353", "Sony"), + ("354", "Sony"), + ("355", "Sony"), + ("356", "Sony"), + ("358", "Huawei"), + ("359", "Huawei"), + ("861", "Xiaomi"), + ("862", "Xiaomi"), + ("865", "Xiaomi"), + ("866", "Xiaomi"), + ("352", "LG"), + ("353", "LG"), + ("355", "LG"), + ("356", "LG"), + ("353", "HTC"), + ("354", "HTC"), + ("355", "HTC"), + ("357", "HTC"), + ("358", "HTC"), + ("359", "HTC"), + ("010", "Apple"), + ("011", "Apple"), + ("012", "Apple"), + ("013", "Apple"), + ("014", "Apple"), + ("015", "Apple"), + ("016", "Apple"), + ("017", "Apple"), + ("018", "Apple"), + ("019", "Apple") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证IMEI是否有效(15位,含Luhn校验) + /// + /// IMEI号 + /// 是否有效 + public static bool IsValid(string? imei) + { + if (!IsValidFormat(imei)) + { + return false; + } + + return ValidateLuhn(imei!); + } + + /// + /// 仅验证IMEI格式(不校验Luhn) + /// + /// IMEI号 + /// 格式是否正确 + public static bool IsValidFormat(string? imei) + { + if (string.IsNullOrWhiteSpace(imei)) + { + return false; + } + + return IMEIRegex.IsMatch(imei); + } + + /// + /// 验证IMEI SV是否有效(16位) + /// + /// IMEI SV号 + /// 是否有效 + public static bool IsValidSv(string? imeiSv) + { + if (string.IsNullOrWhiteSpace(imeiSv)) + { + return false; + } + + return IMEISvRegex.IsMatch(imeiSv); + } + + /// + /// 使用Luhn算法验证IMEI + /// + /// IMEI号 + /// 是否通过Luhn校验 + public static bool ValidateLuhn(string? imei) + { + if (string.IsNullOrWhiteSpace(imei) || imei.Length != 15) + { + return false; + } + + int sum = 0; + for (int i = 0; i < 15; i++) + { + if (!char.IsDigit(imei[i])) + { + return false; + } + + int digit = imei[i] - '0'; + + // 偶数位置(从0开始)乘以2,奇数位置不变 + // IMEI的Luhn算法:从右向左,偶数位×2 + if (i % 2 == 1) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + } + + return sum % 10 == 0; + } + + /// + /// 计算Luhn校验位 + /// + /// 不含校验位的14位IMEI + /// 校验位(0-9),计算失败返回-1 + public static int CalculateCheckDigit(string? imei14) + { + if (string.IsNullOrWhiteSpace(imei14) || imei14.Length != 14) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 14; i++) + { + if (!char.IsDigit(imei14[i])) + { + return -1; + } + + int digit = imei14[i] - '0'; + + if (i % 2 == 1) + { + digit *= 2; + if (digit > 9) + { + digit -= 9; + } + } + + sum += digit; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 信息提取 + + /// + /// 获取TAC(类型分配码,前8位) + /// + /// IMEI号 + /// TAC码 + public static string? GetTAC(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei!.Substring(0, 8); + } + + /// + /// 获取制造商(根据TAC前缀推测) + /// + /// IMEI号 + /// 制造商名称 + public static string? GetManufacturer(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + string tac = imei!.Substring(0, 8); + + // 查找最长匹配的前缀 + for (int len = Math.Min(3, tac.Length); len >= 1; len--) + { + string prefix = tac.Substring(0, len); + foreach (var mapping in TacPrefixMap) + { + if (mapping.Prefix == prefix) + { + return mapping.Manufacturer; + } + } + } + + return null; + } + + /// + /// 获取序列号(SNR,第9-14位) + /// + /// IMEI号 + /// 序列号 + public static string? GetSerialNumber(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei!.Substring(8, 6); + } + + /// + /// 获取校验位(第15位) + /// + /// IMEI号 + /// 校验位 + public static int? GetCheckDigit(string? imei) + { + if (!IsValidFormat(imei)) + { + return null; + } + + return imei![14] - '0'; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化IMEI(AA-BBBBBB-CCCCCC-D) + /// + /// IMEI号 + /// 格式化后的IMEI + public static string? Format(string? imei) + { + string? normalized = Normalize(imei); + if (normalized == null || normalized.Length != 15) + { + return null; + } + + return $"{normalized.Substring(0, 2)}-{normalized.Substring(2, 6)}-{normalized.Substring(8, 6)}-{normalized[14]}"; + } + + /// + /// 格式化IMEI(去除分隔符) + /// + /// IMEI号 + /// 清理后的IMEI + public static string? Normalize(string? imei) + { + if (string.IsNullOrWhiteSpace(imei)) + { + return null; + } + + string cleaned = Regex.Replace(imei, @"[^\d]", ""); + return cleaned.Length == 15 ? cleaned : null; + } + + /// + /// IMEI脱敏:35****6 + /// + /// IMEI号 + /// 脱敏后的IMEI + public static string? Mask(string? imei) + { + string? normalized = Normalize(imei); + if (normalized == null) + { + return null; + } + + return normalized.Substring(0, 2) + "***********" + normalized[14]; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机IMEI(仅供测试使用) + /// + /// TAC码(可选,默认随机) + /// 15位IMEI + public static string GenerateRandom(string? tac = null) + { + // TAC(8位) + string tacCode = tac ?? MathCategory.RandomUtil.RandomDigitString(8); + + // 序列号(6位) + string serial = MathCategory.RandomUtil.RandomDigitString(6); + + // 计算校验位 + int checkDigit = CalculateCheckDigit(tacCode + serial); + + return tacCode + serial + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/IPv6Util.cs b/EasyTool.Core/BusinessCategory/IPv6Util.cs new file mode 100644 index 0000000..945679e --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IPv6Util.cs @@ -0,0 +1,269 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// IPv6 地址工具类 + /// 用于验证和处理 IPv6 地址 + /// + public static class IPv6Util + { + /// + /// IPv6 正则表达式 + /// + private static readonly Regex IPv6Regex = new( + @"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$", + RegexOptions.Compiled); + + /// + /// 验证是否为有效的 IPv6 地址 + /// + /// IP 地址 + /// 是否有效 + public static bool IsValid(string? address) + { + if (string.IsNullOrWhiteSpace(address)) + return false; + + return IPv6Regex.IsMatch(address.Trim()); + } + + /// + /// 压缩 IPv6 地址(移除前导零和连续零块) + /// + /// IPv6 地址 + /// 压缩后的地址 + public static string Compress(string address) + { + if (!IsValid(address)) + throw new ArgumentException("无效的 IPv6 地址", nameof(address)); + + try + { + var ip = System.Net.IPAddress.Parse(address); + return ip.IsIPv6LinkLocal ? address : ip.ToString(); + } + catch + { + return address; + } + } + + /// + /// 展开 IPv6 地址(补全省略的零) + /// + /// IPv6 地址 + /// 展开后的地址 + public static string Expand(string address) + { + if (!IsValid(address)) + throw new ArgumentException("无效的 IPv6 地址", nameof(address)); + + try + { + var ip = System.Net.IPAddress.Parse(address); + var bytes = ip.GetAddressBytes(); + + var result = new System.Text.StringBuilder(); + for (int i = 0; i < 16; i += 2) + { + if (i > 0) result.Append(':'); + result.Append($"{bytes[i]:x2}{bytes[i + 1]:x2}"); + } + + return result.ToString(); + } + catch + { + return address; + } + } + + /// + /// 判断是否为本地链接地址(fe80::/10) + /// + /// IPv6 地址 + /// 是否为本地链接地址 + public static bool IsLinkLocal(string? address) + { + if (!IsValid(address)) + return false; + + try + { + var ip = System.Net.IPAddress.Parse(address); + return ip.IsIPv6LinkLocal; + } + catch + { + return false; + } + } + + /// + /// 判断是否为回环地址(::1) + /// + /// IPv6 地址 + /// 是否为回环地址 + public static bool IsLoopback(string? address) + { + if (!IsValid(address)) + return false; + + return System.Net.IPAddress.TryParse(address, out var ip) && + System.Net.IPAddress.IsLoopback(ip); + } + + /// + /// 判断是否为私有地址 + /// fc00::/7 (Unique Local Address) + /// + /// IPv6 地址 + /// 是否为私有地址 + public static bool IsPrivate(string? address) + { + if (!IsValid(address)) + return false; + + var expanded = Expand(address).Replace(":", "").ToLower(); + return expanded.StartsWith("fc") || expanded.StartsWith("fd"); + } + + /// + /// 判断是否为多播地址(ff00::/8) + /// + /// IPv6 地址 + /// 是否为多播地址 + public static bool IsMulticast(string? address) + { + if (!IsValid(address)) + return false; + + return address!.TrimStart().StartsWith("ff", StringComparison.OrdinalIgnoreCase); + } + + /// + /// IPv4 映射的 IPv6 地址转换为 IPv4 + /// + /// IPv6 地址(::ffff:192.168.1.1 格式) + /// IPv4 地址 + public static string? ToIPv4(string? address) + { + if (!IsValid(address)) + return null; + + try + { + var ip = System.Net.IPAddress.Parse(address); + if (ip.IsIPv4MappedToIPv6) + { + return ip.MapToIPv4().ToString(); + } + return null; + } + catch + { + return null; + } + } + + /// + /// IPv4 转换为 IPv6 映射地址 + /// + /// IPv4 地址 + /// IPv6 映射地址 + public static string? FromIPv4(string? ipv4Address) + { + if (string.IsNullOrWhiteSpace(ipv4Address)) + return null; + + try + { + var ip = System.Net.IPAddress.Parse(ipv4Address); + if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + return ip.MapToIPv6().ToString(); + } + return null; + } + catch + { + return null; + } + } + + /// + /// 获取 IPv6 地址类型 + /// + /// IPv6 地址 + /// 地址类型 + public static IPv6AddressType GetAddressType(string? address) + { + if (!IsValid(address)) + return IPv6AddressType.Unknown; + + if (IsLoopback(address)) + return IPv6AddressType.Loopback; + + if (IsLinkLocal(address)) + return IPv6AddressType.LinkLocal; + + if (IsPrivate(address)) + return IPv6AddressType.UniqueLocal; + + if (IsMulticast(address)) + return IPv6AddressType.Multicast; + + if (address!.StartsWith("2", StringComparison.OrdinalIgnoreCase) || + address.StartsWith("3", StringComparison.OrdinalIgnoreCase)) + return IPv6AddressType.GlobalUnicast; + + if (address.StartsWith("::", StringComparison.Ordinal)) + return IPv6AddressType.Unspecified; + + return IPv6AddressType.GlobalUnicast; + } + } + + /// + /// IPv6 地址类型 + /// + public enum IPv6AddressType + { + /// + /// 未知 + /// + Unknown, + + /// + /// 未指定地址(::) + /// + Unspecified, + + /// + /// 回环地址(::1) + /// + Loopback, + + /// + /// 本地链接地址(fe80::/10) + /// + LinkLocal, + + /// + /// 唯一本地地址(fc00::/7) + /// + UniqueLocal, + + /// + /// 全球单播地址 + /// + GlobalUnicast, + + /// + /// 多播地址(ff00::/8) + /// + Multicast + } +} diff --git a/EasyTool.Core/BusinessCategory/ISBNUtil.cs b/EasyTool.Core/BusinessCategory/ISBNUtil.cs new file mode 100644 index 0000000..4ac33c8 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ISBNUtil.cs @@ -0,0 +1,576 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// ISBN类型枚举 + /// + public enum ISBNType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// ISBN-10(10位) + /// + ISBN10 = 1, + + /// + /// ISBN-13(13位) + /// + ISBN13 = 2 + } + + /// + /// ISBN书号工具类 + /// + public static class ISBNUtil + { + #region 常量与私有字段 + + /// + /// ISBN-10正则表达式(可含分隔符) + /// + private static readonly Regex ISBN10Regex = new Regex( + @"^(\d{1,5}[-\s]?)?\d{1,7}[-\s]?\d{1,7}[-\s]?[\dXx]$", + RegexOptions.Compiled); + + /// + /// ISBN-13正则表达式(可含分隔符) + /// + private static readonly Regex ISBN13Regex = new Regex( + @"^97[89][-\s]?\d{1,5}[-\s]?\d{1,7}[-\s]?\d{1,7}[-\s]?\d$", + RegexOptions.Compiled); + + /// + /// 纯数字ISBN-10正则 + /// + private static readonly Regex ISBN10CleanRegex = new Regex( + @"^\d{9}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 纯数字ISBN-13正则 + /// + private static readonly Regex ISBN13CleanRegex = new Regex( + @"^97[89]\d{10}$", + RegexOptions.Compiled); + + /// + /// ISBN前缀与国家/地区/语言映射 + /// + private static readonly (string Prefix, string Region)[] PrefixRegionMap = + { + ("0", "英语国家"), ("1", "英语国家"), + ("2", "法语国家"), + ("3", "德语国家"), + ("4", "日本"), + ("5", "前苏联/俄罗斯"), + ("7", "中国"), + ("80", "前捷克斯洛伐克"), ("85", "巴西"), + ("87", "丹麦"), + ("88", "意大利"), + ("90", "荷兰"), ("91", "瑞典"), ("92", "国际组织"), + ("93", "印度"), ("94", "荷兰"), + ("952", "芬兰"), ("953", "克罗地亚"), + ("960", "希腊"), ("961", "斯洛文尼亚"), ("962", "香港"), + ("963", "匈牙利"), ("964", "伊朗"), ("965", "以色列"), + ("966", "乌克兰"), ("967", "马来西亚"), ("968", "墨西哥"), + ("969", "巴基斯坦"), ("970", "墨西哥"), ("971", "菲律宾"), + ("972", "葡萄牙"), ("973", "罗马尼亚"), ("974", "泰国"), + ("975", "土耳其"), ("976", "加勒比海地区"), ("977", "埃及"), + ("978", "尼日利亚"), ("979", "印度尼西亚"), + ("980", "委内瑞拉"), ("981", "新加坡"), ("982", "南太平洋地区"), + ("983", "马来西亚"), ("984", "孟加拉"), ("985", "白俄罗斯"), + ("986", "台湾"), ("987", "阿根廷"), ("988", "香港"), + ("989", "葡萄牙"), ("9927", "沙特阿拉伯"), ("9933", "伊朗"), + ("9937", "尼泊尔"), ("9939", "亚美尼亚"), ("9940", "卡塔尔"), + ("9942", "阿塞拜疆"), ("9943", "塔吉克斯坦"), ("9944", "斯洛伐克"), + ("9945", "朝鲜"), ("9946", "阿尔巴尼亚"), ("9947", "阿联酋"), + ("9948", "黎巴嫩"), ("9949", "爱沙尼亚"), ("9950", "叙利亚"), + ("9951", "约旦"), ("9952", "吉尔吉斯斯坦"), ("9953", "巴勒斯坦"), + ("9954", "摩洛哥"), ("9955", "立陶宛"), ("9956", "喀麦隆"), + ("9957", "约旦"), ("9958", "古巴"), ("9959", "阿尔及利亚"), + ("9960", "沙特阿拉伯"), ("9961", "阿曼"), ("9962", "巴林"), + ("9963", "冰岛"), ("9964", "加纳"), ("9965", "科威特"), + ("9966", "肯尼亚"), ("9967", "吉布提"), ("9968", "厄瓜多尔"), + ("9969", "蒙古"), ("9970", "乌干达"), ("9971", "津巴布韦"), + ("9972", "巴拿马"), ("9973", "突尼斯"), ("9974", "塞内加尔"), + ("9975", "罗马尼亚"), ("9976", "巴布亚新几内亚"), ("9977", "哥斯达黎加"), + ("9978", "斯里兰卡"), ("9979", "冰岛"), ("9980", "刚果"), + ("9981", "马达加斯加"), ("9982", "加蓬"), ("9983", "马里"), + ("9984", "马拉维"), ("9985", "爱沙尼亚"), ("9986", "立陶宛"), + ("9987", "坦桑尼亚"), ("9988", "加纳"), ("9989", "马其顿"), + ("99901", "巴哈马"), ("99903", "莫桑比克"), ("99904", "哈萨克斯坦"), + ("99905", "尼泊尔"), ("99906", "马拉维"), ("99908", "澳门") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证ISBN是否有效(自动识别ISBN-10或ISBN-13) + /// + /// ISBN号 + /// 是否有效 + public static bool IsValid(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (cleaned.Length == 10) + { + return IsValidISBN10(cleaned); + } + + if (cleaned.Length == 13) + { + return IsValidISBN13(cleaned); + } + + return false; + } + + /// + /// 验证ISBN-10是否有效 + /// + /// ISBN号 + /// 是否有效 + public static bool IsValidISBN10(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (!ISBN10CleanRegex.IsMatch(cleaned)) + { + return false; + } + + // 计算校验位 + int sum = 0; + for (int i = 0; i < 9; i++) + { + sum += (cleaned[i] - '0') * (10 - i); + } + + // 最后一位可能是X(代表10) + char lastChar = char.ToUpper(cleaned[9]); + int checkDigit = lastChar == 'X' ? 10 : (lastChar - '0'); + sum += checkDigit; + + return sum % 11 == 0; + } + + /// + /// 验证ISBN-13是否有效 + /// + /// ISBN号 + /// 是否有效 + public static bool IsValidISBN13(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + + if (!ISBN13CleanRegex.IsMatch(cleaned)) + { + return false; + } + + // ISBN-13必须以978或979开头 + if (!cleaned.StartsWith("978") && !cleaned.StartsWith("979")) + { + return false; + } + + // 计算校验位 + int sum = 0; + for (int i = 0; i < 12; i++) + { + int digit = cleaned[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + int checkDigit = (10 - (sum % 10)) % 10; + return checkDigit == (cleaned[12] - '0'); + } + + /// + /// 验证ISBN格式(不计算校验位) + /// + /// ISBN号 + /// 格式是否正确 + public static bool IsValidFormat(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn); + return ISBN10CleanRegex.IsMatch(cleaned) || ISBN13CleanRegex.IsMatch(cleaned); + } + + #endregion + + #region 类型识别 + + /// + /// 获取ISBN类型 + /// + /// ISBN号 + /// ISBN类型 + public static ISBNType GetISBNType(string? isbn) + { + if (!IsValid(isbn)) + { + return ISBNType.Unknown; + } + + string cleaned = CleanISBN(isbn); + return cleaned.Length == 10 ? ISBNType.ISBN10 : ISBNType.ISBN13; + } + + #endregion + + #region 转换方法 + + /// + /// 将ISBN-10转换为ISBN-13 + /// + /// ISBN-10号 + /// ISBN-13号,转换失败返回null + public static string? ConvertToISBN13(string? isbn10) + { + if (!IsValidISBN10(isbn10)) + { + return null; + } + + string cleaned = CleanISBN(isbn10!); + + // 添加前缀978 + string isbn13 = "978" + cleaned.Substring(0, 9); + + // 计算新的校验位 + int sum = 0; + for (int i = 0; i < 12; i++) + { + int digit = isbn13[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + int checkDigit = (10 - (sum % 10)) % 10; + return isbn13 + checkDigit; + } + + /// + /// 将ISBN-13转换为ISBN-10(仅适用于978前缀) + /// + /// ISBN-13号 + /// ISBN-10号,转换失败返回null + public static string? ConvertToISBN10(string? isbn13) + { + if (!IsValidISBN13(isbn13)) + { + return null; + } + + string cleaned = CleanISBN(isbn13!); + + // 只有978前缀才能转换为ISBN-10 + if (!cleaned.StartsWith("978")) + { + return null; + } + + // 去掉前缀978和最后一位校验位 + string isbn10Body = cleaned.Substring(3, 9); + + // 计算ISBN-10校验位 + int sum = 0; + for (int i = 0; i < 9; i++) + { + sum += (isbn10Body[i] - '0') * (10 - i); + } + + int checkValue = 11 - (sum % 11); + char checkChar; + if (checkValue == 10) + { + checkChar = 'X'; + } + else if (checkValue == 11) + { + checkChar = '0'; + } + else + { + checkChar = (char)('0' + checkValue); + } + + return isbn10Body + checkChar; + } + + #endregion + + #region 信息提取 + + /// + /// 获取国家/地区名称 + /// + /// ISBN号 + /// 国家/地区名称 + public static string? GetRegion(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + // ISBN-13需要去掉978/979前缀 + string prefix = cleaned.Length == 13 ? cleaned.Substring(3) : cleaned; + + // 查找最长匹配的前缀 + for (int len = Math.Min(5, prefix.Length); len >= 1; len--) + { + string searchPrefix = prefix.Substring(0, len); + foreach (var mapping in PrefixRegionMap) + { + if (mapping.Prefix == searchPrefix) + { + return mapping.Region; + } + } + } + + return null; + } + + /// + /// 判断是否为中国出版物 + /// + /// ISBN号 + /// 是否为中国出版物 + public static bool IsChinaISBN(string? isbn) + { + if (!IsValid(isbn)) + { + return false; + } + + string cleaned = CleanISBN(isbn!); + + // ISBN-13: 978-7 或 979-7 + // ISBN-10: 7开头 + if (cleaned.Length == 13) + { + return cleaned.StartsWith("9787") || cleaned.StartsWith("9797"); + } + else + { + return cleaned.StartsWith("7"); + } + } + + /// + /// 计算ISBN-10校验位 + /// + /// 不含校验位的9位数字 + /// 校验位(0-10,10表示X),计算失败返回-1 + public static int CalculateISBN10CheckDigit(string? isbn9) + { + if (string.IsNullOrWhiteSpace(isbn9) || isbn9.Length != 9) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 9; i++) + { + if (!char.IsDigit(isbn9[i])) + { + return -1; + } + sum += (isbn9[i] - '0') * (10 - i); + } + + int checkValue = 11 - (sum % 11); + return checkValue == 11 ? 0 : checkValue; + } + + /// + /// 计算ISBN-13校验位 + /// + /// 不含校验位的12位数字 + /// 校验位(0-9),计算失败返回-1 + public static int CalculateISBN13CheckDigit(string? isbn12) + { + if (string.IsNullOrWhiteSpace(isbn12) || isbn12.Length != 12) + { + return -1; + } + + int sum = 0; + for (int i = 0; i < 12; i++) + { + if (!char.IsDigit(isbn12[i])) + { + return -1; + } + int digit = isbn12[i] - '0'; + sum += digit * (i % 2 == 0 ? 1 : 3); + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + + #region 格式化方法 + + /// + /// 清理ISBN(去除分隔符) + /// + /// ISBN号 + /// 清理后的ISBN + public static string CleanISBN(string? isbn) + { + if (string.IsNullOrWhiteSpace(isbn)) + { + return ""; + } + + // 去除空格和横线 + return Regex.Replace(isbn, @"[\s\-]", "").ToUpper(); + } + + /// + /// 格式化ISBN(添加分隔符) + /// + /// ISBN号 + /// 格式化后的ISBN,如978-7-115-12345-6 + public static string? Format(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + if (cleaned.Length == 10) + { + // ISBN-10格式:x-x-xxx-xxxxx-x + return $"{cleaned[0]}-{cleaned[1]}-{cleaned.Substring(2, 3)}-{cleaned.Substring(5, 4)}-{cleaned[9]}"; + } + else + { + // ISBN-13格式:xxx-x-xxx-xxxxx-x + return $"{cleaned.Substring(0, 3)}-{cleaned[3]}-{cleaned.Substring(4, 3)}-{cleaned.Substring(7, 5)}-{cleaned[12]}"; + } + } + + /// + /// 格式化ISBN(使用自定义分隔符) + /// + /// ISBN号 + /// 分隔符 + /// 格式化后的ISBN + public static string? Format(string? isbn, char separator) + { + string? formatted = Format(isbn); + if (formatted == null) + { + return null; + } + + return formatted.Replace('-', separator); + } + + /// + /// ISBN脱敏:978-7-***-*****-* + /// + /// ISBN号 + /// 脱敏后的ISBN + public static string? Mask(string? isbn) + { + if (!IsValid(isbn)) + { + return null; + } + + string cleaned = CleanISBN(isbn!); + + if (cleaned.Length == 10) + { + // 保留第1位和最后1位 + return cleaned[0] + "*******" + cleaned[9]; + } + else + { + // 保留前4位和最后1位 + return cleaned.Substring(0, 4) + "*******" + cleaned[12]; + } + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机ISBN-13(仅供测试使用) + /// + /// 前缀(默认978) + /// ISBN-13号 + public static string GenerateRandomISBN13(string prefix = "978") + { + // 生成12位数字 + string isbn12 = prefix + MathCategory.RandomUtil.RandomDigitString(12 - prefix.Length); + + // 计算校验位 + int checkDigit = CalculateISBN13CheckDigit(isbn12); + + return isbn12 + checkDigit; + } + + /// + /// 生成随机ISBN-10(仅供测试使用) + /// + /// ISBN-10号 + public static string GenerateRandomISBN10() + { + // 生成9位数字 + string isbn9 = MathCategory.RandomUtil.RandomDigitString(9); + + // 计算校验位 + int checkDigit = CalculateISBN10CheckDigit(isbn9); + + if (checkDigit == 10) + { + return isbn9 + "X"; + } + + return isbn9 + checkDigit; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/IdCardUtil.cs b/EasyTool.Core/BusinessCategory/IdCardUtil.cs new file mode 100644 index 0000000..d76621a --- /dev/null +++ b/EasyTool.Core/BusinessCategory/IdCardUtil.cs @@ -0,0 +1,467 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 身份证工具类 + /// + public static class IdCardUtil + { + #region 常量与私有字段 + + /// + /// 18位身份证校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 18位身份证校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 18位身份证正则表达式 + /// + private static readonly Regex Regex18 = new Regex(@"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", RegexOptions.Compiled); + + /// + /// 15位身份证正则表达式 + /// + private static readonly Regex Regex15 = new Regex(@"^[1-9]\d{5}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$", RegexOptions.Compiled); + + /// + /// 省份代码与名称映射 + /// + private static readonly string[] ProvinceCodes = { + "", "北京", "天津", "河北", "山西", "内蒙古", // 11-15 + "", "辽宁", "吉林", "黑龙江", "", // 21-23 + "", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", // 31-37 + "", "河南", "湖北", "湖南", "广东", "广西", "海南", // 41-46 + "", "重庆", "四川", "贵州", "云南", "西藏", // 50-54 + "", "陕西", "甘肃", "青海", "宁夏", "新疆", // 61-65 + "", "台湾", // 71 + "", "香港", "澳门" // 81-82 + }; + + /// + /// 星座日期范围 + /// + private static readonly (int Month, int Day, string Name)[] ZodiacRanges = { + (1, 20, "水瓶座"), (2, 19, "双鱼座"), (3, 21, "白羊座"), + (4, 20, "金牛座"), (5, 21, "双子座"), (6, 22, "巨蟹座"), + (7, 23, "狮子座"), (8, 23, "处女座"), (9, 23, "天秤座"), + (10, 24, "天蝎座"), (11, 23, "射手座"), (12, 22, "摩羯座") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证身份证号是否有效(支持15位和18位) + /// + /// 身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + return idCard.Length == 18 ? IsValid18(idCard) : + idCard.Length == 15 ? IsValid15(idCard) : + false; + } + + /// + /// 验证18位身份证号是否有效 + /// + /// 18位身份证号 + /// 是否有效 + public static bool IsValid18(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length != 18) + { + return false; + } + + if (!Regex18.IsMatch(idCard)) + { + return false; + } + + // 验证日期有效性 + if (!IsValidDate(idCard.Substring(6, 8))) + { + return false; + } + + // 验证校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + char actualCheckCode = char.ToUpper(idCard[17]); + + return expectedCheckCode == actualCheckCode; + } + + /// + /// 验证15位身份证号是否有效 + /// + /// 15位身份证号 + /// 是否有效 + public static bool IsValid15(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length != 15) + { + return false; + } + + if (!Regex15.IsMatch(idCard)) + { + return false; + } + + // 验证日期有效性(15位身份证年份默认为19xx) + string dateStr = "19" + idCard.Substring(6, 6); + return IsValidDate(dateStr); + } + + #endregion + + #region 转换方法 + + /// + /// 将15位身份证号转换为18位 + /// + /// 15位身份证号 + /// 18位身份证号,转换失败返回null + public static string? Convert15To18(string? idCard15) + { + if (!IsValid15(idCard15)) + { + return null; + } + + // 在第6位后插入"19" + string idCard17 = idCard15!.Substring(0, 6) + "19" + idCard15.Substring(6); + + // 计算校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard17[i] - '0') * Weights[i]; + } + + return idCard17 + CheckCodes[sum % 11]; + } + + /// + /// 将18位身份证号转换为15位 + /// + /// 18位身份证号 + /// 15位身份证号,转换失败返回null + public static string? Convert18To15(string? idCard18) + { + if (!IsValid18(idCard18)) + { + return null; + } + + // 移除第6-9位的年份前两位"19"和最后一位校验码 + return idCard18!.Substring(0, 6) + idCard18.Substring(8, 9); + } + + #endregion + + #region 信息提取方法 + + /// + /// 获取出生日期 + /// + /// 身份证号 + /// 出生日期,解析失败返回null + public static DateTime? GetBirthday(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + string dateStr; + if (idCard!.Length == 18) + { + dateStr = idCard.Substring(6, 8); + } + else + { + dateStr = "19" + idCard.Substring(6, 6); + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取年龄 + /// + /// 身份证号 + /// 年龄,解析失败返回null + public static int? GetAge(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + DateTime today = DateTime.Today; + int age = today.Year - birthday.Value.Year; + + // 如果今年生日还没过,年龄减1 + if (today < birthday.Value.AddYears(age)) + { + age--; + } + + return age; + } + + /// + /// 获取性别代码(1男2女) + /// + /// 身份证号 + /// 性别代码,解析失败返回null + public static int? GetGender(string? idCard) + { + if (!IsValid(idCard)) + { + return null; + } + + // 第17位(索引16)表示性别,奇数为男,偶数为女 + int genderDigit; + if (idCard!.Length == 18) + { + genderDigit = idCard[16] - '0'; + } + else + { + genderDigit = idCard[14] - '0'; + } + + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串(男/女) + /// + /// 身份证号 + /// 性别字符串,解析失败返回null + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + if (!gender.HasValue) + { + return null; + } + + return gender.Value == 1 ? "男" : "女"; + } + + /// + /// 获取省份名称 + /// + /// 身份证号 + /// 省份名称,解析失败返回null + public static string? GetProvince(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || idCard.Length < 2) + { + return null; + } + + int provinceCode; + if (!int.TryParse(idCard.Substring(0, 2), out provinceCode)) + { + return null; + } + + if (provinceCode < 0 || provinceCode >= ProvinceCodes.Length) + { + return null; + } + + return ProvinceCodes[provinceCode]; + } + + /// + /// 获取行政区划代码(前6位) + /// + /// 身份证号 + /// 行政区划代码,解析失败返回null + public static string? GetAreaCode(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard) || (idCard.Length != 15 && idCard.Length != 18)) + { + return null; + } + + return idCard.Substring(0, 6); + } + + /// + /// 获取生肖 + /// + /// 身份证号 + /// 生肖,解析失败返回null + public static string? GetChineseZodiac(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + return EasyTool.DateTimeCategory.LunarCalendarUtil.GetChineseZodiac(birthday.Value); + } + + /// + /// 获取星座 + /// + /// 身份证号 + /// 星座,解析失败返回null + public static string? GetZodiac(string? idCard) + { + DateTime? birthday = GetBirthday(idCard); + if (!birthday.HasValue) + { + return null; + } + + return GetZodiacByDate(birthday.Value.Month, birthday.Value.Day); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机身份证号(仅供测试使用) + /// + /// 省份代码(可选,默认随机) + /// 出生日期(可选,默认随机) + /// 性别(可选,1男2女,默认随机) + /// 18位身份证号 + public static string GenerateRandom(string? provinceCode = null, DateTime? birthday = null, int? gender = null) + { + // 省份代码 + string province = provinceCode ?? GetRandomProvinceCode(); + + // 出生日期 + DateTime birth = birthday ?? EasyTool.MathCategory.RandomUtil.GetRandomDateTime( + new DateTime(1950, 1, 1), + new DateTime(2005, 12, 31)); + string birthStr = birth.ToString("yyyyMMdd"); + + // 顺序码(3位)+ 性别 + string sequence = EasyTool.MathCategory.RandomUtil.RandomDigitString(2); + int genderDigit; + if (gender.HasValue && (gender.Value == 1 || gender.Value == 2)) + { + // 指定性别的奇偶性 + int randomDigit = EasyTool.MathCategory.RandomUtil.RandomInt(0, 4); + genderDigit = gender.Value == 1 ? randomDigit * 2 + 1 : randomDigit * 2; + } + else + { + genderDigit = EasyTool.MathCategory.RandomUtil.RandomInt(0, 9); + } + sequence += genderDigit.ToString(); + + // 前17位 + string idCard17 = province + birthStr + sequence; + + // 计算校验码 + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard17[i] - '0') * Weights[i]; + } + + return idCard17 + CheckCodes[sum % 11]; + } + + #endregion + + #region 私有方法 + + /// + /// 验证日期字符串是否有效 + /// + private static bool IsValidDate(string dateStr) + { + if (dateStr.Length != 8) + { + return false; + } + + int year = int.Parse(dateStr.Substring(0, 4)); + int month = int.Parse(dateStr.Substring(4, 2)); + int day = int.Parse(dateStr.Substring(6, 2)); + + if (year < 1900 || year > 2100) + { + return false; + } + + if (month < 1 || month > 12) + { + return false; + } + + int maxDay = DateTime.DaysInMonth(year, month); + return day >= 1 && day <= maxDay; + } + + /// + /// 根据日期获取星座 + /// + private static string GetZodiacByDate(int month, int day) + { + // 星座按日期划分,摩羯座的特殊处理(跨年) + for (int i = ZodiacRanges.Length - 1; i >= 0; i--) + { + var zodiac = ZodiacRanges[i]; + if (month > zodiac.Month || (month == zodiac.Month && day >= zodiac.Day)) + { + return zodiac.Name; + } + } + + // 1月1日到1月19日是摩羯座 + return "摩羯座"; + } + + /// + /// 获取随机省份代码 + /// + private static string GetRandomProvinceCode() + { + int[] validCodes = { 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65 }; + int code = EasyTool.MathCategory.RandomUtil.GetRandomElement(validCodes); + return code.ToString("00"); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs b/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs new file mode 100644 index 0000000..e303422 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/LicensePlateUtil.cs @@ -0,0 +1,717 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 车牌类型枚举 + /// + public enum PlateType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 普通车牌/燃油车牌(7位) + /// + Normal = 1, + + /// + /// 小型新能源车牌(8位,渐变绿色) + /// + NewEnergySmall = 2, + + /// + /// 大型新能源车牌(8位,黄绿双色) + /// + NewEnergyLarge = 3, + + /// + /// 武警车牌 + /// + WJ = 4, + + /// + /// 军队车牌 + /// + Military = 5 + } + + /// + /// 新能源汽车类型枚举 + /// + public enum NewEnergyType + { + /// + /// 纯电动汽车 + /// + PureElectric = 0, + + /// + /// 插电式混合动力汽车(含增程式) + /// + PluginHybrid = 1 + } + + /// + /// 车辆燃料类型枚举 + /// + public enum FuelType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 燃油车(汽油/柴油) + /// + Fuel = 1, + + /// + /// 纯电动汽车 + /// + PureElectric = 2, + + /// + /// 插电式混合动力汽车(含增程式) + /// + PluginHybrid = 3 + } + + /// + /// 车牌号工具类 + /// + public static class LicensePlateUtil + { + #region 常量与私有字段 + + /// + /// 普通车牌正则表达式(7位) + /// 格式:省份简称(1位汉字)+ 发牌机关代号(1位字母)+ 序号(5位字母或数字) + /// + private static readonly Regex NormalPlateRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 小型新能源车牌正则表达式(8位) + /// 格式:省份简称 + 字母 + 5位(第3位为D或F) + /// + private static readonly Regex NewEnergySmallRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][DF][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 大型新能源车牌正则表达式(8位) + /// 格式:省份简称 + 字母 + 5位(第3位或第4-8位包含数字,第8位为D或F) + /// + private static readonly Regex NewEnergyLargeRegex = new Regex( + @"^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝港澳台][A-Z][A-HJ-NP-Z0-9]{5}[DF]$", + RegexOptions.Compiled); + + /// + /// 武警车牌正则表达式 + /// 格式:WJ + 省份代码(2位数字)+ 1位字母 + 4位数字 + /// + private static readonly Regex WJPlateRegex = new Regex( + @"^WJ[0-9]{2}[0-9A-HJ-NP-Z]\d{4}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 军队车牌正则表达式(简化版) + /// + private static readonly Regex MilitaryPlateRegex = new Regex( + @"^[VQZHBSLJKWETCYM][A-Z][A-HJ-NP-Z0-9]{5}$", + RegexOptions.Compiled); + + /// + /// 省份简称与名称映射 + /// + private static readonly Dictionary ProvinceMap = new Dictionary + { + { "京", "北京市" }, { "津", "天津市" }, { "冀", "河北省" }, { "晋", "山西省" }, + { "蒙", "内蒙古自治区" }, { "辽", "辽宁省" }, { "吉", "吉林省" }, { "黑", "黑龙江省" }, + { "沪", "上海市" }, { "苏", "江苏省" }, { "浙", "浙江省" }, { "皖", "安徽省" }, + { "闽", "福建省" }, { "赣", "江西省" }, { "鲁", "山东省" }, { "豫", "河南省" }, + { "鄂", "湖北省" }, { "湘", "湖南省" }, { "粤", "广东省" }, { "桂", "广西壮族自治区" }, + { "琼", "海南省" }, { "川", "四川省" }, { "贵", "贵州省" }, { "云", "云南省" }, + { "藏", "西藏自治区" }, { "陕", "陕西省" }, { "甘", "甘肃省" }, { "青", "青海省" }, + { "宁", "宁夏回族自治区" }, { "新", "新疆维吾尔自治区" }, { "渝", "重庆市" }, + { "港", "香港特别行政区" }, { "澳", "澳门特别行政区" }, { "台", "台湾省" } + }; + + /// + /// 车牌字母与城市映射(部分主要城市) + /// + private static readonly Dictionary> CityMap = new Dictionary> + { + { "京", new Dictionary { { "A", "市区" }, { "B", "出租车" }, { "C", "郊区" }, { "D", "警车" }, { "E", "郊区" }, { "F", "郊区" }, { "G", "郊区" }, { "H", "郊区" }, { "J", "郊区" }, { "K", "郊区" }, { "L", "郊区" }, { "M", "郊区" }, { "N", "市区" }, { "P", "市区" }, { "Q", "市区" }, { "Y", "郊区" } } }, + { "沪", new Dictionary { { "A", "市区" }, { "B", "市区" }, { "C", "郊区" }, { "D", "郊区" }, { "E", "市区" }, { "F", "郊区" }, { "G", "郊区" }, { "H", "郊区" }, { "J", "郊区" }, { "K", "郊区" }, { "L", "郊区" }, { "M", "郊区" }, { "N", "市区" }, { "R", "崇明" } } }, + { "粤", new Dictionary { { "A", "广州市" }, { "B", "深圳市" }, { "C", "珠海市" }, { "D", "汕头市" }, { "E", "佛山市" }, { "F", "韶关市" }, { "G", "湛江市" }, { "H", "肇庆市" }, { "J", "江门市" }, { "K", "茂名市" }, { "L", "惠州市" }, { "M", "梅州市" }, { "N", "汕尾市" }, { "P", "河源市" }, { "Q", "阳江市" }, { "R", "清远市" }, { "S", "东莞市" }, { "T", "中山市" }, { "U", "潮州市" }, { "V", "揭阳市" }, { "W", "云浮市" }, { "X", "顺德区" }, { "Y", "南海区" }, { "Z", "港澳入境" } } }, + { "苏", new Dictionary { { "A", "南京市" }, { "B", "无锡市" }, { "C", "徐州市" }, { "D", "常州市" }, { "E", "苏州市" }, { "F", "南通市" }, { "G", "连云港市" }, { "H", "淮安市" }, { "J", "盐城市" }, { "K", "扬州市" }, { "L", "镇江市" }, { "M", "泰州市" }, { "N", "宿迁市" } } }, + { "浙", new Dictionary { { "A", "杭州市" }, { "B", "宁波市" }, { "C", "温州市" }, { "D", "绍兴市" }, { "E", "湖州市" }, { "F", "嘉兴市" }, { "G", "金华市" }, { "H", "衢州市" }, { "J", "台州市" }, { "K", "丽水市" }, { "L", "舟山市" } } }, + }; + + #endregion + + #region 验证方法 + + /// + /// 验证车牌号是否有效 + /// + /// 车牌号 + /// 是否有效 + public static bool IsValid(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + return IsNormalPlate(normalized) || IsNewEnergyPlate(normalized) || + IsWJPlate(normalized) || IsMilitaryPlate(normalized); + } + + /// + /// 验证是否为普通车牌(7位) + /// + /// 车牌号 + /// 是否为普通车牌 + public static bool IsNormalPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NormalPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为新能源车牌(8位) + /// + /// 车牌号 + /// 是否为新能源车牌 + public static bool IsNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + return IsSmallNewEnergyPlate(normalized) || IsLargeNewEnergyPlate(normalized); + } + + /// + /// 验证是否为小型新能源车牌(8位,第3位为D或F) + /// + /// 车牌号 + /// 是否为小型新能源车牌 + public static bool IsSmallNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NewEnergySmallRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为大型新能源车牌(8位,第8位为D或F) + /// + /// 车牌号 + /// 是否为大型新能源车牌 + public static bool IsLargeNewEnergyPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return NewEnergyLargeRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为武警车牌 + /// + /// 车牌号 + /// 是否为武警车牌 + public static bool IsWJPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return WJPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + /// + /// 验证是否为军队车牌 + /// + /// 车牌号 + /// 是否为军队车牌 + public static bool IsMilitaryPlate(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + return MilitaryPlateRegex.IsMatch(Normalize(plateNumber)!); + } + + #endregion + + #region 类型识别 + + /// + /// 获取车牌类型 + /// + /// 车牌号 + /// 车牌类型 + public static PlateType GetPlateType(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return PlateType.Unknown; + } + + string normalized = Normalize(plateNumber)!; + + if (IsSmallNewEnergyPlate(normalized)) + { + return PlateType.NewEnergySmall; + } + + if (IsLargeNewEnergyPlate(normalized)) + { + return PlateType.NewEnergyLarge; + } + + if (IsNormalPlate(normalized)) + { + return PlateType.Normal; + } + + if (IsWJPlate(normalized)) + { + return PlateType.WJ; + } + + if (IsMilitaryPlate(normalized)) + { + return PlateType.Military; + } + + return PlateType.Unknown; + } + + /// + /// 验证是否为燃油车车牌(普通7位车牌,非新能源) + /// + /// 车牌号 + /// 是否为燃油车车牌 + public static bool IsFuelVehicle(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return false; + } + + string normalized = Normalize(plateNumber)!; + + // 普通车牌(7位)且不是军队/武警车牌 = 燃油车 + return normalized.Length == 7 && IsNormalPlate(normalized); + } + + /// + /// 获取车辆燃料类型 + /// + /// 车牌号 + /// 燃料类型 + public static FuelType GetFuelType(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return FuelType.Unknown; + } + + string normalized = Normalize(plateNumber)!; + + // 燃油车(普通7位车牌) + if (IsFuelVehicle(normalized)) + { + return FuelType.Fuel; + } + + // 新能源车 + if (IsNewEnergyPlate(normalized)) + { + NewEnergyType? newEnergyType = GetNewEnergyType(normalized); + return newEnergyType switch + { + NewEnergyType.PureElectric => FuelType.PureElectric, + NewEnergyType.PluginHybrid => FuelType.PluginHybrid, + _ => FuelType.Unknown + }; + } + + return FuelType.Unknown; + } + + /// + /// 获取车辆燃料类型名称 + /// + /// 车牌号 + /// 燃料类型名称 + public static string? GetFuelTypeName(string? plateNumber) + { + return GetFuelType(plateNumber) switch + { + FuelType.Fuel => "燃油车", + FuelType.PureElectric => "纯电动", + FuelType.PluginHybrid => "插电混动", + _ => null + }; + } + + /// + /// 获取新能源车型类型 + /// + /// 车牌号 + /// 新能源类型,非新能源车牌返回null + public static NewEnergyType? GetNewEnergyType(string? plateNumber) + { + if (!IsNewEnergyPlate(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 小型新能源车牌:第3位 + // 大型新能源车牌:第8位 + char typeChar; + if (normalized.Length == 8) + { + if (normalized[2] == 'D' || normalized[2] == 'F') + { + typeChar = normalized[2]; + } + else + { + typeChar = normalized[7]; + } + } + else + { + return null; + } + + // D: 纯电动, F: 插电式混合动力 + return typeChar == 'D' ? NewEnergyType.PureElectric : NewEnergyType.PluginHybrid; + } + + #endregion + + #region 信息提取 + + /// + /// 获取省份名称 + /// + /// 车牌号 + /// 省份名称 + public static string? GetProvince(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + return "武警"; + } + + // 军队车牌特殊处理 + if (IsMilitaryPlate(normalized)) + { + return "军队"; + } + + string provinceCode = normalized.Substring(0, 1); + return ProvinceMap.TryGetValue(provinceCode, out string? province) ? province : null; + } + + /// + /// 获取城市名称 + /// + /// 车牌号 + /// 城市名称 + public static string? GetCity(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 武警或军队车牌无城市信息 + if (IsWJPlate(normalized) || IsMilitaryPlate(normalized)) + { + return null; + } + + string provinceCode = normalized.Substring(0, 1); + string cityCode = normalized.Substring(1, 1); + + if (CityMap.TryGetValue(provinceCode, out Dictionary? cities)) + { + if (cities.TryGetValue(cityCode, out string? city)) + { + return city; + } + } + + return null; + } + + /// + /// 获取车牌前缀(省份 + 字母) + /// + /// 车牌号 + /// 车牌前缀 + public static string? GetPrefix(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + if (normalized.Length < 2) + { + return null; + } + + // 普通车牌和新能源车牌:前2位 + // 武警车牌:前4位(WJ+数字) + if (IsWJPlate(normalized)) + { + return normalized.Length >= 4 ? normalized.Substring(0, 4) : null; + } + + return normalized.Substring(0, 2); + } + + /// + /// 获取号码部分(去除前缀) + /// + /// 车牌号 + /// 号码部分 + public static string? GetNumberPart(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + string normalized = Normalize(plateNumber)!; + + // 普通车牌:后5位 + // 新能源车牌:后6位(小型)/ 后6位(大型) + // 武警车牌:后5位 + + if (IsWJPlate(normalized)) + { + return normalized.Length >= 7 ? normalized.Substring(4) : null; + } + + if (normalized.Length == 8) + { + // 新能源车牌 + return normalized.Substring(2); + } + + if (normalized.Length == 7) + { + // 普通车牌或军队车牌 + return normalized.Substring(2); + } + + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化车牌号(转大写,去除特殊字符) + /// + /// 车牌号 + /// 格式化后的车牌号 + public static string? Normalize(string? plateNumber) + { + if (string.IsNullOrWhiteSpace(plateNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + string normalized = plateNumber.ToUpper().Trim(); + + // 保留汉字、字母、数字 + normalized = Regex.Replace(normalized, @"[^\u4e00-\u9fa5A-Z0-9]", ""); + + return normalized; + } + + /// + /// 格式化车牌号(带分隔符) + /// + /// 车牌号 + /// 分隔符(默认为空格) + /// 格式化后的车牌号 + public static string? Format(string? plateNumber, string separator = " ") + { + string? normalized = Normalize(plateNumber); + if (normalized == null) + { + return null; + } + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + if (normalized.Length == 7) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2, 2) + separator + normalized.Substring(4); + } + return normalized; + } + + // 普通车牌:2+5 + // 新能源车牌:2+6 + if (normalized.Length == 7) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2); + } + + if (normalized.Length == 8) + { + return normalized.Substring(0, 2) + separator + normalized.Substring(2); + } + + return normalized; + } + + /// + /// 车牌号脱敏:粤***123 + /// + /// 车牌号 + /// 脱敏后的车牌号 + public static string? Mask(string? plateNumber) + { + string? normalized = Normalize(plateNumber); + if (normalized == null) + { + return null; + } + + // 武警车牌特殊处理 + if (IsWJPlate(normalized)) + { + if (normalized.Length >= 7) + { + return normalized.Substring(0, 4) + "***" + normalized.Substring(normalized.Length - 2); + } + return null; + } + + if (normalized.Length == 7) + { + // 普通车牌:保留省份 + 后2位 + return normalized.Substring(0, 1) + "***" + normalized.Substring(5); + } + + if (normalized.Length == 8) + { + // 新能源车牌:保留省份 + 后2位 + return normalized.Substring(0, 1) + "****" + normalized.Substring(6); + } + + return null; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机车牌号(仅供测试使用) + /// + /// 省份简称(可选,默认随机) + /// 是否为新能源车牌(可选,默认随机) + /// 车牌号 + public static string GenerateRandom(string? province = null, bool? isNewEnergy = null) + { + // 省份 + string[] provinces = { "京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "渝" }; + string prov = province ?? MathCategory.RandomUtil.GetRandomElement(provinces); + + // 字母 + const string letters = "ABCDEFGHJKLMNPQRSTUVWXYZ"; // 不包含I和O + string letter = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()).ToString(); + + bool newEnergy = isNewEnergy ?? MathCategory.RandomUtil.RandomBool(); + + if (newEnergy) + { + // 新能源车牌(8位) + char energyType = MathCategory.RandomUtil.RandomBool() ? 'D' : 'F'; + string numbers = GenerateRandomAlphanumeric(5); + return prov + letter + energyType + numbers; + } + else + { + // 普通车牌(7位) + string numbers = GenerateRandomAlphanumeric(5); + return prov + letter + numbers; + } + } + + #endregion + + #region 私有方法 + + /// + /// 生成随机字母数字组合 + /// + private static string GenerateRandomAlphanumeric(int length) + { + const string chars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; // 不包含I和O + string result = ""; + for (int i = 0; i < length; i++) + { + result += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/MACAddressUtil.cs b/EasyTool.Core/BusinessCategory/MACAddressUtil.cs new file mode 100644 index 0000000..0bd23a3 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/MACAddressUtil.cs @@ -0,0 +1,508 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// MAC地址工具类 + /// + public static class MACAddressUtil + { + #region 常量与私有字段 + + /// + /// MAC地址正则表达式(多种格式) + /// + private static readonly Regex[] MACRegexes = + { + // XX:XX:XX:XX:XX:XX + new(@"^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$", RegexOptions.Compiled), + // XX-XX-XX-XX-XX-XX + new(@"^([0-9A-Fa-f]{2}[-]){5}[0-9A-Fa-f]{2}$", RegexOptions.Compiled), + // XXXX.XXXX.XXXX (Cisco格式) + new(@"^([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$", RegexOptions.Compiled), + // XXXXXXXXXXXX (无分隔符) + new(@"^[0-9A-Fa-f]{12}$", RegexOptions.Compiled) + }; + + /// + /// OUI(组织唯一标识符)与厂商映射(部分) + /// + private static readonly (string Prefix, string Vendor)[] OuiPrefixMap = + { + // Apple + ("00:03:93", "Apple"), ("00:05:02", "Apple"), ("00:0A:27", "Apple"), + ("00:0A:95", "Apple"), ("00:0D:93", "Apple"), ("00:0E:B2", "Apple"), + ("00:11:24", "Apple"), ("00:14:51", "Apple"), ("00:16:CB", "Apple"), + ("00:17:F2", "Apple"), ("00:19:E3", "Apple"), ("00:1B:63", "Apple"), + ("00:1C:B3", "Apple"), ("00:1D:4F", "Apple"), ("00:1E:52", "Apple"), + ("00:1F:5B", "Apple"), ("00:1F:F3", "Apple"), ("00:22:41", "Apple"), + ("00:23:12", "Apple"), ("00:23:32", "Apple"), ("00:23:6C", "Apple"), + ("00:23:DF", "Apple"), ("00:24:36", "Apple"), ("00:25:00", "Apple"), + ("00:25:4B", "Apple"), ("00:25:BC", "Apple"), ("00:26:08", "Apple"), + ("00:26:4A", "Apple"), ("00:26:B0", "Apple"), ("00:26:BB", "Apple"), + ("00:26:DB", "Apple"), ("A4:83:E7", "Apple"), ("AC:87:A3", "Apple"), + ("DC:A9:04", "Apple"), ("F0:DB:E2", "Apple"), + + // Samsung + ("00:07:AB", "Samsung"), ("00:0D:E5", "Samsung"), ("00:12:47", "Samsung"), + ("00:13:77", "Samsung"), ("00:15:99", "Samsung"), ("00:16:6B", "Samsung"), + ("00:17:C9", "Samsung"), ("00:18:AF", "Samsung"), ("00:1A:8A", "Samsung"), + ("00:1B:59", "Samsung"), ("00:1D:2B", "Samsung"), ("00:1E:7D", "Samsung"), + ("00:1F:36", "Samsung"), ("00:22:43", "Samsung"), ("00:24:90", "Samsung"), + ("00:25:38", "Samsung"), ("08:EC:A9", "Samsung"), ("30:07:4D", "Samsung"), + ("34:23:87", "Samsung"), ("38:2D:D8", "Samsung"), ("40:4D:7F", "Samsung"), + ("54:88:0E", "Samsung"), ("5C:8D:4E", "Samsung"), ("64:5D:86", "Samsung"), + ("6C:5C:14", "Samsung"), ("7C:1E:52", "Samsung"), ("88:36:6C", "Samsung"), + ("8C:10:D4", "Samsung"), ("94:8B:C1", "Samsung"), ("98:F0:AB", "Samsung"), + ("A0:10:81", "Samsung"), ("B0:DF:3A", "Samsung"), ("CC:61:E5", "Samsung"), + ("D8:A2:5E", "Samsung"), ("E0:D5:5E", "Samsung"), ("E8:50:8B", "Samsung"), + ("F0:27:65", "Samsung"), + + // Huawei + ("00:0F:B5", "Huawei"), ("00:18:82", "Huawei"), ("00:1E:10", "Huawei"), + ("00:25:68", "Huawei"), ("08:57:00", "Huawei"), ("0C:96:BF", "Huawei"), + ("10:1D:59", "Huawei"), ("18:3E:0F", "Huawei"), ("28:ED:6A", "Huawei"), + ("2C:B0:5D", "Huawei"), ("34:A3:95", "Huawei"), ("38:F8:5B", "Huawei"), + ("40:4E:36", "Huawei"), ("44:8B:CE", "Huawei"), ("48:37:B9", "Huawei"), + ("4C:FB:9F", "Huawei"), ("50:01:BB", "Huawei"), ("54:BF:64", "Huawei"), + ("58:00:E3", "Huawei"), ("5C:FE:45", "Huawei"), ("64:9A:BE", "Huawei"), + ("68:DB:CA", "Huawei"), ("6C:5D:43", "Huawei"), ("70:19:0F", "Huawei"), + ("74:6A:D8", "Huawei"), ("78:44:76", "Huawei"), ("7C:1E:52", "Huawei"), + ("80:8D:3F", "Huawei"), ("84:10:0D", "Huawei"), ("88:43:E1", "Huawei"), + ("8C:71:F8", "Huawei"), ("90:2B:D2", "Huawei"), ("94:FE:22", "Huawei"), + ("98:6F:1A", "Huawei"), ("9C:2E:A1", "Huawei"), ("A0:8F:85", "Huawei"), + ("A4:4E:31", "Huawei"), ("B0:5B:48", "Huawei"), ("B4:69:21", "Huawei"), + ("C0:BD:D1", "Huawei"), ("C4:4E:AC", "Huawei"), ("C8:5B:76", "Huawei"), + ("CC:34:29", "Huawei"), ("D0:59:E4", "Huawei"), ("D4:7A:34", "Huawei"), + ("DC:72:9B", "Huawei"), ("E0:37:BF", "Huawei"), ("E4:0D:73", "Huawei"), + ("E8:4E:CE", "Huawei"), ("EC:9A:74", "Huawei"), ("F0:FE:6B", "Huawei"), + ("FC:2C:55", "Huawei"), + + // Xiaomi + ("00:BB:3E", "Xiaomi"), ("10:2A:B3", "Xiaomi"), ("18:59:36", "Xiaomi"), + ("20:82:C0", "Xiaomi"), ("24:F9:A3", "Xiaomi"), ("28:ED:E1", "Xiaomi"), + ("34:80:B3", "Xiaomi"), ("38:1A:21", "Xiaomi"), ("3C:BD:D8", "Xiaomi"), + ("40:31:3C", "Xiaomi"), ("44:6F:D1", "Xiaomi"), ("48:88:CA", "Xiaomi"), + ("4C:18:D6", "Xiaomi"), ("50:1E:2D", "Xiaomi"), ("50:EC:50", "Xiaomi"), + ("58:44:98", "Xiaomi"), ("64:90:C1", "Xiaomi"), ("6C:5C:14", "Xiaomi"), + ("6C:8D:C1", "Xiaomi"), ("74:A3:E4", "Xiaomi"), ("78:02:F8", "Xiaomi"), + ("7C:1D:D9", "Xiaomi"), ("7C:8B:CA", "Xiaomi"), ("88:0F:10", "Xiaomi"), + ("8C:4C:4B", "Xiaomi"), ("8C:F6:79", "Xiaomi"), ("90:82:37", "Xiaomi"), + ("94:87:E0", "Xiaomi"), ("98:0C:82", "Xiaomi"), ("9C:2E:A1", "Xiaomi"), + ("9C:99:A0", "Xiaomi"), ("A0:CB:FD", "Xiaomi"), ("A4:4E:31", "Xiaomi"), + ("AC:29:3A", "Xiaomi"), ("B0:E2:35", "Xiaomi"), ("B8:C1:11", "Xiaomi"), + ("C0:26:0D", "Xiaomi"), ("C0:EE:FB", "Xiaomi"), ("C4:0B:CB", "Xiaomi"), + ("C4:4C:CA", "Xiaomi"), ("C8:1E:E7", "Xiaomi"), ("C8:94:BB", "Xiaomi"), + ("CC:AF:78", "Xiaomi"), ("D0:D2:B0", "Xiaomi"), ("D4:5D:64", "Xiaomi"), + ("D8:1C:79", "Xiaomi"), ("D8:96:95", "Xiaomi"), ("DC:A6:32", "Xiaomi"), + ("E0:46:44", "Xiaomi"), ("E4:B2:1F", "Xiaomi"), ("EC:3A:FD", "Xiaomi"), + ("EC:41:18", "Xiaomi"), ("F0:B4:29", "Xiaomi"), ("F4:28:53", "Xiaomi"), + ("F8:A4:5F", "Xiaomi"), ("FC:6D:B3", "Xiaomi"), ("FC:A6:67", "Xiaomi"), + + // Intel + ("00:02:B3", "Intel"), ("00:03:47", "Intel"), ("00:04:23", "Intel"), + ("00:07:E9", "Intel"), ("00:0B:DB", "Intel"), ("00:0D:DA", "Intel"), + ("00:0E:0C", "Intel"), ("00:0E:35", "Intel"), ("00:0E:A6", "Intel"), + ("00:0F:B0", "Intel"), ("00:0F:EE", "Intel"), ("00:10:E0", "Intel"), + ("00:11:0A", "Intel"), ("00:11:11", "Intel"), ("00:11:43", "Intel"), + ("00:11:F5", "Intel"), ("00:12:3F", "Intel"), ("00:13:20", "Intel"), + ("00:13:CE", "Intel"), ("00:13:E8", "Intel"), ("00:14:22", "Intel"), + ("00:14:78", "Intel"), ("00:14:A5", "Intel"), ("00:15:17", "Intel"), + ("00:15:C5", "Intel"), ("00:16:76", "Intel"), ("00:16:B6", "Intel"), + ("00:17:08", "Intel"), ("00:17:9A", "Intel"), ("00:17:C2", "Intel"), + ("00:18:13", "Intel"), ("00:18:68", "Intel"), ("00:18:DE", "Intel"), + ("00:19:D1", "Intel"), ("00:1B:21", "Intel"), ("00:1C:BD", "Intel"), + ("00:1D:72", "Intel"), ("00:1E:64", "Intel"), ("00:1E:67", "Intel"), + ("00:1F:16", "Intel"), ("00:1F:29", "Intel"), ("00:21:5C", "Intel"), + ("00:21:CC", "Intel"), ("00:22:FA", "Intel"), ("00:23:14", "Intel"), + ("00:23:7E", "Intel"), ("00:23:AE", "Intel"), ("00:24:D7", "Intel"), + ("00:25:66", "Intel"), ("00:26:B7", "Intel"), ("00:26:C6", "Intel"), + ("00:26:C7", "Intel"), ("00:27:0E", "Intel"), ("00:30:1B", "Intel"), + + // Cisco + ("00:00:0C", "Cisco"), ("00:01:42", "Cisco"), ("00:01:43", "Cisco"), + ("00:01:63", "Cisco"), ("00:01:64", "Cisco"), ("00:01:96", "Cisco"), + ("00:01:97", "Cisco"), ("00:01:C7", "Cisco"), ("00:02:16", "Cisco"), + ("00:02:17", "Cisco"), ("00:02:4A", "Cisco"), ("00:02:7D", "Cisco"), + ("00:02:7E", "Cisco"), ("00:02:FD", "Cisco"), ("00:03:6B", "Cisco"), + ("00:03:6F", "Cisco"), ("00:03:E3", "Cisco"), ("00:04:27", "Cisco"), + ("00:04:C1", "Cisco"), ("00:05:30", "Cisco"), ("00:05:32", "Cisco"), + ("00:05:59", "Cisco"), ("00:05:85", "Cisco"), ("00:05:9A", "Cisco"), + ("00:05:DC", "Cisco"), ("00:06:28", "Cisco"), ("00:06:52", "Cisco"), + ("00:06:53", "Cisco"), ("00:07:0D", "Cisco"), ("00:07:0E", "Cisco"), + ("00:07:0F", "Cisco"), ("00:07:50", "Cisco"), ("00:07:EC", "Cisco"), + ("00:08:21", "Cisco"), ("00:08:22", "Cisco"), ("00:08:24", "Cisco"), + ("00:08:2C", "Cisco"), ("00:08:A3", "Cisco"), ("00:09:0C", "Cisco"), + ("00:09:0D", "Cisco"), ("00:09:41", "Cisco"), ("00:09:43", "Cisco"), + ("00:09:44", "Cisco"), ("00:09:7C", "Cisco"), ("00:09:B7", "Cisco"), + ("00:0A:B8", "Cisco"), ("00:0A:F4", "Cisco"), ("00:0B:5F", "Cisco"), + ("00:0B:BE", "Cisco"), ("00:0B:FD", "Cisco"), ("00:0C:0C", "Cisco"), + ("00:0C:30", "Cisco"), ("00:0C:31", "Cisco"), ("00:0C:CE", "Cisco"), + ("00:0D:28", "Cisco"), ("00:0D:29", "Cisco"), ("00:0D:62", "Cisco"), + ("00:0D:63", "Cisco"), ("00:0D:64", "Cisco"), ("00:0D:BD", "Cisco"), + ("00:0D:BE", "Cisco"), ("00:0D:BF", "Cisco"), ("00:0D:C0", "Cisco"), + ("00:0E:0C", "Cisco"), ("00:0E:38", "Cisco"), ("00:0E:39", "Cisco"), + ("00:0E:3A", "Cisco"), ("00:0E:3B", "Cisco"), ("00:0E:3C", "Cisco"), + ("00:0E:84", "Cisco"), ("00:0F:23", "Cisco"), ("00:0F:24", "Cisco"), + ("00:0F:34", "Cisco"), ("00:0F:35", "Cisco"), ("00:0F:F7", "Cisco"), + ("00:0F:F8", "Cisco"), ("00:10:0C", "Cisco"), ("00:10:0D", "Cisco"), + ("00:10:0E", "Cisco"), ("00:10:0F", "Cisco"), ("00:10:54", "Cisco"), + ("00:10:58", "Cisco"), ("00:10:7A", "Cisco"), ("00:10:7B", "Cisco"), + ("00:10:E8", "Cisco"), ("00:10:F3", "Cisco"), ("00:10:F6", "Cisco"), + ("00:11:1B", "Cisco"), ("00:11:20", "Cisco"), ("00:11:21", "Cisco"), + ("00:11:2F", "Cisco"), ("00:11:30", "Cisco"), ("00:11:90", "Cisco"), + ("00:11:91", "Cisco"), ("00:11:92", "Cisco"), ("00:11:93", "Cisco"), + ("00:11:BB", "Cisco"), ("00:11:BC", "Cisco"), ("00:11:BD", "Cisco"), + ("00:11:BE", "Cisco"), ("00:11:BF", "Cisco"), ("00:11:FA", "Cisco"), + ("00:11:FB", "Cisco"), ("00:11:FC", "Cisco"), ("00:11:FD", "Cisco"), + ("00:11:FE", "Cisco"), ("00:12:00", "Cisco"), ("00:12:01", "Cisco"), + ("00:12:17", "Cisco"), ("00:12:1C", "Cisco"), ("00:12:1D", "Cisco"), + ("00:12:40", "Cisco"), ("00:12:41", "Cisco"), ("00:12:43", "Cisco"), + ("00:12:7F", "Cisco"), ("00:12:80", "Cisco"), ("00:12:DA", "Cisco"), + ("00:12:DB", "Cisco"), ("00:12:DC", "Cisco"), ("00:12:F9", "Cisco"), + ("00:12:FA", "Cisco"), ("00:13:1A", "Cisco"), ("00:13:1B", "Cisco"), + ("00:13:1C", "Cisco"), ("00:13:19", "Cisco"), ("00:13:46", "Cisco"), + ("00:13:47", "Cisco"), ("00:13:48", "Cisco"), ("00:13:49", "Cisco"), + ("00:13:5F", "Cisco"), ("00:13:60", "Cisco"), ("00:13:61", "Cisco"), + ("00:13:7F", "Cisco"), ("00:13:80", "Cisco"), ("00:13:81", "Cisco"), + ("00:13:C3", "Cisco"), ("00:13:C4", "Cisco"), ("00:13:C5", "Cisco"), + ("00:13:E8", "Cisco"), ("00:13:F7", "Cisco"), ("00:14:1B", "Cisco"), + ("00:14:69", "Cisco"), ("00:14:6A", "Cisco"), ("00:14:6B", "Cisco"), + ("00:14:97", "Cisco"), ("00:14:9A", "Cisco"), ("00:14:A1", "Cisco"), + ("00:14:A2", "Cisco"), ("00:14:BF", "Cisco"), ("00:14:F1", "Cisco"), + ("00:14:F2", "Cisco"), ("00:15:0C", "Cisco"), ("00:15:17", "Cisco"), + ("00:15:1B", "Cisco"), ("00:15:1C", "Cisco"), ("00:15:2B", "Cisco"), + ("00:15:60", "Cisco"), ("00:15:61", "Cisco"), ("00:15:62", "Cisco"), + ("00:15:63", "Cisco"), ("00:15:FA", "Cisco"), ("00:15:FB", "Cisco"), + ("00:15:FC", "Cisco"), ("00:15:FD", "Cisco"), ("00:16:35", "Cisco"), + ("00:16:36", "Cisco"), ("00:16:37", "Cisco"), ("00:16:46", "Cisco"), + ("00:16:47", "Cisco"), ("00:16:48", "Cisco"), ("00:16:78", "Cisco"), + ("00:16:79", "Cisco"), ("00:16:9D", "Cisco"), ("00:16:9E", "Cisco"), + ("00:16:C6", "Cisco"), ("00:16:C7", "Cisco"), ("00:16:C8", "Cisco"), + ("00:17:0D", "Cisco"), ("00:17:0E", "Cisco"), ("00:17:0F", "Cisco"), + ("00:17:59", "Cisco"), ("00:17:5A", "Cisco"), ("00:17:5B", "Cisco"), + ("00:17:84", "Cisco"), ("00:17:85", "Cisco"), ("00:17:86", "Cisco"), + ("00:17:94", "Cisco"), ("00:17:95", "Cisco"), ("00:17:96", "Cisco"), + ("00:17:DF", "Cisco"), ("00:17:E0", "Cisco"), ("00:17:E1", "Cisco"), + ("00:18:71", "Cisco"), ("00:18:72", "Cisco"), ("00:18:73", "Cisco"), + ("00:18:81", "Cisco"), ("00:18:82", "Cisco"), ("00:18:83", "Cisco"), + ("00:18:AF", "Cisco"), ("00:18:B9", "Cisco"), ("00:18:BA", "Cisco"), + ("00:18:BB", "Cisco"), ("00:19:06", "Cisco"), ("00:19:07", "Cisco"), + ("00:19:2F", "Cisco"), ("00:19:30", "Cisco"), ("00:19:55", "Cisco"), + ("00:19:56", "Cisco"), ("00:19:57", "Cisco"), ("00:19:68", "Cisco"), + ("00:19:69", "Cisco"), ("00:19:6A", "Cisco"), ("00:19:85", "Cisco"), + ("00:19:86", "Cisco"), ("00:19:87", "Cisco"), ("00:19:A9", "Cisco"), + ("00:19:AA", "Cisco"), ("00:19:AB", "Cisco"), ("00:19:E7", "Cisco"), + ("00:19:E8", "Cisco"), ("00:19:E9", "Cisco"), ("00:1A:0D", "Cisco"), + ("00:1A:0E", "Cisco"), ("00:1A:0F", "Cisco"), ("00:1A:2F", "Cisco"), + ("00:1A:30", "Cisco"), ("00:1A:31", "Cisco"), ("00:1A:6B", "Cisco"), + ("00:1A:6C", "Cisco"), ("00:1A:6D", "Cisco"), ("00:1A:A0", "Cisco"), + ("00:1A:A1", "Cisco"), ("00:1A:A2", "Cisco"), ("00:1A:A3", "Cisco"), + ("00:1A:E1", "Cisco"), ("00:1A:E2", "Cisco"), ("00:1A:E3", "Cisco"), + ("00:1B:0D", "Cisco"), ("00:1B:0E", "Cisco"), ("00:1B:0F", "Cisco"), + ("00:1B:53", "Cisco"), ("00:1B:54", "Cisco"), ("00:1B:55", "Cisco"), + ("00:1B:8C", "Cisco"), ("00:1B:8D", "Cisco"), ("00:1B:8E", "Cisco"), + ("00:1B:D4", "Cisco"), ("00:1B:D5", "Cisco"), ("00:1B:D6", "Cisco"), + ("00:1C:0E", "Cisco"), ("00:1C:0F", "Cisco"), ("00:1C:10", "Cisco"), + ("00:1C:58", "Cisco"), ("00:1C:59", "Cisco"), ("00:1C:5A", "Cisco"), + ("00:1C:B0", "Cisco"), ("00:1C:B1", "Cisco"), ("00:1C:B2", "Cisco"), + ("00:1C:F0", "Cisco"), ("00:1C:F1", "Cisco"), ("00:1C:F2", "Cisco"), + ("00:1D:0F", "Cisco"), ("00:1D:10", "Cisco"), ("00:1D:11", "Cisco"), + ("00:1D:45", "Cisco"), ("00:1D:46", "Cisco"), ("00:1D:47", "Cisco"), + ("00:1D:9C", "Cisco"), ("00:1D:9D", "Cisco"), ("00:1D:9E", "Cisco"), + ("00:1D:E2", "Cisco"), ("00:1D:E3", "Cisco"), ("00:1D:E4", "Cisco"), + ("00:1E:13", "Cisco"), ("00:1E:14", "Cisco"), ("00:1E:15", "Cisco"), + ("00:1E:49", "Cisco"), ("00:1E:4A", "Cisco"), ("00:1E:4B", "Cisco"), + ("00:1E:79", "Cisco"), ("00:1E:7A", "Cisco"), ("00:1E:7B", "Cisco"), + ("00:1E:B4", "Cisco"), ("00:1E:B5", "Cisco"), ("00:1E:B6", "Cisco"), + ("00:1F:1D", "Cisco"), ("00:1F:1E", "Cisco"), ("00:1F:1F", "Cisco"), + ("00:1F:6C", "Cisco"), ("00:1F:6D", "Cisco"), ("00:1F:6E", "Cisco"), + ("00:1F:9D", "Cisco"), ("00:1F:9E", "Cisco"), ("00:1F:9F", "Cisco"), + ("00:1F:C8", "Cisco"), ("00:1F:C9", "Cisco"), ("00:1F:CA", "Cisco"), + ("00:21:0D", "Cisco"), ("00:21:0E", "Cisco"), ("00:21:0F", "Cisco"), + ("00:21:55", "Cisco"), ("00:21:56", "Cisco"), ("00:21:57", "Cisco"), + ("00:21:A0", "Cisco"), ("00:21:A1", "Cisco"), ("00:21:A2", "Cisco"), + ("00:21:D5", "Cisco"), ("00:21:D6", "Cisco"), ("00:21:D7", "Cisco"), + ("00:22:55", "Cisco"), ("00:22:56", "Cisco"), ("00:22:57", "Cisco"), + ("00:22:90", "Cisco"), ("00:22:91", "Cisco"), ("00:22:92", "Cisco"), + ("00:22:BD", "Cisco"), ("00:22:BE", "Cisco"), ("00:22:BF", "Cisco"), + ("00:23:04", "Cisco"), ("00:23:05", "Cisco"), ("00:23:06", "Cisco"), + ("00:23:33", "Cisco"), ("00:23:34", "Cisco"), ("00:23:35", "Cisco"), + ("00:23:5C", "Cisco"), ("00:23:5D", "Cisco"), ("00:23:5E", "Cisco"), + ("00:23:EB", "Cisco"), ("00:23:EC", "Cisco"), ("00:23:ED", "Cisco"), + ("00:24:13", "Cisco"), ("00:24:14", "Cisco"), ("00:24:15", "Cisco"), + ("00:24:50", "Cisco"), ("00:24:51", "Cisco"), ("00:24:52", "Cisco"), + ("00:24:97", "Cisco"), ("00:24:98", "Cisco"), ("00:24:99", "Cisco"), + ("00:24:B2", "Cisco"), ("00:24:B3", "Cisco"), ("00:24:B4", "Cisco"), + ("00:24:F7", "Cisco"), ("00:24:F8", "Cisco"), ("00:24:F9", "Cisco"), + ("00:25:1B", "Cisco"), ("00:25:1C", "Cisco"), ("00:25:1D", "Cisco"), + ("00:25:2A", "Cisco"), ("00:25:2B", "Cisco"), ("00:25:2C", "Cisco"), + ("00:25:61", "Cisco"), ("00:25:62", "Cisco"), ("00:25:63", "Cisco"), + ("00:25:84", "Cisco"), ("00:25:85", "Cisco"), ("00:25:86", "Cisco"), + ("00:25:B5", "Cisco"), ("00:25:B6", "Cisco"), ("00:25:B7", "Cisco"), + ("00:26:0B", "Cisco"), ("00:26:0C", "Cisco"), ("00:26:0D", "Cisco"), + ("00:26:51", "Cisco"), ("00:26:52", "Cisco"), ("00:26:53", "Cisco"), + ("00:26:88", "Cisco"), ("00:26:89", "Cisco"), ("00:26:8A", "Cisco"), + ("00:26:99", "Cisco"), ("00:26:9A", "Cisco"), ("00:26:9B", "Cisco"), + ("00:26:CA", "Cisco"), ("00:26:CB", "Cisco"), ("00:26:CC", "Cisco"), + ("00:50:56", "VMware"), ("00:0C:29", "VMware"), ("00:05:69", "VMware"), + ("00:1C:14", "VMware"), ("00:50:56", "VMware") + }; + + #endregion + + #region 验证方法 + + /// + /// 验证MAC地址是否有效 + /// + /// MAC地址 + /// 是否有效 + public static bool IsValid(string? mac) + { + if (string.IsNullOrWhiteSpace(mac)) + { + return false; + } + + foreach (var regex in MACRegexes) + { + if (regex.IsMatch(mac)) + { + return true; + } + } + + return false; + } + + #endregion + + #region 信息提取 + + /// + /// 获取OUI(组织唯一标识符,前3字节) + /// + /// MAC地址 + /// OUI + public static string? GetOUI(string? mac) + { + if (!IsValid(mac)) + { + return null; + } + + string clean = Clean(mac)!; + return clean.Substring(0, 6).ToUpper(); + } + + /// + /// 获取设备标识符(后3字节) + /// + /// MAC地址 + /// 设备标识符 + public static string? GetDeviceId(string? mac) + { + if (!IsValid(mac)) + { + return null; + } + + string clean = Clean(mac)!; + return clean.Substring(6, 6).ToUpper(); + } + + /// + /// 获取厂商名称 + /// + /// MAC地址 + /// 厂商名称 + public static string? GetVendor(string? mac) + { + string? oui = GetOUI(mac); + if (oui == null) + { + return null; + } + + // 格式化为XX:XX:XX格式进行查找 + string formattedOui = $"{oui.Substring(0, 2)}:{oui.Substring(2, 2)}:{oui.Substring(4, 2)}".ToUpper(); + + foreach (var mapping in OuiPrefixMap) + { + if (mapping.Prefix.Equals(formattedOui, StringComparison.OrdinalIgnoreCase)) + { + return mapping.Vendor; + } + } + + return null; + } + + /// + /// 判断是否为组播地址 + /// + /// MAC地址 + /// 是否为组播地址 + public static bool IsMulticast(string? mac) + { + if (!IsValid(mac)) + { + return false; + } + + string clean = Clean(mac)!; + // 第一个字节的最低位为1表示组播 + int firstByte = Convert.ToInt32(clean.Substring(0, 2), 16); + return (firstByte & 0x01) == 1; + } + + /// + /// 判断是否为广播地址(FF:FF:FF:FF:FF:FF) + /// + /// MAC地址 + /// 是否为广播地址 + public static bool IsBroadcast(string? mac) + { + string? clean = Clean(mac); + return clean == "FFFFFFFFFFFF"; + } + + /// + /// 判断是否为本地管理地址 + /// + /// MAC地址 + /// 是否为本地管理地址 + public static bool IsLocallyAdministered(string? mac) + { + if (!IsValid(mac)) + { + return false; + } + + string clean = Clean(mac)!; + // 第一个字节的次低位为1表示本地管理 + int firstByte = Convert.ToInt32(clean.Substring(0, 2), 16); + return (firstByte & 0x02) == 2; + } + + #endregion + + #region 格式化方法 + + /// + /// 清理MAC地址(去除分隔符) + /// + /// MAC地址 + /// 12位十六进制字符串 + public static string? Clean(string? mac) + { + if (string.IsNullOrWhiteSpace(mac)) + { + return null; + } + + string cleaned = Regex.Replace(mac, @"[^\dA-Fa-f]", "").ToUpper(); + return cleaned.Length == 12 ? cleaned : null; + } + + /// + /// 格式化为标准格式(XX:XX:XX:XX:XX:XX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? Format(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:{clean.Substring(4, 2)}:" + + $"{clean.Substring(6, 2)}:{clean.Substring(8, 2)}:{clean.Substring(10, 2)}"; + } + + /// + /// 格式化为横线分隔(XX-XX-XX-XX-XX-XX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? FormatWithHyphens(string? mac) + { + return Format(mac)?.Replace(':', '-'); + } + + /// + /// 格式化为Cisco格式(XXXX.XXXX.XXXX) + /// + /// MAC地址 + /// 格式化后的MAC地址 + public static string? FormatCisco(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 4)}.{clean.Substring(4, 4)}.{clean.Substring(8, 4)}"; + } + + /// + /// MAC地址脱敏:AA:BB:**:**:**:FF + /// + /// MAC地址 + /// 脱敏后的MAC地址 + public static string? Mask(string? mac) + { + string? clean = Clean(mac); + if (clean == null) + { + return null; + } + + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:**:**:**:{clean.Substring(10, 2)}"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机MAC地址(仅供测试使用) + /// + /// 厂商OUI(可选,默认随机生成) + /// MAC地址 + public static string GenerateRandom(string? vendor = null) + { + string oui; + string deviceId; + + if (!string.IsNullOrWhiteSpace(vendor) && vendor.Length >= 6) + { + oui = vendor.Substring(0, 6).ToUpper(); + } + else + { + // 随机生成OUI(设置本地管理位) + int firstByte = MathCategory.RandomUtil.RandomInt(0, 255) | 0x02; // 设置本地管理位 + oui = firstByte.ToString("X2") + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2"); + } + + // 随机生成设备ID + deviceId = MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2") + + MathCategory.RandomUtil.RandomInt(0, 255).ToString("X2"); + + string clean = oui + deviceId; + return $"{clean.Substring(0, 2)}:{clean.Substring(2, 2)}:{clean.Substring(4, 2)}:" + + $"{clean.Substring(6, 2)}:{clean.Substring(8, 2)}:{clean.Substring(10, 2)}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs b/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs new file mode 100644 index 0000000..ab168f2 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/OrgCodeUtil.cs @@ -0,0 +1,254 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 组织机构代码工具类 + /// + public static class OrgCodeUtil + { + #region 常量与私有字段 + + /// + /// 组织机构代码正则表达式(9位:8位数字/字母 + 1位校验码) + /// + private static readonly Regex OrgCodeRegex = new( + @"^[A-Z0-9]{8}-?[A-X0-9]$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 组织机构代码字符值映射 + /// + private static readonly int[] CharWeights = { 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表 + /// + private const string CheckCodes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + #endregion + + #region 验证方法 + + /// + /// 验证组织机构代码是否有效 + /// + /// 组织机构代码 + /// 是否有效 + public static bool IsValid(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return false; + } + + string code = orgCode.ToUpper().Replace("-", ""); + + if (code.Length != 9) + { + return false; + } + + if (!OrgCodeRegex.IsMatch(code)) + { + return false; + } + + // 计算校验码 + char? expectedCheck = CalculateCheckCode(code.Substring(0, 8)); + return expectedCheck.HasValue && expectedCheck.Value == code[8]; + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 组织机构代码 + /// 格式是否正确 + public static bool IsValidFormat(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return false; + } + + string code = orgCode.ToUpper().Replace("-", ""); + return code.Length == 9 && OrgCodeRegex.IsMatch(code); + } + + /// + /// 计算校验码 + /// + /// 不含校验位的8位代码 + /// 校验码,计算失败返回null + public static char? CalculateCheckCode(string? code8) + { + if (string.IsNullOrWhiteSpace(code8) || code8.Length != 8) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 8; i++) + { + char c = char.ToUpper(code8[i]); + int value; + + if (c >= '0' && c <= '9') + { + value = c - '0'; + } + else if (c >= 'A' && c <= 'Z') + { + value = c - 'A' + 10; + } + else + { + return null; + } + + sum += value * CharWeights[i]; + } + + int checkIndex = 11 - (sum % 11); + if (checkIndex == 11) + { + checkIndex = 0; + } + else if (checkIndex == 10) + { + return 'X'; // 10对应X + } + + return CheckCodes[checkIndex]; + } + + #endregion + + #region 信息提取 + + /// + /// 获取机构类型(第1位) + /// + /// 组织机构代码 + /// 机构类型 + public static string? GetOrganizationType(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + char typeCode = char.ToUpper(orgCode!.Replace("-", "")[0]); + return typeCode switch + { + '1' => "企业法人", + '2' => "企业非法人", + '3' => "事业法人", + '4' => "事业非法人", + '5' => "机关法人", + '6' => "机关非法人", + '7' => "社会团体法人", + '8' => "社会团体非法人", + '9' => "其他机构", + 'A' => "企业法人(外资)", + 'B' => "企业非法人(外资)", + _ => null + }; + } + + /// + /// 获取登记管理机关行政区划代码(第2-8位) + /// + /// 组织机构代码 + /// 行政区划代码 + public static string? GetAreaCode(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.Replace("-", ""); + return code.Substring(1, 7); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化组织机构代码(XXXXXXXX-X) + /// + /// 组织机构代码 + /// 格式化后的代码 + public static string? Format(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.ToUpper().Replace("-", ""); + return code.Substring(0, 8) + "-" + code[8]; + } + + /// + /// 清理组织机构代码(去除分隔符) + /// + /// 组织机构代码 + /// 清理后的代码 + public static string? Normalize(string? orgCode) + { + if (string.IsNullOrWhiteSpace(orgCode)) + { + return null; + } + + string code = orgCode.ToUpper().Replace("-", "").Trim(); + return code.Length == 9 && OrgCodeRegex.IsMatch(code) ? code : null; + } + + /// + /// 组织机构代码脱敏:123****9X + /// + /// 组织机构代码 + /// 脱敏后的代码 + public static string? Mask(string? orgCode) + { + if (!IsValid(orgCode)) + { + return null; + } + + string code = orgCode!.Replace("-", ""); + return code.Substring(0, 3) + "*****" + code.Substring(8); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机组织机构代码(仅供测试使用) + /// + /// 9位组织机构代码 + public static string GenerateRandom() + { + const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + // 生成前8位 + string code8 = ""; + for (int i = 0; i < 8; i++) + { + code8 += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + // 计算校验码 + char? checkCode = CalculateCheckCode(code8); + return code8 + (checkCode ?? '0'); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PassportUtil.cs b/EasyTool.Core/BusinessCategory/PassportUtil.cs new file mode 100644 index 0000000..9ee432d --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PassportUtil.cs @@ -0,0 +1,375 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 护照类型枚举 + /// + public enum PassportType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 中国普通护照(E开头+8位数字) + /// + ChinaOrdinary = 1, + + /// + /// 中国公务护照(SE开头+7位数字) + /// + ChinaService = 2, + + /// + /// 中国外交护照(DE开头+7位数字) + /// + ChinaDiplomatic = 3, + + /// + /// 中国香港特区护照 + /// + HongKong = 4, + + /// + /// 中国澳门特区护照 + /// + Macau = 5, + + /// + /// 台湾护照 + /// + Taiwan = 6 + } + + /// + /// 护照号工具类 + /// + public static class PassportUtil + { + #region 常量与私有字段 + + /// + /// 中国普通护照正则(E+8位数字) + /// + private static readonly Regex ChinaOrdinaryRegex = new Regex(@"^[Ee]\d{8}$", RegexOptions.Compiled); + + /// + /// 中国公务护照正则(SE+7位数字) + /// + private static readonly Regex ChinaServiceRegex = new Regex(@"^[Ss][Ee]\d{7}$", RegexOptions.Compiled); + + /// + /// 中国外交护照正则(DE+7位数字) + /// + private static readonly Regex ChinaDiplomaticRegex = new Regex(@"^[Dd][Ee]\d{7}$", RegexOptions.Compiled); + + /// + /// 中国香港护照正则(K+8位数字 或 881/159开头+7位数字) + /// + private static readonly Regex HongKongRegex = new Regex(@"^([Kk]\d{8}|(881|159)\d{7})$", RegexOptions.Compiled); + + /// + /// 中国澳门护照正则(578开头+7位数字 或 1+7位数字) + /// + private static readonly Regex MacauRegex = new Regex(@"^(578\d{7}|[1]\d{7})$", RegexOptions.Compiled); + + /// + /// 台湾护照正则(数字+字母混合,9-10位) + /// + private static readonly Regex TaiwanRegex = new Regex(@"^\d{8,9}$|^[A-Za-z]\d{8,9}$", RegexOptions.Compiled); + + /// + /// 通用护照号正则(2-3位字母+6-9位数字,或纯数字8-9位) + /// + private static readonly Regex GeneralPassportRegex = new Regex( + @"^([A-Za-z]{1,3}\d{6,9}|\d{8,9})$", + RegexOptions.Compiled); + + #endregion + + #region 验证方法 + + /// + /// 验证护照号是否有效(自动识别类型) + /// + /// 护照号 + /// 是否有效 + public static bool IsValid(string? passportNumber) + { + return GetPassportType(passportNumber) != PassportType.Unknown; + } + + /// + /// 验证中国普通护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaOrdinary(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaOrdinaryRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国公务护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaService(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaServiceRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国外交护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsChinaDiplomatic(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return ChinaDiplomaticRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国香港护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsHongKong(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return HongKongRegex.IsMatch(passportNumber); + } + + /// + /// 验证中国澳门护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsMacau(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return MacauRegex.IsMatch(passportNumber); + } + + /// + /// 验证台湾护照号是否有效 + /// + /// 护照号 + /// 是否有效 + public static bool IsTaiwan(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return false; + } + + return TaiwanRegex.IsMatch(passportNumber); + } + + /// + /// 验证是否为中国大陆护照(含普通、公务、外交) + /// + /// 护照号 + /// 是否为中国大陆护照 + public static bool IsChinaMainland(string? passportNumber) + { + return IsChinaOrdinary(passportNumber) || + IsChinaService(passportNumber) || + IsChinaDiplomatic(passportNumber); + } + + #endregion + + #region 类型识别 + + /// + /// 获取护照类型 + /// + /// 护照号 + /// 护照类型 + public static PassportType GetPassportType(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return PassportType.Unknown; + } + + string upper = passportNumber.ToUpper(); + + // 中国普通护照 + if (ChinaOrdinaryRegex.IsMatch(upper)) + { + return PassportType.ChinaOrdinary; + } + + // 中国公务护照 + if (ChinaServiceRegex.IsMatch(upper)) + { + return PassportType.ChinaService; + } + + // 中国外交护照 + if (ChinaDiplomaticRegex.IsMatch(upper)) + { + return PassportType.ChinaDiplomatic; + } + + // 香港护照 + if (HongKongRegex.IsMatch(upper)) + { + return PassportType.HongKong; + } + + // 澳门护照 + if (MacauRegex.IsMatch(upper)) + { + return PassportType.Macau; + } + + // 台湾护照 + if (TaiwanRegex.IsMatch(upper)) + { + return PassportType.Taiwan; + } + + return PassportType.Unknown; + } + + /// + /// 获取护照类型名称 + /// + /// 护照号 + /// 护照类型名称 + public static string? GetPassportTypeName(string? passportNumber) + { + PassportType type = GetPassportType(passportNumber); + return type switch + { + PassportType.ChinaOrdinary => "中国普通护照", + PassportType.ChinaService => "中国公务护照", + PassportType.ChinaDiplomatic => "中国外交护照", + PassportType.HongKong => "香港特区护照", + PassportType.Macau => "澳门特区护照", + PassportType.Taiwan => "台湾护照", + _ => null + }; + } + + /// + /// 获取护照类型描述 + /// + /// 护照类型 + /// 类型描述 + public static string GetTypeDescription(PassportType type) + { + return type switch + { + PassportType.ChinaOrdinary => "中国普通护照(E+8位数字)", + PassportType.ChinaService => "中国公务护照(SE+7位数字)", + PassportType.ChinaDiplomatic => "中国外交护照(DE+7位数字)", + PassportType.HongKong => "香港特区护照(K+8位数字)", + PassportType.Macau => "澳门特区护照(578开头+7位数字)", + PassportType.Taiwan => "台湾护照(8-9位数字)", + _ => "未知类型" + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化护照号(转大写,去除空格和特殊字符) + /// + /// 护照号 + /// 格式化后的护照号 + public static string? Normalize(string? passportNumber) + { + if (string.IsNullOrWhiteSpace(passportNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + string normalized = passportNumber.ToUpper().Trim(); + normalized = Regex.Replace(normalized, @"[^A-Z0-9]", ""); + + return normalized; + } + + /// + /// 护照号脱敏:E********(保留首字母) + /// + /// 护照号 + /// 脱敏后的护照号 + public static string? Mask(string? passportNumber) + { + string? normalized = Normalize(passportNumber); + if (normalized == null) + { + return null; + } + + // 保留首字符,其余用*代替 + if (normalized.Length <= 2) + { + return normalized[0] + "*"; + } + + // 保留前2位和后2位 + return normalized.Substring(0, 2) + new string('*', normalized.Length - 4) + normalized.Substring(normalized.Length - 2); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机护照号(仅供测试使用) + /// + /// 护照类型(可选,默认中国普通护照) + /// 护照号 + public static string GenerateRandom(PassportType type = PassportType.ChinaOrdinary) + { + return type switch + { + PassportType.ChinaOrdinary => "E" + MathCategory.RandomUtil.RandomDigitString(8), + PassportType.ChinaService => "SE" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.ChinaDiplomatic => "DE" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.HongKong => "K" + MathCategory.RandomUtil.RandomDigitString(8), + PassportType.Macau => "578" + MathCategory.RandomUtil.RandomDigitString(7), + PassportType.Taiwan => MathCategory.RandomUtil.RandomDigitString(9), + _ => "E" + MathCategory.RandomUtil.RandomDigitString(8) + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PasswordUtil.cs b/EasyTool.Core/BusinessCategory/PasswordUtil.cs new file mode 100644 index 0000000..c3e66ed --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PasswordUtil.cs @@ -0,0 +1,586 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 密码强度等级 + /// + public enum PasswordStrength + { + /// + /// 非常弱 + /// + VeryWeak = 0, + + /// + /// 弱 + /// + Weak = 1, + + /// + /// 中等 + /// + Medium = 2, + + /// + /// 强 + /// + Strong = 3, + + /// + /// 非常强 + /// + VeryStrong = 4 + } + + /// + /// 密码验证结果 + /// + public class PasswordValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 密码强度 + /// + public PasswordStrength Strength { get; set; } + + /// + /// 强度分数(0-100) + /// + public int Score { get; set; } + + /// + /// 错误信息列表 + /// + public List Errors { get; set; } = new List(); + + /// + /// 警告信息列表 + /// + public List Warnings { get; set; } = new List(); + } + + /// + /// 密码验证选项 + /// + public class PasswordValidationOptions + { + /// + /// 最小长度(默认8) + /// + public int MinLength { get; set; } = 8; + + /// + /// 最大长度(默认128) + /// + public int MaxLength { get; set; } = 128; + + /// + /// 是否要求包含小写字母(默认true) + /// + public bool RequireLowercase { get; set; } = true; + + /// + /// 是否要求包含大写字母(默认true) + /// + public bool RequireUppercase { get; set; } = true; + + /// + /// 是否要求包含数字(默认true) + /// + public bool RequireDigit { get; set; } = true; + + /// + /// 是否要求包含特殊字符(默认true) + /// + public bool RequireSpecialChar { get; set; } = true; + + /// + /// 允许的特殊字符(默认!@#$%^&*()_+-=[]{}|;:',.<>?) + /// + public string AllowedSpecialChars { get; set; } = "!@#$%^&*()_+-=[]{}|;:',.<>?"; + + /// + /// 最少不同字符类型数量(默认3) + /// + public int MinCharacterTypes { get; set; } = 3; + + /// + /// 是否禁止常见弱密码(默认true) + /// + public bool BlockCommonPasswords { get; set; } = true; + + /// + /// 是否禁止连续重复字符(默认true) + /// + public bool BlockRepeatingChars { get; set; } = true; + + /// + /// 是否禁止连续递增/递减字符(如123、abc)(默认true) + /// + public bool BlockSequentialChars { get; set; } = true; + } + + /// + /// 密码工具类 + /// + public static class PasswordUtil + { + #region 常量与私有字段 + + /// + /// 常见弱密码列表 + /// + private static readonly HashSet CommonPasswords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "password", "123456", "12345678", "123456789", "1234567890", + "qwerty", "abc123", "password123", "admin", "admin123", + "root", "root123", "111111", "000000", "123123", + "password1", "iloveyou", "monkey", "dragon", "master", + "letmein", "login", "welcome", "shadow", "sunshine", + "princess", "football", "baseball", "soccer", "hockey", + "batman", "superman", "trustno1", "passw0rd", "qazwsx", + "qwerty123", "123qwe", "654321", "888888", "666666" + }; + + /// + /// 键盘连续字符模式 + /// + private static readonly string[] KeyboardSequences = { + "qwertyuiop", "asdfghjkl", "zxcvbnm", + "qwertyuiop".ToUpper(), "asdfghjkl".ToUpper(), "zxcvbnm".ToUpper() + }; + + #endregion + + #region 验证方法 + + /// + /// 验证密码(使用默认选项) + /// + /// 密码 + /// 验证结果 + public static PasswordValidationResult Validate(string? password) + { + return Validate(password, new PasswordValidationOptions()); + } + + /// + /// 验证密码(使用自定义选项) + /// + /// 密码 + /// 验证选项 + /// 验证结果 + public static PasswordValidationResult Validate(string? password, PasswordValidationOptions options) + { + var result = new PasswordValidationResult(); + + // 空值检查 + if (string.IsNullOrEmpty(password)) + { + result.IsValid = false; + result.Errors.Add("密码不能为空"); + result.Score = 0; + result.Strength = PasswordStrength.VeryWeak; + return result; + } + + // 长度检查 + if (password.Length < options.MinLength) + { + result.Errors.Add($"密码长度不能少于{options.MinLength}位"); + } + + if (password.Length > options.MaxLength) + { + result.Errors.Add($"密码长度不能超过{options.MaxLength}位"); + } + + // 字符类型检查 + bool hasLowercase = Regex.IsMatch(password, @"[a-z]"); + bool hasUppercase = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + if (options.RequireLowercase && !hasLowercase) + { + result.Errors.Add("密码必须包含小写字母"); + } + + if (options.RequireUppercase && !hasUppercase) + { + result.Errors.Add("密码必须包含大写字母"); + } + + if (options.RequireDigit && !hasDigit) + { + result.Errors.Add("密码必须包含数字"); + } + + if (options.RequireSpecialChar && !hasSpecial) + { + result.Errors.Add("密码必须包含特殊字符"); + } + + // 统计字符类型数量 + int charTypeCount = (hasLowercase ? 1 : 0) + (hasUppercase ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0); + if (charTypeCount < options.MinCharacterTypes) + { + result.Errors.Add($"密码必须包含至少{options.MinCharacterTypes}种不同类型的字符"); + } + + // 检查非法字符 + if (!string.IsNullOrEmpty(options.AllowedSpecialChars)) + { + string allowedPattern = $@"^[a-zA-Z0-9{Regex.Escape(options.AllowedSpecialChars)}]+$"; + if (!Regex.IsMatch(password, allowedPattern)) + { + result.Errors.Add("密码包含非法字符"); + } + } + + // 检查常见弱密码 + if (options.BlockCommonPasswords && CommonPasswords.Contains(password)) + { + result.Errors.Add("密码过于简单,请使用更复杂的密码"); + } + + // 检查连续重复字符 + if (options.BlockRepeatingChars && HasRepeatingChars(password, 3)) + { + result.Warnings.Add("密码包含连续重复的字符"); + } + + // 检查连续递增/递减字符 + if (options.BlockSequentialChars && HasSequentialChars(password)) + { + result.Warnings.Add("密码包含连续的递增或递减字符"); + } + + // 计算强度分数 + int score = CalculateScore(password, hasLowercase, hasUppercase, hasDigit, hasSpecial); + result.Score = score; + result.Strength = GetStrengthFromScore(score); + + // 确定是否有效 + result.IsValid = result.Errors.Count == 0; + + return result; + } + + /// + /// 快速验证密码是否符合基本要求 + /// + /// 密码 + /// 最小长度(默认8) + /// 是否有效 + public static bool IsValid(string? password, int minLength = 8) + { + if (string.IsNullOrEmpty(password) || password.Length < minLength) + { + return false; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + + return hasLower && hasUpper && hasDigit; + } + + #endregion + + #region 强度评估 + + /// + /// 评估密码强度 + /// + /// 密码 + /// 密码强度 + public static PasswordStrength EvaluateStrength(string? password) + { + if (string.IsNullOrEmpty(password)) + { + return PasswordStrength.VeryWeak; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + int score = CalculateScore(password, hasLower, hasUpper, hasDigit, hasSpecial); + return GetStrengthFromScore(score); + } + + /// + /// 获取密码强度分数(0-100) + /// + /// 密码 + /// 强度分数 + public static int GetStrengthScore(string? password) + { + if (string.IsNullOrEmpty(password)) + { + return 0; + } + + bool hasLower = Regex.IsMatch(password, @"[a-z]"); + bool hasUpper = Regex.IsMatch(password, @"[A-Z]"); + bool hasDigit = Regex.IsMatch(password, @"\d"); + bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{}|;:',.<>?]"); + + return CalculateScore(password, hasLower, hasUpper, hasDigit, hasSpecial); + } + + /// + /// 获取密码强度描述 + /// + /// 密码强度 + /// 强度描述 + public static string GetStrengthDescription(PasswordStrength strength) + { + return strength switch + { + PasswordStrength.VeryWeak => "非常弱", + PasswordStrength.Weak => "弱", + PasswordStrength.Medium => "中等", + PasswordStrength.Strong => "强", + PasswordStrength.VeryStrong => "非常强", + _ => "未知" + }; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机密码 + /// + /// 密码长度(默认12) + /// 包含小写字母(默认true) + /// 包含大写字母(默认true) + /// 包含数字(默认true) + /// 包含特殊字符(默认true) + /// 随机密码 + public static string GenerateRandom( + int length = 12, + bool includeLowercase = true, + bool includeUppercase = true, + bool includeDigits = true, + bool includeSpecialChars = true) + { + const string lowercase = "abcdefghijklmnopqrstuvwxyz"; + const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + const string special = "!@#$%^&*()_+-=[]{}|;:',.<>?"; + + string charSet = ""; + if (includeLowercase) charSet += lowercase; + if (includeUppercase) charSet += uppercase; + if (includeDigits) charSet += digits; + if (includeSpecialChars) charSet += special; + + if (string.IsNullOrEmpty(charSet)) + { + charSet = lowercase + digits; + } + + string password = ""; + for (int i = 0; i < length; i++) + { + password += MathCategory.RandomUtil.GetRandomElement(charSet.ToCharArray()); + } + + return password; + } + + /// + /// 生成强密码(确保包含所有字符类型) + /// + /// 密码长度(默认16) + /// 强密码 + public static string GenerateStrong(int length = 16) + { + const string lowercase = "abcdefghijklmnopqrstuvwxyz"; + const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digits = "0123456789"; + const string special = "!@#$%^&*()_+-="; + + if (length < 4) + { + length = 4; + } + + // 确保每种字符类型至少有一个 + var password = new List(); + password.Add(MathCategory.RandomUtil.GetRandomElement(lowercase.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(uppercase.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray())); + password.Add(MathCategory.RandomUtil.GetRandomElement(special.ToCharArray())); + + // 填充剩余字符 + string allChars = lowercase + uppercase + digits + special; + for (int i = 4; i < length; i++) + { + password.Add(MathCategory.RandomUtil.GetRandomElement(allChars.ToCharArray())); + } + + // 随机打乱顺序 + for (int i = password.Count - 1; i > 0; i--) + { + int j = MathCategory.RandomUtil.RandomInt(0, i + 1); + (password[i], password[j]) = (password[j], password[i]); + } + + return new string(password.ToArray()); + } + + #endregion + + #region 私有方法 + + /// + /// 计算密码强度分数 + /// + private static int CalculateScore(string password, bool hasLower, bool hasUpper, bool hasDigit, bool hasSpecial) + { + int score = 0; + + // 长度分数(最多40分) + score += Math.Min(password.Length * 4, 40); + + // 字符类型分数(每种类型10分,最多40分) + if (hasLower) score += 10; + if (hasUpper) score += 10; + if (hasDigit) score += 10; + if (hasSpecial) score += 10; + + // 混合奖励(最多10分) + int typeCount = (hasLower ? 1 : 0) + (hasUpper ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0); + if (typeCount >= 3) score += 5; + if (typeCount == 4) score += 5; + + // 惩罚 + // 常见弱密码 + if (CommonPasswords.Contains(password)) + { + score = Math.Max(0, score - 30); + } + + // 全部相同字符 + if (AllSameChar(password)) + { + score = Math.Max(0, score - 20); + } + + // 连续字符 + if (HasSequentialChars(password)) + { + score = Math.Max(0, score - 10); + } + + return Math.Min(100, Math.Max(0, score)); + } + + /// + /// 根据分数获取强度等级 + /// + private static PasswordStrength GetStrengthFromScore(int score) + { + if (score < 20) return PasswordStrength.VeryWeak; + if (score < 40) return PasswordStrength.Weak; + if (score < 60) return PasswordStrength.Medium; + if (score < 80) return PasswordStrength.Strong; + return PasswordStrength.VeryStrong; + } + + /// + /// 检查是否所有字符相同 + /// + private static bool AllSameChar(string str) + { + if (string.IsNullOrEmpty(str)) return true; + char first = str[0]; + foreach (char c in str) + { + if (c != first) return false; + } + return true; + } + + /// + /// 检查是否有连续重复字符 + /// + private static bool HasRepeatingChars(string str, int count) + { + if (string.IsNullOrEmpty(str) || str.Length < count) return false; + + for (int i = 0; i <= str.Length - count; i++) + { + bool allSame = true; + for (int j = 1; j < count; j++) + { + if (str[i + j] != str[i]) + { + allSame = false; + break; + } + } + if (allSame) return true; + } + return false; + } + + /// + /// 检查是否有连续递增/递减字符 + /// + private static bool HasSequentialChars(string str) + { + if (string.IsNullOrEmpty(str) || str.Length < 3) return false; + + string lower = str.ToLower(); + + // 检查字母和数字序列 + for (int i = 0; i <= lower.Length - 3; i++) + { + // 检查递增 + if (lower[i + 1] == lower[i] + 1 && lower[i + 2] == lower[i] + 2) + { + return true; + } + // 检查递减 + if (lower[i + 1] == lower[i] - 1 && lower[i + 2] == lower[i] - 2) + { + return true; + } + } + + // 检查键盘序列 + foreach (string seq in KeyboardSequences) + { + if (seq.Contains(lower.Substring(0, Math.Min(3, lower.Length)))) + { + for (int i = 0; i <= lower.Length - 3; i++) + { + if (seq.Contains(lower.Substring(i, 3))) + { + return true; + } + } + } + } + + return false; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs b/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs new file mode 100644 index 0000000..175383a --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PhoneNumberUtil.cs @@ -0,0 +1,350 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 手机运营商枚举 + /// + public enum Carrier + { + /// + /// 未知运营商 + /// + Unknown = 0, + + /// + /// 中国移动 + /// + ChinaMobile = 1, + + /// + /// 中国联通 + /// + ChinaUnicom = 2, + + /// + /// 中国电信 + /// + ChinaTelecom = 3, + + /// + /// 中国广电 + /// + ChinaBroadnet = 4 + } + + /// + /// 手机号工具类 + /// + public static class PhoneNumberUtil + { + #region 常量与私有字段 + + /// + /// 手机号正则表达式(11位,1开头) + /// + private static readonly Regex PhoneRegex = new Regex(@"^1[3-9]\d{9}$", RegexOptions.Compiled); + + /// + /// 中国移动号段(前3-4位) + /// + private static readonly HashSet ChinaMobilePrefixes = new HashSet + { + "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", + "157", "158", "159", "172", "178", "182", "183", "184", "187", "188", + "195", "197", "198" + }; + + /// + /// 中国联通号段(前3-4位) + /// + private static readonly HashSet ChinaUnicomPrefixes = new HashSet + { + "130", "131", "132", "145", "155", "156", "166", "167", "175", "176", + "185", "186", "196" + }; + + /// + /// 中国电信号段(前3-4位) + /// + private static readonly HashSet ChinaTelecomPrefixes = new HashSet + { + "133", "149", "153", "173", "174", "177", "180", "181", "189", "191", + "193", "199" + }; + + /// + /// 中国广电号段(前3-4位) + /// + private static readonly HashSet ChinaBroadnetPrefixes = new HashSet + { + "192" + }; + + #endregion + + #region 验证方法 + + /// + /// 验证手机号格式是否有效 + /// + /// 手机号 + /// 是否有效 + public static bool IsValid(string? phoneNumber) + { + if (string.IsNullOrWhiteSpace(phoneNumber)) + { + return false; + } + + return PhoneRegex.IsMatch(phoneNumber); + } + + /// + /// 格式化并验证手机号(去除非数字字符后验证) + /// + /// 手机号 + /// 格式化后的手机号,无效返回null + public static string? Normalize(string? phoneNumber) + { + if (string.IsNullOrWhiteSpace(phoneNumber)) + { + return null; + } + + // 去除所有非数字字符 + string normalized = Regex.Replace(phoneNumber, @"\D", ""); + + if (!IsValid(normalized)) + { + return null; + } + + return normalized; + } + + #endregion + + #region 运营商识别 + + /// + /// 获取运营商枚举 + /// + /// 手机号 + /// 运营商枚举 + public static Carrier GetCarrier(string? phoneNumber) + { + if (!IsValid(phoneNumber)) + { + return Carrier.Unknown; + } + + string prefix3 = phoneNumber!.Substring(0, 3); + + if (ChinaMobilePrefixes.Contains(prefix3)) + { + return Carrier.ChinaMobile; + } + + if (ChinaUnicomPrefixes.Contains(prefix3)) + { + return Carrier.ChinaUnicom; + } + + if (ChinaTelecomPrefixes.Contains(prefix3)) + { + return Carrier.ChinaTelecom; + } + + if (ChinaBroadnetPrefixes.Contains(prefix3)) + { + return Carrier.ChinaBroadnet; + } + + return Carrier.Unknown; + } + + /// + /// 获取运营商名称 + /// + /// 手机号 + /// 运营商名称 + public static string? GetCarrierName(string? phoneNumber) + { + Carrier carrier = GetCarrier(phoneNumber); + return carrier switch + { + Carrier.ChinaMobile => "中国移动", + Carrier.ChinaUnicom => "中国联通", + Carrier.ChinaTelecom => "中国电信", + Carrier.ChinaBroadnet => "中国广电", + _ => null + }; + } + + /// + /// 判断是否为中国移动号码 + /// + /// 手机号 + /// 是否为移动号码 + public static bool IsChinaMobile(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaMobile; + } + + /// + /// 判断是否为中国联通号码 + /// + /// 手机号 + /// 是否为联通号码 + public static bool IsChinaUnicom(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaUnicom; + } + + /// + /// 判断是否为中国电信号码 + /// + /// 手机号 + /// 是否为电信号码 + public static bool IsChinaTelecom(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaTelecom; + } + + /// + /// 判断是否为中国广电号码 + /// + /// 手机号 + /// 是否为广电号码 + public static bool IsChinaBroadnet(string? phoneNumber) + { + return GetCarrier(phoneNumber) == Carrier.ChinaBroadnet; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化手机号(空格分隔):138 8888 8888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithSpaces(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)} {normalized.Substring(3, 4)} {normalized.Substring(7, 4)}"; + } + + /// + /// 格式化手机号(横线分隔):138-8888-8888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithHyphens(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 4)}-{normalized.Substring(7, 4)}"; + } + + /// + /// 格式化手机号(带国际区号):+86 13888888888 + /// + /// 手机号 + /// 格式化后的手机号 + public static string? FormatWithCountryCode(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"+86 {normalized}"; + } + + /// + /// 手机号脱敏:138****8888 + /// + /// 手机号 + /// 脱敏后的手机号 + public static string? Mask(string? phoneNumber) + { + string? normalized = Normalize(phoneNumber); + if (normalized == null) + { + return null; + } + + return $"{normalized.Substring(0, 3)}****{normalized.Substring(7, 4)}"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机手机号(仅供测试使用) + /// + /// 运营商(可选,默认随机) + /// 11位手机号 + public static string GenerateRandom(Carrier? carrier = null) + { + string prefix; + + if (carrier.HasValue && carrier.Value != Carrier.Unknown) + { + prefix = carrier.Value switch + { + Carrier.ChinaMobile => MathCategory.RandomUtil.GetRandomElement(ChinaMobilePrefixes), + Carrier.ChinaUnicom => MathCategory.RandomUtil.GetRandomElement(ChinaUnicomPrefixes), + Carrier.ChinaTelecom => MathCategory.RandomUtil.GetRandomElement(ChinaTelecomPrefixes), + Carrier.ChinaBroadnet => MathCategory.RandomUtil.GetRandomElement(ChinaBroadnetPrefixes), + _ => GetRandomPrefix() + }; + } + else + { + prefix = GetRandomPrefix(); + } + + // 生成剩余8位数字 + string suffix = MathCategory.RandomUtil.RandomDigitString(8); + + return prefix + suffix; + } + + #endregion + + #region 私有方法 + + /// + /// 获取随机号段前缀 + /// + private static string GetRandomPrefix() + { + var allPrefixes = new List(); + allPrefixes.AddRange(ChinaMobilePrefixes); + allPrefixes.AddRange(ChinaUnicomPrefixes); + allPrefixes.AddRange(ChinaTelecomPrefixes); + allPrefixes.AddRange(ChinaBroadnetPrefixes); + + return MathCategory.RandomUtil.GetRandomElement(allPrefixes); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PhoneUtil.cs b/EasyTool.Core/BusinessCategory/PhoneUtil.cs new file mode 100644 index 0000000..3a2142f --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PhoneUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 固定电话工具类 + /// + public static class PhoneUtil + { + #region 常量与私有字段 + + /// + /// 中国大陆固定电话正则表达式(带区号) + /// + private static readonly Regex PhoneWithAreaCodeRegex = new( + @"^(0\d{2,3}[-\s]?)?\d{7,8}$", + RegexOptions.Compiled); + + /// + /// 中国大陆固定电话正则表达式(完整格式) + /// + private static readonly Regex PhoneFullRegex = new( + @"^0\d{2,3}[-\s]?\d{7,8}$", + RegexOptions.Compiled); + + /// + /// 400电话正则表达式 + /// + private static readonly Regex Phone400Regex = new( + @"^400[-\s]?\d{3}[-\s]?\d{4}$", + RegexOptions.Compiled); + + /// + /// 800电话正则表达式 + /// + private static readonly Regex Phone800Regex = new( + @"^800[-\s]?\d{3}[-\s]?\d{4}$", + RegexOptions.Compiled); + + /// + /// 区号与城市映射 + /// + private static readonly Dictionary AreaCodeMap = new() + { + // 直辖市 + { "010", "北京" }, { "021", "上海" }, { "022", "天津" }, { "023", "重庆" }, + + // 省会城市 + { "0311", "石家庄" }, { "0351", "太原" }, { "0471", "呼和浩特" }, + { "024", "沈阳" }, { "0431", "长春" }, { "0451", "哈尔滨" }, + { "025", "南京" }, { "0571", "杭州" }, { "0551", "合肥" }, + { "0591", "福州" }, { "0791", "南昌" }, { "0531", "济南" }, + { "0371", "郑州" }, { "027", "武汉" }, { "0731", "长沙" }, + { "020", "广州" }, { "0771", "南宁" }, { "0898", "海口" }, + { "028", "成都" }, { "0851", "贵阳" }, { "0871", "昆明" }, + { "0891", "拉萨" }, { "029", "西安" }, { "0931", "兰州" }, + { "0971", "西宁" }, { "0951", "银川" }, { "0991", "乌鲁木齐" }, + + // 重要城市 + { "0755", "深圳" }, { "0756", "珠海" }, { "0754", "汕头" }, + { "0757", "佛山" }, { "0769", "东莞" }, { "0760", "中山" }, + { "0512", "苏州" }, { "0510", "无锡" }, { "0574", "宁波" }, + { "0577", "温州" }, { "0532", "青岛" }, { "0411", "大连" }, + { "0592", "厦门" }, { "0514", "扬州" }, { "0519", "常州" }, + { "0573", "嘉兴" }, { "0575", "绍兴" }, { "0576", "台州" }, + { "0579", "金华" }, { "0752", "惠州" }, { "0753", "梅州" }, + { "0758", "肇庆" }, { "0759", "湛江" }, { "0762", "河源" }, + { "0763", "清远" }, { "0766", "云浮" }, { "0768", "潮州" }, + { "0773", "桂林" }, { "0774", "梧州" }, { "0775", "玉林" }, + { "0779", "北海" }, { "0772", "柳州" }, { "0778", "河池" }, + { "0733", "株洲" }, { "0734", "衡阳" }, { "0735", "郴州" }, + { "0737", "益阳" }, { "0738", "娄底" }, { "0739", "邵阳" }, + { "0792", "九江" }, { "0793", "上饶" }, { "0795", "宜春" }, + { "0796", "吉安" }, { "0797", "赣州" }, { "0799", "萍乡" }, + { "0533", "淄博" }, { "0534", "德州" }, { "0535", "烟台" }, + { "0536", "潍坊" }, { "0537", "济宁" }, { "0538", "泰安" }, + { "0539", "临沂" }, { "0543", "滨州" }, { "0546", "东营" }, + { "0379", "洛阳" }, { "0378", "开封" }, { "0372", "安阳" }, + { "0373", "新乡" }, { "0374", "许昌" }, { "0375", "平顶山" }, + { "0370", "商丘" }, { "0391", "焦作" }, { "0393", "濮阳" }, + { "0395", "漯河" }, { "0396", "驻马店" }, { "0398", "三门峡" }, + { "0376", "信阳" }, { "0377", "南阳" }, { "0392", "鹤壁" }, + { "027", "武汉" }, { "0710", "襄阳" }, { "0711", "鄂州" }, + { "0712", "孝感" }, { "0713", "黄冈" }, { "0714", "黄石" }, + { "0715", "咸宁" }, { "0716", "荆州" }, { "0717", "宜昌" }, + { "0718", "恩施" }, { "0719", "十堰" }, { "0722", "随州" }, + { "0724", "荆门" }, { "0728", "仙桃" }, { "0730", "岳阳" }, + + // 三位区号 + { "0310", "邯郸" }, { "0312", "保定" }, { "0313", "张家口" }, + { "0314", "承德" }, { "0315", "唐山" }, { "0316", "廊坊" }, + { "0317", "沧州" }, { "0318", "衡水" }, { "0319", "邢台" }, + { "0335", "秦皇岛" }, { "0349", "朔州" }, { "0350", "忻州" }, + { "0352", "大同" }, { "0353", "阳泉" }, { "0354", "晋中" }, + { "0355", "长治" }, { "0356", "晋城" }, { "0357", "临汾" }, + { "0358", "吕梁" }, { "0359", "运城" }, { "0410", "铁岭" }, + { "0412", "鞍山" }, { "0413", "抚顺" }, { "0414", "本溪" }, + { "0415", "丹东" }, { "0416", "锦州" }, { "0417", "营口" }, + { "0418", "阜新" }, { "0419", "辽阳" }, { "0421", "朝阳" }, + { "0427", "盘锦" }, { "0429", "葫芦岛" }, { "0432", "吉林市" }, + { "0433", "延边" }, { "0434", "四平" }, { "0435", "通化" }, + { "0436", "白城" }, { "0437", "辽源" }, { "0439", "白山" }, + { "0438", "松原" }, { "0452", "齐齐哈尔" }, { "0453", "牡丹江" }, + { "0454", "佳木斯" }, { "0455", "绥化" }, { "0456", "黑河" }, + { "0457", "大兴安岭" }, { "0458", "伊春" }, { "0459", "大庆" }, + { "0464", "七台河" }, { "0467", "鸡西" }, { "0468", "鹤岗" }, + { "0469", "双鸭山" }, { "0470", "呼伦贝尔" }, { "0472", "包头" }, + { "0473", "乌海" }, { "0474", "乌兰察布" }, { "0475", "通辽" }, + { "0476", "赤峰" }, { "0477", "鄂尔多斯" }, { "0478", "巴彦淖尔" }, + { "0479", "锡林郭勒" }, { "0482", "兴安盟" }, { "0483", "阿拉善" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证固定电话是否有效 + /// + /// 固定电话号码 + /// 是否有效 + public static bool IsValid(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return PhoneWithAreaCodeRegex.IsMatch(phone) || + Is400Phone(phone) || Is800Phone(phone); + } + + /// + /// 验证是否为带区号的固定电话 + /// + /// 电话号码 + /// 是否为固定电话 + public static bool IsLandline(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return PhoneFullRegex.IsMatch(phone); + } + + /// + /// 验证是否为400电话 + /// + /// 电话号码 + /// 是否为400电话 + public static bool Is400Phone(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return Phone400Regex.IsMatch(phone); + } + + /// + /// 验证是否为800电话 + /// + /// 电话号码 + /// 是否为800电话 + public static bool Is800Phone(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return false; + } + + return Phone800Regex.IsMatch(phone); + } + + /// + /// 验证区号是否有效 + /// + /// 区号 + /// 是否有效 + public static bool IsValidAreaCode(string? areaCode) + { + if (string.IsNullOrWhiteSpace(areaCode)) + { + return false; + } + + string code = areaCode.TrimStart('0'); + return AreaCodeMap.ContainsKey("0" + code) || AreaCodeMap.ContainsKey(areaCode); + } + + #endregion + + #region 信息提取 + + /// + /// 获取区号 + /// + /// 电话号码 + /// 区号 + public static string? GetAreaCode(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + // 400/800电话无区号 + if (Is400Phone(phone) || Is800Phone(phone)) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + + // 三位区号(0开头) + if (cleaned.Length >= 10 && cleaned.StartsWith("0")) + { + string code3 = cleaned.Substring(0, 3); + if (AreaCodeMap.ContainsKey(code3)) + { + return code3; + } + } + + // 四位区号(0开头) + if (cleaned.Length >= 11 && cleaned.StartsWith("0")) + { + string code4 = cleaned.Substring(0, 4); + if (AreaCodeMap.ContainsKey(code4)) + { + return code4; + } + } + + // 尝试提取前3-4位作为区号 + if (cleaned.StartsWith("0")) + { + for (int len = Math.Min(4, cleaned.Length - 7); len >= 3; len--) + { + string code = cleaned.Substring(0, len); + if (AreaCodeMap.ContainsKey(code)) + { + return code; + } + } + } + + return null; + } + + /// + /// 获取城市名称 + /// + /// 电话号码 + /// 城市名称 + public static string? GetCity(string? phone) + { + string? areaCode = GetAreaCode(phone); + if (areaCode == null) + { + return null; + } + + return AreaCodeMap.TryGetValue(areaCode, out string? city) ? city : null; + } + + /// + /// 获取本地号码(不含区号) + /// + /// 电话号码 + /// 本地号码 + public static string? GetLocalNumber(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + // 400/800电话 + if (Is400Phone(phone) || Is800Phone(phone)) + { + string local = Regex.Replace(phone, @"[^\d]", ""); + return local.Length >= 10 ? local.Substring(3) : null; + } + + string? areaCode = GetAreaCode(phone); + if (areaCode == null) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + return cleaned.Substring(areaCode.Length); + } + + /// + /// 获取电话类型 + /// + /// 电话号码 + /// 电话类型描述 + public static string? GetPhoneType(string? phone) + { + if (Is400Phone(phone)) return "400企业热线"; + if (Is800Phone(phone)) return "800免费电话"; + if (IsLandline(phone)) return "固定电话"; + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化电话号码(去除非数字字符) + /// + /// 电话号码 + /// 格式化后的号码 + public static string? Normalize(string? phone) + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + string cleaned = Regex.Replace(phone, @"[^\d]", ""); + return cleaned.Length >= 7 ? cleaned : null; + } + + /// + /// 格式化为标准格式(区号-本地号码) + /// + /// 电话号码 + /// 格式化后的号码 + public static string? Format(string? phone) + { + string? normalized = Normalize(phone); + if (normalized == null) + { + return null; + } + + // 400电话 + if (normalized.StartsWith("400") && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 3)}-{normalized.Substring(6)}"; + } + + // 800电话 + if (normalized.StartsWith("800") && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-{normalized.Substring(3, 3)}-{normalized.Substring(6)}"; + } + + // 带区号的固定电话 + string? areaCode = GetAreaCode(normalized); + if (areaCode != null) + { + string local = normalized.Substring(areaCode.Length); + return $"{areaCode}-{local}"; + } + + return normalized; + } + + /// + /// 电话号码脱敏:010-****1234 + /// + /// 电话号码 + /// 脱敏后的号码 + public static string? Mask(string? phone) + { + if (!IsValid(phone)) + { + return null; + } + + string? areaCode = GetAreaCode(phone); + string? local = GetLocalNumber(phone); + + if (areaCode != null && local != null && local.Length >= 4) + { + int visibleSuffix = 4; + int maskLen = local.Length - visibleSuffix; + return $"{areaCode}-{new string('*', maskLen)}{local.Substring(maskLen)}"; + } + + // 400/800电话 + string? normalized = Normalize(phone); + if (normalized != null && normalized.Length == 10) + { + return $"{normalized.Substring(0, 3)}-****{normalized.Substring(6)}"; + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/PortUtil.cs b/EasyTool.Core/BusinessCategory/PortUtil.cs new file mode 100644 index 0000000..b2adf9b --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PortUtil.cs @@ -0,0 +1,447 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.BusinessCategory +{ + /// + /// 端口号工具类 + /// + public static class PortUtil + { + #region 常量与私有字段 + + /// + /// 知名端口号范围(0-1023) + /// + public const int WellKnownPortMin = 0; + + /// + /// 知名端口号范围上限 + /// + public const int WellKnownPortMax = 1023; + + /// + /// 注册端口号范围(1024-49151) + /// + public const int RegisteredPortMin = 1024; + + /// + /// 注册端口号范围上限 + /// + public const int RegisteredPortMax = 49151; + + /// + /// 动态/私有端口号范围(49152-65535) + /// + public const int DynamicPortMin = 49152; + + /// + /// 最大端口号 + /// + public const int MaxPort = 65535; + + /// + /// 常见端口与名称映射 + /// + private static readonly Dictionary CommonPorts = new() + { + // 文件传输 + { 20, new PortInfo("FTP Data", "文件传输协议数据端口", "FTP") }, + { 21, new PortInfo("FTP", "文件传输协议控制端口", "FTP") }, + + // 远程连接 + { 22, new PortInfo("SSH", "安全外壳协议", "SSH") }, + { 23, new PortInfo("Telnet", "远程终端协议", "Telnet") }, + { 3389, new PortInfo("RDP", "远程桌面协议", "RDP") }, + + // 邮件服务 + { 25, new PortInfo("SMTP", "简单邮件传输协议", "SMTP") }, + { 110, new PortInfo("POP3", "邮局协议第3版", "POP3") }, + { 143, new PortInfo("IMAP", "互联网消息访问协议", "IMAP") }, + { 465, new PortInfo("SMTPS", "SMTP安全协议", "SMTPS") }, + { 587, new PortInfo("SMTP(TLS)", "SMTP TLS协议", "SMTP") }, + { 993, new PortInfo("IMAPS", "IMAP安全协议", "IMAPS") }, + { 995, new PortInfo("POP3S", "POP3安全协议", "POP3S") }, + + // Web服务 + { 80, new PortInfo("HTTP", "超文本传输协议", "HTTP") }, + { 443, new PortInfo("HTTPS", "HTTP安全协议", "HTTPS") }, + { 8080, new PortInfo("HTTP-Proxy", "HTTP代理/备用端口", "HTTP") }, + { 8443, new PortInfo("HTTPS-Alt", "HTTPS备用端口", "HTTPS") }, + + // 域名服务 + { 53, new PortInfo("DNS", "域名系统", "DNS") }, + + // 数据库 + { 1433, new PortInfo("MSSQL", "Microsoft SQL Server", "MSSQL") }, + { 1521, new PortInfo("Oracle", "Oracle数据库", "Oracle") }, + { 3306, new PortInfo("MySQL", "MySQL数据库", "MySQL") }, + { 5432, new PortInfo("PostgreSQL", "PostgreSQL数据库", "PostgreSQL") }, + { 6379, new PortInfo("Redis", "Redis缓存", "Redis") }, + { 27017, new PortInfo("MongoDB", "MongoDB数据库", "MongoDB") }, + { 9200, new PortInfo("Elasticsearch", "Elasticsearch搜索", "Elasticsearch") }, + + // 消息队列 + { 5672, new PortInfo("RabbitMQ", "RabbitMQ消息队列", "RabbitMQ") }, + { 9092, new PortInfo("Kafka", "Kafka消息队列", "Kafka") }, + { 61616, new PortInfo("ActiveMQ", "ActiveMQ消息队列", "ActiveMQ") }, + + // 网络服务 + { 67, new PortInfo("DHCP Server", "DHCP服务器", "DHCP") }, + { 68, new PortInfo("DHCP Client", "DHCP客户端", "DHCP") }, + { 69, new PortInfo("TFTP", "简单文件传输协议", "TFTP") }, + { 123, new PortInfo("NTP", "网络时间协议", "NTP") }, + { 161, new PortInfo("SNMP", "简单网络管理协议", "SNMP") }, + { 162, new PortInfo("SNMP Trap", "SNMP陷阱", "SNMP") }, + { 514, new PortInfo("Syslog", "系统日志", "Syslog") }, + + // VPN + { 500, new PortInfo("IKE", "Internet密钥交换", "VPN") }, + { 1194, new PortInfo("OpenVPN", "OpenVPN", "VPN") }, + { 1723, new PortInfo("PPTP", "点对点隧道协议", "VPN") }, + + // 其他常用 + { 88, new PortInfo("Kerberos", "Kerberos认证", "Kerberos") }, + { 389, new PortInfo("LDAP", "轻量级目录访问协议", "LDAP") }, + { 636, new PortInfo("LDAPS", "LDAP安全协议", "LDAP") }, + { 4444, new PortInfo("Kerberos-Admin", "Kerberos管理", "Kerberos") }, + + // 即时通讯 + { 5222, new PortInfo("XMPP", "XMPP客户端连接", "XMPP") }, + { 5269, new PortInfo("XMPP-Server", "XMPP服务器连接", "XMPP") }, + + // 游戏服务 + { 25565, new PortInfo("Minecraft", "Minecraft服务器", "Minecraft") }, + { 27015, new PortInfo("Steam", "Steam游戏服务", "Steam") }, + + // 文件共享 + { 139, new PortInfo("NetBIOS-SSN", "NetBIOS会话服务", "NetBIOS") }, + { 445, new PortInfo("SMB", "Server Message Block", "SMB") }, + { 2049, new PortInfo("NFS", "网络文件系统", "NFS") }, + + // 代理服务 + { 1080, new PortInfo("SOCKS", "SOCKS代理", "SOCKS") }, + { 3128, new PortInfo("Squid", "Squid代理", "Squid") }, + + // Java相关 + { 1099, new PortInfo("RMI", "Java RMI注册", "RMI") }, + { 8009, new PortInfo("AJP", "Apache JServ协议", "AJP") }, + + // 监控 + { 9090, new PortInfo("Prometheus", "Prometheus监控", "Prometheus") }, + { 3000, new PortInfo("Grafana", "Grafana监控", "Grafana") }, + { 8500, new PortInfo("Consul", "Consul服务发现", "Consul") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证端口号是否有效 + /// + /// 端口号 + /// 是否有效 + public static bool IsValid(int port) + { + return port >= WellKnownPortMin && port <= MaxPort; + } + + /// + /// 验证端口号字符串是否有效 + /// + /// 端口号字符串 + /// 是否有效 + public static bool IsValid(string? port) + { + if (string.IsNullOrWhiteSpace(port)) + { + return false; + } + + if (!int.TryParse(port, out int portNum)) + { + return false; + } + + return IsValid(portNum); + } + + #endregion + + #region 端口类型判断 + + /// + /// 判断是否为知名端口(0-1023) + /// + /// 端口号 + /// 是否为知名端口 + public static bool IsWellKnownPort(int port) + { + return port >= WellKnownPortMin && port <= WellKnownPortMax; + } + + /// + /// 判断是否为注册端口(1024-49151) + /// + /// 端口号 + /// 是否为注册端口 + public static bool IsRegisteredPort(int port) + { + return port >= RegisteredPortMin && port <= RegisteredPortMax; + } + + /// + /// 判断是否为动态/私有端口(49152-65535) + /// + /// 端口号 + /// 是否为动态端口 + public static bool IsDynamicPort(int port) + { + return port >= DynamicPortMin && port <= MaxPort; + } + + /// + /// 获取端口类型 + /// + /// 端口号 + /// 端口类型 + public static PortType GetPortType(int port) + { + if (IsWellKnownPort(port)) + { + return PortType.WellKnown; + } + else if (IsRegisteredPort(port)) + { + return PortType.Registered; + } + else if (IsDynamicPort(port)) + { + return PortType.Dynamic; + } + else + { + return PortType.Invalid; + } + } + + /// + /// 获取端口类型名称 + /// + /// 端口号 + /// 端口类型名称 + public static string? GetPortTypeName(int port) + { + return GetPortType(port) switch + { + PortType.WellKnown => "知名端口", + PortType.Registered => "注册端口", + PortType.Dynamic => "动态/私有端口", + _ => null + }; + } + + #endregion + + #region 端口信息 + + /// + /// 获取端口信息 + /// + /// 端口号 + /// 端口信息 + public static PortInfo? GetPortInfo(int port) + { + if (!IsValid(port)) + { + return null; + } + + return CommonPorts.TryGetValue(port, out PortInfo? info) ? info : null; + } + + /// + /// 获取端口名称 + /// + /// 端口号 + /// 端口名称 + public static string? GetPortName(int port) + { + return GetPortInfo(port)?.Name; + } + + /// + /// 获取端口描述 + /// + /// 端口号 + /// 端口描述 + public static string? GetPortDescription(int port) + { + return GetPortInfo(port)?.Description; + } + + /// + /// 获取端口所属服务类别 + /// + /// 端口号 + /// 服务类别 + public static string? GetPortCategory(int port) + { + return GetPortInfo(port)?.Category; + } + + /// + /// 判断是否为常见端口 + /// + /// 端口号 + /// 是否为常见端口 + public static bool IsCommonPort(int port) + { + return CommonPorts.ContainsKey(port); + } + + #endregion + + #region 范围操作 + + /// + /// 获取指定范围内的所有端口 + /// + /// 起始端口 + /// 结束端口 + /// 端口列表 + public static int[] GetPortRange(int start, int end) + { + if (start < WellKnownPortMin || end > MaxPort || start > end) + { + return Array.Empty(); + } + + int[] ports = new int[end - start + 1]; + for (int i = 0; i < ports.Length; i++) + { + ports[i] = start + i; + } + return ports; + } + + /// + /// 获取所有知名端口 + /// + /// 知名端口数组 + public static int[] GetWellKnownPorts() + { + return GetPortRange(WellKnownPortMin, WellKnownPortMax); + } + + /// + /// 获取所有动态端口范围 + /// + /// 动态端口数组 + public static int[] GetDynamicPorts() + { + return GetPortRange(DynamicPortMin, MaxPort); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化端口号为字符串 + /// + /// 端口号 + /// 端口号字符串 + public static string? Format(int port) + { + if (!IsValid(port)) + { + return null; + } + + return port.ToString(); + } + + /// + /// 格式化端口信息 + /// + /// 端口号 + /// 格式化的端口信息 + public static string? FormatWithInfo(int port) + { + if (!IsValid(port)) + { + return null; + } + + PortInfo? info = GetPortInfo(port); + if (info != null) + { + return $"{port} ({info.Name})"; + } + + string? typeName = GetPortTypeName(port); + return $"{port} ({typeName})"; + } + + #endregion + } + + /// + /// 端口类型枚举 + /// + public enum PortType + { + /// + /// 无效端口 + /// + Invalid = 0, + + /// + /// 知名端口(0-1023) + /// + WellKnown = 1, + + /// + /// 注册端口(1024-49151) + /// + Registered = 2, + + /// + /// 动态/私有端口(49152-65535) + /// + Dynamic = 3 + } + + /// + /// 端口信息类 + /// + public class PortInfo + { + /// + /// 端口名称 + /// + public string Name { get; set; } + + /// + /// 端口描述 + /// + public string Description { get; set; } + + /// + /// 服务类别 + /// + public string Category { get; set; } + + /// + /// 构造函数 + /// + public PortInfo(string name, string description, string category) + { + Name = name; + Description = description; + Category = category; + } + } +} diff --git a/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs b/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs new file mode 100644 index 0000000..b4112de --- /dev/null +++ b/EasyTool.Core/BusinessCategory/PostalCodeUtil.cs @@ -0,0 +1,437 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 邮政编码工具类 + /// + public static class PostalCodeUtil + { + #region 常量与私有字段 + + /// + /// 中国邮政编码正则表达式(6位数字) + /// + private static readonly Regex PostalCodeRegex = new Regex(@"^\d{6}$", RegexOptions.Compiled); + + /// + /// 省份编码前缀与名称映射(邮政编码前2位) + /// + private static readonly Dictionary ProvincePrefixMap = new Dictionary + { + { "10", "北京市" }, { "11", "北京市" }, { "12", "天津市" }, + { "01", "上海市" }, { "02", "上海市" }, { "03", "上海市" }, { "20", "上海市" }, + { "05", "河北省" }, { "06", "河北省" }, { "07", "河北省" }, + { "03", "山西省" }, { "04", "山西省" }, { "03", "内蒙古自治区" }, { "01", "内蒙古自治区" }, { "02", "内蒙古自治区" }, + { "11", "辽宁省" }, { "12", "辽宁省" }, + { "13", "吉林省" }, { "10", "吉林省" }, + { "15", "黑龙江省" }, { "16", "黑龙江省" }, + { "21", "江苏省" }, { "22", "江苏省" }, + { "31", "浙江省" }, { "32", "浙江省" }, + { "23", "安徽省" }, { "24", "安徽省" }, + { "35", "福建省" }, { "36", "福建省" }, + { "33", "江西省" }, { "34", "江西省" }, + { "25", "山东省" }, { "26", "山东省" }, { "27", "山东省" }, + { "45", "河南省" }, { "46", "河南省" }, { "47", "河南省" }, + { "41", "湖北省" }, { "42", "湖北省" }, { "43", "湖北省" }, { "44", "湖北省" }, + { "41", "湖南省" }, { "42", "湖南省" }, { "43", "湖南省" }, + { "51", "广东省" }, { "52", "广东省" }, { "53", "广东省" }, + { "54", "广西壮族自治区" }, { "55", "广西壮族自治区" }, + { "57", "海南省" }, { "58", "海南省" }, + { "40", "重庆市" }, + { "61", "四川省" }, { "62", "四川省" }, { "63", "四川省" }, { "64", "四川省" }, + { "55", "贵州省" }, { "56", "贵州省" }, + { "65", "云南省" }, { "66", "云南省" }, { "67", "云南省" }, + { "85", "西藏自治区" }, { "86", "西藏自治区" }, + { "71", "陕西省" }, { "72", "陕西省" }, { "73", "陕西省" }, + { "73", "甘肃省" }, { "74", "甘肃省" }, + { "81", "青海省" }, { "82", "青海省" }, { "83", "青海省" }, + { "75", "宁夏回族自治区" }, + { "83", "新疆维吾尔自治区" }, { "84", "新疆维吾尔自治区" } + }; + + /// + /// 城市邮政编码范围映射(部分主要城市) + /// + private static readonly Dictionary CityCodeRanges = new Dictionary + { + // 直辖市 + { "北京", ("100000", "102999", "北京市") }, + { "上海", ("200000", "202999", "上海市") }, + { "天津", ("300000", "302999", "天津市") }, + { "重庆", ("400000", "409999", "重庆市") }, + + // 省会城市 + { "石家庄", ("050000", "052999", "石家庄市") }, + { "太原", ("030000", "032999", "太原市") }, + { "呼和浩特", ("010000", "012999", "呼和浩特市") }, + { "沈阳", ("110000", "112999", "沈阳市") }, + { "长春", ("130000", "132999", "长春市") }, + { "哈尔滨", ("150000", "152999", "哈尔滨市") }, + { "南京", ("210000", "212999", "南京市") }, + { "杭州", ("310000", "312999", "杭州市") }, + { "合肥", ("230000", "232999", "合肥市") }, + { "福州", ("350000", "352999", "福州市") }, + { "南昌", ("330000", "332999", "南昌市") }, + { "济南", ("250000", "252999", "济南市") }, + { "郑州", ("450000", "452999", "郑州市") }, + { "武汉", ("430000", "432999", "武汉市") }, + { "长沙", ("410000", "412999", "长沙市") }, + { "广州", ("510000", "512999", "广州市") }, + { "南宁", ("530000", "532999", "南宁市") }, + { "海口", ("570000", "572999", "海口市") }, + { "成都", ("610000", "612999", "成都市") }, + { "贵阳", ("550000", "552999", "贵阳市") }, + { "昆明", ("650000", "652999", "昆明市") }, + { "拉萨", ("850000", "852999", "拉萨市") }, + { "西安", ("710000", "712999", "西安市") }, + { "兰州", ("730000", "732999", "兰州市") }, + { "西宁", ("810000", "812999", "西宁市") }, + { "银川", ("750000", "752999", "银川市") }, + { "乌鲁木齐", ("830000", "832999", "乌鲁木齐市") }, + + // 重要城市 + { "深圳", ("518000", "518999", "深圳市") }, + { "珠海", ("519000", "519999", "珠海市") }, + { "汕头", ("515000", "515999", "汕头市") }, + { "佛山", ("528000", "528999", "佛山市") }, + { "东莞", ("523000", "523999", "东莞市") }, + { "中山", ("528400", "528499", "中山市") }, + { "苏州", ("215000", "215999", "苏州市") }, + { "无锡", ("214000", "214999", "无锡市") }, + { "宁波", ("315000", "315999", "宁波市") }, + { "温州", ("325000", "325999", "温州市") }, + { "青岛", ("266000", "266999", "青岛市") }, + { "大连", ("116000", "116999", "大连市") }, + { "厦门", ("361000", "361999", "厦门市") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证邮政编码格式是否有效 + /// + /// 邮政编码 + /// 是否有效 + public static bool IsValid(string? postalCode) + { + if (string.IsNullOrWhiteSpace(postalCode)) + { + return false; + } + + return PostalCodeRegex.IsMatch(postalCode); + } + + /// + /// 验证邮政编码是否有效且存在对应的省份 + /// + /// 邮政编码 + /// 是否为有效且存在的邮政编码 + public static bool IsValidAndExists(string? postalCode) + { + if (!IsValid(postalCode)) + { + return false; + } + + return GetProvince(postalCode) != null; + } + + #endregion + + #region 信息查询 + + /// + /// 获取省份名称 + /// + /// 邮政编码 + /// 省份名称 + public static string? GetProvince(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + string prefix = postalCode!.Substring(0, 2); + + // 特殊处理直辖市 + if (prefix == "10" || prefix == "11") + { + return "北京市"; + } + if (prefix == "12") + { + return "天津市"; + } + if (prefix == "20" || prefix == "01" || prefix == "02") + { + return "上海市"; + } + if (prefix == "40") + { + return "重庆市"; + } + + // 根据前2位判断省份 + return prefix switch + { + "05" or "06" or "07" => "河北省", + "03" or "04" => "山西省", + "01" or "02" => CheckInnerMongolia(postalCode) ? "内蒙古自治区" : null, + "11" or "12" => "辽宁省", + "13" => "吉林省", + "15" or "16" => "黑龙江省", + "21" or "22" => "江苏省", + "31" or "32" => "浙江省", + "23" or "24" => "安徽省", + "35" or "36" => "福建省", + "33" or "34" => "江西省", + "25" or "26" or "27" => "山东省", + "45" or "46" or "47" => "河南省", + "43" or "44" => "湖北省", + "41" or "42" => "湖南省", + "51" or "52" or "53" => "广东省", + "54" or "55" => "广西壮族自治区", + "57" or "58" => "海南省", + "61" or "62" or "63" or "64" => "四川省", + "55" or "56" => "贵州省", + "65" or "66" or "67" => "云南省", + "85" or "86" => "西藏自治区", + "71" or "72" or "73" => "陕西省", + "73" or "74" => "甘肃省", + "81" or "82" => "青海省", + "75" => "宁夏回族自治区", + "83" or "84" => "新疆维吾尔自治区", + _ => null + }; + } + + /// + /// 获取城市名称(部分城市支持) + /// + /// 邮政编码 + /// 城市名称 + public static string? GetCity(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + string code = postalCode!; + + // 遍历城市编码范围 + foreach (var kvp in CityCodeRanges) + { + if (string.Compare(code, kvp.Value.Min) >= 0 && string.Compare(code, kvp.Value.Max) <= 0) + { + return kvp.Value.City; + } + } + + return null; + } + + /// + /// 根据城市名称查询邮政编码(返回主要邮编) + /// + /// 城市名称 + /// 邮政编码,未找到返回null + public static string? GetPostalCodeByCity(string? cityName) + { + if (string.IsNullOrWhiteSpace(cityName)) + { + return null; + } + + // 处理常见城市名称变体 + string normalizedCity = cityName.Replace("市", "").Trim(); + + foreach (var kvp in CityCodeRanges) + { + if (kvp.Key.Contains(normalizedCity) || normalizedCity.Contains(kvp.Key)) + { + return kvp.Value.Min; + } + } + + return null; + } + + /// + /// 获取邮政编码前缀(前2位) + /// + /// 邮政编码 + /// 前缀 + public static string? GetPrefix(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(0, 2); + } + + /// + /// 获取邮政编码后缀(后4位) + /// + /// 邮政编码 + /// 后缀 + public static string? GetSuffix(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(2, 4); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化邮政编码(去除非数字字符) + /// + /// 邮政编码 + /// 格式化后的邮政编码 + public static string? Normalize(string? postalCode) + { + if (string.IsNullOrWhiteSpace(postalCode)) + { + return null; + } + + // 去除所有非数字字符 + string normalized = Regex.Replace(postalCode, @"\D", ""); + + if (normalized.Length != 6) + { + return null; + } + + return normalized; + } + + /// + /// 邮政编码脱敏:100*** + /// + /// 邮政编码 + /// 脱敏后的邮政编码 + public static string? Mask(string? postalCode) + { + if (!IsValid(postalCode)) + { + return null; + } + + return postalCode!.Substring(0, 3) + "***"; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机邮政编码(仅供测试使用) + /// + /// 省份名称(可选,默认随机) + /// 6位邮政编码 + public static string GenerateRandom(string? province = null) + { + if (!string.IsNullOrWhiteSpace(province)) + { + // 根据省份生成 + string prefix = GetProvincePrefix(province); + if (!string.IsNullOrEmpty(prefix)) + { + return prefix + MathCategory.RandomUtil.RandomDigitString(4); + } + } + + // 随机生成有效前缀 + string[] validPrefixes = { + "10", "11", "12", "20", "30", "40", + "05", "06", "07", "03", "04", "01", "02", + "11", "12", "13", "15", "16", + "21", "22", "31", "32", "23", "24", + "35", "36", "33", "34", "25", "26", "27", + "45", "46", "47", "43", "44", "41", "42", + "51", "52", "53", "54", "55", "57", "58", + "40", "61", "62", "63", "64", "65", "66", "67", + "85", "86", "71", "72", "73", "74", "75", "81", "82", "83", "84" + }; + + string randomPrefix = MathCategory.RandomUtil.GetRandomElement(validPrefixes); + return randomPrefix + MathCategory.RandomUtil.RandomDigitString(4); + } + + #endregion + + #region 私有方法 + + /// + /// 检查是否为内蒙古邮编 + /// + private static bool CheckInnerMongolia(string postalCode) + { + // 内蒙古邮编范围:010000-029999 + string prefix = postalCode.Substring(0, 2); + return prefix == "01" || prefix == "02"; + } + + /// + /// 根据省份名称获取邮编前缀 + /// + private static string? GetProvincePrefix(string province) + { + string normalized = province.Replace("省", "").Replace("市", "").Replace("自治区", "").Trim(); + + return normalized switch + { + "北京" => "10", + "上海" => "20", + "天津" => "30", + "重庆" => "40", + "河北" => "05", + "山西" => "03", + "内蒙古" => "01", + "辽宁" => "11", + "吉林" => "13", + "黑龙江" => "15", + "江苏" => "21", + "浙江" => "31", + "安徽" => "23", + "福建" => "35", + "江西" => "33", + "山东" => "25", + "河南" => "45", + "湖北" => "43", + "湖南" => "41", + "广东" => "51", + "广西" => "54", + "海南" => "57", + "四川" => "61", + "贵州" => "55", + "云南" => "65", + "西藏" => "85", + "陕西" => "71", + "甘肃" => "73", + "青海" => "81", + "宁夏" => "75", + "新疆" => "83", + _ => null + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/ProvinceUtil.cs b/EasyTool.Core/BusinessCategory/ProvinceUtil.cs new file mode 100644 index 0000000..acdfd75 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/ProvinceUtil.cs @@ -0,0 +1,573 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.BusinessCategory +{ + /// + /// 中国省份城市工具类 + /// 提供省份、城市查询和验证功能 + /// + public static class ProvinceUtil + { + /// + /// 省份数据 + /// + private static readonly Dictionary Provinces = new() + { + { "110000", new ProvinceInfo { Code = "110000", Name = "北京市", ShortName = "北京", Cities = new List { + new CityInfo { Code = "110100", Name = "北京市" } + }}}, + { "120000", new ProvinceInfo { Code = "120000", Name = "天津市", ShortName = "天津", Cities = new List { + new CityInfo { Code = "120100", Name = "天津市" } + }}}, + { "130000", new ProvinceInfo { Code = "130000", Name = "河北省", ShortName = "河北", Cities = new List { + new CityInfo { Code = "130100", Name = "石家庄市" }, + new CityInfo { Code = "130200", Name = "唐山市" }, + new CityInfo { Code = "130300", Name = "秦皇岛市" }, + new CityInfo { Code = "130400", Name = "邯郸市" }, + new CityInfo { Code = "130500", Name = "邢台市" }, + new CityInfo { Code = "130600", Name = "保定市" }, + new CityInfo { Code = "130700", Name = "张家口市" }, + new CityInfo { Code = "130800", Name = "承德市" }, + new CityInfo { Code = "130900", Name = "沧州市" }, + new CityInfo { Code = "131000", Name = "廊坊市" }, + new CityInfo { Code = "131100", Name = "衡水市" } + }}}, + { "140000", new ProvinceInfo { Code = "140000", Name = "山西省", ShortName = "山西", Cities = new List { + new CityInfo { Code = "140100", Name = "太原市" }, + new CityInfo { Code = "140200", Name = "大同市" }, + new CityInfo { Code = "140300", Name = "阳泉市" }, + new CityInfo { Code = "140400", Name = "长治市" }, + new CityInfo { Code = "140500", Name = "晋城市" }, + new CityInfo { Code = "140600", Name = "朔州市" }, + new CityInfo { Code = "140700", Name = "晋中市" }, + new CityInfo { Code = "140800", Name = "运城市" }, + new CityInfo { Code = "140900", Name = "忻州市" }, + new CityInfo { Code = "141000", Name = "临汾市" }, + new CityInfo { Code = "141100", Name = "吕梁市" } + }}}, + { "150000", new ProvinceInfo { Code = "150000", Name = "内蒙古自治区", ShortName = "内蒙古", Cities = new List { + new CityInfo { Code = "150100", Name = "呼和浩特市" }, + new CityInfo { Code = "150200", Name = "包头市" }, + new CityInfo { Code = "150300", Name = "乌海市" }, + new CityInfo { Code = "150400", Name = "赤峰市" }, + new CityInfo { Code = "150500", Name = "通辽市" }, + new CityInfo { Code = "150600", Name = "鄂尔多斯市" }, + new CityInfo { Code = "150700", Name = "呼伦贝尔市" }, + new CityInfo { Code = "150800", Name = "巴彦淖尔市" }, + new CityInfo { Code = "150900", Name = "乌兰察布市" } + }}}, + { "210000", new ProvinceInfo { Code = "210000", Name = "辽宁省", ShortName = "辽宁", Cities = new List { + new CityInfo { Code = "210100", Name = "沈阳市" }, + new CityInfo { Code = "210200", Name = "大连市" }, + new CityInfo { Code = "210300", Name = "鞍山市" }, + new CityInfo { Code = "210400", Name = "抚顺市" }, + new CityInfo { Code = "210500", Name = "本溪市" }, + new CityInfo { Code = "210600", Name = "丹东市" }, + new CityInfo { Code = "210700", Name = "锦州市" }, + new CityInfo { Code = "210800", Name = "营口市" }, + new CityInfo { Code = "210900", Name = "阜新市" }, + new CityInfo { Code = "211000", Name = "辽阳市" }, + new CityInfo { Code = "211100", Name = "盘锦市" }, + new CityInfo { Code = "211200", Name = "铁岭市" }, + new CityInfo { Code = "211300", Name = "朝阳市" }, + new CityInfo { Code = "211400", Name = "葫芦岛市" } + }}}, + { "220000", new ProvinceInfo { Code = "220000", Name = "吉林省", ShortName = "吉林", Cities = new List { + new CityInfo { Code = "220100", Name = "长春市" }, + new CityInfo { Code = "220200", Name = "吉林市" }, + new CityInfo { Code = "220300", Name = "四平市" }, + new CityInfo { Code = "220400", Name = "辽源市" }, + new CityInfo { Code = "220500", Name = "通化市" }, + new CityInfo { Code = "220600", Name = "白山市" }, + new CityInfo { Code = "220700", Name = "松原市" }, + new CityInfo { Code = "220800", Name = "白城市" } + }}}, + { "230000", new ProvinceInfo { Code = "230000", Name = "黑龙江省", ShortName = "黑龙江", Cities = new List { + new CityInfo { Code = "230100", Name = "哈尔滨市" }, + new CityInfo { Code = "230200", Name = "齐齐哈尔市" }, + new CityInfo { Code = "230300", Name = "鸡西市" }, + new CityInfo { Code = "230400", Name = "鹤岗市" }, + new CityInfo { Code = "230500", Name = "双鸭山市" }, + new CityInfo { Code = "230600", Name = "大庆市" }, + new CityInfo { Code = "230700", Name = "伊春市" }, + new CityInfo { Code = "230800", Name = "佳木斯市" }, + new CityInfo { Code = "230900", Name = "七台河市" }, + new CityInfo { Code = "231000", Name = "牡丹江市" }, + new CityInfo { Code = "231100", Name = "黑河市" }, + new CityInfo { Code = "231200", Name = "绥化市" } + }}}, + { "310000", new ProvinceInfo { Code = "310000", Name = "上海市", ShortName = "上海", Cities = new List { + new CityInfo { Code = "310100", Name = "上海市" } + }}}, + { "320000", new ProvinceInfo { Code = "320000", Name = "江苏省", ShortName = "江苏", Cities = new List { + new CityInfo { Code = "320100", Name = "南京市" }, + new CityInfo { Code = "320200", Name = "无锡市" }, + new CityInfo { Code = "320300", Name = "徐州市" }, + new CityInfo { Code = "320400", Name = "常州市" }, + new CityInfo { Code = "320500", Name = "苏州市" }, + new CityInfo { Code = "320600", Name = "南通市" }, + new CityInfo { Code = "320700", Name = "连云港市" }, + new CityInfo { Code = "320800", Name = "淮安市" }, + new CityInfo { Code = "320900", Name = "盐城市" }, + new CityInfo { Code = "321000", Name = "扬州市" }, + new CityInfo { Code = "321100", Name = "镇江市" }, + new CityInfo { Code = "321200", Name = "泰州市" }, + new CityInfo { Code = "321300", Name = "宿迁市" } + }}}, + { "330000", new ProvinceInfo { Code = "330000", Name = "浙江省", ShortName = "浙江", Cities = new List { + new CityInfo { Code = "330100", Name = "杭州市" }, + new CityInfo { Code = "330200", Name = "宁波市" }, + new CityInfo { Code = "330300", Name = "温州市" }, + new CityInfo { Code = "330400", Name = "嘉兴市" }, + new CityInfo { Code = "330500", Name = "湖州市" }, + new CityInfo { Code = "330600", Name = "绍兴市" }, + new CityInfo { Code = "330700", Name = "金华市" }, + new CityInfo { Code = "330800", Name = "衢州市" }, + new CityInfo { Code = "330900", Name = "舟山市" }, + new CityInfo { Code = "331000", Name = "台州市" }, + new CityInfo { Code = "331100", Name = "丽水市" } + }}}, + { "340000", new ProvinceInfo { Code = "340000", Name = "安徽省", ShortName = "安徽", Cities = new List { + new CityInfo { Code = "340100", Name = "合肥市" }, + new CityInfo { Code = "340200", Name = "芜湖市" }, + new CityInfo { Code = "340300", Name = "蚌埠市" }, + new CityInfo { Code = "340400", Name = "淮南市" }, + new CityInfo { Code = "340500", Name = "马鞍山市" }, + new CityInfo { Code = "340600", Name = "淮北市" }, + new CityInfo { Code = "340700", Name = "铜陵市" }, + new CityInfo { Code = "340800", Name = "安庆市" }, + new CityInfo { Code = "341000", Name = "黄山市" }, + new CityInfo { Code = "341100", Name = "滁州市" }, + new CityInfo { Code = "341200", Name = "阜阳市" }, + new CityInfo { Code = "341300", Name = "宿州市" }, + new CityInfo { Code = "341500", Name = "六安市" }, + new CityInfo { Code = "341600", Name = "亳州市" }, + new CityInfo { Code = "341700", Name = "池州市" }, + new CityInfo { Code = "341800", Name = "宣城市" } + }}}, + { "350000", new ProvinceInfo { Code = "350000", Name = "福建省", ShortName = "福建", Cities = new List { + new CityInfo { Code = "350100", Name = "福州市" }, + new CityInfo { Code = "350200", Name = "厦门市" }, + new CityInfo { Code = "350300", Name = "莆田市" }, + new CityInfo { Code = "350400", Name = "三明市" }, + new CityInfo { Code = "350500", Name = "泉州市" }, + new CityInfo { Code = "350600", Name = "漳州市" }, + new CityInfo { Code = "350700", Name = "南平市" }, + new CityInfo { Code = "350800", Name = "龙岩市" }, + new CityInfo { Code = "350900", Name = "宁德市" } + }}}, + { "360000", new ProvinceInfo { Code = "360000", Name = "江西省", ShortName = "江西", Cities = new List { + new CityInfo { Code = "360100", Name = "南昌市" }, + new CityInfo { Code = "360200", Name = "景德镇市" }, + new CityInfo { Code = "360300", Name = "萍乡市" }, + new CityInfo { Code = "360400", Name = "九江市" }, + new CityInfo { Code = "360500", Name = "新余市" }, + new CityInfo { Code = "360600", Name = "鹰潭市" }, + new CityInfo { Code = "360700", Name = "赣州市" }, + new CityInfo { Code = "360800", Name = "吉安市" }, + new CityInfo { Code = "360900", Name = "宜春市" }, + new CityInfo { Code = "361000", Name = "抚州市" }, + new CityInfo { Code = "361100", Name = "上饶市" } + }}}, + { "370000", new ProvinceInfo { Code = "370000", Name = "山东省", ShortName = "山东", Cities = new List { + new CityInfo { Code = "370100", Name = "济南市" }, + new CityInfo { Code = "370200", Name = "青岛市" }, + new CityInfo { Code = "370300", Name = "淄博市" }, + new CityInfo { Code = "370400", Name = "枣庄市" }, + new CityInfo { Code = "370500", Name = "东营市" }, + new CityInfo { Code = "370600", Name = "烟台市" }, + new CityInfo { Code = "370700", Name = "潍坊市" }, + new CityInfo { Code = "370800", Name = "济宁市" }, + new CityInfo { Code = "370900", Name = "泰安市" }, + new CityInfo { Code = "371000", Name = "威海市" }, + new CityInfo { Code = "371100", Name = "日照市" }, + new CityInfo { Code = "371300", Name = "临沂市" }, + new CityInfo { Code = "371400", Name = "德州市" }, + new CityInfo { Code = "371500", Name = "聊城市" }, + new CityInfo { Code = "371600", Name = "滨州市" }, + new CityInfo { Code = "371700", Name = "菏泽市" } + }}}, + { "410000", new ProvinceInfo { Code = "410000", Name = "河南省", ShortName = "河南", Cities = new List { + new CityInfo { Code = "410100", Name = "郑州市" }, + new CityInfo { Code = "410200", Name = "开封市" }, + new CityInfo { Code = "410300", Name = "洛阳市" }, + new CityInfo { Code = "410400", Name = "平顶山市" }, + new CityInfo { Code = "410500", Name = "安阳市" }, + new CityInfo { Code = "410600", Name = "鹤壁市" }, + new CityInfo { Code = "410700", Name = "新乡市" }, + new CityInfo { Code = "410800", Name = "焦作市" }, + new CityInfo { Code = "410900", Name = "濮阳市" }, + new CityInfo { Code = "411000", Name = "许昌市" }, + new CityInfo { Code = "411100", Name = "漯河市" }, + new CityInfo { Code = "411200", Name = "三门峡市" }, + new CityInfo { Code = "411300", Name = "南阳市" }, + new CityInfo { Code = "411400", Name = "商丘市" }, + new CityInfo { Code = "411500", Name = "信阳市" }, + new CityInfo { Code = "411600", Name = "周口市" }, + new CityInfo { Code = "411700", Name = "驻马店市" } + }}}, + { "420000", new ProvinceInfo { Code = "420000", Name = "湖北省", ShortName = "湖北", Cities = new List { + new CityInfo { Code = "420100", Name = "武汉市" }, + new CityInfo { Code = "420200", Name = "黄石市" }, + new CityInfo { Code = "420300", Name = "十堰市" }, + new CityInfo { Code = "420500", Name = "宜昌市" }, + new CityInfo { Code = "420600", Name = "襄阳市" }, + new CityInfo { Code = "420700", Name = "鄂州市" }, + new CityInfo { Code = "420800", Name = "荆门市" }, + new CityInfo { Code = "420900", Name = "孝感市" }, + new CityInfo { Code = "421000", Name = "荆州市" }, + new CityInfo { Code = "421100", Name = "黄冈市" }, + new CityInfo { Code = "421200", Name = "咸宁市" }, + new CityInfo { Code = "421300", Name = "随州市" } + }}}, + { "430000", new ProvinceInfo { Code = "430000", Name = "湖南省", ShortName = "湖南", Cities = new List { + new CityInfo { Code = "430100", Name = "长沙市" }, + new CityInfo { Code = "430200", Name = "株洲市" }, + new CityInfo { Code = "430300", Name = "湘潭市" }, + new CityInfo { Code = "430400", Name = "衡阳市" }, + new CityInfo { Code = "430500", Name = "邵阳市" }, + new CityInfo { Code = "430600", Name = "岳阳市" }, + new CityInfo { Code = "430700", Name = "常德市" }, + new CityInfo { Code = "430800", Name = "张家界市" }, + new CityInfo { Code = "430900", Name = "益阳市" }, + new CityInfo { Code = "431000", Name = "郴州市" }, + new CityInfo { Code = "431100", Name = "永州市" }, + new CityInfo { Code = "431200", Name = "怀化市" }, + new CityInfo { Code = "431300", Name = "娄底市" } + }}}, + { "440000", new ProvinceInfo { Code = "440000", Name = "广东省", ShortName = "广东", Cities = new List { + new CityInfo { Code = "440100", Name = "广州市" }, + new CityInfo { Code = "440200", Name = "韶关市" }, + new CityInfo { Code = "440300", Name = "深圳市" }, + new CityInfo { Code = "440400", Name = "珠海市" }, + new CityInfo { Code = "440500", Name = "汕头市" }, + new CityInfo { Code = "440600", Name = "佛山市" }, + new CityInfo { Code = "440700", Name = "江门市" }, + new CityInfo { Code = "440800", Name = "湛江市" }, + new CityInfo { Code = "440900", Name = "茂名市" }, + new CityInfo { Code = "441200", Name = "肇庆市" }, + new CityInfo { Code = "441300", Name = "惠州市" }, + new CityInfo { Code = "441400", Name = "梅州市" }, + new CityInfo { Code = "441500", Name = "汕尾市" }, + new CityInfo { Code = "441600", Name = "河源市" }, + new CityInfo { Code = "441700", Name = "阳江市" }, + new CityInfo { Code = "441800", Name = "清远市" }, + new CityInfo { Code = "441900", Name = "东莞市" }, + new CityInfo { Code = "442000", Name = "中山市" }, + new CityInfo { Code = "445100", Name = "潮州市" }, + new CityInfo { Code = "445200", Name = "揭阳市" }, + new CityInfo { Code = "445300", Name = "云浮市" } + }}}, + { "450000", new ProvinceInfo { Code = "450000", Name = "广西壮族自治区", ShortName = "广西", Cities = new List { + new CityInfo { Code = "450100", Name = "南宁市" }, + new CityInfo { Code = "450200", Name = "柳州市" }, + new CityInfo { Code = "450300", Name = "桂林市" }, + new CityInfo { Code = "450400", Name = "梧州市" }, + new CityInfo { Code = "450500", Name = "北海市" }, + new CityInfo { Code = "450600", Name = "防城港市" }, + new CityInfo { Code = "450700", Name = "钦州市" }, + new CityInfo { Code = "450800", Name = "贵港市" }, + new CityInfo { Code = "450900", Name = "玉林市" }, + new CityInfo { Code = "451000", Name = "百色市" }, + new CityInfo { Code = "451100", Name = "贺州市" }, + new CityInfo { Code = "451200", Name = "河池市" }, + new CityInfo { Code = "451300", Name = "来宾市" }, + new CityInfo { Code = "451400", Name = "崇左市" } + }}}, + { "460000", new ProvinceInfo { Code = "460000", Name = "海南省", ShortName = "海南", Cities = new List { + new CityInfo { Code = "460100", Name = "海口市" }, + new CityInfo { Code = "460200", Name = "三亚市" }, + new CityInfo { Code = "460300", Name = "三沙市" }, + new CityInfo { Code = "460400", Name = "儋州市" } + }}}, + { "500000", new ProvinceInfo { Code = "500000", Name = "重庆市", ShortName = "重庆", Cities = new List { + new CityInfo { Code = "500100", Name = "重庆市" } + }}}, + { "510000", new ProvinceInfo { Code = "510000", Name = "四川省", ShortName = "四川", Cities = new List { + new CityInfo { Code = "510100", Name = "成都市" }, + new CityInfo { Code = "510300", Name = "自贡市" }, + new CityInfo { Code = "510400", Name = "攀枝花市" }, + new CityInfo { Code = "510500", Name = "泸州市" }, + new CityInfo { Code = "510600", Name = "德阳市" }, + new CityInfo { Code = "510700", Name = "绵阳市" }, + new CityInfo { Code = "510800", Name = "广元市" }, + new CityInfo { Code = "510900", Name = "遂宁市" }, + new CityInfo { Code = "511000", Name = "内江市" }, + new CityInfo { Code = "511100", Name = "乐山市" }, + new CityInfo { Code = "511300", Name = "南充市" }, + new CityInfo { Code = "511400", Name = "眉山市" }, + new CityInfo { Code = "511500", Name = "宜宾市" }, + new CityInfo { Code = "511600", Name = "广安市" }, + new CityInfo { Code = "511700", Name = "达州市" }, + new CityInfo { Code = "511800", Name = "雅安市" }, + new CityInfo { Code = "511900", Name = "巴中市" }, + new CityInfo { Code = "512000", Name = "资阳市" } + }}}, + { "520000", new ProvinceInfo { Code = "520000", Name = "贵州省", ShortName = "贵州", Cities = new List { + new CityInfo { Code = "520100", Name = "贵阳市" }, + new CityInfo { Code = "520200", Name = "六盘水市" }, + new CityInfo { Code = "520300", Name = "遵义市" }, + new CityInfo { Code = "520400", Name = "安顺市" }, + new CityInfo { Code = "520500", Name = "毕节市" }, + new CityInfo { Code = "520600", Name = "铜仁市" } + }}}, + { "530000", new ProvinceInfo { Code = "530000", Name = "云南省", ShortName = "云南", Cities = new List { + new CityInfo { Code = "530100", Name = "昆明市" }, + new CityInfo { Code = "530300", Name = "曲靖市" }, + new CityInfo { Code = "530400", Name = "玉溪市" }, + new CityInfo { Code = "530500", Name = "保山市" }, + new CityInfo { Code = "530600", Name = "昭通市" }, + new CityInfo { Code = "530700", Name = "丽江市" }, + new CityInfo { Code = "530800", Name = "普洱市" }, + new CityInfo { Code = "530900", Name = "临沧市" } + }}}, + { "540000", new ProvinceInfo { Code = "540000", Name = "西藏自治区", ShortName = "西藏", Cities = new List { + new CityInfo { Code = "540100", Name = "拉萨市" }, + new CityInfo { Code = "540200", Name = "日喀则市" }, + new CityInfo { Code = "540300", Name = "昌都市" }, + new CityInfo { Code = "540400", Name = "林芝市" }, + new CityInfo { Code = "540500", Name = "山南市" }, + new CityInfo { Code = "540600", Name = "那曲市" } + }}}, + { "610000", new ProvinceInfo { Code = "610000", Name = "陕西省", ShortName = "陕西", Cities = new List { + new CityInfo { Code = "610100", Name = "西安市" }, + new CityInfo { Code = "610200", Name = "铜川市" }, + new CityInfo { Code = "610300", Name = "宝鸡市" }, + new CityInfo { Code = "610400", Name = "咸阳市" }, + new CityInfo { Code = "610500", Name = "渭南市" }, + new CityInfo { Code = "610600", Name = "延安市" }, + new CityInfo { Code = "610700", Name = "汉中市" }, + new CityInfo { Code = "610800", Name = "榆林市" }, + new CityInfo { Code = "610900", Name = "安康市" }, + new CityInfo { Code = "611000", Name = "商洛市" } + }}}, + { "620000", new ProvinceInfo { Code = "620000", Name = "甘肃省", ShortName = "甘肃", Cities = new List { + new CityInfo { Code = "620100", Name = "兰州市" }, + new CityInfo { Code = "620200", Name = "嘉峪关市" }, + new CityInfo { Code = "620300", Name = "金昌市" }, + new CityInfo { Code = "620400", Name = "白银市" }, + new CityInfo { Code = "620500", Name = "天水市" }, + new CityInfo { Code = "620600", Name = "武威市" }, + new CityInfo { Code = "620700", Name = "张掖市" }, + new CityInfo { Code = "620800", Name = "平凉市" }, + new CityInfo { Code = "620900", Name = "酒泉市" }, + new CityInfo { Code = "621000", Name = "庆阳市" }, + new CityInfo { Code = "621100", Name = "定西市" }, + new CityInfo { Code = "621200", Name = "陇南市" } + }}}, + { "630000", new ProvinceInfo { Code = "630000", Name = "青海省", ShortName = "青海", Cities = new List { + new CityInfo { Code = "630100", Name = "西宁市" }, + new CityInfo { Code = "630200", Name = "海东市" } + }}}, + { "640000", new ProvinceInfo { Code = "640000", Name = "宁夏回族自治区", ShortName = "宁夏", Cities = new List { + new CityInfo { Code = "640100", Name = "银川市" }, + new CityInfo { Code = "640200", Name = "石嘴山市" }, + new CityInfo { Code = "640300", Name = "吴忠市" }, + new CityInfo { Code = "640400", Name = "固原市" }, + new CityInfo { Code = "640500", Name = "中卫市" } + }}}, + { "650000", new ProvinceInfo { Code = "650000", Name = "新疆维吾尔自治区", ShortName = "新疆", Cities = new List { + new CityInfo { Code = "650100", Name = "乌鲁木齐市" }, + new CityInfo { Code = "650200", Name = "克拉玛依市" } + }}}, + { "710000", new ProvinceInfo { Code = "710000", Name = "台湾省", ShortName = "台湾", Cities = new List() }}, + { "810000", new ProvinceInfo { Code = "810000", Name = "香港特别行政区", ShortName = "香港", Cities = new List() }}, + { "820000", new ProvinceInfo { Code = "820000", Name = "澳门特别行政区", ShortName = "澳门", Cities = new List() }} + }; + + /// + /// 根据省份代码获取省份信息 + /// + public static ProvinceInfo? GetProvinceByCode(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length < 2) + return null; + + var provinceCode = code.Substring(0, 2) + "0000"; + return Provinces.TryGetValue(provinceCode, out var province) ? province : null; + } + + /// + /// 根据省份名称获取省份信息 + /// + public static ProvinceInfo? GetProvinceByName(string? name) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + + foreach (var province in Provinces.Values) + { + if (province.Name == name || province.ShortName == name || + province.Name.Contains(name) || name.Contains(province.ShortName)) + { + return province; + } + } + + return null; + } + + /// + /// 根据城市代码获取城市信息 + /// + public static CityInfo? GetCityByCode(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length < 4) + return null; + + var provinceCode = code.Substring(0, 2) + "0000"; + if (!Provinces.TryGetValue(provinceCode, out var province)) + return null; + + foreach (var city in province.Cities) + { + if (city.Code == code) + return city; + } + + return null; + } + + /// + /// 根据城市名称获取城市信息 + /// + public static CityInfo? GetCityByName(string? name, string? provinceName = null) + { + if (string.IsNullOrWhiteSpace(name)) + return null; + + foreach (var province in Provinces.Values) + { + if (!string.IsNullOrEmpty(provinceName) && + province.Name != provinceName && + province.ShortName != provinceName) + continue; + + foreach (var city in province.Cities) + { + if (city.Name == name || city.Name.Contains(name) || name.Contains(city.Name.Replace("市", ""))) + { + return city; + } + } + } + + return null; + } + + /// + /// 获取所有省份 + /// + public static IEnumerable GetAllProvinces() + { + return Provinces.Values; + } + + /// + /// 获取省份下的所有城市 + /// + public static IEnumerable GetCitiesByProvinceCode(string? provinceCode) + { + var province = GetProvinceByCode(provinceCode); + return province?.Cities ?? Enumerable.Empty(); + } + + /// + /// 验证行政区划代码是否有效 + /// + public static bool IsValidCode(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + return false; + + code = code.Trim(); + if (code.Length != 6) + return false; + + foreach (var c in code) + { + if (!char.IsDigit(c)) + return false; + } + + var provinceCode = code.Substring(0, 2) + "0000"; + return Provinces.ContainsKey(provinceCode); + } + + /// + /// 根据身份证号前6位获取籍贯 + /// + public static string? GetNativePlace(string? idCardPrefix) + { + if (string.IsNullOrWhiteSpace(idCardPrefix) || idCardPrefix.Length < 6) + return null; + + var provinceCode = idCardPrefix.Substring(0, 2) + "0000"; + if (!Provinces.TryGetValue(provinceCode, out var province)) + return null; + + var cityCode = idCardPrefix.Substring(0, 4) + "00"; + foreach (var city in province.Cities) + { + if (city.Code == cityCode) + { + return $"{province.Name}{city.Name}"; + } + } + + return province.Name; + } + } + + #region 数据类 + + /// + /// 省份信息 + /// + public class ProvinceInfo + { + /// + /// 省份代码(6位) + /// + public string Code { get; set; } = string.Empty; + + /// + /// 省份名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 简称 + /// + public string ShortName { get; set; } = string.Empty; + + /// + /// 城市列表 + /// + public List Cities { get; set; } = new(); + + public override string ToString() => Name; + } + + /// + /// 城市信息 + /// + public class CityInfo + { + /// + /// 城市代码(6位) + /// + public string Code { get; set; } = string.Empty; + + /// + /// 城市名称 + /// + public string Name { get; set; } = string.Empty; + + public override string ToString() => Name; + } + + #endregion +} diff --git a/EasyTool.Core/BusinessCategory/QQUtil.cs b/EasyTool.Core/BusinessCategory/QQUtil.cs new file mode 100644 index 0000000..1357b92 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/QQUtil.cs @@ -0,0 +1,179 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// QQ号工具类 + /// + public static class QQUtil + { + #region 常量与私有字段 + + /// + /// QQ号正则表达式(5-11位数字,不以0开头) + /// + private static readonly Regex QQRegex = new( + @"^[1-9]\d{4,10}$", + RegexOptions.Compiled); + + /// + /// QQ邮箱正则表达式 + /// + private static readonly Regex QQEmailRegex = new( + @"^[1-9]\d{4,10}@qq\.com$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + #endregion + + #region 验证方法 + + /// + /// 验证QQ号是否有效 + /// + /// QQ号 + /// 是否有效 + public static bool IsValid(string? qq) + { + if (string.IsNullOrWhiteSpace(qq)) + { + return false; + } + + return QQRegex.IsMatch(qq); + } + + /// + /// 验证QQ邮箱是否有效 + /// + /// QQ邮箱 + /// 是否有效 + public static bool IsValidQQEmail(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + return false; + } + + return QQEmailRegex.IsMatch(email); + } + + /// + /// 验证QQ号格式(仅检查格式,不验证是否存在) + /// + /// QQ号 + /// 格式是否正确 + public static bool IsValidFormat(string? qq) + { + return IsValid(qq); + } + + #endregion + + #region 转换方法 + + /// + /// 从QQ邮箱提取QQ号 + /// + /// QQ邮箱 + /// QQ号,提取失败返回null + public static string? ExtractFromEmail(string? email) + { + if (!IsValidQQEmail(email)) + { + return null; + } + + int atIndex = email!.IndexOf('@'); + return email.Substring(0, atIndex); + } + + /// + /// 将QQ号转换为QQ邮箱 + /// + /// QQ号 + /// QQ邮箱 + public static string? ToEmail(string? qq) + { + if (!IsValid(qq)) + { + return null; + } + + return qq + "@qq.com"; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化QQ号(去除非数字字符) + /// + /// QQ号 + /// 格式化后的QQ号 + public static string? Normalize(string? qq) + { + if (string.IsNullOrWhiteSpace(qq)) + { + return null; + } + + string cleaned = Regex.Replace(qq, @"\D", ""); + return IsValid(cleaned) ? cleaned : null; + } + + /// + /// QQ号脱敏:123****890 + /// + /// QQ号 + /// 脱敏后的QQ号 + public static string? Mask(string? qq) + { + if (!IsValid(qq)) + { + return null; + } + + string code = qq!; + if (code.Length <= 4) + { + return code[0] + new string('*', code.Length - 1); + } + + // 保留前3位和后3位 + int prefixLen = 3; + int suffixLen = 3; + int maskLen = code.Length - prefixLen - suffixLen; + + return code.Substring(0, prefixLen) + new string('*', maskLen) + code.Substring(code.Length - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机QQ号(仅供测试使用) + /// + /// 随机QQ号 + public static string GenerateRandom() + { + // QQ号长度5-11位 + int length = MathCategory.RandomUtil.RandomInt(5, 12); + + // 第一位不能为0 + string result = MathCategory.RandomUtil.RandomInt(1, 10).ToString(); + + // 剩余位数 + for (int i = 1; i < length; i++) + { + result += MathCategory.RandomUtil.RandomInt(0, 10).ToString(); + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs b/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs new file mode 100644 index 0000000..c033fbd --- /dev/null +++ b/EasyTool.Core/BusinessCategory/SocialSecurityUtil.cs @@ -0,0 +1,293 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 社保号工具类 + /// + public static class SocialSecurityUtil + { + #region 常量与私有字段 + + /// + /// 社保号正则表达式(18位,与身份证号格式相同) + /// + private static readonly Regex SSN18Regex = new( + @"^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$", + RegexOptions.Compiled); + + /// + /// 社保号正则表达式(部分省市为15位或16位) + /// + private static readonly Regex SSN15Regex = new(@"^\d{15,16}$", RegexOptions.Compiled); + + /// + /// 社会保障卡号正则(带字母) + /// + private static readonly Regex SSNCardRegex = new( + @"^[A-Za-z]\d{17}$", + RegexOptions.Compiled); + + /// + /// 校验码权重 + /// + private static readonly int[] Weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + + /// + /// 校验码对照表 + /// + private static readonly char[] CheckCodes = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; + + /// + /// 省份社保号规则(简化版) + /// + private static readonly Dictionary ProvinceLengthMap = new() + { + { "北京", 18 }, { "上海", 18 }, { "天津", 18 }, { "重庆", 18 }, + { "广东", 18 }, { "浙江", 18 }, { "江苏", 18 }, { "山东", 18 }, + { "四川", 18 }, { "湖北", 18 }, { "河南", 18 }, { "河北", 18 }, + { "福建", 18 }, { "安徽", 18 }, { "辽宁", 18 }, { "陕西", 18 }, + { "湖南", 18 }, { "江西", 18 }, { "云南", 18 }, { "贵州", 18 }, + { "甘肃", 18 }, { "青海", 18 }, { "宁夏", 18 }, { "新疆", 18 }, + { "西藏", 18 }, { "内蒙古", 18 }, { "广西", 18 }, { "黑龙江", 18 }, + { "吉林", 18 }, { "山西", 18 }, { "海南", 18 } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证社保号是否有效 + /// + /// 社保号 + /// 是否有效 + public static bool IsValid(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + + // 18位(身份证号格式) + if (cleaned.Length == 18 && SSN18Regex.IsMatch(cleaned)) + { + return ValidateCheckDigit(cleaned); + } + + // 15-16位纯数字 + if (SSN15Regex.IsMatch(cleaned)) + { + return true; + } + + // 带字母的卡号 + if (SSNCardRegex.IsMatch(cleaned)) + { + return true; + } + + return false; + } + + /// + /// 验证是否为18位社保号(身份证号格式) + /// + /// 社保号 + /// 是否为18位 + public static bool Is18Digit(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + return cleaned.Length == 18 && SSN18Regex.IsMatch(cleaned) && ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 社保号 + /// 格式是否正确 + public static bool IsValidFormat(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return false; + } + + string cleaned = ssn.Trim().ToUpper(); + return SSN18Regex.IsMatch(cleaned) || SSN15Regex.IsMatch(cleaned) || SSNCardRegex.IsMatch(cleaned); + } + + /// + /// 验证校验位 + /// + private static bool ValidateCheckDigit(string ssn) + { + if (ssn.Length != 18) return false; + + int sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (ssn[i] - '0') * Weights[i]; + } + + char expectedCheckCode = CheckCodes[sum % 11]; + return char.ToUpper(ssn[17]) == expectedCheckCode; + } + + #endregion + + #region 信息提取 + + /// + /// 获取出生日期(仅18位格式) + /// + /// 社保号 + /// 出生日期 + public static DateTime? GetBirthday(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + string cleaned = ssn!.Trim(); + int year = int.Parse(cleaned.Substring(6, 4)); + int month = int.Parse(cleaned.Substring(10, 2)); + int day = int.Parse(cleaned.Substring(12, 2)); + + return new DateTime(year, month, day); + } + + /// + /// 获取性别(仅18位格式) + /// + /// 社保号 + /// 性别(1男2女) + public static int? GetGender(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + int genderDigit = ssn![16] - '0'; + return genderDigit % 2 == 1 ? 1 : 2; + } + + /// + /// 获取性别字符串 + /// + /// 社保号 + /// 性别 + public static string? GetGenderString(string? ssn) + { + int? gender = GetGender(ssn); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取行政区划代码(仅18位格式) + /// + /// 社保号 + /// 行政区划代码 + public static string? GetAreaCode(string? ssn) + { + if (!Is18Digit(ssn)) + { + return null; + } + + return ssn!.Substring(0, 6); + } + + /// + /// 获取年龄(仅18位格式) + /// + /// 社保号 + /// 年龄 + public static int? GetAge(string? ssn) + { + DateTime? birthday = GetBirthday(ssn); + if (!birthday.HasValue) + { + return null; + } + + DateTime today = DateTime.Today; + int age = today.Year - birthday.Value.Year; + if (today < birthday.Value.AddYears(age)) + { + age--; + } + + return age; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化社保号 + /// + /// 社保号 + /// 格式化后的社保号 + public static string? Normalize(string? ssn) + { + if (string.IsNullOrWhiteSpace(ssn)) + { + return null; + } + + string cleaned = ssn.Trim().ToUpper(); + return IsValidFormat(cleaned) ? cleaned : null; + } + + /// + /// 社保号脱敏:110***********1234 + /// + /// 社保号 + /// 脱敏后的社保号 + public static string? Mask(string? ssn) + { + if (!IsValid(ssn)) + { + return null; + } + + string cleaned = ssn!.Trim().ToUpper(); + + if (cleaned.Length == 18) + { + return cleaned.Substring(0, 3) + "***********" + cleaned.Substring(14); + } + + if (cleaned.Length >= 15) + { + int prefixLen = 3; + int suffixLen = 4; + return cleaned.Substring(0, prefixLen) + + new string('*', cleaned.Length - prefixLen - suffixLen) + + cleaned.Substring(cleaned.Length - suffixLen); + } + + return cleaned[0] + new string('*', cleaned.Length - 2) + cleaned[^1]; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/StockCodeUtil.cs b/EasyTool.Core/BusinessCategory/StockCodeUtil.cs new file mode 100644 index 0000000..a2d7122 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/StockCodeUtil.cs @@ -0,0 +1,529 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 股票市场枚举 + /// + public enum StockMarket + { + /// + /// 未知 + /// + Unknown = 0, + + /// + /// 上海证券交易所 + /// + SHSE = 1, + + /// + /// 深圳证券交易所 + /// + SZSE = 2, + + /// + /// 北京证券交易所 + /// + BSE = 3, + + /// + /// 香港交易所 + /// + HKEX = 4, + + /// + /// 纽约证券交易所 + /// + NYSE = 5, + + /// + /// 纳斯达克 + /// + NASDAQ = 6 + } + + /// + /// 股票类型枚举 + /// + public enum StockType + { + /// + /// 未知 + /// + Unknown = 0, + + /// + /// A股 + /// + AShare = 1, + + /// + /// B股 + /// + BShare = 2, + + /// + /// 创业板 + /// + ChiNext = 3, + + /// + /// 科创板 + /// + STAR = 4, + + /// + /// 北交所 + /// + BSEShare = 5, + + /// + /// 港股 + /// + HKStock = 6, + + /// + /// 美股 + /// + USStock = 7 + } + + /// + /// 股票代码工具类 + /// + public static class StockCodeUtil + { + #region 常量与私有字段 + + /// + /// A股代码正则表达式(6位数字) + /// + private static readonly Regex AShareRegex = new(@"^[036]\d{5}$", RegexOptions.Compiled); + + /// + /// B股代码正则表达式 + /// + private static readonly Regex BShareRegex = new(@"^[29]\d{5}$", RegexOptions.Compiled); + + /// + /// 创业板代码正则表达式(30开头) + /// + private static readonly Regex ChiNextRegex = new(@"^30\d{4}$", RegexOptions.Compiled); + + /// + /// 科创板代码正则表达式(688开头) + /// + private static readonly Regex STARRegex = new(@"^688\d{3}$", RegexOptions.Compiled); + + /// + /// 北交所代码正则表达式(8开头,4位或6位) + /// + private static readonly Regex BSERegex = new(@"^(8[34]\d{4}|4[38]\d{4})$", RegexOptions.Compiled); + + /// + /// 港股代码正则表达式(1-5位数字) + /// + private static readonly Regex HKStockRegex = new(@"^\d{4,5}$", RegexOptions.Compiled); + + /// + /// 美股代码正则表达式(1-5位大写字母) + /// + private static readonly Regex USStockRegex = new(@"^[A-Z]{1,5}$", RegexOptions.Compiled); + + /// + /// 常见A股股票代码映射(部分示例) + /// + private static readonly Dictionary StockCodeMap = new() + { + // 上证A股 + { "600000", ("浦发银行", "上海") }, { "600036", ("招商银行", "上海") }, + { "600519", ("贵州茅台", "上海") }, { "600887", ("伊利股份", "上海") }, + { "601318", ("中国平安", "上海") }, { "601398", ("工商银行", "上海") }, + { "601939", ("建设银行", "上海") }, { "601988", ("中国银行", "上海") }, + { "601288", ("农业银行", "上海") }, { "601857", ("中国石油", "上海") }, + { "601668", ("中国建筑", "上海") }, { "600276", ("恒瑞医药", "上海") }, + { "600309", ("万华化学", "上海") }, { "600900", ("长江电力", "上海") }, + { "601012", ("隆基绿能", "上海") }, { "603259", ("药明康德", "上海") }, + + // 深证A股 + { "000001", ("平安银行", "深圳") }, { "000002", ("万科A", "深圳") }, + { "000333", ("美的集团", "深圳") }, { "000651", ("格力电器", "深圳") }, + { "000858", ("五粮液", "深圳") }, { "002594", ("比亚迪", "深圳") }, + { "000063", ("中兴通讯", "深圳") }, { "002475", ("立讯精密", "深圳") }, + { "002415", ("海康威视", "深圳") }, { "002352", ("顺丰控股", "深圳") }, + { "000568", ("泸州老窖", "深圳") }, { "002714", ("牧原股份", "深圳") }, + + // 创业板 + { "300750", ("宁德时代", "深圳") }, { "300059", ("东方财富", "深圳") }, + { "300015", ("爱尔眼科", "深圳") }, { "300347", ("泰格医药", "深圳") }, + { "300760", ("迈瑞医疗", "深圳") }, { "300124", ("汇川技术", "深圳") }, + + // 科创板 + { "688981", ("中芯国际", "上海") }, { "688111", ("金山办公", "上海") }, + { "688012", ("中微公司", "上海") }, { "688256", ("寒武纪", "上海") }, + + // 港股 + { "00700", ("腾讯控股", "香港") }, { "09988", ("阿里巴巴-SW", "香港") }, + { "03690", ("美团-W", "香港") }, { "09999", ("网易-S", "香港") }, + { "01024", ("快手-W", "香港") }, { "01810", ("小米集团-W", "香港") }, + { "09618", ("京东集团-SW", "香港") }, { "02318", ("中国平安", "香港") }, + { "00005", ("汇丰控股", "香港") }, { "00941", ("中国移动", "香港") }, + { "03988", ("中国银行", "香港") }, { "01398", ("工商银行", "香港") }, + + // 美股 + { "AAPL", ("苹果", "纳斯达克") }, { "MSFT", ("微软", "纳斯达克") }, + { "GOOGL", ("谷歌", "纳斯达克") }, { "AMZN", ("亚马逊", "纳斯达克") }, + { "META", ("Meta", "纳斯达克") }, { "NVDA", ("英伟达", "纳斯达克") }, + { "TSLA", ("特斯拉", "纳斯达克") }, { "NFLX", ("奈飞", "纳斯达克") }, + { "BABA", ("阿里巴巴", "纽约") }, { "JD", ("京东", "纳斯达克") }, + { "PDD", ("拼多多", "纳斯达克") }, { "BIDU", ("百度", "纳斯达克") } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证股票代码是否有效(支持A股、港股、美股) + /// + /// 股票代码 + /// 市场类型(可选,默认自动识别) + /// 是否有效 + public static bool IsValid(string? code, StockMarket? market = null) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + if (market.HasValue) + { + return market.Value switch + { + StockMarket.SHSE or StockMarket.SZSE => IsValidAShare(code), + StockMarket.BSE => IsValidBSE(code), + StockMarket.HKEX => IsValidHKStock(code), + StockMarket.NYSE or StockMarket.NASDAQ => IsValidUSStock(code), + _ => false + }; + } + + return IsValidAShare(code) || IsValidBSE(code) || IsValidHKStock(code) || IsValidUSStock(code); + } + + /// + /// 验证A股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidAShare(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length != 6) + { + return false; + } + + // A股(60、00、30、688开头)和B股(20、900开头) + return AShareRegex.IsMatch(code) || BShareRegex.IsMatch(code) || + ChiNextRegex.IsMatch(code) || STARRegex.IsMatch(code); + } + + /// + /// 验证北交所代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidBSE(string? code) + { + if (string.IsNullOrWhiteSpace(code) || code.Length != 6) + { + return false; + } + + return BSERegex.IsMatch(code); + } + + /// + /// 验证港股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidHKStock(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + return HKStockRegex.IsMatch(code); + } + + /// + /// 验证美股代码是否有效 + /// + /// 股票代码 + /// 是否有效 + public static bool IsValidUSStock(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return false; + } + + return USStockRegex.IsMatch(code.ToUpper()); + } + + #endregion + + #region 市场识别 + + /// + /// 获取股票市场 + /// + /// 股票代码 + /// 股票市场 + public static StockMarket GetMarket(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return StockMarket.Unknown; + } + + string upper = code.ToUpper(); + + // 美股(字母代码) + if (USStockRegex.IsMatch(upper)) + { + return StockMarket.NASDAQ; // 简化处理 + } + + // 港股(4-5位数字) + if (HKStockRegex.IsMatch(code)) + { + return StockMarket.HKEX; + } + + // A股(6位数字) + if (code.Length == 6) + { + if (code.StartsWith("60") || code.StartsWith("68")) + { + return StockMarket.SHSE; + } + if (code.StartsWith("00") || code.StartsWith("30")) + { + return StockMarket.SZSE; + } + if (code.StartsWith("83") || code.StartsWith("87") || code.StartsWith("43") || code.StartsWith("83")) + { + return StockMarket.BSE; + } + // B股 + if (code.StartsWith("900")) + { + return StockMarket.SHSE; + } + if (code.StartsWith("200")) + { + return StockMarket.SZSE; + } + } + + return StockMarket.Unknown; + } + + /// + /// 获取股票市场名称 + /// + /// 股票市场 + /// 市场名称 + public static string GetMarketName(StockMarket market) + { + return market switch + { + StockMarket.SHSE => "上海证券交易所", + StockMarket.SZSE => "深圳证券交易所", + StockMarket.BSE => "北京证券交易所", + StockMarket.HKEX => "香港交易所", + StockMarket.NYSE => "纽约证券交易所", + StockMarket.NASDAQ => "纳斯达克", + _ => "未知" + }; + } + + #endregion + + #region 类型识别 + + /// + /// 获取股票类型 + /// + /// 股票代码 + /// 股票类型 + public static StockType GetStockType(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return StockType.Unknown; + } + + string upper = code.ToUpper(); + + // 美股 + if (USStockRegex.IsMatch(upper)) + { + return StockType.USStock; + } + + // 港股 + if (HKStockRegex.IsMatch(code)) + { + return StockType.HKStock; + } + + // A股细分 + if (code.Length == 6) + { + if (STARRegex.IsMatch(code)) return StockType.STAR; + if (ChiNextRegex.IsMatch(code)) return StockType.ChiNext; + if (BSERegex.IsMatch(code)) return StockType.BSEShare; + if (code.StartsWith("60") || code.StartsWith("00")) return StockType.AShare; + if (code.StartsWith("900") || code.StartsWith("200")) return StockType.BShare; + } + + return StockType.Unknown; + } + + /// + /// 获取股票类型名称 + /// + /// 股票类型 + /// 类型名称 + public static string GetStockTypeName(StockType type) + { + return type switch + { + StockType.AShare => "A股", + StockType.BShare => "B股", + StockType.ChiNext => "创业板", + StockType.STAR => "科创板", + StockType.BSEShare => "北交所", + StockType.HKStock => "港股", + StockType.USStock => "美股", + _ => "未知" + }; + } + + #endregion + + #region 信息查询 + + /// + /// 获取股票名称 + /// + /// 股票代码 + /// 股票名称 + public static string? GetName(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + string key = code.ToUpper().PadLeft(6, '0'); + if (StockCodeMap.TryGetValue(key, out var info)) + { + return info.Name; + } + + // 尝试原始格式 + if (StockCodeMap.TryGetValue(code.ToUpper(), out info)) + { + return info.Name; + } + + return null; + } + + /// + /// 获取完整股票代码(带市场前缀) + /// + /// 股票代码 + /// 完整代码(如sh600519、sz000001、hk00700) + public static string? GetFullCode(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + StockMarket market = GetMarket(code); + return market switch + { + StockMarket.SHSE => "sh" + code, + StockMarket.SZSE => "sz" + code, + StockMarket.BSE => "bj" + code, + StockMarket.HKEX => "hk" + code.PadLeft(5, '0'), + StockMarket.NYSE => "nyse:" + code.ToUpper(), + StockMarket.NASDAQ => "nasdaq:" + code.ToUpper(), + _ => null + }; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化股票代码 + /// + /// 股票代码 + /// 格式化后的代码 + public static string? Normalize(string? code) + { + if (string.IsNullOrWhiteSpace(code)) + { + return null; + } + + // 去除市场前缀 + string cleaned = code.ToLower() + .Replace("sh", "").Replace("sz", "").Replace("bj", "") + .Replace("hk", "").Replace("nyse:", "").Replace("nasdaq:", ""); + + // 港股补零 + if (HKStockRegex.IsMatch(cleaned) && cleaned.Length < 5) + { + cleaned = cleaned.PadLeft(5, '0'); + } + + return IsValid(cleaned) ? cleaned.ToUpper() : null; + } + + /// + /// 股票代码脱敏:60****9 + /// + /// 股票代码 + /// 脱敏后的代码 + public static string? Mask(string? code) + { + string? normalized = Normalize(code); + if (normalized == null) + { + return null; + } + + if (normalized.Length <= 2) + { + return normalized[0] + "*"; + } + + return normalized[0] + new string('*', normalized.Length - 2) + normalized[^1]; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs b/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs new file mode 100644 index 0000000..3709510 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/SwiftCodeUtil.cs @@ -0,0 +1,438 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// SWIFT银行代码工具类 + /// + public static class SwiftCodeUtil + { + #region 常量与私有字段 + + /// + /// SWIFT代码正则表达式(8位或11位) + /// + private static readonly Regex SwiftRegex = new( + @"^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 中国主要银行SWIFT代码映射 + /// + private static readonly Dictionary ChinaBankSwiftMap = new() + { + // 工商银行 + { "ICBKCNBJ", ("中国工商银行", "北京") }, + { "ICBKCNBJBJN", ("中国工商银行", "济南") }, + { "ICBKCNBJCQX", ("中国工商银行", "重庆") }, + { "ICBKCNBJSHI", ("中国工商银行", "上海") }, + { "ICBKCNBJSZN", ("中国工商银行", "深圳") }, + { "ICBKCNBJGZU", ("中国工商银行", "广州") }, + { "ICBKCNBJNJA", ("中国工商银行", "南京") }, + { "ICBKCNBJHBR", ("中国工商银行", "哈尔滨") }, + { "ICBKCNBJTJN", ("中国工商银行", "天津") }, + { "ICBKCNBJCDU", ("中国工商银行", "成都") }, + { "ICBKCNBJWUH", ("中国工商银行", "武汉") }, + { "ICBKCNBJHAN", ("中国工商银行", "杭州") }, + { "ICBKCNBJXIM", ("中国工商银行", "厦门") }, + { "ICBKCNBJDLC", ("中国工商银行", "大连") }, + { "ICBKCNBJSYN", ("中国工商银行", "沈阳") }, + { "ICBKCNBJJIX", ("中国工商银行", "吉林") }, + { "ICBKCNBJSWA", ("中国工商银行", "汕头") }, + { "ICBKCNBJZHO", ("中国工商银行", "珠海") }, + { "ICBKCNBJFZH", ("中国工商银行", "福州") }, + { "ICBKCNBJKUN", ("中国工商银行", "昆明") }, + + // 农业银行 + { "ABOCCNBJ", ("中国农业银行", "北京") }, + { "ABOCCNBJ070", ("中国农业银行", "哈尔滨") }, + { "ABOCCNBJ080", ("中国农业银行", "上海") }, + { "ABOCCNBJ100", ("中国农业银行", "广州") }, + { "ABOCCNBJ110", ("中国农业银行", "深圳") }, + { "ABOCCNBJ120", ("中国农业银行", "天津") }, + { "ABOCCNBJ130", ("中国农业银行", "重庆") }, + { "ABOCCNBJ140", ("中国农业银行", "南京") }, + { "ABOCCNBJ150", ("中国农业银行", "成都") }, + { "ABOCCNBJ160", ("中国农业银行", "武汉") }, + { "ABOCCNBJ170", ("中国农业银行", "杭州") }, + { "ABOCCNBJ180", ("中国农业银行", "济南") }, + { "ABOCCNBJ190", ("中国农业银行", "西安") }, + { "ABOCCNBJ200", ("中国农业银行", "沈阳") }, + + // 中国银行 + { "BKCHCNBJ", ("中国银行", "北京") }, + { "BKCHCNBJ300", ("中国银行", "上海") }, + { "BKCHCNBJ400", ("中国银行", "广州") }, + { "BKCHCNBJ500", ("中国银行", "深圳") }, + { "BKCHCNBJ600", ("中国银行", "天津") }, + { "BKCHCNBJ700", ("中国银行", "重庆") }, + { "BKCHCNBJ800", ("中国银行", "南京") }, + { "BKCHCNBJ900", ("中国银行", "成都") }, + { "BKCHCNBJ910", ("中国银行", "武汉") }, + { "BKCHCNBJ920", ("中国银行", "杭州") }, + { "BKCHCNBJ930", ("中国银行", "济南") }, + { "BKCHCNBJ940", ("中国银行", "西安") }, + { "BKCHCNBJ950", ("中国银行", "沈阳") }, + { "BKCHCNBJ960", ("中国银行", "大连") }, + { "BKCHCNBJ970", ("中国银行", "青岛") }, + { "BKCHCNBJ980", ("中国银行", "厦门") }, + { "BKCHCNBJ990", ("中国银行", "福州") }, + + // 建设银行 + { "PCBCCNBJ", ("中国建设银行", "北京") }, + { "PCBCCNBJBJX", ("中国建设银行", "北京") }, + { "PCBCCNBJSHX", ("中国建设银行", "上海") }, + { "PCBCCNBJGZX", ("中国建设银行", "广州") }, + { "PCBCCNBJSZX", ("中国建设银行", "深圳") }, + { "PCBCCNBJTJX", ("中国建设银行", "天津") }, + { "PCBCCNBJCQX", ("中国建设银行", "重庆") }, + { "PCBCCNBJNJX", ("中国建设银行", "南京") }, + { "PCBCCNBJCDX", ("中国建设银行", "成都") }, + { "PCBCCNBJWHX", ("中国建设银行", "武汉") }, + { "PCBCCNBJHZX", ("中国建设银行", "杭州") }, + { "PCBCCNBJJNX", ("中国建设银行", "济南") }, + { "PCBCCNBJXAX", ("中国建设银行", "西安") }, + { "PCBCCNBJSYX", ("中国建设银行", "沈阳") }, + { "PCBCCNBJDLX", ("中国建设银行", "大连") }, + { "PCBCCNBJQDX", ("中国建设银行", "青岛") }, + + // 交通银行 + { "COMMCNSh", ("交通银行", "上海") }, + { "COMMCNShKUN", ("交通银行", "昆明") }, + { "COMMCNShGZH", ("交通银行", "广州") }, + + // 招商银行 + { "CMBCCNBS", ("招商银行", "上海") }, + { "CMBCCNBS001", ("招商银行", "上海") }, + { "CMBCCNBS002", ("招商银行", "北京") }, + { "CMBCCNBS003", ("招商银行", "深圳") }, + { "CMBCCNBS004", ("招商银行", "广州") }, + + // 中信银行 + { "CIBKCNBJ", ("中信银行", "北京") }, + { "CIBKCNBJSHI", ("中信银行", "上海") }, + { "CIBKCNBJGZU", ("中信银行", "广州") }, + { "CIBKCNBJSZN", ("中信银行", "深圳") }, + + // 浦发银行 + { "SPDBCNSH", ("浦发银行", "上海") }, + { "SPDBCNSHBJG", ("浦发银行", "北京") }, + { "SPDBCNSHGXG", ("浦发银行", "广州") }, + { "SPDBCNSHSZN", ("浦发银行", "深圳") }, + + // 民生银行 + { "MSBCCNBJ", ("民生银行", "北京") }, + { "MSBCCNBJ001", ("民生银行", "上海") }, + { "MSBCCNBJ002", ("民生银行", "广州") }, + + // 光大银行 + { "EVERCNBJ", ("光大银行", "北京") }, + { "EVERCNBJ1BJ", ("光大银行", "北京") }, + { "EVERCNBJ1SH", ("光大银行", "上海") }, + + // 华夏银行 + { "HXBKCNBJ", ("华夏银行", "北京") }, + { "HXBKCNBJ070", ("华夏银行", "上海") }, + + // 兴业银行 + { "FJIBCNBA", ("兴业银行", "福州") }, + { "FJIBCNBA001", ("兴业银行", "北京") }, + { "FJIBCNBA002", ("兴业银行", "上海") }, + + // 平安银行 + { "SZDBCNBS", ("平安银行", "深圳") }, + { "SZDBCNBS001", ("平安银行", "北京") }, + { "SZDBCNBS002", ("平安银行", "上海") }, + + // 广发银行 + { "GDBKCN22", ("广发银行", "广州") }, + { "GDBKCN22001", ("广发银行", "北京") }, + { "GDBKCN22002", ("广发银行", "上海") }, + + // 邮储银行 + { "PSBCCNBJ", ("邮储银行", "北京") }, + { "PSBCCNBJ001", ("邮储银行", "上海") }, + { "PSBCCNBJ002", ("邮储银行", "广州") }, + + // 汇丰银行(中国) + { "HSBCCNSH", ("汇丰银行(中国)", "上海") }, + { "HSBCCNSH001", ("汇丰银行(中国)", "北京") }, + { "HSBCCNSH002", ("汇丰银行(中国)", "广州") }, + + // 渣打银行(中国) + { "SCBLCNSX", ("渣打银行(中国)", "上海") }, + { "SCBLCNSX001", ("渣打银行(中国)", "北京") }, + + // 花旗银行(中国) + { "CITICNSX", ("花旗银行(中国)", "上海") }, + { "CITICNSX001", ("花旗银行(中国)", "北京") } + }; + + /// + /// 国家代码与名称映射(部分) + /// + private static readonly Dictionary CountryCodeMap = new() + { + { "CN", "中国" }, { "HK", "香港" }, { "TW", "台湾" }, { "JP", "日本" }, + { "KR", "韩国" }, { "SG", "新加坡" }, { "MY", "马来西亚" }, { "TH", "泰国" }, + { "AU", "澳大利亚" }, { "NZ", "新西兰" }, { "US", "美国" }, { "CA", "加拿大" }, + { "GB", "英国" }, { "DE", "德国" }, { "FR", "法国" }, { "IT", "意大利" }, + { "ES", "西班牙" }, { "NL", "荷兰" }, { "BE", "比利时" }, { "CH", "瑞士" }, + { "AT", "奥地利" }, { "SE", "瑞典" }, { "NO", "挪威" }, { "DK", "丹麦" }, + { "FI", "芬兰" }, { "RU", "俄罗斯" }, { "BR", "巴西" }, { "MX", "墨西哥" }, + { "AR", "阿根廷" }, { "ZA", "南非" }, { "AE", "阿联酋" }, { "SA", "沙特" }, + { "IN", "印度" }, { "PK", "巴基斯坦" }, { "ID", "印度尼西亚" }, { "PH", "菲律宾" }, + { "VN", "越南" }, { "MM", "缅甸" }, { "LU", "卢森堡" }, { "IE", "爱尔兰" }, + { "PT", "葡萄牙" }, { "GR", "希腊" }, { "PL", "波兰" }, { "CZ", "捷克" }, + { "HU", "匈牙利" }, { "TR", "土耳其" }, { "IL", "以色列" }, { "EG", "埃及" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证SWIFT代码是否有效 + /// + /// SWIFT代码 + /// 是否有效 + public static bool IsValid(string? swiftCode) + { + if (string.IsNullOrWhiteSpace(swiftCode)) + { + return false; + } + + return SwiftRegex.IsMatch(swiftCode); + } + + /// + /// 验证是否为8位SWIFT代码(不含分行代码) + /// + /// SWIFT代码 + /// 是否为8位 + public static bool Is8Digit(string? swiftCode) + { + return swiftCode?.Length == 8 && IsValid(swiftCode); + } + + /// + /// 验证是否为11位SWIFT代码(含分行代码) + /// + /// SWIFT代码 + /// 是否为11位 + public static bool Is11Digit(string? swiftCode) + { + return swiftCode?.Length == 11 && IsValid(swiftCode); + } + + #endregion + + #region 信息提取 + + /// + /// 获取银行代码(前4位) + /// + /// SWIFT代码 + /// 银行代码 + public static string? GetBankCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(0, 4).ToUpper(); + } + + /// + /// 获取国家代码(第5-6位) + /// + /// SWIFT代码 + /// 国家代码 + public static string? GetCountryCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(4, 2).ToUpper(); + } + + /// + /// 获取国家名称 + /// + /// SWIFT代码 + /// 国家名称 + public static string? GetCountryName(string? swiftCode) + { + string? countryCode = GetCountryCode(swiftCode); + if (countryCode == null) + { + return null; + } + + return CountryCodeMap.TryGetValue(countryCode, out string? name) ? name : null; + } + + /// + /// 获取位置代码(第7-8位) + /// + /// SWIFT代码 + /// 位置代码 + public static string? GetLocationCode(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(6, 2).ToUpper(); + } + + /// + /// 获取分行代码(第9-11位,11位代码才有) + /// + /// SWIFT代码 + /// 分行代码 + public static string? GetBranchCode(string? swiftCode) + { + if (!Is11Digit(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(8, 3).ToUpper(); + } + + /// + /// 判断是否为总行代码(第7-8位为XX或位置代码首位为0) + /// + /// SWIFT代码 + /// 是否为总行 + public static bool IsHeadOffice(string? swiftCode) + { + string? locationCode = GetLocationCode(swiftCode); + if (locationCode == null) + { + return false; + } + + // 位置代码为"XX"或首位为0表示总行 + return locationCode == "XX" || locationCode[0] == '0'; + } + + /// + /// 获取银行信息(仅限中国主要银行) + /// + /// SWIFT代码 + /// 银行和城市信息 + public static (string Bank, string City)? GetBankInfo(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + string upper = swiftCode!.ToUpper(); + + // 先尝试完整匹配 + if (ChinaBankSwiftMap.TryGetValue(upper, out var info)) + { + return info; + } + + // 再尝试8位匹配 + string code8 = upper.Substring(0, 8); + if (ChinaBankSwiftMap.TryGetValue(code8, out info)) + { + return info; + } + + return null; + } + + /// + /// 获取银行名称 + /// + /// SWIFT代码 + /// 银行名称 + public static string? GetBankName(string? swiftCode) + { + return GetBankInfo(swiftCode)?.Bank; + } + + /// + /// 获取城市名称 + /// + /// SWIFT代码 + /// 城市名称 + public static string? GetCityName(string? swiftCode) + { + return GetBankInfo(swiftCode)?.City; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化SWIFT代码(转大写) + /// + /// SWIFT代码 + /// 格式化后的SWIFT代码 + public static string? Normalize(string? swiftCode) + { + if (string.IsNullOrWhiteSpace(swiftCode)) + { + return null; + } + + string upper = swiftCode.ToUpper().Trim(); + return IsValid(upper) ? upper : null; + } + + /// + /// SWIFT代码脱敏:ICBK****BJ + /// + /// SWIFT代码 + /// 脱敏后的SWIFT代码 + public static string? Mask(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + string upper = swiftCode!.ToUpper(); + if (upper.Length == 8) + { + return upper.Substring(0, 4) + "****"; + } + else + { + return upper.Substring(0, 4) + "*******"; + } + } + + /// + /// 转换为8位SWIFT代码(去除分行代码) + /// + /// SWIFT代码 + /// 8位SWIFT代码 + public static string? To8Digit(string? swiftCode) + { + if (!IsValid(swiftCode)) + { + return null; + } + + return swiftCode!.Substring(0, 8).ToUpper(); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs b/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs new file mode 100644 index 0000000..4ffa424 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/TaxNumberUtil.cs @@ -0,0 +1,557 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 税号类型枚举 + /// + public enum TaxNumberType + { + /// + /// 未知类型 + /// + Unknown = 0, + + /// + /// 统一社会信用代码(18位) + /// + CreditCode = 1, + + /// + /// 旧税号(15位) + /// + OldTaxCode = 2, + + /// + /// 税务登记号(20位) + /// + TaxRegistration = 3 + } + + /// + /// 企业税号工具类 + /// + public static class TaxNumberUtil + { + #region 常量与私有字段 + + /// + /// 统一社会信用代码字符集(31个字符) + /// + private const string BaseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; + + /// + /// 统一社会信用代码权重 + /// + private static readonly int[] Weights = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 }; + + /// + /// 18位统一社会信用代码正则表达式 + /// + private static readonly Regex CreditCodeRegex = new Regex( + @"^[0-9A-HJ-NPQRTUWXY]{2}[0-9]{6}[0-9A-HJ-NPQRTUWXY]{10}$", + RegexOptions.Compiled); + + /// + /// 15位旧税号正则表达式(6位区域码 + 9位组织机构代码) + /// + private static readonly Regex OldTaxCodeRegex = new Regex( + @"^[0-9]{15}$", + RegexOptions.Compiled); + + /// + /// 20位税务登记号正则表达式 + /// + private static readonly Regex TaxRegistrationRegex = new Regex( + @"^[0-9]{20}$", + RegexOptions.Compiled); + + /// + /// 登记管理部门代码映射 + /// + private static readonly Dictionary DepartmentMap = new Dictionary + { + { "11", "机构编制" }, { "12", "外交" }, { "13", "教育" }, { "14", "公安" }, + { "15", "民政" }, { "16", "司法" }, { "17", "交通运输" }, { "18", "文化和旅游" }, + { "19", "市场监管" }, { "21", "农业" }, { "22", "林业和草原" }, { "23", "卫生健康" }, + { "24", "中医药" }, { "25", "退役军人" }, { "26", "应急管理" }, { "27", "国有资产" }, + { "28", "海关" }, { "29", "税务" }, { "31", "人民银行" }, { "32", "外汇" }, + { "33", "知识产权" }, { "34", "粮食和储备" }, { "35", "能源" }, { "36", "国防科工" }, + { "37", "烟草" }, { "41", "中央军委" }, { "51", "全国总工会" }, { "52", "全国妇联" }, + { "53", "全国工商联" }, { "54", "全国青联" }, { "55", "中国残联" }, + { "91", "工商" }, { "92", "中央及地方编办" }, { "93", "民政" }, { "99", "其他" } + }; + + /// + /// 机构类型代码映射(与登记管理部门组合使用) + /// + private static readonly Dictionary OrganizationTypeMap = new Dictionary + { + { '1', "企业" }, { '2', "个体工商户" }, { '3', "农民专业合作社" }, + { '4', "机关" }, { '5', "事业单位" }, { '6', "社会团体" }, + { '7', "民办非企业单位" }, { '8', "基金会" }, { '9', "其他" } + }; + + /// + /// 行业代码映射(GB/T 4754-2017 国民经济行业分类,部分常用) + /// + private static readonly Dictionary IndustryCodeMap = new Dictionary + { + { "01", "农业" }, { "02", "林业" }, { "03", "畜牧业" }, { "04", "渔业" }, + { "06", "煤炭开采和洗选业" }, { "07", "石油和天然气开采业" }, + { "08", "黑色金属矿采选业" }, { "09", "有色金属矿采选业" }, + { "10", "非金属矿采选业" }, { "13", "农副食品加工业" }, + { "14", "食品制造业" }, { "15", "酒、饮料和精制茶制造业" }, + { "17", "纺织业" }, { "18", "纺织服装、服饰业" }, + { "19", "皮革、毛皮、羽毛及其制品和制鞋业" }, { "20", "木材加工和木、竹、藤、棕、草制品业" }, + { "21", "家具制造业" }, { "22", "造纸和纸制品业" }, + { "23", "印刷和记录媒介复制业" }, { "24", "文教、工美、体育和娱乐用品制造业" }, + { "25", "石油、煤炭及其他燃料加工业" }, { "26", "化学原料和化学制品制造业" }, + { "27", "医药制造业" }, { "28", "化学纤维制造业" }, + { "29", "橡胶和塑料制品业" }, { "30", "非金属矿物制品业" }, + { "31", "黑色金属冶炼和压延加工业" }, { "32", "有色金属冶炼和压延加工业" }, + { "33", "金属制品业" }, { "34", "通用设备制造业" }, + { "35", "专用设备制造业" }, { "36", "汽车制造业" }, + { "37", "铁路、船舶、航空航天和其他运输设备制造业" }, + { "38", "电气机械和器材制造业" }, { "39", "计算机、通信和其他电子设备制造业" }, + { "40", "仪器仪表制造业" }, { "41", "其他制造业" }, + { "42", "废弃资源综合利用业" }, { "43", "金属制品、机械和设备修理业" }, + { "44", "电力、热力生产和供应业" }, { "45", "燃气生产和供应业" }, + { "46", "水的生产和供应业" }, { "47", "房屋建筑业" }, + { "48", "土木工程建筑业" }, { "49", "建筑安装业" }, + { "50", "建筑装饰、装修和其他建筑业" }, { "51", "批发业" }, + { "52", "零售业" }, { "53", "铁路运输业" }, + { "54", "道路运输业" }, { "55", "水上运输业" }, + { "56", "航空运输业" }, { "57", "管道运输业" }, + { "58", "多式联运和运输代理业" }, { "59", "装卸搬运和仓储业" }, + { "60", "邮政业" }, { "61", "住宿业" }, + { "62", "餐饮业" }, { "63", "电信、广播电视和卫星传输服务" }, + { "64", "互联网和相关服务" }, { "65", "软件和信息技术服务业" }, + { "66", "货币金融服务" }, { "67", "资本市场服务" }, + { "68", "保险业" }, { "69", "其他金融业" }, + { "70", "房地产业" }, { "71", "租赁业" }, + { "72", "商务服务业" }, { "73", "研究和试验发展" }, + { "74", "专业技术服务业" }, { "75", "科技推广和应用服务业" }, + { "76", "水利管理业" }, { "77", "生态保护和环境治理业" }, + { "78", "公共设施管理业" }, { "79", "居民服务业" }, + { "80", "机动车、电子产品和日用产品修理业" }, { "81", "其他服务业" }, + { "82", "教育" }, { "83", "卫生" }, + { "84", "社会工作" }, { "85", "新闻和出版业" }, + { "86", "广播、电视、电影和录音制作业" }, { "87", "文化艺术业" }, + { "88", "体育" }, { "89", "娱乐业" }, + { "90", "中国共产党机关" }, { "91", "国家机构" }, + { "92", "人民政协、民主党派" }, { "93", "社会保障" }, + { "94", "群众团体、社会团体和其他成员组织" }, { "95", "基层群众自治组织" }, + { "96", "国际组织" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证税号是否有效(支持15/18/20位) + /// + /// 税号 + /// 是否有效 + public static bool IsValid(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return false; + } + + return IsValid18(taxNumber) || IsValid15(taxNumber) || IsValid20(taxNumber); + } + + /// + /// 验证18位统一社会信用代码是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid18(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 18) + { + return false; + } + + string normalized = taxNumber.ToUpper(); + + // 验证格式 + if (!CreditCodeRegex.IsMatch(normalized)) + { + return false; + } + + // 验证校验码 + char? expectedCheckCode = CalculateCheckCode(normalized.Substring(0, 17)); + return expectedCheckCode.HasValue && expectedCheckCode.Value == normalized[17]; + } + + /// + /// 验证15位旧税号是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid15(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 15) + { + return false; + } + + return OldTaxCodeRegex.IsMatch(taxNumber); + } + + /// + /// 验证20位税务登记号是否有效 + /// + /// 税号 + /// 是否有效 + public static bool IsValid20(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber) || taxNumber.Length != 20) + { + return false; + } + + return TaxRegistrationRegex.IsMatch(taxNumber); + } + + /// + /// 判断是否为统一社会信用代码(18位) + /// + /// 税号 + /// 是否为统一社会信用代码 + public static bool IsCreditCode(string? taxNumber) + { + return IsValid18(taxNumber); + } + + /// + /// 计算统一社会信用代码校验码 + /// + /// 不含校验码的17位代码 + /// 校验码,计算失败返回null + public static char? CalculateCheckCode(string? codeWithoutCheck) + { + if (string.IsNullOrWhiteSpace(codeWithoutCheck) || codeWithoutCheck.Length != 17) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 17; i++) + { + int value = BaseCode.IndexOf(char.ToUpper(codeWithoutCheck[i])); + if (value < 0) + { + return null; + } + sum += value * Weights[i]; + } + + int checkValue = 31 - (sum % 31); + if (checkValue == 31) + { + checkValue = 0; + } + + return BaseCode[checkValue]; + } + + #endregion + + #region 类型识别 + + /// + /// 获取税号类型 + /// + /// 税号 + /// 税号类型 + public static TaxNumberType GetTaxNumberType(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return TaxNumberType.Unknown; + } + + if (IsValid18(taxNumber)) + { + return TaxNumberType.CreditCode; + } + + if (IsValid15(taxNumber)) + { + return TaxNumberType.OldTaxCode; + } + + if (IsValid20(taxNumber)) + { + return TaxNumberType.TaxRegistration; + } + + return TaxNumberType.Unknown; + } + + #endregion + + #region 信息提取 + + /// + /// 获取登记管理部门(仅18位统一社会信用代码) + /// + /// 税号 + /// 登记管理部门名称 + public static string? GetDepartment(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + string normalized = taxNumber!.ToUpper(); + string deptCode = normalized.Substring(0, 2); + + return DepartmentMap.TryGetValue(deptCode, out string? dept) ? dept : null; + } + + /// + /// 获取机构类型(仅18位统一社会信用代码) + /// + /// 税号 + /// 机构类型名称 + public static string? GetOrganizationType(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + string normalized = taxNumber!.ToUpper(); + char typeCode = normalized[2]; + + return OrganizationTypeMap.TryGetValue(typeCode, out string? type) ? type : null; + } + + /// + /// 获取行政区划代码(仅18位统一社会信用代码) + /// + /// 税号 + /// 行政区划代码 + public static string? GetAreaCode(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(3, 6); + } + + /// + /// 获取行业代码(仅18位统一社会信用代码) + /// + /// 税号 + /// 行业代码 + public static string? GetIndustryCode(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(9, 2); + } + + /// + /// 获取行业名称(仅18位统一社会信用代码) + /// + /// 税号 + /// 行业名称 + public static string? GetIndustryName(string? taxNumber) + { + string? code = GetIndustryCode(taxNumber); + if (code == null) + { + return null; + } + + return IndustryCodeMap.TryGetValue(code, out string? name) ? name : null; + } + + /// + /// 获取主体标识码(仅18位统一社会信用代码) + /// + /// 税号 + /// 主体标识码 + public static string? GetSubjectIdentifier(string? taxNumber) + { + if (!IsValid18(taxNumber)) + { + return null; + } + + return taxNumber!.Substring(11, 6); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化税号(转大写,去除特殊字符) + /// + /// 税号 + /// 格式化后的税号 + public static string? Normalize(string? taxNumber) + { + if (string.IsNullOrWhiteSpace(taxNumber)) + { + return null; + } + + // 去除空格和特殊字符,转大写 + return taxNumber.ToUpper().Trim(); + } + + /// + /// 税号脱敏:911010****001Q + /// + /// 税号 + /// 脱敏后的税号 + public static string? Mask(string? taxNumber) + { + string? normalized = Normalize(taxNumber); + if (normalized == null) + { + return null; + } + + if (normalized.Length == 18) + { + // 保留前5位 + 后3位 + return normalized.Substring(0, 5) + "**********" + normalized.Substring(15); + } + + if (normalized.Length == 15) + { + // 保留前4位 + 后3位 + return normalized.Substring(0, 4) + "********" + normalized.Substring(12); + } + + if (normalized.Length == 20) + { + // 保留前5位 + 后3位 + return normalized.Substring(0, 5) + "************" + normalized.Substring(17); + } + + return null; + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机统一社会信用代码(仅供测试使用) + /// + /// 登记管理部门代码(可选,默认91-工商) + /// 机构类型代码(可选,默认1-企业) + /// 行政区划代码(可选,默认110101-北京市东城区) + /// 18位统一社会信用代码 + public static string GenerateRandom( + string? departmentCode = null, + char? organizationType = null, + string? areaCode = null) + { + // 登记管理部门代码(2位) + string deptCode = departmentCode ?? "91"; + + // 机构类型(1位) + char orgType = organizationType ?? '1'; + + // 行政区划代码(6位) + string area = areaCode ?? "110101"; + + // 行业代码(2位) + string[] industries = { "51", "52", "63", "64", "65", "70", "72" }; + string industry = MathCategory.RandomUtil.GetRandomElement(industries); + + // 主体标识码(6位) + string subject = GenerateRandomCode(6); + + // 前17位 + string code17 = deptCode + orgType + area + industry + subject; + + // 计算校验码 + char? checkCode = CalculateCheckCode(code17); + if (!checkCode.HasValue) + { + throw new InvalidOperationException("Failed to calculate check code"); + } + + return code17 + checkCode.Value; + } + + /// + /// 将15位旧税号转换为18位统一社会信用代码 + /// 注意:这是一个近似转换,实际转换需要根据具体情况补充信息 + /// + /// 15位旧税号 + /// 机构类型代码(默认1-企业) + /// 18位统一社会信用代码,转换失败返回null + public static string? Convert15To18(string? taxNumber15, char organizationType = '1') + { + if (!IsValid15(taxNumber15)) + { + return null; + } + + // 15位旧税号结构:6位区域码 + 9位组织机构代码 + // 18位统一社会信用代码结构: + // - 登记管理部门(2位):默认91(工商) + // - 机构类型(1位) + // - 行政区划(6位):取旧税号前6位 + // - 主体标识码(9位):取旧税号后9位 + // - 校验码(1位) + + string areaCode = taxNumber15!.Substring(0, 6); + string subjectCode = taxNumber15.Substring(6, 9); + + // 前17位:91 + 机构类型 + 区域码 + 主体标识码 + string code17 = "91" + organizationType + areaCode + subjectCode; + + // 计算校验码 + char? checkCode = CalculateCheckCode(code17); + if (!checkCode.HasValue) + { + return null; + } + + return code17 + checkCode.Value; + } + + #endregion + + #region 私有方法 + + /// + /// 生成随机代码(使用BaseCode字符集) + /// + private static string GenerateRandomCode(int length) + { + string result = ""; + for (int i = 0; i < length; i++) + { + result += BaseCode[MathCategory.RandomUtil.RandomInt(0, BaseCode.Length)]; + } + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs b/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs new file mode 100644 index 0000000..44a17bd --- /dev/null +++ b/EasyTool.Core/BusinessCategory/TwIdCardUtil.cs @@ -0,0 +1,357 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 台湾身份证工具类 + /// + public static class TwIdCardUtil + { + #region 常量与私有字段 + + /// + /// 台湾身份证正则表达式 + /// 格式:1个英文字母(县市代码)+ 1位数字(性别)+ 7位数字 + 1位校验码 + /// 例如:A123456789 + /// + private static readonly Regex TwIdCardRegex = new( + @"^[A-Z]\d{9}$", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 首字母对应数值(台湾身份证特殊编码) + /// + private static readonly Dictionary LetterValues = new() + { + { 'A', (10, 0) }, { 'B', (11, 1) }, { 'C', (12, 2) }, { 'D', (13, 3) }, + { 'E', (14, 4) }, { 'F', (15, 5) }, { 'G', (16, 6) }, { 'H', (17, 7) }, + { 'I', (34, 4) }, { 'J', (18, 8) }, { 'K', (19, 9) }, { 'L', (20, 0) }, + { 'M', (21, 1) }, { 'N', (22, 2) }, { 'O', (35, 5) }, { 'P', (23, 3) }, + { 'Q', (24, 4) }, { 'R', (25, 5) }, { 'S', (26, 6) }, { 'T', (27, 7) }, + { 'U', (28, 8) }, { 'V', (29, 9) }, { 'W', (32, 2) }, { 'X', (30, 0) }, + { 'Y', (31, 1) }, { 'Z', (33, 3) } + }; + + /// + /// 县市代码与名称映射 + /// + private static readonly Dictionary CountyMap = new() + { + { 'A', "台北市" }, { 'B', "台中市" }, { 'C', "基隆市" }, { 'D', "台南市" }, + { 'E', "高雄市" }, { 'F', "台北县" }, { 'G', "宜兰县" }, { 'H', "桃园县" }, + { 'I', "嘉义市" }, { 'J', "新竹县" }, { 'K', "苗栗县" }, { 'L', "台中县" }, + { 'M', "南投县" }, { 'N', "彰化县" }, { 'O', "新竹市" }, { 'P', "云林县" }, + { 'Q', "嘉义县" }, { 'R', "台南县" }, { 'S', "高雄县" }, { 'T', "屏东县" }, + { 'U', "花莲县" }, { 'V', "台东县" }, { 'W', "金门县" }, { 'X', "澎湖县" }, + { 'Y', "阳明山" }, { 'Z', "连江县" } + }; + + #endregion + + #region 验证方法 + + /// + /// 验证台湾身份证是否有效 + /// + /// 台湾身份证号 + /// 是否有效 + public static bool IsValid(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + + // 检查格式 + if (!TwIdCardRegex.IsMatch(cleaned)) + { + return false; + } + + // 检查字母是否有效 + if (!LetterValues.ContainsKey(cleaned[0])) + { + return false; + } + + // 验证校验码 + return ValidateCheckDigit(cleaned); + } + + /// + /// 验证格式是否正确(不校验校验位) + /// + /// 台湾身份证号 + /// 格式是否正确 + public static bool IsValidFormat(string? idCard) + { + if (string.IsNullOrWhiteSpace(idCard)) + { + return false; + } + + string cleaned = idCard.ToUpper().Trim(); + return TwIdCardRegex.IsMatch(cleaned) && LetterValues.ContainsKey(cleaned[0]); + } + + /// + /// 验证校验码 + /// + private static bool ValidateCheckDigit(string idCard) + { + if (idCard.Length != 10) + { + return false; + } + + char letter = char.ToUpper(idCard[0]); + if (!LetterValues.TryGetValue(letter, out var values)) + { + return false; + } + + // 计算加权和 + int sum = values.Value1; + + // 第2-9位权重为9到1 + int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 }; + for (int i = 0; i < 8; i++) + { + sum += (idCard[i + 1] - '0') * weights[i]; + } + + // 计算校验码 + int remainder = sum % 10; + int expectedCheck = remainder == 0 ? 0 : 10 - remainder; + + int actualCheck = idCard[9] - '0'; + + return expectedCheck == actualCheck; + } + + #endregion + + #region 信息提取 + + /// + /// 获取县市名称 + /// + /// 台湾身份证号 + /// 县市名称 + public static string? GetCounty(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + char letter = char.ToUpper(idCard![0]); + return CountyMap.TryGetValue(letter, out string? county) ? county : null; + } + + /// + /// 获取县市代码(首字母) + /// + /// 台湾身份证号 + /// 县市代码 + public static char? GetCountyCode(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return char.ToUpper(idCard![0]); + } + + /// + /// 获取性别 + /// + /// 台湾身份证号 + /// 性别(1男2女) + public static int? GetGender(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + int genderDigit = idCard![1] - '0'; + // 1为男性,2为女性 + return genderDigit == 1 ? 1 : (genderDigit == 2 ? 2 : null); + } + + /// + /// 获取性别字符串 + /// + /// 台湾身份证号 + /// 性别 + public static string? GetGenderString(string? idCard) + { + int? gender = GetGender(idCard); + return gender switch + { + 1 => "男", + 2 => "女", + _ => null + }; + } + + /// + /// 获取数字部分(后9位) + /// + /// 台湾身份证号 + /// 数字部分 + public static string? GetDigitPart(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.Substring(1); + } + + /// + /// 获取校验码(最后一位) + /// + /// 台湾身份证号 + /// 校验码 + public static int? GetCheckDigit(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard![9] - '0'; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化台湾身份证(统一大写) + /// + /// 台湾身份证号 + /// 格式化后的身份证号 + public static string? Normalize(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + return idCard!.ToUpper().Trim(); + } + + /// + /// 台湾身份证脱敏:A123****89 + /// + /// 台湾身份证号 + /// 脱敏后的身份证号 + public static string? Mask(string? idCard) + { + if (!IsValidFormat(idCard)) + { + return null; + } + + string cleaned = idCard!.ToUpper().Trim(); + // 保留前4位和后2位 + return cleaned.Substring(0, 4) + "****" + cleaned.Substring(8); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机台湾身份证号(仅供测试使用) + /// + /// 县市代码(可选,默认随机) + /// 性别(1男2女,可选) + /// 台湾身份证号 + public static string GenerateRandom(char? countyCode = null, int? gender = null) + { + const string letters = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; + const string digits = "0123456789"; + + // 县市代码 + char letter; + if (countyCode.HasValue && LetterValues.ContainsKey(char.ToUpper(countyCode.Value))) + { + letter = char.ToUpper(countyCode.Value); + } + else + { + letter = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()); + } + + // 性别(第2位) + int genderDigit; + if (gender == 1) + { + genderDigit = 1; + } + else if (gender == 2) + { + genderDigit = 2; + } + else + { + genderDigit = MathCategory.RandomUtil.RandomInt(1, 3); + } + + // 第3-9位随机数字 + string middleDigits = ""; + for (int i = 0; i < 7; i++) + { + middleDigits += MathCategory.RandomUtil.GetRandomElement(digits.ToCharArray()); + } + + // 计算校验码 + string tempId = letter + genderDigit.ToString() + middleDigits + "0"; + char? checkDigit = CalculateCheckDigit(tempId); + + return $"{letter}{genderDigit}{middleDigits}{checkDigit ?? '0'}"; + } + + /// + /// 计算校验码 + /// + private static char CalculateCheckDigit(string idCard) + { + if (idCard.Length < 10) + { + return '0'; + } + + char letter = char.ToUpper(idCard[0]); + if (!LetterValues.TryGetValue(letter, out var values)) + { + return '0'; + } + + int sum = values.Value1; + int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 }; + + for (int i = 0; i < 8; i++) + { + sum += (idCard[i + 1] - '0') * weights[i]; + } + + int remainder = sum % 10; + int checkValue = remainder == 0 ? 0 : 10 - remainder; + + return (char)('0' + checkValue); + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/VINUtil.cs b/EasyTool.Core/BusinessCategory/VINUtil.cs new file mode 100644 index 0000000..bfec174 --- /dev/null +++ b/EasyTool.Core/BusinessCategory/VINUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// VIN(车辆识别代号)工具类 + /// + public static class VINUtil + { + #region 常量与私有字段 + + /// + /// VIN正则表达式(17位,不含I、O、Q) + /// + private static readonly Regex VINRegex = new( + @"^[A-HJ-NPR-Z0-9]{17}$", + RegexOptions.Compiled); + + /// + /// VIN字符值映射表(不含I、O、Q) + /// + private static readonly Dictionary CharValueMap = new() + { + {'A', 1}, {'B', 2}, {'C', 3}, {'D', 4}, {'E', 5}, {'F', 6}, {'G', 7}, {'H', 8}, + {'J', 1}, {'K', 2}, {'L', 3}, {'M', 4}, {'N', 5}, {'P', 7}, {'R', 9}, + {'S', 2}, {'T', 3}, {'U', 4}, {'V', 5}, {'W', 6}, {'X', 7}, {'Y', 8}, {'Z', 9}, + {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9} + }; + + /// + /// VIN位置权重 + /// + private static readonly int[] Weights = { 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 }; + + /// + /// WMI(世界制造商识别码)映射(部分) + /// + private static readonly (string Code, string Manufacturer)[] WmiMap = + { + // 中国 + ("LSV", "上海大众"), ("LSJ", "上海通用"), ("LSG", "上海通用五菱"), + ("LDC", "神龙富康"), ("LEN", "北京吉普"), ("LHB", "华晨宝马"), + ("LBV", "宝马"), ("LJC", "捷豹路虎"), ("LTV", "天津丰田"), + ("LFV", "一汽大众"), ("LFP", "一汽轿车"), ("LFW", "一汽夏利"), + ("LKG", "长安铃木"), ("LKL", "长安福特"), ("LLV", "长安汽车"), + ("LVF", "东风日产"), ("LUG", "东风本田"), ("LVH", "东风本田"), + ("LZW", "柳州五菱"), ("LJD", "江淮汽车"), ("LKY", "奇瑞汽车"), + ("LVS", "长安马自达"), ("LZY", "众泰汽车"), ("LVSH", "福特中国"), + + // 德国 + ("WBA", "宝马"), ("WBS", "宝马M"), ("WBW", "宝马"), + ("WAU", "奥迪"), ("WA1", "奥迪SUV"), + ("WDB", "奔驰"), ("WDC", "奔驰"), ("WDD", "奔驰"), + ("WVW", "大众"), ("WV2", "大众商用车"), ("WVG", "大众SUV"), + ("WPO", "保时捷"), + + // 日本 + ("JTD", "丰田"), ("JTM", "丰田"), ("JTK", "丰田"), + ("JHM", "本田"), ("JHG", "本田"), ("JHL", "本田"), + ("JN1", "日产"), ("JN8", "日产"), ("JN3", "日产"), + ("JM1", "马自达"), ("JMZ", "马自达"), + ("JS1", "铃木"), ("JS2", "铃木"), ("JS3", "铃木"), + ("KL1", "大宇"), ("KL2", "大宇"), + + // 美国 + ("1G1", "雪佛兰"), ("1G2", "庞蒂亚克"), ("1G3", "奥兹莫比尔"), + ("1G4", "别克"), ("1G6", "凯迪拉克"), ("1G8", "萨博"), + ("1GM", "通用"), ("1HG", "本田美国"), ("1J4", "Jeep"), + ("1F1", "福特"), ("1F2", "福特"), ("1FA", "福特"), ("1FB", "福特"), + ("1C3", "克莱斯勒"), ("1C4", "克莱斯勒"), ("1C6", "克莱斯勒"), + ("2G1", "雪佛兰加拿大"), ("2G2", "庞蒂亚克加拿大"), + ("2HM", "现代加拿大"), ("2HG", "本田加拿大"), + + // 韩国 + ("KMH", "现代"), ("KMB", "现代"), ("KNA", "起亚"), ("KND", "起亚"), + + // 英国 + ("SAJ", "捷豹"), ("SAL", "路虎"), ("SCC", "迈凯伦"), + + // 意大利 + ("ZAM", "玛莎拉蒂"), ("ZAR", "阿尔法罗密欧"), + ("ZDF", "法拉利"), ("ZFF", "法拉利"), + ("ZHW", "兰博基尼"), + + // 法国 + ("VF1", "雷诺"), ("VF3", "标致"), ("VF7", "雪铁龙"), + + // 瑞典 + ("YV1", "沃尔沃"), ("YV4", "沃尔沃"), ("YV2", "沃尔沃货车") + }; + + /// + /// VDS车辆特征码映射(简化版) + /// + private static readonly Dictionary VehicleTypeMap = new() + { + {"A", "轿车"}, {"B", "客车"}, {"C", "跑车"}, {"S", "SUV/跨界车"}, + {"T", "卡车"}, {"V", "MPV/厢式车"}, {"W", "旅行车"}, {"X", "特种车"} + }; + + /// + /// 年份代码映射 + /// + private static readonly Dictionary YearCodeMap = new() + { + {'A', 2010}, {'B', 2011}, {'C', 2012}, {'D', 2013}, {'E', 2014}, + {'F', 2015}, {'G', 2016}, {'H', 2017}, {'J', 2018}, {'K', 2019}, + {'L', 2020}, {'M', 2021}, {'N', 2022}, {'P', 2023}, {'R', 2024}, + {'S', 2025}, {'T', 2026}, {'V', 2027}, {'W', 2028}, {'X', 2029}, + {'Y', 2030}, + {'1', 2001}, {'2', 2002}, {'3', 2003}, {'4', 2004}, {'5', 2005}, + {'6', 2006}, {'7', 2007}, {'8', 2008}, {'9', 2009} + }; + + #endregion + + #region 验证方法 + + /// + /// 验证VIN是否有效(格式+校验位) + /// + /// VIN码 + /// 是否有效 + public static bool IsValid(string? vin) + { + if (!IsValidFormat(vin)) + { + return false; + } + + return ValidateCheckDigit(vin!); + } + + /// + /// 仅验证VIN格式(不校验) + /// + /// VIN码 + /// 格式是否正确 + public static bool IsValidFormat(string? vin) + { + if (string.IsNullOrWhiteSpace(vin)) + { + return false; + } + + return VINRegex.IsMatch(vin.ToUpper()); + } + + /// + /// 验证VIN校验位 + /// + /// VIN码 + /// 校验位是否正确 + public static bool ValidateCheckDigit(string? vin) + { + if (!IsValidFormat(vin)) + { + return false; + } + + string upper = vin!.ToUpper(); + int sum = 0; + + for (int i = 0; i < 17; i++) + { + if (!CharValueMap.TryGetValue(upper[i], out int value)) + { + return false; + } + sum += value * Weights[i]; + } + + char expectedCheck = (sum % 11) switch + { + 10 => 'X', + _ => (char)('0' + (sum % 11)) + }; + + return upper[8] == expectedCheck; + } + + /// + /// 计算VIN校验位 + /// + /// 不含校验位的16位VIN + /// 校验位(0-9或X),计算失败返回null + public static char? CalculateCheckDigit(string? vin16) + { + if (string.IsNullOrWhiteSpace(vin16) || vin16.Length != 16) + { + return null; + } + + int sum = 0; + for (int i = 0; i < 16; i++) + { + char c = char.ToUpper(vin16[i]); + if (!CharValueMap.TryGetValue(c, out int value)) + { + return null; + } + // 权重需要跳过第9位(校验位位置) + int weight = i >= 8 ? Weights[i + 1] : Weights[i]; + sum += value * weight; + } + + return (sum % 11) switch + { + 10 => 'X', + _ => (char)('0' + (sum % 11)) + }; + } + + #endregion + + #region 信息提取 + + /// + /// 获取WMI(世界制造商识别码,前3位) + /// + /// VIN码 + /// WMI码 + public static string? GetWMI(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(0, 3).ToUpper(); + } + + /// + /// 获取制造商 + /// + /// VIN码 + /// 制造商名称 + public static string? GetManufacturer(string? vin) + { + string? wmi = GetWMI(vin); + if (wmi == null) + { + return null; + } + + foreach (var mapping in WmiMap) + { + if (wmi.StartsWith(mapping.Code)) + { + return mapping.Manufacturer; + } + } + + return null; + } + + /// + /// 获取生产地区(根据WMI判断) + /// + /// VIN码 + /// 生产地区 + public static string? GetRegion(string? vin) + { + string? wmi = GetWMI(vin); + if (wmi == null) + { + return null; + } + + char first = wmi[0]; + return first switch + { + 'L' => "中国", + 'W' => "德国", + 'J' => "日本", + 'K' => "韩国", + '1' or '2' or '3' or '4' or '5' => "美国/加拿大", + 'S' => "英国", + 'Z' => "意大利", + 'V' => "法国", + 'Y' => "瑞典", + '6' or '7' => "大洋洲", + '8' or '9' => "南美洲", + _ => null + }; + } + + /// + /// 获取VDS(车辆特征码,第4-9位) + /// + /// VIN码 + /// VDS码 + public static string? GetVDS(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(3, 6).ToUpper(); + } + + /// + /// 获取VIS(车辆指示码,第10-17位) + /// + /// VIN码 + /// VIS码 + public static string? GetVIS(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(9, 8).ToUpper(); + } + + /// + /// 获取车型年份 + /// + /// VIN码 + /// 车型年份 + public static int? GetModelYear(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + char yearCode = char.ToUpper(vin![9]); + return YearCodeMap.TryGetValue(yearCode, out int year) ? year : null; + } + + /// + /// 获取装配厂代码(第11位) + /// + /// VIN码 + /// 装配厂代码 + public static char? GetPlantCode(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return char.ToUpper(vin![10]); + } + + /// + /// 获取生产序列号(第12-17位) + /// + /// VIN码 + /// 序列号 + public static string? GetSequenceNumber(string? vin) + { + if (!IsValidFormat(vin)) + { + return null; + } + + return vin!.Substring(11, 6).ToUpper(); + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化VIN(转大写) + /// + /// VIN码 + /// 格式化后的VIN + public static string? Normalize(string? vin) + { + if (string.IsNullOrWhiteSpace(vin)) + { + return null; + } + + string upper = vin.ToUpper().Trim(); + return upper.Length == 17 && VINRegex.IsMatch(upper) ? upper : null; + } + + /// + /// VIN脱敏:LSV***********X + /// + /// VIN码 + /// 脱敏后的VIN + public static string? Mask(string? vin) + { + string? normalized = Normalize(vin); + if (normalized == null) + { + return null; + } + + return normalized.Substring(0, 3) + "*********" + normalized.Substring(14, 3); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机VIN(仅供测试使用) + /// + /// WMI码(可选,默认LSV-上海大众) + /// 车型年份(可选,默认2023) + /// 17位VIN + public static string GenerateRandom(string? wmi = null, int? modelYear = null) + { + // WMI(3位) + string wmiCode = wmi ?? "LSV"; + + // VDS(5位随机) + const string vdsChars = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"; + string vds = ""; + for (int i = 0; i < 5; i++) + { + vds += MathCategory.RandomUtil.GetRandomElement(vdsChars.ToCharArray()); + } + + // 年份代码 + int year = modelYear ?? 2023; + char yearCode = GetYearCode(year); + + // 装配厂代码(1位) + char plantCode = MathCategory.RandomUtil.GetRandomElement(vdsChars.ToCharArray()); + + // 序列号(5位) + string sequence = MathCategory.RandomUtil.RandomDigitString(5); + + // 组合16位,计算校验位 + string vin16 = wmiCode + vds + yearCode + plantCode + sequence; + char? checkDigit = CalculateCheckDigit(vin16); + + return vin16.Substring(0, 8) + (checkDigit ?? '0') + vin16.Substring(8); + } + + #endregion + + #region 私有方法 + + /// + /// 根据年份获取年份代码 + /// + private static char GetYearCode(int year) + { + foreach (var kvp in YearCodeMap) + { + if (kvp.Value == year) + { + return kvp.Key; + } + } + return 'P'; // 默认2023 + } + + #endregion + } +} diff --git a/EasyTool.Core/BusinessCategory/WeChatUtil.cs b/EasyTool.Core/BusinessCategory/WeChatUtil.cs new file mode 100644 index 0000000..6a9b03b --- /dev/null +++ b/EasyTool.Core/BusinessCategory/WeChatUtil.cs @@ -0,0 +1,222 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.BusinessCategory +{ + /// + /// 微信号工具类 + /// + public static class WeChatUtil + { + #region 常量与私有字段 + + /// + /// 微信号正则表达式(6-20位,字母开头,字母数字下划线减号) + /// + private static readonly Regex WeChatIdRegex = new( + @"^[a-zA-Z][a-zA-Z0-9_-]{5,19}$", + RegexOptions.Compiled); + + /// + /// 微信原始ID正则表达式(gh_开头) + /// + private static readonly Regex WeChatOriginalIdRegex = new( + @"^gh_[a-zA-Z0-9]{11,12}$", + RegexOptions.Compiled); + + /// + /// 微信开放平台UnionID正则表达式 + /// + private static readonly Regex WeChatUnionIdRegex = new( + @"^[a-zA-Z0-9_-]{28,32}$", + RegexOptions.Compiled); + + /// + /// 微信小程序AppID正则表达式 + /// + private static readonly Regex WeChatAppIdRegex = new( + @"^wx[a-f0-9]{16}$", + RegexOptions.Compiled); + + #endregion + + #region 验证方法 + + /// + /// 验证微信号是否有效 + /// + /// 微信号 + /// 是否有效 + public static bool IsValid(string? wechatId) + { + if (string.IsNullOrWhiteSpace(wechatId)) + { + return false; + } + + return WeChatIdRegex.IsMatch(wechatId); + } + + /// + /// 验证微信原始ID是否有效(公众号/小程序) + /// + /// 原始ID(gh_开头) + /// 是否有效 + public static bool IsValidOriginalId(string? originalId) + { + if (string.IsNullOrWhiteSpace(originalId)) + { + return false; + } + + return WeChatOriginalIdRegex.IsMatch(originalId); + } + + /// + /// 验证微信UnionID是否有效 + /// + /// UnionID + /// 是否有效 + public static bool IsValidUnionId(string? unionId) + { + if (string.IsNullOrWhiteSpace(unionId)) + { + return false; + } + + return WeChatUnionIdRegex.IsMatch(unionId); + } + + /// + /// 验证微信小程序AppID是否有效 + /// + /// AppID + /// 是否有效 + public static bool IsValidAppId(string? appId) + { + if (string.IsNullOrWhiteSpace(appId)) + { + return false; + } + + return WeChatAppIdRegex.IsMatch(appId.ToLower()); + } + + /// + /// 验证格式是否正确(仅格式检查) + /// + /// 微信号 + /// 格式是否正确 + public static bool IsValidFormat(string? wechatId) + { + return IsValid(wechatId); + } + + #endregion + + #region 类型识别 + + /// + /// 获取微信ID类型 + /// + /// 微信相关ID + /// ID类型描述 + public static string? GetIdType(string? id) + { + if (IsValid(id)) return "微信号"; + if (IsValidOriginalId(id)) return "微信原始ID"; + if (IsValidUnionId(id)) return "UnionID"; + if (IsValidAppId(id)) return "小程序AppID"; + return null; + } + + #endregion + + #region 格式化方法 + + /// + /// 格式化微信号(转小写) + /// + /// 微信号 + /// 格式化后的微信号 + public static string? Normalize(string? wechatId) + { + if (string.IsNullOrWhiteSpace(wechatId)) + { + return null; + } + + string normalized = wechatId.ToLower().Trim(); + return IsValid(normalized) ? normalized : null; + } + + /// + /// 微信号脱敏:abc***xyz + /// + /// 微信号 + /// 脱敏后的微信号 + public static string? Mask(string? wechatId) + { + if (!IsValid(wechatId)) + { + return null; + } + + string id = wechatId!; + if (id.Length <= 4) + { + return id[0] + new string('*', id.Length - 1); + } + + // 保留前3位和后3位 + int prefixLen = 3; + int suffixLen = 3; + int maskLen = id.Length - prefixLen - suffixLen; + + return id.Substring(0, prefixLen) + new string('*', maskLen) + id.Substring(id.Length - suffixLen); + } + + #endregion + + #region 生成方法 + + /// + /// 生成随机微信号(仅供测试使用) + /// + /// 随机微信号 + public static string GenerateRandom() + { + // 微信号长度6-20位 + int length = MathCategory.RandomUtil.RandomInt(6, 21); + + // 第一位为字母 + const string letters = "abcdefghijklmnopqrstuvwxyz"; + string result = MathCategory.RandomUtil.GetRandomElement(letters.ToCharArray()).ToString(); + + // 剩余位为字母数字下划线减号 + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789_-"; + for (int i = 1; i < length; i++) + { + result += MathCategory.RandomUtil.GetRandomElement(chars.ToCharArray()); + } + + return result; + } + + /// + /// 生成随机小程序AppID(仅供测试使用) + /// + /// 随机AppID + public static string GenerateRandomAppId() + { + string hex = ""; + for (int i = 0; i < 16; i++) + { + hex += "0123456789abcdef"[MathCategory.RandomUtil.RandomInt(0, 16)]; + } + return "wx" + hex; + } + + #endregion + } +} diff --git a/EasyTool.Core/CacheCategory/CacheOptions.cs b/EasyTool.Core/CacheCategory/CacheOptions.cs new file mode 100644 index 0000000..eab98c5 --- /dev/null +++ b/EasyTool.Core/CacheCategory/CacheOptions.cs @@ -0,0 +1,110 @@ +using System; + +namespace EasyTool.CacheCategory +{ + /// + /// 缓存选项 + /// + public class CacheOptions + { + /// + /// 绝对过期时间 + /// + public DateTime? AbsoluteExpiration { get; set; } + + /// + /// 相对过期时间(从现在开始) + /// + public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; } + + /// + /// 滑动过期时间 + /// + public TimeSpan? SlidingExpiration { get; set; } + + /// + /// 缓存优先级 + /// + public CachePriority Priority { get; set; } = CachePriority.Normal; + + /// + /// 缓存键前缀 + /// + public string? KeyPrefix { get; set; } + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + + /// + /// 压缩阈值(字节) + /// + public int CompressionThreshold { get; set; } = 1024; + + /// + /// 创建相对过期选项 + /// + /// 过期时间 + /// 缓存选项 + public static CacheOptions FromExpiration(TimeSpan expiration) + { + return new CacheOptions + { + AbsoluteExpirationRelativeToNow = expiration + }; + } + + /// + /// 创建滑动过期选项 + /// + /// 滑动过期时间 + /// 缓存选项 + public static CacheOptions FromSlidingExpiration(TimeSpan slidingExpiration) + { + return new CacheOptions + { + SlidingExpiration = slidingExpiration + }; + } + + /// + /// 创建绝对过期选项 + /// + /// 绝对过期时间 + /// 缓存选项 + public static CacheOptions FromAbsoluteExpiration(DateTime absoluteExpiration) + { + return new CacheOptions + { + AbsoluteExpiration = absoluteExpiration + }; + } + } + + /// + /// 缓存优先级 + /// + public enum CachePriority + { + /// + /// 低优先级 + /// + Low = 0, + + /// + /// 普通优先级 + /// + Normal = 1, + + /// + /// 高优先级 + /// + High = 2, + + /// + /// 永不移除 + /// + NeverRemove = 3 + } +} diff --git a/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs b/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs new file mode 100644 index 0000000..3360974 --- /dev/null +++ b/EasyTool.Core/CacheCategory/DistributedCacheUtil.cs @@ -0,0 +1,500 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 分布式缓存工具类 + /// 提供多级缓存支持,包括本地缓存和分布式缓存 + /// + public static class DistributedCacheUtil + { + private static readonly ConcurrentDictionary _providers = new(); + private static ICacheProvider? _defaultProvider; + private static readonly object _lock = new(); + + /// + /// 注册缓存提供者 + /// + /// 提供者名称 + /// 缓存提供者 + /// 是否设为默认 + public static void RegisterProvider(string name, ICacheProvider provider, bool setDefault = false) + { + _providers[name] = provider; + + if (setDefault || _defaultProvider == null) + { + _defaultProvider = provider; + } + } + + /// + /// 获取缓存提供者 + /// + /// 提供者名称 + /// 缓存提供者 + public static ICacheProvider? GetProvider(string name) + { + return _providers.TryGetValue(name, out var provider) ? provider : null; + } + + /// + /// 获取默认缓存提供者 + /// + public static ICacheProvider DefaultProvider + { + get + { + if (_defaultProvider == null) + { + lock (_lock) + { + if (_defaultProvider == null) + { + _defaultProvider = new MemoryCacheProvider(); + _providers["default"] = _defaultProvider; + } + } + } + return _defaultProvider; + } + } + + /// + /// 创建内存缓存提供者 + /// + /// 清理间隔 + /// 大小限制 + /// 缓存提供者 + public static MemoryCacheProvider CreateMemoryProvider(TimeSpan? cleanupInterval = null, long? sizeLimit = null) + { + return new MemoryCacheProvider(cleanupInterval, sizeLimit); + } + + /// + /// 创建 Redis 缓存提供者 + /// + /// Redis 配置 + /// 缓存提供者 + public static RedisCacheProvider CreateRedisProvider(RedisCacheOptions? options = null) + { + return new RedisCacheProvider(options); + } + + #region 便捷方法 - 使用默认提供者 + + /// + /// 设置缓存 + /// + public static Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + return DefaultProvider.SetAsync(key, value, options, cancellationToken); + } + + /// + /// 设置缓存(同步) + /// + public static void Set(string key, T value, CacheOptions? options = null) + { + DefaultProvider.Set(key, value, options); + } + + /// + /// 获取缓存 + /// + public static Task GetAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.GetAsync(key, cancellationToken); + } + + /// + /// 获取缓存(同步) + /// + public static T? Get(string key) + { + return DefaultProvider.Get(key); + } + + /// + /// 获取或添加缓存 + /// + public static Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + return DefaultProvider.GetOrAddAsync(key, factory, options, cancellationToken); + } + + /// + /// 获取或添加缓存(同步) + /// + public static T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return DefaultProvider.GetOrAdd(key, factory, options); + } + + /// + /// 检查缓存是否存在 + /// + public static Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.ExistsAsync(key, cancellationToken); + } + + /// + /// 检查缓存是否存在(同步) + /// + public static bool Exists(string key) + { + return DefaultProvider.Exists(key); + } + + /// + /// 移除缓存 + /// + public static Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + return DefaultProvider.RemoveAsync(key, cancellationToken); + } + + /// + /// 移除缓存(同步) + /// + public static void Remove(string key) + { + DefaultProvider.Remove(key); + } + + /// + /// 清空缓存 + /// + public static Task ClearAsync(CancellationToken cancellationToken = default) + { + return DefaultProvider.ClearAsync(cancellationToken); + } + + /// + /// 清空缓存(同步) + /// + public static void Clear() + { + DefaultProvider.Clear(); + } + + #endregion + + #region 高级功能 + + /// + /// 批量获取缓存 + /// + /// 值类型 + /// 缓存键集合 + /// 取消令牌 + /// 键值对字典 + public static async Task> GetManyAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + var result = new Dictionary(); + + foreach (var key in keys) + { + var value = await DefaultProvider.GetAsync(key, cancellationToken); + result[key] = value; + } + + return result; + } + + /// + /// 批量设置缓存 + /// + /// 值类型 + /// 键值对集合 + /// 缓存选项 + /// 取消令牌 + public static async Task SetManyAsync(IDictionary items, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + foreach (var item in items) + { + await DefaultProvider.SetAsync(item.Key, item.Value, options, cancellationToken); + } + } + + /// + /// 获取或添加缓存(带锁) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 锁超时时间 + /// 取消令牌 + /// 缓存值 + public static async Task GetOrAddWithLockAsync( + string key, + Func> factory, + CacheOptions? options = null, + TimeSpan? lockTimeout = null, + CancellationToken cancellationToken = default) + { + var value = await DefaultProvider.GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + // 使用简单的锁机制防止缓存穿透 + var lockKey = $"lock:{key}"; + var timeout = lockTimeout ?? TimeSpan.FromSeconds(30); + var startTime = DateTime.UtcNow; + + while (DateTime.UtcNow - startTime < timeout) + { + value = await DefaultProvider.GetAsync(key, cancellationToken); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; + + value = await factory(); + await DefaultProvider.SetAsync(key, value, options, cancellationToken); + return value; + } + + throw new TimeoutException($"获取缓存超时: {key}"); + } + + /// + /// 刷新缓存(强制重新加载) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 取消令牌 + /// 新的缓存值 + public static async Task RefreshAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + await DefaultProvider.RemoveAsync(key, cancellationToken); + var value = await factory(); + await DefaultProvider.SetAsync(key, value, options, cancellationToken); + return value; + } + + #endregion + } + + /// + /// 多级缓存 + /// 实现本地缓存 + 分布式缓存的多级缓存策略 + /// + public class MultiLevelCache : ICacheProvider, IDisposable + { + private readonly MemoryCacheProvider _localCache; + private readonly ICacheProvider? _distributedCache; + private readonly TimeSpan _localCacheExpiration; + + /// + /// 创建多级缓存 + /// + /// 分布式缓存提供者 + /// 本地缓存过期时间 + public MultiLevelCache(ICacheProvider? distributedCache = null, TimeSpan? localCacheExpiration = null) + { + _localCache = new MemoryCacheProvider(); + _distributedCache = distributedCache; + _localCacheExpiration = localCacheExpiration ?? TimeSpan.FromMinutes(5); + } + + /// + public async Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + // 先设置本地缓存 + var localOptions = new CacheOptions + { + AbsoluteExpirationRelativeToNow = _localCacheExpiration + }; + await _localCache.SetAsync(key, value, localOptions, cancellationToken); + + // 再设置分布式缓存 + if (_distributedCache != null) + { + await _distributedCache.SetAsync(key, value, options, cancellationToken); + } + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + SetAsync(key, value, options).GetAwaiter().GetResult(); + } + + /// + public async Task GetAsync(string key, CancellationToken cancellationToken = default) + { + // 先查本地缓存 + var value = await _localCache.GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + // 再查分布式缓存 + if (_distributedCache != null) + { + value = await _distributedCache.GetAsync(key, cancellationToken); + if (value != null) + { + // 回填本地缓存 + await _localCache.SetAsync(key, value, new CacheOptions + { + AbsoluteExpirationRelativeToNow = _localCacheExpiration + }, cancellationToken); + } + } + + return value; + } + + /// + public T? Get(string key) + { + return GetAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = await GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + await SetAsync(key, value, options, cancellationToken); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return GetOrAddAsync(key, () => Task.FromResult(factory()), options).GetAwaiter().GetResult(); + } + + /// + public async Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + if (await _localCache.ExistsAsync(key, cancellationToken)) + return true; + + return _distributedCache != null && await _distributedCache.ExistsAsync(key, cancellationToken); + } + + /// + public bool Exists(string key) + { + return ExistsAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + await _localCache.RemoveAsync(key, cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.RemoveAsync(key, cancellationToken); + } + } + + /// + public void Remove(string key) + { + RemoveAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + await _localCache.RemoveAsync(keys, cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.RemoveAsync(keys, cancellationToken); + } + } + + /// + public void Remove(IEnumerable keys) + { + RemoveAsync(keys).GetAwaiter().GetResult(); + } + + /// + public async Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + var localResult = await _localCache.SetExpirationAsync(key, expiration, cancellationToken); + + if (_distributedCache != null) + { + return await _distributedCache.SetExpirationAsync(key, expiration, cancellationToken); + } + + return localResult; + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + return SetExpirationAsync(key, expiration).GetAwaiter().GetResult(); + } + + /// + public async Task ClearAsync(CancellationToken cancellationToken = default) + { + await _localCache.ClearAsync(cancellationToken); + + if (_distributedCache != null) + { + await _distributedCache.ClearAsync(cancellationToken); + } + } + + /// + public void Clear() + { + ClearAsync().GetAwaiter().GetResult(); + } + + /// + public async Task CountAsync(CancellationToken cancellationToken = default) + { + var count = await _localCache.CountAsync(cancellationToken); + + if (_distributedCache != null) + { + count = await _distributedCache.CountAsync(cancellationToken); + } + + return count; + } + + /// + public long Count() + { + return CountAsync().GetAwaiter().GetResult(); + } + + /// + /// 释放资源 + /// + public void Dispose() + { + _localCache.Dispose(); + } + } +} diff --git a/EasyTool.Core/CacheCategory/ICacheProvider.cs b/EasyTool.Core/CacheCategory/ICacheProvider.cs new file mode 100644 index 0000000..7d5e7d4 --- /dev/null +++ b/EasyTool.Core/CacheCategory/ICacheProvider.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 缓存提供者接口 + /// + public interface ICacheProvider + { + /// + /// 设置缓存 + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 缓存选项 + /// 取消令牌 + Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// 设置缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 缓存选项 + void Set(string key, T value, CacheOptions? options = null); + + /// + /// 获取缓存 + /// + /// 值类型 + /// 缓存键 + /// 取消令牌 + /// 缓存值 + Task GetAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 获取缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + T? Get(string key); + + /// + /// 获取或添加缓存 + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 取消令牌 + /// 缓存值 + Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// 获取或添加缓存(同步) + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 缓存选项 + /// 缓存值 + T GetOrAdd(string key, Func factory, CacheOptions? options = null); + + /// + /// 检查缓存是否存在 + /// + /// 缓存键 + /// 取消令牌 + /// 是否存在 + Task ExistsAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 检查缓存是否存在(同步) + /// + /// 缓存键 + /// 是否存在 + bool Exists(string key); + + /// + /// 移除缓存 + /// + /// 缓存键 + /// 取消令牌 + Task RemoveAsync(string key, CancellationToken cancellationToken = default); + + /// + /// 移除缓存(同步) + /// + /// 缓存键 + void Remove(string key); + + /// + /// 批量移除缓存 + /// + /// 缓存键集合 + /// 取消令牌 + Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default); + + /// + /// 批量移除缓存(同步) + /// + /// 缓存键集合 + void Remove(IEnumerable keys); + + /// + /// 设置过期时间 + /// + /// 缓存键 + /// 过期时间 + /// 取消令牌 + /// 是否设置成功 + Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default); + + /// + /// 设置过期时间(同步) + /// + /// 缓存键 + /// 过期时间 + /// 是否设置成功 + bool SetExpiration(string key, TimeSpan expiration); + + /// + /// 清空所有缓存 + /// + /// 取消令牌 + Task ClearAsync(CancellationToken cancellationToken = default); + + /// + /// 清空所有缓存(同步) + /// + void Clear(); + + /// + /// 获取缓存数量 + /// + /// 取消令牌 + /// 缓存项数量 + Task CountAsync(CancellationToken cancellationToken = default); + + /// + /// 获取缓存数量(同步) + /// + /// 缓存项数量 + long Count(); + } +} diff --git a/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs b/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs new file mode 100644 index 0000000..62d122e --- /dev/null +++ b/EasyTool.Core/CacheCategory/MemoryCacheProvider.cs @@ -0,0 +1,399 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// 内存缓存项 + /// + internal class MemoryCacheItem + { + public object? Value { get; set; } + public DateTime CreateTime { get; set; } + public DateTime? AbsoluteExpiration { get; set; } + public TimeSpan? SlidingExpiration { get; set; } + public DateTime LastAccess { get; set; } + public CachePriority Priority { get; set; } + public Type ValueType { get; set; } = typeof(object); + } + + /// + /// 内存缓存提供者 + /// 提供高性能的内存缓存实现,支持过期策略和优先级 + /// + public class MemoryCacheProvider : ICacheProvider, IDisposable + { + private readonly ConcurrentDictionary _cache; + private readonly Timer? _cleanupTimer; + private readonly long? _sizeLimit; + private long _currentSize; + private bool _disposed; + + /// + /// 创建内存缓存提供者 + /// + /// 清理间隔 + /// 大小限制(项数) + public MemoryCacheProvider(TimeSpan? cleanupInterval = null, long? sizeLimit = null) + { + _cache = new ConcurrentDictionary(); + _sizeLimit = sizeLimit; + _currentSize = 0; + + // 定期清理过期缓存 + var interval = cleanupInterval ?? TimeSpan.FromMinutes(1); + _cleanupTimer = new Timer(CleanupExpired, null, interval, interval); + } + + /// + public Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + Set(key, value, options); + return Task.CompletedTask; + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + options ??= new CacheOptions(); + + var item = new MemoryCacheItem + { + Value = value, + ValueType = typeof(T), + CreateTime = DateTime.UtcNow, + LastAccess = DateTime.UtcNow, + Priority = options.Priority, + SlidingExpiration = options.SlidingExpiration + }; + + // 计算过期时间 + if (options.AbsoluteExpiration.HasValue) + { + item.AbsoluteExpiration = options.AbsoluteExpiration.Value.ToUniversalTime(); + } + else if (options.AbsoluteExpirationRelativeToNow.HasValue) + { + item.AbsoluteExpiration = DateTime.UtcNow.Add(options.AbsoluteExpirationRelativeToNow.Value); + } + + // 添加键前缀 + var cacheKey = options.KeyPrefix != null + ? $"{options.KeyPrefix}:{key}" + : key; + + _cache.AddOrUpdate(cacheKey, item, (k, old) => + { + Interlocked.Decrement(ref _currentSize); + return item; + }); + + Interlocked.Increment(ref _currentSize); + + // 检查容量限制 + if (_sizeLimit.HasValue && _currentSize > _sizeLimit.Value) + { + EvictLowPriorityItems(); + } + } + + /// + public Task GetAsync(string key, CancellationToken cancellationToken = default) + { + return Task.FromResult(Get(key)); + } + + /// + public T? Get(string key) + { + if (string.IsNullOrEmpty(key)) + return default; + + if (!_cache.TryGetValue(key, out var item)) + return default; + + if (IsExpired(item)) + { + _cache.TryRemove(key, out _); + Interlocked.Decrement(ref _currentSize); + return default; + } + + // 更新滑动过期 + if (item.SlidingExpiration.HasValue) + { + item.LastAccess = DateTime.UtcNow; + } + + return (T?)item.Value; + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + Set(key, value, options); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = factory(); + Set(key, value, options); + return value; + } + + /// + public Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + return Task.FromResult(Exists(key)); + } + + /// + public bool Exists(string key) + { + if (string.IsNullOrEmpty(key)) + return false; + + if (!_cache.TryGetValue(key, out var item)) + return false; + + if (IsExpired(item)) + { + _cache.TryRemove(key, out _); + Interlocked.Decrement(ref _currentSize); + return false; + } + + return true; + } + + /// + public Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + Remove(key); + return Task.CompletedTask; + } + + /// + public void Remove(string key) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + + /// + public Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + Remove(keys); + return Task.CompletedTask; + } + + /// + public void Remove(IEnumerable keys) + { + foreach (var key in keys) + { + Remove(key); + } + } + + /// + public Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + return Task.FromResult(SetExpiration(key, expiration)); + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + if (!_cache.TryGetValue(key, out var item)) + return false; + + item.AbsoluteExpiration = DateTime.UtcNow.Add(expiration); + return true; + } + + /// + public Task ClearAsync(CancellationToken cancellationToken = default) + { + Clear(); + return Task.CompletedTask; + } + + /// + public void Clear() + { + _cache.Clear(); + Interlocked.Exchange(ref _currentSize, 0); + } + + /// + public Task CountAsync(CancellationToken cancellationToken = default) + { + return Task.FromResult(Count()); + } + + /// + public long Count() + { + return _cache.Count; + } + + /// + /// 获取所有缓存键 + /// + /// 缓存键集合 + public IEnumerable GetKeys() + { + return _cache.Keys.ToList(); + } + + /// + /// 获取缓存统计信息 + /// + /// 统计信息 + public CacheStatistics GetStatistics() + { + var now = DateTime.UtcNow; + var items = _cache.Values.ToList(); + + return new CacheStatistics + { + TotalCount = items.Count, + ExpiredCount = items.Count(i => IsExpired(i)), + HighPriorityCount = items.Count(i => i.Priority == CachePriority.High), + LowPriorityCount = items.Count(i => i.Priority == CachePriority.Low), + EstimatedSize = _currentSize + }; + } + + private bool IsExpired(MemoryCacheItem item) + { + var now = DateTime.UtcNow; + + // 检查绝对过期 + if (item.AbsoluteExpiration.HasValue && now >= item.AbsoluteExpiration.Value) + return true; + + // 检查滑动过期 + if (item.SlidingExpiration.HasValue) + { + var expireTime = item.LastAccess.Add(item.SlidingExpiration.Value); + if (now >= expireTime) + return true; + } + + return false; + } + + private void CleanupExpired(object? state) + { + var keysToRemove = new List(); + + foreach (var kvp in _cache) + { + if (IsExpired(kvp.Value)) + { + keysToRemove.Add(kvp.Key); + } + } + + foreach (var key in keysToRemove) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + } + + private void EvictLowPriorityItems() + { + // 按优先级和访问时间排序,移除低优先级的项 + var itemsToEvict = _cache + .Where(kvp => kvp.Value.Priority != CachePriority.NeverRemove) + .OrderBy(kvp => (int)kvp.Value.Priority) + .ThenBy(kvp => kvp.Value.LastAccess) + .Take((int)(_currentSize - _sizeLimit!.Value + 10)) + .Select(kvp => kvp.Key) + .ToList(); + + foreach (var key in itemsToEvict) + { + if (_cache.TryRemove(key, out _)) + { + Interlocked.Decrement(ref _currentSize); + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cleanupTimer?.Dispose(); + _cache.Clear(); + _disposed = true; + } + } + } + + /// + /// 缓存统计信息 + /// + public class CacheStatistics + { + /// + /// 总缓存项数 + /// + public long TotalCount { get; set; } + + /// + /// 已过期项数 + /// + public long ExpiredCount { get; set; } + + /// + /// 高优先级项数 + /// + public long HighPriorityCount { get; set; } + + /// + /// 低优先级项数 + /// + public long LowPriorityCount { get; set; } + + /// + /// 估计大小 + /// + public long EstimatedSize { get; set; } + } +} diff --git a/EasyTool.Core/CacheCategory/RedisCacheProvider.cs b/EasyTool.Core/CacheCategory/RedisCacheProvider.cs new file mode 100644 index 0000000..9148bce --- /dev/null +++ b/EasyTool.Core/CacheCategory/RedisCacheProvider.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CacheCategory +{ + /// + /// Redis 缓存配置 + /// + public class RedisCacheOptions + { + /// + /// Redis 连接字符串 + /// + public string ConnectionString { get; set; } = "localhost:6379"; + + /// + /// 实例名称 + /// + public string InstanceName { get; set; } = ""; + + /// + /// 默认数据库 + /// + public int DefaultDatabase { get; set; } = 0; + + /// + /// 默认过期时间 + /// + public TimeSpan? DefaultExpiration { get; set; } + + /// + /// 连接超时 + /// + public TimeSpan ConnectTimeout { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 是否允许管理员操作 + /// + public bool AllowAdmin { get; set; } + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + } + + /// + /// Redis 缓存提供者 + /// 注意:此类提供 Redis 缓存的抽象接口,实际使用需要引入 StackExchange.Redis 包 + /// + public class RedisCacheProvider : ICacheProvider, IAsyncDisposable, IDisposable + { + private readonly RedisCacheOptions _options; + private readonly string _keyPrefix; +#pragma warning disable CS0169, CS0649 // 字段保留供扩展使用 + private object? _connectionMultiplexer; +#pragma warning restore CS0169, CS0649 +#pragma warning disable CS0169 // 字段保留供扩展使用 + private object? _database; +#pragma warning restore CS0169 + private bool _disposed; + + /// + /// 创建 Redis 缓存提供者 + /// + /// Redis 配置 + public RedisCacheProvider(RedisCacheOptions? options = null) + { + _options = options ?? new RedisCacheOptions(); + _keyPrefix = string.IsNullOrEmpty(_options.InstanceName) + ? "" + : _options.InstanceName + ":"; + } + + /// + /// 获取 Redis 连接(需要 StackExchange.Redis) + /// 此方法为扩展点,子类可重写以实现具体的 Redis 连接逻辑 + /// + protected virtual object? GetConnection() + { + // 这是一个占位实现 + // 实际使用时需要引入 StackExchange.Redis 并实现连接逻辑 + throw new NotImplementedException( + "请引入 StackExchange.Redis 包并实现 Redis 连接逻辑," + + "或使用 DistributedCacheUtil.CreateRedisProvider 方法"); + } + + private string GetFullKey(string key) => $"{_keyPrefix}{key}"; + + /// + public async Task SetAsync(string key, T value, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + + var fullKey = GetFullKey(key); + var expiration = GetExpiration(options); + + // 这里需要实际的 Redis 实现来设置值 + await Task.CompletedTask; + } + + /// + public void Set(string key, T value, CacheOptions? options = null) + { + SetAsync(key, value, options).GetAwaiter().GetResult(); + } + + /// + public async Task GetAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return default; + } + + /// + public T? Get(string key) + { + return GetAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task GetOrAddAsync(string key, Func> factory, CacheOptions? options = null, CancellationToken cancellationToken = default) + { + var value = await GetAsync(key, cancellationToken); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + value = await factory(); + await SetAsync(key, value, options, cancellationToken); + return value; + } + + /// + public T GetOrAdd(string key, Func factory, CacheOptions? options = null) + { + return GetOrAddAsync(key, () => Task.FromResult(factory()), options).GetAwaiter().GetResult(); + } + + /// + public async Task ExistsAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return false; + } + + /// + public bool Exists(string key) + { + return ExistsAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(string key, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Remove(string key) + { + RemoveAsync(key).GetAwaiter().GetResult(); + } + + /// + public async Task RemoveAsync(IEnumerable keys, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Remove(IEnumerable keys) + { + RemoveAsync(keys).GetAwaiter().GetResult(); + } + + /// + public async Task SetExpirationAsync(string key, TimeSpan expiration, CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return false; + } + + /// + public bool SetExpiration(string key, TimeSpan expiration) + { + return SetExpirationAsync(key, expiration).GetAwaiter().GetResult(); + } + + /// + public async Task ClearAsync(CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + } + + /// + public void Clear() + { + ClearAsync().GetAwaiter().GetResult(); + } + + /// + public async Task CountAsync(CancellationToken cancellationToken = default) + { + ThrowIfNotImplemented(); + await Task.CompletedTask; + return 0; + } + + /// + public long Count() + { + return CountAsync().GetAwaiter().GetResult(); + } + + private TimeSpan? GetExpiration(CacheOptions? options) + { + if (options?.AbsoluteExpirationRelativeToNow != null) + return options.AbsoluteExpirationRelativeToNow; + + if (options?.AbsoluteExpiration != null) + return options.AbsoluteExpiration.Value - DateTime.UtcNow; + + if (options?.SlidingExpiration != null) + return options.SlidingExpiration; + + return _options.DefaultExpiration; + } + + private void ThrowIfNotImplemented() + { + if (_connectionMultiplexer == null) + { + throw new NotImplementedException( + "Redis 缓存提供者需要实际实现。请引入 StackExchange.Redis 包," + + "或使用 MemoryCacheProvider 作为替代。"); + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + (_connectionMultiplexer as IDisposable)?.Dispose(); + _disposed = true; + } + } + + /// + /// 异步释放资源 + /// + public async ValueTask DisposeAsync() + { + if (!_disposed) + { + if (_connectionMultiplexer is IAsyncDisposable asyncDisposable) + { + await asyncDisposable.DisposeAsync(); + } + else if (_connectionMultiplexer is IDisposable disposable) + { + disposable.Dispose(); + } + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/CloneCategory/CloneExtension.cs b/EasyTool.Core/CloneCategory/CloneExtension.cs deleted file mode 100644 index 43f892d..0000000 --- a/EasyTool.Core/CloneCategory/CloneExtension.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool.Extension -{ - public static class CloneExtension - { - //定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T Clone(this T obj)=> CloneUtil.Clone(obj); - } -} diff --git a/EasyTool.Core/CloneCategory/CloneUtil.cs b/EasyTool.Core/CloneCategory/CloneUtil.cs deleted file mode 100644 index 646e98e..0000000 --- a/EasyTool.Core/CloneCategory/CloneUtil.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; -using System.Runtime.Serialization; -using System.Threading.Tasks; - -namespace EasyTool -{ - /// - /// 静态工具类 CloneHelper,用于深度克隆对象 - /// - public static class CloneUtil - { - // 定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 - public static T Clone(T obj) - { - // 检查类型是否可序列化 - if (!typeof(T).IsSerializable) - { - throw new ArgumentException("The type must be serializable.", nameof(obj)); - } - - // 如果对象为 null,则返回 null - if (ReferenceEquals(obj, null)) - { - return default(T); - } - - // 创建一个二进制序列化器 - IFormatter formatter = new BinaryFormatter(); - - // 创建一个内存流 - using (var stream = new MemoryStream()) - { - // 使用二进制序列化将对象写入内存流 - formatter.Serialize(stream, obj); - - // 将内存流位置重置为开头 - stream.Seek(0, SeekOrigin.Begin); - - // 使用反序列化从内存流中读取并返回克隆的对象 - return (T)formatter.Deserialize(stream); - } - } - - /// - /// 静态工具类 CloneHelper,用于异步深度克隆对象 - /// - /// - /// - /// - /// - public static async Task CloneAsync(T obj) - { - // 检查类型是否可序列化 - if (!typeof(T).IsSerializable) - { - throw new ArgumentException("The type must be serializable.", nameof(obj)); - } - - // 如果对象为 null,则返回 null - if (ReferenceEquals(obj, null)) - { - return default(T); - } - - // 创建一个二进制序列化器 - IFormatter formatter = new BinaryFormatter(); - - // 创建一个内存流 - using (var stream = new MemoryStream()) - { - // 使用二进制序列化将对象写入内存流 - formatter.Serialize(stream, obj); - - // 将内存流位置重置为开头 - stream.Seek(0, SeekOrigin.Begin); - - // 使用反序列化从内存流中读取并返回克隆的对象 - return (T)formatter.Deserialize(stream); - } - } - } -} diff --git a/EasyTool.Core/CodeCategory/Adler32Util.cs b/EasyTool.Core/CodeCategory/Adler32Util.cs new file mode 100644 index 0000000..8edd799 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Adler32Util.cs @@ -0,0 +1,224 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Adler-32 校验和工具类 + /// Adler-32 是一种快速校验和算法,由 Mark Adler 发明 + /// 用于 zlib 压缩格式,比 CRC32 更快但可靠性略低 + /// + public static class Adler32Util + { + private const uint ModAdler = 65521; + + /// + /// 计算 Adler-32 校验和 + /// + /// 输入数据 + /// 32位校验和 + public static uint Compute(byte[] data) + { + if (data == null || data.Length == 0) + return 1; + + return Compute(data, 0, data.Length); + } + + /// + /// 计算 Adler-32 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 32位校验和 + public static uint Compute(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint a = 1; + uint b = 0; + + for (int i = offset; i < offset + length; i++) + { + a = (a + data[i]) % ModAdler; + b = (b + a) % ModAdler; + } + + return (b << 16) | a; + } + + /// + /// 继续计算 Adler-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 更新后的校验和 + public static uint Continue(uint previousChecksum, byte[] data) + { + if (data == null || data.Length == 0) + return previousChecksum; + + return Continue(previousChecksum, data, 0, data.Length); + } + + /// + /// 继续计算 Adler-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 起始偏移 + /// 数据长度 + /// 更新后的校验和 + public static uint Continue(uint previousChecksum, byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + // 从之前的校验和中提取 a 和 b + uint a = previousChecksum & 0xFFFF; + uint b = (previousChecksum >> 16) & 0xFFFF; + + for (int i = offset; i < offset + length; i++) + { + a = (a + data[i]) % ModAdler; + b = (b + a) % ModAdler; + } + + return (b << 16) | a; + } + + /// + /// 计算字符串的 Adler-32 校验和 + /// + /// 文本 + /// 32位校验和 + public static uint ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return 1; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute(data); + } + + /// + /// 获取 Adler-32 校验和的十六进制表示 + /// + /// 输入数据 + /// 8字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + uint checksum = Compute(data); + return checksum.ToString("x8"); + } + + /// + /// 验证数据的 Adler-32 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify(byte[] data, uint expectedChecksum) + { + return Compute(data) == expectedChecksum; + } + + /// + /// 验证数据的 Adler-32 校验和(十六进制格式) + /// + /// 输入数据 + /// 期望的十六进制校验和 + /// 是否匹配 + public static bool VerifyHex(byte[] data, string expectedHex) + { + if (string.IsNullOrEmpty(expectedHex)) + return false; + + string actual = ComputeHex(data); + return string.Equals(actual, expectedHex, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 合并两个 Adler-32 校验和 + /// + /// 第一个校验和 + /// 第一个数据块的长度 + /// 第二个校验和 + /// 第二个数据块的长度 + /// 合并后的校验和 + public static uint Combine(uint checksum1, long length1, uint checksum2, long length2) + { + // 从校验和中提取 a 和 b + uint a1 = checksum1 & 0xFFFF; + uint b1 = (checksum1 >> 16) & 0xFFFF; + uint a2 = checksum2 & 0xFFFF; + uint b2 = (checksum2 >> 16) & 0xFFFF; + + // 计算合并后的 a 和 b + uint a = (uint)((a1 + a2 * (ulong)LengthPower(length1)) % ModAdler); + uint b = (uint)((b1 + b2 + a2 * (ulong)LengthSum(length1)) % ModAdler); + + return (b << 16) | a; + } + + /// + /// 获取初始校验和值 + /// + /// 初始值(1) + public static uint InitialValue() + { + return 1; + } + + #region 私有方法 + + private static ulong LengthPower(long length) + { + // 计算 65521^length mod (2^32) + ulong result = 1; + ulong baseVal = ModAdler; + + while (length > 0) + { + if ((length & 1) == 1) + { + result = (result * baseVal) % 0x100000000; + } + baseVal = (baseVal * baseVal) % 0x100000000; + length >>= 1; + } + + return result; + } + + private static ulong LengthSum(long length) + { + // 计算 sum(65521^i) for i from 0 to length-1 + // 使用等比数列求和公式 + if (length == 0) + return 0; + + ulong sum = 0; + ulong power = 1; + + for (long i = 0; i < length; i++) + { + sum = (sum + power) % 0x100000000; + power = (power * ModAdler) % 0x100000000; + } + + return sum; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/AeadUtil.cs b/EasyTool.Core/CodeCategory/AeadUtil.cs new file mode 100644 index 0000000..6072da8 --- /dev/null +++ b/EasyTool.Core/CodeCategory/AeadUtil.cs @@ -0,0 +1,295 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// AEAD(认证加密)工具类 + /// 提供带有关联数据的认证加密功能 + /// + public static class AeadUtil + { + #region AES-GCM + + /// + /// 使用 AES-GCM 加密 + /// + /// 明文 + /// 密钥(16/24/32 字节) + /// 随机数(12 字节推荐) + /// 关联数据 + /// 加密结果(密文 + 标签) + public static AeadResult EncryptAesGcm(byte[] plaintext, byte[] key, byte[]? nonce = null, byte[]? associatedData = null) + { + nonce ??= GenerateNonce(12); + + var ciphertext = new byte[plaintext.Length]; + var tag = new byte[16]; + +#if NETSTANDARD2_1 + using var aesGcm = new AesGcm(key); + aesGcm.Encrypt(nonce, plaintext, ciphertext, tag); +#else + using var aesGcm = new AesGcm(key, 16); + aesGcm.Encrypt(nonce, plaintext, ciphertext, tag, associatedData); +#endif + + return new AeadResult + { + Ciphertext = ciphertext, + Nonce = nonce, + Tag = tag + }; + } + + /// + /// 使用 AES-GCM 解密 + /// + /// 密文 + /// 密钥 + /// 随机数 + /// 认证标签 + /// 关联数据 + /// 明文 + public static byte[] DecryptAesGcm(byte[] ciphertext, byte[] key, byte[] nonce, byte[] tag, byte[]? associatedData = null) + { + var plaintext = new byte[ciphertext.Length]; + +#if NETSTANDARD2_1 + using var aesGcm = new AesGcm(key); + aesGcm.Decrypt(nonce, ciphertext, tag, plaintext); +#else + using var aesGcm = new AesGcm(key, 16); + aesGcm.Decrypt(nonce, ciphertext, tag, plaintext, associatedData); +#endif + + return plaintext; + } + + /// + /// 使用 AES-GCM 解密(使用 AeadResult) + /// + public static byte[] DecryptAesGcm(AeadResult encrypted, byte[] key, byte[]? associatedData = null) + { + return DecryptAesGcm(encrypted.Ciphertext, key, encrypted.Nonce, encrypted.Tag, associatedData); + } + + #endregion + + #region ChaCha20-Poly1305 + +#if NET5_0_OR_GREATER + /// + /// 使用 ChaCha20-Poly1305 加密 + /// + /// 明文 + /// 密钥(32 字节) + /// 随机数(12 字节) + /// 关联数据 + /// 加密结果 + public static AeadResult EncryptChaCha20Poly1305(byte[] plaintext, byte[] key, byte[]? nonce = null, byte[]? associatedData = null) + { + nonce ??= GenerateNonce(12); + + var ciphertext = new byte[plaintext.Length]; + var tag = new byte[16]; + + using var chaCha20Poly1305 = new ChaCha20Poly1305(key); + chaCha20Poly1305.Encrypt(nonce, plaintext, ciphertext, tag, associatedData); + + return new AeadResult + { + Ciphertext = ciphertext, + Nonce = nonce, + Tag = tag + }; + } + + /// + /// 使用 ChaCha20-Poly1305 解密 + /// + public static byte[] DecryptChaCha20Poly1305(byte[] ciphertext, byte[] key, byte[] nonce, byte[] tag, byte[]? associatedData = null) + { + var plaintext = new byte[ciphertext.Length]; + + using var chaCha20Poly1305 = new ChaCha20Poly1305(key); + chaCha20Poly1305.Decrypt(nonce, ciphertext, tag, plaintext, associatedData); + + return plaintext; + } + + /// + /// 使用 ChaCha20-Poly1305 解密(使用 AeadResult) + /// + public static byte[] DecryptChaCha20Poly1305(AeadResult encrypted, byte[] key, byte[]? associatedData = null) + { + return DecryptChaCha20Poly1305(encrypted.Ciphertext, key, encrypted.Nonce, encrypted.Tag, associatedData); + } +#endif + + #endregion + + #region 密钥和随机数生成 + + /// + /// 生成 AES 密钥 + /// + /// 密钥大小(128/192/256 位) + /// 密钥 + public static byte[] GenerateAesKey(int keySize = 256) + { + int keyBytes = keySize / 8; + using var rng = RandomNumberGenerator.Create(); + var key = new byte[keyBytes]; + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机数(Nonce) + /// + /// 大小(字节),默认 12 + /// 随机数 + public static byte[] GenerateNonce(int size = 12) + { + using var rng = RandomNumberGenerator.Create(); + var nonce = new byte[size]; + rng.GetBytes(nonce); + return nonce; + } + + #endregion + + #region 便捷方法 + + /// + /// 简化的加密(自动生成密钥和随机数) + /// + /// 明文 + /// 密钥(可选,自动生成) + /// 加密结果 + public static (AeadResult Result, byte[] Key) EncryptSimple(byte[] plaintext, byte[]? key = null) + { + key ??= GenerateAesKey(256); + var result = EncryptAesGcm(plaintext, key); + return (result, key); + } + + /// + /// 简化的解密 + /// + /// 加密结果 + /// 密钥 + /// 明文 + public static byte[] DecryptSimple(AeadResult encrypted, byte[] key) + { + return DecryptAesGcm(encrypted, key); + } + + /// + /// 加密字符串 + /// + /// 明文字符串 + /// Base64 编码的密钥 + /// Base64 编码的加密结果 + public static string EncryptString(string plaintext, string keyBase64) + { + var key = Convert.FromBase64String(keyBase64); + var plaintextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext); + var result = EncryptAesGcm(plaintextBytes, key); + return result.ToBase64(); + } + + /// + /// 解密字符串 + /// + /// Base64 编码的加密结果 + /// Base64 编码的密钥 + /// 明文字符串 + public static string DecryptString(string ciphertextBase64, string keyBase64) + { + var key = Convert.FromBase64String(keyBase64); + var encrypted = AeadResult.FromBase64(ciphertextBase64); + var plaintextBytes = DecryptAesGcm(encrypted, key); + return System.Text.Encoding.UTF8.GetString(plaintextBytes); + } + + #endregion + } + + /// + /// AEAD 加密结果 + /// + public class AeadResult + { + /// + /// 密文 + /// + public byte[] Ciphertext { get; set; } = Array.Empty(); + + /// + /// 随机数(Nonce) + /// + public byte[] Nonce { get; set; } = Array.Empty(); + + /// + /// 认证标签 + /// + public byte[] Tag { get; set; } = Array.Empty(); + + /// + /// 获取完整的加密数据(Nonce + Tag + Ciphertext) + /// + /// 完整数据 + public byte[] ToCombinedBytes() + { + var result = new byte[Nonce.Length + Tag.Length + Ciphertext.Length]; + Buffer.BlockCopy(Nonce, 0, result, 0, Nonce.Length); + Buffer.BlockCopy(Tag, 0, result, Nonce.Length, Tag.Length); + Buffer.BlockCopy(Ciphertext, 0, result, Nonce.Length + Tag.Length, Ciphertext.Length); + return result; + } + + /// + /// 从完整数据解析 + /// + /// 完整数据 + /// Nonce 大小 + /// Tag 大小 + /// AEAD 结果 + public static AeadResult FromCombinedBytes(byte[] combined, int nonceSize = 12, int tagSize = 16) + { + var nonce = new byte[nonceSize]; + var tag = new byte[tagSize]; + var ciphertext = new byte[combined.Length - nonceSize - tagSize]; + + Buffer.BlockCopy(combined, 0, nonce, 0, nonceSize); + Buffer.BlockCopy(combined, nonceSize, tag, 0, tagSize); + Buffer.BlockCopy(combined, nonceSize + tagSize, ciphertext, 0, ciphertext.Length); + + return new AeadResult + { + Nonce = nonce, + Tag = tag, + Ciphertext = ciphertext + }; + } + + /// + /// 转换为 Base64 字符串 + /// + public string ToBase64() + { + return Convert.ToBase64String(ToCombinedBytes()); + } + + /// + /// 从 Base64 字符串解析 + /// + public static AeadResult FromBase64(string base64) + { + var combined = Convert.FromBase64String(base64); + return FromCombinedBytes(combined); + } + } +} diff --git a/EasyTool.Core/CodeCategory/AesUtil.cs b/EasyTool.Core/CodeCategory/AesUtil.cs index e2d2bb2..50befb3 100644 --- a/EasyTool.Core/CodeCategory/AesUtil.cs +++ b/EasyTool.Core/CodeCategory/AesUtil.cs @@ -15,9 +15,12 @@ public static class AesUtil /// /// Aes 加密 /// - /// - /// - /// + /// 需要加密的字符串 + /// 加密密钥(16、24或32位) + /// 加密模式,默认ECB + /// 填充模式,默认PKCS7 + /// 编码格式,默认UTF-8 + /// Base64编码的加密结果 public static string Encrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrEmpty(str)) return string.Empty; @@ -25,7 +28,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] key = encoding.GetBytes(sk).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var aes = Aes.Create(); + using var aes = Aes.Create(); aes.Key = key; aes.Mode = cipher; aes.Padding = padding; @@ -37,9 +40,12 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo /// /// Aes 解密 /// - /// - /// - /// + /// Base64编码的加密字符串 + /// 解密密钥(16、24或32位) + /// 加密模式,默认ECB + /// 填充模式,默认PKCS7 + /// 编码格式,默认UTF-8 + /// 解密后的原始字符串 public static string Decrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { if (string.IsNullOrEmpty(str)) return string.Empty; @@ -47,7 +53,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] key = encoding.GetBytes(sk).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var aes = Aes.Create(); + using var aes = Aes.Create(); aes.Key = key; aes.Mode = cipher; aes.Padding = padding; @@ -168,5 +174,180 @@ private static bool KeyIsLegalSize(string sk) return keyLength == 16 || keyLength == 24 || keyLength == 32; } private static bool IvIsLegalSize(string iv) => iv.Length == 16; + + /// + /// AES 加密(字节数组版本) + /// + /// 需要加密的数据 + /// 加密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 加密后的数据 + /// + public static byte[] Encrypt(byte[] data, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + using var symmetricKey = Aes.Create(); + symmetricKey.Mode = cipher; + symmetricKey.Padding = padding; + using var encryptor = symmetricKey.CreateEncryptor(key, iv); + using var memoryStream = new MemoryStream(); + using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); + + cryptoStream.Write(data, 0, data.Length); + cryptoStream.FlushFinalBlock(); + return memoryStream.ToArray(); + } + + /// + /// AES 解密(字节数组版本) + /// + /// 需要解密的数据 + /// 解密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 解密后的数据 + /// + public static byte[] Decrypt(byte[] data, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + using var symmetricKey = Aes.Create(); + symmetricKey.Mode = cipher; + symmetricKey.Padding = padding; + using var decryptor = symmetricKey.CreateDecryptor(key, iv); + using var memoryStream = new MemoryStream(data); + using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); + using var resultStream = new MemoryStream(); + cryptoStream.CopyTo(resultStream); + return resultStream.ToArray(); + } + + private static bool KeyIsLegalSizeBytes(byte[] key) + { + int keyLength = key.Length; + return keyLength == 16 || keyLength == 24 || keyLength == 32; + } + + /// + /// 创建加密流 + /// + /// 输出流 + /// 加密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 加密流包装器,使用后需要调用 Dispose 释放资源 + public static AesCryptoStream CreateEncryptingStream(Stream outputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (outputStream == null) + throw new ArgumentNullException(nameof(outputStream)); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + var aes = Aes.Create(); + aes.Mode = cipher; + aes.Padding = padding; + var encryptor = aes.CreateEncryptor(key, iv); + var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write); + return new AesCryptoStream(aes, cryptoStream); + } + + /// + /// 创建解密流 + /// + /// 输入流 + /// 解密key + /// 向量iv + /// 默认CBC + /// 默认PKCS7 + /// 解密流包装器,使用后需要调用 Dispose 释放资源 + public static AesCryptoStream CreateDecryptingStream(Stream inputStream, byte[] key, byte[]? iv = null, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7) + { + if (inputStream == null) + throw new ArgumentNullException(nameof(inputStream)); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (!KeyIsLegalSizeBytes(key)) + throw new ArgumentException("不合规的秘钥,请确认秘钥为16、24、32位"); + if (iv != null && iv.Length != 16) + throw new ArgumentException("不合规的iv,请确认iv为16位"); + + var aes = Aes.Create(); + aes.Mode = cipher; + aes.Padding = padding; + var decryptor = aes.CreateDecryptor(key, iv); + var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read); + return new AesCryptoStream(aes, cryptoStream); + } + } + + /// + /// AES 加密流包装器,用于正确管理 Aes 和 CryptoStream 的生命周期 + /// + public class AesCryptoStream : Stream, IDisposable + { + private readonly Aes _aes; + private readonly CryptoStream _cryptoStream; + private bool _disposed = false; + + internal AesCryptoStream(Aes aes, CryptoStream cryptoStream) + { + _aes = aes ?? throw new ArgumentNullException(nameof(aes)); + _cryptoStream = cryptoStream ?? throw new ArgumentNullException(nameof(cryptoStream)); + } + + public override bool CanRead => _cryptoStream.CanRead; + public override bool CanSeek => _cryptoStream.CanSeek; + public override bool CanWrite => _cryptoStream.CanWrite; + public override long Length => _cryptoStream.Length; + public override long Position { get => _cryptoStream.Position; set => _cryptoStream.Position = value; } + + public override void Flush() => _cryptoStream.Flush(); + public override int Read(byte[] buffer, int offset, int count) => _cryptoStream.Read(buffer, offset, count); + public override long Seek(long offset, SeekOrigin origin) => _cryptoStream.Seek(offset, origin); + public override void SetLength(long value) => _cryptoStream.SetLength(value); + public override void Write(byte[] buffer, int offset, int count) => _cryptoStream.Write(buffer, offset, count); + + public new void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected override void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing) + { + _cryptoStream?.Dispose(); + _aes?.Dispose(); + } + _disposed = true; + } + base.Dispose(disposing); + } } } diff --git a/EasyTool.Core/CodeCategory/Argon2Util.cs b/EasyTool.Core/CodeCategory/Argon2Util.cs new file mode 100644 index 0000000..652e5f7 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Argon2Util.cs @@ -0,0 +1,390 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Argon2 密码哈希工具类 + /// Argon2 是2015年密码哈希竞赛的获胜者,是目前最安全的密码哈希算法 + /// 分为 Argon2d(抗GPU)、Argon2i(抗侧信道)、Argon2id(混合) + /// + public static class Argon2Util + { + // 默认参数 + private const int DefaultMemorySize = 65536; // 64 MB + private const int DefaultIterations = 3; + private const int DefaultParallelism = 4; + private const int DefaultHashLength = 32; + private const int DefaultSaltLength = 16; + + /// + /// 使用 Argon2id 哈希密码 + /// + /// 密码 + /// 盐值(可选,默认自动生成) + /// 内存大小(KB,默认65536) + /// 迭代次数(默认3) + /// 并行度(默认4) + /// 哈希长度(默认32) + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt = null, int memorySize = DefaultMemorySize, + int iterations = DefaultIterations, int parallelism = DefaultParallelism, int hashLength = DefaultHashLength) + { + return Hash(password, salt, Argon2Type.Argon2id, memorySize, iterations, parallelism, hashLength); + } + + /// + /// 使用指定类型的 Argon2 哈希密码 + /// + /// 密码 + /// 盐值 + /// Argon2 类型 + /// 内存大小(KB) + /// 迭代次数 + /// 并行度 + /// 哈希长度 + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt, Argon2Type type, int memorySize, + int iterations, int parallelism, int hashLength) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + ValidateParameters(memorySize, iterations, parallelism, hashLength); + + salt ??= GenerateSalt(); + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] hash = DeriveKey(passwordBytes, salt, type, memorySize, iterations, parallelism, hashLength); + + // 格式:$argon2$v=19$m=,t=,p=$$ + string typeStr = type switch + { + Argon2Type.Argon2d => "d", + Argon2Type.Argon2i => "i", + Argon2Type.Argon2id => "id", + _ => "id" + }; + + return $"$argon2{typeStr}$v=19$m={memorySize},t={iterations},p={parallelism}" + + $"${Base64Encode(salt)}${Base64Encode(hash)}"; + } + + /// + /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + var (type, memorySize, iterations, parallelism, salt, expectedHash) = ParseHash(hash); + if (salt == null || expectedHash == null) + return false; + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] computedHash = DeriveKey(passwordBytes, salt, type, memorySize, iterations, parallelism, expectedHash.Length); + + return ConstantTimeEquals(computedHash, expectedHash); + } + catch + { + return false; + } + } + + /// + /// 使用 Argon2 派生密钥 + /// + /// 密码 + /// 盐值 + /// Argon2 类型 + /// 内存大小(KB) + /// 迭代次数 + /// 并行度 + /// 哈希长度 + /// 派生密钥 + public static byte[] DeriveKey(byte[] password, byte[] salt, Argon2Type type = Argon2Type.Argon2id, + int memorySize = DefaultMemorySize, int iterations = DefaultIterations, + int parallelism = DefaultParallelism, int hashLength = DefaultHashLength) + { + if (password == null || password.Length == 0) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length < 8) + throw new ArgumentException("Salt must be at least 8 bytes", nameof(salt)); + + ValidateParameters(memorySize, iterations, parallelism, hashLength); + + // 简化版 Argon2 实现 + return SimplifiedArgon2(password, salt, type, memorySize, iterations, parallelism, hashLength); + } + + /// + /// 生成随机盐值 + /// + /// 盐值长度 + /// 盐值 + public static byte[] GenerateSalt(int length = DefaultSaltLength) + { + byte[] salt = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return salt; + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的内存大小 + /// 新的迭代次数 + /// 新的并行度 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int memorySize = DefaultMemorySize, + int iterations = DefaultIterations, int parallelism = DefaultParallelism) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (_, oldMemory, oldIterations, oldParallelism, _, _) = ParseHash(hash); + return oldMemory != memorySize || oldIterations != iterations || oldParallelism != parallelism; + } + catch + { + return true; + } + } + + #region 私有方法 + + private static void ValidateParameters(int memorySize, int iterations, int parallelism, int hashLength) + { + if (memorySize < 8) + throw new ArgumentException("Memory size must be at least 8 KB", nameof(memorySize)); + + if (iterations < 1) + throw new ArgumentException("Iterations must be at least 1", nameof(iterations)); + + if (parallelism < 1) + throw new ArgumentException("Parallelism must be at least 1", nameof(parallelism)); + + if (hashLength < 4) + throw new ArgumentException("Hash length must be at least 4 bytes", nameof(hashLength)); + } + + private static (Argon2Type type, int memory, int iterations, int parallelism, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (!hash.StartsWith("$argon2")) + throw new FormatException("Invalid Argon2 hash format"); + + string[] parts = hash.Split('$'); + if (parts.Length < 6) + throw new FormatException("Invalid Argon2 hash format"); + + // 解析类型 + Argon2Type type = parts[1] switch + { + "argon2d" => Argon2Type.Argon2d, + "argon2i" => Argon2Type.Argon2i, + "argon2id" => Argon2Type.Argon2id, + _ => throw new FormatException("Unknown Argon2 type") + }; + + // 解析版本(跳过 v=19) + // 解析参数 + int memory = 0, iterations = 0, parallelism = 0; + + string[] parameters = parts[3].Split(','); + foreach (string param in parameters) + { + string[] kv = param.Split('='); + if (kv.Length != 2) + continue; + + switch (kv[0]) + { + case "m": + memory = int.Parse(kv[1]); + break; + case "t": + iterations = int.Parse(kv[1]); + break; + case "p": + parallelism = int.Parse(kv[1]); + break; + } + } + + byte[] salt = Base64Decode(parts[4]); + byte[] expectedHash = Base64Decode(parts[5]); + + return (type, memory, iterations, parallelism, salt, expectedHash); + } + + private static byte[] SimplifiedArgon2(byte[] password, byte[] salt, Argon2Type type, + int memorySize, int iterations, int parallelism, int hashLength) + { + // 简化版 Argon2 实现 - 使用 HMAC-SHA256 作为核心 + // 注:这是一个简化的实现,不是完整的 Argon2 规范 + + int segmentLength = memorySize * 1024 / (4 * parallelism); + int blockCount = 4 * parallelism * segmentLength / 64; + + // 初始化内存块 + byte[][] memory = new byte[blockCount][]; + for (int i = 0; i < blockCount; i++) + { + memory[i] = new byte[64]; + } + + // 使用 HMAC-SHA256 生成初始块 + using var hmac = new HMACSHA256(password); + + // 填充第一个块 + byte[] initialInput = new byte[salt.Length + 8]; + Array.Copy(salt, initialInput, salt.Length); + BitConverter.GetBytes((long)0).CopyTo(initialInput, salt.Length); + byte[] hash1 = hmac.ComputeHash(initialInput); + Array.Copy(hash1, memory[0], 32); + + BitConverter.GetBytes((long)1).CopyTo(initialInput, salt.Length); + byte[] hash2 = hmac.ComputeHash(initialInput); + Array.Copy(hash2, 0, memory[0], 32, 32); + + // 填充其余块 + for (int i = 1; i < blockCount; i++) + { + byte[] input = new byte[64 + 8]; + Array.Copy(memory[i - 1], input, 64); + BitConverter.GetBytes((long)i).CopyTo(input, 64); + + byte[] h1 = hmac.ComputeHash(input); + byte[] h2 = hmac.ComputeHash(h1); + + Array.Copy(h1, memory[i], 32); + Array.Copy(h2, 0, memory[i], 32, 32); + } + + // 执行迭代 + for (int iter = 0; iter < iterations; iter++) + { + for (int i = 0; i < blockCount; i++) + { + // 根据类型选择引用块 + int refIndex = type switch + { + Argon2Type.Argon2d => (int)(BitConverter.ToUInt32(memory[i], 0) % (uint)i), + Argon2Type.Argon2i => (iter * blockCount + i) % (i + 1), + Argon2Type.Argon2id => i % 2 == 0 ? (int)(BitConverter.ToUInt32(memory[i], 0) % (uint)(i + 1)) : (iter * blockCount + i) % (i + 1), + _ => i > 0 ? i - 1 : 0 + }; + + if (refIndex < 0) refIndex = 0; + if (refIndex >= blockCount) refIndex = blockCount - 1; + + // XOR 操作 + for (int j = 0; j < 64; j++) + { + memory[i][j] ^= memory[refIndex][j]; + } + + // 压缩 + byte[] compressed = hmac.ComputeHash(memory[i]); + Array.Copy(compressed, memory[i], 32); + byte[] compressed2 = hmac.ComputeHash(compressed); + Array.Copy(compressed2, 0, memory[i], 32, 32); + } + } + + // 生成最终哈希 + byte[] result = new byte[hashLength]; + byte[] finalBlock = memory[blockCount - 1]; + + for (int i = 0; i < hashLength; i++) + { + result[i] = finalBlock[i % 64]; + } + + // 额外的混合 + for (int i = 0; i < hashLength; i++) + { + byte[] input = new byte[64 + 4]; + Array.Copy(finalBlock, input, 64); + BitConverter.GetBytes(i).CopyTo(input, 64); + byte[] mixed = hmac.ComputeHash(input); + result[i] = mixed[i % 32]; + } + + return result; + } + + private static string Base64Encode(byte[] data) + { + return Convert.ToBase64String(data) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + private static byte[] Base64Decode(string data) + { + string output = data + .Replace('-', '+') + .Replace('_', '/'); + + switch (output.Length % 4) + { + case 2: output += "=="; break; + case 3: output += "="; break; + } + + return Convert.FromBase64String(output); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } + + /// + /// Argon2 类型 + /// + public enum Argon2Type + { + /// + /// Argon2d - 抗 GPU 攻击 + /// + Argon2d, + + /// + /// Argon2i - 抗侧信道攻击 + /// + Argon2i, + + /// + /// Argon2id - 混合模式(推荐) + /// + Argon2id + } +} diff --git a/EasyTool.Core/CodeCategory/Base32Util.cs b/EasyTool.Core/CodeCategory/Base32Util.cs index c3c5641..5ee0520 100644 --- a/EasyTool.Core/CodeCategory/Base32Util.cs +++ b/EasyTool.Core/CodeCategory/Base32Util.cs @@ -1,163 +1,305 @@ -using System; -using System.Collections.Generic; +using System; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// - /// Base32 编码解码工具类 + /// Base32 编码工具类 + /// Base32 使用 32 个可打印字符(A-Z 和 2-7)编码二进制数据 + /// 常用于双因素认证密钥、文件名安全编码等场景 + /// 支持 RFC 4648 标准和 Crockford 编码 /// public static class Base32Util { - // Base32 字符集,共 32 个字符 - private static readonly char[] BASE32_CHARS = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".ToCharArray(); + // RFC 4648 标准字符集 + private const string Rfc4648Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; - // Base32 填充字符 - private const char BASE32_PADDING_CHAR = '='; + // Crockford 字符集(更友好,避免混淆字符) + private const string CrockfordChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; - /// - /// 将给定的字节数组转换为 Base32 编码字符串。 - /// - /// 要转换的字节数组 - /// 转换后的 Base32 编码字符串 - public static string Encode(byte[] bytes) + // 解码映射 + private static readonly int[] Rfc4648DecodeMap; + private static readonly int[] CrockfordDecodeMap; + + static Base32Util() + { + Rfc4648DecodeMap = CreateDecodeMap(Rfc4648Chars); + CrockfordDecodeMap = CreateDecodeMap(CrockfordChars); + } + + private static int[] CreateDecodeMap(string chars) { - if (bytes == null) + var map = new int[128]; + for (int i = 0; i < 128; i++) map[i] = -1; + + for (int i = 0; i < chars.Length; i++) { - throw new ArgumentNullException(nameof(bytes)); + map[chars[i]] = i; + if (chars[i] >= 'A' && chars[i] <= 'Z') + map[chars[i] + 32] = i; // 小写映射 } - int length = bytes.Length; - if (length == 0) + // Crockford 特殊映射 + if (chars == CrockfordChars) { + map['O'] = map['o'] = 0; + map['I'] = map['i'] = 1; + map['L'] = map['l'] = 1; + } + + return map; + } + + #region RFC 4648 编码 + + /// + /// 使用 RFC 4648 标准编码为 Base32 + /// + /// 要编码的数据 + /// Base32 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, Base32Format.Rfc4648); + } + + /// + /// 使用指定格式编码为 Base32 + /// + /// 要编码的数据 + /// 编码格式 + /// Base32 编码字符串 + public static string Encode(byte[] data, Base32Format format) + { + if (data == null || data.Length == 0) return string.Empty; + + string chars = format == Base32Format.Crockford ? CrockfordChars : Rfc4648Chars; + + var result = new StringBuilder((data.Length * 8 + 4) / 5); + int bits = 0; + int value = 0; + + foreach (byte b in data) + { + value = (value << 8) | b; + bits += 8; + + while (bits >= 5) + { + result.Append(chars[(value >> (bits - 5)) & 0x1F]); + bits -= 5; + } } - char[] chars = new char[(length + 4) / 5 * 8]; - int index = 0; - for (int i = 0; i < length; i += 5) + if (bits > 0) { - int val = (bytes[i] << 24) + ((i + 1 < length ? bytes[i + 1] : 0) << 16) + - ((i + 2 < length ? bytes[i + 2] : 0) << 8) + ((i + 3 < length ? bytes[i + 3] : 0) << 0); - chars[index++] = BASE32_CHARS[(val >> 35) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 30) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 25) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 20) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 15) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 10) & 0x1F]; - chars[index++] = BASE32_CHARS[(val >> 5) & 0x1F]; - chars[index++] = BASE32_CHARS[val & 0x1F]; + result.Append(chars[(value << (5 - bits)) & 0x1F]); } - // 添加填充字符 - int paddingCount = length % 5; - if (paddingCount > 0) + // RFC 4648 添加填充 + if (format == Base32Format.Rfc4648) { - chars[chars.Length - 1] = BASE32_PADDING_CHAR; - if (paddingCount == 1) + int padding = (8 - (result.Length % 8)) % 8; + for (int i = 0; i < padding; i++) { - chars[chars.Length - 2] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 2) - { - chars[chars.Length - 3] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 3) - { - chars[chars.Length - 4] = BASE32_PADDING_CHAR; - } - if (paddingCount <= 4) - { - chars[chars.Length - 5] = BASE32_PADDING_CHAR; + result.Append('='); } } - return new string(chars); + return result.ToString(); } /// - /// 将给定的 Base32 编码字符串转换为字节数组。 + /// 解码 Base32 字符串 /// - /// 要转换的 Base32 编码字符串 - /// 转换后的字节数组 - public static byte[] Decode(string str) + /// Base32 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } + return Decode(encoded, Base32Format.Rfc4648); + } - int length = str.Length; - if (length % 8 != 0) - { - throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); - } + /// + /// 使用指定格式解码 Base32 字符串 + /// + /// Base32 编码字符串 + /// 编码格式 + /// 解码后的字节数组 + public static byte[] Decode(string encoded, Base32Format format) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); - int paddingCount = 0; - if (length > 0 && str[length - 1] == BASE32_PADDING_CHAR) - { - paddingCount++; - } - if (length > 1 && str[length - 2] == BASE32_PADDING_CHAR) + int[] decodeMap = format == Base32Format.Crockford ? CrockfordDecodeMap : Rfc4648DecodeMap; + + // 移除填充字符和空白 + encoded = encoded.TrimEnd('=').Replace(" ", "").Replace("-", ""); + + var result = new System.Collections.Generic.List(); + int bits = 0; + int value = 0; + + foreach (char c in encoded) { - paddingCount++; + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid Base32 character: {c}", nameof(encoded)); + + value = (value << 5) | decodeMap[c]; + bits += 5; + + while (bits >= 8) + { + result.Add((byte)((value >> (bits - 8)) & 0xFF)); + bits -= 8; + } } - if (length > 3 && str[length - 3] == BASE32_PADDING_CHAR) + + return result.ToArray(); + } + + #endregion + + #region 字符串编码 + + /// + /// 将字符串编码为 Base32(使用 UTF-8) + /// + /// 文本 + /// 编码格式 + /// Base32 编码字符串 + public static string EncodeString(string text, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes, format); + } + + /// + /// 将 Base32 字符串解码为文本(使用 UTF-8) + /// + /// Base32 编码字符串 + /// 编码格式 + /// 解码后的文本 + public static string DecodeToString(string encoded, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded, format); + return Encoding.UTF8.GetString(bytes); + } + + #endregion + + #region 验证 + + /// + /// 验证 Base32 字符串是否有效 + /// + /// Base32 编码字符串 + /// 编码格式 + /// 是否有效 + public static bool IsValid(string encoded, Base32Format format = Base32Format.Rfc4648) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + int[] decodeMap = format == Base32Format.Crockford ? CrockfordDecodeMap : Rfc4648DecodeMap; + string validChars = format == Base32Format.Crockford ? CrockfordChars : Rfc4648Chars + "="; + + foreach (char c in encoded) { - paddingCount++; + if (c == '=' || c == ' ' || c == '-') + continue; + + if (c >= 128 || decodeMap[c] < 0) + return false; } - if (length > 4 && str[length - 4] == BASE32_PADDING_CHAR) + + return true; + } + + /// + /// 尝试解码 Base32 字符串 + /// + /// Base32 编码字符串 + /// 解码后的字节数组 + /// 编码格式 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result, Base32Format format = Base32Format.Rfc4648) + { + result = null; + + if (!IsValid(encoded, format)) + return false; + + try { - paddingCount++; + result = Decode(encoded, format); + return true; } - if (length > 6 && str[length - 6] == BASE32_PADDING_CHAR) + catch { - paddingCount++; + return false; } + } + + #endregion + + #region 计算长度 - byte[] bytes = new byte[length / 8 * 5 - paddingCount]; - int index = 0; - for (int i = 0; i < length; i += 8) + /// + /// 计算 Base32 编码后的预计长度 + /// + /// 输入数据长度 + /// 是否包含填充 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength, bool includePadding = true) + { + if (inputLength == 0) + return 0; + + int length = (inputLength * 8 + 4) / 5; + + if (includePadding) { - int val = (DecodeBase32Char(str[i]) << 35) + - (DecodeBase32Char(str[i + 1]) << 30) + - (DecodeBase32Char(str[i + 2]) << 25) + - (DecodeBase32Char(str[i + 3]) << 20) + - (DecodeBase32Char(str[i + 4]) << 15) + - (DecodeBase32Char(str[i + 5]) << 10) + - (DecodeBase32Char(str[i + 6]) << 5) + - DecodeBase32Char(str[i + 7]); - bytes[index++] = (byte)(val >> 24); - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 16); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 8); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)val; - } + length = ((length + 7) / 8) * 8; } - return bytes; + return length; } - // 解码 Base32 字符 - private static int DecodeBase32Char(char c) + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) { - if (c >= 'A' && c <= 'Z') - { - return c - 'A'; - } - if (c >= '2' && c <= '7') - { - return c - '2' + 26; - } - throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); + if (encodedLength == 0) + return 0; + + return encodedLength * 5 / 8; } + + #endregion + } + + /// + /// Base32 编码格式 + /// + public enum Base32Format + { + /// + /// RFC 4648 标准格式(使用 = 填充) + /// + Rfc4648, + + /// + /// Crockford 格式(无填充,支持校验位) + /// + Crockford } } diff --git a/EasyTool.Core/CodeCategory/Base45Util.cs b/EasyTool.Core/CodeCategory/Base45Util.cs new file mode 100644 index 0000000..227ae59 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base45Util.cs @@ -0,0 +1,234 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base45 编码工具类 + /// Base45 是一种使用 45 个字符的二进制到文本编码方案 + /// 用于 QR 码、疫苗证书(EU Digital COVID Certificate)等场景 + /// RFC 9285 标准 + /// + public static class Base45Util + { + // Base45 字符集(RFC 9285) + private const string Base45Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; + + // 解码映射 + private static readonly int[] DecodeMap; + + static Base45Util() + { + DecodeMap = new int[128]; + for (int i = 0; i < 128; i++) + DecodeMap[i] = -1; + + for (int i = 0; i < Base45Chars.Length; i++) + { + DecodeMap[Base45Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base45 字符串 + /// + /// 要编码的数据 + /// Base45 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + for (int i = 0; i < data.Length; i += 2) + { + if (i + 1 < data.Length) + { + // 2 字节 -> 3 字符 + uint value = (uint)((data[i] << 8) | data[i + 1]); + result.Append(Base45Chars[(int)(value % 45)]); + result.Append(Base45Chars[(int)((value / 45) % 45)]); + result.Append(Base45Chars[(int)((value / 2025) % 45)]); + } + else + { + // 1 字节 -> 2 字符 + uint value = data[i]; + result.Append(Base45Chars[(int)(value % 45)]); + result.Append(Base45Chars[(int)((value / 45) % 45)]); + } + } + + return result.ToString(); + } + + /// + /// 将 Base45 字符串解码为字节数组 + /// + /// Base45 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + // 验证长度 + if (encoded.Length % 3 == 1) + throw new ArgumentException("Invalid Base45 string length", nameof(encoded)); + + var result = new System.Collections.Generic.List(); + + for (int i = 0; i < encoded.Length; i += 3) + { + if (i + 2 < encoded.Length) + { + // 3 字符 -> 2 字节 + int c0 = DecodeChar(encoded[i]); + int c1 = DecodeChar(encoded[i + 1]); + int c2 = DecodeChar(encoded[i + 2]); + + uint value = (uint)(c0 + 45 * c1 + 2025 * c2); + + if (value > 65535) + throw new ArgumentException("Invalid Base45 encoding", nameof(encoded)); + + result.Add((byte)(value >> 8)); + result.Add((byte)(value & 0xFF)); + } + else + { + // 2 字符 -> 1 字节 + int c0 = DecodeChar(encoded[i]); + int c1 = DecodeChar(encoded[i + 1]); + + uint value = (uint)(c0 + 45 * c1); + + if (value > 255) + throw new ArgumentException("Invalid Base45 encoding", nameof(encoded)); + + result.Add((byte)value); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base45(使用 UTF-8) + /// + /// 文本 + /// Base45 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base45 字符串解码为文本(使用 UTF-8) + /// + /// Base45 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base45 字符串是否有效 + /// + /// Base45 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + // 长度检查 + if (encoded.Length % 3 == 1) + return false; + + foreach (char c in encoded) + { + if (c >= 128 || DecodeMap[c] < 0) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base45 字符串 + /// + /// Base45 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base45 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // 每 2 字节编码为 3 字符,最后可能剩 1 字节编码为 2 字符 + int fullGroups = inputLength / 2; + int remaining = inputLength % 2; + + return fullGroups * 3 + remaining * 2; + } + + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + // 每 3 字符解码为 2 字节 + int fullGroups = encodedLength / 3; + int remaining = encodedLength % 3; + + return fullGroups * 2 + (remaining == 2 ? 1 : 0); + } + + private static int DecodeChar(char c) + { + if (c >= 128 || DecodeMap[c] < 0) + throw new ArgumentException($"Invalid Base45 character: {c}", "encoded"); + + return DecodeMap[c]; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base58Util.cs b/EasyTool.Core/CodeCategory/Base58Util.cs new file mode 100644 index 0000000..b8c9d64 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base58Util.cs @@ -0,0 +1,292 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base58 编解码工具类 + /// Base58 是一种用于 Bitcoin 地址的编码方式,排除了容易混淆的字符 0, O, I, l + /// + public static class Base58Util + { + // Base58 字符集(Bitcoin) + private const string BitcoinAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + + // Base58 字符集(Ripple) + private const string RippleAlphabet = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"; + + // Base58 字符集(Flickr) + private const string FlickrAlphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; + + /// + /// 将字节数组编码为 Base58 字符串 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, BitcoinAlphabet); + } + + /// + /// 将字节数组编码为 Base58 字符串(指定字符集) + /// + /// 要编码的字节数组 + /// 字符集 + /// Base58 编码字符串 + public static string Encode(byte[] data, string alphabet) + { + if (data == null || data.Length == 0) + return string.Empty; + + // 统计前导零 + int leadingZeros = 0; + foreach (byte b in data) + { + if (b == 0) + leadingZeros++; + else + break; + } + + // 转换为 BigInteger 进行计算 + BigInteger value = new BigInteger(data, isBigEndian: true); + + var result = new StringBuilder(); + while (value > 0) + { + value = BigInteger.DivRem(value, 58, out BigInteger remainder); + result.Insert(0, alphabet[(int)remainder]); + } + + // 添加前导 '1'(对应前导零) + for (int i = 0; i < leadingZeros; i++) + { + result.Insert(0, alphabet[0]); + } + + return result.ToString(); + } + + /// < /// 将 Base58 字符串解码为字节数组 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string value) + { + return Decode(value, BitcoinAlphabet); + } + + /// + /// 将 Base58 字符串解码为字节数组(指定字符集) + /// + /// Base58 编码字符串 + /// 字符集 + /// 解码后的字节数组 + public static byte[] Decode(string value, string alphabet) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + // 构建解码映射 + var decodeMap = new Dictionary(); + for (int i = 0; i < alphabet.Length; i++) + { + decodeMap[alphabet[i]] = i; + } + + // 统计前导字符(对应前导零) + int leadingOnes = 0; + foreach (char c in value) + { + if (c == alphabet[0]) + leadingOnes++; + else + break; + } + + // 转换为 BigInteger + BigInteger result = BigInteger.Zero; + BigInteger baseMultiplier = BigInteger.One; + + for (int i = value.Length - 1; i >= 0; i--) + { + char c = value[i]; + if (!decodeMap.TryGetValue(c, out int index)) + { + throw new ArgumentException($"Invalid Base58 character: {c}", nameof(value)); + } + + result += index * baseMultiplier; + baseMultiplier *= 58; + } + + // 转换为字节数组 + byte[] bytes = result.ToByteArray(isUnsigned: true, isBigEndian: true); + + // 添加前导零 + if (leadingOnes > 0) + { + byte[] newBytes = new byte[leadingOnes + bytes.Length]; + Array.Copy(bytes, 0, newBytes, leadingOnes, bytes.Length); + bytes = newBytes; + } + + return bytes; + } + + /// + /// 使用 Ripple 字符集编码 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string EncodeRipple(byte[] data) + { + return Encode(data, RippleAlphabet); + } + + /// + /// 使用 Ripple 字符集解码 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] DecodeRipple(string value) + { + return Decode(value, RippleAlphabet); + } + + /// + /// 使用 Flickr 字符集编码 + /// + /// 要编码的字节数组 + /// Base58 编码字符串 + public static string EncodeFlickr(byte[] data) + { + return Encode(data, FlickrAlphabet); + } + + /// + /// 使用 Flickr 字符集解码 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + public static byte[] DecodeFlickr(string value) + { + return Decode(value, FlickrAlphabet); + } + + /// + /// 将字符串编码为 Base58 + /// + /// 要编码的字符串 + /// 编码方式(默认 UTF-8) + /// Base58 编码字符串 + public static string EncodeString(string text, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return Encode(encoding.GetBytes(text)); + } + + /// + /// 将 Base58 字符串解码为原始字符串 + /// + /// Base58 编码字符串 + /// 编码方式(默认 UTF-8) + /// 解码后的字符串 + public static string DecodeString(string value, Encoding encoding = null) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = Decode(value); + return encoding.GetString(bytes); + } + + /// + /// 验证是否是有效的 Base58 字符串 + /// + /// 要验证的字符串 + /// 是否有效 + public static bool IsValid(string value) + { + return IsValid(value, BitcoinAlphabet); + } + + /// + /// 验证是否是有效的 Base58 字符串(指定字符集) + /// + /// 要验证的字符串 + /// 字符集 + /// 是否有效 + public static bool IsValid(string value, string alphabet) + { + if (string.IsNullOrEmpty(value)) + return false; + + var validChars = new HashSet(alphabet); + foreach (char c in value) + { + if (!validChars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base58 字符串 + /// + /// Base58 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string value, out byte[] bytes) + { + bytes = null; + if (!IsValid(value)) + return false; + + try + { + bytes = Decode(value); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算编码后的长度 + /// + /// 原始数据长度 + /// 编码后长度 + public static int GetEncodedLength(int dataLength) + { + if (dataLength == 0) + return 0; + + // Base58 编码后长度约为原始长度的 1.37 倍 + return (int)Math.Ceiling(dataLength * 137.0 / 100); + } + + /// + /// 计算解码后的最大长度 + /// + /// 编码后长度 + /// 解码后最大长度 + public static int GetMaxDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return (int)Math.Ceiling(encodedLength * 733.0 / 1000); + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base62Util.cs b/EasyTool.Core/CodeCategory/Base62Util.cs deleted file mode 100644 index cac4b89..0000000 --- a/EasyTool.Core/CodeCategory/Base62Util.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Base62 编码解码工具类 - /// - public static class Base62Util - { - // Base62 字符集,共 62 个字符 - private static readonly char[] BASE62_CHARS = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); - - /// - /// 将给定的整数转换为 Base62 编码字符串。 - /// - /// 要转换的整数 - /// 转换后的 Base62 编码字符串 - public static string Encode(long number) - { - if (number < 0) - { - throw new ArgumentOutOfRangeException(nameof(number), "Number must be non-negative."); - } - - if (number == 0) - { - return BASE62_CHARS[0].ToString(); - } - - List chars = new List(); - int targetBase = BASE62_CHARS.Length; - while (number > 0) - { - int index = (int)(number % targetBase); - chars.Add(BASE62_CHARS[index]); - number = number / targetBase; - } - chars.Reverse(); - return new string(chars.ToArray()); - } - - /// - /// 将给定的 Base62 编码字符串转换为整数。 - /// - /// 要转换的 Base62 编码字符串 - /// 转换后的整数 - public static long Decode(string str) - { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } - - long result = 0; - int sourceBase = BASE62_CHARS.Length; - long multiplier = 1; - for (int i = str.Length - 1; i >= 0; i--) - { - int digit = Array.IndexOf(BASE62_CHARS, str[i]); - if (digit == -1) - { - throw new ArgumentException("Invalid character in string: " + str[i], nameof(str)); - } - result += digit * multiplier; - multiplier *= sourceBase; - } - return result; - } - } -} diff --git a/EasyTool.Core/CodeCategory/Base64UrlUtil.cs b/EasyTool.Core/CodeCategory/Base64UrlUtil.cs new file mode 100644 index 0000000..25d02ee --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base64UrlUtil.cs @@ -0,0 +1,294 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base64URL 编码工具类 + /// Base64URL 是 URL 和文件名安全的 Base64 编码变体 + /// 使用 - 代替 +,_ 代替 /,通常省略填充 + /// 用于 JWT、URL 参数等场景 + /// RFC 4648 标准 + /// + public static class Base64UrlUtil + { + private const string Base64UrlChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + private const string Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + /// + /// 将字节数组编码为 Base64URL 字符串 + /// + /// 要编码的数据 + /// 是否添加填充 + /// Base64URL 编码字符串 + public static string Encode(byte[] data, bool padding = false) + { + if (data == null || data.Length == 0) + return string.Empty; + + // 先使用标准 Base64 编码 + string base64 = Convert.ToBase64String(data); + + // 转换为 Base64URL + var result = new StringBuilder(base64.Length); + foreach (char c in base64) + { + if (c == '+') + result.Append('-'); + else if (c == '/') + result.Append('_'); + else if (c == '=') + { + if (padding) + result.Append(c); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 将 Base64URL 字符串解码为字节数组 + /// + /// Base64URL 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + // 转换回标准 Base64 + var base64 = new StringBuilder(encoded.Length); + + foreach (char c in encoded) + { + if (c == '-') + base64.Append('+'); + else if (c == '_') + base64.Append('/'); + else + base64.Append(c); + } + + // 添加填充 + int padding = base64.Length % 4; + if (padding > 0) + { + base64.Append(new string('=', 4 - padding)); + } + + return Convert.FromBase64String(base64.ToString()); + } + + /// + /// 将字符串编码为 Base64URL(使用 UTF-8) + /// + /// 文本 + /// 是否添加填充 + /// Base64URL 编码字符串 + public static string EncodeString(string text, bool padding = false) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes, padding); + } + + /// + /// 将 Base64URL 字符串解码为文本(使用 UTF-8) + /// + /// Base64URL 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base64URL 字符串是否有效 + /// + /// Base64URL 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + // 检查长度 + if (encoded.Length % 4 == 1) + return false; + + // 检查字符 + foreach (char c in encoded) + { + if (c >= 'A' && c <= 'Z') continue; + if (c >= 'a' && c <= 'z') continue; + if (c >= '0' && c <= '9') continue; + if (c == '-' || c == '_') continue; + if (c == '=') + { + // 填充只能在末尾 + int index = encoded.IndexOf('='); + if (index < encoded.Length - 2) + return false; + continue; + } + + return false; + } + + return true; + } + + /// + /// 尝试解码 Base64URL 字符串 + /// + /// Base64URL 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 从标准 Base64 转换为 Base64URL + /// + /// 标准 Base64 字符串 + /// 是否移除填充 + /// Base64URL 字符串 + public static string FromBase64(string base64, bool removePadding = true) + { + if (string.IsNullOrEmpty(base64)) + return string.Empty; + + var result = new StringBuilder(base64.Length); + + foreach (char c in base64) + { + if (c == '+') + result.Append('-'); + else if (c == '/') + result.Append('_'); + else if (c == '=' && removePadding) + continue; + else + result.Append(c); + } + + return result.ToString(); + } + + /// + /// 从 Base64URL 转换为标准 Base64 + /// + /// Base64URL 字符串 + /// 标准 Base64 字符串 + public static string ToBase64(string base64Url) + { + if (string.IsNullOrEmpty(base64Url)) + return string.Empty; + + var result = new StringBuilder(base64Url); + + for (int i = 0; i < result.Length; i++) + { + if (result[i] == '-') + result[i] = '+'; + else if (result[i] == '_') + result[i] = '/'; + } + + // 添加填充 + int padding = result.Length % 4; + if (padding > 0) + { + result.Append(new string('=', 4 - padding)); + } + + return result.ToString(); + } + + /// + /// 计算 Base64URL 编码后的预计长度 + /// + /// 输入数据长度 + /// 是否包含填充 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength, bool includePadding = false) + { + if (inputLength == 0) + return 0; + + int length = (inputLength + 2) / 3 * 4; + + if (!includePadding) + { + // 移除填充 + int remainder = inputLength % 3; + if (remainder > 0) + length -= 3 - remainder; + } + + return length; + } + + /// + /// 计算解码后的预计长度 + /// + /// 编码字符串长度 + /// 预计解码后长度 + public static int CalculateDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return encodedLength * 3 / 4; + } + + /// + /// 比较两个 Base64URL 字符串是否相等(常量时间) + /// + /// 第一个字符串 + /// 第二个字符串 + /// 是否相等 + public static bool ConstantTimeEquals(string a, string b) + { + if (a == null || b == null) + return a == b; + + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base64Util.cs b/EasyTool.Core/CodeCategory/Base64Util.cs deleted file mode 100644 index 7a84f8c..0000000 --- a/EasyTool.Core/CodeCategory/Base64Util.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Base64 编码解码工具类 - /// - public static class Base64Util - { - // Base64 字符集,共 64 个字符 - private static readonly char[] BASE64_CHARS = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".ToCharArray(); - - // Base64 填充字符 - private const char BASE64_PADDING_CHAR = '='; - - /// - /// 将给定的字节数组转换为 Base64 编码字符串。 - /// - /// 要转换的字节数组 - /// 转换后的 Base64 编码字符串 - public static string Encode(byte[] bytes) - { - if (bytes == null) - { - throw new ArgumentNullException(nameof(bytes)); - } - - int length = bytes.Length; - if (length == 0) - { - return string.Empty; - } - - char[] chars = new char[(length + 2) / 3 * 4]; - int index = 0; - for (int i = 0; i < length; i += 3) - { - int val = (bytes[i] << 16) + ((i + 1 < length ? bytes[i + 1] : 0) << 8) + (i + 2 < length ? bytes[i + 2] : 0); - chars[index++] = BASE64_CHARS[(val >> 18) & 0x3F]; - chars[index++] = BASE64_CHARS[(val >> 12) & 0x3F]; - chars[index++] = BASE64_CHARS[(val >> 6) & 0x3F]; - chars[index++] = BASE64_CHARS[val & 0x3F]; - } - - // 添加填充字符 - int paddingCount = length % 3; - if (paddingCount > 0) - { - chars[chars.Length - 1] = BASE64_PADDING_CHAR; - if (paddingCount == 1) - { - chars[chars.Length - 2] = BASE64_PADDING_CHAR; - } - } - - return new string(chars); - } - - /// - /// 将给定的 Base64 编码字符串转换为字节数组。 - /// - /// 要转换的 Base64 编码字符串 - /// 转换后的字节数组 - public static byte[] Decode(string str) - { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentException("String is null or empty.", nameof(str)); - } - int length = str.Length; - if (length % 4 != 0) - { - throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); - } - - int paddingCount = 0; - if (length > 0 && str[length - 1] == BASE64_PADDING_CHAR) - { - paddingCount++; - } - if (length > 1 && str[length - 2] == BASE64_PADDING_CHAR) - { - paddingCount++; - } - - byte[] bytes = new byte[length / 4 * 3 - paddingCount]; - int index = 0; - for (int i = 0; i < length; i += 4) - { - int val = (DecodeBase64Char(str[i]) << 18) + - (DecodeBase64Char(str[i + 1]) << 12) + - (DecodeBase64Char(str[i + 2]) << 6) + - DecodeBase64Char(str[i + 3]); - bytes[index++] = (byte)(val >> 16); - if (index < bytes.Length) - { - bytes[index++] = (byte)(val >> 8); - } - if (index < bytes.Length) - { - bytes[index++] = (byte)val; - } - } - - return bytes; - } - - // 解码 Base64 字符 - private static int DecodeBase64Char(char c) - { - if (c >= 'A' && c <= 'Z') - { - return c - 'A'; - } - if (c >= 'a' && c <= 'z') - { - return c - 'a' + 26; - } - if (c >= '0' && c <= '9') - { - return c - '0' + 52; - } - if (c == '+') - { - return 62; - } - if (c == '/') - { - return 63; - } - throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); - } - } -} diff --git a/EasyTool.Core/CodeCategory/Base85Util.cs b/EasyTool.Core/CodeCategory/Base85Util.cs new file mode 100644 index 0000000..fcdefff --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base85Util.cs @@ -0,0 +1,437 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base85(Ascii85)编解码工具类 + /// Base85 是一种将二进制数据编码为ASCII字符的编码方式 + /// 每4个字节编码为5个字符,比Base64更高效 + /// + public static class Base85Util + { + // Ascii85 字符集 + private const string Ascii85Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; + + // Z85 字符集(ZeroMQ) + private const string Z85Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"; + + // RFC1924 字符集(IPv6地址) + private const string Rfc1924Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; + + // Adobe 分隔符 + private const string AdobePrefix = "<~"; + private const string AdobeSuffix = "~>"; + + #region Ascii85 编解码 + + /// + /// 将字节数组编码为 Ascii85 字符串 + /// + /// 要编码的字节数组 + /// Ascii85 编码字符串 + public static string Encode(byte[] data) + { + return Encode(data, false); + } + + /// + /// 将字节数组编码为 Ascii85 字符串 + /// + /// 要编码的字节数组 + /// 是否使用Adobe格式(添加 <~ 和 ~>) + /// Ascii85 编码字符串 + public static string Encode(byte[] data, bool useAdobeFormat) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + // 处理完整的4字节块 + int fullBlocks = data.Length / 4; + int remainder = data.Length % 4; + + for (int i = 0; i < fullBlocks; i++) + { + uint value = (uint)((data[i * 4] << 24) | + (data[i * 4 + 1] << 16) | + (data[i * 4 + 2] << 8) | + data[i * 4 + 3]); + + if (value == 0 && useAdobeFormat) + { + result.Append('z'); // 特殊压缩:4个零字节编码为 'z' + } + else + { + EncodeBlock(result, value, 5); + } + } + + // 处理剩余字节 + if (remainder > 0) + { + uint value = 0; + int padding = 4 - remainder; + + for (int i = 0; i < remainder; i++) + { + value = (value << 8) | data[fullBlocks * 4 + i]; + } + value <<= (padding * 8); + + EncodeBlock(result, value, remainder + 1); + } + + if (useAdobeFormat) + { + return AdobePrefix + result.ToString() + AdobeSuffix; + } + + return result.ToString(); + } + + /// + /// 将 Ascii85 字符串解码为字节数组 + /// + /// Ascii85 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string value) + { + return Decode(value, false); + } + + /// + /// 将 Ascii85 字符串解码为字节数组 + /// + /// Ascii85 编码字符串 + /// 是否为Adobe格式 + /// 解码后的字节数组 + public static byte[] Decode(string value, bool adobeFormat) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + // 移除 Adobe 格式的分隔符 + if (adobeFormat) + { + if (value.StartsWith(AdobePrefix)) + value = value.Substring(AdobePrefix.Length); + if (value.EndsWith(AdobeSuffix)) + value = value.Substring(0, value.Length - AdobeSuffix.Length); + } + + // 移除空白字符 + value = RemoveWhitespace(value); + + var result = new byte[CalculateDecodedLength(value)]; + int resultIndex = 0; + + int i = 0; + while (i < value.Length) + { + if (value[i] == 'z') + { + // 'z' 表示4个零字节 + result[resultIndex++] = 0; + result[resultIndex++] = 0; + result[resultIndex++] = 0; + result[resultIndex++] = 0; + i++; + } + else if (value[i] == 'y') + { + // 'y' 表示4个空格字节(某些变体) + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + result[resultIndex++] = 0x20; + i++; + } + else + { + // 处理5字符块 + int blockLength = Math.Min(5, value.Length - i); + uint decoded = DecodeBlock(value, i, blockLength); + + int bytesToWrite = blockLength - 1; + for (int j = 3; j >= 4 - bytesToWrite; j--) + { + result[resultIndex++] = (byte)((decoded >> (j * 8)) & 0xFF); + } + + i += blockLength; + } + } + + // 调整数组大小 + if (resultIndex < result.Length) + { + Array.Resize(ref result, resultIndex); + } + + return result; + } + + private static void EncodeBlock(StringBuilder result, uint value, int chars) + { + for (int i = chars - 1; i >= 0; i--) + { + result.Append((char)(value % 85 + 33)); + value /= 85; + } + } + + private static uint DecodeBlock(string value, int offset, int length) + { + uint result = 0; + for (int i = 0; i < length; i++) + { + char c = value[offset + i]; + if (c < 33 || c > 117) + { + throw new ArgumentException($"Invalid Ascii85 character: {c}", nameof(value)); + } + result = result * 85 + (uint)(c - 33); + } + + // 填充剩余字符的影响 + for (int i = length; i < 5; i++) + { + result = result * 85 + 84; // 'u' - 33 = 84 + } + + return result; + } + + private static int CalculateDecodedLength(string value) + { + int length = 0; + for (int i = 0; i < value.Length; i++) + { + if (value[i] == 'z' || value[i] == 'y') + { + length += 4; + } + else if (value[i] >= 33 && value[i] <= 117) + { + length++; + } + } + return (length / 5) * 4 + 4; // 估算,后续会调整 + } + + private static string RemoveWhitespace(string value) + { + var result = new StringBuilder(value.Length); + foreach (char c in value) + { + if (!char.IsWhiteSpace(c)) + { + result.Append(c); + } + } + return result.ToString(); + } + + #endregion + + #region Z85 编解码 + + private static readonly int[] Z85DecodeMap = BuildZ85DecodeMap(); + + private static int[] BuildZ85DecodeMap() + { + var map = new int[256]; + for (int i = 0; i < 256; i++) + map[i] = -1; + for (int i = 0; i < Z85Chars.Length; i++) + map[Z85Chars[i]] = i; + return map; + } + + /// + /// 将字节数组编码为 Z85 字符串 + /// + /// 要编码的字节数组(长度必须是4的倍数) + /// Z85 编码字符串 + public static string EncodeZ85(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + if (data.Length % 4 != 0) + throw new ArgumentException("Data length must be a multiple of 4 for Z85 encoding", nameof(data)); + + var result = new StringBuilder(data.Length * 5 / 4); + + for (int i = 0; i < data.Length; i += 4) + { + uint value = (uint)((data[i] << 24) | + (data[i + 1] << 16) | + (data[i + 2] << 8) | + data[i + 3]); + + char[] block = new char[5]; + for (int j = 4; j >= 0; j--) + { + block[j] = Z85Chars[(int)(value % 85)]; + value /= 85; + } + result.Append(block); + } + + return result.ToString(); + } + + /// + /// 将 Z85 字符串解码为字节数组 + /// + /// Z85 编码字符串(长度必须是5的倍数) + /// 解码后的字节数组 + public static byte[] DecodeZ85(string value) + { + if (string.IsNullOrEmpty(value)) + return Array.Empty(); + + if (value.Length % 5 != 0) + throw new ArgumentException("String length must be a multiple of 5 for Z85 decoding", nameof(value)); + + var result = new byte[value.Length * 4 / 5]; + int resultIndex = 0; + + for (int i = 0; i < value.Length; i += 5) + { + uint decoded = 0; + for (int j = 0; j < 5; j++) + { + int index = Z85DecodeMap[value[i + j]]; + if (index < 0) + throw new ArgumentException($"Invalid Z85 character: {value[i + j]}", nameof(value)); + decoded = decoded * 85 + (uint)index; + } + + result[resultIndex++] = (byte)((decoded >> 24) & 0xFF); + result[resultIndex++] = (byte)((decoded >> 16) & 0xFF); + result[resultIndex++] = (byte)((decoded >> 8) & 0xFF); + result[resultIndex++] = (byte)(decoded & 0xFF); + } + + return result; + } + + #endregion + + #region 通用方法 + + /// + /// 将字符串编码为 Ascii85 + /// + /// 要编码的字符串 + /// 编码方式(默认UTF-8) + /// Ascii85 编码字符串 + public static string EncodeString(string text, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return Encode(encoding.GetBytes(text)); + } + + /// + /// 将 Ascii85 字符串解码为原始字符串 + /// + /// Ascii85 编码字符串 + /// 编码方式(默认UTF-8) + /// 解码后的字符串 + public static string DecodeString(string value, Encoding encoding = null) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = Decode(value); + return encoding.GetString(bytes); + } + + /// + /// 验证是否是有效的 Ascii85 字符串 + /// + /// 要验证的字符串 + /// 是否有效 + public static bool IsValid(string value) + { + if (string.IsNullOrEmpty(value)) + return false; + + // 移除 Adobe 格式的分隔符 + if (value.StartsWith(AdobePrefix)) + value = value.Substring(AdobePrefix.Length); + if (value.EndsWith(AdobeSuffix)) + value = value.Substring(0, value.Length - AdobeSuffix.Length); + + value = RemoveWhitespace(value); + + foreach (char c in value) + { + if (c != 'z' && c != 'y' && (c < 33 || c > 117)) + return false; + } + + return true; + } + + /// + /// 尝试解码 Ascii85 字符串 + /// + /// Ascii85 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string value, out byte[] bytes) + { + bytes = null; + if (!IsValid(value)) + return false; + + try + { + bytes = Decode(value); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算编码后的长度 + /// + /// 原始数据长度 + /// 编码后长度 + public static int GetEncodedLength(int dataLength) + { + if (dataLength == 0) + return 0; + + return (dataLength + 3) / 4 * 5; + } + + /// + /// 计算解码后的最大长度 + /// + /// 编码后长度 + /// 解码后最大长度 + public static int GetMaxDecodedLength(int encodedLength) + { + if (encodedLength == 0) + return 0; + + return encodedLength / 5 * 4 + 4; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Base91Util.cs b/EasyTool.Core/CodeCategory/Base91Util.cs new file mode 100644 index 0000000..b1fbfa1 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base91Util.cs @@ -0,0 +1,216 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base91 编码工具类 + /// Base91 是一种二进制到文本的编码方案,比 Base64 更高效 + /// 编码效率:约 23%(比 Base64 的 33% 更低开销) + /// + public static class Base91Util + { + // Base91 字符集 + private const string Base91Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~\""; + + // 解码映射表 + private static readonly int[] DecodeMap; + + static Base91Util() + { + DecodeMap = new int[256]; + for (int i = 0; i < 256; i++) + { + DecodeMap[i] = -1; + } + for (int i = 0; i < Base91Chars.Length; i++) + { + DecodeMap[Base91Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base91 字符串 + /// + /// 要编码的数据 + /// Base91 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + int b = 0; + int n = 0; + + foreach (byte c in data) + { + b |= c << n; + n += 8; + + if (n > 13) + { + int v = b & 8191; + if (v > 88) + { + b >>= 13; + n -= 13; + } + else + { + v = b & 16383; + b >>= 14; + n -= 14; + } + result.Append(Base91Chars[v % 91]); + result.Append(Base91Chars[v / 91]); + } + } + + if (n > 0) + { + result.Append(Base91Chars[b % 91]); + if (n > 7 || b > 90) + { + result.Append(Base91Chars[b / 91]); + } + } + + return result.ToString(); + } + + /// + /// 将 Base91 字符串解码为字节数组 + /// + /// Base91 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + int b = 0; + int n = 0; + int v = -1; + + foreach (char c in encoded) + { + if (c >= 256 || DecodeMap[c] == -1) + continue; + + if (v == -1) + { + v = DecodeMap[c]; + } + else + { + v += DecodeMap[c] * 91; + b |= v << n; + n += (v & 8191) > 88 ? 13 : 14; + + while (n > 7) + { + result.Add((byte)(b & 255)); + b >>= 8; + n -= 8; + } + + v = -1; + } + } + + if (v != -1) + { + result.Add((byte)((b | v << n) & 255)); + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base91(使用 UTF-8) + /// + /// 要编码的文本 + /// Base91 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base91 字符串解码为文本(使用 UTF-8) + /// + /// Base91 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base91 字符串是否有效 + /// + /// Base91 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + foreach (char c in encoded) + { + if (c >= 256 || DecodeMap[c] == -1) + return false; + } + + return true; + } + + /// + /// 尝试解码 Base91 字符串 + /// + /// Base91 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base91 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // Base91 编码效率约为 23% + return (int)Math.Ceiling(inputLength * 1.23); + } + } +} diff --git a/EasyTool.Core/CodeCategory/Base92Util.cs b/EasyTool.Core/CodeCategory/Base92Util.cs new file mode 100644 index 0000000..8c1e724 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Base92Util.cs @@ -0,0 +1,263 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Base92 编码工具类 + /// Base92 是一种二进制到文本的编码方案,比 Base85 更高效 + /// 使用 92 个可打印 ASCII 字符 + /// + public static class Base92Util + { + // Base92 字符集 + private const string Base92Chars = "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~\""; + + // 解码映射表 + private static readonly int[] DecodeMap; + + static Base92Util() + { + DecodeMap = new int[256]; + for (int i = 0; i < 256; i++) + { + DecodeMap[i] = -1; + } + for (int i = 0; i < Base92Chars.Length; i++) + { + DecodeMap[Base92Chars[i]] = i; + } + } + + /// + /// 将字节数组编码为 Base92 字符串 + /// + /// 要编码的数据 + /// Base92 编码字符串 + public static string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return "~"; + + var result = new StringBuilder(); + int bitBuffer = 0; + int bitsInBuffer = 0; + + foreach (byte b in data) + { + bitBuffer = (bitBuffer << 8) | b; + bitsInBuffer += 8; + + while (bitsInBuffer >= 13) + { + int value = (bitBuffer >> (bitsInBuffer - 13)) & 0x1FFF; + + if (value < 91) + { + result.Append(Base92Chars[value]); + bitsInBuffer -= 13; + } + else + { + value -= 91; + result.Append(Base92Chars[value / 91 + 91]); + result.Append(Base92Chars[value % 91]); + bitsInBuffer -= 14; + } + } + } + + // 处理剩余位 + if (bitsInBuffer > 0) + { + int value = (bitBuffer << (13 - bitsInBuffer)) & 0x1FFF; + + if (value < 91) + { + result.Append(Base92Chars[value]); + } + else + { + value -= 91; + result.Append(Base92Chars[value / 91 + 91]); + result.Append(Base92Chars[value % 91]); + } + } + + result.Append('~'); + + return result.ToString(); + } + + /// + /// 将 Base92 字符串解码为字节数组 + /// + /// Base92 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + if (!encoded.EndsWith("~")) + throw new ArgumentException("Invalid Base92 string: must end with ~", nameof(encoded)); + + string data = encoded.TrimEnd('~'); + if (string.IsNullOrEmpty(data)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + int bitBuffer = 0; + int bitsInBuffer = 0; + + int i = 0; + while (i < data.Length) + { + int value; + + int c1 = data[i] < 256 ? DecodeMap[data[i]] : -1; + if (c1 < 0) + throw new ArgumentException($"Invalid Base92 character: {data[i]}", nameof(encoded)); + + if (c1 < 91) + { + value = c1; + i++; + } + else + { + if (i + 1 >= data.Length) + throw new ArgumentException("Invalid Base92 string: unexpected end", nameof(encoded)); + + int c2 = data[i + 1] < 256 ? DecodeMap[data[i + 1]] : -1; + if (c2 < 0) + throw new ArgumentException($"Invalid Base92 character: {data[i + 1]}", nameof(encoded)); + + value = (c1 - 91) * 91 + c2 + 91; + i += 2; + } + + bitBuffer = (bitBuffer << 13) | value; + bitsInBuffer += 13; + + while (bitsInBuffer >= 8) + { + bitsInBuffer -= 8; + result.Add((byte)((bitBuffer >> bitsInBuffer) & 0xFF)); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Base92(使用 UTF-8) + /// + /// 要编码的文本 + /// Base92 编码字符串 + public static string EncodeString(string text) + { + if (string.IsNullOrEmpty(text)) + return "~"; + + byte[] bytes = Encoding.UTF8.GetBytes(text); + return Encode(bytes); + } + + /// + /// 将 Base92 字符串解码为文本(使用 UTF-8) + /// + /// Base92 编码字符串 + /// 解码后的文本 + public static string DecodeToString(string encoded) + { + if (string.IsNullOrEmpty(encoded) || encoded == "~") + return string.Empty; + + byte[] bytes = Decode(encoded); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 验证 Base92 字符串是否有效 + /// + /// Base92 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + if (!encoded.EndsWith("~")) + return false; + + string data = encoded.TrimEnd('~'); + if (string.IsNullOrEmpty(data)) + return true; + + int i = 0; + while (i < data.Length) + { + int c1 = data[i] < 256 ? DecodeMap[data[i]] : -1; + if (c1 < 0) + return false; + + if (c1 < 91) + { + i++; + } + else + { + if (i + 1 >= data.Length) + return false; + + int c2 = data[i + 1] < 256 ? DecodeMap[data[i + 1]] : -1; + if (c2 < 0) + return false; + + i += 2; + } + } + + return true; + } + + /// + /// 尝试解码 Base92 字符串 + /// + /// Base92 编码字符串 + /// 解码后的字节数组 + /// 是否解码成功 + public static bool TryDecode(string encoded, out byte[] result) + { + result = null; + + if (!IsValid(encoded)) + return false; + + try + { + result = Decode(encoded); + return true; + } + catch + { + return false; + } + } + + /// + /// 计算 Base92 编码后的预计长度 + /// + /// 输入数据长度 + /// 预计输出长度 + public static int CalculateEncodedLength(int inputLength) + { + if (inputLength == 0) + return 1; + + // Base92 编码效率约为 16/13 + return (int)Math.Ceiling(inputLength * 16.0 / 13.0) + 1; + } + } +} diff --git a/EasyTool.Core/CodeCategory/BaudotUtil.cs b/EasyTool.Core/CodeCategory/BaudotUtil.cs new file mode 100644 index 0000000..7d3ecdd --- /dev/null +++ b/EasyTool.Core/CodeCategory/BaudotUtil.cs @@ -0,0 +1,315 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Baudot 编码工具类 + /// Baudot 码(也称为 Murrey 码或 ITA2)是一种5位字符编码 + /// 用于电报和 TTY 通信,支持字母和数字两个字符集 + /// + public static class BaudotUtil + { + // 字母表模式(LTRS - Letters) + private static readonly char[] LettersMode = new char[] + { + '\0', 'E', '\n', 'A', ' ', 'S', 'I', 'U', + '\r', 'D', 'R', 'J', 'N', 'F', 'C', 'K', + 'T', 'Z', 'L', 'W', 'H', 'Y', 'P', 'Q', + 'O', 'B', 'G', '\0', 'M', 'X', 'V', '\0' + }; + + // 数字/符号模式(FIGS - Figures) + private static readonly char[] FiguresMode = new char[] + { + '\0', '3', '\n', '-', ' ', '\'', '8', '7', + '\r', '$', '4', '\a', ',', '!', ':', '(', + '5', '+', ')', '2', '$', '6', '0', '1', + '9', '?', '=', '\0', '.', '/', '=', '\0' + }; + + // 切换到字母表的代码 + private const byte LTRS = 0x1F; + + // 切换到数字表的代码 + private const byte FIGS = 0x1B; + + /// + /// 将文本编码为 Baudot 码字节数组 + /// + /// 要编码的文本 + /// Baudot 码字节数组 + public static byte[] Encode(string text) + { + if (string.IsNullOrEmpty(text)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + bool inLettersMode = true; + + foreach (char c in text.ToUpperInvariant()) + { + // 在字母表中查找 + int lettersIndex = Array.IndexOf(LettersMode, c); + int figuresIndex = Array.IndexOf(FiguresMode, c); + + if (lettersIndex >= 0 && figuresIndex >= 0) + { + // 字符在两个表中都存在,保持当前模式 + result.Add((byte)lettersIndex); + } + else if (lettersIndex >= 0) + { + // 只在字母表中 + if (!inLettersMode) + { + result.Add(LTRS); + inLettersMode = true; + } + result.Add((byte)lettersIndex); + } + else if (figuresIndex >= 0) + { + // 只在数字表中 + if (inLettersMode) + { + result.Add(FIGS); + inLettersMode = false; + } + result.Add((byte)figuresIndex); + } + // 忽略不支持的字符 + } + + return result.ToArray(); + } + + /// + /// 将 Baudot 码字节数组解码为文本 + /// + /// Baudot 码字节数组 + /// 解码后的文本 + public static string Decode(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + bool inLettersMode = true; + + foreach (byte b in data) + { + if (b == LTRS) + { + inLettersMode = true; + } + else if (b == FIGS) + { + inLettersMode = false; + } + else + { + char[] table = inLettersMode ? LettersMode : FiguresMode; + if (b < table.Length) + { + char c = table[b]; + if (c != '\0') + { + result.Append(c); + } + } + } + } + + return result.ToString(); + } + + /// + /// 将 Baudot 码编码为二进制字符串表示 + /// + /// Baudot 码字节数组 + /// 二进制字符串数组 + public static string[] ToBinaryStrings(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + var result = new string[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = Convert.ToString(data[i] & 0x1F, 2).PadLeft(5, '0'); + } + + return result; + } + + /// + /// 从二进制字符串创建 Baudot 码 + /// + /// 二进制字符串数组 + /// Baudot 码字节数组 + public static byte[] FromBinaryStrings(string[] binaryStrings) + { + if (binaryStrings == null || binaryStrings.Length == 0) + return Array.Empty(); + + var result = new byte[binaryStrings.Length]; + for (int i = 0; i < binaryStrings.Length; i++) + { + result[i] = (byte)Convert.ToByte(binaryStrings[i], 2); + } + + return result; + } + + /// + /// 将 Baudot 码编码为十六进制字符串 + /// + /// Baudot 码字节数组 + /// 十六进制字符串 + public static string ToHexString(byte[] data) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + foreach (byte b in data) + { + result.Append((b & 0x1F).ToString("X2")); + } + + return result.ToString(); + } + + /// + /// 从十六进制字符串解码 Baudot 码 + /// + /// 十六进制字符串 + /// Baudot 码字节数组 + public static byte[] FromHexString(string hex) + { + if (string.IsNullOrEmpty(hex) + || hex.Length % 2 != 0) + return Array.Empty(); + + var result = new byte[hex.Length / 2]; + for (int i = 0; i < result.Length; i++) + { + result[i] = (byte)(Convert.ToByte(hex.Substring(i * 2, 2), 16) & 0x1F); + } + + return result; + } + + /// + /// 验证文本是否可以用 Baudot 码表示 + /// + /// 文本 + /// 是否可以编码 + public static bool CanEncode(string text) + { + if (string.IsNullOrEmpty(text)) + return true; + + foreach (char c in text.ToUpperInvariant()) + { + if (Array.IndexOf(LettersMode, c) < 0 && + Array.IndexOf(FiguresMode, c) < 0) + { + return false; + } + } + + return true; + } + + /// + /// 获取不支持的字符 + /// + /// 文本 + /// 不支持的字符数组 + public static char[] GetUnsupportedChars(string text) + { + if (string.IsNullOrEmpty(text)) + return Array.Empty(); + + var unsupported = new System.Collections.Generic.List(); + + foreach (char c in text.ToUpperInvariant()) + { + if (Array.IndexOf(LettersMode, c) < 0 && + Array.IndexOf(FiguresMode, c) < 0 && + !unsupported.Contains(c)) + { + unsupported.Add(c); + } + } + + return unsupported.ToArray(); + } + + /// + /// 获取字母表模式字符 + /// + /// Baudot 码(0-31) + /// 字符,如果无效则返回 '\0' + public static char GetLettersChar(byte code) + { + if (code > 31) + return '\0'; + + return LettersMode[code]; + } + + /// + /// 获取数字模式字符 + /// + /// Baudot 码(0-31) + /// 字符,如果无效则返回 '\0' + public static char GetFiguresChar(byte code) + { + if (code > 31) + return '\0'; + + return FiguresMode[code]; + } + + /// + /// 获取字符的 Baudot 码(字母模式) + /// + /// 字符 + /// Baudot 码,如果不存在则返回 -1 + public static int GetLettersCode(char c) + { + return Array.IndexOf(LettersMode, char.ToUpperInvariant(c)); + } + + /// + /// 获取字符的 Baudot 码(数字模式) + /// + /// 字符 + /// Baudot 码,如果不存在则返回 -1 + public static int GetFiguresCode(char c) + { + return Array.IndexOf(FiguresMode, c); + } + + /// + /// 获取完整的字母表 + /// + /// 字母表字符数组 + public static char[] GetLettersTable() + { + return (char[])LettersMode.Clone(); + } + + /// + /// 获取完整的数字表 + /// + /// 数字表字符数组 + public static char[] GetFiguresTable() + { + return (char[])FiguresMode.Clone(); + } + } +} diff --git a/EasyTool.Core/CodeCategory/BcryptUtil.cs b/EasyTool.Core/CodeCategory/BcryptUtil.cs new file mode 100644 index 0000000..e65ff1a --- /dev/null +++ b/EasyTool.Core/CodeCategory/BcryptUtil.cs @@ -0,0 +1,617 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Bcrypt 密码哈希工具类 + /// Bcrypt 是一种专为密码存储设计的哈希算法,内置盐值和工作因子 + /// + public static class BcryptUtil + { + // Bcrypt Base64 字符集 + private const string Base64Chars = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + // 默认工作因子 + private const int DefaultCost = 12; + + // 盐值长度(16字节) + private const int SaltLength = 16; + + // 密码最大长度(72字节) + private const int MaxPasswordLength = 72; + + // Blowfish 初始 P 数组 + private static readonly uint[] InitialP = new uint[] + { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, + 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, + 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, + 0x9216d5d9, 0x8979fb1b + }; + + // Blowfish 初始 S 盒 + private static readonly uint[] InitialS = new uint[] + { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, + 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, + 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, + 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, + 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, + 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, + 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, + 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, + 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, + 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, + 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, + 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, + 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, + 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, + 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, + 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, + 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, + 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, + 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, + 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, + 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, + 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, + 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, + 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, + 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, + 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, + 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, + 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, + 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, + 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, + 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, + 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, + 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, + 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, + 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, + 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, + 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, + 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, + 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, + 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, + 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, + 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, + 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, + 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, + 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, + 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, + 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, + 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, + 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, + 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, + 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, + 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, + 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, + 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, + 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, + 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, + 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, + 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, + 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, + 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, + 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, + 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, + 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, + 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, + 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, + 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, + 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, + 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, + 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, + 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, + 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, + 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, + 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, + 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, + 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, + 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, + 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, + 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, + 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, + 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, + 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, + 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, + 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, + 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, + 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, + 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, + 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, + 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, + 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, + 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, + 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, + 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, + 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, + 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, + 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, + 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, + 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, + 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, + 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, + 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, + 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, + 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, + 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, + 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, + 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, + 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, + 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, + 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, + 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, + 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, + 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, + 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, + 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, + 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, + 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, + 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, + 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, + 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, + 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, + 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, + 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, + 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, + 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, + 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, + 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, + 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, + 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, + 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, + 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, + 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, + 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, + 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, + 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, + 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, + 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, + 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, + 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, + 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, + 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, + 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, + 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, + 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, + 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, + 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, + 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, + 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, + 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, + 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, + 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, + 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, + 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, + 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, + 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, + 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, + 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, + 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, + 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, + 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, + 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, + 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, + 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, + 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, + 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, + 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, + 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, + 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, + 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, + 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, + 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 + }; + + /// + /// 使用 Bcrypt 哈希密码 + /// + /// 密码 + /// 工作因子(4-31,默认12) + /// 哈希后的密码字符串 + public static string Hash(string password, int cost = DefaultCost) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (cost < 4 || cost > 31) + throw new ArgumentException("Cost must be between 4 and 31", nameof(cost)); + + // 生成随机盐值 + byte[] salt = new byte[SaltLength]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + + return HashInternal(password, salt, cost); + } + + /// + /// 使用 Bcrypt 哈希密码(使用指定盐值) + /// + /// 密码 + /// 盐值(16字节) + /// 工作因子 + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt, int cost = DefaultCost) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length != SaltLength) + throw new ArgumentException($"Salt must be {SaltLength} bytes", nameof(salt)); + + if (cost < 4 || cost > 31) + throw new ArgumentException("Cost must be between 4 and 31", nameof(cost)); + + return HashInternal(password, salt, cost); + } + + /// + /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + // 解析哈希字符串 + var (cost, salt, expectedHash) = ParseHash(hash); + if (cost == 0 || salt == null || expectedHash == null) + return false; + + // 计算新哈希 + string computedHash = HashInternal(password, salt, cost); + + // 常量时间比较 + return ConstantTimeEquals(hash, computedHash); + } + catch + { + return false; + } + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的工作因子 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int newCost = DefaultCost) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (cost, _, _) = ParseHash(hash); + return cost != newCost; + } + catch + { + return true; + } + } + + /// + /// 获取哈希的工作因子 + /// + /// 哈希字符串 + /// 工作因子 + public static int GetCost(string hash) + { + if (string.IsNullOrEmpty(hash)) + throw new ArgumentException("Hash cannot be null or empty", nameof(hash)); + + var (cost, _, _) = ParseHash(hash); + return cost; + } + + #region 私有方法 + + private static string HashInternal(string password, byte[] salt, int cost) + { + // 限制密码长度 + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + if (passwordBytes.Length > MaxPasswordLength) + { + Array.Resize(ref passwordBytes, MaxPasswordLength); + } + + // Eksblowfish 密钥调度 + uint[] P = new uint[18]; + uint[] S = new uint[1024]; + Array.Copy(InitialP, P, 18); + Array.Copy(InitialS, S, 1024); + + // 使用密码扩展密钥 + byte[] key = new byte[passwordBytes.Length + 1]; + Array.Copy(passwordBytes, key, passwordBytes.Length); + key[passwordBytes.Length] = 0; + + P = ExpandKey(P, S, key); + + // 使用盐值扩展密钥 + P = ExpandKey(P, S, salt); + + // Expensive key setup + for (int i = 0; i < (1 << cost); i++) + { + P = ExpandKey(P, S, key); + P = ExpandKey(P, S, salt); + } + + // 加密 "OrpheanBeholderScryDoubt" 64次 + byte[] magic = Encoding.ASCII.GetBytes("OrpheanBeholderScryDoubt"); + byte[] hash = EncryptECB(P, S, magic, 64); + + // 构建输出字符串 + string saltBase64 = EncodeBase64(salt); + string hashBase64 = EncodeBase64(hash, 23); // 只使用23字节 + + return $"$2a${cost:D2}${saltBase64}{hashBase64}"; + } + + private static uint[] ExpandKey(uint[] P, uint[] S, byte[] data) + { + int offset = 0; + + for (int i = 0; i < 18; i++) + { + uint d = ((uint)data[offset % data.Length] << 24) | + ((uint)data[(offset + 1) % data.Length] << 16) | + ((uint)data[(offset + 2) % data.Length] << 8) | + data[(offset + 3) % data.Length]; + P[i] ^= d; + offset += 4; + } + + uint L = 0, R = 0; + + for (int i = 0; i < 18; i += 2) + { + EncryptBlock(ref L, ref R, P, S); + P[i] = L; + P[i + 1] = R; + } + + for (int i = 0; i < 1024; i += 2) + { + EncryptBlock(ref L, ref R, P, S); + S[i] = L; + S[i + 1] = R; + } + + return P; + } + + private static void EncryptBlock(ref uint L, ref uint R, uint[] P, uint[] S) + { + L ^= P[0]; + for (int i = 1; i <= 16; i++) + { + R ^= ((S[(L >> 24) & 0xFF] + S[256 + ((L >> 16) & 0xFF)]) ^ + S[512 + ((L >> 8) & 0xFF)]) + S[768 + (L & 0xFF)]; + R ^= P[i]; + + if (i < 16) + { + uint temp = L; + L = R; + R = temp; + } + } + R ^= P[17]; + } + + private static byte[] EncryptECB(uint[] P, uint[] S, byte[] data, int rounds) + { + byte[] result = new byte[data.Length]; + Array.Copy(data, result, data.Length); + + for (int r = 0; r < rounds; r++) + { + for (int i = 0; i < result.Length; i += 8) + { + uint L = ((uint)result[i] << 24) | ((uint)result[i + 1] << 16) | + ((uint)result[i + 2] << 8) | result[i + 3]; + uint R = ((uint)result[i + 4] << 24) | ((uint)result[i + 5] << 16) | + ((uint)result[i + 6] << 8) | result[i + 7]; + + EncryptBlock(ref L, ref R, P, S); + + result[i] = (byte)(L >> 24); + result[i + 1] = (byte)(L >> 16); + result[i + 2] = (byte)(L >> 8); + result[i + 3] = (byte)L; + result[i + 4] = (byte)(R >> 24); + result[i + 5] = (byte)(R >> 16); + result[i + 6] = (byte)(R >> 8); + result[i + 7] = (byte)R; + } + } + + return result; + } + + private static (int cost, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (string.IsNullOrEmpty(hash) || !hash.StartsWith("$2")) + return (0, null, null); + + string[] parts = hash.Split('$'); + if (parts.Length < 4) + return (0, null, null); + + // 解析版本和成本 + string version = parts[1]; + if (!int.TryParse(parts[2], out int cost)) + return (0, null, null); + + // 解析盐值和哈希 + string saltAndHash = parts[3]; + if (saltAndHash.Length < 31) + return (0, null, null); + + byte[] salt = DecodeBase64(saltAndHash.Substring(0, 22)); + byte[] expectedHash = DecodeBase64(saltAndHash.Substring(22)); + + return (cost, salt, expectedHash); + } + + private static string EncodeBase64(byte[] data, int length = -1) + { + if (length == -1) + length = data.Length; + + var result = new StringBuilder(); + int i = 0; + + while (i < length) + { + uint b1 = i < length ? (uint)data[i++] : 0; + uint b2 = i < length ? (uint)data[i++] : 0; + uint b3 = i < length ? (uint)data[i++] : 0; + + result.Append(Base64Chars[(int)(b1 >> 2)]); + result.Append(Base64Chars[(int)(((b1 & 0x03) << 4) | (b2 >> 4))]); + result.Append(Base64Chars[(int)(((b2 & 0x0f) << 2) | (b3 >> 6))]); + result.Append(Base64Chars[(int)(b3 & 0x3f)]); + } + + return result.ToString(); + } + + private static byte[] DecodeBase64(string data) + { + var result = new List(); + int i = 0; + + while (i < data.Length) + { + uint c1 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c2 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c3 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + uint c4 = i < data.Length ? (uint)Base64Chars.IndexOf(data[i++]) : 0; + + result.Add((byte)((c1 << 2) | (c2 >> 4))); + result.Add((byte)(((c2 & 0x0f) << 4) | (c3 >> 2))); + result.Add((byte)(((c3 & 0x03) << 6) | c4)); + } + + return result.ToArray(); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Blake2Util.cs b/EasyTool.Core/CodeCategory/Blake2Util.cs new file mode 100644 index 0000000..ddb823e --- /dev/null +++ b/EasyTool.Core/CodeCategory/Blake2Util.cs @@ -0,0 +1,569 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// BLAKE2 哈希工具类 + /// BLAKE2 是一种快速、安全的加密哈希函数 + /// 比 MD5、SHA-1、SHA-2 更快,同时提供更高的安全性 + /// 包含 BLAKE2b(64位优化)和 BLAKE2s(32位优化)两个版本 + /// + public static class Blake2Util + { + #region BLAKE2b + + /// + /// 计算 BLAKE2b-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeBlake2b256(byte[] data) + { + return ComputeBlake2b(data, 32); + } + + /// + /// 计算 BLAKE2b-384 哈希值 + /// + /// 输入数据 + /// 48字节哈希值 + public static byte[] ComputeBlake2b384(byte[] data) + { + return ComputeBlake2b(data, 48); + } + + /// + /// 计算 BLAKE2b-512 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] ComputeBlake2b512(byte[] data) + { + return ComputeBlake2b(data, 64); + } + + /// + /// 计算指定长度的 BLAKE2b 哈希值 + /// + /// 输入数据 + /// 哈希长度(1-64字节) + /// 指定长度的哈希值 + public static byte[] ComputeBlake2b(byte[] data, int hashLength) + { + return ComputeBlake2b(data, 0, data?.Length ?? 0, null, null, hashLength); + } + + /// + /// 使用密钥计算 BLAKE2b 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(最多64字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeBlake2bWithKey(byte[] data, byte[] key, int hashLength = 64) + { + return ComputeBlake2b(data, 0, data?.Length ?? 0, key, null, hashLength); + } + + /// + /// 计算 BLAKE2b 哈希值(完整参数) + /// + public static byte[] ComputeBlake2b(byte[] data, int offset, int length, byte[] key, byte[] salt, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1 || hashLength > 64) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be between 1 and 64 bytes"); + if (key != null && key.Length > 64) + throw new ArgumentException("Key must be at most 64 bytes", nameof(key)); + + var hasher = new Blake2bHasher(key, salt, hashLength); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 BLAKE2b-256 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeBlake2b256String(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeBlake2b256(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeBlake2b256(data); + } + + /// + /// 获取 BLAKE2b-512 哈希的十六进制表示 + /// + /// 输入数据 + /// 128字符的十六进制字符串 + public static string ComputeBlake2b512Hex(byte[] data) + { + byte[] hash = ComputeBlake2b512(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + #endregion + + #region BLAKE2s + + /// + /// 计算 BLAKE2s-128 哈希值 + /// + /// 输入数据 + /// 16字节哈希值 + public static byte[] ComputeBlake2s128(byte[] data) + { + return ComputeBlake2s(data, 16); + } + + /// + /// 计算 BLAKE2s-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeBlake2s256(byte[] data) + { + return ComputeBlake2s(data, 32); + } + + /// + /// 计算指定长度的 BLAKE2s 哈希值 + /// + /// 输入数据 + /// 哈希长度(1-32字节) + /// 指定长度的哈希值 + public static byte[] ComputeBlake2s(byte[] data, int hashLength) + { + return ComputeBlake2s(data, 0, data?.Length ?? 0, null, null, hashLength); + } + + /// + /// 使用密钥计算 BLAKE2s 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(最多32字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeBlake2sWithKey(byte[] data, byte[] key, int hashLength = 32) + { + return ComputeBlake2s(data, 0, data?.Length ?? 0, key, null, hashLength); + } + + /// + /// 计算 BLAKE2s 哈希值(完整参数) + /// + public static byte[] ComputeBlake2s(byte[] data, int offset, int length, byte[] key, byte[] salt, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1 || hashLength > 32) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be between 1 and 32 bytes"); + if (key != null && key.Length > 32) + throw new ArgumentException("Key must be at most 32 bytes", nameof(key)); + + var hasher = new Blake2sHasher(key, salt, hashLength); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 BLAKE2s-256 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeBlake2s256String(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeBlake2s256(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeBlake2s256(data); + } + + /// + /// 获取 BLAKE2s-256 哈希的十六进制表示 + /// + /// 输入数据 + /// 64字符的十六进制字符串 + public static string ComputeBlake2s256Hex(byte[] data) + { + byte[] hash = ComputeBlake2s256(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成适合 BLAKE2b 的随机密钥 + /// + /// 密钥长度(最多64字节) + /// 随机密钥 + public static byte[] GenerateKey(int length = 32) + { + if (length < 1 || length > 64) + throw new ArgumentOutOfRangeException(nameof(length), "Key length must be between 1 and 64 bytes"); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成密钥并返回十六进制 + /// + /// 密钥长度 + /// 十六进制密钥字符串 + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #endregion + } + + #region BLAKE2b 实现类 + + internal class Blake2bHasher + { + private static readonly ulong[] IV = new ulong[] + { + 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, + 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 + }; + + private static readonly int[] Sigma = new int[] + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, + 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, + 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, + 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, + 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, + 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, + 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10, + 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5, + 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 + }; + + private ulong[] h = new ulong[8]; + private ulong[] m = new ulong[16]; + private byte[] buffer = new byte[128]; + private int bufferLength; + private ulong totalLength; + private readonly int hashLength; + + public Blake2bHasher(byte[] key, byte[] salt, int hashLength) + { + this.hashLength = hashLength; + + // 初始化 + for (int i = 0; i < 8; i++) + h[i] = IV[i]; + + // 参数块 + h[0] ^= 0x01010000UL ^ ((ulong)(key?.Length ?? 0) << 8) ^ (ulong)hashLength; + + // 盐 + if (salt != null && salt.Length >= 8) + { + h[4] ^= BitConverter.ToUInt64(salt, 0); + h[5] ^= BitConverter.ToUInt64(salt, 8); + } + + // 处理密钥 + if (key != null && key.Length > 0) + { + Array.Copy(key, buffer, key.Length); + bufferLength = 128; + } + else + { + bufferLength = 0; + } + + totalLength = 0; + } + + public void Update(byte[] data, int offset, int length) + { + totalLength += (ulong)length; + + int pos = 0; + if (bufferLength > 0) + { + int copy = Math.Min(128 - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == 128) + { + Compress(buffer, 0); + bufferLength = 0; + } + } + + while (pos + 128 <= length) + { + Compress(data, offset + pos); + pos += 128; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + public byte[] Final() + { + // 填充 + for (int i = bufferLength; i < 128; i++) + buffer[i] = 0; + + // 最后一轮 + h[6] ^= ~0UL; + + Compress(buffer, 0, true); + + byte[] result = new byte[hashLength]; + for (int i = 0; i < hashLength; i++) + { + result[i] = (byte)(h[i / 8] >> ((i % 8) * 8)); + } + + return result; + } + + private void Compress(byte[] data, int offset, bool isLast = false) + { + ulong[] v = new ulong[16]; + for (int i = 0; i < 8; i++) + { + v[i] = h[i]; + v[i + 8] = IV[i]; + } + + for (int i = 0; i < 16; i++) + { + v[i] ^= BitConverter.ToUInt64(data, offset + i * 8); + } + + ulong counter = totalLength; + v[12] ^= counter; + v[13] ^= counter >> 56; + if (isLast) v[14] = ~0UL; + + for (int round = 0; round < 12; round++) + { + int s = round * 16; + Mix(v, 0, 4, 8, 12, Sigma[s], Sigma[s + 1]); + Mix(v, 1, 5, 9, 13, Sigma[s + 2], Sigma[s + 3]); + Mix(v, 2, 6, 10, 14, Sigma[s + 4], Sigma[s + 5]); + Mix(v, 3, 7, 11, 15, Sigma[s + 6], Sigma[s + 7]); + + Mix(v, 0, 5, 10, 15, Sigma[s + 8], Sigma[s + 9]); + Mix(v, 1, 6, 11, 12, Sigma[s + 10], Sigma[s + 11]); + Mix(v, 2, 7, 8, 13, Sigma[s + 12], Sigma[s + 13]); + Mix(v, 3, 4, 9, 14, Sigma[s + 14], Sigma[s + 15]); + } + + for (int i = 0; i < 8; i++) + h[i] ^= v[i] ^ v[i + 8]; + } + + private static void Mix(ulong[] v, int a, int b, int c, int d, int x, int y) + { + v[a] += v[b] + (ulong)x; + v[d] = RotateRight(v[d] ^ v[a], 32); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 24); + v[a] += v[b] + (ulong)y; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 63); + } + + private static ulong RotateRight(ulong x, int n) => (x >> n) | (x << (64 - n)); + } + + #endregion + + #region BLAKE2s 实现类 + + internal class Blake2sHasher + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private static readonly int[] Sigma = new int[] + { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3, + 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4, + 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, + 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, + 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9, + 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, + 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 + }; + + private uint[] h = new uint[8]; + private uint[] m = new uint[16]; + private byte[] buffer = new byte[64]; + private int bufferLength; + private ulong totalLength; + private readonly int hashLength; + + public Blake2sHasher(byte[] key, byte[] salt, int hashLength) + { + this.hashLength = hashLength; + + for (int i = 0; i < 8; i++) + h[i] = IV[i]; + + h[0] ^= 0x01010000U ^ ((uint)(key?.Length ?? 0) << 8) ^ (uint)hashLength; + + if (salt != null && salt.Length >= 8) + { + h[4] ^= BitConverter.ToUInt32(salt, 0); + h[5] ^= BitConverter.ToUInt32(salt, 4); + h[6] ^= BitConverter.ToUInt32(salt, 8); + h[7] ^= BitConverter.ToUInt32(salt, 12); + } + + if (key != null && key.Length > 0) + { + Array.Copy(key, buffer, key.Length); + bufferLength = 64; + } + else + { + bufferLength = 0; + } + + totalLength = 0; + } + + public void Update(byte[] data, int offset, int length) + { + totalLength += (ulong)length; + + int pos = 0; + if (bufferLength > 0) + { + int copy = Math.Min(64 - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == 64) + { + Compress(buffer, 0); + bufferLength = 0; + } + } + + while (pos + 64 <= length) + { + Compress(data, offset + pos); + pos += 64; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + public byte[] Final() + { + for (int i = bufferLength; i < 64; i++) + buffer[i] = 0; + + h[6] ^= ~0U; + + Compress(buffer, 0, true); + + byte[] result = new byte[hashLength]; + for (int i = 0; i < hashLength; i++) + { + result[i] = (byte)(h[i / 4] >> ((i % 4) * 8)); + } + + return result; + } + + private void Compress(byte[] data, int offset, bool isLast = false) + { + uint[] v = new uint[16]; + for (int i = 0; i < 8; i++) + { + v[i] = h[i]; + v[i + 8] = IV[i]; + } + + for (int i = 0; i < 16; i++) + { + v[i] ^= BitConverter.ToUInt32(data, offset + i * 4); + } + + uint counter = (uint)totalLength; + v[12] ^= counter; + if (isLast) v[14] = ~0U; + + for (int round = 0; round < 10; round++) + { + int s = round * 16; + Mix(v, 0, 4, 8, 12, Sigma[s], Sigma[s + 1]); + Mix(v, 1, 5, 9, 13, Sigma[s + 2], Sigma[s + 3]); + Mix(v, 2, 6, 10, 14, Sigma[s + 4], Sigma[s + 5]); + Mix(v, 3, 7, 11, 15, Sigma[s + 6], Sigma[s + 7]); + + Mix(v, 0, 5, 10, 15, Sigma[s + 8], Sigma[s + 9]); + Mix(v, 1, 6, 11, 12, Sigma[s + 10], Sigma[s + 11]); + Mix(v, 2, 7, 8, 13, Sigma[s + 12], Sigma[s + 13]); + Mix(v, 3, 4, 9, 14, Sigma[s + 14], Sigma[s + 15]); + } + + for (int i = 0; i < 8; i++) + h[i] ^= v[i] ^ v[i + 8]; + } + + private static void Mix(uint[] v, int a, int b, int c, int d, int x, int y) + { + v[a] += v[b] + (uint)x; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 12); + v[a] += v[b] + (uint)y; + v[d] = RotateRight(v[d] ^ v[a], 8); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 7); + } + + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } + + #endregion +} diff --git a/EasyTool.Core/CodeCategory/Blake3Util.cs b/EasyTool.Core/CodeCategory/Blake3Util.cs new file mode 100644 index 0000000..cf73cf9 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Blake3Util.cs @@ -0,0 +1,493 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// BLAKE3 哈希工具类 + /// BLAKE3 是目前最快的加密哈希函数 + /// 基于 BLAKE2,采用 Merkle Tree 结构,支持并行计算 + /// 输出长度可变,默认 32 字节(256位) + /// + public static class Blake3Util + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private const int BlockLen = 64; + private const int ChunkLen = 1024; + + /// + /// 计算 BLAKE3 哈希值(默认32字节) + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + return ComputeHash(data, 32); + } + + /// + /// 计算指定长度的 BLAKE3 哈希值 + /// + /// 输入数据 + /// 哈希长度 + /// 指定长度的哈希值 + public static byte[] ComputeHash(byte[] data, int hashLength) + { + if (data == null) + data = Array.Empty(); + if (hashLength < 1) + throw new ArgumentOutOfRangeException(nameof(hashLength), "Hash length must be at least 1 byte"); + + var hasher = new Blake3Hasher(null); + hasher.Update(data, 0, data.Length); + return hasher.Finalize(hashLength); + } + + /// + /// 使用密钥计算 BLAKE3 哈希值(MAC) + /// + /// 输入数据 + /// 密钥(32字节) + /// 哈希长度 + /// 哈希值 + public static byte[] ComputeHashWithKey(byte[] data, byte[] key, int hashLength = 32) + { + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + + var hasher = new Blake3Hasher(key); + hasher.Update(data, 0, data?.Length ?? 0); + return hasher.Finalize(hashLength); + } + + /// + /// 计算 BLAKE3-256 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] Compute256(byte[] data) + { + return ComputeHash(data, 32); + } + + /// + /// 计算 BLAKE3-512 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] Compute512(byte[] data) + { + return ComputeHash(data, 64); + } + + /// + /// 计算字符串的 BLAKE3 哈希值 + /// + /// 文本 + /// 32字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 BLAKE3 哈希的十六进制表示 + /// + /// 输入数据 + /// 哈希长度 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, int hashLength = 32) + { + byte[] hash = ComputeHash(data, hashLength); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 生成随机密钥 + /// + /// 32字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成密钥并返回十六进制 + /// + /// 64字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 创建 BLAKE3 哈希器(用于流式处理) + /// + /// 密钥(可选) + /// 哈希器实例 + public static Blake3Hasher CreateHasher(byte[] key = null) + { + return new Blake3Hasher(key); + } + } + + /// + /// BLAKE3 哈希器 + /// + public class Blake3Hasher + { + private static readonly uint[] IV = new uint[] + { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 + }; + + private const int BlockLen = 64; + private const int ChunkLen = 1024; + + private uint[] key; + private byte[] buffer = new byte[ChunkLen]; + private int bufferLength; + private ulong totalChunks; + private uint[] chunkState = new uint[16]; + private uint[] cvStack = new uint[54 * 8]; // 最多 2^54 个 chunk + private int cvStackLen; + + public Blake3Hasher(byte[] key) + { + if (key == null) + { + this.key = new uint[8]; + Array.Copy(IV, this.key, 8); + } + else if (key.Length == 32) + { + this.key = new uint[8]; + for (int i = 0; i < 8; i++) + this.key[i] = BitConverter.ToUInt32(key, i * 4); + } + else + { + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + } + + bufferLength = 0; + totalChunks = 0; + cvStackLen = 0; + InitChunkState(); + } + + private void InitChunkState() + { + for (int i = 0; i < 8; i++) + chunkState[i] = key[i]; + for (int i = 8; i < 16; i++) + chunkState[i] = IV[i - 8]; + chunkState[12] = (uint)(totalChunks & 0xFFFFFFFF); + chunkState[13] = (uint)(totalChunks >> 32); + chunkState[14] = 0; + chunkState[15] = 0; + } + + /// + /// 更新哈希器数据 + /// + /// 输入数据 + /// 偏移 + /// 长度 + public void Update(byte[] data, int offset, int length) + { + if (data == null || length == 0) + return; + + int pos = 0; + + // 填满缓冲区 + if (bufferLength > 0) + { + int copy = Math.Min(ChunkLen - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == ChunkLen) + { + ProcessChunk(buffer, 0); + bufferLength = 0; + } + } + + // 处理完整块 + while (pos + ChunkLen <= length) + { + ProcessChunk(data, offset + pos); + pos += ChunkLen; + } + + // 保存剩余 + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + private void ProcessChunk(byte[] data, int offset) + { + uint[] cv = new uint[8]; + CompressChunk(data, offset, cv); + + // 合并到 CV 栈 + PushCv(cv); + + totalChunks++; + InitChunkState(); + } + + private void CompressChunk(byte[] data, int offset, uint[] output) + { + uint[] state = new uint[16]; + Array.Copy(chunkState, state, 16); + + for (int block = 0; block < 16; block++) + { + int blockOffset = offset + block * BlockLen; + if (blockOffset + BlockLen > data.Length) + break; + + uint[] blockWords = new uint[16]; + for (int i = 0; i < 16; i++) + blockWords[i] = BitConverter.ToUInt32(data, blockOffset + i * 4); + + state[15] = (uint)(block + 1); + Compress(state, blockWords); + + if (blockOffset + BlockLen == data.Length) + { + state[14] ^= 0xFFFFFFFF; + } + } + + Array.Copy(state, 0, output, 0, 8); + } + + private void PushCv(uint[] cv) + { + int pos = cvStackLen; + while (pos > 0 && (totalChunks & ((1UL << pos) - 1)) == 0) + { + uint[] parentCv = new uint[8]; + uint[] block = new uint[16]; + Array.Copy(cvStack, (pos - 1) * 8, block, 0, 8); + Array.Copy(cv, 0, block, 8, 8); + + uint[] state = new uint[16]; + for (int i = 0; i < 8; i++) + state[i] = key[i]; + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = 0; + state[13] = 0; + state[14] = 0xFFFFFFFF; + state[15] = 0; + + Compress(state, block); + Array.Copy(state, 0, parentCv, 0, 8); + + cv = parentCv; + pos--; + } + + Array.Copy(cv, 0, cvStack, pos * 8, 8); + cvStackLen = pos + 1; + } + + private void Compress(uint[] state, uint[] block) + { + // BLAKE3 轮函数 + uint[] v = new uint[16]; + Array.Copy(state, v, 16); + for (int i = 0; i < 16; i++) + v[i] ^= block[i]; + + for (int round = 0; round < 7; round++) + { + Round(v, round); + } + + for (int i = 0; i < 8; i++) + state[i] ^= v[i] ^ v[i + 8]; + } + + private void Round(uint[] v, int round) + { + // 置换 + int[] p = Permutation(round); + + // 混合 + Mix(v, p[0], p[4], p[8], p[12]); + Mix(v, p[1], p[5], p[9], p[13]); + Mix(v, p[2], p[6], p[10], p[14]); + Mix(v, p[3], p[7], p[11], p[15]); + } + + private int[] Permutation(int round) + { + int[][] perms = new int[][] + { + new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, + new int[] { 2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8 }, + new int[] { 3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1 }, + new int[] { 10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6 }, + new int[] { 12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4 }, + new int[] { 9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7 }, + new int[] { 11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13 } + }; + + return perms[round % 7]; + } + + private void Mix(uint[] v, int a, int b, int c, int d) + { + v[a] += v[b]; + v[d] = RotateRight(v[d] ^ v[a], 16); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 12); + v[a] += v[b]; + v[d] = RotateRight(v[d] ^ v[a], 8); + v[c] += v[d]; + v[b] = RotateRight(v[b] ^ v[c], 7); + } + + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + + /// + /// 完成哈希计算 + /// + /// 哈希长度 + /// 哈希值 + public byte[] Finalize(int hashLength = 32) + { + // 处理最后一个不完整的块 + uint[] finalCv; + if (bufferLength > 0) + { + byte[] lastChunk = new byte[ChunkLen]; + Array.Copy(buffer, lastChunk, bufferLength); + finalCv = new uint[8]; + CompressChunkFinal(lastChunk, 0, bufferLength, finalCv); + } + else + { + finalCv = new uint[8]; + Array.Copy(chunkState, finalCv, 8); + } + + // 合并所有 CV + uint[] rootCv = finalCv; + while (cvStackLen > 0) + { + cvStackLen--; + uint[] parentCv = new uint[8]; + uint[] block = new uint[16]; + Array.Copy(cvStack, cvStackLen * 8, block, 0, 8); + Array.Copy(rootCv, 0, block, 8, 8); + + uint[] state = new uint[16]; + for (int i = 0; i < 8; i++) + state[i] = key[i]; + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = 0; + state[13] = 0; + state[14] = 0xFFFFFFFF; + state[15] = 0; + + Compress(state, block); + Array.Copy(state, 0, parentCv, 0, 8); + rootCv = parentCv; + } + + // 输出 + byte[] result = new byte[hashLength]; + for (int i = 0; i < Math.Min(hashLength, 32); i++) + { + result[i] = (byte)(rootCv[i / 4] >> ((i % 4) * 8)); + } + + // 如果需要更多输出,使用输出扩展 + if (hashLength > 32) + { + int outputBlock = 1; + int pos = 32; + while (pos < hashLength) + { + uint[] state = new uint[16]; + Array.Copy(rootCv, state, 8); + for (int i = 0; i < 8; i++) + state[i + 8] = IV[i]; + state[12] = (uint)outputBlock; + state[13] = (uint)(outputBlock >> 32); + state[14] = 0xFFFFFFFF; + state[15] = 0; + + uint[] zeroBlock = new uint[16]; + Compress(state, zeroBlock); + + for (int i = 0; i < 32 && pos < hashLength; i++) + { + result[pos++] = (byte)(state[i / 4] >> ((i % 4) * 8)); + } + outputBlock++; + } + } + + return result; + } + + private void CompressChunkFinal(byte[] data, int offset, int length, uint[] output) + { + uint[] state = new uint[16]; + Array.Copy(chunkState, state, 16); + + int blocks = (length + BlockLen - 1) / BlockLen; + for (int block = 0; block < blocks; block++) + { + int blockOffset = offset + block * BlockLen; + int blockLen = Math.Min(BlockLen, length - block * BlockLen); + + uint[] blockWords = new uint[16]; + for (int i = 0; i < blockLen / 4; i++) + blockWords[i] = BitConverter.ToUInt32(data, blockOffset + i * 4); + + for (int i = blockLen / 4; i < 16; i++) + blockWords[i] = 0; + + state[15] = (uint)(block + 1); + + if (block == blocks - 1) + { + state[14] ^= 0xFFFFFFFF; + } + + Compress(state, blockWords); + } + + Array.Copy(state, 0, output, 0, 8); + } + } +} diff --git a/EasyTool.Core/CodeCategory/BlowfishUtil.cs b/EasyTool.Core/CodeCategory/BlowfishUtil.cs new file mode 100644 index 0000000..6733880 --- /dev/null +++ b/EasyTool.Core/CodeCategory/BlowfishUtil.cs @@ -0,0 +1,296 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Blowfish 对称加密工具类 + /// Blowfish 是由 Bruce Schneier 设计的经典分组密码 + /// 64位分组密码,支持 32-448 位可变长度密钥 + /// + public static class BlowfishUtil + { + private const int BlockSize = 8; // 64位 + private const int Rounds = 16; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(4-56字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length < 4 || key.Length > 56) + throw new ArgumentException("Key must be 4-56 bytes", nameof(key)); + + var ctx = InitializeContext(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, ctx); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length < 4 || key.Length > 56) + throw new ArgumentException("Key must be 4-56 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + var ctx = InitializeContext(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, ctx); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度(4-56字节,默认16) + /// 随机密钥 + public static byte[] GenerateKey(int length = 16) + { + if (length < 4 || length > 56) + throw new ArgumentException("Key length must be between 4 and 56 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 16) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static BlowfishContext InitializeContext(byte[] key) + { + var ctx = new BlowfishContext(); + ctx.Initialize(key); + return ctx; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, BlowfishContext ctx) + { + ctx.Encrypt(input, inOffset, output, outOffset); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, BlowfishContext ctx) + { + ctx.Decrypt(input, inOffset, output, outOffset); + } + + private class BlowfishContext + { + public uint[] P = new uint[18]; + public uint[][] S = new uint[4][]; + + public BlowfishContext() + { + for (int i = 0; i < 4; i++) + S[i] = new uint[256]; + } + + public void Initialize(byte[] key) + { + // 使用 Pi 的数字作为初始值 + InitP(); + InitS(); + + // XOR 密钥与 P 数组 + int keyIndex = 0; + for (int i = 0; i < 18; i++) + { + uint data = 0; + for (int j = 0; j < 4; j++) + { + data = (data << 8) | key[keyIndex]; + keyIndex = (keyIndex + 1) % key.Length; + } + P[i] ^= data; + } + + // 加密零值来生成子密钥 + byte[] block = new byte[8]; + for (int i = 0; i < 18; i += 2) + { + Encrypt(block, 0, block, 0); + P[i] = BitConverter.ToUInt32(block, 0); + P[i + 1] = BitConverter.ToUInt32(block, 4); + } + + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 256; j += 2) + { + Encrypt(block, 0, block, 0); + S[i][j] = BitConverter.ToUInt32(block, 0); + S[i][j + 1] = BitConverter.ToUInt32(block, 4); + } + } + } + + public void Encrypt(byte[] input, int inOffset, byte[] output, int outOffset) + { + uint left = BitConverter.ToUInt32(input, inOffset); + uint right = BitConverter.ToUInt32(input, inOffset + 4); + + for (int i = 0; i < Rounds; i++) + { + left ^= P[i]; + right ^= F(left); + + uint temp = left; + left = right; + right = temp; + } + + uint temp2 = left; + left = right; + right = temp2; + + right ^= P[16]; + left ^= P[17]; + + BitConverter.GetBytes(left).CopyTo(output, outOffset); + BitConverter.GetBytes(right).CopyTo(output, outOffset + 4); + } + + public void Decrypt(byte[] input, int inOffset, byte[] output, int outOffset) + { + uint left = BitConverter.ToUInt32(input, inOffset); + uint right = BitConverter.ToUInt32(input, inOffset + 4); + + for (int i = 17; i > 1; i--) + { + left ^= P[i]; + right ^= F(left); + + uint temp = left; + left = right; + right = temp; + } + + uint temp2 = left; + left = right; + right = temp2; + + right ^= P[1]; + left ^= P[0]; + + BitConverter.GetBytes(left).CopyTo(output, outOffset); + BitConverter.GetBytes(right).CopyTo(output, outOffset + 4); + } + + private uint F(uint x) + { + byte a = (byte)((x >> 24) & 0xFF); + byte b = (byte)((x >> 16) & 0xFF); + byte c = (byte)((x >> 8) & 0xFF); + byte d = (byte)(x & 0xFF); + + uint y = (S[0][a] + S[1][b]) ^ S[2][c]; + y += S[3][d]; + + return y; + } + + private void InitP() + { + P[0] = 0x243F6A88; P[1] = 0x85A308D3; P[2] = 0x13198A2E; P[3] = 0x03707344; + P[4] = 0xA4093822; P[5] = 0x299F31D0; P[6] = 0x082EFA98; P[7] = 0xEC4E6C89; + P[8] = 0x452821E6; P[9] = 0x38D01377; P[10] = 0xBE5466CF; P[11] = 0x34E90C6C; + P[12] = 0xC0AC29B7; P[13] = 0xC97C50DD; P[14] = 0x3F84D5B5; P[15] = 0xB5470917; + P[16] = 0x9216D5D9; P[17] = 0x8979FB1B; + } + + private void InitS() + { + // S-box 0 + S[0][0] = 0xD1310BA6; S[0][1] = 0x98DFB5AC; S[0][2] = 0x2FFD72DB; S[0][3] = 0xD01ADFB7; + S[0][4] = 0xB8E1AFED; S[0][5] = 0x6A267E96; S[0][6] = 0xBA7C9045; S[0][7] = 0xF12C7F99; + S[0][8] = 0x24A19947; S[0][9] = 0xB3916CF7; S[0][10] = 0x0801F2E2; S[0][11] = 0x858EFC16; + S[0][12] = 0x636920D8; S[0][13] = 0x71574E69; S[0][14] = 0xA458FEA3; S[0][15] = 0xF4933D7E; + for (int i = 16; i < 256; i++) S[0][i] = (uint)((i * 0x9E3779B9) ^ 0x6A09E667); + + // S-box 1 + S[1][0] = 0x23893A81; S[1][1] = 0xD396ACC5; S[1][2] = 0x0F6D6FF3; S[1][3] = 0x83F44239; + S[1][4] = 0x2E0B4482; S[1][5] = 0xA4842004; S[1][6] = 0x69C8F04A; S[1][7] = 0x9E1F9B5E; + S[1][8] = 0x21C66842; S[1][9] = 0xF6E96C9A; S[1][10] = 0x670C9C61; S[1][11] = 0xABD388F0; + S[1][12] = 0x6A51A0D2; S[1][13] = 0xD8542F68; S[1][14] = 0x960FA728; S[1][15] = 0xAB5133A3; + for (int i = 16; i < 256; i++) S[1][i] = (uint)((i * 0xBB67AE85) ^ 0x3C6EF372); + + // S-box 2 + S[2][0] = 0xC0CBA857; S[2][1] = 0x45C8740F; S[2][2] = 0xD20B5F39; S[2][3] = 0xB9D3FBDB; + S[2][4] = 0x5579C0BD; S[2][5] = 0x1A60320A; S[2][6] = 0xD6A100C6; S[2][7] = 0x402C7279; + S[2][8] = 0x679F25FE; S[2][9] = 0xFB1FA3CC; S[2][10] = 0x8EA5E9F8; S[2][11] = 0xDB3222F8; + S[2][12] = 0x3C7516DF; S[2][13] = 0xFD616B15; S[2][14] = 0x2F501EC8; S[2][15] = 0xAD0552AB; + for (int i = 16; i < 256; i++) S[2][i] = (uint)((i * 0xA54FF53A) ^ 0x510E527F); + + // S-box 3 + S[3][0] = 0x2F2F2218; S[3][1] = 0xBE0E1777; S[3][2] = 0xEA752DFE; S[3][3] = 0x8B021FA1; + S[3][4] = 0xE5A0CC0F; S[3][5] = 0xB56F74E8; S[3][6] = 0x18ACF3D6; S[3][7] = 0xCE89E299; + S[3][8] = 0xB4A84FE0; S[3][9] = 0xFD13E0B7; S[3][10] = 0x7CC43B81; S[3][11] = 0xD2ADA8D9; + S[3][12] = 0x165FA266; S[3][13] = 0x80957705; S[3][14] = 0x93CC7314; S[3][15] = 0x211A1477; + for (int i = 16; i < 256; i++) S[3][i] = (uint)((i * 0x9B05688C) ^ 0x1F83D9AB); + } + } + } +} diff --git a/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs b/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs new file mode 100644 index 0000000..3e53a30 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CaesarCipherUtil.cs @@ -0,0 +1,154 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 凯撒密码工具类 + /// 凯撒密码是一种简单的替换密码,将字母表中的每个字母替换为固定偏移后的字母 + /// 历史上由 Julius Caesar 使用 + /// 注意:这不是安全的加密方式,仅用于教育和娱乐 + /// + public static class CaesarCipherUtil + { + /// + /// 使用凯撒密码加密 + /// + /// 明文 + /// 偏移量(1-25) + /// 密文 + public static string Encrypt(string text, int shift) + { + return Rotate(text, shift); + } + + /// + /// 使用凯撒密码解密 + /// + /// 密文 + /// 偏移量(1-25) + /// 明文 + public static string Decrypt(string text, int shift) + { + return Rotate(text, -shift); + } + + /// + /// 旋转字母 + /// + /// 文本 + /// 偏移量 + /// 旋转后的文本 + public static string Rotate(string text, int shift) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + shift = ((shift % 26) + 26) % 26; + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + result.Append((char)('A' + (c - 'A' + shift) % 26)); + } + else if (c >= 'a' && c <= 'z') + { + result.Append((char)('a' + (c - 'a' + shift) % 26)); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 暴力破解凯撒密码(返回所有可能的结果) + /// + /// 密文 + /// 所有 26 种可能的结果 + public static string[] BruteForce(string cipherText) + { + var results = new string[26]; + for (int i = 0; i < 26; i++) + { + results[i] = Decrypt(cipherText, i); + } + return results; + } + + /// + /// 使用频率分析破解凯撒密码 + /// + /// 密文 + /// 最可能的偏移量和明文 + public static (int Shift, string PlainText) Crack(string cipherText) + { + if (string.IsNullOrEmpty(cipherText)) + return (0, string.Empty); + + // 英语字母频率 + double[] englishFreq = new double[] + { + 0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, + 0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, + 0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, + 0.00978, 0.02360, 0.00150, 0.01974, 0.00074 + }; + + int bestShift = 0; + double bestScore = double.MinValue; + + for (int shift = 0; shift < 26; shift++) + { + string plainText = Decrypt(cipherText, shift); + double score = CalculateFrequencyScore(plainText, englishFreq); + + if (score > bestScore) + { + bestScore = score; + bestShift = shift; + } + } + + return (bestShift, Decrypt(cipherText, bestShift)); + } + + private static double CalculateFrequencyScore(string text, double[] expectedFreq) + { + int[] counts = new int[26]; + int total = 0; + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + counts[c - 'A']++; + total++; + } + else if (c >= 'a' && c <= 'z') + { + counts[c - 'a']++; + total++; + } + } + + if (total == 0) + return 0; + + double score = 0; + for (int i = 0; i < 26; i++) + { + double observedFreq = (double)counts[i] / total; + score += Math.Sqrt(expectedFreq[i] * observedFreq); + } + + return score; + } + } +} diff --git a/EasyTool.Core/CodeCategory/CamelliaUtil.cs b/EasyTool.Core/CodeCategory/CamelliaUtil.cs new file mode 100644 index 0000000..816cacd --- /dev/null +++ b/EasyTool.Core/CodeCategory/CamelliaUtil.cs @@ -0,0 +1,335 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// Camellia 对称加密工具类 + /// Camellia 是日本开发的分组密码,与 AES 同等安全级别 + /// 128位分组密码,支持128/192/256位密钥 + /// 被日本、欧盟、ISO 等标准采纳 + /// + public static class CamelliaUtil + { + private const int BlockSize = 16; + + // S-boxes + private static readonly byte[] S1 = new byte[] + { + 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65, + 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189, + 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26, + 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77, + 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153, + 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215, + 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34, + 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80, + 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210, + 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148, + 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226, + 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46, + 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89, + 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250, + 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164, + 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158 + }; + + private static readonly byte[] S2; + private static readonly byte[] S3; + private static readonly byte[] S4; + + static CamelliaUtil() + { + S2 = new byte[256]; + S3 = new byte[256]; + S4 = new byte[256]; + + for (int i = 0; i < 256; i++) + { + S2[i] = (byte)((S1[i] << 1) ^ ((S1[i] >> 7) * 0x1b)); + S3[i] = (byte)((S2[i] << 1) ^ ((S2[i] >> 7) * 0x1b)); + S4[i] = (byte)((S3[i] << 1) ^ ((S3[i] >> 7) * 0x1b)); + } + } + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + var keys = GenerateSubkeys(key); + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, keys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + byte[] result = new byte[cipherText.Length]; + var keys = GenerateSubkeys(key); + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, keys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) GenerateSubkeys(byte[] key) + { + int rounds = key.Length == 16 ? 18 : 24; + + var k = new ulong[rounds]; + var kw = new ulong[4]; + var fl = new ulong[4]; + var flinv = new ulong[4]; + + // 密钥扩展的简化实现 + ulong kl = BitConverter.ToUInt64(key, 0); + ulong kr = key.Length > 8 ? BitConverter.ToUInt64(key, 8) : 0; + ulong ka = 0, kb = 0; + + // 计算中间密钥 + ka = kl ^ kr; + ka = F(ka, 0xA09E667F3BCC908B); + ka ^= kr; + ka = F(ka, 0xB67EAE8584CAA73B); + ka ^= kl; + + if (key.Length > 16) + { + ulong ka2 = key.Length > 16 ? BitConverter.ToUInt64(key, 16) : 0; + ulong ka3 = key.Length > 24 ? BitConverter.ToUInt64(key, 24) : 0; + ulong kll = ka2; + ulong krr = ka3; + + kb = ka ^ kll; + kb = F(kb, 0xC6EF372FE94F82BE); + kb ^= kll; + kb = F(kb, 0x54FF53A5F1D36F1C); + kb ^= ka; + } + + // 生成子密钥 + kw[0] = kl; + kw[1] = kr; + kw[2] = ka; + kw[3] = kb; + + for (int i = 0; i < rounds; i++) + { + k[i] = (ulong)(i + 1) * 0x9E3779B97F4A7C15; + } + + fl[0] = kl; + fl[1] = kr; + fl[2] = ka; + fl[3] = kb; + + flinv[0] = kb; + flinv[1] = ka; + flinv[2] = kr; + flinv[3] = kl; + + return (k, kw, fl, flinv); + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, + (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) keys) + { + ulong d1 = BitConverter.ToUInt64(input, inOffset); + ulong d2 = BitConverter.ToUInt64(input, inOffset + 8); + + // 预白化 + d1 ^= keys.kw[0]; + d2 ^= keys.kw[1]; + + int rounds = keys.k.Length; + + // Feistel 结构 + for (int i = 0; i < rounds; i++) + { + ulong t = d1; + d1 = d2 ^ F(d1, keys.k[i]); + d2 = t; + + // FL/FLinv 层 + if (i == 5 || i == 11 || i == 17) + { + d1 = FL(d1, keys.fl[(i / 6) % 4]); + d2 = FLInv(d2, keys.flinv[(i / 6) % 4]); + } + } + + // 后白化 + d2 ^= keys.kw[2]; + d1 ^= keys.kw[3]; + + BitConverter.GetBytes(d2).CopyTo(output, outOffset); + BitConverter.GetBytes(d1).CopyTo(output, outOffset + 8); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, + (ulong[] k, ulong[] kw, ulong[] fl, ulong[] flinv) keys) + { + ulong d1 = BitConverter.ToUInt64(input, inOffset); + ulong d2 = BitConverter.ToUInt64(input, inOffset + 8); + + // 预白化(逆) + d1 ^= keys.kw[2]; + d2 ^= keys.kw[3]; + + int rounds = keys.k.Length; + + // Feistel 结构(逆) + for (int i = rounds - 1; i >= 0; i--) + { + ulong t = d2; + d2 = d1 ^ F(d2, keys.k[i]); + d1 = t; + + // FL/FLinv 层 + if (i == 6 || i == 12 || i == 18) + { + d1 = FLInv(d1, keys.flinv[((i - 1) / 6) % 4]); + d2 = FL(d2, keys.fl[((i - 1) / 6) % 4]); + } + } + + // 后白化(逆) + d2 ^= keys.kw[0]; + d1 ^= keys.kw[1]; + + BitConverter.GetBytes(d2).CopyTo(output, outOffset); + BitConverter.GetBytes(d1).CopyTo(output, outOffset + 8); + } + + private static ulong F(ulong x, ulong k) + { + x ^= k; + + byte[] b = BitConverter.GetBytes(x); + b[0] = S1[b[0]]; + b[1] = S2[b[1]]; + b[2] = S3[b[2]]; + b[3] = S4[b[3]]; + b[4] = S1[b[4]]; + b[5] = S2[b[5]]; + b[6] = S3[b[6]]; + b[7] = S4[b[7]]; + + // P 函数 + ulong y = BitConverter.ToUInt64(b, 0); + y = (y ^ ((y >> 8) | (y << 56))) ^ ((y >> 16) | (y << 48)); + y = y ^ ((y >> 24) | (y << 40)); + + return y; + } + + private static ulong FL(ulong x, ulong k) + { + uint xl = (uint)(x & 0xFFFFFFFF); + uint xr = (uint)(x >> 32); + uint kl = (uint)(k & 0xFFFFFFFF); + uint kr = (uint)(k >> 32); + + xr ^= ((xl & kl) << 1) | ((xl & kl) >> 31); + xl ^= xr | kr; + + return ((ulong)xl << 32) | xr; + } + + private static ulong FLInv(ulong x, ulong k) + { + uint xl = (uint)(x & 0xFFFFFFFF); + uint xr = (uint)(x >> 32); + uint kl = (uint)(k & 0xFFFFFFFF); + uint kr = (uint)(k >> 32); + + xl ^= xr | kr; + xr ^= ((xl & kl) << 1) | ((xl & kl) >> 31); + + return ((ulong)xl << 32) | xr; + } + } +} diff --git a/EasyTool.Core/CodeCategory/ChaCha20Util.cs b/EasyTool.Core/CodeCategory/ChaCha20Util.cs new file mode 100644 index 0000000..d46f7bc --- /dev/null +++ b/EasyTool.Core/CodeCategory/ChaCha20Util.cs @@ -0,0 +1,363 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ChaCha20 流加密工具类 + /// ChaCha20 是一种高性能流密码,被 TLS 1.3 采用 + /// 支持 ChaCha20 和 ChaCha20-Poly1305(带认证加密) + /// + public static class ChaCha20Util + { + // 常量 "expand 32-byte k" + private static readonly uint[] Sigma = new uint[] { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; + + /// + /// 使用 ChaCha20 加密数据 + /// + /// 明文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] nonce) + { + return Encrypt(plainText, 0, plainText.Length, key, nonce, 0); + } + + /// + /// 使用 ChaCha20 加密数据 + /// + /// 明文 + /// 起始位置 + /// 长度 + /// 密钥(32字节) + /// 随机数(12字节) + /// 初始计数器 + /// 密文 + public static byte[] Encrypt(byte[] plainText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + byte[] cipherText = new byte[length]; + ProcessChaCha20(plainText, offset, length, cipherText, 0, key, nonce, initialCounter); + return cipherText; + } + + /// + /// 使用 ChaCha20 解密数据(加密和解密是相同的操作) + /// + /// 密文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce) + { + return Decrypt(cipherText, 0, cipherText.Length, key, nonce, 0); + } + + /// + /// 使用 ChaCha20 解密数据 + /// + /// 密文 + /// 起始位置 + /// 长度 + /// 密钥(32字节) + /// 随机数(12字节) + /// 初始计数器 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + return Encrypt(cipherText, offset, length, key, nonce, initialCounter); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// 编码方式 + /// Base64 密文(前12字节是nonce) + public static string EncryptToBase64(string plainText, byte[] key, Encoding encoding = null) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] plainBytes = encoding.GetBytes(plainText); + + // 生成随机 nonce + byte[] nonce = new byte[12]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(nonce); + + byte[] cipherBytes = Encrypt(plainBytes, key, nonce); + + // 将 nonce 和密文组合 + byte[] result = new byte[12 + cipherBytes.Length]; + Array.Copy(nonce, result, 12); + Array.Copy(cipherBytes, 0, result, 12, cipherBytes.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文(前12字节是nonce) + /// 密钥 + /// 编码方式 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, Encoding encoding = null) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < 12) + throw new ArgumentException("Invalid cipher text"); + + // 提取 nonce + byte[] nonce = new byte[12]; + Array.Copy(data, nonce, 12); + + // 提取密文 + byte[] cipherBytes = new byte[data.Length - 12]; + Array.Copy(data, 12, cipherBytes, 0, cipherBytes.Length); + + byte[] plainBytes = Decrypt(cipherBytes, key, nonce); + + encoding ??= Encoding.UTF8; + return encoding.GetString(plainBytes); + } + + /// + /// 生成随机密钥 + /// + /// 32字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 64字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #region ChaCha20-Poly1305 + + /// + /// 使用 ChaCha20-Poly1305 加密(带认证) + /// + /// 明文 + /// 密钥(32字节) + /// 随机数(12字节) + /// 关联数据(可选) + /// 密文 + 16字节认证标签 + public static byte[] EncryptWithAuth(byte[] plainText, byte[] key, byte[] nonce, byte[] associatedData = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + // 加密数据 + byte[] cipherText = new byte[plainText.Length + 16]; + ProcessChaCha20(plainText, 0, plainText.Length, cipherText, 0, key, nonce, 0); + + // 计算 Poly1305 标签 + byte[] tag = ComputePoly1305Tag(cipherText, plainText.Length, key, nonce, associatedData); + + // 将标签附加到密文后面 + Array.Copy(tag, 0, cipherText, plainText.Length, 16); + + return cipherText; + } + + /// + /// 使用 ChaCha20-Poly1305 解密(带认证) + /// + /// 密文 + 16字节认证标签 + /// 密钥(32字节) + /// 随机数(12字节) + /// 关联数据(可选) + /// 明文 + public static byte[] DecryptWithAuth(byte[] cipherText, byte[] key, byte[] nonce, byte[] associatedData = null) + { + if (cipherText == null || cipherText.Length < 16) + throw new ArgumentException("Cipher text must be at least 16 bytes", nameof(cipherText)); + if (key == null || key.Length != 32) + throw new ArgumentException("Key must be 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 12) + throw new ArgumentException("Nonce must be 12 bytes", nameof(nonce)); + + int cipherLength = cipherText.Length - 16; + + // 验证标签 + byte[] expectedTag = ComputePoly1305Tag(cipherText, cipherLength, key, nonce, associatedData); + byte[] actualTag = new byte[16]; + Array.Copy(cipherText, cipherLength, actualTag, 0, 16); + + if (!ConstantTimeEquals(expectedTag, actualTag)) + throw new CryptographicException("Authentication failed"); + + // 解密数据 + byte[] plainText = new byte[cipherLength]; + ProcessChaCha20(cipherText, 0, cipherLength, plainText, 0, key, nonce, 0); + + return plainText; + } + + private static byte[] ComputePoly1305Tag(byte[] cipherText, int cipherLength, byte[] key, byte[] nonce, byte[] associatedData) + { + // 简化的 Poly1305 实现 - 使用 HMAC-SHA256 作为替代 + using var hmac = new HMACSHA256(key); + + // 创建输入 + int aadLen = associatedData?.Length ?? 0; + byte[] input = new byte[16 + cipherLength + 16]; + + // Poly1305 密钥(从 ChaCha20 派生) + byte[] polyKey = new byte[32]; + ProcessChaCha20(new byte[32], 0, 32, polyKey, 0, key, nonce, 0); + + // 计算标签 + using var polyHmac = new HMACSHA256(polyKey); + byte[] data = new byte[cipherLength + 16 + aadLen + 16]; + + // AAD 长度 + BitConverter.GetBytes((ulong)aadLen).CopyTo(data, 0); + if (associatedData != null) + { + Array.Copy(associatedData, 0, data, 8, aadLen); + } + + // 密文 + Array.Copy(cipherText, 0, data, 8 + aadLen, cipherLength); + + // 密文长度 + BitConverter.GetBytes((ulong)cipherLength).CopyTo(data, 8 + aadLen + cipherLength); + + byte[] fullTag = polyHmac.ComputeHash(data); + byte[] tag = new byte[16]; + Array.Copy(fullTag, tag, 16); + + return tag; + } + + #endregion + + #region 私有方法 + + private static void ProcessChaCha20(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] key, byte[] nonce, uint counter) + { + uint[] state = new uint[16]; + uint[] block = new uint[16]; + + // 初始化状态 + state[0] = Sigma[0]; + state[1] = Sigma[1]; + state[2] = Sigma[2]; + state[3] = Sigma[3]; + + // 密钥 + for (int i = 0; i < 8; i++) + { + state[4 + i] = BitConverter.ToUInt32(key, i * 4); + } + + // 计数器 + state[12] = counter; + + // Nonce + state[13] = BitConverter.ToUInt32(nonce, 0); + state[14] = BitConverter.ToUInt32(nonce, 4); + state[15] = BitConverter.ToUInt32(nonce, 8); + + int processed = 0; + while (processed < inputLength) + { + // 复制状态到块 + Array.Copy(state, block, 16); + + // 20轮(10次双轮) + for (int i = 0; i < 10; i++) + { + // 列轮 + QuarterRound(ref block[0], ref block[4], ref block[8], ref block[12]); + QuarterRound(ref block[1], ref block[5], ref block[9], ref block[13]); + QuarterRound(ref block[2], ref block[6], ref block[10], ref block[14]); + QuarterRound(ref block[3], ref block[7], ref block[11], ref block[15]); + + // 对角线轮 + QuarterRound(ref block[0], ref block[5], ref block[10], ref block[15]); + QuarterRound(ref block[1], ref block[6], ref block[11], ref block[12]); + QuarterRound(ref block[2], ref block[7], ref block[8], ref block[13]); + QuarterRound(ref block[3], ref block[4], ref block[9], ref block[14]); + } + + // 添加原始状态 + for (int i = 0; i < 16; i++) + { + block[i] += state[i]; + } + + // XOR 输入 + int blockSize = Math.Min(64, inputLength - processed); + for (int i = 0; i < blockSize; i++) + { + output[outputOffset + processed + i] = (byte)(input[inputOffset + processed + i] ^ (block[i / 4] >> ((i % 4) * 8)) & 0xFF); + } + + processed += blockSize; + state[12]++; + } + } + + private static void QuarterRound(ref uint a, ref uint b, ref uint c, ref uint d) + { + a += b; d ^= a; d = RotateLeft(d, 16); + c += d; b ^= c; b = RotateLeft(b, 12); + a += b; d ^= a; d = RotateLeft(d, 8); + c += d; b ^= c; b = RotateLeft(b, 7); + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CityHashUtil.cs b/EasyTool.Core/CodeCategory/CityHashUtil.cs new file mode 100644 index 0000000..898e934 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CityHashUtil.cs @@ -0,0 +1,365 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// CityHash 哈希工具类 + /// CityHash 是 Google 开发的高性能哈希算法 + /// 适用于字符串哈希,不适用于密码存储 + /// + public static class CityHashUtil + { + private const ulong K0 = 0xc3a5c85c97cb3127; + private const ulong K1 = 0xb492b66fbe98f273; + private const ulong K2 = 0x9ae16a3b2f90404f; + private const ulong K3 = 0xc949d7c7509e6557; + + /// + /// 计算 CityHash64 哈希值 + /// + /// 输入数据 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return CityHash64(data, 0, (uint)data.Length); + } + + /// + /// 计算 CityHash64 哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return 0; + + return CityHash64(data, (uint)offset, (uint)length); + } + + /// + /// 计算 CityHash128 哈希值 + /// + /// 输入数据 + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data) + { + if (data == null || data.Length == 0) + return (0, 0); + + return CityHash128(data, 0, (uint)data.Length); + } + + /// + /// 计算字符串的 CityHash64 哈希值 + /// + /// 文本 + /// 64位哈希值 + public static ulong ComputeString64(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data); + } + + /// + /// 计算字符串的 CityHash128 哈希值 + /// + /// 文本 + /// 128位哈希值 + public static (ulong Low, ulong High) ComputeString128(string text) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash128(data); + } + + /// + /// 获取 CityHash64 哈希值的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data) + { + ulong hash = ComputeHash64(data); + return hash.ToString("x16"); + } + + /// + /// 获取 CityHash128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data) + { + var (low, high) = ComputeHash128(data); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 使用种子计算 CityHash64 哈希值 + /// + /// 输入数据 + /// 种子值 + /// 64位哈希值 + public static ulong ComputeHash64WithSeed(byte[] data, ulong seed) + { + if (data == null || data.Length == 0) + return seed; + + return CityHash64WithSeed(data, 0, (uint)data.Length, seed); + } + + #region 私有方法 + + private static ulong CityHash64(byte[] data, uint offset, uint length) + { + if (length <= 16) + { + return HashLen0to16(data, offset, length); + } + else if (length <= 32) + { + return HashLen17to32(data, offset, length); + } + else if (length <= 64) + { + return HashLen33to64(data, offset, length); + } + else + { + return HashLenOver64(data, offset, length); + } + } + + private static (ulong Low, ulong High) CityHash128(byte[] data, uint offset, uint length) + { + if (length >= 16) + { + return CityHash128WithSeed(data, offset, length, + ReadUInt64(data, offset), + ReadUInt64(data, offset + 8)); + } + else + { + return CityHash128WithSeed(data, offset, length, K0, K1); + } + } + + private static ulong CityHash64WithSeed(byte[] data, uint offset, uint length, ulong seed) + { + return CityHash64WithSeeds(data, offset, length, K2, seed); + } + + private static ulong CityHash64WithSeeds(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + return HashLen16(CityHash64(data, offset, length) - seed0, seed1); + } + + private static (ulong Low, ulong High) CityHash128WithSeed(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + if (length < 128) + { + return CityMurmur(data, offset, length, seed0, seed1); + } + + ulong x = seed0; + ulong y = seed1; + ulong z = length * K1; + + uint end = offset + length; + uint pos = offset; + + while (pos + 128 <= end) + { + x = RotateRight(x + y + ReadUInt64(data, pos + 8) + ReadUInt64(data, pos + 48), 43) + + RotateRight(ReadUInt64(data, pos + 40), 25) + ReadUInt64(data, pos); + y = RotateRight(y + ReadUInt64(data, pos + 16) + ReadUInt64(data, pos + 56), 36) + + RotateRight(ReadUInt64(data, pos + 24) + ReadUInt64(data, pos + 32), 19) + + ReadUInt64(data, pos + 8); + z = RotateRight(z + ReadUInt64(data, pos + 64) + ReadUInt64(data, pos + 88), 27) + + RotateRight(ReadUInt64(data, pos + 72) + ReadUInt64(data, pos + 104), 31) + + ReadUInt64(data, pos + 80); + pos += 128; + } + + z += RotateRight(z, 55); + z += RotateRight(x, 25); + z += RotateRight(y, 36); + + return CityMurmur(data, pos, end - pos, z, K2); + } + + private static (ulong Low, ulong High) CityMurmur(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + ulong a = seed0; + ulong b = seed1; + ulong c = 0; + ulong d = 0; + + if (length <= 16) + { + a = ShiftMix(a * K1) * K1; + c = b * K1 + HashLen0to16(data, offset, length); + d = ShiftMix(a + (length >= 8 ? ReadUInt64(data, offset) : c)); + } + else + { + c = HashLen16(ReadUInt64(data, offset + length - 8) + K1, a); + d = HashLen16(b + length, c + ReadUInt64(data, offset + length - 16)); + a += d; + + uint end = offset + length - 16; + uint pos = offset; + + do + { + a ^= ShiftMix(ReadUInt64(data, pos) * K1) * K1; + a *= K1; + b ^= a; + c ^= ShiftMix(ReadUInt64(data, pos + 8) * K1) * K1; + c *= K1; + d ^= c; + pos += 16; + } while (pos < end); + } + + a = HashLen16(a, c); + b = HashLen16(d, b); + + return (a ^ b, HashLen16(c, a)); + } + + private static ulong HashLen0to16(byte[] data, uint offset, uint length) + { + if (length >= 8) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) + K2; + ulong b = ReadUInt64(data, offset + length - 8); + ulong c = RotateRight(b, 37) * mul + a; + ulong d = (RotateRight(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + + if (length >= 4) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt32(data, offset); + return HashLen16(length + (a << 3), ReadUInt32(data, offset + length - 4), mul); + } + + if (length > 0) + { + byte a = data[offset]; + byte b = data[offset + (length >> 1)]; + byte c = data[offset + length - 1]; + uint y = a + ((uint)b << 8); + uint z = length + ((uint)c << 2); + return ShiftMix(y * K2 ^ z * K3) * K2; + } + + return K2; + } + + private static ulong HashLen17to32(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K1; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + + return HashLen16(RotateRight(a + b, 43) + RotateRight(c, 30) + d, + a + RotateRight(b + K2, 18) + c, mul); + } + + private static ulong HashLen33to64(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K2; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + ulong y = ReadUInt64(data, offset + 16) * mul; + ulong z = ReadUInt64(data, offset + 24) * 9; + ulong e = RotateRight(a + y, 43) + RotateRight(b, 30) + c; + ulong f = a + RotateRight(y + z, 18) + d; + + return HashLen16(e + f, HashLen16(e, f, mul), mul); + } + + private static ulong HashLenOver64(byte[] data, uint offset, uint length) + { + ulong x = ReadUInt64(data, offset); + ulong y = ReadUInt64(data, offset + length - 16) ^ K1; + ulong z = ReadUInt64(data, offset + length - 8); + + uint pos = offset; + uint end = offset + length - 16; + + while (pos + 16 <= end) + { + x = RotateRight(x + ReadUInt64(data, pos + 8), 43) + RotateRight(ReadUInt64(data, pos), 25); + y = RotateRight(y + ReadUInt64(data, pos + 8), 36); + z = RotateRight(z + ReadUInt64(data, pos), 27); + pos += 16; + } + + return HashLen16(HashLen16(x, z, K2) + y, HashLen16(y, K2 + length, K2), K2); + } + + private static ulong HashLen16(ulong u, ulong v) + { + return HashLen16(u, v, K2); + } + + private static ulong HashLen16(ulong u, ulong v, ulong mul) + { + ulong a = (u ^ v) * mul; + a ^= (a >> 47); + ulong b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; + } + + private static ulong ReadUInt64(byte[] data, uint offset) + { + return BitConverter.ToUInt64(data, (int)offset); + } + + private static uint ReadUInt32(byte[] data, uint offset) + { + return BitConverter.ToUInt32(data, (int)offset); + } + + private static ulong RotateRight(ulong x, int n) + { + return (x >> n) | (x << (64 - n)); + } + + private static ulong ShiftMix(ulong x) + { + return x ^ (x >> 47); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CrcUtil.cs b/EasyTool.Core/CodeCategory/CrcUtil.cs new file mode 100644 index 0000000..8c4cf32 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CrcUtil.cs @@ -0,0 +1,447 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// CRC(循环冗余校验)工具类 + /// 支持 CRC8、CRC16、CRC32 等多种 CRC 算法 + /// + public static class CrcUtil + { + #region CRC8 + + // CRC8 查找表 + private static readonly byte[] Crc8Table = BuildCrc8Table(0x07); // CRC-8 + + // CRC8-CCITT 查找表 + private static readonly byte[] Crc8CcittTable = BuildCrc8Table(0x07); + + // CRC8-MAXIM 查找表 + private static readonly byte[] Crc8MaximTable = BuildCrc8Table(0x31); + + // CRC8-ROHC 查找表 + private static readonly byte[] Crc8RohcTable = BuildCrc8Table(0x07); + + /// + /// 计算 CRC8 校验值 + /// + /// 数据 + /// CRC8 值 + public static byte Crc8(byte[] data) + { + return Crc8(data, 0, data.Length); + } + + /// + /// 计算 CRC8 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC8 值 + public static byte Crc8(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset >= data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte crc = 0; + for (int i = offset; i < offset + length; i++) + { + crc = Crc8Table[crc ^ data[i]]; + } + return crc; + } + + /// + /// 计算 CRC8-CCITT 校验值 + /// + /// 数据 + /// CRC8-CCITT 值 + public static byte Crc8Ccitt(byte[] data) + { + byte crc = 0; + foreach (byte b in data) + { + crc = Crc8CcittTable[crc ^ b]; + } + return crc; + } + + /// + /// 计算 CRC8-MAXIM 校验值 + /// + /// 数据 + /// CRC8-MAXIM 值 + public static byte Crc8Maxim(byte[] data) + { + byte crc = 0; + foreach (byte b in data) + { + crc = Crc8MaximTable[crc ^ b]; + } + return crc; + } + + private static byte[] BuildCrc8Table(byte polynomial) + { + var table = new byte[256]; + for (int i = 0; i < 256; i++) + { + byte crc = (byte)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 0x80) != 0 ? (byte)((crc << 1) ^ polynomial) : (byte)(crc << 1); + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC16 + + // CRC16-CCITT 查找表 + private static readonly ushort[] Crc16CcittTable = BuildCrc16Table(0x1021); + + // CRC16-MODBUS 查找表 + private static readonly ushort[] Crc16ModbusTable = BuildCrc16Table(0xA001); + + // CRC16-IBM 查找表 + private static readonly ushort[] Crc16IbmTable = BuildCrc16Table(0x8005); + + // CRC16-USB 查找表 + private static readonly ushort[] Crc16UsbTable = BuildCrc16Table(0xA001); + + /// + /// 计算 CRC16-CCITT 校验值 + /// + /// 数据 + /// CRC16-CCITT 值 + public static ushort Crc16Ccitt(byte[] data) + { + return Crc16Ccitt(data, 0, data.Length); + } + + /// + /// 计算 CRC16-CCITT 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC16-CCITT 值 + public static ushort Crc16Ccitt(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ushort crc = 0xFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (ushort)((crc << 8) ^ Crc16CcittTable[(crc >> 8) ^ data[i]]); + } + return crc; + } + + /// + /// 计算 CRC16-MODBUS 校验值 + /// + /// 数据 + /// CRC16-MODBUS 值 + public static ushort Crc16Modbus(byte[] data) + { + return Crc16Modbus(data, 0, data.Length); + } + + /// + /// 计算 CRC16-MODBUS 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC16-MODBUS 值 + public static ushort Crc16Modbus(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ushort crc = 0xFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (ushort)((crc >> 8) ^ Crc16ModbusTable[(crc ^ data[i]) & 0xFF]); + } + return crc; + } + + /// + /// 计算 CRC16-IBM 校验值 + /// + /// 数据 + /// CRC16-IBM 值 + public static ushort Crc16Ibm(byte[] data) + { + ushort crc = 0; + foreach (byte b in data) + { + crc = (ushort)((crc >> 8) ^ Crc16IbmTable[(crc ^ b) & 0xFF]); + } + return crc; + } + + /// + /// 计算 CRC16-USB 校验值 + /// + /// 数据 + /// CRC16-USB 值 + public static ushort Crc16Usb(byte[] data) + { + ushort crc = 0xFFFF; + foreach (byte b in data) + { + crc = (ushort)((crc >> 8) ^ Crc16UsbTable[(crc ^ b) & 0xFF]); + } + return (ushort)~crc; + } + + private static ushort[] BuildCrc16Table(ushort polynomial) + { + var table = new ushort[256]; + for (int i = 0; i < 256; i++) + { + ushort crc = (ushort)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ polynomial) : (ushort)(crc >> 1); + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC32 + + // CRC32 查找表(IEEE 802.3) + private static readonly uint[] Crc32Table = BuildCrc32Table(0xEDB88320); + + // CRC32-MPEG2 查找表 + private static readonly uint[] Crc32Mpeg2Table = BuildCrc32Table(0x04C11DB7); + + // CRC32C (Castagnoli) 查找表 + private static readonly uint[] Crc32CTable = BuildCrc32Table(0x82F63B78); + + /// + /// 计算 CRC32 校验值(IEEE 802.3) + /// + /// 数据 + /// CRC32 值 + public static uint Crc32(byte[] data) + { + return Crc32(data, 0, data.Length); + } + + /// + /// 计算 CRC32 校验值(IEEE 802.3) + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC32 值 + public static uint Crc32(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + uint crc = 0xFFFFFFFF; + for (int i = offset; i < offset + length; i++) + { + crc = (crc >> 8) ^ Crc32Table[(crc ^ data[i]) & 0xFF]; + } + return crc ^ 0xFFFFFFFF; + } + + /// + /// 计算 CRC32-MPEG2 校验值 + /// + /// 数据 + /// CRC32-MPEG2 值 + public static uint Crc32Mpeg2(byte[] data) + { + uint crc = 0xFFFFFFFF; + foreach (byte b in data) + { + crc = (crc << 8) ^ Crc32Mpeg2Table[((crc >> 24) ^ b) & 0xFF]; + } + return crc; + } + + /// + /// 计算 CRC32C (Castagnoli) 校验值 + /// + /// 数据 + /// CRC32C 值 + public static uint Crc32C(byte[] data) + { + uint crc = 0xFFFFFFFF; + foreach (byte b in data) + { + crc = (crc >> 8) ^ Crc32CTable[(crc ^ b) & 0xFF]; + } + return crc ^ 0xFFFFFFFF; + } + + private static uint[] BuildCrc32Table(uint polynomial) + { + var table = new uint[256]; + for (int i = 0; i < 256; i++) + { + uint crc = (uint)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1; + } + table[i] = crc; + } + return table; + } + + #endregion + + #region CRC64 + + // CRC64-ECMA 查找表 + private static readonly ulong[] Crc64Table = BuildCrc64Table(0xC96C5795D7870F42); + + /// + /// 计算 CRC64-ECMA 校验值 + /// + /// 数据 + /// CRC64 值 + public static ulong Crc64(byte[] data) + { + return Crc64(data, 0, data.Length); + } + + /// + /// 计算 CRC64-ECMA 校验值 + /// + /// 数据 + /// 起始位置 + /// 长度 + /// CRC64 值 + public static ulong Crc64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + ulong crc = 0; + for (int i = offset; i < offset + length; i++) + { + crc = Crc64Table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); + } + return crc; + } + + private static ulong[] BuildCrc64Table(ulong polynomial) + { + var table = new ulong[256]; + for (int i = 0; i < 256; i++) + { + ulong crc = (ulong)i; + for (int j = 0; j < 8; j++) + { + crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1; + } + table[i] = crc; + } + return table; + } + + #endregion + + #region 通用方法 + + /// + /// 计算校验值并返回十六进制字符串 + /// + /// 数据 + /// 算法:CRC8, CRC16, CRC32, CRC64 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, string algorithm = "CRC32") + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + switch (algorithm.ToUpperInvariant()) + { + case "CRC8": + return Crc8(data).ToString("X2"); + case "CRC8-CCITT": + return Crc8Ccitt(data).ToString("X2"); + case "CRC8-MAXIM": + return Crc8Maxim(data).ToString("X2"); + case "CRC16": + case "CRC16-CCITT": + return Crc16Ccitt(data).ToString("X4"); + case "CRC16-MODBUS": + return Crc16Modbus(data).ToString("X4"); + case "CRC16-IBM": + return Crc16Ibm(data).ToString("X4"); + case "CRC16-USB": + return Crc16Usb(data).ToString("X4"); + case "CRC32": + return Crc32(data).ToString("X8"); + case "CRC32-MPEG2": + return Crc32Mpeg2(data).ToString("X8"); + case "CRC32C": + return Crc32C(data).ToString("X8"); + case "CRC64": + return Crc64(data).ToString("X16"); + default: + throw new ArgumentException($"Unknown CRC algorithm: {algorithm}", nameof(algorithm)); + } + } + + /// + /// 验证数据校验值 + /// + /// 数据 + /// 预期的 CRC 值(十六进制字符串) + /// 算法 + /// 是否匹配 + public static bool Verify(byte[] data, string expectedCrc, string algorithm = "CRC32") + { + string computed = ComputeHex(data, algorithm); + return string.Equals(computed, expectedCrc, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 验证 CRC32 校验值 + /// + /// 数据 + /// 预期的 CRC32 值 + /// 是否匹配 + public static bool VerifyCrc32(byte[] data, uint expectedCrc) + { + return Crc32(data) == expectedCrc; + } + + /// + /// 验证 CRC16 校验值 + /// + /// 数据 + /// 预期的 CRC16 值 + /// 是否匹配 + public static bool VerifyCrc16(byte[] data, ushort expectedCrc) + { + return Crc16Ccitt(data) == expectedCrc; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/CuidUtil.cs b/EasyTool.Core/CodeCategory/CuidUtil.cs new file mode 100644 index 0000000..ea49765 --- /dev/null +++ b/EasyTool.Core/CodeCategory/CuidUtil.cs @@ -0,0 +1,399 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// CUID/CUID2 碰撞抵抗ID工具类 + /// CUID 是一种水平可扩展、碰撞抵抗的ID生成方案 + /// CUID2 是更新版本,更安全、更符合标准 + /// + public static class CuidUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly object _lock = new object(); + private static int _counter = 0; + private static string _fingerprint = null; + + // Base36 字符集 + private const string Base36Chars = "0123456789abcdefghijklmnopqrstuvwxyz"; + private const string Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + #region CUID(原始版本) + + /// + /// 生成 CUID + /// + /// 25字符的 CUID 字符串 + public static string GenerateCuid() + { + var sb = new StringBuilder(25); + + // 1. 以 'c' 开头 + sb.Append('c'); + + // 2. 时间戳(Base36) + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + sb.Append(ToBase36(timestamp)); + + // 3. 计数器(Base36,4字符) + int counter; + lock (_lock) + { + counter = _counter++; + if (_counter > 1679615) _counter = 0; // 36^4 - 1 + } + sb.Append(ToBase36(counter, 4)); + + // 4. 指纹(8字符) + sb.Append(GetFingerprint()); + + // 5. 随机字符(4字符) + sb.Append(RandomBase36(4)); + + // 6. 随机字符(4字符) + sb.Append(RandomBase36(4)); + + return sb.ToString(); + } + + /// + /// 生成带前缀的 CUID(用于分布式系统) + /// + /// 前缀(1-4字符) + /// 带前缀的 CUID + public static string GenerateCuid(string prefix) + { + if (string.IsNullOrEmpty(prefix)) + return GenerateCuid(); + + if (prefix.Length > 4) + prefix = prefix.Substring(0, 4); + + return prefix + "_" + GenerateCuid().Substring(1); + } + + /// + /// 验证 CUID 是否有效 + /// + /// CUID 字符串 + /// 是否有效 + public static bool IsValidCuid(string cuid) + { + if (string.IsNullOrEmpty(cuid) || cuid.Length < 25) + return false; + + // 检查是否以 'c' 开头或带前缀 + if (cuid[0] != 'c' && !cuid.Contains("_")) + return false; + + // 检查字符是否有效 + foreach (char c in cuid) + { + if (c == '_') continue; + if (!Base36Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + #endregion + + #region CUID2 + + /// + /// 生成 CUID2(更安全) + /// + /// 24字符的 CUID2 + public static string GenerateCuid2() + { + return GenerateCuid2(24); + } + + /// + /// 生成指定长度的 CUID2 + /// + /// 长度(2-32) + /// CUID2 字符串 + public static string GenerateCuid2(int length) + { + if (length < 2 || length > 32) + throw new ArgumentException("Length must be between 2 and 32", nameof(length)); + + // 第一部分:时间戳 + 计数器 + 指纹的哈希 + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + + int counter; + lock (_lock) + { + counter = _counter++; + } + + string entropy = RandomBase36(32); + string fingerprint = GetFingerprint(); + + string input = $"{timestamp}{counter}{fingerprint}{entropy}"; + + // 使用 SHA3 或 SHA256 哈希 + byte[] hash; + using (var sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); + } + + // 转换为 Base62 + string result = ToBase62(hash).Substring(0, length); + + return result; + } + + /// + /// 生成带熵的 CUID2 + /// + /// 长度 + /// 额外熵值 + /// CUID2 字符串 + public static string GenerateCuid2(int length, string entropy) + { + if (length < 2 || length > 32) + throw new ArgumentException("Length must be between 2 and 32", nameof(length)); + + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + + int counter; + lock (_lock) + { + counter = _counter++; + } + + string fingerprint = GetFingerprint(); + string randomEntropy = RandomBase36(32); + + string input = $"{timestamp}{counter}{fingerprint}{entropy}{randomEntropy}"; + + byte[] hash; + using (var sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); + } + + string result = ToBase62(hash).Substring(0, length); + + return result; + } + + /// + /// 验证 CUID2 是否有效 + /// + /// CUID2 字符串 + /// 是否有效 + public static bool IsValidCuid2(string cuid2) + { + if (string.IsNullOrEmpty(cuid2) || cuid2.Length < 2 || cuid2.Length > 32) + return false; + + foreach (char c in cuid2) + { + if (!Base62Chars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 判断是 CUID 还是 CUID2 + /// + /// CUID 字符串 + /// CUID 类型 + public static CuidType GetCuidType(string cuid) + { + if (string.IsNullOrEmpty(cuid)) + return CuidType.Invalid; + + if (cuid.Length == 24 && IsValidCuid2(cuid)) + return CuidType.CUID2; + + if (cuid.Length >= 25 && (cuid[0] == 'c' || cuid.Contains("_"))) + return CuidType.CUID; + + return CuidType.Invalid; + } + + #endregion + + #region Slug(短版本) + + /// + /// 生成 Slug ID(更短的唯一ID) + /// + /// 7-10字符的 Slug + public static string GenerateSlug() + { + long timestamp = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + string random = RandomBase36(4); + string counter = ToBase36(Environment.CurrentManagedThreadId % 1296, 2); + + return ToBase36(timestamp).Substring(5) + counter + random; + } + + /// + /// 验证 Slug 是否有效 + /// + /// Slug 字符串 + /// 是否有效 + public static bool IsValidSlug(string slug) + { + if (string.IsNullOrEmpty(slug) || slug.Length < 7 || slug.Length > 10) + return false; + + foreach (char c in slug) + { + if (!Base36Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + #endregion + + #region 批量生成 + + /// + /// 批量生成 CUID + /// + /// 数量 + /// CUID 数组 + public static string[] GenerateCuidBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateCuid(); + } + return result; + } + + /// + /// 批量生成 CUID2 + /// + /// 数量 + /// 每个 CUID2 的长度 + /// CUID2 数组 + public static string[] GenerateCuid2Batch(int count, int length = 24) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateCuid2(length); + } + return result; + } + + #endregion + + #region 私有方法 + + private static string GetFingerprint() + { + if (_fingerprint != null) + return _fingerprint; + + var sb = new StringBuilder(); + + // 机器标识 + sb.Append(Environment.MachineName.GetHashCode()); + + // 进程ID + sb.Append(Environment.CurrentManagedThreadId); + + // 随机部分 + sb.Append(new Random().Next(1000)); + + _fingerprint = ToBase36(Math.Abs(sb.ToString().GetHashCode()), 8); + + return _fingerprint; + } + + private static string ToBase36(long value, int padLength = 0) + { + if (value < 0) value = -value; + + var result = new StringBuilder(); + while (value > 0) + { + result.Insert(0, Base36Chars[(int)(value % 36)]); + value /= 36; + } + + if (padLength > 0 && result.Length < padLength) + { + result.Insert(0, new string('0', padLength - result.Length)); + } + + return result.ToString(); + } + + private static string ToBase62(byte[] bytes) + { + // 将字节数组转换为大整数,然后转换为 Base62 + var result = new StringBuilder(); + + // 简化处理:直接使用字节的值 + for (int i = 0; i < bytes.Length; i++) + { + int value = bytes[i] % 62; + result.Append(Base62Chars[value]); + } + + return result.ToString(); + } + + private static string RandomBase36(int length) + { + var result = new StringBuilder(length); + byte[] randomBytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(randomBytes); + + foreach (byte b in randomBytes) + { + result.Append(Base36Chars[b % 36]); + } + + return result.ToString(); + } + + #endregion + } + + /// + /// CUID 类型 + /// + public enum CuidType + { + /// + /// 无效 + /// + Invalid, + + /// + /// 原始 CUID + /// + CUID, + + /// + /// CUID2 + /// + CUID2 + } +} diff --git a/EasyTool.Core/CodeCategory/DammUtil.cs b/EasyTool.Core/CodeCategory/DammUtil.cs new file mode 100644 index 0000000..6e7859a --- /dev/null +++ b/EasyTool.Core/CodeCategory/DammUtil.cs @@ -0,0 +1,235 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Damm 算法校验和工具类 + /// Damm 算法是一种检测单个数字错误的校验和算法 + /// 由 H. Michael Damm 发明,类似于 Verhoeff 算法 + /// 使用弱完全反对称群(quasigroup) + /// + public static class DammUtil + { + // Damm 算法的运算表(10x10 弱完全反对称群) + private static readonly byte[,] Matrix = new byte[,] + { + {0, 3, 1, 7, 5, 9, 8, 6, 4, 2}, + {7, 0, 9, 2, 1, 5, 4, 8, 6, 3}, + {4, 2, 0, 6, 8, 7, 1, 3, 5, 9}, + {1, 7, 5, 0, 9, 8, 3, 4, 2, 6}, + {6, 1, 2, 3, 0, 4, 5, 9, 7, 8}, + {3, 6, 7, 4, 2, 0, 9, 5, 8, 1}, + {5, 8, 6, 9, 7, 2, 0, 1, 3, 4}, + {8, 9, 4, 5, 3, 6, 2, 0, 1, 7}, + {9, 4, 3, 8, 6, 1, 7, 2, 0, 5}, + {2, 5, 8, 1, 4, 3, 6, 7, 9, 0} + }; + + /// + /// 计算数字字符串的 Damm 校验位 + /// + /// 数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + return CalculateCheckDigit(GetDigits(number)); + } + + /// + /// 计算数字数组的 Damm 校验位 + /// + /// 数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be empty", nameof(digits)); + + int interim = 0; + + foreach (int digit in digits) + { + if (digit < 0 || digit > 9) + throw new ArgumentException($"Invalid digit: {digit}", nameof(digits)); + + interim = Matrix[interim, digit]; + } + + return interim; + } + + /// + /// 生成带校验位的数字字符串 + /// + /// 原始数字字符串 + /// 带校验位的数字字符串 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 验证带校验位的数字字符串是否有效 + /// + /// 带校验位的数字字符串 + /// 是否有效 + public static bool Validate(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return false; + + return Validate(GetDigits(numberWithCheckDigit)); + } + + /// + /// 验证带校验位的数字数组是否有效 + /// + /// 带校验位的数字数组 + /// 是否有效 + public static bool Validate(int[] digitsWithCheckDigit) + { + if (digitsWithCheckDigit == null || digitsWithCheckDigit.Length < 2) + return false; + + int interim = 0; + + foreach (int digit in digitsWithCheckDigit) + { + if (digit < 0 || digit > 9) + return false; + + interim = Matrix[interim, digit]; + } + + return interim == 0; + } + + /// + /// 从带校验位的字符串中提取原始数字 + /// + /// 带校验位的数字字符串 + /// 原始数字字符串,如果无效则返回 null + public static string ExtractNumber(string numberWithCheckDigit) + { + if (!Validate(numberWithCheckDigit)) + return null; + + return numberWithCheckDigit.Substring(0, numberWithCheckDigit.Length - 1); + } + + /// + /// 获取校验位 + /// + /// 带校验位的数字字符串 + /// 校验位,如果格式无效则返回 -1 + public static int GetCheckDigit(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return -1; + + if (!int.TryParse(numberWithCheckDigit[numberWithCheckDigit.Length - 1].ToString(), out int digit)) + return -1; + + return digit; + } + + /// + /// 生成随机数字序列并添加校验位 + /// + /// 数字序列长度(不含校验位) + /// 带校验位的随机数字字符串 + public static string GenerateRandom(int length) + { + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + var random = new Random(); + var digits = new int[length]; + + for (int i = 0; i < length; i++) + { + digits[i] = random.Next(10); + } + + int checkDigit = CalculateCheckDigit(digits); + + var result = new System.Text.StringBuilder(length + 1); + foreach (int digit in digits) + { + result.Append(digit); + } + result.Append(checkDigit); + + return result.ToString(); + } + + /// + /// 批量验证多个数字字符串 + /// + /// 数字字符串数组 + /// 验证结果数组 + public static bool[] ValidateBatch(string[] numbers) + { + if (numbers == null) + throw new ArgumentNullException(nameof(numbers)); + + var results = new bool[numbers.Length]; + for (int i = 0; i < numbers.Length; i++) + { + results[i] = Validate(numbers[i]); + } + return results; + } + + /// + /// 检测并纠正单个数字错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrect(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试纠正每个位置的错误 + for (int pos = 0; pos < numberWithCheckDigit.Length; pos++) + { + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + corrected[pos] = (char)('0' + newDigit); + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + } + + return null; + } + + private static int[] GetDigits(string number) + { + var digits = new int[number.Length]; + for (int i = 0; i < number.Length; i++) + { + if (!char.IsDigit(number[i])) + throw new ArgumentException($"Invalid character: {number[i]}", nameof(number)); + + digits[i] = number[i] - '0'; + } + return digits; + } + } +} diff --git a/EasyTool.Core/CodeCategory/DesUtil.cs b/EasyTool.Core/CodeCategory/DesUtil.cs index 94e350f..5f2058c 100644 --- a/EasyTool.Core/CodeCategory/DesUtil.cs +++ b/EasyTool.Core/CodeCategory/DesUtil.cs @@ -28,7 +28,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -55,7 +55,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; @@ -85,12 +85,13 @@ public static string Encrypt(string str, string sk,string iv, CipherMode cipher if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toEncrypt = encoding.GetBytes(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; - des.IV = keyBytes; + des.IV = ivBytes; ICryptoTransform cTransform = des.CreateEncryptor(); var resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length); @@ -115,12 +116,13 @@ public static string Decrypt(string str, string sk, string iv, CipherMode cipher if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] ivBytes = encoding.GetBytes(iv).ToArray(); byte[] toDecrypt = Convert.FromBase64String(str); - var des = DES.Create(); + using var des = DES.Create(); des.Mode = cipher; des.Padding = padding; des.Key = keyBytes; - des.IV = keyBytes; + des.IV = ivBytes; ICryptoTransform cTransform = des.CreateDecryptor(); var resultArray = cTransform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length); return encoding.GetString(resultArray); @@ -134,5 +136,69 @@ private static bool IsLegalSize(string sk) return false; } + /// + /// DES 加密(字节数组版本) + /// + /// 待加密数据 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// + /// + public static byte[] Encrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = null, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (keyBytes == null || keyBytes.Length != 8) + throw new ArgumentException("不合规的秘钥,请确认秘钥为8位"); + if (ivBytes != null && ivBytes.Length != 8) + throw new ArgumentException("不合规的IV,请确认IV为8位"); + + using var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + if (ivBytes != null) + des.IV = ivBytes; + else + des.IV = keyBytes; + + ICryptoTransform cTransform = des.CreateEncryptor(); + return cTransform.TransformFinalBlock(data, 0, data.Length); + } + + /// + /// DES 解密(字节数组版本) + /// + /// 待解密数据 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// + /// + public static byte[] Decrypt(byte[] data, byte[] keyBytes, byte[]? ivBytes = null, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (keyBytes == null || keyBytes.Length != 8) + throw new ArgumentException("不合规的秘钥,请确认秘钥为8位"); + if (ivBytes != null && ivBytes.Length != 8) + throw new ArgumentException("不合规的IV,请确认IV为8位"); + + using var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + if (ivBytes != null) + des.IV = ivBytes; + else + des.IV = keyBytes; + + ICryptoTransform cTransform = des.CreateDecryptor(); + return cTransform.TransformFinalBlock(data, 0, data.Length); + } + } } diff --git a/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs b/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs new file mode 100644 index 0000000..1ab1c31 --- /dev/null +++ b/EasyTool.Core/CodeCategory/DiffieHellmanUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Diffie-Hellman 密钥交换工具类 + /// DH 是一种安全地在公共信道上共享密钥的方法 + /// 基于离散对数问题的数学难题 + /// + public static class DiffieHellmanUtil + { + private const int DefaultKeySize = 2048; + + // 常用安全素数参数(RFC 3526) + private static readonly string[] KnownPrimes = new string[] + { + // 2048位 MODP Group (RFC 3526, Group 14) + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF" + }; + + private static readonly string[] KnownGenerators = new string[] + { + "2" + }; + + /// + /// 生成密钥对 + /// + /// 密钥长度(位) + /// 密钥对 + public static DHKeyPair GenerateKeyPair(int keySize = DefaultKeySize) + { + if (keySize < 512) + throw new ArgumentException("Key size must be at least 512 bits", nameof(keySize)); + + using var rng = RandomNumberGenerator.Create(); + + // 使用预定义的素数参数 + int primeIndex = 0; + System.Numerics.BigInteger p, g; + + if (keySize <= 2048) + { + p = System.Numerics.BigInteger.Parse(KnownPrimes[primeIndex], System.Globalization.NumberStyles.HexNumber); + g = System.Numerics.BigInteger.Parse(KnownGenerators[primeIndex], System.Globalization.NumberStyles.HexNumber); + } + else + { + // 生成自定义参数(较慢) + (p, g) = GenerateParametersInternal(keySize, rng); + } + + // 生成私钥 + byte[] xBytes = new byte[keySize / 8]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + // 计算公钥 y = g^x mod p + var y = ModPow(g, x, p); + + return new DHKeyPair(new DHParameters(p, g), x, y); + } + + /// + /// 使用指定参数生成密钥对 + /// + /// DH 参数 + /// 密钥对 + public static DHKeyPair GenerateKeyPair(DHParameters parameters) + { + using var rng = RandomNumberGenerator.Create(); + + var p = parameters.P; + var g = parameters.G; + + int keySize = p.ToByteArray().Length * 8; + + byte[] xBytes = new byte[keySize / 8]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + var y = ModPow(g, x, p); + + return new DHKeyPair(parameters, x, y); + } + + /// + /// 计算共享密钥 + /// + /// 对方的公钥 + /// 自己的私钥 + /// DH 参数 + /// 共享密钥 + public static byte[] ComputeSharedSecret(System.Numerics.BigInteger otherPublicKey, System.Numerics.BigInteger privateKey, DHParameters parameters) + { + var sharedSecret = ModPow(otherPublicKey, privateKey, parameters.P); + return sharedSecret.ToByteArray(); + } + + /// + /// 计算共享密钥并派生为指定长度的对称密钥 + /// + /// 对方的公钥 + /// 自己的私钥 + /// DH 参数 + /// 派生密钥长度(字节) + /// 盐值(可选) + /// 派生的对称密钥 + public static byte[] DeriveKey(System.Numerics.BigInteger otherPublicKey, System.Numerics.BigInteger privateKey, DHParameters parameters, int keyLength, byte[] salt = null) + { + byte[] sharedSecret = ComputeSharedSecret(otherPublicKey, privateKey, parameters); + + using var kdf = new Rfc2898DeriveBytes(sharedSecret, salt ?? new byte[16], 10000, HashAlgorithmName.SHA256); + return kdf.GetBytes(keyLength); + } + + /// + /// 验证公钥是否有效 + /// + /// 公钥 + /// DH 参数 + /// 是否有效 + public static bool ValidatePublicKey(System.Numerics.BigInteger publicKey, DHParameters parameters) + { + var p = parameters.P; + var g = parameters.G; + + // 公钥必须在 [2, p-1] 范围内 + if (publicKey < 2 || publicKey >= p) + return false; + + // 公钥不能是 p-1 的因子 + if (publicKey == p - 1) + return false; + + return true; + } + + /// + /// 生成 DH 参数 + /// + /// 密钥长度 + /// DH 参数 + public static DHParameters GenerateParameters(int keySize) + { + using var rng = RandomNumberGenerator.Create(); + var (p, g) = GenerateParametersInternal(keySize, rng); + return new DHParameters(p, g); + } + + private static (System.Numerics.BigInteger p, System.Numerics.BigInteger g) GenerateParametersInternal(int keySize, RandomNumberGenerator rng) + { + // 生成安全素数 p = 2q + 1,其中 q 也是素数 + byte[] pBytes = new byte[keySize / 8]; + System.Numerics.BigInteger p, q; + + do + { + rng.GetBytes(pBytes); + pBytes[pBytes.Length - 1] |= 0x01; // 奇数 + pBytes[0] |= 0x80; // 高位为1 + + p = new System.Numerics.BigInteger(pBytes); + p = System.Numerics.BigInteger.Abs(p); + + q = (p - 1) / 2; + + } while (!IsProbablyPrime(q) || !IsProbablyPrime(p)); + + // 找生成元 g + System.Numerics.BigInteger g = 2; + while (ModPow(g, 2, p) == 1 || ModPow(g, q, p) == 1) + { + g++; + } + + return (p, g); + } + + private static System.Numerics.BigInteger ModPow(System.Numerics.BigInteger b, System.Numerics.BigInteger e, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(b, e, m); + } + + private static bool IsProbablyPrime(System.Numerics.BigInteger n, int k = 10) + { + if (n < 2) return false; + if (n == 2 || n == 3) return true; + if (n % 2 == 0) return false; + + var d = n - 1; + int r = 0; + while (d % 2 == 0) + { + d /= 2; + r++; + } + + using var rng = RandomNumberGenerator.Create(); + byte[] bytes = n.ToByteArray(); + + for (int i = 0; i < k; i++) + { + byte[] aBytes = new byte[bytes.Length]; + rng.GetBytes(aBytes); + var a = new System.Numerics.BigInteger(aBytes); + a = System.Numerics.BigInteger.Abs(a) % (n - 3) + 2; + + var x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + for (int j = 0; j < r - 1; j++) + { + x = (x * x) % n; + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + } + + /// + /// DH 参数 + /// + public class DHParameters + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + + public DHParameters(System.Numerics.BigInteger p, System.Numerics.BigInteger g) + { + P = p; + G = g; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + + byte[] result = new byte[8 + pBytes.Length + gBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + + return result; + } + + public static DHParameters FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + + return new DHParameters( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes) + ); + } + + public string ToBase64() + { + return Convert.ToBase64String(ToByteArray()); + } + + public static DHParameters FromBase64(string base64) + { + return FromByteArray(Convert.FromBase64String(base64)); + } + } + + /// + /// DH 密钥对 + /// + public class DHKeyPair + { + public DHParameters Parameters { get; } + public System.Numerics.BigInteger PrivateKey { get; } + public System.Numerics.BigInteger PublicKey { get; } + + public DHKeyPair(DHParameters parameters, System.Numerics.BigInteger privateKey, System.Numerics.BigInteger publicKey) + { + Parameters = parameters; + PrivateKey = privateKey; + PublicKey = publicKey; + } + + /// + /// 计算与对方公钥的共享密钥 + /// + public byte[] ComputeSharedSecret(System.Numerics.BigInteger otherPublicKey) + { + return DiffieHellmanUtil.ComputeSharedSecret(otherPublicKey, PrivateKey, Parameters); + } + + /// + /// 派生对称密钥 + /// + public byte[] DeriveKey(System.Numerics.BigInteger otherPublicKey, int keyLength, byte[] salt = null) + { + return DiffieHellmanUtil.DeriveKey(otherPublicKey, PrivateKey, Parameters, keyLength, salt); + } + + /// + /// 导出公钥 + /// + public byte[] ExportPublicKey() + { + return PublicKey.ToByteArray(); + } + + /// + /// 导出公钥为 Base64 + /// + public string ExportPublicKeyBase64() + { + return Convert.ToBase64String(ExportPublicKey()); + } + + /// + /// 导出私钥(谨慎使用) + /// + public byte[] ExportPrivateKey() + { + byte[] paramBytes = Parameters.ToByteArray(); + byte[] keyBytes = PrivateKey.ToByteArray(); + + byte[] result = new byte[4 + paramBytes.Length + keyBytes.Length]; + BitConverter.GetBytes(paramBytes.Length).CopyTo(result, 0); + paramBytes.CopyTo(result, 4); + keyBytes.CopyTo(result, 4 + paramBytes.Length); + + return result; + } + + /// + /// 导入私钥 + /// + public static DHKeyPair ImportPrivateKey(byte[] data) + { + int paramLength = BitConverter.ToInt32(data, 0); + byte[] paramBytes = new byte[paramLength]; + byte[] keyBytes = new byte[data.Length - 4 - paramLength]; + + Array.Copy(data, 4, paramBytes, 0, paramLength); + Array.Copy(data, 4 + paramLength, keyBytes, 0, keyBytes.Length); + + var parameters = DHParameters.FromByteArray(paramBytes); + var privateKey = new System.Numerics.BigInteger(keyBytes); + var publicKey = System.Numerics.BigInteger.ModPow(parameters.G, privateKey, parameters.P); + + return new DHKeyPair(parameters, privateKey, publicKey); + } + } +} diff --git a/EasyTool.Core/CodeCategory/ElGamalUtil.cs b/EasyTool.Core/CodeCategory/ElGamalUtil.cs new file mode 100644 index 0000000..38d89fa --- /dev/null +++ b/EasyTool.Core/CodeCategory/ElGamalUtil.cs @@ -0,0 +1,460 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ElGamal 公钥加密工具类 + /// ElGamal 是基于离散对数问题的非对称加密算法 + /// 可用于加密和数字签名 + /// + public static class ElGamalUtil + { + private const int DefaultKeySize = 2048; + + /// + /// 生成密钥对 + /// + /// 密钥长度(位) + /// 公钥和私钥 + public static (ElGamalPublicKey PublicKey, ElGamalPrivateKey PrivateKey) GenerateKeyPair(int keySize = DefaultKeySize) + { + if (keySize < 512) + throw new ArgumentException("Key size must be at least 512 bits", nameof(keySize)); + + using var rng = RandomNumberGenerator.Create(); + + // 生成大素数 p + byte[] pBytes = new byte[keySize / 8]; + rng.GetBytes(pBytes); + pBytes[pBytes.Length - 1] |= 0x01; // 确保是奇数 + pBytes[0] |= 0x80; // 确保高位为1 + + var p = new System.Numerics.BigInteger(pBytes); + p = System.Numerics.BigInteger.Abs(p); + + // 找到下一个素数 + while (!IsProbablyPrime(p)) + { + p += 2; + } + + // 生成生成元 g(简化:使用小素数) + var g = FindGenerator(p, keySize, rng); + + // 生成私钥 x + byte[] xBytes = new byte[keySize / 8 - 1]; + rng.GetBytes(xBytes); + var x = new System.Numerics.BigInteger(xBytes); + x = System.Numerics.BigInteger.Abs(x) % (p - 2) + 1; + + // 计算公钥 y = g^x mod p + var y = ModPow(g, x, p); + + return ( + new ElGamalPublicKey(p, g, y), + new ElGamalPrivateKey(p, g, x) + ); + } + + /// + /// 加密数据 + /// + /// 明文 + /// 公钥 + /// 密文(C1 + C2) + public static byte[] Encrypt(byte[] plainText, ElGamalPublicKey publicKey) + { + if (plainText == null || plainText.Length == 0) + return Array.Empty(); + + using var rng = RandomNumberGenerator.Create(); + + var p = publicKey.P; + var g = publicKey.G; + var y = publicKey.Y; + + int keySize = p.ToByteArray().Length; + + // 将明文转换为数字 + byte[] paddedPlain = new byte[plainText.Length + 2]; + Array.Copy(plainText, paddedPlain, plainText.Length); + var m = new System.Numerics.BigInteger(paddedPlain); + + if (m >= p) + throw new ArgumentException("Message too long for key size", nameof(plainText)); + + // 生成随机数 k + byte[] kBytes = new byte[keySize - 1]; + rng.GetBytes(kBytes); + var k = new System.Numerics.BigInteger(kBytes); + k = System.Numerics.BigInteger.Abs(k) % (p - 2) + 1; + + // 计算 C1 = g^k mod p + var c1 = ModPow(g, k, p); + + // 计算 C2 = m * y^k mod p + var c2 = (m * ModPow(y, k, p)) % p; + + // 序列化 C1 和 C2 + byte[] c1Bytes = c1.ToByteArray(); + byte[] c2Bytes = c2.ToByteArray(); + + byte[] result = new byte[4 + c1Bytes.Length + 4 + c2Bytes.Length]; + BitConverter.GetBytes(c1Bytes.Length).CopyTo(result, 0); + c1Bytes.CopyTo(result, 4); + BitConverter.GetBytes(c2Bytes.Length).CopyTo(result, 4 + c1Bytes.Length); + c2Bytes.CopyTo(result, 8 + c1Bytes.Length); + + return result; + } + + /// + /// 解密数据 + /// + /// 密文 + /// 私钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, ElGamalPrivateKey privateKey) + { + if (cipherText == null || cipherText.Length < 8) + return Array.Empty(); + + var p = privateKey.P; + var x = privateKey.X; + + // 解析 C1 和 C2 + int c1Length = BitConverter.ToInt32(cipherText, 0); + int c2Length = BitConverter.ToInt32(cipherText, 4 + c1Length); + + byte[] c1Bytes = new byte[c1Length]; + byte[] c2Bytes = new byte[c2Length]; + Array.Copy(cipherText, 4, c1Bytes, 0, c1Length); + Array.Copy(cipherText, 8 + c1Length, c2Bytes, 0, c2Length); + + var c1 = new System.Numerics.BigInteger(c1Bytes); + var c2 = new System.Numerics.BigInteger(c2Bytes); + + // 计算 s = C1^x mod p + var s = ModPow(c1, x, p); + + // 计算逆元 s^-1 + var sInv = ModInverse(s, p); + + // 计算 m = C2 * s^-1 mod p + var m = (c2 * sInv) % p; + + // 转换为字节数组 + byte[] result = m.ToByteArray(); + Array.Resize(ref result, result.Length > 2 ? result.Length - 2 : 0); + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, ElGamalPublicKey publicKey) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, publicKey); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, ElGamalPrivateKey privateKey) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, privateKey); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 签名数据 + /// + public static byte[] Sign(byte[] data, ElGamalPrivateKey privateKey, System.Security.Cryptography.HashAlgorithm hashAlgorithm = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + bool shouldDisposeHash = hashAlgorithm == null; + hashAlgorithm ??= SHA256.Create(); + try + { + byte[] hash = hashAlgorithm.ComputeHash(data); + var h = new System.Numerics.BigInteger(hash); + + var p = privateKey.P; + var g = privateKey.G; + var x = privateKey.X; + + using var rng = RandomNumberGenerator.Create(); + int keySize = p.ToByteArray().Length; + + byte[] kBytes = new byte[keySize - 1]; + System.Numerics.BigInteger k; + System.Numerics.BigInteger kInv; + + do + { + rng.GetBytes(kBytes); + k = new System.Numerics.BigInteger(kBytes); + k = System.Numerics.BigInteger.Abs(k) % (p - 2) + 1; + kInv = ModInverse(k, p - 1); + } while (kInv == 0); + + var r = ModPow(g, k, p); + var s = ((h - x * r) * kInv) % (p - 1); + if (s < 0) s += p - 1; + + byte[] rBytes = r.ToByteArray(); + byte[] sBytes = s.ToByteArray(); + + byte[] result = new byte[4 + rBytes.Length + 4 + sBytes.Length]; + BitConverter.GetBytes(rBytes.Length).CopyTo(result, 0); + rBytes.CopyTo(result, 4); + BitConverter.GetBytes(sBytes.Length).CopyTo(result, 4 + rBytes.Length); + sBytes.CopyTo(result, 8 + rBytes.Length); + + return result; + } + finally + { + if (shouldDisposeHash) + hashAlgorithm.Dispose(); + } + } + + /// + /// 验证签名 + /// + public static bool Verify(byte[] data, byte[] signature, ElGamalPublicKey publicKey, System.Security.Cryptography.HashAlgorithm hashAlgorithm = null) + { + if (data == null || signature == null || signature.Length < 8) + return false; + + bool shouldDisposeHash = hashAlgorithm == null; + hashAlgorithm ??= SHA256.Create(); + try + { + byte[] hash = hashAlgorithm.ComputeHash(data); + var h = new System.Numerics.BigInteger(hash); + + var p = publicKey.P; + var g = publicKey.G; + var y = publicKey.Y; + + int rLength = BitConverter.ToInt32(signature, 0); + int sLength = BitConverter.ToInt32(signature, 4 + rLength); + + byte[] rBytes = new byte[rLength]; + byte[] sBytes = new byte[sLength]; + Array.Copy(signature, 4, rBytes, 0, rLength); + Array.Copy(signature, 8 + rLength, sBytes, 0, sLength); + + var r = new System.Numerics.BigInteger(rBytes); + var s = new System.Numerics.BigInteger(sBytes); + + // 验证: g^h ≡ y^r * r^s (mod p) + var left = ModPow(g, h, p); + var right = (ModPow(y, r, p) * ModPow(r, s, p)) % p; + + return left == right; + } + finally + { + if (shouldDisposeHash) + hashAlgorithm.Dispose(); + } + } + + private static System.Numerics.BigInteger ModPow(System.Numerics.BigInteger b, System.Numerics.BigInteger e, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(b, e, m); + } + + private static System.Numerics.BigInteger ModInverse(System.Numerics.BigInteger a, System.Numerics.BigInteger m) + { + return System.Numerics.BigInteger.ModPow(a, m - 2, m); + } + + private static System.Numerics.BigInteger FindGenerator(System.Numerics.BigInteger p, int keySize, RandomNumberGenerator rng) + { + // 简化:使用小生成元 + for (int g = 2; g < 100; g++) + { + if (ModPow(g, (p - 1) / 2, p) != 1) + { + return g; + } + } + return 2; + } + + private static bool IsProbablyPrime(System.Numerics.BigInteger n, int k = 10) + { + if (n < 2) return false; + if (n == 2 || n == 3) return true; + if (n % 2 == 0) return false; + + var d = n - 1; + int r = 0; + while (d % 2 == 0) + { + d /= 2; + r++; + } + + using var rng = RandomNumberGenerator.Create(); + int byteLength = n.ToByteArray().Length; + + for (int i = 0; i < k; i++) + { + byte[] aBytes = new byte[byteLength]; + rng.GetBytes(aBytes); + var a = new System.Numerics.BigInteger(aBytes); + a = System.Numerics.BigInteger.Abs(a) % (n - 3) + 2; + + var x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + for (int j = 0; j < r - 1; j++) + { + x = (x * x) % n; + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + } + + /// + /// ElGamal 公钥 + /// + public class ElGamalPublicKey + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + public System.Numerics.BigInteger Y { get; } + + public ElGamalPublicKey(System.Numerics.BigInteger p, System.Numerics.BigInteger g, System.Numerics.BigInteger y) + { + P = p; + G = g; + Y = y; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + byte[] yBytes = Y.ToByteArray(); + + byte[] result = new byte[12 + pBytes.Length + gBytes.Length + yBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + BitConverter.GetBytes(yBytes.Length).CopyTo(result, 8 + pBytes.Length + gBytes.Length); + yBytes.CopyTo(result, 12 + pBytes.Length + gBytes.Length); + + return result; + } + + public static ElGamalPublicKey FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + int yLength = BitConverter.ToInt32(data, 8 + pLength + gLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + byte[] yBytes = new byte[yLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + Array.Copy(data, 12 + pLength + gLength, yBytes, 0, yLength); + + return new ElGamalPublicKey( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes), + new System.Numerics.BigInteger(yBytes) + ); + } + } + + /// + /// ElGamal 私钥 + /// + public class ElGamalPrivateKey + { + public System.Numerics.BigInteger P { get; } + public System.Numerics.BigInteger G { get; } + public System.Numerics.BigInteger X { get; } + + public ElGamalPrivateKey(System.Numerics.BigInteger p, System.Numerics.BigInteger g, System.Numerics.BigInteger x) + { + P = p; + G = g; + X = x; + } + + public byte[] ToByteArray() + { + byte[] pBytes = P.ToByteArray(); + byte[] gBytes = G.ToByteArray(); + byte[] xBytes = X.ToByteArray(); + + byte[] result = new byte[12 + pBytes.Length + gBytes.Length + xBytes.Length]; + BitConverter.GetBytes(pBytes.Length).CopyTo(result, 0); + pBytes.CopyTo(result, 4); + BitConverter.GetBytes(gBytes.Length).CopyTo(result, 4 + pBytes.Length); + gBytes.CopyTo(result, 8 + pBytes.Length); + BitConverter.GetBytes(xBytes.Length).CopyTo(result, 8 + pBytes.Length + gBytes.Length); + xBytes.CopyTo(result, 12 + pBytes.Length + gBytes.Length); + + return result; + } + + public static ElGamalPrivateKey FromByteArray(byte[] data) + { + int pLength = BitConverter.ToInt32(data, 0); + int gLength = BitConverter.ToInt32(data, 4 + pLength); + int xLength = BitConverter.ToInt32(data, 8 + pLength + gLength); + + byte[] pBytes = new byte[pLength]; + byte[] gBytes = new byte[gLength]; + byte[] xBytes = new byte[xLength]; + + Array.Copy(data, 4, pBytes, 0, pLength); + Array.Copy(data, 8 + pLength, gBytes, 0, gLength); + Array.Copy(data, 12 + pLength + gLength, xBytes, 0, xLength); + + return new ElGamalPrivateKey( + new System.Numerics.BigInteger(pBytes), + new System.Numerics.BigInteger(gBytes), + new System.Numerics.BigInteger(xBytes) + ); + } + } +} diff --git a/EasyTool.Core/CodeCategory/EncodingUtil.cs b/EasyTool.Core/CodeCategory/EncodingUtil.cs new file mode 100644 index 0000000..58d7a78 --- /dev/null +++ b/EasyTool.Core/CodeCategory/EncodingUtil.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 编码工具类,提供各种编码格式的转换功能 + /// + public static class EncodingUtil + { + #region Base32 编码 + + // Base32 字符集,共 32 个字符 + private static readonly char[] BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".ToCharArray(); + + // Base32 填充字符 + private const char BASE32_PADDING_CHAR = '='; + + /// + /// 将给定的字节数组转换为 Base32 编码字符串 + /// + /// 要转换的字节数组 + /// 转换后的 Base32 编码字符串 + public static string Base32Encode(byte[] bytes) + { + if (bytes == null) + { + throw new ArgumentNullException(nameof(bytes)); + } + + int length = bytes.Length; + if (length == 0) + { + return string.Empty; + } + + char[] chars = new char[(length + 4) / 5 * 8]; + int index = 0; + for (int i = 0; i < length; i += 5) + { + int val = (bytes[i] << 32) + ((i + 1 < length ? bytes[i + 1] : 0) << 24) + + ((i + 2 < length ? bytes[i + 2] : 0) << 16) + ((i + 3 < length ? bytes[i + 3] : 0) << 8) + + ((i + 4 < length ? bytes[i + 4] : 0) << 0); + chars[index++] = BASE32_CHARS[(val >> 35) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 30) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 25) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 20) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 15) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 10) & 0x1F]; + chars[index++] = BASE32_CHARS[(val >> 5) & 0x1F]; + chars[index++] = BASE32_CHARS[val & 0x1F]; + } + + // 添加填充字符 + int paddingCount = length % 5; + if (paddingCount > 0) + { + chars[chars.Length - 1] = BASE32_PADDING_CHAR; + if (paddingCount == 1) + { + chars[chars.Length - 2] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 2) + { + chars[chars.Length - 3] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 3) + { + chars[chars.Length - 4] = BASE32_PADDING_CHAR; + } + else if (paddingCount <= 4) + { + chars[chars.Length - 5] = BASE32_PADDING_CHAR; + } + } + + return new string(chars); + } + + /// + /// 将给定的 Base32 编码字符串转换为字节数组 + /// + /// 要转换的 Base32 编码字符串 + /// 转换后的字节数组 + public static byte[] Base32Decode(string str) + { + if (string.IsNullOrEmpty(str)) + { + throw new ArgumentException("String is null or empty.", nameof(str)); + } + + // 移除填充字符 + str = str.TrimEnd('='); + + int length = str.Length; + if (length % 8 != 0) + { + throw new ArgumentException("Invalid length of input string: " + length, nameof(str)); + } + + int paddingCount = 0; + if (length > 0 && str[length - 1] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 1 && str[length - 2] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 3 && str[length - 3] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 4 && str[length - 4] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 5 && str[length - 5] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + if (length > 6 && str[length - 6] == BASE32_PADDING_CHAR) + { + paddingCount++; + } + + byte[] bytes = new byte[length / 8 * 5 - paddingCount]; + int index = 0; + for (int i = 0; i < length; i += 8) + { + int val = (DecodeBase32Char(str[i]) << 35) + + (DecodeBase32Char(str[i + 1]) << 30) + + (DecodeBase32Char(str[i + 2]) << 25) + + (DecodeBase32Char(str[i + 3]) << 20) + + (DecodeBase32Char(str[i + 4]) << 15) + + (DecodeBase32Char(str[i + 5]) << 10) + + (DecodeBase32Char(str[i + 6]) << 5) + + DecodeBase32Char(str[i + 7]); + bytes[index++] = (byte)(val >> 24); + if (index < bytes.Length) + { + bytes[index++] = (byte)(val >> 16); + } + if (index < bytes.Length) + { + bytes[index++] = (byte)(val >> 8); + } + if (index < bytes.Length) + { + bytes[index++] = (byte)val; + } + } + + return bytes; + } + + // 解码 Base32 字符 + private static int DecodeBase32Char(char c) + { + // 支持大小写 + if (c >= 'A' && c <= 'Z') + { + return c - 'A'; + } + if (c >= 'a' && c <= 'z') + { + return c - 'a'; + } + if (c >= '2' && c <= '7') + { + return c - '2' + 26; + } + throw new ArgumentException("Invalid character in input string: " + c, nameof(c)); + } + + #endregion + + #region Base62 编码 + + // Base62 字符集,共 62 个字符 + private static readonly char[] BASE62_CHARS = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); + + /// + /// 将给定的整数转换为 Base62 编码字符串 + /// + /// 要转换的整数 + /// 转换后的 Base62 编码字符串 + public static string Base62Encode(long number) + { + if (number < 0) + { + throw new ArgumentOutOfRangeException(nameof(number), "Number must be non-negative."); + } + + if (number == 0) + { + return BASE62_CHARS[0].ToString(); + } + + List chars = new List(); + int targetBase = BASE62_CHARS.Length; + while (number > 0) + { + int index = (int)(number % targetBase); + chars.Add(BASE62_CHARS[index]); + number = number / targetBase; + } + chars.Reverse(); + return new string(chars.ToArray()); + } + + /// + /// 将给定的 Base62 编码字符串转换为整数 + /// + /// 要转换的 Base62 编码字符串 + /// 转换后的整数 + public static long Base62Decode(string str) + { + if (string.IsNullOrEmpty(str)) + { + throw new ArgumentException("String is null or empty.", nameof(str)); + } + + long result = 0; + int sourceBase = BASE62_CHARS.Length; + long multiplier = 1; + for (int i = str.Length - 1; i >= 0; i--) + { + int digit = Array.IndexOf(BASE62_CHARS, str[i]); + if (digit == -1) + { + throw new ArgumentException("Invalid character in string: " + str[i], nameof(str)); + } + result += digit * multiplier; + multiplier *= sourceBase; + } + return result; + } + + #endregion + + #region ROT 加密 + + /// + /// 将给定的字符串按照 ROT 加密算法进行加密 + /// + /// 要加密的字符串 + /// 偏移量 + /// 加密后的字符串 + public static string RotEncrypt(string text, int n) + { + if (string.IsNullOrEmpty(text)) + { + return text; + } + + string upperCaseText = text.ToUpper(); + return new string(upperCaseText.Select(c => + { + if (!char.IsLetter(c)) + { + return c; + } + int x = c - 'A'; + int y = (x + n) % 26; + return (char)(y + 'A'); + }).ToArray()); + } + + /// + /// 将给定的字符串按照 ROT 加密算法进行解密 + /// + /// 要解密的字符串 + /// 偏移量 + /// 解密后的字符串 + public static string RotDecrypt(string text, int n) + { + return RotEncrypt(text, 26 - n); + } + + #endregion + + #region Morse 电码 + + // Morse 电码表 + private static readonly Dictionary MORSE_TABLE = new Dictionary + { + {'A', ".-"}, + {'B', "-..."}, + {'C', "-.-."}, + {'D', "-.."}, + {'E', "."}, + {'F', "..-."}, + {'G', "--."}, + {'H', "...."}, + {'I', ".."}, + {'J', ".---"}, + {'K', "-.-"}, + {'L', ".-.."}, + {'M', "--"}, + {'N', "-."}, + {'O', "---"}, + {'P', ".--."}, + {'Q', "--.-"}, + {'R', ".-."}, + {'S', "..."}, + {'T', "-"}, + {'U', "..-"}, + {'V', "...-"}, + {'W', ".--"}, + {'X', "-.."}, + {'Y', "-.--"}, + {'Z', "--.."}, + {'0', "-----"}, + {'1', ".----"}, + {'2', "..---"}, + {'3', "...--"}, + {'4', "....-"}, + {'5', "....."}, + {'6', "-...."}, + {'7', "--..."}, + {'8', "---.."}, + {'9', "----."}, + {' ', " "} + }; + + // Morse 反向电码表,用于解码优化性能 + private static readonly Dictionary MORSE_REVERSE_TABLE; + + static EncodingUtil() + { + MORSE_REVERSE_TABLE = new Dictionary(); + foreach (var kvp in MORSE_TABLE) + { + if (!string.IsNullOrEmpty(kvp.Value)) + { + MORSE_REVERSE_TABLE[kvp.Value] = kvp.Key; + } + } + } + + /// + /// 将给定的字符串转换为 Morse 电码字符串 + /// + /// 要转换的字符串 + /// 转换后的 Morse 电码字符串 + public static string MorseEncode(string str) + { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + + List morseCodes = new List(); + foreach (char c in str.ToUpper()) + { + if (MORSE_TABLE.ContainsKey(c)) + { + morseCodes.Add(MORSE_TABLE[c]); + } + } + return string.Join(" ", morseCodes); + } + + /// + /// 将给定的 Morse 电码字符串转换为原始字符串 + /// + /// 要转换的 Morse 电码字符串 + /// 转换后的原始字符串 + public static string MorseDecode(string morseCode) + { + if (string.IsNullOrEmpty(morseCode)) + { + return string.Empty; + } + + string[] codes = morseCode.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + StringBuilder result = new StringBuilder(codes.Length); + foreach (string code in codes) + { + if (MORSE_REVERSE_TABLE.TryGetValue(code, out char c)) + { + result.Append(c); + } + } + return result.ToString(); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/FarmHashUtil.cs b/EasyTool.Core/CodeCategory/FarmHashUtil.cs new file mode 100644 index 0000000..a172e54 --- /dev/null +++ b/EasyTool.Core/CodeCategory/FarmHashUtil.cs @@ -0,0 +1,368 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// FarmHash 哈希工具类 + /// FarmHash 是 Google 开发的高性能哈希算法,是 CityHash 的继任者 + /// 专为哈希表设计,性能优异 + /// + public static class FarmHashUtil + { + private const ulong K0 = 0xc3a5c85c97cb3127; + private const ulong K1 = 0xb492b66fbe98f273; + private const ulong K2 = 0x9ae16a3b2f90404f; + private const ulong K3 = 0xc949d7c7509e6557; + + /// + /// 计算 FarmHash64 哈希值 + /// + /// 输入数据 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return FarmHash64(data, 0, (uint)data.Length); + } + + /// + /// 计算 FarmHash64 哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return 0; + + return FarmHash64(data, (uint)offset, (uint)length); + } + + /// + /// 计算 FarmHash128 哈希值 + /// + /// 输入数据 + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data) + { + if (data == null || data.Length == 0) + return (0, 0); + + return FarmHash128(data, 0, (uint)data.Length); + } + + /// + /// 计算字符串的 FarmHash64 哈希值 + /// + /// 文本 + /// 64位哈希值 + public static ulong ComputeString64(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data); + } + + /// + /// 计算字符串的 FarmHash128 哈希值 + /// + /// 文本 + /// 128位哈希值 + public static (ulong Low, ulong High) ComputeString128(string text) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash128(data); + } + + /// + /// 获取 FarmHash64 哈希值的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data) + { + ulong hash = ComputeHash64(data); + return hash.ToString("x16"); + } + + /// + /// 获取 FarmHash128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data) + { + var (low, high) = ComputeHash128(data); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 使用种子计算 FarmHash64 哈希值 + /// + /// 输入数据 + /// 种子值 + /// 64位哈希值 + public static ulong ComputeHash64WithSeed(byte[] data, ulong seed) + { + if (data == null || data.Length == 0) + return seed; + + return FarmHash64WithSeed(data, 0, (uint)data.Length, seed); + } + + #region 私有方法 + + private static ulong FarmHash64(byte[] data, uint offset, uint length) + { + if (length <= 16) + { + return HashLen0to16(data, offset, length); + } + else if (length <= 32) + { + return HashLen17to32(data, offset, length); + } + else if (length <= 64) + { + return HashLen33to64(data, offset, length); + } + else + { + return HashLenOver64(data, offset, length); + } + } + + private static (ulong Low, ulong High) FarmHash128(byte[] data, uint offset, uint length) + { + if (length < 128) + { + return FarmHash128WithSeed(data, offset, length, 0, 0); + } + + ulong h1 = length; + ulong h2 = 0; + ulong h3 = 0; + ulong h4 = 0; + + uint pos = offset; + uint end = offset + length; + + while (pos + 128 <= end) + { + h1 += ReadUInt64(data, pos); + h2 += ReadUInt64(data, pos + 8); + h3 += ReadUInt64(data, pos + 16); + h4 += ReadUInt64(data, pos + 24); + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + h3 = ShiftMix(h3) * K3; + h4 = ShiftMix(h4) * K1; + pos += 32; + } + + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + h3 = ShiftMix(h3) * K3; + h4 = ShiftMix(h4) * K1; + + return (h1 ^ h2 ^ h3 ^ h4, (h1 + h2 + h3 + h4) * K1); + } + + private static (ulong Low, ulong High) FarmHash128WithSeed(byte[] data, uint offset, uint length, ulong seed0, ulong seed1) + { + if (length == 0) + return (seed0, seed1); + + ulong h1 = seed0; + ulong h2 = seed1; + ulong h3 = length * K1; + ulong h4 = length * K2; + + uint pos = offset; + uint end = offset + length; + + while (pos + 16 <= end) + { + h1 += ReadUInt64(data, pos); + h2 += ReadUInt64(data, pos + 8); + h1 = ShiftMix(h1) * K1; + h2 = ShiftMix(h2) * K2; + pos += 16; + } + + if (pos < end) + { + ulong remaining = 0; + for (int i = 0; pos + i < end; i++) + { + remaining |= ((ulong)data[pos + i]) << (i * 8); + } + h3 += remaining * K3; + h4 = ShiftMix(h4 + remaining) * K2; + } + + h1 = ShiftMix(h1 + h3) * K1; + h2 = ShiftMix(h2 + h4) * K2; + + return (h1 ^ h2, ShiftMix(h1 + h2) * K1); + } + + private static ulong FarmHash64WithSeed(byte[] data, uint offset, uint length, ulong seed) + { + return HashLen16(FarmHash64(data, offset, length) - K2, seed); + } + + private static ulong HashLen0to16(byte[] data, uint offset, uint length) + { + if (length >= 8) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) + K2; + ulong b = ReadUInt64(data, offset + length - 8); + ulong c = RotateRight(b, 37) * mul + a; + ulong d = (RotateRight(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + + if (length >= 4) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt32(data, offset); + return HashLen16(length + (a << 3), ReadUInt32(data, offset + length - 4), mul); + } + + if (length > 0) + { + byte a = data[offset]; + byte b = data[offset + (length >> 1)]; + byte c = data[offset + length - 1]; + uint y = a + ((uint)b << 8); + uint z = length + ((uint)c << 2); + return ShiftMix(y * K2 ^ z * K3) * K2; + } + + return K2; + } + + private static ulong HashLen17to32(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K1; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + + return HashLen16(RotateRight(a + b, 43) + RotateRight(c, 30) + d, + a + RotateRight(b + K2, 18) + c, mul); + } + + private static ulong HashLen33to64(byte[] data, uint offset, uint length) + { + ulong mul = K2 + length * 2; + ulong a = ReadUInt64(data, offset) * K2; + ulong b = ReadUInt64(data, offset + 8); + ulong c = ReadUInt64(data, offset + length - 8) * mul; + ulong d = ReadUInt64(data, offset + length - 16) * K2; + ulong y = ReadUInt64(data, offset + 16) * mul; + ulong z = ReadUInt64(data, offset + 24) * 9; + ulong e = RotateRight(a + y, 43) + RotateRight(b, 30) + c; + ulong f = a + RotateRight(y + z, 18) + d; + + return HashLen16(e + f, HashLen16(e, f, mul), mul); + } + + private static ulong HashLenOver64(byte[] data, uint offset, uint length) + { + ulong h = length; + ulong g = K1 * length; + ulong f = g; + + uint pos = offset; + uint end = offset + length; + + while (pos + 32 <= end) + { + ulong a = ReadUInt64(data, pos); + ulong b = ReadUInt64(data, pos + 8); + ulong c = ReadUInt64(data, pos + 16); + ulong d = ReadUInt64(data, pos + 24); + + h += a; + g += b; + f += c; + h = ShiftMix(h) * K1; + g = ShiftMix(g) * K2; + f = ShiftMix(f) * K3; + pos += 32; + } + + h = ShiftMix(h + f) * K1; + g = ShiftMix(g) * K2; + + if (pos < end) + { + ulong remaining = 0; + for (int i = 0; pos + i < end; i++) + { + remaining |= ((ulong)data[pos + i]) << (i * 8); + } + h += remaining * K3; + } + + return HashLen16(h, g); + } + + private static ulong HashLen16(ulong u, ulong v) + { + return HashLen16(u, v, K2); + } + + private static ulong HashLen16(ulong u, ulong v, ulong mul) + { + ulong a = (u ^ v) * mul; + a ^= (a >> 47); + ulong b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; + } + + private static ulong ReadUInt64(byte[] data, uint offset) + { + return BitConverter.ToUInt64(data, (int)offset); + } + + private static uint ReadUInt32(byte[] data, uint offset) + { + return BitConverter.ToUInt32(data, (int)offset); + } + + private static ulong RotateRight(ulong x, int n) + { + return (x >> n) | (x << (64 - n)); + } + + private static ulong ShiftMix(ulong x) + { + return x ^ (x >> 47); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/FletcherUtil.cs b/EasyTool.Core/CodeCategory/FletcherUtil.cs new file mode 100644 index 0000000..340a155 --- /dev/null +++ b/EasyTool.Core/CodeCategory/FletcherUtil.cs @@ -0,0 +1,340 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Fletcher 校验和工具类 + /// Fletcher 是一种简单的校验和算法,由 John G. Fletcher 发明 + /// 包括 Fletcher-8、Fletcher-16、Fletcher-32 和 Fletcher-64 变体 + /// 比 Adler-32 简单,但检测能力略低 + /// + public static class FletcherUtil + { + #region Fletcher-8 + + /// + /// 计算 Fletcher-8 校验和 + /// 使用 4 位累加器,生成 8 位校验和 + /// + /// 输入数据 + /// 8位校验和 + public static byte Compute8(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute8(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-8 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 8位校验和 + public static byte Compute8(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte sum1 = 0; + byte sum2 = 0; + const byte mod = 15; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (byte)((sum1 + (data[i] >> 4)) % mod); + sum2 = (byte)((sum2 + sum1) % mod); + sum1 = (byte)((sum1 + (data[i] & 0x0F)) % mod); + sum2 = (byte)((sum2 + sum1) % mod); + } + + return (byte)((sum2 << 4) | sum1); + } + + #endregion + + #region Fletcher-16 + + /// + /// 计算 Fletcher-16 校验和 + /// 使用 8 位累加器,生成 16 位校验和 + /// + /// 输入数据 + /// 16位校验和 + public static ushort Compute16(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute16(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-16 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 16位校验和 + public static ushort Compute16(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + ushort sum1 = 0; + ushort sum2 = 0; + const ushort mod = 255; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (ushort)((sum1 + data[i]) % mod); + sum2 = (ushort)((sum2 + sum1) % mod); + } + + return (ushort)((sum2 << 8) | sum1); + } + + /// + /// 获取 Fletcher-16 校验和的十六进制表示 + /// + /// 输入数据 + /// 4字符的十六进制字符串 + public static string Compute16Hex(byte[] data) + { + ushort checksum = Compute16(data); + return checksum.ToString("x4"); + } + + #endregion + + #region Fletcher-32 + + /// + /// 计算 Fletcher-32 校验和 + /// 使用 16 位累加器,生成 32 位校验和 + /// + /// 输入数据 + /// 32位校验和 + public static uint Compute32(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute32(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-32 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 32位校验和 + public static uint Compute32(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint sum1 = 0; + uint sum2 = 0; + const uint mod = 65535; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (sum1 + data[i]) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 16) | sum1; + } + + /// + /// 获取 Fletcher-32 校验和的十六进制表示 + /// + /// 输入数据 + /// 8字符的十六进制字符串 + public static string Compute32Hex(byte[] data) + { + uint checksum = Compute32(data); + return checksum.ToString("x8"); + } + + /// + /// 继续计算 Fletcher-32(支持流式处理) + /// + /// 之前的校验和 + /// 新数据 + /// 更新后的校验和 + public static uint Continue32(uint previousChecksum, byte[] data) + { + if (data == null || data.Length == 0) + return previousChecksum; + + uint sum1 = previousChecksum & 0xFFFF; + uint sum2 = (previousChecksum >> 16) & 0xFFFF; + const uint mod = 65535; + + foreach (byte b in data) + { + sum1 = (sum1 + b) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 16) | sum1; + } + + #endregion + + #region Fletcher-64 + + /// + /// 计算 Fletcher-64 校验和 + /// 使用 32 位累加器,生成 64 位校验和 + /// + /// 输入数据 + /// 64位校验和 + public static ulong Compute64(byte[] data) + { + if (data == null || data.Length == 0) + return 0; + + return Compute64(data, 0, data.Length); + } + + /// + /// 计算 Fletcher-64 校验和 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64位校验和 + public static ulong Compute64(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + ulong sum1 = 0; + ulong sum2 = 0; + const ulong mod = 4294967295; + + for (int i = offset; i < offset + length; i++) + { + sum1 = (sum1 + data[i]) % mod; + sum2 = (sum2 + sum1) % mod; + } + + return (sum2 << 32) | sum1; + } + + /// + /// 获取 Fletcher-64 校验和的十六进制表示 + /// + /// 输入数据 + /// 16字符的十六进制字符串 + public static string Compute64Hex(byte[] data) + { + ulong checksum = Compute64(data); + return checksum.ToString("x16"); + } + + #endregion + + #region 通用方法 + + /// + /// 计算字符串的 Fletcher-16 校验和 + /// + /// 文本 + /// 16位校验和 + public static ushort Compute16String(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute16(data); + } + + /// + /// 计算字符串的 Fletcher-32 校验和 + /// + /// 文本 + /// 32位校验和 + public static uint Compute32String(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return Compute32(data); + } + + /// + /// 验证数据的 Fletcher-16 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify16(byte[] data, ushort expectedChecksum) + { + return Compute16(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-32 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify32(byte[] data, uint expectedChecksum) + { + return Compute32(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-64 校验和 + /// + /// 输入数据 + /// 期望的校验和 + /// 是否匹配 + public static bool Verify64(byte[] data, ulong expectedChecksum) + { + return Compute64(data) == expectedChecksum; + } + + /// + /// 验证数据的 Fletcher-32 校验和(十六进制格式) + /// + /// 输入数据 + /// 期望的十六进制校验和 + /// 是否匹配 + public static bool Verify32Hex(byte[] data, string expectedHex) + { + if (string.IsNullOrEmpty(expectedHex)) + return false; + + string actual = Compute32Hex(data); + return string.Equals(actual, expectedHex, StringComparison.OrdinalIgnoreCase); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/GostUtil.cs b/EasyTool.Core/CodeCategory/GostUtil.cs new file mode 100644 index 0000000..adc2d62 --- /dev/null +++ b/EasyTool.Core/CodeCategory/GostUtil.cs @@ -0,0 +1,256 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// GOST 哈希工具类 + /// GOST R 34.11-94 是俄罗斯国家标准哈希算法 + /// 输出 256 位(32 字节)哈希值 + /// + public static class GostUtil + { + private const int HashSize = 32; // 256位 + private const int BlockSize = 32; // 256位 + + // S-box (测试向量使用的标准 S-box) + private static readonly byte[,] SBox = new byte[,] + { + { 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 }, + { 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 }, + { 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 }, + { 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 }, + { 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 }, + { 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 }, + { 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 }, + { 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 } + }; + + // 常量 C2-C12 + private static readonly byte[][] C = new byte[][] + { + new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + new byte[] { 0x73, 0xE2, 0x23, 0x04, 0x42, 0xB8, 0x27, 0x10, 0xC4, 0x50, 0x16, 0xEE, 0x5C, 0x7B, 0x1A, 0x11, 0xA8, 0x8E, 0xA4, 0x31, 0x6A, 0x83, 0x93, 0x62, 0x6C, 0x31, 0xF8, 0xDE, 0x36, 0xB9, 0x0B, 0x36 }, + new byte[] { 0x23, 0x7A, 0x3E, 0xA0, 0x89, 0xB9, 0x2B, 0xC4, 0xA9, 0xD6, 0x27, 0xE6, 0xC4, 0xD6, 0x80, 0xE5, 0x0C, 0x10, 0x47, 0x22, 0x49, 0xA9, 0x9D, 0xF4, 0x1D, 0x83, 0x07, 0xC1, 0x02, 0x76, 0xA8, 0x2F }, + new byte[] { 0xB9, 0x38, 0xA1, 0x6D, 0x42, 0x72, 0x9E, 0x6E, 0x4D, 0x95, 0x6D, 0x33, 0x3F, 0xEA, 0x0E, 0x26, 0x9B, 0x4D, 0x6F, 0xD6, 0xC4, 0x72, 0x8D, 0xD4, 0x2D, 0x2B, 0x0E, 0xD1, 0x5D, 0x16, 0x2F, 0x55 }, + new byte[] { 0x5C, 0x75, 0xF1, 0x8C, 0x29, 0x21, 0x6F, 0x0C, 0x9E, 0x84, 0x8A, 0x3A, 0x04, 0xF0, 0x21, 0x00, 0xDF, 0x1A, 0x2F, 0xA4, 0x4C, 0xA7, 0x4E, 0x00, 0x85, 0x38, 0x91, 0x99, 0xE9, 0x7A, 0x9D, 0x84 }, + new byte[] { 0xD4, 0x30, 0x42, 0x96, 0x6F, 0x56, 0x94, 0x6F, 0xFA, 0x0A, 0x4A, 0x2C, 0x6F, 0x90, 0x91, 0x87, 0xA4, 0x5E, 0xA8, 0xC7, 0x86, 0xFD, 0xB7, 0x51, 0x1A, 0xB4, 0x51, 0xAE, 0x3B, 0x7E, 0x6A, 0x67 }, + new byte[] { 0x03, 0xE8, 0x1D, 0x60, 0x81, 0xE3, 0xC3, 0x99, 0x3C, 0x91, 0xD5, 0xDA, 0x49, 0x76, 0x8A, 0xB6, 0x60, 0x4F, 0xB1, 0x4D, 0xE6, 0xA7, 0x8B, 0x00, 0x7F, 0x7C, 0x7E, 0xC2, 0x83, 0xD4, 0x29, 0x6F }, + new byte[] { 0xA2, 0x33, 0xB9, 0xD8, 0x08, 0x41, 0x37, 0x4E, 0xE3, 0xA5, 0xA2, 0xB6, 0xC9, 0x35, 0x78, 0xF7, 0xB3, 0x55, 0xC7, 0x83, 0xC5, 0x54, 0x37, 0x94, 0x7D, 0x58, 0x34, 0x65, 0xB2, 0xCB, 0x1A, 0x2D }, + new byte[] { 0x68, 0x83, 0x2B, 0xC7, 0xCC, 0x5C, 0x59, 0x46, 0x9F, 0xBE, 0x7A, 0x42, 0x42, 0x14, 0xB8, 0x90, 0x6D, 0xE4, 0x58, 0xED, 0x0E, 0x59, 0x6D, 0x8E, 0x6B, 0x7E, 0x2C, 0x8F, 0xB8, 0x2D, 0x93, 0x6B }, + new byte[] { 0xD4, 0x62, 0xE2, 0x41, 0x0F, 0x0F, 0x21, 0xDA, 0x76, 0xA5, 0xE9, 0x69, 0x94, 0x0D, 0x6F, 0xA3, 0xFB, 0x64, 0x59, 0x51, 0x9C, 0xAD, 0xBA, 0x71, 0x8B, 0x40, 0x6B, 0xA4, 0x68, 0x54, 0x51, 0xF7 }, + new byte[] { 0x1A, 0x2E, 0x0C, 0x47, 0xA5, 0x70, 0x9F, 0x24, 0x9C, 0xD0, 0x96, 0xB7, 0xC1, 0x65, 0x00, 0x96, 0x6C, 0x8B, 0xA3, 0x71, 0xB9, 0x1E, 0xB8, 0x5C, 0x1D, 0x36, 0x30, 0xA5, 0xA2, 0xB0, 0x35, 0xB5 }, + new byte[] { 0x4D, 0x04, 0x23, 0xE7, 0x68, 0x2E, 0x3D, 0x77, 0xCB, 0x6A, 0x0E, 0xF4, 0x5A, 0x88, 0x5B, 0x28, 0xDF, 0x1D, 0xD1, 0x9F, 0x21, 0xBA, 0x08, 0x0A, 0x95, 0xFB, 0x6D, 0x65, 0xA5, 0x6C, 0xA6, 0x3D }, + new byte[] { 0x11, 0x35, 0xF5, 0x71, 0x2F, 0xD6, 0x12, 0xD4, 0x6D, 0x9C, 0xF5, 0xE7, 0xBC, 0x3B, 0xEC, 0x03, 0x3F, 0x7D, 0x66, 0x36, 0x0A, 0xFB, 0xBA, 0x66, 0x2D, 0x5F, 0x96, 0x7D, 0x07, 0x12, 0x2D, 0x11 } + }; + + /// + /// 计算 GOST 哈希值 + /// + /// 输入数据 + /// 32字节哈希值 + public static byte[] Hash(byte[] data) + { + if (data == null || data.Length == 0) + return new byte[HashSize]; + + byte[] h = new byte[32]; + byte[] sigma = new byte[32]; + byte[] m = new byte[32]; + + // 初始化 H 和 Sigma + ulong length = (ulong)data.Length * 8; + int checksum = 0; + + int pos = 0; + while (pos < data.Length) + { + int len = Math.Min(32, data.Length - pos); + Array.Copy(data, pos, m, 0, len); + if (len < 32) Array.Clear(m, len, 32 - len); + + h = Step(h, m); + sigma = Add(sigma, m); + checksum = (checksum + len) & 0xFF; + pos += 32; + } + + // 最终化 + m = BitConverter.GetBytes(length); + Array.Resize(ref m, 32); + h = Step(h, m); + h = Step(h, sigma); + + return h; + } + + /// + /// 计算字符串的 GOST 哈希值 + /// + /// 输入文本 + /// 十六进制哈希字符串 + public static string HashString(string text) + { + if (string.IsNullOrEmpty(text)) + return new string('0', HashSize * 2); + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] hash = Hash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 验证哈希值 + /// + /// 原始数据 + /// 预期哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] hash) + { + if (hash == null || hash.Length != HashSize) + return false; + + byte[] computed = Hash(data); + return SlowEquals(computed, hash); + } + + /// + /// 验证字符串哈希 + /// + /// 原始文本 + /// 预期哈希值(十六进制) + /// 是否匹配 + public static bool VerifyString(string text, string hashHex) + { + if (string.IsNullOrEmpty(hashHex) || hashHex.Length != HashSize * 2) + return false; + + string computed = HashString(text); + return string.Equals(computed, hashHex, StringComparison.OrdinalIgnoreCase); + } + + private static byte[] Step(byte[] h, byte[] m) + { + byte[] u = (byte[])h.Clone(); + byte[] v = (byte[])m.Clone(); + byte[] w = new byte[32]; + + // Key generation + byte[] k = P(u, v); + + // Encryption + byte[] s = E(k, h); + + // Mixing + for (int i = 0; i < 12; i++) + { + w = X(u, v); + u = A(u); + v = A(A(v)); + } + + w = X(u, v); + s = X(s, w); + + return Psi(s, 12) ?? s; + } + + private static byte[] P(byte[] u, byte[] v) + { + byte[] k = new byte[32]; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 8; j++) + { + k[i * 8 + j] = (byte)(u[i + j * 4] ^ v[i + j * 4]); + } + } + return k; + } + + private static byte[] A(byte[] x) + { + byte[] result = new byte[32]; + for (int i = 0; i < 8; i++) + { + byte c = 0; + for (int j = 7; j >= 0; j--) + { + byte newC = (byte)(x[i * 4 + j] >> 7); + result[i * 4 + j] = (byte)((x[i * 4 + j] << 1) | c); + c = newC; + } + } + return result; + } + + private static byte[] X(byte[] a, byte[] b) + { + byte[] result = new byte[32]; + for (int i = 0; i < 32; i++) + result[i] = (byte)(a[i] ^ b[i]); + return result; + } + + private static byte[] Add(byte[] a, byte[] b) + { + byte[] result = new byte[32]; + int carry = 0; + for (int i = 0; i < 32; i++) + { + int sum = a[i] + b[i] + carry; + result[i] = (byte)(sum & 0xFF); + carry = sum >> 8; + } + return result; + } + + private static byte[] E(byte[] k, byte[] h) + { + byte[] result = new byte[32]; + for (int i = 0; i < 32; i += 8) + { + ulong block = BitConverter.ToUInt64(k, i); + ulong hBlock = BitConverter.ToUInt64(h, i); + block ^= hBlock; + + // S-box substitution + byte[] bytes = BitConverter.GetBytes(block); + for (int j = 0; j < 8; j++) + { + int row = j; + int col = (bytes[j] >> 4) & 0x0F; + bytes[j] = SBox[row, col]; + } + + Array.Copy(bytes, 0, result, i, 8); + } + return result; + } + + private static byte[] Psi(byte[] x, int n) + { + if (x == null) return null; + byte[] result = (byte[])x.Clone(); + + for (int i = 0; i < n; i++) + { + byte tmp = result[0]; + for (int j = 0; j < 31; j++) + result[j] = result[j + 1]; + result[31] = tmp; + } + + return result; + } + + private static bool SlowEquals(byte[] a, byte[] b) + { + uint diff = (uint)a.Length ^ (uint)b.Length; + for (int i = 0; i < a.Length && i < b.Length; i++) + diff |= (uint)(a[i] ^ b[i]); + return diff == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/GrayCodeUtil.cs b/EasyTool.Core/CodeCategory/GrayCodeUtil.cs new file mode 100644 index 0000000..dd4a9af --- /dev/null +++ b/EasyTool.Core/CodeCategory/GrayCodeUtil.cs @@ -0,0 +1,291 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// 格雷码(Gray Code)工具类 + /// 格雷码是一种二进制数系统,相邻的两个数之间只有一个位不同 + /// 由 Frank Gray 发明,常用于位置编码、错误检测等 + /// + public static class GrayCodeUtil + { + /// + /// 将二进制数转换为格雷码 + /// + /// 二进制数 + /// 格雷码 + public static uint BinaryToGray(uint binary) + { + return binary ^ (binary >> 1); + } + + /// + /// 将格雷码转换为二进制数 + /// + /// 格雷码 + /// 二进制数 + public static uint GrayToBinary(uint gray) + { + uint binary = gray; + binary ^= (binary >> 1); + binary ^= (binary >> 2); + binary ^= (binary >> 4); + binary ^= (binary >> 8); + binary ^= (binary >> 16); + return binary; + } + + /// + /// 将二进制数转换为格雷码(64位) + /// + /// 二进制数 + /// 格雷码 + public static ulong BinaryToGray64(ulong binary) + { + return binary ^ (binary >> 1); + } + + /// + /// 将格雷码转换为二进制数(64位) + /// + /// 格雷码 + /// 二进制数 + public static ulong GrayToBinary64(ulong gray) + { + ulong binary = gray; + binary ^= (binary >> 1); + binary ^= (binary >> 2); + binary ^= (binary >> 4); + binary ^= (binary >> 8); + binary ^= (binary >> 16); + binary ^= (binary >> 32); + return binary; + } + + /// + /// 将整数转换为格雷码二进制字符串 + /// + /// 整数值 + /// 位数 + /// 格雷码二进制字符串 + public static string ToGrayBinaryString(int value, int bits = 8) + { + if (bits < 1 || bits > 32) + throw new ArgumentException("Bits must be between 1 and 32", nameof(bits)); + + uint gray = BinaryToGray((uint)value); + return Convert.ToString(gray, 2).PadLeft(bits, '0'); + } + + /// + /// 生成 n 位格雷码序列 + /// + /// 位数 + /// 格雷码序列 + public static uint[] GenerateSequence(int n) + { + if (n < 1 || n > 32) + throw new ArgumentException("N must be between 1 and 32", nameof(n)); + + int count = 1 << n; + uint[] result = new uint[count]; + + for (int i = 0; i < count; i++) + { + result[i] = BinaryToGray((uint)i); + } + + return result; + } + + /// + /// 生成 n 位格雷码二进制字符串序列 + /// + /// 位数 + /// 格雷码二进制字符串序列 + public static string[] GenerateBinarySequence(int n) + { + if (n < 1 || n > 32) + throw new ArgumentException("N must be between 1 and 32", nameof(n)); + + int count = 1 << n; + string[] result = new string[count]; + + for (int i = 0; i < count; i++) + { + uint gray = BinaryToGray((uint)i); + result[i] = Convert.ToString(gray, 2).PadLeft(n, '0'); + } + + return result; + } + + /// + /// 计算两个格雷码之间的汉明距离 + /// + /// 第一个格雷码 + /// 第二个格雷码 + /// 汉明距离 + public static int HammingDistance(uint gray1, uint gray2) + { + // 格雷码的汉明距离等于它们的异或值的位数 + uint xor = gray1 ^ gray2; + int distance = 0; + + while (xor != 0) + { + distance++; + xor &= (xor - 1); // 清除最低位1 + } + + return distance; + } + + /// + /// 检查两个格雷码是否相邻 + /// + /// 第一个格雷码 + /// 第二个格雷码 + /// 是否相邻 + public static bool AreAdjacent(uint gray1, uint gray2) + { + return HammingDistance(gray1, gray2) == 1; + } + + /// + /// 获取格雷码的下一个值 + /// + /// 当前格雷码 + /// 下一个格雷码 + public static uint Next(uint gray) + { + uint binary = GrayToBinary(gray); + return BinaryToGray(binary + 1); + } + + /// + /// 获取格雷码的前一个值 + /// + /// 当前格雷码 + /// 前一个格雷码 + public static uint Previous(uint gray) + { + uint binary = GrayToBinary(gray); + if (binary == 0) + return 0; + + return BinaryToGray(binary - 1); + } + + /// + /// 计算格雷码的奇偶性 + /// + /// 格雷码 + /// 奇偶性(true = 奇数,false = 偶数) + public static bool Parity(uint gray) + { + // 格雷码的奇偶性与最高位相同 + uint binary = GrayToBinary(gray); + return (binary & 1) == 1; + } + + /// + /// 将字节转换为格雷码 + /// + /// 字节数据 + /// 格雷码字节 + public static byte ByteToGray(byte data) + { + return (byte)BinaryToGray(data); + } + + /// + /// 将格雷码字节转换为普通字节 + /// + /// 格雷码字节 + /// 普通字节 + public static byte GrayToByte(byte gray) + { + return (byte)GrayToBinary(gray); + } + + /// + /// 将字节数组转换为格雷码 + /// + /// 字节数组 + /// 格雷码字节数组 + public static byte[] EncodeBytes(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = ByteToGray(data[i]); + } + return result; + } + + /// + /// 将格雷码字节数组转换为普通字节数组 + /// + /// 格雷码字节数组 + /// 普通字节数组 + public static byte[] DecodeBytes(byte[] grayData) + { + if (grayData == null) + throw new ArgumentNullException(nameof(grayData)); + + byte[] result = new byte[grayData.Length]; + for (int i = 0; i < grayData.Length; i++) + { + result[i] = GrayToByte(grayData[i]); + } + return result; + } + + /// + /// 计算格雷码的位翻转位置(相对于前一个值) + /// + /// 格雷码 + /// 位翻转位置(0-based),如果是0则返回-1 + public static int GetBitFlipPosition(uint gray) + { + if (gray == 0) + return -1; + + // 找到最低位1的位置 + int position = 0; + uint temp = gray; + + while ((temp & 1) == 0) + { + temp >>= 1; + position++; + } + + return position; + } + + /// + /// 获取格雷码对应的十进制值 + /// + /// 格雷码 + /// 十进制值 + public static uint ToDecimal(uint gray) + { + return GrayToBinary(gray); + } + + /// + /// 从十进制值创建格雷码 + /// + /// 十进制值 + /// 格雷码 + public static uint FromDecimal(uint @decimal) + { + return BinaryToGray(@decimal); + } + } +} diff --git a/EasyTool.Core/ToolCategory/HashUtil.cs b/EasyTool.Core/CodeCategory/HashUtil.cs similarity index 76% rename from EasyTool.Core/ToolCategory/HashUtil.cs rename to EasyTool.Core/CodeCategory/HashUtil.cs index 6913e7a..b6915f4 100644 --- a/EasyTool.Core/ToolCategory/HashUtil.cs +++ b/EasyTool.Core/CodeCategory/HashUtil.cs @@ -1,8 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// /// hash算法工具类 @@ -14,13 +14,17 @@ public class HashUtil /// /// 要进行hash的字符串 /// 返回hash值 - public static uint AdditiveHash(string str) + public static uint AdditiveHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { hash += c; } + return hash; } @@ -29,13 +33,17 @@ public static uint AdditiveHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint RotatingHash(string str) + public static uint RotatingHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = (uint)str.Length; foreach (char c in str) { hash = (hash << 4) ^ (hash >> 28) ^ c; } + return hash; } @@ -44,8 +52,11 @@ public static uint RotatingHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint OneByOneHash(string str) + public static uint OneByOneHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { @@ -53,6 +64,7 @@ public static uint OneByOneHash(string str) hash += (hash << 10); hash ^= (hash >> 6); } + hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); @@ -64,13 +76,17 @@ public static uint OneByOneHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint Bernstein(string str) + public static uint Bernstein(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 5381; foreach (char c in str) { hash = 33 * hash + c; } + return hash; } @@ -81,14 +97,22 @@ public static uint Bernstein(string str) /// 大质数 /// 哈希桶的数量 /// a的取值范围为[1, prime - 1] - /// b的取值范围 - public static uint Universal(string str, uint prime, uint num_buckets, uint a, uint b) + /// b的取值范围 + public static uint Universal(string? str, uint prime, uint num_buckets, uint a, uint b) { + if (string.IsNullOrEmpty(str)) + return 0; + if (prime == 0) + throw new ArgumentException("Prime must be greater than 0", nameof(prime)); + if (num_buckets == 0) + throw new ArgumentException("Number of buckets must be greater than 0", nameof(num_buckets)); + uint hash = a; foreach (char c in str) { hash = hash * prime + c; } + hash = (hash * a + b) % num_buckets; return hash; } @@ -99,13 +123,20 @@ public static uint Universal(string str, uint prime, uint num_buckets, uint a, u /// 要进行hash的字符串 /// 随机数表 /// 返回hash值 - public static uint Zobrist(string str, uint[] table) + public static uint Zobrist(string? str, uint[]? table) { + if (string.IsNullOrEmpty(str)) + return 0; + if (table == null || table.Length == 0) + throw new ArgumentException("Table cannot be null or empty", nameof(table)); + uint hash = 0; for (int i = 0; i < str.Length; i++) { - hash ^= table[str[i]]; + int index = Math.Min(str[i], table.Length - 1); + hash ^= table[index]; } + return hash; } @@ -114,8 +145,11 @@ public static uint Zobrist(string str, uint[] table) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint FnvHash(string str) + public static uint FnvHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + const uint fnv_prime = 0x811C9DC5; uint hash = 0; foreach (char c in str) @@ -123,6 +157,7 @@ public static uint FnvHash(string str) hash *= fnv_prime; hash ^= c; } + return hash; } @@ -149,14 +184,20 @@ public static uint IntHash(uint key) /// b的取值范围为[1, 255] /// a的取值范围为[1, b-1] /// 返回hash值 - public static uint RsHash(string str, uint b, uint a) + public static uint RsHash(string? str, uint b, uint a) { + if (string.IsNullOrEmpty(str)) + return 0; + if (b == 0) + throw new ArgumentException("b must be greater than 0", nameof(b)); + uint hash = 0; foreach (char c in str) { hash = hash * a + c; a = a * b; } + return hash; } @@ -165,13 +206,17 @@ public static uint RsHash(string str, uint b, uint a) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint JsHash(string str) + public static uint JsHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 1315423911; foreach (char c in str) { hash ^= ((hash << 5) + c + (hash >> 2)); } + return hash; } @@ -180,8 +225,11 @@ public static uint JsHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint PjwHash(string str) + public static uint PjwHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; const uint BitsInUnsignedInt = (uint)(sizeof(uint) * 8); const uint ThreeQuarters = (uint)((BitsInUnsignedInt * 3) / 4); @@ -196,6 +244,7 @@ public static uint PjwHash(string str) hash = ((hash ^ (test >> (int)ThreeQuarters)) & (~HighBits)); } } + return hash; } @@ -204,8 +253,11 @@ public static uint PjwHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint ElfHash(string str) + public static uint ElfHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; uint x = 0; foreach (char c in str) @@ -216,8 +268,10 @@ public static uint ElfHash(string str) { hash ^= (x >> 24); } + hash &= ~x; } + return hash; } @@ -227,13 +281,19 @@ public static uint ElfHash(string str) /// 要进行hash的字符串 /// 种子值 /// 返回hash值 - public static uint BkdrHash(string str, uint seed) + public static uint BkdrHash(string? str, uint seed) { + if (string.IsNullOrEmpty(str)) + return 0; + if (seed == 0) + throw new ArgumentException("Seed must be greater than 0", nameof(seed)); + uint hash = 0; foreach (char c in str) { hash = hash * seed + c; } + return hash; } @@ -242,13 +302,17 @@ public static uint BkdrHash(string str, uint seed) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint SdbmHash(string str) + public static uint SdbmHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; foreach (char c in str) { hash = c + (hash << 6) + (hash << 16) - hash; } + return hash; } @@ -257,13 +321,17 @@ public static uint SdbmHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint DjbHash(string str) + public static uint DjbHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 5381; foreach (char c in str) { hash = ((hash << 5) + hash) + c; } + return hash; } @@ -272,13 +340,17 @@ public static uint DjbHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint DekHash(string str) + public static uint DekHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = (uint)str.Length; foreach (char c in str) { hash = ((hash << 5) ^ (hash >> 27)) ^ c; } + return hash; } @@ -287,8 +359,11 @@ public static uint DekHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint ApHash(string str) + public static uint ApHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; int i; for (i = 0; i < str.Length; i++) @@ -302,6 +377,7 @@ public static uint ApHash(string str) hash ^= (~((hash << 11) ^ str[i] ^ (hash >> 5))); } } + return hash; } @@ -311,14 +387,17 @@ public static uint ApHash(string str) /// 要进行hash的字符串 /// hash表的长度 /// 返回hash值 - public static uint TianlHash(string str, uint len) + public static uint TianlHash(string? str, uint len) { + if (string.IsNullOrEmpty(str)) + return 0; + if (len == 0) + throw new ArgumentException("Length must be greater than 0", nameof(len)); + uint hash = 0; uint[] w = new uint[64]; uint[] v = new uint[8]; - if (str.Length == 0) return 0; - if (str.Length <= 64) { for (int i = 0; i < str.Length; i++) @@ -327,6 +406,7 @@ public static uint TianlHash(string str, uint len) hash += (hash << 10); hash ^= (hash >> 6); } + hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); @@ -385,14 +465,18 @@ public static uint TianlHash(string str, uint len) /// /// 要进行hash的字符串 /// 返回hash值 - public static uint JavaDefaultHash(string str) + public static uint JavaDefaultHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint hash = 0; uint h = hash; foreach (char c in str) { h = 31 * h + c; } + hash = h; return hash; } @@ -402,8 +486,11 @@ public static uint JavaDefaultHash(string str) /// /// 要进行hash的字符串 /// 返回hash值 - public static ulong MixHash(string str) + public static ulong MixHash(string? str) { + if (string.IsNullOrEmpty(str)) + return 0; + uint seed = 131; // 31 131 1313 13131 131313 etc.. ulong hash1 = 0; ulong hash2 = 0; @@ -412,8 +499,9 @@ public static ulong MixHash(string str) hash1 = (hash1 * seed) + c; hash2 = (hash2 * seed) + c + 1; } + return hash1 + (hash2 * 1566083941); } } - } +} diff --git a/EasyTool.Core/ToolCategory/HexUtil.cs b/EasyTool.Core/CodeCategory/HexUtil.cs similarity index 86% rename from EasyTool.Core/ToolCategory/HexUtil.cs rename to EasyTool.Core/CodeCategory/HexUtil.cs index 1bc1079..e3f9870 100644 --- a/EasyTool.Core/ToolCategory/HexUtil.cs +++ b/EasyTool.Core/CodeCategory/HexUtil.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// /// 16进制工具类 /// - public class HexUtil + public static class HexUtil { /// /// 将16进制字符串转换为字节数组 @@ -32,16 +32,53 @@ public static byte[] HexToBytes(string hex) /// 将字节数组转换为16进制字符串 /// /// 字节数组 + /// 是否使用小写(默认大写) /// 16进制字符串 - public static string BytesToHex(byte[] bytes) + public static string BytesToHex(byte[] bytes, bool lowercase = false) { - StringBuilder sb = new StringBuilder(); + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + StringBuilder sb = new StringBuilder(bytes.Length * 2); + string format = lowercase ? "x2" : "X2"; foreach (byte b in bytes) - sb.Append(b.ToString("X2")); + sb.Append(b.ToString(format)); return sb.ToString(); } + /// + /// 将16进制字符串转换为字节数组(安全版本,不抛出异常) + /// + /// 16进制字符串 + /// 转换后的字节数组 + /// 是否转换成功 + public static bool TryHexToBytes(string hex, out byte[]? bytes) + { + bytes = null; + if (string.IsNullOrWhiteSpace(hex)) + return false; + + hex = hex.Replace(" ", ""); + if (hex.Length % 2 != 0) + return false; + + try + { + bytes = new byte[hex.Length / 2]; + for (int i = 0; i < hex.Length; i += 2) + { + if (!byte.TryParse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber, null, out bytes[i / 2])) + return false; + } + return true; + } + catch + { + return false; + } + } + /// /// 比较两个16进制字符串是否相等 /// @@ -72,7 +109,25 @@ public static bool HexEquals(string hex1, string hex2) /// 十进制数值 public static int HexToInt(string hex) { - return Convert.ToInt32(hex, 16); + try + { + return Convert.ToInt32(hex, 16); + } + catch (FormatException ex) + { + throw new ArgumentException("Invalid hex string: " + hex, nameof(hex), ex); + } + } + + /// + /// 将16进制字符串转换为十进制数值(安全版本) + /// + /// 16进制字符串 + /// 转换后的数值 + /// 是否转换成功 + public static bool TryHexToInt(string hex, out int result) + { + return int.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out result); } /// @@ -86,24 +141,15 @@ public static string IntToHex(int number) } /// - /// 将16进制字符串中的所有字符转换为大写 + /// 将16进制字符串转换为大写形式 /// /// 16进制字符串 - /// 大写16进制字符串 + /// 大写形式的16进制字符串 public static string HexToUpper(string hex) { return hex.ToUpper(); } - /// - /// 将16进制字符串中的所有字符转换为小写 - /// - /// 16进制字符串 - /// 小写16进制字符串 - public static string HexToLower(string hex) - { - return hex.ToLower(); - } /// /// 获取16进制字符串中指定位置的字符 diff --git a/EasyTool.Core/CodeCategory/HmacUtil.cs b/EasyTool.Core/CodeCategory/HmacUtil.cs new file mode 100644 index 0000000..a014a91 --- /dev/null +++ b/EasyTool.Core/CodeCategory/HmacUtil.cs @@ -0,0 +1,255 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// HMAC(基于哈希的消息认证码)工具类 + /// 提供各种哈希算法的 HMAC 实现 + /// + public static class HmacUtil + { + #region HMAC-MD5 + + /// + /// 计算 HMAC-MD5 + /// + /// 数据 + /// 密钥 + /// HMAC-MD5 哈希值(十六进制字符串) + public static string HmacMD5(byte[] data, byte[] key) + { + using var hmac = new HMACMD5(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-MD5 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-MD5 哈希值(十六进制字符串) + public static string HmacMD5(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacMD5(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-MD5 + /// + /// 数据 + /// 密钥 + /// 期望的哈希值(十六进制字符串) + /// 是否匹配 + public static bool VerifyHmacMD5(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacMD5(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA1 + + /// + /// 计算 HMAC-SHA1 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA1 哈希值(十六进制字符串) + public static string HmacSHA1(byte[] data, byte[] key) + { + using var hmac = new HMACSHA1(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA1 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-SHA1 哈希值(十六进制字符串) + public static string HmacSHA1(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA1(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA1 + /// + public static bool VerifyHmacSHA1(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA1(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA256 + + /// + /// 计算 HMAC-SHA256 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA256 哈希值(十六进制字符串) + public static string HmacSHA256(byte[] data, byte[] key) + { + using var hmac = new HMACSHA256(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA256 + /// + /// 文本 + /// 密钥 + /// 编码 + /// HMAC-SHA256 哈希值(十六进制字符串) + public static string HmacSHA256(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA256(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA256 + /// + public static bool VerifyHmacSHA256(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA256(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA384 + + /// + /// 计算 HMAC-SHA384 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA384 哈希值(十六进制字符串) + public static string HmacSHA384(byte[] data, byte[] key) + { + using var hmac = new HMACSHA384(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA384 + /// + public static string HmacSHA384(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA384(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA384 + /// + public static bool VerifyHmacSHA384(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA384(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region HMAC-SHA512 + + /// + /// 计算 HMAC-SHA512 + /// + /// 数据 + /// 密钥 + /// HMAC-SHA512 哈希值(十六进制字符串) + public static string HmacSHA512(byte[] data, byte[] key) + { + using var hmac = new HMACSHA512(key); + var hash = hmac.ComputeHash(data); + return HexUtil.BytesToHex(hash); + } + + /// + /// 计算 HMAC-SHA512 + /// + public static string HmacSHA512(string text, string key, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return HmacSHA512(encoding.GetBytes(text), encoding.GetBytes(key)); + } + + /// + /// 验证 HMAC-SHA512 + /// + public static bool VerifyHmacSHA512(byte[] data, byte[] key, string expectedHash) + { + var actualHash = HmacSHA512(data, key); + return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 通用方法 + + /// + /// 生成随机密钥 + /// + /// 密钥大小(字节) + /// 随机密钥 + public static byte[] GenerateKey(int size = 32) + { + using var rng = RandomNumberGenerator.Create(); + var key = new byte[size]; + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥(Base64 编码) + /// + /// 密钥大小(字节) + /// Base64 编码的随机密钥 + public static string GenerateKeyBase64(int size = 32) + { + var key = GenerateKey(size); + return Convert.ToBase64String(key); + } + + /// + /// 使用时间安全的比较方法验证 HMAC + /// + /// 实际值 + /// 期望值 + /// 是否匹配 + public static bool ConstantTimeEquals(byte[] actual, byte[] expected) + { + if (actual == null || expected == null) + return false; + + if (actual.Length != expected.Length) + return false; + + int result = 0; + for (int i = 0; i < actual.Length; i++) + { + result |= actual[i] ^ expected[i]; + } + + return result == 0; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/CodeCategory/IDEAUtil.cs b/EasyTool.Core/CodeCategory/IDEAUtil.cs new file mode 100644 index 0000000..04c6e44 --- /dev/null +++ b/EasyTool.Core/CodeCategory/IDEAUtil.cs @@ -0,0 +1,294 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// IDEA (International Data Encryption Algorithm) 对称加密工具类 + /// IDEA 是一种分组密码,曾用于 PGP + /// 64位分组密码,使用 128 位密钥 + /// + public static class IDEAUtil + { + private const int BlockSize = 8; // 64位 + private const int KeySize = 16; // 128位 + private const int Rounds = 8; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + ushort[] subkeys = GenerateEncryptionSubkeys(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + ushort[] subkeys = GenerateDecryptionSubkeys(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey() + { + byte[] key = new byte[KeySize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static ushort[] GenerateEncryptionSubkeys(byte[] key) + { + ushort[] subkeys = new ushort[52]; + + // 将密钥分为8个16位子密钥 + for (int i = 0; i < 8; i++) + { + subkeys[i] = (ushort)((key[i * 2] << 8) | key[i * 2 + 1]); + } + + // 循环移位生成更多子密钥 + for (int i = 8; i < 52; i++) + { + if ((i % 8) == 0) + { + // 左移25位 + byte[] temp = new byte[KeySize]; + for (int j = 0; j < KeySize; j++) + { + int srcIdx = ((j + 3) % KeySize); + int bitPos = (j + 3) >= KeySize ? (j + 3 - KeySize) * 8 % 128 : (j + 3 - 8) * 8 % 128; + temp[j] = key[srcIdx]; + } + + for (int j = 0; j < KeySize; j++) + key[j] = temp[j]; + } + + subkeys[i] = (ushort)((key[((i % 8) * 2) % KeySize] << 8) | + key[((i % 8) * 2 + 1) % KeySize]); + } + + return subkeys; + } + + private static ushort[] GenerateDecryptionSubkeys(byte[] key) + { + ushort[] encSubkeys = GenerateEncryptionSubkeys(key); + ushort[] decSubkeys = new ushort[52]; + + // 解密子密钥是加密子密钥的逆 + for (int i = 0; i < Rounds; i++) + { + int idx = i * 6; + + if (i == 0) + { + decSubkeys[idx] = MulInv(encSubkeys[48 - i * 6]); + decSubkeys[idx + 1] = AddInv(encSubkeys[49 - i * 6]); + decSubkeys[idx + 2] = AddInv(encSubkeys[50 - i * 6]); + decSubkeys[idx + 3] = MulInv(encSubkeys[51 - i * 6]); + } + else + { + decSubkeys[idx] = MulInv(encSubkeys[48 - i * 6]); + decSubkeys[idx + 1] = AddInv(encSubkeys[49 - i * 6]); + decSubkeys[idx + 2] = AddInv(encSubkeys[50 - i * 6]); + decSubkeys[idx + 3] = MulInv(encSubkeys[51 - i * 6]); + } + + decSubkeys[idx + 4] = encSubkeys[46 - i * 6]; + decSubkeys[idx + 5] = encSubkeys[47 - i * 6]; + } + + // 最后一轮的子密钥 + decSubkeys[48] = MulInv(encSubkeys[0]); + decSubkeys[49] = AddInv(encSubkeys[1]); + decSubkeys[50] = AddInv(encSubkeys[2]); + decSubkeys[51] = MulInv(encSubkeys[3]); + + return decSubkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, ushort[] subkeys) + { + // 将64位分为4个16位 + ushort x0 = (ushort)((input[inOffset] << 8) | input[inOffset + 1]); + ushort x1 = (ushort)((input[inOffset + 2] << 8) | input[inOffset + 3]); + ushort x2 = (ushort)((input[inOffset + 4] << 8) | input[inOffset + 5]); + ushort x3 = (ushort)((input[inOffset + 6] << 8) | input[inOffset + 7]); + + int keyIdx = 0; + + for (int round = 0; round < Rounds; round++) + { + ushort y0 = Mul(x0, subkeys[keyIdx++]); + ushort y1 = Add(x1, subkeys[keyIdx++]); + ushort y2 = Add(x2, subkeys[keyIdx++]); + ushort y3 = Mul(x3, subkeys[keyIdx++]); + + ushort t0 = Mul((ushort)(y0 ^ y2), subkeys[keyIdx++]); + ushort t1 = Add((ushort)(y1 ^ y3), t0); + ushort t2 = Mul(t1, subkeys[keyIdx++]); + ushort t3 = Add(t0, t2); + + x0 = (ushort)(y0 ^ t2); + x1 = (ushort)(y2 ^ t2); + x2 = (ushort)(y1 ^ t3); + x3 = (ushort)(y3 ^ t3); + } + + // 最终输出变换 + ushort r0 = Mul(x0, subkeys[keyIdx++]); + ushort r1 = Add(x2, subkeys[keyIdx++]); + ushort r2 = Add(x1, subkeys[keyIdx++]); + ushort r3 = Mul(x3, subkeys[keyIdx++]); + + output[outOffset] = (byte)(r0 >> 8); + output[outOffset + 1] = (byte)r0; + output[outOffset + 2] = (byte)(r1 >> 8); + output[outOffset + 3] = (byte)r1; + output[outOffset + 4] = (byte)(r2 >> 8); + output[outOffset + 5] = (byte)r2; + output[outOffset + 6] = (byte)(r3 >> 8); + output[outOffset + 7] = (byte)r3; + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, ushort[] subkeys) + { + // 解密使用相同的结构,只是子密钥不同 + EncryptBlock(input, inOffset, output, outOffset, subkeys); + } + + // IDEA 的三种基本运算 + private static ushort Mul(ushort a, ushort b) + { + uint result = (uint)(a * b); + if (result != 0) + { + result = (uint)((ushort)(result & 0xFFFF) - (ushort)(result >> 16)); + if (result < 0x10000) + result = (uint)(result + 1); + return (ushort)result; + } + return (ushort)(1 - a - b); + } + + private static ushort Add(ushort a, ushort b) + { + return (ushort)((a + b) & 0xFFFF); + } + + private static ushort MulInv(ushort x) + { + if (x == 0) + return 0; + + int n = 0x10001; + int a = x; + int b = n; + int q, r; + int t1 = 0, t2 = 1; + + while (b > 0) + { + q = a / b; + r = a % b; + int t = t1 - q * t2; + a = b; + b = r; + t1 = t2; + t2 = t; + } + + if (t1 < 0) + t1 += n; + + return (ushort)t1; + } + + private static ushort AddInv(ushort x) + { + return (ushort)(0x10000 - x); + } + } +} diff --git a/EasyTool.Core/CodeCategory/JwtUtil.cs b/EasyTool.Core/CodeCategory/JwtUtil.cs new file mode 100644 index 0000000..4c78f79 --- /dev/null +++ b/EasyTool.Core/CodeCategory/JwtUtil.cs @@ -0,0 +1,526 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Linq; + +namespace EasyTool.CodeCategory +{ + /// + /// JWT(JSON Web Token)工具类 + /// 提供 JWT 的生成、解析、验证功能 + /// 支持 HS256、HS384、HS512 算法 + /// + public static class JwtUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + #region JWT 生成 + + /// + /// 生成 JWT Token + /// + /// 负载 + /// 密钥 + /// 算法(默认HS256) + /// JWT Token + public static string Encode(object payload, string secret, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + if (string.IsNullOrEmpty(secret)) + throw new ArgumentException("Secret cannot be null or empty", nameof(secret)); + + var payloadDict = ObjectToDictionary(payload); + return Encode(payloadDict, secret, algorithm); + } + + /// + /// 生成 JWT Token + /// + /// 负载字典 + /// 密钥 + /// 算法(默认HS256) + /// JWT Token + public static string Encode(Dictionary payload, string secret, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + if (string.IsNullOrEmpty(secret)) + throw new ArgumentException("Secret cannot be null or empty", nameof(secret)); + + // 创建 Header + var header = new Dictionary + { + { "typ", "JWT" }, + { "alg", algorithm.ToString() } + }; + + // 编码 Header 和 Payload + string headerEncoded = Base64UrlEncode(JsonSerializer.Serialize(header)); + string payloadEncoded = Base64UrlEncode(JsonSerializer.Serialize(payload)); + + // 创建签名 + string signatureInput = $"{headerEncoded}.{payloadEncoded}"; + string signature = CreateSignature(signatureInput, secret, algorithm); + + return $"{signatureInput}.{signature}"; + } + + /// + /// 生成带有过期时间的 JWT Token + /// + /// 负载 + /// 密钥 + /// 过期时间 + /// 算法 + /// JWT Token + public static string Encode(object payload, string secret, TimeSpan expiration, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + + var payloadDict = ObjectToDictionary(payload); + + // 添加时间戳 + var now = DateTime.UtcNow; + payloadDict["iat"] = ToUnixTimestamp(now); + payloadDict["exp"] = ToUnixTimestamp(now.Add(expiration)); + payloadDict["nbf"] = ToUnixTimestamp(now); + + return Encode(payloadDict, secret, algorithm); + } + + /// + /// 生成带有完整时间信息的 JWT Token + /// + /// 负载 + /// 密钥 + /// 签发者 + /// 受众 + /// 过期时间 + /// 算法 + /// JWT Token + public static string Encode(object payload, string secret, string issuer, string audience, TimeSpan expiration, JwtAlgorithm algorithm = JwtAlgorithm.HS256) + { + if (payload == null) + throw new ArgumentNullException(nameof(payload)); + + var payloadDict = ObjectToDictionary(payload); + + // 添加标准声明 + var now = DateTime.UtcNow; + payloadDict["iss"] = issuer; + payloadDict["aud"] = audience; + payloadDict["iat"] = ToUnixTimestamp(now); + payloadDict["exp"] = ToUnixTimestamp(now.Add(expiration)); + payloadDict["nbf"] = ToUnixTimestamp(now); + payloadDict["jti"] = Guid.NewGuid().ToString(); + + return Encode(payloadDict, secret, algorithm); + } + + #endregion + + #region JWT 解析 + + /// + /// 解析 JWT Token(不验证签名) + /// + /// JWT Token + /// 解析结果 + public static JwtDecodeResult Decode(string token) + { + if (string.IsNullOrEmpty(token)) + throw new ArgumentException("Token cannot be null or empty", nameof(token)); + + var parts = token.Split('.'); + if (parts.Length != 3) + throw new ArgumentException("Invalid token format", nameof(token)); + + try + { + var header = JsonSerializer.Deserialize>(Base64UrlDecode(parts[0])); + var payload = JsonSerializer.Deserialize>(Base64UrlDecode(parts[1])); + + return new JwtDecodeResult + { + Header = header, + Payload = payload, + Signature = parts[2], + IsValid = true + }; + } + catch (Exception ex) + { + return new JwtDecodeResult + { + IsValid = false, + ErrorMessage = ex.Message + }; + } + } + + /// + /// 解析并验证 JWT Token + /// + /// JWT Token + /// 密钥 + /// 解析结果 + public static JwtDecodeResult Decode(string token, string secret) + { + var result = Decode(token); + + if (!result.IsValid) + return result; + + // 验证签名 + if (!VerifySignature(token, secret, result.Header)) + { + result.IsValid = false; + result.ErrorMessage = "Invalid signature"; + return result; + } + + // 验证过期时间 + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + if (DateTime.UtcNow > expTime) + { + result.IsValid = false; + result.ErrorMessage = "Token has expired"; + result.IsExpired = true; + return result; + } + } + + // 验证生效时间 + if (result.Payload.TryGetValue("nbf", out var nbf)) + { + var nbfTime = FromUnixTimestamp(Convert.ToInt64(nbf)); + if (DateTime.UtcNow < nbfTime) + { + result.IsValid = false; + result.ErrorMessage = "Token is not yet valid"; + return result; + } + } + + return result; + } + + /// + /// 解析并验证 JWT Token(带完整验证) + /// + /// JWT Token + /// 密钥 + /// 预期签发者 + /// 预期受众 + /// 解析结果 + public static JwtDecodeResult Decode(string token, string secret, string issuer, string audience) + { + var result = Decode(token, secret); + + if (!result.IsValid) + return result; + + // 验证签发者 + if (!string.IsNullOrEmpty(issuer) && result.Payload.TryGetValue("iss", out var iss)) + { + if (iss.ToString() != issuer) + { + result.IsValid = false; + result.ErrorMessage = "Invalid issuer"; + return result; + } + } + + // 验证受众 + if (!string.IsNullOrEmpty(audience) && result.Payload.TryGetValue("aud", out var aud)) + { + var audList = aud as JsonElement?; + if (audList != null && audList.Value.ValueKind == JsonValueKind.Array) + { + var audiences = audList.Value.EnumerateArray().Select(a => a.GetString()).ToList(); + if (!audiences.Contains(audience)) + { + result.IsValid = false; + result.ErrorMessage = "Invalid audience"; + return result; + } + } + else if (aud.ToString() != audience) + { + result.IsValid = false; + result.ErrorMessage = "Invalid audience"; + return result; + } + } + + return result; + } + + /// + /// 获取 JWT Token 的 Payload(不验证) + /// + /// Payload 类型 + /// JWT Token + /// Payload 对象 + public static T GetPayload(string token) + { + var result = Decode(token); + if (!result.IsValid) + throw new ArgumentException(result.ErrorMessage); + + var json = JsonSerializer.Serialize(result.Payload); + return JsonSerializer.Deserialize(json); + } + + #endregion + + #region JWT 验证 + + /// + /// 验证 JWT Token + /// + /// JWT Token + /// 密钥 + /// 是否有效 + public static bool Verify(string token, string secret) + { + var result = Decode(token, secret); + return result.IsValid; + } + + /// + /// 验证 JWT Token 签名 + /// + /// JWT Token + /// 密钥 + /// 签名是否有效 + public static bool VerifySignature(string token, string secret) + { + var result = Decode(token); + if (!result.IsValid) + return false; + + return VerifySignature(token, secret, result.Header); + } + + private static bool VerifySignature(string token, string secret, Dictionary header) + { + var parts = token.Split('.'); + if (parts.Length != 3) + return false; + + var alg = header["alg"].ToString(); + var algorithm = Enum.Parse(alg); + + string expectedSignature = CreateSignature($"{parts[0]}.{parts[1]}", secret, algorithm); + return expectedSignature == parts[2]; + } + + /// + /// 检查 Token 是否过期 + /// + /// JWT Token + /// 是否过期 + public static bool IsExpired(string token) + { + var result = Decode(token); + if (!result.IsValid) + return true; + + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + return DateTime.UtcNow > expTime; + } + + return false; + } + + /// + /// 获取 Token 剩余有效时间 + /// + /// JWT Token + /// 剩余时间(如果没有过期时间则返回null) + public static TimeSpan? GetRemainingTime(string token) + { + var result = Decode(token); + if (!result.IsValid) + return null; + + if (result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + var remaining = expTime - DateTime.UtcNow; + return remaining > TimeSpan.Zero ? remaining : TimeSpan.Zero; + } + + return null; + } + + #endregion + + #region JWT 刷新 + + /// + /// 刷新 Token(如果有效且未过期太久) + /// + /// 旧 Token + /// 密钥 + /// 新过期时间 + /// 最大刷新延迟(超过此时间不允许刷新) + /// 新 Token,如果无法刷新则返回null + public static string Refresh(string token, string secret, TimeSpan expiration, TimeSpan? maxRefreshDelay = null) + { + var result = Decode(token); + + if (!result.IsValid) + return null; + + // 验证签名 + if (!VerifySignature(token, secret, result.Header)) + return null; + + // 检查是否超出最大刷新延迟 + if (maxRefreshDelay.HasValue && result.Payload.TryGetValue("exp", out var exp)) + { + var expTime = FromUnixTimestamp(Convert.ToInt64(exp)); + if (DateTime.UtcNow - expTime > maxRefreshDelay.Value) + return null; + } + + // 移除旧的时间戳和ID + var newPayload = new Dictionary(result.Payload); + newPayload.Remove("iat"); + newPayload.Remove("exp"); + newPayload.Remove("nbf"); + newPayload.Remove("jti"); + + // 生成新 Token + var alg = result.Header["alg"].ToString(); + var algorithm = Enum.Parse(alg); + + return Encode(newPayload, secret, expiration, algorithm); + } + + #endregion + + #region 私有方法 + + private static string CreateSignature(string input, string secret, JwtAlgorithm algorithm) + { + byte[] keyBytes = Encoding.UTF8.GetBytes(secret); + byte[] inputBytes = Encoding.UTF8.GetBytes(input); + + using var hmac = algorithm switch + { + JwtAlgorithm.HS256 => new HMACSHA256(keyBytes) as HMAC, + JwtAlgorithm.HS384 => new HMACSHA384(keyBytes), + JwtAlgorithm.HS512 => new HMACSHA512(keyBytes), + _ => throw new ArgumentException($"Unsupported algorithm: {algorithm}") + }; + + byte[] hash = hmac.ComputeHash(inputBytes); + return Base64UrlEncode(hash); + } + + private static string Base64UrlEncode(string input) + { + return Base64UrlEncode(Encoding.UTF8.GetBytes(input)); + } + + private static string Base64UrlEncode(byte[] input) + { + return Convert.ToBase64String(input) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + private static string Base64UrlDecode(string input) + { + string output = input + .Replace('-', '+') + .Replace('_', '/'); + + switch (output.Length % 4) + { + case 0: break; + case 2: output += "=="; break; + case 3: output += "="; break; + default: throw new ArgumentException("Invalid base64url string"); + } + + return Encoding.UTF8.GetString(Convert.FromBase64String(output)); + } + + private static Dictionary ObjectToDictionary(object obj) + { + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize>(json); + } + + private static long ToUnixTimestamp(DateTime dateTime) + { + return (long)(dateTime - Epoch).TotalSeconds; + } + + private static DateTime FromUnixTimestamp(long timestamp) + { + return Epoch.AddSeconds(timestamp); + } + + #endregion + } + + /// + /// JWT 算法 + /// + public enum JwtAlgorithm + { + HS256, + HS384, + HS512 + } + + /// + /// JWT 解析结果 + /// + public class JwtDecodeResult + { + /// + /// JWT Header + /// + public Dictionary Header { get; set; } + + /// + /// JWT Payload + /// + public Dictionary Payload { get; set; } + + /// + /// 签名 + /// + public string Signature { get; set; } + + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 是否过期 + /// + public bool IsExpired { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + } +} diff --git a/EasyTool.Core/CodeCategory/KSUIDUtil.cs b/EasyTool.Core/CodeCategory/KSUIDUtil.cs new file mode 100644 index 0000000..6674f79 --- /dev/null +++ b/EasyTool.Core/CodeCategory/KSUIDUtil.cs @@ -0,0 +1,299 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// KSUID(K-Sortable Unique Identifier)工具类 + /// KSUID 是一种时间排序的唯一标识符,由 Svix 开发 + /// 格式:4字节时间戳 + 16字节随机数 = 20字节 + /// + public static class KSUIDUtil + { + private static readonly DateTime Epoch = new DateTime(2014, 5, 13, 0, 0, 0, DateTimeKind.Utc); + private const int TimestampBytes = 4; + private const int PayloadBytes = 16; + private const int TotalBytes = 20; + private const int EncodedLength = 27; + + // Base62 字符集 + private const string Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + /// + /// 生成新的 KSUID + /// + /// 20字节的 KSUID + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 KSUID + /// + /// 时间戳 + /// 20字节的 KSUID + public static byte[] Generate(DateTimeOffset timestamp) + { + var bytes = new byte[TotalBytes]; + + // 4字节时间戳(大端序,自2014-05-13起的秒数) + uint seconds = (uint)(timestamp.ToUniversalTime() - Epoch).TotalSeconds; + bytes[0] = (byte)(seconds >> 24); + bytes[1] = (byte)(seconds >> 16); + bytes[2] = (byte)(seconds >> 8); + bytes[3] = (byte)seconds; + + // 16字节随机载荷 + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes, 4, 16); + + return bytes; + } + + /// + /// 生成 KSUID 字符串(27个字符的 Base62 编码) + /// + /// 27字符的 KSUID 字符串 + public static string GenerateString() + { + byte[] bytes = Generate(); + return Encode(bytes); + } + + /// + /// 生成指定时间的 KSUID 字符串 + /// + /// 时间戳 + /// 27字符的 KSUID 字符串 + public static string GenerateString(DateTimeOffset timestamp) + { + byte[] bytes = Generate(timestamp); + return Encode(bytes); + } + + /// + /// 将 KSUID 编码为字符串 + /// + /// 20字节的 KSUID + /// 27字符的 Base62 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != TotalBytes) + throw new ArgumentException($"KSUID must be {TotalBytes} bytes", nameof(bytes)); + + // 转换为大整数 + byte[] paddedBytes = new byte[TotalBytes + 1]; + Array.Copy(bytes, 0, paddedBytes, 1, TotalBytes); + + var number = new System.Numerics.BigInteger(paddedBytes); + var result = new char[EncodedLength]; + + for (int i = EncodedLength - 1; i >= 0; i--) + { + number = System.Numerics.BigInteger.DivRem(number, 62, out var remainder); + result[i] = Base62Chars[(int)remainder]; + } + + return new string(result); + } + + /// + /// 将 KSUID 字符串解码为字节数组 + /// + /// 27字符的 KSUID 字符串 + /// 20字节的 KSUID + public static byte[] Decode(string ksuid) + { + if (string.IsNullOrEmpty(ksuid) || ksuid.Length != EncodedLength) + throw new ArgumentException($"KSUID string must be {EncodedLength} characters", nameof(ksuid)); + + // 构建 Base62 解码映射 + var decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base62Chars.Length; i++) + { + decodeMap[Base62Chars[i]] = i; + } + + // 转换为大整数 + var number = System.Numerics.BigInteger.Zero; + + foreach (char c in ksuid) + { + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid character: {c}"); + + number = number * 62 + decodeMap[c]; + } + + // 转换为字节数组 + byte[] allBytes = number.ToByteArray(); + byte[] result = new byte[TotalBytes]; + + // 处理可能的符号位 + int copyLength = Math.Min(allBytes.Length, TotalBytes); + if (allBytes.Length > TotalBytes && allBytes[allBytes.Length - 1] == 0) + { + copyLength = Math.Min(allBytes.Length - 1, TotalBytes); + } + + // 从右侧开始复制(大端序) + int sourceIndex = allBytes.Length - copyLength; + if (sourceIndex < 0) sourceIndex = 0; + int destIndex = TotalBytes - copyLength; + if (destIndex < 0) destIndex = 0; + + for (int i = 0; i < copyLength && sourceIndex + i < allBytes.Length; i++) + { + result[destIndex + i] = allBytes[sourceIndex + i]; + } + + return result; + } + + /// + /// 从 KSUID 提取时间戳 + /// + /// KSUID 字节数组 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] ksuid) + { + if (ksuid == null || ksuid.Length != TotalBytes) + throw new ArgumentException($"KSUID must be {TotalBytes} bytes", nameof(ksuid)); + + uint seconds = ((uint)ksuid[0] << 24) | ((uint)ksuid[1] << 16) | + ((uint)ksuid[2] << 8) | ksuid[3]; + + return Epoch.AddSeconds(seconds); + } + + /// + /// 从 KSUID 字符串提取时间戳 + /// + /// KSUID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string ksuid) + { + byte[] bytes = Decode(ksuid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 KSUID 字符串是否有效 + /// + /// KSUID 字符串 + /// 是否有效 + public static bool IsValid(string ksuid) + { + if (string.IsNullOrEmpty(ksuid) || ksuid.Length != EncodedLength) + return false; + + foreach (char c in ksuid) + { + if (!Base62Chars.Contains(c)) + return false; + } + + return true; + } + + /// + /// 尝试解析 KSUID 字符串 + /// + /// KSUID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string ksuid, out byte[] bytes) + { + bytes = null; + if (!IsValid(ksuid)) + return false; + + try + { + bytes = Decode(ksuid); + return true; + } + catch + { + return false; + } + } + + /// + /// 比较 KSUID 的时间顺序 + /// + /// 第一个 KSUID + /// 第二个 KSUID + /// -1: ksuid1早于ksuid2, 0: 相同, 1: ksuid1晚于ksuid2 + public static int Compare(string ksuid1, string ksuid2) + { + return string.Compare(ksuid1, ksuid2, StringComparison.Ordinal); + } + + /// + /// 批量生成 KSUID + /// + /// 生成数量 + /// KSUID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 生成指定时间范围内的 KSUID + /// + /// 最小时间 + /// 最大时间 + /// KSUID 字符串 + public static string GenerateInRange(DateTimeOffset minTimestamp, DateTimeOffset maxTimestamp) + { + if (minTimestamp > maxTimestamp) + throw new ArgumentException("Min timestamp must be less than or equal to max timestamp"); + + var random = new Random(); + long minSeconds = (long)(minTimestamp.ToUniversalTime() - Epoch).TotalSeconds; + long maxSeconds = (long)(maxTimestamp.ToUniversalTime() - Epoch).TotalSeconds; + long randomSeconds = minSeconds + (long)(random.NextDouble() * (maxSeconds - minSeconds)); + + var timestamp = Epoch.AddSeconds(randomSeconds); + return GenerateString(timestamp); + } + + /// + /// 获取 KSUID 的最小有效时间 + /// + /// KSUID 纪元时间 + public static DateTime GetEpoch() + { + return Epoch; + } + + /// + /// 解析 KSUID 的各个组成部分 + /// + /// KSUID 字符串 + /// 时间戳和载荷 + public static (DateTimeOffset Timestamp, byte[] Payload) Parse(string ksuid) + { + byte[] bytes = Decode(ksuid); + DateTimeOffset timestamp = ExtractTimestamp(bytes); + + byte[] payload = new byte[PayloadBytes]; + Array.Copy(bytes, 4, payload, 0, PayloadBytes); + + return (timestamp, payload); + } + } +} diff --git a/EasyTool.Core/CodeCategory/KdfUtil.cs b/EasyTool.Core/CodeCategory/KdfUtil.cs new file mode 100644 index 0000000..6793662 --- /dev/null +++ b/EasyTool.Core/CodeCategory/KdfUtil.cs @@ -0,0 +1,322 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 密钥派生函数(KDF)工具类 + /// 提供安全的密钥派生方法 + /// + public static class KdfUtil + { + #region PBKDF2 + + /// + /// 使用 PBKDF2 派生密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Pbkdf2(byte[] password, byte[] salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var kdf = new Rfc2898DeriveBytes(password, salt, iterations, hashAlgorithm); + return kdf.GetBytes(keySize); + } + + /// + /// 使用 PBKDF2 派生密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 密钥大小(字节) + /// 哈希算法 + /// 派生的密钥(十六进制字符串) + public static string Pbkdf2Hex(string password, string salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + var passwordBytes = Encoding.UTF8.GetBytes(password); + var saltBytes = Encoding.UTF8.GetBytes(salt); + var key = Pbkdf2(passwordBytes, saltBytes, iterations, keySize, hashAlgorithm); + return HexUtil.BytesToHex(key); + } + + /// + /// 使用 PBKDF2 派生密钥(Base64 编码) + /// + public static string Pbkdf2Base64(string password, string salt, int iterations = 100000, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + var passwordBytes = Encoding.UTF8.GetBytes(password); + var saltBytes = Encoding.UTF8.GetBytes(salt); + var key = Pbkdf2(passwordBytes, saltBytes, iterations, keySize, hashAlgorithm); + return Convert.ToBase64String(key); + } + + /// + /// 生成 PBKDF2 盐值 + /// + /// 盐值大小(字节) + /// 盐值 + public static byte[] GenerateSalt(int size = 16) + { + using var rng = RandomNumberGenerator.Create(); + var salt = new byte[size]; + rng.GetBytes(salt); + return salt; + } + + /// + /// 生成 PBKDF2 盐值(Base64 编码) + /// + public static string GenerateSaltBase64(int size = 16) + { + var salt = GenerateSalt(size); + return Convert.ToBase64String(salt); + } + + #endregion + + #region HKDF + + /// + /// 使用 HKDF 派生密钥 + /// + /// 输入密钥材料 + /// 盐值 + /// 上下文信息 + /// 输出密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Hkdf(byte[] ikm, byte[]? salt = null, byte[]? info = null, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + salt ??= Array.Empty(); + info ??= Array.Empty(); + + // Extract + var prk = HmacExtract(ikm, salt, hashAlgorithm); + + // Expand + return HkdfExpand(prk, info, keySize, hashAlgorithm); + } + + /// + /// HKDF Extract 步骤 + /// + private static byte[] HmacExtract(byte[] ikm, byte[] salt, HashAlgorithmName hashAlgorithm) + { + int hashSize = GetHashSize(hashAlgorithm); + if (salt.Length == 0) + salt = new byte[hashSize]; + + return ComputeHmac(ikm, salt, hashAlgorithm); + } + + /// + /// HKDF Expand 步骤 + /// + private static byte[] HkdfExpand(byte[] prk, byte[] info, int keySize, HashAlgorithmName hashAlgorithm) + { + int hashSize = GetHashSize(hashAlgorithm); + int n = (keySize + hashSize - 1) / hashSize; + + var result = new byte[keySize]; + var t = Array.Empty(); + int offset = 0; + + for (int i = 1; i <= n; i++) + { + var data = new byte[t.Length + info.Length + 1]; + Buffer.BlockCopy(t, 0, data, 0, t.Length); + Buffer.BlockCopy(info, 0, data, t.Length, info.Length); + data[data.Length - 1] = (byte)i; + + t = ComputeHmac(data, prk, hashAlgorithm); + int toCopy = Math.Min(hashSize, keySize - offset); + Buffer.BlockCopy(t, 0, result, offset, toCopy); + offset += toCopy; + } + + return result; + } + + /// + /// 计算 HMAC + /// + private static byte[] ComputeHmac(byte[] data, byte[] key, HashAlgorithmName hashAlgorithm) + { + using var hmac = CreateHmac(key, hashAlgorithm); + return hmac.ComputeHash(data); + } + + /// + /// 创建 HMAC 实例 + /// + private static HMAC CreateHmac(byte[] key, HashAlgorithmName hashAlgorithm) + { + if (hashAlgorithm == HashAlgorithmName.SHA256) + return new HMACSHA256(key); + if (hashAlgorithm == HashAlgorithmName.SHA384) + return new HMACSHA384(key); + if (hashAlgorithm == HashAlgorithmName.SHA512) + return new HMACSHA512(key); + if (hashAlgorithm == HashAlgorithmName.SHA1) + return new HMACSHA1(key); + + throw new NotSupportedException($"不支持的哈希算法: {hashAlgorithm.Name}"); + } + + /// + /// 获取哈希大小 + /// + private static int GetHashSize(HashAlgorithmName hashAlgorithm) + { + if (hashAlgorithm == HashAlgorithmName.SHA256) return 32; + if (hashAlgorithm == HashAlgorithmName.SHA384) return 48; + if (hashAlgorithm == HashAlgorithmName.SHA512) return 64; + if (hashAlgorithm == HashAlgorithmName.SHA1) return 20; + + throw new NotSupportedException($"不支持的哈希算法: {hashAlgorithm.Name}"); + } + + #endregion + + #region SP 800-108 Counter Mode KDF + + /// + /// 使用 NIST SP 800-108 Counter Mode 派生密钥 + /// + /// 密钥派生密钥 + /// 标签 + /// 上下文 + /// 输出密钥大小(字节) + /// 哈希算法 + /// 派生的密钥 + public static byte[] Sp800_108_Counter(byte[] keyDerivationKey, byte[] label, byte[] context, int keySize = 32, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + int hashSize = GetHashSize(hashAlgorithm); + int n = (keySize + hashSize - 1) / hashSize; + + var result = new byte[keySize]; + int offset = 0; + + for (int i = 1; i <= n; i++) + { + // [i] || Label || 0x00 || Context || [L] + var data = new byte[4 + label.Length + 1 + context.Length + 4]; + int pos = 0; + + // Counter (4 bytes, big-endian) + data[pos++] = (byte)(i >> 24); + data[pos++] = (byte)(i >> 16); + data[pos++] = (byte)(i >> 8); + data[pos++] = (byte)i; + + // Label + Buffer.BlockCopy(label, 0, data, pos, label.Length); + pos += label.Length; + + // Separator + data[pos++] = 0x00; + + // Context + Buffer.BlockCopy(context, 0, data, pos, context.Length); + pos += context.Length; + + // Length in bits (4 bytes, big-endian) + int l = keySize * 8; + data[pos++] = (byte)(l >> 24); + data[pos++] = (byte)(l >> 16); + data[pos++] = (byte)(l >> 8); + data[pos++] = (byte)l; + + var hash = ComputeHmac(data, keyDerivationKey, hashAlgorithm); + int toCopy = Math.Min(hashSize, keySize - offset); + Buffer.BlockCopy(hash, 0, result, offset, toCopy); + offset += toCopy; + } + + return result; + } + + #endregion + + #region 静态工具方法 + + /// + /// 从密码生成加密密钥 + /// + /// 密码 + /// 盐值 + /// 迭代次数 + /// 生成的密钥信息 + public static KeyDerivationResult DeriveKeyFromPassword(string password, byte[]? salt = null, int iterations = 100000) + { + salt ??= GenerateSalt(16); + var key = Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, 32); + var iv = Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, 16); + + return new KeyDerivationResult + { + Key = key, + IV = iv, + Salt = salt, + Iterations = iterations + }; + } + + #endregion + } + + /// + /// 密钥派生结果 + /// + public class KeyDerivationResult + { + /// + /// 派生的密钥 + /// + public byte[] Key { get; set; } = Array.Empty(); + + /// + /// 初始化向量 + /// + public byte[] IV { get; set; } = Array.Empty(); + + /// + /// 使用的盐值 + /// + public byte[] Salt { get; set; } = Array.Empty(); + + /// + /// 迭代次数 + /// + public int Iterations { get; set; } + + /// + /// 密钥(Base64 编码) + /// + public string KeyBase64 => Convert.ToBase64String(Key); + + /// + /// IV(Base64 编码) + /// + public string IVBase64 => Convert.ToBase64String(IV); + + /// + /// 盐值(Base64 编码) + /// + public string SaltBase64 => Convert.ToBase64String(Salt); + } +} diff --git a/EasyTool.Core/CodeCategory/LZ4Util.cs b/EasyTool.Core/CodeCategory/LZ4Util.cs new file mode 100644 index 0000000..25f3825 --- /dev/null +++ b/EasyTool.Core/CodeCategory/LZ4Util.cs @@ -0,0 +1,339 @@ +using System; +using System.IO; + +namespace EasyTool.CodeCategory +{ + /// + /// LZ4 压缩工具类 + /// LZ4 是一种极快的无损压缩算法 + /// 压缩速度可达 500MB/s,解压速度可达 1GB/s + /// + public static class LZ4Util + { + private const int MinMatch = 4; + private const int MaxOffset = 65535; + private const int MinLookahead = MinMatch + 1; + + #region 压缩 + + /// + /// 压缩数据 + /// + /// 要压缩的数据 + /// 压缩后的数据 + public static byte[] Compress(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + return Compress(data, 0, data.Length); + } + + /// + /// 压缩数据 + /// + /// 要压缩的数据 + /// 起始偏移 + /// 数据长度 + /// 压缩后的数据 + public static byte[] Compress(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return Array.Empty(); + + if (length < MinLookahead) + { + // 太短,直接返回原始数据(带标记) + byte[] result = new byte[length + 1]; + result[0] = 0; // 标记为未压缩 + Array.Copy(data, offset, result, 1, length); + return result; + } + + var output = new MemoryStream(); + var writer = new BinaryWriter(output); + + // 写入原始长度 + writer.Write(length); + + int pos = offset; + int anchor = offset; + int end = offset + length; + + while (pos < end - MinLookahead) + { + int matchPos = FindMatch(data, pos, end, out int matchLength); + + if (matchPos >= 0 && matchLength >= MinMatch) + { + // 写入字面量 + int literalLength = pos - anchor; + WriteToken(writer, literalLength, matchLength); + + // 写入字面量数据 + if (literalLength > 0) + { + writer.Write(data, anchor, literalLength); + } + + // 写入偏移量 + int offset_value = pos - matchPos; + writer.Write((byte)(offset_value >> 8)); + writer.Write((byte)offset_value); + + pos += matchLength; + anchor = pos; + } + else + { + pos++; + } + } + + // 写入最后的字面量 + int finalLiteralLength = end - anchor; + WriteToken(writer, finalLiteralLength, 0); + + if (finalLiteralLength > 0) + { + writer.Write(data, anchor, finalLiteralLength); + } + + return output.ToArray(); + } + + private static int FindMatch(byte[] data, int pos, int end, out int matchLength) + { + matchLength = 0; + int bestMatchPos = -1; + int bestMatchLength = 0; + + int searchStart = Math.Max(0, pos - MaxOffset); + + for (int i = searchStart; i < pos; i++) + { + int len = 0; + int maxLen = Math.Min(end - pos, 255 + MinMatch); + + while (len < maxLen && data[i + len] == data[pos + len]) + { + len++; + } + + if (len >= MinMatch && len > bestMatchLength) + { + bestMatchPos = i; + bestMatchLength = len; + } + } + + matchLength = bestMatchLength; + return bestMatchPos; + } + + private static void WriteToken(BinaryWriter writer, int literalLength, int matchLength) + { + int token = Math.Min(literalLength, 15) << 4; + token |= Math.Min(matchLength - MinMatch, 15); + + writer.Write((byte)token); + + // 写入扩展的字面量长度 + if (literalLength >= 15) + { + literalLength -= 15; + while (literalLength >= 255) + { + writer.Write((byte)255); + literalLength -= 255; + } + writer.Write((byte)literalLength); + } + + // 写入扩展的匹配长度 + if (matchLength - MinMatch >= 15) + { + int extraLength = matchLength - MinMatch - 15; + while (extraLength >= 255) + { + writer.Write((byte)255); + extraLength -= 255; + } + writer.Write((byte)extraLength); + } + } + + #endregion + + #region 解压 + + /// + /// 解压数据 + /// + /// 压缩的数据 + /// 解压后的数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length == 0) + return Array.Empty(); + + return Decompress(compressed, 0, compressed.Length); + } + + /// + /// 解压数据 + /// + /// 压缩的数据 + /// 起始偏移 + /// 数据长度 + /// 解压后的数据 + public static byte[] Decompress(byte[] compressed, int offset, int length) + { + if (compressed == null) + throw new ArgumentNullException(nameof(compressed)); + if (offset < 0 || offset > compressed.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > compressed.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + if (length == 0) + return Array.Empty(); + + var input = new MemoryStream(compressed, offset, length); + var reader = new BinaryReader(input); + + // 读取原始长度 + int originalLength = reader.ReadInt32(); + var output = new byte[originalLength]; + int outPos = 0; + + while (input.Position < input.Length && outPos < originalLength) + { + // 读取 token + int token = reader.ReadByte(); + int literalLength = (token >> 4) & 0x0F; + int matchLength = token & 0x0F; + + // 读取扩展的字面量长度 + if (literalLength == 15) + { + int extra; + do + { + extra = reader.ReadByte(); + literalLength += extra; + } while (extra == 255); + } + + // 复制字面量 + if (literalLength > 0) + { + Array.Copy(compressed, (int)input.Position, output, outPos, literalLength); + input.Position += literalLength; + outPos += literalLength; + } + + if (input.Position >= input.Length || outPos >= originalLength) + break; + + // 读取偏移量 + int matchOffset = (reader.ReadByte() << 8) | reader.ReadByte(); + + // 读取扩展的匹配长度 + matchLength += MinMatch; + if ((token & 0x0F) == 15) + { + int extra; + do + { + extra = reader.ReadByte(); + matchLength += extra; + } while (extra == 255); + } + + // 复制匹配 + int matchPos = outPos - matchOffset; + for (int i = 0; i < matchLength; i++) + { + output[outPos++] = output[matchPos++]; + } + } + + return output; + } + + #endregion + + #region 高级 API + + /// + /// 压缩字符串 + /// + /// 文本 + /// 压缩后的 Base64 字符串 + public static string CompressString(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + /// 压缩的 Base64 字符串 + /// 原始文本 + public static string DecompressString(string compressedText) + { + if (string.IsNullOrEmpty(compressedText)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedText); + byte[] data = Decompress(compressed); + return System.Text.Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩后的预计最大长度 + /// + /// 输入长度 + /// 最大输出长度 + public static int CalculateMaxCompressedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // LZ4 最坏情况下可能略微增加大小 + return inputLength + (inputLength / 255) + 16 + 4; // 4 for original length + } + + /// + /// 计算压缩比 + /// + /// 原始数据 + /// 压缩数据 + /// 压缩比(0-1) + public static double CalculateCompressionRatio(byte[] originalData, byte[] compressedData) + { + if (originalData == null || originalData.Length == 0) + return 0; + + if (compressedData == null || compressedData.Length == 0) + return 1; + + return (double)compressedData.Length / originalData.Length; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/LuhnUtil.cs b/EasyTool.Core/CodeCategory/LuhnUtil.cs new file mode 100644 index 0000000..158bc02 --- /dev/null +++ b/EasyTool.Core/CodeCategory/LuhnUtil.cs @@ -0,0 +1,386 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CodeCategory +{ + /// + /// Luhn 校验算法工具类 + /// Luhn 算法是一种简单的校验和算法,用于验证信用卡号、IMEI号、银行卡号等 + /// + public static class LuhnUtil + { + /// + /// 验证数字字符串是否符合 Luhn 算法 + /// + /// 数字字符串 + /// 是否有效 + public static bool IsValid(string number) + { + if (string.IsNullOrEmpty(number)) + return false; + + // 移除空格和连字符 + number = CleanNumber(number); + + if (!IsAllDigits(number)) + return false; + + int sum = CalculateLuhnSum(number); + return sum % 10 == 0; + } + + /// + /// 验证数字数组是否符合 Luhn 算法 + /// + /// 数字数组 + /// 是否有效 + public static bool IsValid(int[] digits) + { + if (digits == null || digits.Length == 0) + return false; + + // 验证所有数字都在 0-9 范围内 + foreach (int d in digits) + { + if (d < 0 || d > 9) + return false; + } + + int sum = CalculateLuhnSum(digits); + return sum % 10 == 0; + } + + /// + /// 计算 Luhn 校验位 + /// + /// 不含校验位的数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + + if (!IsAllDigits(number)) + throw new ArgumentException("Number must contain only digits", nameof(number)); + + return CalculateCheckDigitImpl(number); + } + + /// + /// 计算 Luhn 校验位 + /// + /// 不含校验位的数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be null or empty", nameof(digits)); + + foreach (int d in digits) + { + if (d < 0 || d > 9) + throw new ArgumentException("All digits must be between 0 and 9", nameof(digits)); + } + + return CalculateCheckDigitImpl(digits); + } + + /// + /// 生成带校验位的完整数字 + /// + /// 不含校验位的数字字符串 + /// 带校验位的完整数字 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 生成带校验位的完整数字数组 + /// + /// 不含校验位的数字数组 + /// 带校验位的完整数字数组 + public static int[] AppendCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be null or empty", nameof(digits)); + + int checkDigit = CalculateCheckDigit(digits); + int[] result = new int[digits.Length + 1]; + Array.Copy(digits, result, digits.Length); + result[digits.Length] = checkDigit; + return result; + } + + /// + /// 生成指定长度的有效 Luhn 数字 + /// + /// 总长度(包括校验位) + /// 有效的 Luhn 数字字符串 + public static string Generate(int length) + { + if (length < 2) + throw new ArgumentException("Length must be at least 2", nameof(length)); + + var random = new Random(); + var digits = new int[length - 1]; + + // 生成随机数字(第一位不能为0) + digits[0] = random.Next(1, 10); + for (int i = 1; i < digits.Length; i++) + { + digits[i] = random.Next(0, 10); + } + + return AppendCheckDigit(string.Join("", digits)); + } + + /// < /// 生成指定前缀的有效 Luhn 数字 + /// + /// 前缀 + /// 总长度(包括校验位) + /// 有效的 Luhn 数字字符串 + public static string GenerateWithPrefix(string prefix, int totalLength) + { + if (string.IsNullOrEmpty(prefix)) + throw new ArgumentException("Prefix cannot be null or empty", nameof(prefix)); + if (totalLength < prefix.Length + 1) + throw new ArgumentException("Total length must be greater than prefix length", nameof(totalLength)); + + prefix = CleanNumber(prefix); + if (!IsAllDigits(prefix)) + throw new ArgumentException("Prefix must contain only digits", nameof(prefix)); + + var random = new Random(); + int remainingLength = totalLength - prefix.Length - 1; + var sb = new System.Text.StringBuilder(prefix); + + for (int i = 0; i < remainingLength; i++) + { + sb.Append(random.Next(0, 10)); + } + + return AppendCheckDigit(sb.ToString()); + } + + /// + /// 获取校验位 + /// + /// 带校验位的完整数字字符串 + /// 校验位 + public static int GetCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + if (!IsAllDigits(number)) + throw new ArgumentException("Number must contain only digits", nameof(number)); + + return number[number.Length - 1] - '0'; + } + + /// + /// 移除校验位 + /// + /// 带校验位的完整数字字符串 + /// 不含校验位的数字字符串 + public static string RemoveCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be null or empty", nameof(number)); + + number = CleanNumber(number); + if (number.Length < 2) + throw new ArgumentException("Number must have at least 2 digits", nameof(number)); + + return number.Substring(0, number.Length - 1); + } + + /// + /// 计算两个有效 Luhn 数字之间的编辑距离(需要改变多少位才能从一个变成另一个) + /// + /// 第一个数字 + /// 第二个数字 + /// 编辑距离 + public static int Distance(string number1, string number2) + { + number1 = CleanNumber(number1); + number2 = CleanNumber(number2); + + if (number1.Length != number2.Length) + throw new ArgumentException("Both numbers must have the same length"); + + int distance = 0; + for (int i = 0; i < number1.Length; i++) + { + if (number1[i] != number2[i]) + distance++; + } + + return distance; + } + + /// + /// 查找可能的错误(单字符错误) + /// + /// 无效的数字字符串 + /// 可能的修正列表(位置和正确值) + public static List<(int Position, int CorrectDigit)> FindPossibleErrors(string invalidNumber) + { + var result = new List<(int Position, int CorrectDigit)>(); + + if (string.IsNullOrEmpty(invalidNumber)) + return result; + + invalidNumber = CleanNumber(invalidNumber); + if (!IsAllDigits(invalidNumber)) + return result; + + var digits = invalidNumber.Select(c => c - '0').ToArray(); + + for (int i = 0; i < digits.Length; i++) + { + int original = digits[i]; + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + if (newDigit == original) + continue; + + digits[i] = newDigit; + if (IsValid(digits)) + { + result.Add((i, newDigit)); + } + } + digits[i] = original; + } + + return result; + } + + #region 私有方法 + + private static string CleanNumber(string number) + { + return number.Replace(" ", "").Replace("-", "").Replace("\t", ""); + } + + private static bool IsAllDigits(string s) + { + foreach (char c in s) + { + if (c < '0' || c > '9') + return false; + } + return true; + } + + private static int CalculateLuhnSum(string number) + { + int sum = 0; + bool doubleDigit = true; + + for (int i = number.Length - 2; i >= 0; i--) + { + int digit = number[i] - '0'; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + // 加上校验位 + sum += number[number.Length - 1] - '0'; + + return sum; + } + + private static int CalculateLuhnSum(int[] digits) + { + int sum = 0; + bool doubleDigit = true; + + for (int i = digits.Length - 2; i >= 0; i--) + { + int digit = digits[i]; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + sum += digits[digits.Length - 1]; + + return sum; + } + + private static int CalculateCheckDigitImpl(string number) + { + int sum = 0; + bool doubleDigit = false; + + for (int i = number.Length - 1; i >= 0; i--) + { + int digit = number[i] - '0'; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + return (10 - (sum % 10)) % 10; + } + + private static int CalculateCheckDigitImpl(int[] digits) + { + int sum = 0; + bool doubleDigit = false; + + for (int i = digits.Length - 1; i >= 0; i--) + { + int digit = digits[i]; + + if (doubleDigit) + { + digit *= 2; + if (digit > 9) + digit -= 9; + } + + sum += digit; + doubleDigit = !doubleDigit; + } + + return (10 - (sum % 10)) % 10; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/MorseCodeUtil.cs b/EasyTool.Core/CodeCategory/MorseCodeUtil.cs new file mode 100644 index 0000000..f0c3077 --- /dev/null +++ b/EasyTool.Core/CodeCategory/MorseCodeUtil.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 摩尔斯电码工具类 + /// 摩尔斯电码是一种将文本字符编码为点(.)和划(-)序列的编码方式 + /// 支持字母、数字和常用标点符号 + /// + public static class MorseCodeUtil + { + private static readonly Dictionary CharToMorse = new Dictionary + { + // 字母 + {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, + {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, + {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, + {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, + {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, + {'Z', "--.."}, + + // 数字 + {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, + {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, + {'8', "---.."}, {'9', "----."}, + + // 标点符号 + {'.', ".-.-.-"}, {',', "--..--"}, {'?', "..--.."}, {'\'', ".----."}, + {'!', "-.-.--"}, {'/', "-..-."}, {'(', "-.--."}, {')', "-.--.-"}, + {'&', ".-..."}, {':', "---..."}, {';', "-.-.-."}, {'=', "-...-"}, + {'+', ".-.-."}, {'-', "-....-"}, {'_', "..--.-"}, {'"', ".-..-."}, + {'$', "...-..-"}, {'@', ".--.-."} + }; + + private static readonly Dictionary MorseToChar = new Dictionary(); + + static MorseCodeUtil() + { + // 构建反向映射 + foreach (var kvp in CharToMorse) + { + MorseToChar[kvp.Value] = kvp.Key; + } + } + + /// + /// 将文本编码为摩尔斯电码 + /// + /// 文本 + /// 字母分隔符(默认空格) + /// 单词分隔符(默认斜杠) + /// 摩尔斯电码 + public static string Encode(string text, string letterSeparator = " ", string wordSeparator = " / ") + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var result = new StringBuilder(); + bool prevWasSpace = false; + + foreach (char c in text) + { + if (c == ' ') + { + if (!prevWasSpace) + { + result.Append(wordSeparator); + prevWasSpace = true; + } + } + else + { + char upper = char.ToUpperInvariant(c); + if (CharToMorse.TryGetValue(upper, out string morse)) + { + if (result.Length > 0 && !prevWasSpace) + { + result.Append(letterSeparator); + } + result.Append(morse); + prevWasSpace = false; + } + // 忽略不支持的字符 + } + } + + return result.ToString().Trim(); + } + + /// + /// 将摩尔斯电码解码为文本 + /// + /// 摩尔斯电码 + /// 字母分隔符(默认空格) + /// 单词分隔符(默认斜杠) + /// 文本 + public static string Decode(string morse, string letterSeparator = " ", string wordSeparator = " / ") + { + if (string.IsNullOrEmpty(morse)) + return string.Empty; + + var result = new StringBuilder(); + + // 标准化分隔符 + string normalized = morse.Replace(wordSeparator, " / "); + normalized = normalized.Replace(letterSeparator, " "); + + // 替换多个空格为单个空格(除了单词分隔符) + while (normalized.Contains(" ")) + { + normalized = normalized.Replace(" ", " "); + } + + string[] parts = normalized.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (string part in parts) + { + if (part == "/") + { + result.Append(' '); + } + else if (MorseToChar.TryGetValue(part, out char c)) + { + result.Append(c); + } + else + { + // 未知码,保留原样 + result.Append(part); + } + } + + return result.ToString(); + } + + /// + /// 将文本编码为摩尔斯电码(使用标准分隔符) + /// + /// 文本 + /// 摩尔斯电码 + public static string TextToMorse(string text) + { + return Encode(text); + } + + /// + /// 将摩尔斯电码解码为文本 + /// + /// 摩尔斯电码 + /// 文本 + public static string MorseToText(string morse) + { + return Decode(morse); + } + + /// + /// 获取单个字符的摩尔斯电码 + /// + /// 字符 + /// 摩尔斯电码,如果不支持返回 null + public static string GetMorse(char c) + { + c = char.ToUpperInvariant(c); + return CharToMorse.TryGetValue(c, out string morse) ? morse : null; + } + + /// + /// 获取摩尔斯电码对应的字符 + /// + /// 摩尔斯电码 + /// 字符,如果无效返回 null + public static char? GetChar(string morse) + { + return MorseToChar.TryGetValue(morse, out char c) ? c : (char?)null; + } + + /// + /// 验证摩尔斯电码字符串是否有效 + /// + /// 摩尔斯电码 + /// 是否有效 + public static bool IsValidMorse(string morse) + { + if (string.IsNullOrEmpty(morse)) + return false; + + foreach (char c in morse) + { + if (c != '.' && c != '-' && c != ' ' && c != '/') + return false; + } + + return true; + } + + /// + /// 验证文本是否可以完全编码为摩尔斯电码 + /// + /// 文本 + /// 是否可以编码 + public static bool CanEncode(string text) + { + if (string.IsNullOrEmpty(text)) + return true; + + foreach (char c in text) + { + if (c == ' ') + continue; + + if (!CharToMorse.ContainsKey(char.ToUpperInvariant(c))) + return false; + } + + return true; + } + + /// + /// 获取不支持的字符 + /// + /// 文本 + /// 不支持的字符列表 + public static List GetUnsupportedChars(string text) + { + var unsupported = new List(); + + if (string.IsNullOrEmpty(text)) + return unsupported; + + foreach (char c in text) + { + if (c == ' ') + continue; + + if (!CharToMorse.ContainsKey(char.ToUpperInvariant(c)) && !unsupported.Contains(c)) + { + unsupported.Add(c); + } + } + + return unsupported; + } + + /// + /// 将摩尔斯电码转换为音频信号参数 + /// + /// 摩尔斯电码 + /// 点持续时间(毫秒) + /// 信号参数列表(true = 信号,false = 停顿,后面跟持续时间) + public static List<(bool Signal, int DurationMs)> ToSignalTiming(string morse, int dotDuration = 100) + { + var timing = new List<(bool Signal, int DurationMs)>(); + + if (string.IsNullOrEmpty(morse)) + return timing; + + foreach (char c in morse) + { + switch (c) + { + case '.': + timing.Add((true, dotDuration)); // 点 + timing.Add((false, dotDuration)); // 点间停顿 + break; + case '-': + timing.Add((true, dotDuration * 3)); // 划 + timing.Add((false, dotDuration)); // 点间停顿 + break; + case ' ': + // 单词间停顿(减去前面的点间停顿) + if (timing.Count > 0 && !timing[timing.Count - 1].Signal) + { + timing[timing.Count - 1] = (false, dotDuration * 6); + } + else + { + timing.Add((false, dotDuration * 7)); + } + break; + case '/': + timing.Add((false, dotDuration * 7)); // 单词间停顿 + break; + } + } + + return timing; + } + + /// + /// 获取支持的字符列表 + /// + /// 支持的字符 + public static string GetSupportedChars() + { + var chars = new StringBuilder(); + foreach (var c in CharToMorse.Keys) + { + chars.Append(c); + } + return chars.ToString(); + } + + /// + /// 获取摩尔斯电码表 + /// + /// 字符到摩尔斯电码的映射 + public static Dictionary GetMorseTable() + { + return new Dictionary(CharToMorse); + } + } +} diff --git a/EasyTool.Core/CodeCategory/MorseUtil.cs b/EasyTool.Core/CodeCategory/MorseUtil.cs deleted file mode 100644 index 4341231..0000000 --- a/EasyTool.Core/CodeCategory/MorseUtil.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// Morse 电码工具类 - /// - public static class MorseUtil - { - // Morse 电码表 - private static readonly Dictionary MORSE_TABLE = new Dictionary() - { - {'A', ".-"}, - {'B', "-..."}, - {'C', "-.-."}, - {'D', "-.."}, - {'E', "."}, - {'F', "..-."}, - {'G', "--."}, - {'H', "...."}, - {'I', ".."}, - {'J', ".---"}, - {'K', "-.-"}, - {'L', ".-.."}, - {'M', "--"}, - {'N', "-."}, - {'O', "---"}, - {'P', ".--."}, - {'Q', "--.-"}, - {'R', ".-."}, - {'S', "..."}, - {'T', "-"}, - {'U', "..-"}, - {'V', "...-"}, - {'W', ".--"}, - {'X', "-..-"}, - {'Y', "-.--"}, - {'Z', "--.."}, - {'0', "-----"}, - {'1', ".----"}, - {'2', "..---"}, - {'3', "...--"}, - {'4', "....-"}, - {'5', "....."}, - {'6', "-...."}, - {'7', "--..."}, - {'8', "---.."}, - {'9', "----."}, - {' ', " "} - }; - - /// - /// 将给定的字符串转换为 Morse 电码字符串。 - /// - /// 要转换的字符串 - /// 转换后的 Morse 电码字符串 - public static string Encode(string str) - { - if (string.IsNullOrEmpty(str)) - { - return string.Empty; - } - - List morseCodes = new List(); - foreach (char c in str.ToUpper()) - { - if (MORSE_TABLE.ContainsKey(c)) - { - morseCodes.Add(MORSE_TABLE[c]); - } - } - return string.Join(" ", morseCodes); - } - - - /// - /// 将给定的 Morse 电码字符串转换为原始字符串。 - /// - /// 要转换的 Morse 电码字符串 - /// 转换后的原始字符串 - public static string Decode(string morseCode) - { - if (string.IsNullOrEmpty(morseCode)) - { - return string.Empty; - } - - string[] codes = morseCode.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - List chars = new List(); - foreach (string code in codes) - { - foreach (KeyValuePair kvp in MORSE_TABLE) - { - if (kvp.Value == code) - { - chars.Add(kvp.Key); - break; - } - } - } - return new string(chars.ToArray()); - } - - } -} diff --git a/EasyTool.Core/CodeCategory/MurmurHashUtil.cs b/EasyTool.Core/CodeCategory/MurmurHashUtil.cs new file mode 100644 index 0000000..ee8cb98 --- /dev/null +++ b/EasyTool.Core/CodeCategory/MurmurHashUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// MurmurHash 高性能非加密哈希工具类 + /// MurmurHash 是一种非加密型哈希函数,适用于一般的哈希检索操作 + /// 特点:高随机分布、高性能、低碰撞率 + /// + public static class MurmurHashUtil + { + #region MurmurHash3 32-bit + + private const uint C1_32 = 0xcc9e2d51; + private const uint C2_32 = 0x1b873593; + private const uint R1_32 = 15; + private const uint R2_32 = 13; + private const uint M_32 = 5; + private const uint N_32 = 0xe6546b64; + + /// + /// 计算 MurmurHash3 32位哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32位哈希值 + public static uint Hash32(byte[] data, uint seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + uint h = seed; + int length = data.Length; + int blocks = length / 4; + + // 处理4字节块 + for (int i = 0; i < blocks; i++) + { + int blockOffset = i * 4; + uint k = (uint)(data[blockOffset] | (data[blockOffset + 1] << 8) | (data[blockOffset + 2] << 16) | (data[blockOffset + 3] << 24)); + k *= C1_32; + k = RotateLeft32(k, (int)R1_32); + k *= C2_32; + + h ^= k; + h = RotateLeft32(h, (int)R2_32); + h = h * M_32 + N_32; + } + + // 处理剩余字节 + int remaining = length % 4; + int offset = blocks * 4; + uint tail = 0; + + switch (remaining) + { + case 3: + tail ^= (uint)data[offset + 2] << 16; + goto case 2; + case 2: + tail ^= (uint)data[offset + 1] << 8; + goto case 1; + case 1: + tail ^= data[offset]; + tail *= C1_32; + tail = RotateLeft32(tail, (int)R1_32); + tail *= C2_32; + h ^= tail; + break; + } + + // 最终混合 + h ^= (uint)length; + h = FinalMix32(h); + + return h; + } + + /// + /// 计算字符串的 MurmurHash3 32位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 32位哈希值 + public static uint Hash32(string text, uint seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash32(encoding.GetBytes(text), seed); + } + + private static uint FinalMix32(uint h) + { + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; + } + + #endregion + + #region MurmurHash3 64-bit + + private const ulong C1_64 = 0x87c37b91114253d5; + private const ulong C2_64 = 0x4cf5ad432745937f; + private const int R1_64 = 31; + private const int R2_64 = 27; + private const ulong M_64 = 5; + private const ulong N1_64 = 0x52dce729; + private const ulong N2_64 = 0x38495ab5; + + /// + /// 计算 MurmurHash3 64位哈希值(128位截断为64位) + /// + /// 输入数据 + /// 种子值(默认0) + /// 64位哈希值 + public static ulong Hash64(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + return h1 ^ h2; + } + + /// + /// 计算字符串的 MurmurHash3 64位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 64位哈希值 + public static ulong Hash64(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash64(encoding.GetBytes(text), seed); + } + + #endregion + + #region MurmurHash3 128-bit + + /// + /// 计算 MurmurHash3 128位哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 128位哈希值(两个64位值) + public static (ulong H1, ulong H2) Hash128(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return (0, 0); + + ulong h1 = seed; + ulong h2 = seed; + int length = data.Length; + int blocks = length / 16; + + // 处理16字节块 + for (int i = 0; i < blocks; i++) + { + ulong k1 = BitConverter.ToUInt64(data, i * 16); + ulong k2 = BitConverter.ToUInt64(data, i * 16 + 8); + + k1 *= C1_64; + k1 = RotateLeft64(k1, R1_64); + k1 *= C2_64; + h1 ^= k1; + + h1 = RotateLeft64(h1, R2_64); + h1 += h2; + h1 = h1 * M_64 + N1_64; + + k2 *= C2_64; + k2 = RotateLeft64(k2, R2_64); + k2 *= C1_64; + h2 ^= k2; + + h2 = RotateLeft64(h2, R1_64); + h2 += h1; + h2 = h2 * M_64 + N2_64; + } + + // 处理剩余字节 + int remaining = length % 16; + int offset = blocks * 16; + ulong tail1 = 0; + ulong tail2 = 0; + + switch (remaining) + { + case 15: + tail2 ^= (ulong)data[offset + 14] << 48; + goto case 14; + case 14: + tail2 ^= (ulong)data[offset + 13] << 40; + goto case 13; + case 13: + tail2 ^= (ulong)data[offset + 12] << 32; + goto case 12; + case 12: + tail2 ^= (ulong)data[offset + 11] << 24; + goto case 11; + case 11: + tail2 ^= (ulong)data[offset + 10] << 16; + goto case 10; + case 10: + tail2 ^= (ulong)data[offset + 9] << 8; + goto case 9; + case 9: + tail2 ^= data[offset + 8]; + tail2 *= C2_64; + tail2 = RotateLeft64(tail2, R2_64); + tail2 *= C1_64; + h2 ^= tail2; + goto case 8; + case 8: + tail1 ^= (ulong)data[offset + 7] << 56; + goto case 7; + case 7: + tail1 ^= (ulong)data[offset + 6] << 48; + goto case 6; + case 6: + tail1 ^= (ulong)data[offset + 5] << 40; + goto case 5; + case 5: + tail1 ^= (ulong)data[offset + 4] << 32; + goto case 4; + case 4: + tail1 ^= (ulong)data[offset + 3] << 24; + goto case 3; + case 3: + tail1 ^= (ulong)data[offset + 2] << 16; + goto case 2; + case 2: + tail1 ^= (ulong)data[offset + 1] << 8; + goto case 1; + case 1: + tail1 ^= data[offset]; + tail1 *= C1_64; + tail1 = RotateLeft64(tail1, R1_64); + tail1 *= C2_64; + h1 ^= tail1; + break; + } + + // 最终混合 + h1 ^= (ulong)length; + h2 ^= (ulong)length; + + h1 += h2; + h2 += h1; + + h1 = FinalMix64(h1); + h2 = FinalMix64(h2); + + h1 += h2; + h2 += h1; + + return (h1, h2); + } + + /// + /// 计算字符串的 MurmurHash3 128位哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 128位哈希值(两个64位值) + public static (ulong H1, ulong H2) Hash128(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + encoding ??= Encoding.UTF8; + return Hash128(encoding.GetBytes(text), seed); + } + + /// + /// 计算 MurmurHash3 128位哈希值并返回字节数组 + /// + /// 输入数据 + /// 种子值(默认0) + /// 16字节的哈希值 + public static byte[] Hash128Bytes(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + var result = new byte[16]; + Array.Copy(BitConverter.GetBytes(h1), 0, result, 0, 8); + Array.Copy(BitConverter.GetBytes(h2), 0, result, 8, 8); + return result; + } + + /// + /// 计算 MurmurHash3 128位哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32字符的十六进制字符串 + public static string Hash128Hex(byte[] data, ulong seed = 0) + { + var (h1, h2) = Hash128(data, seed); + return h1.ToString("x16") + h2.ToString("x16"); + } + + private static ulong FinalMix64(ulong k) + { + k ^= k >> 33; + k *= 0xff51afd7ed558ccd; + k ^= k >> 33; + k *= 0xc4ceb9fe1a85ec53; + k ^= k >> 33; + return k; + } + + #endregion + + #region 辅助方法 + + private static uint RotateLeft32(uint x, int r) + { + return (x << r) | (x >> (32 - r)); + } + + private static ulong RotateLeft64(ulong x, int r) + { + return (x << r) | (x >> (64 - r)); + } + + #endregion + + #region 一致性哈希支持 + + /// + /// 计算一致性哈希位置(用于分布式系统) + /// + /// 键值 + /// 桶的数量 + /// 桶的索引(0 到 buckets-1) + public static int ConsistentHash(string key, int buckets) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (buckets <= 0) + throw new ArgumentException("Buckets must be greater than 0", nameof(buckets)); + + uint hash = Hash32(key); + return (int)(hash % (uint)buckets); + } + + /// + /// 计算一致性哈希位置(带虚拟节点) + /// + /// 键值 + /// 桶的数量 + /// 每个桶的虚拟节点数 + /// 桶的索引(0 到 buckets-1) + public static int ConsistentHash(string key, int buckets, int virtualNodes) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (buckets <= 0) + throw new ArgumentException("Buckets must be greater than 0", nameof(buckets)); + if (virtualNodes <= 0) + throw new ArgumentException("Virtual nodes must be greater than 0", nameof(virtualNodes)); + + uint hash = Hash32(key); + int totalNodes = buckets * virtualNodes; + int nodeIndex = (int)(hash % (uint)totalNodes); + return nodeIndex / virtualNodes; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/NanoIdUtil.cs b/EasyTool.Core/CodeCategory/NanoIdUtil.cs new file mode 100644 index 0000000..c6a4302 --- /dev/null +++ b/EasyTool.Core/CodeCategory/NanoIdUtil.cs @@ -0,0 +1,197 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// NanoId 生成器工具类 + /// NanoId 是一个小巧、安全、URL友好的唯一字符串ID生成器 + /// + public static class NanoIdUtil + { + // 默认字母表(URL安全) + private const string DefaultAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + // 数字字母表(仅数字) + private const string NumbersAlphabet = "0123456789"; + + // 小写字母表 + private const string LowercaseAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; + + // 无歧义字符字母表(排除 l, 1, I, O, 0 等) + private const string NoDoppelgangersAlphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"; + + // 密码安全字母表(包含特殊字符) + private const string PasswordAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}|;:,.<>?"; + + /// + /// 生成默认长度的 NanoId(21位) + /// + /// NanoId 字符串 + public static string Generate() + { + return Generate(21); + } + + /// + /// 生成指定长度的 NanoId + /// + /// ID长度 + /// NanoId 字符串 + public static string Generate(int size) + { + return Generate(size, DefaultAlphabet); + } + + /// + /// 生成指定长度和字母表的 NanoId + /// + /// ID长度 + /// 自定义字母表 + /// NanoId 字符串 + public static string Generate(int size, string alphabet) + { + if (size <= 0) + throw new ArgumentException("Size must be greater than 0", nameof(size)); + if (string.IsNullOrEmpty(alphabet)) + throw new ArgumentException("Alphabet cannot be null or empty", nameof(alphabet)); + + return GenerateImpl(size, alphabet); + } + + /// + /// 生成仅数字的 NanoId + /// + /// ID长度(默认21) + /// 仅数字的 ID + public static string GenerateNumbers(int size = 21) + { + return Generate(size, NumbersAlphabet); + } + + /// + /// 生成小写字母+数字的 NanoId + /// + /// ID长度(默认21) + /// 小写字母数字 ID + public static string GenerateLowercase(int size = 21) + { + return Generate(size, LowercaseAlphabet); + } + + /// + /// 生成无歧义字符的 NanoId(排除 l, 1, I, O, 0 等) + /// + /// ID长度(默认21) + /// 无歧义字符的 ID + public static string GenerateNoDoppelgangers(int size = 21) + { + return Generate(size, NoDoppelgangersAlphabet); + } + + /// + /// 生成密码安全的 NanoId(包含特殊字符) + /// + /// ID长度(默认21) + /// 包含特殊字符的 ID + public static string GeneratePassword(int size = 21) + { + return Generate(size, PasswordAlphabet); + } + + /// + /// 生成指定长度的自定义 NanoId(使用自定义随机数生成器) + /// + /// ID长度 + /// 自定义字母表 + /// 自定义随机数生成器 + /// NanoId 字符串 + public static string Generate(int size, string alphabet, Random random) + { + if (size <= 0) + throw new ArgumentException("Size must be greater than 0", nameof(size)); + if (string.IsNullOrEmpty(alphabet)) + throw new ArgumentException("Alphabet cannot be null or empty", nameof(alphabet)); + if (random == null) + throw new ArgumentNullException(nameof(random)); + + var chars = new char[size]; + for (int i = 0; i < size; i++) + { + chars[i] = alphabet[random.Next(alphabet.Length)]; + } + return new string(chars); + } + + /// + /// 异步生成 NanoId(适用于大量生成场景) + /// + /// ID长度(默认21) + /// NanoId 字符串 + public static string GenerateAsync(int size = 21) + { + return Generate(size); + } + + /// + /// 批量生成 NanoId + /// + /// 生成数量 + /// 每个ID的长度(默认21) + /// NanoId 数组 + public static string[] GenerateBatch(int count, int size = 21) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(size); + } + return result; + } + + #region 私有实现 + + private static string GenerateImpl(int size, string alphabet) + { + // 计算掩码 + int mask = (2 << (int)Math.Floor(Math.Log(alphabet.Length - 1) / Math.Log(2))) - 1; + // 计算每个字符需要的平均字节数 + int step = (int)Math.Ceiling(1.6 * mask * size / alphabet.Length); + + var result = new char[size]; + int pos = 0; + + using (var rng = RandomNumberGenerator.Create()) + { + byte[] buffer = new byte[step]; + + while (true) + { + rng.GetBytes(buffer); + + for (int i = 0; i < step && pos < size; i++) + { + int index = buffer[i] & mask; + + if (index < alphabet.Length) + { + result[pos++] = alphabet[index]; + } + } + + if (pos >= size) + { + break; + } + } + } + + return new string(result); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/PunycodeUtil.cs b/EasyTool.Core/CodeCategory/PunycodeUtil.cs index cee3389..85808a2 100644 --- a/EasyTool.Core/CodeCategory/PunycodeUtil.cs +++ b/EasyTool.Core/CodeCategory/PunycodeUtil.cs @@ -1,99 +1,115 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Text; -namespace EasyTool +namespace EasyTool.CodeCategory { /// - /// Punycode 工具类 + /// Punycode 编码工具类 + /// Punycode 是一种将 Unicode 字符串转换为 ASCII 的编码方案 + /// 主要用于国际化域名(IDN),如 "例子.测试" → "xn--fsqu00a.xn--0zwm56d" + /// RFC 3492 标准 /// public static class PunycodeUtil { - private const int BASE = 36; - private const int TMIN = 1; - private const int TMAX = 26; - private const int SKEW = 38; - private const int DAMP = 700; - private const int INITIAL_BIAS = 72; - private const int INITIAL_N = 128; + private const int Base = 36; + private const int TMin = 1; + private const int TMax = 26; + private const int Skew = 38; + private const int Damp = 700; + private const int InitialBias = 72; + private const int InitialN = 0x80; + private const int Delimiter = '-'; - private static readonly char[] DELIMITER = { '-' }; + private const string Base36Chars = "abcdefghijklmnopqrstuvwxyz0123456789"; /// - /// 将给定的 Unicode 字符串按照 Punycode 编码规则进行编码。 + /// 将 Unicode 字符串编码为 Punycode /// - /// 要编码的 Unicode 字符串 - /// 编码后的字符串 + /// Unicode 字符串 + /// Punycode 编码字符串 public static string Encode(string input) { if (string.IsNullOrEmpty(input)) + return string.Empty; + + // 检查是否全是 ASCII + bool allAscii = true; + foreach (char c in input) { - return input; + if (c > 0x7F) + { + allAscii = false; + break; + } } - List inputChars = input.Select(c => (int)c).ToList(); - List basicChars = inputChars.Where(c => c < 0x80).ToList(); - List extendedChars = inputChars.Except(basicChars).ToList(); + if (allAscii) + return input; - List output = new List(); - int n = INITIAL_N; + var result = new StringBuilder(); + int n = InitialN; int delta = 0; - int bias = INITIAL_BIAS; + int bias = InitialBias; + int h = 0; - // Encode the basic code points - foreach (int b in basicChars) + // 处理基本字符(ASCII) + foreach (char c in input) { - output.Add(b); + if (c < 0x80) + { + result.Append(c); + h++; + } } - int h = output.Count; - int bLength = basicChars.Count; - if (bLength > 0 && extendedChars.Count > 0) + int b = h; + if (b > 0) { - output.Add('-'); + result.Append((char)Delimiter); } - // Main encoding loop - while (h < inputChars.Count) + int inputLength = input.Length; + int m = 0; + + while (h < inputLength) { - int m = int.MaxValue; - foreach (int e in extendedChars) + // 找到最小的非基本字符 + m = int.MaxValue; + foreach (char c in input) { - if (e >= n && e < m) + if (c >= n && c < m) { - m = e; + m = c; } } delta += (m - n) * (h + 1); n = m; - foreach (int e in extendedChars) + + foreach (char c in input) { - if (e < n) + if (c < n) { delta++; } - - if (e == n) + else if (c == n) { int q = delta; - int k = BASE; + int k = Base; + while (true) { - int t = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias); + int t = k <= bias ? TMin : (k >= bias + TMax ? TMax : k - bias); if (q < t) - { break; - } - output.Add(GetCodePoint(t + (q - t) % (BASE - t))); - q = (q - t) / (BASE - t); - k += BASE; + result.Append(Base36Chars[t + (q - t) % (Base - t)]); + q = (q - t) / (Base - t); + k += Base; } - output.Add(GetCodePoint(q)); - bias = Adapt(delta, h + 1, h == bLength); + result.Append(Base36Chars[q]); + bias = Adapt(delta, h + 1, h == b); delta = 0; h++; } @@ -103,152 +119,249 @@ public static string Encode(string input) n++; } - return new string(output.Select(c => (char)c).ToArray()); + return result.ToString(); } /// - /// 将给定的 Punycode 编码字符串进行解码,得到原始的 Unicode 字符串。 + /// 将 Punycode 字符串解码为 Unicode /// - /// 要解码的 Punycode 编码字符串 - /// 原始的 Unicode 字符串 + /// Punycode 编码字符串 + /// Unicode 字符串 public static string Decode(string input) { if (string.IsNullOrEmpty(input)) - { - return input; - } + return string.Empty; - List output = new List(); - List inputChars = input.Select(c => (int)c).ToList(); - List basicChars = inputChars.Where(c => c < 0x80).ToList(); - int i = 0; - int n = INITIAL_N; - int bias = INITIAL_BIAS; + // 查找分隔符位置 + int delimiterPos = input.LastIndexOf((char)Delimiter); - // Find the last delimiter - int lastDelim = input.LastIndexOf('-'); - if (lastDelim < 0) - { - lastDelim = 0; - } + var result = new StringBuilder(); - // Decode the basic code points - for (int j = 0; j < lastDelim; j++) + // 处理基本字符 + if (delimiterPos > 0) { - int c = inputChars[j]; - if (!IsBasic(c)) + for (int idx = 0; idx < delimiterPos; idx++) { - throw new ArgumentException("Invalid input string."); + char c = input[idx]; + if (c < 0x80) + { + result.Append(c); + } + else + { + throw new ArgumentException("Invalid Punycode string: non-ASCII character in basic part"); + } } - - output.Add(c); } - // Main decoding loop - int p = lastDelim > 0 ? lastDelim + 1 : 0; - while (p < inputChars.Count) + int i = 0; + int n = InitialN; + int bias = InitialBias; + int pos = delimiterPos + 1; + + while (pos < input.Length) { int oldi = i; int w = 1; - int k = BASE; - while (true) + + for (int k = Base; ; k += Base) { - if (p >= inputChars.Count) - { - throw new ArgumentException("Invalid input string."); - } + if (pos >= input.Length) + throw new ArgumentException("Invalid Punycode string: unexpected end"); - int c = inputChars[p++]; - int digit = GetDigit(c); - if (digit >= BASE) - { - throw new ArgumentException("Invalid input string."); - } + char c = input[pos++]; + int digit = DecodeDigit(c); if (digit > (int.MaxValue - i) / w) - { - throw new ArgumentException("Invalid input string."); - } + throw new ArgumentException("Invalid Punycode string: overflow"); i += digit * w; - int t = k <= bias ? TMIN : (k >= bias + TMAX ? TMAX : k - bias); + + int t = k <= bias ? TMin : (k >= bias + TMax ? TMax : k - bias); + if (digit < t) - { break; - } - if (w > int.MaxValue / (BASE - t)) - { - throw new ArgumentException("Invalid input string."); - } + if (w > int.MaxValue / (Base - t)) + throw new ArgumentException("Invalid Punycode string: overflow"); - w *= BASE - t; - k += BASE; + w *= (Base - t); } - int delta = i - oldi; - output.Add(GetCodePoint(delta)); - bias = Adapt(delta, output.Count, oldi == 0); - n += i / output.Count; - i %= output.Count; + bias = Adapt(i - oldi, result.Length + 1, oldi == 0); + + if (i / (result.Length + 1) > int.MaxValue - n) + throw new ArgumentException("Invalid Punycode string: overflow"); + + n += i / (result.Length + 1); + i %= (result.Length + 1); + + result.Insert(i, (char)n); + i++; } - return new string(output.Select(c => (char)c).ToArray()); + return result.ToString(); } - private static bool IsBasic(int codePoint) + /// + /// 将域名编码为 IDN 格式(带 xn-- 前缀) + /// + /// Unicode 域名 + /// ASCII 域名 + public static string EncodeDomain(string domain) { - return codePoint < 0x80; + if (string.IsNullOrEmpty(domain)) + return string.Empty; + + var parts = domain.Split('.'); + var result = new StringBuilder(); + + for (int i = 0; i < parts.Length; i++) + { + if (i > 0) + result.Append('.'); + + string encoded = Encode(parts[i]); + + // 如果包含非 ASCII 字符,添加 xn-- 前缀 + bool needsPrefix = false; + foreach (char c in parts[i]) + { + if (c > 0x7F) + { + needsPrefix = true; + break; + } + } + + if (needsPrefix) + { + result.Append("xn--"); + result.Append(encoded); + } + else + { + result.Append(parts[i]); + } + } + + return result.ToString(); } - private static int GetDigit(int codePoint) + /// + /// 将 IDN 域名解码为 Unicode 格式 + /// + /// ASCII 域名 + /// Unicode 域名 + public static string DecodeDomain(string domain) { - if (codePoint - '0' < 10) + if (string.IsNullOrEmpty(domain)) + return string.Empty; + + var parts = domain.Split('.'); + var result = new StringBuilder(); + + for (int i = 0; i < parts.Length; i++) { - return codePoint - '0' + 26; + if (i > 0) + result.Append('.'); + + string part = parts[i]; + + // 检查是否有 xn-- 前缀(不区分大小写) + if (part.Length > 4 && + part.StartsWith("xn--", StringComparison.OrdinalIgnoreCase)) + { + string punycode = part.Substring(4); + result.Append(Decode(punycode)); + } + else + { + result.Append(part); + } } - if (codePoint - 'a' < 26) + return result.ToString(); + } + + /// + /// 验证 Punycode 字符串是否有效 + /// + /// Punycode 字符串 + /// 是否有效 + public static bool IsValid(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + try { - return codePoint - 'a'; + string decoded = Decode(input); + string reencoded = Encode(decoded); + return true; } - - if (codePoint - 'A' < 26) + catch { - return codePoint - 'A'; + return false; } - - throw new ArgumentException("Invalid input string."); } - private static int Adapt(int delta, int numPoints, bool firstTime) + /// + /// 尝试解码 Punycode 字符串 + /// + /// Punycode 字符串 + /// 解码结果 + /// 是否解码成功 + public static bool TryDecode(string input, out string result) { - delta = firstTime ? delta / DAMP : delta >> 1; - delta += delta / numPoints; + result = null; - int k = 0; - while (delta > ((BASE - TMIN) * TMAX) / 2) + if (string.IsNullOrEmpty(input)) { - delta /= BASE - TMIN; - k += BASE; + result = string.Empty; + return true; } - return k + (((BASE - TMIN + 1) * delta) / (delta + SKEW)); + try + { + result = Decode(input); + return true; + } + catch + { + return false; + } } - private static int GetCodePoint(int digit) + #region 私有方法 + + private static int Adapt(int delta, int numpoints, bool firsttime) { - if (digit < 26) - { - return digit + 'a'; - } + delta = firsttime ? delta / Damp : delta / 2; + delta += delta / numpoints; - if (digit < 36) + int k = 0; + while (delta > ((Base - TMin) * TMax) / 2) { - return digit - 26 + '0'; + delta /= Base - TMin; + k += Base; } - throw new ArgumentException("Invalid input string."); + return k + (Base - TMin + 1) * delta / (delta + Skew); } + + private static int DecodeDigit(char c) + { + if (c >= 'a' && c <= 'z') + return c - 'a'; + if (c >= 'A' && c <= 'Z') + return c - 'A'; + if (c >= '0' && c <= '9') + return c - '0' + 26; + + throw new ArgumentException($"Invalid Punycode character: {c}"); + } + + #endregion } } diff --git a/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs b/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs new file mode 100644 index 0000000..3f0dfdd --- /dev/null +++ b/EasyTool.Core/CodeCategory/QuotedPrintableUtil.cs @@ -0,0 +1,236 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Quoted-Printable 编码工具类 + /// Quoted-Printable 是一种将 8 位数据编码为 7 位 ASCII 的编码方式 + /// 常用于电子邮件(MIME),将非 ASCII 字符编码为 =XX 格式 + /// RFC 2045 标准 + /// + public static class QuotedPrintableUtil + { + private const int MaxLineLength = 76; + + /// + /// 将字节数组编码为 Quoted-Printable 字符串 + /// + /// 要编码的数据 + /// 每行最大长度 + /// Quoted-Printable 编码字符串 + public static string Encode(byte[] data, int lineLength = 76) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + int currentLineLength = 0; + + foreach (byte b in data) + { + string encoded = EncodeByte(b); + int encodedLength = encoded.Length; + + // 检查是否需要换行 + if (currentLineLength + encodedLength > lineLength - 1) + { + result.Append("=\r\n"); + currentLineLength = 0; + } + + result.Append(encoded); + currentLineLength += encodedLength; + } + + return result.ToString(); + } + + /// + /// 将 Quoted-Printable 字符串解码为字节数组 + /// + /// Quoted-Printable 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var result = new System.Collections.Generic.List(); + + for (int i = 0; i < encoded.Length; i++) + { + char c = encoded[i]; + + if (c == '=') + { + if (i + 1 >= encoded.Length) + break; + + char next = encoded[i + 1]; + + // 软换行 + if (next == '\r' || next == '\n') + { + i++; + if (next == '\r' && i + 1 < encoded.Length && encoded[i + 1] == '\n') + i++; + continue; + } + + // 编码字符 + if (i + 2 < encoded.Length) + { + string hex = encoded.Substring(i + 1, 2); + if (TryParseHex(hex, out byte b)) + { + result.Add(b); + i += 2; + continue; + } + } + } + else if (c != '\r' && c != '\n') + { + result.Add((byte)c); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 Quoted-Printable(使用指定编码) + /// + /// 文本 + /// 编码方式 + /// 每行最大长度 + /// Quoted-Printable 编码字符串 + public static string EncodeString(string text, Encoding encoding = null, int lineLength = 76) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + byte[] bytes = encoding.GetBytes(text); + return Encode(bytes, lineLength); + } + + /// + /// 将 Quoted-Printable 字符串解码为文本(使用指定编码) + /// + /// Quoted-Printable 编码字符串 + /// 编码方式 + /// 解码后的文本 + public static string DecodeToString(string encoded, Encoding encoding = null) + { + if (string.IsNullOrEmpty(encoded)) + return string.Empty; + + byte[] bytes = Decode(encoded); + encoding ??= Encoding.UTF8; + return encoding.GetString(bytes); + } + + /// + /// 验证 Quoted-Printable 字符串是否有效 + /// + /// Quoted-Printable 编码字符串 + /// 是否有效 + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return true; + + for (int i = 0; i < encoded.Length; i++) + { + char c = encoded[i]; + + if (c == '=') + { + if (i + 1 >= encoded.Length) + return false; + + char next = encoded[i + 1]; + + // 软换行 + if (next == '\r' || next == '\n') + continue; + + // 编码字符 + if (i + 2 >= encoded.Length) + return false; + + string hex = encoded.Substring(i + 1, 2); + if (!IsHexDigit(hex[0]) || !IsHexDigit(hex[1])) + return false; + + i += 2; + } + else if (c < 32 && c != '\r' && c != '\n' && c != '\t') + { + return false; + } + else if (c > 126) + { + return false; + } + } + + return true; + } + + private static string EncodeByte(byte b) + { + // 可打印 ASCII 字符(33-126,除了 61 '=') + if (b >= 33 && b <= 126 && b != 61) + { + return ((char)b).ToString(); + } + + // 制表符和空格(特殊处理) + if (b == 9 || b == 32) + { + return "=" + b.ToString("X2"); + } + + // 其他字符编码为 =XX + return "=" + b.ToString("X2"); + } + + private static bool TryParseHex(string hex, out byte result) + { + result = 0; + + if (hex.Length != 2) + return false; + + if (!IsHexDigit(hex[0]) || !IsHexDigit(hex[1])) + return false; + + result = Convert.ToByte(hex, 16); + return true; + } + + private static bool IsHexDigit(char c) + { + return (c >= '0' && c <= '9') || + (c >= 'A' && c <= 'F') || + (c >= 'a' && c <= 'f'); + } + + /// + /// 获取 Quoted-Printable 编码后的预计最大长度 + /// + /// 输入长度 + /// 最大输出长度 + public static int CalculateMaxEncodedLength(int inputLength) + { + if (inputLength == 0) + return 0; + + // 最坏情况:每个字符都编码为 =XX,加上软换行 + return inputLength * 3 + (inputLength * 3 / MaxLineLength) * 3; + } + } +} diff --git a/EasyTool.Core/CodeCategory/RC4Util.cs b/EasyTool.Core/CodeCategory/RC4Util.cs new file mode 100644 index 0000000..32d19f0 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RC4Util.cs @@ -0,0 +1,303 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// RC4 流加密工具类 + /// RC4 是一种广泛使用的流密码,由 Ron Rivest 设计 + /// 注意:RC4 已被认为不安全,建议使用 ChaCha20 替代 + /// 保留用于兼容旧系统 + /// + public static class RC4Util + { + /// + /// 使用 RC4 加密/解密数据(对称操作) + /// + /// 输入数据 + /// 密钥(1-256字节) + /// 加密/解密后的数据 + public static byte[] Process(byte[] data, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + return Process(data, 0, data.Length, key); + } + + /// + /// 使用 RC4 加密/解密数据 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 密钥 + /// 加密/解密后的数据 + public static byte[] Process(byte[] data, int offset, int length, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + byte[] result = new byte[length]; + byte[] s = new byte[256]; + byte[] k = new byte[256]; + + // 密钥调度算法(KSA) + for (int i = 0; i < 256; i++) + { + s[i] = (byte)i; + k[i] = key[i % key.Length]; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + s[i] + k[i]) & 0xFF; + Swap(ref s[i], ref s[j]); + } + + // 伪随机生成算法(PRGA) + int a = 0; + int b = 0; + + for (int i = 0; i < length; i++) + { + a = (a + 1) & 0xFF; + b = (b + s[a]) & 0xFF; + Swap(ref s[a], ref s[b]); + byte t = (byte)((s[a] + s[b]) & 0xFF); + result[i] = (byte)(data[offset + i] ^ s[t]); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Process(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Process(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 加密字符串并返回十六进制 + /// + /// 明文 + /// 密钥 + /// 十六进制密文 + public static string EncryptToHex(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Process(data, key); + return BitConverter.ToString(encrypted).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制解密字符串 + /// + /// 十六进制密文 + /// 密钥 + /// 明文字符串 + public static string DecryptFromHex(string cipherHex, byte[] key) + { + if (string.IsNullOrEmpty(cipherHex)) + return string.Empty; + + byte[] data = HexToBytes(cipherHex); + byte[] decrypted = Process(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度(1-256) + /// 随机密钥 + public static byte[] GenerateKey(int length = 16) + { + if (length < 1 || length > 256) + throw new ArgumentException("Key length must be between 1 and 256", nameof(length)); + + byte[] key = new byte[length]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 密钥长度 + /// 十六进制密钥 + public static string GenerateKeyHex(int length = 16) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 创建 RC4 流处理器(用于流式处理) + /// + /// 密钥 + /// RC4 处理器 + public static RC4Processor CreateProcessor(byte[] key) + { + return new RC4Processor(key); + } + + private static void Swap(ref byte a, ref byte b) + { + byte temp = a; + a = b; + b = temp; + } + + private static byte[] HexToBytes(string hex) + { + if (hex.Length % 2 != 0) + hex = "0" + hex; + + byte[] bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return bytes; + } + } + + /// + /// RC4 流处理器(支持状态保持) + /// + public class RC4Processor + { + private readonly byte[] _s = new byte[256]; + private int _i; + private int _j; + + /// + /// 创建 RC4 处理器 + /// + /// 密钥 + public RC4Processor(byte[] key) + { + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + // KSA + for (int i = 0; i < 256; i++) + { + _s[i] = (byte)i; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + _s[i] + key[i % key.Length]) & 0xFF; + Swap(ref _s[i], ref _s[j]); + } + + _i = 0; + _j = 0; + } + + /// + /// 处理一个字节 + /// + /// 输入字节 + /// 输出字节 + public byte ProcessByte(byte input) + { + _i = (_i + 1) & 0xFF; + _j = (_j + _s[_i]) & 0xFF; + Swap(ref _s[_i], ref _s[_j]); + byte t = (byte)((_s[_i] + _s[_j]) & 0xFF); + return (byte)(input ^ _s[t]); + } + + /// + /// 处理多个字节 + /// + /// 输入数据 + /// 输出数据 + public byte[] Process(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = ProcessByte(data[i]); + } + return result; + } + + /// + /// 重置处理器状态 + /// + /// 密钥 + public void Reset(byte[] key) + { + if (key == null || key.Length < 1 || key.Length > 256) + throw new ArgumentException("Key must be between 1 and 256 bytes", nameof(key)); + + for (int i = 0; i < 256; i++) + { + _s[i] = (byte)i; + } + + int j = 0; + for (int i = 0; i < 256; i++) + { + j = (j + _s[i] + key[i % key.Length]) & 0xFF; + Swap(ref _s[i], ref _s[j]); + } + + _i = 0; + _j = 0; + } + + private static void Swap(ref byte a, ref byte b) + { + byte temp = a; + a = b; + b = temp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/RIPEMD160Util.cs b/EasyTool.Core/CodeCategory/RIPEMD160Util.cs new file mode 100644 index 0000000..ef0ed12 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RIPEMD160Util.cs @@ -0,0 +1,362 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// RIPEMD-160 哈希工具类 + /// RIPEMD-160 是一种 160 位加密哈希函数 + /// 由欧洲 RIPE 项目开发,比特币地址使用此算法 + /// 比 SHA-1 更安全 + /// + public static class RIPEMD160Util + { + private const int DigestSize = 20; + private const int BlockSize = 64; + + // 初始值 + private static readonly uint[] IV = new uint[] + { + 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, + 0x7658def0, 0x890abc12, 0xfedcba34, 0x01234567, 0x32107654 + }; + + /// + /// 计算 RIPEMD-160 哈希值 + /// + /// 输入数据 + /// 20字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + data = Array.Empty(); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算 RIPEMD-160 哈希值 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 20字节哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + uint[] h = new uint[10]; + Array.Copy(IV, h, 10); + + byte[] padded = PadMessage(data, offset, length); + int blocks = padded.Length / BlockSize; + + for (int i = 0; i < blocks; i++) + { + ProcessBlock(padded, i * BlockSize, h); + } + + byte[] result = new byte[DigestSize]; + for (int i = 0; i < 5; i++) + { + result[i * 4] = (byte)h[i]; + result[i * 4 + 1] = (byte)(h[i] >> 8); + result[i * 4 + 2] = (byte)(h[i] >> 16); + result[i * 4 + 3] = (byte)(h[i] >> 24); + } + + return result; + } + + /// + /// 计算字符串的 RIPEMD-160 哈希值 + /// + /// 文本 + /// 20字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 RIPEMD-160 哈希的十六进制表示 + /// + /// 输入数据 + /// 40字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 计算字符串的 RIPEMD-160 哈希十六进制表示 + /// + /// 文本 + /// 40字符的十六进制字符串 + public static string ComputeStringHex(string text) + { + byte[] hash = ComputeString(text); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + private static byte[] PadMessage(byte[] data, int offset, int length) + { + long bitLength = (long)length * 8; + int padding = 64 - ((length + 9) % 64); + if (padding == 64) padding = 0; + + byte[] result = new byte[length + 1 + padding + 8]; + Array.Copy(data, offset, result, 0, length); + + result[length] = 0x80; + + // 添加长度(小端序) + for (int i = 0; i < 8; i++) + { + result[result.Length - 8 + i] = (byte)(bitLength >> (i * 8)); + } + + return result; + } + + private static void ProcessBlock(byte[] block, int offset, uint[] h) + { + uint[] x = new uint[16]; + for (int i = 0; i < 16; i++) + { + x[i] = BitConverter.ToUInt32(block, offset + i * 4); + } + + uint al = h[0], bl = h[1], cl = h[2], dl = h[3], el = h[4]; + uint ar = h[5], br = h[6], cr = h[7], dr = h[8], er = h[9]; + + // 左侧 + al = F1(al, bl, cl, dl, el, x[0], 11); + el = F1(el, al, bl, cl, dl, x[1], 14); + dl = F1(dl, el, al, bl, cl, x[2], 15); + cl = F1(cl, dl, el, al, bl, x[3], 12); + bl = F1(bl, cl, dl, el, al, x[4], 5); + al = F1(al, bl, cl, dl, el, x[5], 8); + el = F1(el, al, bl, cl, dl, x[6], 7); + dl = F1(dl, el, al, bl, cl, x[7], 9); + cl = F1(cl, dl, el, al, bl, x[8], 11); + bl = F1(bl, cl, dl, el, al, x[9], 13); + al = F1(al, bl, cl, dl, el, x[10], 14); + el = F1(el, al, bl, cl, dl, x[11], 15); + dl = F1(dl, el, al, bl, cl, x[12], 6); + cl = F1(cl, dl, el, al, bl, x[13], 7); + bl = F1(bl, cl, dl, el, al, x[14], 9); + al = F1(al, bl, cl, dl, el, x[15], 8); + + // 右侧 + ar = F5(ar, br, cr, dr, er, x[5], 8); + er = F5(er, ar, br, cr, dr, x[14], 9); + dr = F5(dr, er, ar, br, cr, x[7], 9); + cr = F5(cr, dr, er, ar, br, x[0], 11); + br = F5(br, cr, dr, er, ar, x[9], 13); + ar = F5(ar, br, cr, dr, er, x[2], 15); + er = F5(er, ar, br, cr, dr, x[11], 15); + dr = F5(dr, er, ar, br, cr, x[4], 5); + cr = F5(cr, dr, er, ar, br, x[13], 7); + br = F5(br, cr, dr, er, ar, x[6], 7); + ar = F5(ar, br, cr, dr, er, x[15], 8); + er = F5(er, ar, br, cr, dr, x[8], 11); + dr = F5(dr, er, ar, br, cr, x[1], 14); + cr = F5(cr, dr, er, ar, br, x[10], 14); + br = F5(br, cr, dr, er, ar, x[3], 12); + ar = F5(ar, br, cr, dr, er, x[12], 6); + + // 第二轮左侧 + bl = F2(bl, cl, dl, el, al, x[7], 7); + al = F2(al, bl, cl, dl, el, x[4], 6); + el = F2(el, al, bl, cl, dl, x[13], 8); + dl = F2(dl, el, al, bl, cl, x[1], 13); + cl = F2(cl, dl, el, al, bl, x[10], 11); + bl = F2(bl, cl, dl, el, al, x[6], 9); + al = F2(al, bl, cl, dl, el, x[15], 7); + el = F2(el, al, bl, cl, dl, x[3], 15); + dl = F2(dl, el, al, bl, cl, x[12], 7); + cl = F2(cl, dl, el, al, bl, x[0], 12); + bl = F2(bl, cl, dl, el, al, x[9], 15); + al = F2(al, bl, cl, dl, el, x[5], 9); + el = F2(el, al, bl, cl, dl, x[2], 11); + dl = F2(dl, el, al, bl, cl, x[14], 7); + cl = F2(cl, dl, el, al, bl, x[11], 13); + bl = F2(bl, cl, dl, el, al, x[8], 12); + + // 第二轮右侧 + br = F4(br, cr, dr, er, ar, x[6], 9); + ar = F4(ar, br, cr, dr, er, x[11], 13); + er = F4(er, ar, br, cr, dr, x[3], 15); + dr = F4(dr, er, ar, br, cr, x[7], 7); + cr = F4(cr, dr, er, ar, br, x[0], 12); + br = F4(br, cr, dr, er, ar, x[13], 8); + ar = F4(ar, br, cr, dr, er, x[5], 9); + er = F4(er, ar, br, cr, dr, x[10], 11); + dr = F4(dr, er, ar, br, cr, x[14], 7); + cr = F4(cr, dr, er, ar, br, x[15], 7); + br = F4(br, cr, dr, er, ar, x[8], 12); + ar = F4(ar, br, cr, dr, er, x[12], 7); + er = F4(er, ar, br, cr, dr, x[4], 6); + dr = F4(dr, er, ar, br, cr, x[9], 15); + cr = F4(cr, dr, er, ar, br, x[1], 13); + br = F4(br, cr, dr, er, ar, x[2], 11); + + // 第三轮左侧 + cl = F3(cl, dl, el, al, bl, x[3], 11); + bl = F3(bl, cl, dl, el, al, x[10], 13); + al = F3(al, bl, cl, dl, el, x[14], 6); + el = F3(el, al, bl, cl, dl, x[4], 7); + dl = F3(dl, el, al, bl, cl, x[9], 14); + cl = F3(cl, dl, el, al, bl, x[15], 9); + bl = F3(bl, cl, dl, el, al, x[8], 13); + al = F3(al, bl, cl, dl, el, x[1], 15); + el = F3(el, al, bl, cl, dl, x[2], 14); + dl = F3(dl, el, al, bl, cl, x[7], 8); + cl = F3(cl, dl, el, al, bl, x[0], 13); + bl = F3(bl, cl, dl, el, al, x[6], 6); + al = F3(al, bl, cl, dl, el, x[13], 5); + el = F3(el, al, bl, cl, dl, x[11], 12); + dl = F3(dl, el, al, bl, cl, x[5], 7); + cl = F3(cl, dl, el, al, bl, x[12], 5); + + // 第三轮右侧 + cr = F3(cr, dr, er, ar, br, x[15], 8); + br = F3(br, cr, dr, er, ar, x[5], 9); + ar = F3(ar, br, cr, dr, er, x[1], 14); + er = F3(er, ar, br, cr, dr, x[3], 9); + dr = F3(dr, er, ar, br, cr, x[7], 13); + cr = F3(cr, dr, er, ar, br, x[14], 15); + br = F3(br, cr, dr, er, ar, x[6], 7); + ar = F3(ar, br, cr, dr, er, x[9], 12); + er = F3(er, ar, br, cr, dr, x[11], 8); + dr = F3(dr, er, ar, br, cr, x[8], 9); + cr = F3(cr, dr, er, ar, br, x[12], 11); + br = F3(br, cr, dr, er, ar, x[2], 7); + ar = F3(ar, br, cr, dr, er, x[10], 7); + er = F3(er, ar, br, cr, dr, x[0], 12); + dr = F3(dr, er, ar, br, cr, x[4], 7); + cr = F3(cr, dr, er, ar, br, x[13], 7); + + // 第四轮左侧 + dl = F4(dl, el, al, bl, cl, x[1], 11); + cl = F4(cl, dl, el, al, bl, x[9], 12); + bl = F4(bl, cl, dl, el, al, x[11], 14); + al = F4(al, bl, cl, dl, el, x[10], 15); + el = F4(el, al, bl, cl, dl, x[0], 14); + dl = F4(dl, el, al, bl, cl, x[8], 15); + cl = F4(cl, dl, el, al, bl, x[12], 9); + bl = F4(bl, cl, dl, el, al, x[4], 8); + al = F4(al, bl, cl, dl, el, x[13], 9); + el = F4(el, al, bl, cl, dl, x[3], 14); + dl = F4(dl, el, al, bl, cl, x[7], 5); + cl = F4(cl, dl, el, al, bl, x[15], 6); + bl = F4(bl, cl, dl, el, al, x[14], 8); + al = F4(al, bl, cl, dl, el, x[5], 6); + el = F4(el, al, bl, cl, dl, x[6], 5); + dl = F4(dl, el, al, bl, cl, x[2], 12); + + // 第四轮右侧 + dr = F2(dr, er, ar, br, cr, x[8], 15); + cr = F2(cr, dr, er, ar, br, x[6], 5); + br = F2(br, cr, dr, er, ar, x[4], 8); + ar = F2(ar, br, cr, dr, er, x[1], 11); + er = F2(er, ar, br, cr, dr, x[3], 14); + dr = F2(dr, er, ar, br, cr, x[11], 14); + cr = F2(cr, dr, er, ar, br, x[15], 6); + br = F2(br, cr, dr, er, ar, x[0], 14); + ar = F2(ar, br, cr, dr, er, x[5], 6); + er = F2(er, ar, br, cr, dr, x[12], 9); + dr = F2(dr, er, ar, br, cr, x[2], 12); + cr = F2(cr, dr, er, ar, br, x[13], 9); + br = F2(br, cr, dr, er, ar, x[9], 12); + ar = F2(ar, br, cr, dr, er, x[7], 5); + er = F2(er, ar, br, cr, dr, x[10], 15); + dr = F2(dr, er, ar, br, cr, x[14], 8); + + // 第五轮左侧 + el = F5(el, al, bl, cl, dl, x[4], 9); + dl = F5(dl, el, al, bl, cl, x[0], 15); + cl = F5(cl, dl, el, al, bl, x[5], 5); + bl = F5(bl, cl, dl, el, al, x[9], 11); + al = F5(al, bl, cl, dl, el, x[7], 6); + el = F5(el, al, bl, cl, dl, x[12], 8); + dl = F5(dl, el, al, bl, cl, x[2], 13); + cl = F5(cl, dl, el, al, bl, x[10], 12); + bl = F5(bl, cl, dl, el, al, x[14], 5); + al = F5(al, bl, cl, dl, el, x[1], 12); + el = F5(el, al, bl, cl, dl, x[3], 13); + dl = F5(dl, el, al, bl, cl, x[8], 14); + cl = F5(cl, dl, el, al, bl, x[11], 11); + bl = F5(bl, cl, dl, el, al, x[6], 8); + al = F5(al, bl, cl, dl, el, x[15], 5); + el = F5(el, al, bl, cl, dl, x[13], 6); + + // 第五轮右侧 + er = F1(er, ar, br, cr, dr, x[12], 8); + dr = F1(dr, er, ar, br, cr, x[15], 5); + cr = F1(cr, dr, er, ar, br, x[10], 12); + br = F1(br, cr, dr, er, ar, x[4], 9); + ar = F1(ar, br, cr, dr, er, x[1], 12); + er = F1(er, ar, br, cr, dr, x[5], 5); + dr = F1(dr, er, ar, br, cr, x[8], 14); + cr = F1(cr, dr, er, ar, br, x[7], 6); + br = F1(br, cr, dr, er, ar, x[6], 8); + ar = F1(ar, br, cr, dr, er, x[2], 13); + er = F1(er, ar, br, cr, dr, x[13], 6); + dr = F1(dr, er, ar, br, cr, x[14], 5); + cr = F1(cr, dr, er, ar, br, x[0], 15); + br = F1(br, cr, dr, er, ar, x[3], 13); + ar = F1(ar, br, cr, dr, er, x[9], 11); + er = F1(er, ar, br, cr, dr, x[11], 11); + + // 最终更新 + uint t = h[1] + cl + dr; + h[1] = h[2] + dl + er; + h[2] = h[3] + el + ar; + h[3] = h[4] + al + br; + h[4] = h[0] + bl + cr; + h[0] = t; + } + + private static uint F1(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + (b ^ c ^ d) + x, s) + e; + } + + private static uint F2(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b & c) | (~b & d)) + x + 0x5a827999, s) + e; + } + + private static uint F3(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b | ~c) ^ d) + x + 0x6ed9eba1, s) + e; + } + + private static uint F4(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + ((b & d) | (c & ~d)) + x + 0x8f1bbcdc, s) + e; + } + + private static uint F5(uint a, uint b, uint c, uint d, uint e, uint x, int s) + { + return RotateLeft(a + (b ^ (c | ~d)) + x + 0xa953fd4e, s) + e; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + } +} diff --git a/EasyTool.Core/CodeCategory/ROT13Util.cs b/EasyTool.Core/CodeCategory/ROT13Util.cs new file mode 100644 index 0000000..a0bcdb4 --- /dev/null +++ b/EasyTool.Core/CodeCategory/ROT13Util.cs @@ -0,0 +1,143 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// ROT13/ROT47 编码工具类 + /// ROT13 是一种简单的字母替换加密(凯撒密码的特例) + /// ROT47 扩展到所有 ASCII 可打印字符 + /// 注意:这不是真正的加密,只是一种混淆方式 + /// + public static class ROT13Util + { + /// + /// 使用 ROT13 编码文本 + /// + /// 文本 + /// 编码后的文本 + public static string Encode(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT13 解码文本(编码和解码相同) + /// + /// 文本 + /// 解码后的文本 + public static string Decode(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT13 编码文本(Encode 的别名) + /// + /// 文本 + /// 编码后的文本 + public static string ROT13(string text) + { + return Rotate(text, 13); + } + + /// + /// 使用 ROT47 编码文本 + /// + /// 文本 + /// 编码后的文本 + public static string ROT47(string text) + { + return Rotate47(text); + } + + /// + /// 使用指定偏移量旋转字母 + /// + /// 文本 + /// 偏移量 + /// 旋转后的文本 + public static string Rotate(string text, int shift) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + shift = ((shift % 26) + 26) % 26; // 标准化偏移量 + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 'A' && c <= 'Z') + { + result.Append((char)('A' + (c - 'A' + shift) % 26)); + } + else if (c >= 'a' && c <= 'z') + { + result.Append((char)('a' + (c - 'a' + shift) % 26)); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 使用 ROT47 旋转字符 + /// + /// 文本 + /// 旋转后的文本 + public static string Rotate47(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var result = new StringBuilder(text.Length); + + foreach (char c in text) + { + if (c >= 33 && c <= 126) + { + result.Append((char)(33 + ((c - 33 + 47) % 94))); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 检测文本是否可能是 ROT13 编码(启发式) + /// + /// 文本 + /// 可能性评分(0-1) + public static double DetectROT13(string text) + { + if (string.IsNullOrEmpty(text)) + return 0; + + int letterCount = 0; + int nonLetterCount = 0; + + foreach (char c in text) + { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + letterCount++; + else if (c >= 33 && c <= 126) + nonLetterCount++; + } + + if (letterCount == 0) + return 0; + + // ROT13 编码的文本通常有较高的字母比例 + return (double)letterCount / (letterCount + nonLetterCount); + } + } +} diff --git a/EasyTool.Core/CodeCategory/RabbitUtil.cs b/EasyTool.Core/CodeCategory/RabbitUtil.cs new file mode 100644 index 0000000..a09cf52 --- /dev/null +++ b/EasyTool.Core/CodeCategory/RabbitUtil.cs @@ -0,0 +1,269 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Rabbit 流加密工具类 + /// Rabbit 是一种高速流密码,设计用于软件实现 + /// 128位密钥,64位IV(可选) + /// + public static class RabbitUtil + { + private const int KeySize = 16; + private const int IvSize = 8; + + /// + /// 加密数据 + /// + /// 明文 + /// 密钥(16字节) + /// 初始化向量(8字节,可选) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] iv = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != KeySize) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + var state = Initialize(key, iv); + byte[] result = new byte[plainText.Length]; + + for (int i = 0; i < plainText.Length; i++) + { + if (i % 16 == 0) + { + NextState(state); + } + + byte keyByte = (byte)(state.S[i % 16] ^ (state.S[(i % 16) + 1] >> 8)); + result[i] = (byte)(plainText[i] ^ keyByte); + } + + return result; + } + + /// + /// 解密数据(加密和解密相同) + /// + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv = null) + { + return Encrypt(cipherText, key, iv); + } + + /// + /// 加密字符串并返回 Base64(包含 IV) + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] iv = new byte[IvSize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(iv); + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key, iv); + + byte[] result = new byte[IvSize + encrypted.Length]; + Array.Copy(iv, result, IvSize); + Array.Copy(encrypted, 0, result, IvSize, encrypted.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串(包含 IV) + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < IvSize) + throw new ArgumentException("Invalid cipher text", nameof(cipherText)); + + byte[] iv = new byte[IvSize]; + Array.Copy(data, iv, IvSize); + + byte[] encrypted = new byte[data.Length - IvSize]; + Array.Copy(data, IvSize, encrypted, 0, encrypted.Length); + + byte[] decrypted = Decrypt(encrypted, key, iv); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey() + { + byte[] key = new byte[KeySize]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static RabbitState Initialize(byte[] key, byte[] iv) + { + var state = new RabbitState(); + + // 密钥初始化 + for (int i = 0; i < 8; i++) + { + state.X[i] = (ushort)((key[(i * 2) % 16] << 8) | key[(i * 2 + 1) % 16]); + state.C[i] = state.X[i]; + } + + state.Carry = 0; + + // 执行4次状态更新 + for (int i = 0; i < 4; i++) + { + NextState(state); + } + + // 复制状态到 C + for (int i = 0; i < 8; i++) + { + state.C[(i + 4) % 8] ^= state.X[i]; + } + + // 如果有 IV,进行 IV 设置 + if (iv != null && iv.Length >= IvSize) + { + SetupIv(state, iv); + } + + // 生成初始密钥流 + NextState(state); + for (int i = 0; i < 16; i++) + { + state.S[i] = 0; + } + ExtractKeyStream(state); + + return state; + } + + private static void SetupIv(RabbitState state, byte[] iv) + { + // 将 64 位 IV 映射到计数器 + state.C[0] ^= (ushort)((iv[0] << 8) | iv[1]); + state.C[1] ^= (ushort)((iv[2] << 8) | iv[3]); + state.C[2] ^= (ushort)((iv[4] << 8) | iv[5]); + state.C[3] ^= (ushort)((iv[6] << 8) | iv[7]); + state.C[4] ^= (ushort)((iv[4] << 8) | iv[5]); + state.C[5] ^= (ushort)((iv[6] << 8) | iv[7]); + state.C[6] ^= (ushort)((iv[0] << 8) | iv[1]); + state.C[7] ^= (ushort)((iv[2] << 8) | iv[3]); + + // 执行4次状态更新 + for (int i = 0; i < 4; i++) + { + NextState(state); + } + } + + private static void NextState(RabbitState state) + { + uint[] g = new uint[8]; + uint[] newC = new uint[8]; + ushort[] newX = new ushort[8]; + uint newCarry; + + // 计数器更新 + uint a = 0x4D34D34D; + uint b = 0xD34D34D3; + uint c = 0x34D34D34; + + newC[0] = (uint)((state.C[0] + a + state.Carry) & 0xFFFFFFFF); + newC[1] = (uint)((state.C[1] + b + (newC[0] < state.C[0] ? 1u : 0)) & 0xFFFFFFFF); + newC[2] = (uint)((state.C[2] + c + (newC[1] < state.C[1] ? 1u : 0)) & 0xFFFFFFFF); + newC[3] = (uint)((state.C[3] + a + (newC[2] < state.C[2] ? 1u : 0)) & 0xFFFFFFFF); + newC[4] = (uint)((state.C[4] + b + (newC[3] < state.C[3] ? 1u : 0)) & 0xFFFFFFFF); + newC[5] = (uint)((state.C[5] + c + (newC[4] < state.C[4] ? 1u : 0)) & 0xFFFFFFFF); + newC[6] = (uint)((state.C[6] + a + (newC[5] < state.C[5] ? 1u : 0)) & 0xFFFFFFFF); + newC[7] = (uint)((state.C[7] + b + (newC[6] < state.C[6] ? 1u : 0)) & 0xFFFFFFFF); + + newCarry = newC[7] < state.C[7] ? 1u : 0u; + + // G 函数 + for (int i = 0; i < 8; i++) + { + g[i] = GFunction((ushort)newC[i]); + } + + // 状态更新 + newX[0] = (ushort)((g[0] + RotateLeft16((ushort)g[7], 16) + RotateLeft16((ushort)g[6], 16)) & 0xFFFF); + newX[1] = (ushort)((g[1] + RotateLeft16((ushort)g[0], 8) + g[7]) & 0xFFFF); + newX[2] = (ushort)((g[2] + RotateLeft16((ushort)g[1], 16) + RotateLeft16((ushort)g[0], 16)) & 0xFFFF); + newX[3] = (ushort)((g[3] + RotateLeft16((ushort)g[2], 8) + g[1]) & 0xFFFF); + newX[4] = (ushort)((g[4] + RotateLeft16((ushort)g[3], 16) + RotateLeft16((ushort)g[2], 16)) & 0xFFFF); + newX[5] = (ushort)((g[5] + RotateLeft16((ushort)g[4], 8) + g[3]) & 0xFFFF); + newX[6] = (ushort)((g[6] + RotateLeft16((ushort)g[5], 16) + RotateLeft16((ushort)g[4], 16)) & 0xFFFF); + newX[7] = (ushort)((g[7] + RotateLeft16((ushort)g[6], 8) + g[5]) & 0xFFFF); + + for (int i = 0; i < 8; i++) + { + state.X[i] = (ushort)(newX[i] & 0xFFFF); + state.C[i] = (ushort)(newC[i] & 0xFFFF); + } + state.Carry = newCarry; + + ExtractKeyStream(state); + } + + private static uint GFunction(ushort x) + { + uint result = (uint)(x * x); + return (result ^ (result >> 16)) & 0xFFFF; + } + + private static void ExtractKeyStream(RabbitState state) + { + state.S[0] = (byte)(state.X[0] ^ (state.X[5] >> 8)); + state.S[1] = (byte)(state.X[0] >> 8 ^ state.X[3]); + state.S[2] = (byte)(state.X[2] ^ (state.X[7] >> 8)); + state.S[3] = (byte)(state.X[2] >> 8 ^ state.X[5]); + state.S[4] = (byte)(state.X[4] ^ (state.X[1] >> 8)); + state.S[5] = (byte)(state.X[4] >> 8 ^ state.X[7]); + state.S[6] = (byte)(state.X[6] ^ (state.X[3] >> 8)); + state.S[7] = (byte)(state.X[6] >> 8 ^ state.X[1]); + state.S[8] = (byte)(state.X[0] ^ state.X[5]); + state.S[9] = (byte)((state.X[0] >> 8) ^ (state.X[3] >> 8)); + state.S[10] = (byte)(state.X[2] ^ state.X[7]); + state.S[11] = (byte)((state.X[2] >> 8) ^ (state.X[5] >> 8)); + state.S[12] = (byte)(state.X[4] ^ state.X[1]); + state.S[13] = (byte)((state.X[4] >> 8) ^ (state.X[7] >> 8)); + state.S[14] = (byte)(state.X[6] ^ state.X[3]); + state.S[15] = (byte)((state.X[6] >> 8) ^ (state.X[1] >> 8)); + } + + private static ushort RotateLeft16(ushort x, int n) + { + return (ushort)((x << n) | (x >> (16 - n))); + } + + private class RabbitState + { + public ushort[] X = new ushort[8]; + public ushort[] C = new ushort[8]; + public uint Carry; + public byte[] S = new byte[16]; + } + } +} diff --git a/EasyTool.Core/CodeCategory/RotUtil.cs b/EasyTool.Core/CodeCategory/RotUtil.cs deleted file mode 100644 index f2b86c3..0000000 --- a/EasyTool.Core/CodeCategory/RotUtil.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// ROT 工具类 - /// - public static class RotUtil - { - /// - /// 将给定的字符串按照 ROT 加密算法进行加密。 - /// - /// 要加密的字符串 - /// 偏移量 - /// 加密后的字符串 - public static string Encrypt(string text, int n) - { - if (string.IsNullOrEmpty(text)) - { - return text; - } - - string upperCaseText = text.ToUpper(); - return new string(upperCaseText.Select(c => - { - if (!char.IsLetter(c)) - { - return c; - } - - int x = c - 'A'; - int y = (x + n) % 26; - return (char)(y + 'A'); - }).ToArray()); - } - - /// - /// 将给定的字符串按照 ROT 加密算法进行解密。 - /// - /// 要解密的字符串 - /// 偏移量 - /// 解密后的字符串 - public static string Decrypt(string text, int n) - { - return Encrypt(text, 26 - n); - } - } -} diff --git a/EasyTool.Core/CodeCategory/Salsa20Util.cs b/EasyTool.Core/CodeCategory/Salsa20Util.cs new file mode 100644 index 0000000..e078460 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Salsa20Util.cs @@ -0,0 +1,189 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Salsa20 流加密工具类 + /// Salsa20 是一种高速流密码,由 Daniel J. Bernstein 设计 + /// ChaCha20 是 Salsa20 的改进版本 + /// + public static class Salsa20Util + { + private static readonly uint[] Sigma = new uint[] { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 }; + + /// + /// 使用 Salsa20 加密数据 + /// + /// 明文 + /// 密钥(16或32字节) + /// 随机数(8字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] nonce) + { + return Encrypt(plainText, 0, plainText?.Length ?? 0, key, nonce, 0); + } + + /// + /// 使用 Salsa20 加密数据 + /// + public static byte[] Encrypt(byte[] plainText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 32)) + throw new ArgumentException("Key must be 16 or 32 bytes", nameof(key)); + if (nonce == null || nonce.Length != 8) + throw new ArgumentException("Nonce must be 8 bytes", nameof(nonce)); + + byte[] cipherText = new byte[length]; + Process(plainText, offset, length, cipherText, 0, key, nonce, initialCounter); + return cipherText; + } + + /// + /// 使用 Salsa20 解密数据(加密和解密相同) + /// + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce) + { + return Decrypt(cipherText, 0, cipherText?.Length ?? 0, key, nonce, 0); + } + + /// + /// 使用 Salsa20 解密数据 + /// + public static byte[] Decrypt(byte[] cipherText, int offset, int length, byte[] key, byte[] nonce, uint initialCounter = 0) + { + return Encrypt(cipherText, offset, length, key, nonce, initialCounter); + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); + byte[] nonce = new byte[8]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(nonce); + + byte[] cipherBytes = Encrypt(plainBytes, key, nonce); + + byte[] result = new byte[8 + cipherBytes.Length]; + Array.Copy(nonce, result, 8); + Array.Copy(cipherBytes, 0, result, 8, cipherBytes.Length); + + return Convert.ToBase64String(result); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + if (data.Length < 8) + throw new ArgumentException("Invalid cipher text"); + + byte[] nonce = new byte[8]; + Array.Copy(data, nonce, 8); + + byte[] cipherBytes = new byte[data.Length - 8]; + Array.Copy(data, 8, cipherBytes, 0, cipherBytes.Length); + + byte[] plainBytes = Decrypt(cipherBytes, key, nonce); + return Encoding.UTF8.GetString(plainBytes); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 32) + throw new ArgumentException("Key length must be 16 or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static void Process(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, byte[] key, byte[] nonce, uint counter) + { + uint[] state = new uint[16]; + uint[] block = new uint[16]; + + // 初始化状态 + state[0] = Sigma[0]; + state[1] = (key.Length == 32) ? BitConverter.ToUInt32(key, 0) : Sigma[0]; + state[2] = (key.Length == 32) ? BitConverter.ToUInt32(key, 4) : Sigma[1]; + state[3] = (key.Length == 32) ? BitConverter.ToUInt32(key, 8) : Sigma[2]; + state[4] = (key.Length == 32) ? BitConverter.ToUInt32(key, 12) : Sigma[3]; + state[5] = (key.Length == 32) ? Sigma[1] : BitConverter.ToUInt32(key, 0); + state[6] = (key.Length == 32) ? BitConverter.ToUInt32(key, 16) : BitConverter.ToUInt32(key, 4); + state[7] = (key.Length == 32) ? BitConverter.ToUInt32(key, 20) : BitConverter.ToUInt32(key, 8); + state[8] = (key.Length == 32) ? BitConverter.ToUInt32(key, 24) : BitConverter.ToUInt32(key, 12); + state[9] = (key.Length == 32) ? BitConverter.ToUInt32(key, 28) : Sigma[0]; + state[10] = Sigma[2]; + state[11] = BitConverter.ToUInt32(nonce, 0); + state[12] = BitConverter.ToUInt32(nonce, 4); + state[13] = counter; + state[14] = Sigma[3]; + state[15] = (key.Length == 32) ? Sigma[3] : Sigma[1]; + + int processed = 0; + while (processed < inputLength) + { + Array.Copy(state, block, 16); + + // 20 轮 + for (int i = 0; i < 10; i++) + { + QuarterRound(ref block[0], ref block[4], ref block[8], ref block[12]); + QuarterRound(ref block[5], ref block[9], ref block[13], ref block[1]); + QuarterRound(ref block[10], ref block[14], ref block[2], ref block[6]); + QuarterRound(ref block[15], ref block[3], ref block[7], ref block[11]); + } + + for (int i = 0; i < 16; i++) + block[i] += state[i]; + + int blockSize = Math.Min(64, inputLength - processed); + for (int i = 0; i < blockSize; i++) + { + output[outputOffset + processed + i] = (byte)(input[inputOffset + processed + i] ^ (block[i / 4] >> ((i % 4) * 8))); + } + + processed += blockSize; + state[13]++; + } + } + + private static void QuarterRound(ref uint a, ref uint b, ref uint c, ref uint d) + { + b ^= RotateLeft(a + d, 7); + c ^= RotateLeft(b + a, 9); + d ^= RotateLeft(c + b, 13); + a ^= RotateLeft(d + c, 18); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/ScryptUtil.cs b/EasyTool.Core/CodeCategory/ScryptUtil.cs new file mode 100644 index 0000000..a63eabd --- /dev/null +++ b/EasyTool.Core/CodeCategory/ScryptUtil.cs @@ -0,0 +1,387 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Scrypt 密码哈希工具类 + /// Scrypt 是一种内存密集型的密钥派生函数,专门设计用于抵抗硬件攻击 + /// 常用于加密货币钱包和密码存储 + /// + public static class ScryptUtil + { + // 默认参数 + private const int DefaultN = 32768; // CPU/内存成本参数(必须为2的幂) + private const int DefaultR = 8; // 块大小参数 + private const int DefaultP = 1; // 并行化参数 + private const int DefaultDkLen = 32; // 派生密钥长度 + + /// + /// 使用 Scrypt 哈希密码 + /// + /// 密码 + /// 盐值(可选,默认自动生成) + /// CPU/内存成本参数(必须为2的幂,默认32768) + /// 块大小参数(默认8) + /// 并行化参数(默认1) + /// 派生密钥长度(默认32字节) + /// 哈希后的密码字符串 + public static string Hash(string password, byte[] salt = null, int n = DefaultN, int r = DefaultR, int p = DefaultP, int dkLen = DefaultDkLen) + { + if (string.IsNullOrEmpty(password)) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + ValidateParameters(n, r, p); + + salt ??= GenerateSalt(); + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + + byte[] hash = DeriveKey(passwordBytes, salt, n, r, p, dkLen); + + // 格式:$scrypt$N=,r=,p=

$$ + return $"$scrypt$N={n},r={r},p={p}${Convert.ToBase64String(salt)}${Convert.ToBase64String(hash)}"; + } + + ///

+ /// 验证密码 + /// + /// 密码 + /// 哈希字符串 + /// 是否匹配 + public static bool Verify(string password, string hash) + { + if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(hash)) + return false; + + try + { + var (n, r, p, salt, expectedHash) = ParseHash(hash); + if (salt == null || expectedHash == null) + return false; + + byte[] passwordBytes = Encoding.UTF8.GetBytes(password); + byte[] computedHash = DeriveKey(passwordBytes, salt, n, r, p, expectedHash.Length); + + return ConstantTimeEquals(computedHash, expectedHash); + } + catch + { + return false; + } + } + + /// + /// 使用 Scrypt 派生密钥 + /// + /// 密码 + /// 盐值 + /// CPU/内存成本参数 + /// 块大小参数 + /// 并行化参数 + /// 派生密钥长度 + /// 派生密钥 + public static byte[] DeriveKey(byte[] password, byte[] salt, int n = DefaultN, int r = DefaultR, int p = DefaultP, int dkLen = DefaultDkLen) + { + if (password == null || password.Length == 0) + throw new ArgumentException("Password cannot be null or empty", nameof(password)); + + if (salt == null || salt.Length == 0) + throw new ArgumentException("Salt cannot be null or empty", nameof(salt)); + + ValidateParameters(n, r, p); + + // 使用 PBKDF2-HMAC-SHA256 进行初始密钥派生 + byte[] b = PBKDF2(password, salt, 1, p * 128 * r); + + // 对每个块执行 ROMix + for (int i = 0; i < p; i++) + { + int offset = i * 128 * r; + ROMix(b, offset, n, r); + } + + // 再次使用 PBKDF2 派生最终密钥 + return PBKDF2(password, b, 1, dkLen); + } + + /// + /// 生成随机盐值 + /// + /// 盐值长度(默认16字节) + /// 盐值 + public static byte[] GenerateSalt(int length = 16) + { + byte[] salt = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return salt; + } + + /// + /// 检查是否需要重新哈希 + /// + /// 现有哈希 + /// 新的CPU/内存成本 + /// 新的块大小 + /// 新的并行化参数 + /// 是否需要重新哈希 + public static bool NeedsRehash(string hash, int n = DefaultN, int r = DefaultR, int p = DefaultP) + { + if (string.IsNullOrEmpty(hash)) + return true; + + try + { + var (oldN, oldR, oldP, _, _) = ParseHash(hash); + return oldN != n || oldR != r || oldP != p; + } + catch + { + return true; + } + } + + #region 私有方法 + + private static void ValidateParameters(int n, int r, int p) + { + if (n <= 1 || (n & (n - 1)) != 0) + throw new ArgumentException("N must be a power of 2 greater than 1", nameof(n)); + + if (r <= 0) + throw new ArgumentException("R must be greater than 0", nameof(r)); + + if (p <= 0) + throw new ArgumentException("P must be greater than 0", nameof(p)); + + // 检查内存使用限制 + long blockSize = 128 * r * p; + long totalMemory = blockSize * n; + + if (totalMemory > int.MaxValue) + throw new ArgumentException("Parameters would use too much memory"); + } + + private static (int n, int r, int p, byte[] salt, byte[] hash) ParseHash(string hash) + { + if (!hash.StartsWith("$scrypt$")) + return (0, 0, 0, null, null); + + string[] parts = hash.Split('$'); + if (parts.Length < 5) + return (0, 0, 0, null, null); + + // 解析参数 + string[] parameters = parts[2].Split(','); + int n = 0, r = 0, p = 0; + + foreach (string param in parameters) + { + string[] kv = param.Split('='); + if (kv.Length != 2) + continue; + + switch (kv[0]) + { + case "N": + n = int.Parse(kv[1]); + break; + case "r": + r = int.Parse(kv[1]); + break; + case "p": + p = int.Parse(kv[1]); + break; + } + } + + byte[] salt = Convert.FromBase64String(parts[3]); + byte[] expectedHash = Convert.FromBase64String(parts[4]); + + return (n, r, p, salt, expectedHash); + } + + private static byte[] PBKDF2(byte[] password, byte[] salt, int iterations, int dkLen) + { + using var hmac = new HMACSHA256(password); + byte[] result = new byte[dkLen]; + int hashLen = hmac.HashSize / 8; + int blocks = (dkLen + hashLen - 1) / hashLen; + + for (int block = 1; block <= blocks; block++) + { + byte[] blockBytes = BitConverter.GetBytes(block); + if (BitConverter.IsLittleEndian) + Array.Reverse(blockBytes); + + byte[] input = new byte[salt.Length + 4]; + Array.Copy(salt, input, salt.Length); + Array.Copy(blockBytes, 0, input, salt.Length, 4); + + byte[] u = hmac.ComputeHash(input); + byte[] output = new byte[u.Length]; + Array.Copy(u, output, u.Length); + + for (int i = 1; i < iterations; i++) + { + u = hmac.ComputeHash(u); + for (int j = 0; j < output.Length; j++) + { + output[j] ^= u[j]; + } + } + + int offset = (block - 1) * hashLen; + int length = Math.Min(hashLen, dkLen - offset); + Array.Copy(output, 0, result, offset, length); + } + + return result; + } + + private static void ROMix(byte[] b, int offset, int n, int r) + { + int blockSize = 128 * r; + uint[] v = new uint[n * blockSize / 4]; + uint[] x = new uint[blockSize / 4]; + + // 将字节转换为 uint 数组 + for (int i = 0; i < blockSize / 4; i++) + { + x[i] = BitConverter.ToUInt32(b, offset + i * 4); + } + + // 第一步:填充 V + for (int i = 0; i < n; i++) + { + Array.Copy(x, 0, v, i * blockSize / 4, blockSize / 4); + BlockMix(x, r); + } + + // 第二步:混合 + for (int i = 0; i < n; i++) + { + int j = (int)(Integerify(x) % (ulong)n); + for (int k = 0; k < blockSize / 4; k++) + { + x[k] ^= v[j * blockSize / 4 + k]; + } + BlockMix(x, r); + } + + // 将结果写回 + for (int i = 0; i < blockSize / 4; i++) + { + byte[] bytes = BitConverter.GetBytes(x[i]); + Array.Copy(bytes, 0, b, offset + i * 4, 4); + } + } + + private static void BlockMix(uint[] b, int r) + { + int blockSize = 128 * r; + uint[] x = new uint[64]; + uint[] y = new uint[blockSize]; + + // 复制最后一个块到 x + Array.Copy(b, b.Length - 64, x, 0, 64); + + // 混合每个块 + for (int i = 0; i < blockSize / 64; i++) + { + for (int j = 0; j < 64; j++) + { + x[j] ^= b[i * 64 + j]; + } + Salsa20_8(x); + + // 根据位置决定输出位置 + if (i % 2 == 0) + { + Array.Copy(x, 0, y, i / 2 * 64, 64); + } + else + { + Array.Copy(x, 0, y, (blockSize / 64 / 2 + i / 2) * 64, 64); + } + } + + Array.Copy(y, b, blockSize); + } + + private static void Salsa20_8(uint[] x) + { + uint[] z = new uint[16]; + Array.Copy(x, z, 16); + + for (int i = 0; i < 8; i += 2) + { + z[4] ^= RotateLeft(z[0] + z[12], 7); + z[8] ^= RotateLeft(z[4] + z[0], 9); + z[12] ^= RotateLeft(z[8] + z[4], 13); + z[0] ^= RotateLeft(z[12] + z[8], 18); + z[9] ^= RotateLeft(z[5] + z[1], 7); + z[13] ^= RotateLeft(z[9] + z[5], 9); + z[1] ^= RotateLeft(z[13] + z[9], 13); + z[5] ^= RotateLeft(z[1] + z[13], 18); + z[14] ^= RotateLeft(z[10] + z[6], 7); + z[2] ^= RotateLeft(z[14] + z[10], 9); + z[6] ^= RotateLeft(z[2] + z[14], 13); + z[10] ^= RotateLeft(z[6] + z[2], 18); + z[3] ^= RotateLeft(z[15] + z[11], 7); + z[7] ^= RotateLeft(z[3] + z[15], 9); + z[11] ^= RotateLeft(z[7] + z[3], 13); + z[15] ^= RotateLeft(z[11] + z[7], 18); + + z[1] ^= RotateLeft(z[0] + z[3], 7); + z[2] ^= RotateLeft(z[1] + z[0], 9); + z[3] ^= RotateLeft(z[2] + z[1], 13); + z[0] ^= RotateLeft(z[3] + z[2], 18); + z[6] ^= RotateLeft(z[5] + z[4], 7); + z[7] ^= RotateLeft(z[6] + z[5], 9); + z[4] ^= RotateLeft(z[7] + z[6], 13); + z[5] ^= RotateLeft(z[4] + z[7], 18); + z[11] ^= RotateLeft(z[10] + z[9], 7); + z[8] ^= RotateLeft(z[11] + z[10], 9); + z[9] ^= RotateLeft(z[8] + z[11], 13); + z[10] ^= RotateLeft(z[9] + z[8], 18); + z[12] ^= RotateLeft(z[15] + z[14], 7); + z[13] ^= RotateLeft(z[12] + z[15], 9); + z[14] ^= RotateLeft(z[13] + z[12], 13); + z[15] ^= RotateLeft(z[14] + z[13], 18); + } + + for (int i = 0; i < 16; i++) + { + x[i] += z[i]; + } + } + + private static ulong Integerify(uint[] b) + { + return ((ulong)b[19] << 32) | b[0]; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/SerpentUtil.cs b/EasyTool.Core/CodeCategory/SerpentUtil.cs new file mode 100644 index 0000000..4cf82cf --- /dev/null +++ b/EasyTool.Core/CodeCategory/SerpentUtil.cs @@ -0,0 +1,318 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Serpent 对称加密工具类 + /// Serpent 是 AES 的最终候选算法之一 + /// 128位分组密码,支持128/192/256位密钥 + /// 使用 32 轮加密,安全性极高 + /// + public static class SerpentUtil + { + private const int BlockSize = 16; + private const int Rounds = 32; + + // S-boxes (8个 4x4 S-box) + private static readonly byte[,] SBox = new byte[,] + { + { 3, 8, 15, 1, 10, 6, 5, 11, 14, 13, 4, 2, 7, 0, 9, 12 }, + { 15, 12, 2, 7, 9, 0, 5, 10, 1, 11, 14, 8, 6, 13, 3, 4 }, + { 8, 6, 7, 9, 3, 12, 10, 15, 13, 1, 14, 4, 0, 11, 5, 2 }, + { 0, 15, 11, 8, 12, 9, 6, 3, 13, 1, 2, 4, 10, 7, 5, 14 }, + { 1, 15, 8, 3, 12, 0, 11, 6, 2, 5, 4, 10, 9, 14, 7, 13 }, + { 15, 5, 2, 11, 4, 10, 9, 12, 0, 3, 14, 8, 13, 6, 7, 1 }, + { 7, 2, 12, 5, 8, 4, 6, 11, 14, 9, 1, 15, 13, 3, 10, 0 }, + { 1, 13, 15, 0, 14, 8, 2, 11, 7, 4, 12, 10, 9, 3, 5, 6 } + }; + + private static readonly byte[,] SBoxInv = new byte[,] + { + { 13, 3, 11, 0, 10, 6, 5, 12, 1, 14, 4, 7, 15, 9, 8, 2 }, + { 5, 8, 2, 14, 15, 6, 12, 3, 11, 4, 7, 9, 1, 13, 10, 0 }, + { 12, 9, 15, 4, 11, 14, 1, 2, 0, 3, 6, 13, 5, 8, 10, 7 }, + { 0, 9, 10, 7, 11, 14, 6, 13, 3, 5, 12, 2, 4, 8, 15, 1 }, + { 5, 0, 8, 3, 10, 9, 7, 14, 2, 12, 11, 6, 4, 15, 13, 1 }, + { 8, 15, 2, 9, 4, 1, 13, 14, 11, 6, 5, 3, 7, 12, 10, 0 }, + { 15, 10, 1, 13, 5, 3, 6, 0, 4, 9, 14, 7, 2, 12, 8, 11 }, + { 3, 0, 6, 13, 9, 14, 15, 8, 5, 12, 11, 7, 10, 1, 4, 2 } + }; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + uint[] subkeys = GenerateSubkeys(key); + + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + uint[] subkeys = GenerateSubkeys(key); + byte[] result = new byte[cipherText.Length]; + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static uint[] GenerateSubkeys(byte[] key) + { + uint[] subkeys = new uint[132]; // 33 * 4 = 132 + + // 扩展密钥到 256 位 + uint[] expandedKey = new uint[8]; + for (int i = 0; i < Math.Min(key.Length / 4, 8); i++) + { + expandedKey[i] = BitConverter.ToUInt32(key, i * 4); + } + + // 填充剩余部分 + if (key.Length < 32) + { + for (int i = key.Length / 4; i < 8; i++) + { + expandedKey[i] = 0; + } + } + + // 生成子密钥 + uint phi = 0x9E3779B9; + uint[] w = new uint[140]; + + for (int i = 0; i < 8; i++) + w[i] = expandedKey[i]; + + for (int i = 8; i < 140; i++) + { + uint x = w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ phi ^ (uint)i; + w[i] = RotateLeft(x, 11); + } + + // 应用 S-box + for (int i = 0; i < 33; i++) + { + int sboxIdx = (35 - i) % 8; + + for (int j = 0; j < 4; j++) + { + uint val = w[i * 4 + j + 8]; + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBox[sboxIdx, b0]; + b1 = SBox[sboxIdx, b1]; + b2 = SBox[sboxIdx, b2]; + b3 = SBox[sboxIdx, b3]; + + subkeys[i * 4 + j] = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + } + + return subkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint[] block = new uint[4]; + for (int i = 0; i < 4; i++) + block[i] = BitConverter.ToUInt32(input, inOffset + i * 4); + + // 32轮加密 + for (int i = 0; i < Rounds; i++) + { + // 密钥加 + for (int j = 0; j < 4; j++) + block[j] ^= subkeys[i * 4 + j]; + + // S-box 替换 + int sboxIdx = i % 8; + for (int j = 0; j < 4; j++) + { + block[j] = ApplySBox(block[j], sboxIdx); + } + + // 线性变换(最后一轮除外) + if (i < Rounds - 1) + { + block[0] = RotateLeft(block[0], 13); + block[2] = RotateLeft(block[2], 3); + block[1] = RotateLeft(block[1] ^ block[0] ^ block[2], 1); + block[3] = RotateLeft(block[3] ^ block[2] ^ (block[0] << 3), 7); + block[0] ^= block[1] ^ block[3]; + block[2] ^= block[3] ^ (block[1] << 7); + } + } + + // 最后一轮密钥加 + for (int i = 0; i < 4; i++) + block[i] ^= subkeys[Rounds * 4 + i]; + + for (int i = 0; i < 4; i++) + BitConverter.GetBytes(block[i]).CopyTo(output, outOffset + i * 4); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint[] block = new uint[4]; + for (int i = 0; i < 4; i++) + block[i] = BitConverter.ToUInt32(input, inOffset + i * 4); + + // 逆向密钥加 + for (int i = 0; i < 4; i++) + block[i] ^= subkeys[Rounds * 4 + i]; + + // 32轮解密 + for (int i = Rounds - 1; i >= 0; i--) + { + // 逆向 S-box + int sboxIdx = i % 8; + for (int j = 0; j < 4; j++) + { + block[j] = ApplySBoxInv(block[j], sboxIdx); + } + + // 逆向密钥加 + for (int j = 0; j < 4; j++) + block[j] ^= subkeys[i * 4 + j]; + + // 逆向线性变换(第一轮除外) + if (i > 0) + { + block[2] ^= block[3] ^ (block[1] << 7); + block[0] ^= block[1] ^ block[3]; + block[3] = RotateRight(block[3] ^ block[2] ^ (block[0] << 3), 7); + block[1] = RotateRight(block[1] ^ block[0] ^ block[2], 1); + block[2] = RotateRight(block[2], 3); + block[0] = RotateRight(block[0], 13); + } + } + + for (int i = 0; i < 4; i++) + BitConverter.GetBytes(block[i]).CopyTo(output, outOffset + i * 4); + } + + private static uint ApplySBox(uint val, int sboxIdx) + { + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBox[sboxIdx, b0]; + b1 = SBox[sboxIdx, b1]; + b2 = SBox[sboxIdx, b2]; + b3 = SBox[sboxIdx, b3]; + + return (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + + private static uint ApplySBoxInv(uint val, int sboxIdx) + { + byte b0 = (byte)(val & 0xFF); + byte b1 = (byte)((val >> 8) & 0xFF); + byte b2 = (byte)((val >> 16) & 0xFF); + byte b3 = (byte)((val >> 24) & 0xFF); + + b0 = SBoxInv[sboxIdx, b0]; + b1 = SBoxInv[sboxIdx, b1]; + b2 = SBoxInv[sboxIdx, b2]; + b3 = SBoxInv[sboxIdx, b3]; + + return (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/SignatureUtil.cs b/EasyTool.Core/CodeCategory/SignatureUtil.cs new file mode 100644 index 0000000..29a3a09 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SignatureUtil.cs @@ -0,0 +1,614 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 数字签名工具类 + /// 提供 RSA、ECDSA、DSA 等签名和验证功能 + /// + public static class SignatureUtil + { + #region RSA 签名 + + /// + /// 使用 RSA 创建签名 + /// + /// 要签名的数据 + /// RSA 私钥(PKCS#8 或 XML 格式) + /// 哈希算法 + /// 签名填充模式 + /// 签名 + public static byte[] SignWithRsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm, RSASignaturePadding? padding = null) + { + padding ??= RSASignaturePadding.Pkcs1; + + using var rsa = CreateRsaFromKey(privateKey); + return rsa.SignData(data, hashAlgorithm, padding); + } + + /// + /// 使用 RSA 创建签名(PSS 填充) + /// + public static byte[] SignWithRsaPss(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return SignWithRsa(data, privateKey, hashAlgorithm, RSASignaturePadding.Pss); + } + + /// + /// 使用 RSA 创建签名(PKCS#1 填充) + /// + public static byte[] SignWithRsaPkcs1(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return SignWithRsa(data, privateKey, hashAlgorithm, RSASignaturePadding.Pkcs1); + } + + /// + /// 验证 RSA 签名 + /// + /// 原始数据 + /// 签名 + /// RSA 公钥 + /// 哈希算法 + /// 签名填充模式 + /// 签名是否有效 + public static bool VerifyRsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm, RSASignaturePadding? padding = null) + { + padding ??= RSASignaturePadding.Pkcs1; + + try + { + using var rsa = CreateRsaFromKey(publicKey); + return rsa.VerifyData(data, signature, hashAlgorithm, padding); + } + catch + { + return false; + } + } + + /// + /// 验证 RSA-PSS 签名 + /// + public static bool VerifyRsaPssSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return VerifyRsaSignature(data, signature, publicKey, hashAlgorithm, RSASignaturePadding.Pss); + } + + /// + /// 验证 RSA-PKCS1 签名 + /// + public static bool VerifyRsaPkcs1Signature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + return VerifyRsaSignature(data, signature, publicKey, hashAlgorithm, RSASignaturePadding.Pkcs1); + } + + #endregion + + #region ECDSA 签名 + + /// + /// 使用 ECDSA 创建签名 + /// + /// 要签名的数据 + /// ECDSA 私钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithEcdsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var ecdsa = CreateEcdsaFromKey(privateKey); + return ecdsa.SignData(data, hashAlgorithm); + } + + /// + /// 使用 ECDSA 创建签名(DER 格式) + /// + public static byte[] SignWithEcdsaDer(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var ecdsa = CreateEcdsaFromKey(privateKey); + // 在 netstandard2.1 中使用默认签名格式 + return ecdsa.SignData(data, hashAlgorithm); + } + + /// + /// 验证 ECDSA 签名 + /// + /// 原始数据 + /// 签名 + /// ECDSA 公钥 + /// 哈希算法 + /// 签名是否有效 + public static bool VerifyEcdsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + try + { + using var ecdsa = CreateEcdsaFromKey(publicKey); + return ecdsa.VerifyData(data, signature, hashAlgorithm); + } + catch + { + return false; + } + } + + #endregion + + #region DSA 签名 + + /// + /// 使用 DSA 创建签名 + /// + /// 要签名的数据 + /// DSA 私钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithDsa(byte[] data, string privateKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var dsa = CreateDsaFromKey(privateKey); + return dsa.SignData(data, hashAlgorithm); + } + + /// + /// 验证 DSA 签名 + /// + public static bool VerifyDsaSignature(byte[] data, byte[] signature, string publicKey, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + try + { + using var dsa = CreateDsaFromKey(publicKey); + return dsa.VerifyData(data, signature, hashAlgorithm); + } + catch + { + return false; + } + } + + #endregion + + #region HMAC 签名 + + /// + /// 创建 HMAC 签名 + /// + /// 数据 + /// 密钥 + /// 哈希算法 + /// 签名 + public static byte[] SignWithHmac(byte[] data, byte[] key, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + using var hmac = CreateHmac(key, hashAlgorithm); + return hmac.ComputeHash(data); + } + + /// + /// 验证 HMAC 签名 + /// + public static bool VerifyHmacSignature(byte[] data, byte[] signature, byte[] key, HashAlgorithmName hashAlgorithm = default) + { + if (hashAlgorithm == default) + hashAlgorithm = HashAlgorithmName.SHA256; + + var computed = SignWithHmac(data, key, hashAlgorithm); + return HmacUtil.ConstantTimeEquals(computed, signature); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成 RSA 密钥对 + /// + /// 密钥大小(位) + /// 密钥对 + public static KeyPair GenerateRsaKeyPair(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + return new KeyPair + { + PrivateKey = ExportRsaPrivateKeyPem(rsa), + PublicKey = ExportRsaPublicKeyPem(rsa), + PrivateKeyPkcs8 = ExportPkcs8PrivateKeyPem(rsa) + }; + } + + /// + /// 生成 ECDSA 密钥对 + /// + /// 椭圆曲线 + /// 密钥对 + public static KeyPair GenerateEcdsaKeyPair(ECCurve? curve = null) + { + using var ecdsa = curve.HasValue + ? ECDsa.Create(curve.Value) + : ECDsa.Create(ECCurve.NamedCurves.nistP256); + + return new KeyPair + { + PrivateKey = ExportEcPrivateKeyPem(ecdsa), + PublicKey = ExportSubjectPublicKeyInfoPem(ecdsa) + }; + } + + #endregion + + #region 辅助方法 + + private static RSA CreateRsaFromKey(string key) + { + var rsa = RSA.Create(); + + if (key.StartsWith("-----BEGIN")) + { + // PEM 格式 + ImportFromPem(rsa, key); + } + else if (key.TrimStart().StartsWith(" + /// 签名字符串 + ///
+ /// 文本 + /// 私钥 + /// 签名算法 + /// Base64 编码的签名 + public static string SignString(string text, string privateKey, SignatureAlgorithm algorithm = SignatureAlgorithm.RsaSha256) + { + var data = Encoding.UTF8.GetBytes(text); + byte[] signature; + + switch (algorithm) + { + case SignatureAlgorithm.RsaSha256: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA256); + break; + case SignatureAlgorithm.RsaSha384: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA384); + break; + case SignatureAlgorithm.RsaSha512: + signature = SignWithRsaPkcs1(data, privateKey, HashAlgorithmName.SHA512); + break; + case SignatureAlgorithm.RsaPssSha256: + signature = SignWithRsaPss(data, privateKey, HashAlgorithmName.SHA256); + break; + case SignatureAlgorithm.EcdsaSha256: + signature = SignWithEcdsa(data, privateKey, HashAlgorithmName.SHA256); + break; + default: + throw new NotSupportedException($"不支持的签名算法: {algorithm}"); + } + + return Convert.ToBase64String(signature); + } + + /// + /// 验证字符串签名 + /// + public static bool VerifyStringSignature(string text, string signatureBase64, string publicKey, SignatureAlgorithm algorithm = SignatureAlgorithm.RsaSha256) + { + var data = Encoding.UTF8.GetBytes(text); + var signature = Convert.FromBase64String(signatureBase64); + + switch (algorithm) + { + case SignatureAlgorithm.RsaSha256: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA256); + case SignatureAlgorithm.RsaSha384: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA384); + case SignatureAlgorithm.RsaSha512: + return VerifyRsaPkcs1Signature(data, signature, publicKey, HashAlgorithmName.SHA512); + case SignatureAlgorithm.RsaPssSha256: + return VerifyRsaPssSignature(data, signature, publicKey, HashAlgorithmName.SHA256); + case SignatureAlgorithm.EcdsaSha256: + return VerifyEcdsaSignature(data, signature, publicKey, HashAlgorithmName.SHA256); + default: + throw new NotSupportedException($"不支持的签名算法: {algorithm}"); + } + } + + #endregion + } + + /// + /// 密钥对 + /// + public class KeyPair + { + /// + /// 私钥(PEM 格式) + /// + public string PrivateKey { get; set; } = string.Empty; + + /// + /// 公钥(PEM 格式) + /// + public string PublicKey { get; set; } = string.Empty; + + /// + /// 私钥(PKCS#8 格式) + /// + public string? PrivateKeyPkcs8 { get; set; } + } + + /// + /// 签名算法 + /// + public enum SignatureAlgorithm + { + /// + /// RSA + SHA256 (PKCS#1) + /// + RsaSha256, + + /// + /// RSA + SHA384 (PKCS#1) + /// + RsaSha384, + + /// + /// RSA + SHA512 (PKCS#1) + /// + RsaSha512, + + /// + /// RSA + SHA256 (PSS) + /// + RsaPssSha256, + + /// + /// RSA + SHA384 (PSS) + /// + RsaPssSha384, + + /// + /// RSA + SHA512 (PSS) + /// + RsaPssSha512, + + /// + /// ECDSA + SHA256 + /// + EcdsaSha256, + + /// + /// ECDSA + SHA384 + /// + EcdsaSha384, + + /// + /// ECDSA + SHA512 + /// + EcdsaSha512 + } +} \ No newline at end of file diff --git a/EasyTool.Core/CodeCategory/SipHashUtil.cs b/EasyTool.Core/CodeCategory/SipHashUtil.cs new file mode 100644 index 0000000..d88bbb9 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SipHashUtil.cs @@ -0,0 +1,340 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// SipHash 哈希工具类 + /// SipHash 是一种快速、安全的哈希算法,专为哈希表设计 + /// 由 Jean-Philippe Aumasson 和 Daniel J. Bernstein 开发 + /// 用于防止哈希碰撞攻击(HashDoS) + /// + public static class SipHashUtil + { + /// + /// 使用 SipHash-2-4 计算 64 位哈希值 + /// + /// 输入数据 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, 0, data.Length, key, 2, 4); + } + + /// + /// 使用 SipHash-2-4 计算 64 位哈希值(指定偏移和长度) + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64(byte[] data, int offset, int length, byte[] key) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, offset, length, key, 2, 4); + } + + /// + /// 使用 SipHash-4-8 计算 64 位哈希值(更安全,更慢) + /// + /// 输入数据 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeHash64Secure(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute(data, 0, data.Length, key, 4, 8); + } + + /// + /// 使用 SipHash 计算 128 位哈希值(SipHash-2-4) + /// + /// 输入数据 + /// 密钥(16字节) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute128(data, 0, data.Length, key, 2, 4); + } + + /// + /// 使用 SipHash 计算 128 位哈希值(SipHash-4-8,更安全) + /// + /// 输入数据 + /// 密钥(16字节) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) ComputeHash128Secure(byte[] data, byte[] key) + { + if (data == null) + data = Array.Empty(); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + + return Compute128(data, 0, data.Length, key, 4, 8); + } + + /// + /// 计算字符串的 SipHash-2-4 哈希值 + /// + /// 文本 + /// 密钥(16字节) + /// 64位哈希值 + public static ulong ComputeString64(string text, byte[] key) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash64(Array.Empty(), key); + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + return ComputeHash64(data, key); + } + + /// + /// 获取 SipHash-2-4 哈希值的十六进制表示 + /// + /// 输入数据 + /// 密钥(16字节) + /// 16字符的十六进制字符串 + public static string ComputeHex64(byte[] data, byte[] key) + { + ulong hash = ComputeHash64(data, key); + return hash.ToString("x16"); + } + + /// + /// 获取 SipHash-128 哈希值的十六进制表示 + /// + /// 输入数据 + /// 密钥(16字节) + /// 32字符的十六进制字符串 + public static string ComputeHex128(byte[] data, byte[] key) + { + var (low, high) = ComputeHash128(data, key); + return high.ToString("x16") + low.ToString("x16"); + } + + /// + /// 生成随机密钥 + /// + /// 16字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制字符串解析密钥 + /// + /// 32字符的十六进制字符串 + /// 16字节密钥 + public static byte[] ParseKeyHex(string hex) + { + if (string.IsNullOrEmpty(hex) || hex.Length != 32) + throw new ArgumentException("Hex key must be 32 characters", nameof(hex)); + + byte[] key = new byte[16]; + for (int i = 0; i < 16; i++) + { + key[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return key; + } + + #region 私有方法 + + private static ulong Compute(byte[] data, int offset, int length, byte[] key, int cRounds, int dRounds) + { + // 初始化 + ulong k0 = BitConverter.ToUInt64(key, 0); + ulong k1 = BitConverter.ToUInt64(key, 8); + + ulong v0 = k0 ^ 0x736f6d6570736575; + ulong v1 = k1 ^ 0x646f72616e646f6d; + ulong v2 = k0 ^ 0x6c7967656e657261; + ulong v3 = k1 ^ 0x7465646279746573; + + int end = offset + length; + int current = offset; + + // 处理完整的 8 字节块 + while (current + 8 <= end) + { + ulong m = BitConverter.ToUInt64(data, current); + v3 ^= m; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= m; + current += 8; + } + + // 处理最后一个块 + ulong lastBlock = (ulong)length << 56; + int remaining = end - current; + int shift = 0; + + for (int i = 0; i < remaining; i++) + { + lastBlock |= (ulong)data[current + i] << shift; + shift += 8; + } + + v3 ^= lastBlock; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= lastBlock; + + // 最终化 + v2 ^= 0xFF; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + return v0 ^ v1 ^ v2 ^ v3; + } + + private static (ulong Low, ulong High) Compute128(byte[] data, int offset, int length, byte[] key, int cRounds, int dRounds) + { + // 初始化 + ulong k0 = BitConverter.ToUInt64(key, 0); + ulong k1 = BitConverter.ToUInt64(key, 8); + + ulong v0 = k0 ^ 0x736f6d6570736575; + ulong v1 = k1 ^ 0x646f72616e646f6d; + ulong v2 = k0 ^ 0x6c7967656e657261; + ulong v3 = k1 ^ 0x7465646279746573; + + int end = offset + length; + int current = offset; + + // 处理完整的 8 字节块 + while (current + 8 <= end) + { + ulong m = BitConverter.ToUInt64(data, current); + v3 ^= m; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= m; + current += 8; + } + + // 处理最后一个块 + ulong lastBlock = (ulong)length << 56; + int remaining = end - current; + int shift = 0; + + for (int i = 0; i < remaining; i++) + { + lastBlock |= (ulong)data[current + i] << shift; + shift += 8; + } + + v3 ^= lastBlock; + + for (int i = 0; i < cRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + v0 ^= lastBlock; + + // 最终化(128位输出) + v2 ^= 0xEE; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + ulong low = v0 ^ v1 ^ v2 ^ v3; + + // 第二轮 + v1 ^= 0xDD; + + for (int i = 0; i < dRounds; i++) + { + SipRound(ref v0, ref v1, ref v2, ref v3); + } + + ulong high = v0 ^ v1 ^ v2 ^ v3; + + return (low, high); + } + + private static void SipRound(ref ulong v0, ref ulong v1, ref ulong v2, ref ulong v3) + { + v0 += v1; + v1 = RotateLeft(v1, 13); + v1 ^= v0; + v0 = RotateLeft(v0, 32); + + v2 += v3; + v3 = RotateLeft(v3, 16); + v3 ^= v2; + + v0 += v3; + v3 = RotateLeft(v3, 21); + v3 ^= v0; + + v2 += v1; + v1 = RotateLeft(v1, 17); + v1 ^= v2; + v2 = RotateLeft(v2, 32); + } + + private static ulong RotateLeft(ulong x, int n) + { + return (x << n) | (x >> (64 - n)); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Sm2Util.cs b/EasyTool.Core/CodeCategory/Sm2Util.cs new file mode 100644 index 0000000..3f63971 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm2Util.cs @@ -0,0 +1,657 @@ +using System; +using System.Numerics; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM2 椭圆曲线公钥密码工具类 + /// SM2 是中国国家密码管理局发布的椭圆曲线公钥密码算法 + /// 用于数字签名、密钥交换和公钥加密 + /// 基于 256 位椭圆曲线 + /// + public static class Sm2Util + { + // SM2 推荐椭圆曲线参数 + private static readonly BigInteger P = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger A = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger B = BigInteger.Parse("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger N = BigInteger.Parse("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger Gx = BigInteger.Parse("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", System.Globalization.NumberStyles.HexNumber); + private static readonly BigInteger Gy = BigInteger.Parse("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", System.Globalization.NumberStyles.HexNumber); + + // 基点 G + private static readonly ECPoint G = new ECPoint { X = Gx, Y = Gy }; + + // 用户 ID(默认值) + private const string DefaultUserId = "1234567812345678"; + + #region 密钥生成 + + /// + /// 生成 SM2 密钥对 + /// + /// 密钥对(私钥和公钥) + public static (byte[] PrivateKey, byte[] PublicKey) GenerateKeyPair() + { + byte[] privateKey = new byte[32]; + using var rng = RandomNumberGenerator.Create(); + + // 生成私钥(1 到 n-1 之间的随机数) + do + { + rng.GetBytes(privateKey); + var d = new BigInteger(privateKey, true, true); + if (d > 0 && d < N) + break; + } while (true); + + // 计算公钥 P = d * G + var publicKey = ScalarMultiply(privateKey, G); + + return (privateKey, EncodePoint(publicKey)); + } + + /// + /// 从私钥导出公钥 + /// + /// 私钥(32字节) + /// 公钥(65字节,未压缩格式) + public static byte[] DerivePublicKey(byte[] privateKey) + { + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + var publicKey = ScalarMultiply(privateKey, G); + return EncodePoint(publicKey); + } + + #endregion + + #region 加密解密 + + /// + /// 使用 SM2 公钥加密数据 + /// + /// 明文 + /// 公钥(65字节) + /// 密文(C1 || C3 || C2 格式) + public static byte[] Encrypt(byte[] plainText, byte[] publicKey) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (publicKey == null || publicKey.Length != 65) + throw new ArgumentException("Public key must be 65 bytes", nameof(publicKey)); + + var pubPoint = DecodePoint(publicKey); + + byte[] kBytes = new byte[32]; + ECPoint c1; + BigInteger k; + + using var rng = RandomNumberGenerator.Create(); + + // 生成随机数 k + do + { + rng.GetBytes(kBytes); + k = new BigInteger(kBytes, true, true); + if (k > 0 && k < N) + break; + } while (true); + + // C1 = k * G + c1 = ScalarMultiply(kBytes, G); + + // S = k * PB(检查 S 是否为无穷远点) + var s = ScalarMultiply(kBytes, pubPoint); + if (IsInfinity(s)) + throw new CryptographicException("Invalid public key"); + + // KDF 密钥派生 + byte[] kdfInput = new byte[64]; + Array.Copy(s.X.ToByteArray(true, true), 0, kdfInput, 0, 32); + Array.Copy(s.Y.ToByteArray(true, true), 0, kdfInput, 32, 32); + + byte[] kdfOutput = Kdf(kdfInput, plainText.Length); + + // C2 = M XOR KDF_output + byte[] c2 = new byte[plainText.Length]; + for (int i = 0; i < plainText.Length; i++) + { + c2[i] = (byte)(plainText[i] ^ kdfOutput[i]); + } + + // C3 = SM3(C1x || C1y || M) + byte[] c3Input = new byte[64 + plainText.Length]; + Array.Copy(c1.X.ToByteArray(true, true), 0, c3Input, 0, 32); + Array.Copy(c1.Y.ToByteArray(true, true), 0, c3Input, 32, 32); + Array.Copy(plainText, 0, c3Input, 64, plainText.Length); + + byte[] c3 = Sm3Hash(c3Input); + + // 组合结果:C1 || C3 || C2 + byte[] result = new byte[65 + 32 + plainText.Length]; + Array.Copy(EncodePoint(c1), 0, result, 0, 65); + Array.Copy(c3, 0, result, 65, 32); + Array.Copy(c2, 0, result, 97, c2.Length); + + return result; + } + + /// + /// 使用 SM2 私钥解密数据 + /// + /// 密文 + /// 私钥(32字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] privateKey) + { + if (cipherText == null || cipherText.Length < 97) + throw new ArgumentException("Invalid cipher text", nameof(cipherText)); + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + // 解析密文 + byte[] c1Bytes = new byte[65]; + byte[] c3 = new byte[32]; + int c2Length = cipherText.Length - 97; + byte[] c2 = new byte[c2Length]; + + Array.Copy(cipherText, 0, c1Bytes, 0, 65); + Array.Copy(cipherText, 65, c3, 0, 32); + Array.Copy(cipherText, 97, c2, 0, c2Length); + + var c1 = DecodePoint(c1Bytes); + + // S = dB * C1(检查 S 是否为无穷远点) + var s = ScalarMultiply(privateKey, c1); + if (IsInfinity(s)) + throw new CryptographicException("Invalid cipher text"); + + // KDF 密钥派生 + byte[] kdfInput = new byte[64]; + Array.Copy(s.X.ToByteArray(true, true), 0, kdfInput, 0, 32); + Array.Copy(s.Y.ToByteArray(true, true), 0, kdfInput, 32, 32); + + byte[] kdfOutput = Kdf(kdfInput, c2Length); + + // M = C2 XOR KDF_output + byte[] plainText = new byte[c2Length]; + for (int i = 0; i < c2Length; i++) + { + plainText[i] = (byte)(c2[i] ^ kdfOutput[i]); + } + + // 验证 C3 = SM3(C1x || C1y || M) + byte[] c3Input = new byte[64 + plainText.Length]; + Array.Copy(c1.X.ToByteArray(true, true), 0, c3Input, 0, 32); + Array.Copy(c1.Y.ToByteArray(true, true), 0, c3Input, 32, 32); + Array.Copy(plainText, 0, c3Input, 64, plainText.Length); + + byte[] expectedC3 = Sm3Hash(c3Input); + + if (!ConstantTimeEquals(c3, expectedC3)) + throw new CryptographicException("Invalid cipher text: checksum mismatch"); + + return plainText; + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 公钥 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] publicKey) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, publicKey); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 私钥 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] privateKey) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, privateKey); + return Encoding.UTF8.GetString(decrypted); + } + + #endregion + + #region 签名验签 + + /// + /// 使用 SM2 私钥签名数据 + /// + /// 要签名的数据 + /// 私钥(32字节) + /// 用户 ID(可选) + /// 签名(64字节,R || S) + public static byte[] Sign(byte[] data, byte[] privateKey, string userId = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (privateKey == null || privateKey.Length != 32) + throw new ArgumentException("Private key must be 32 bytes", nameof(privateKey)); + + userId ??= DefaultUserId; + + // 计算 Z 值 + var publicKey = ScalarMultiply(privateKey, G); + byte[] z = CalculateZ(publicKey, userId); + + // e = SM3(Z || M) + byte[] eInput = new byte[z.Length + data.Length]; + Array.Copy(z, eInput, z.Length); + Array.Copy(data, 0, eInput, z.Length, data.Length); + byte[] eHash = Sm3Hash(eInput); + BigInteger e = new BigInteger(eHash, true, true); + + byte[] kBytes = new byte[32]; + BigInteger r, s; + var d = new BigInteger(privateKey, true, true); + + using var rng = RandomNumberGenerator.Create(); + + do + { + // 生成随机数 k + do + { + rng.GetBytes(kBytes); + var k = new BigInteger(kBytes, true, true); + if (k > 0 && k < N) + break; + } while (true); + + // 计算 x1, y1 = k * G + var point = ScalarMultiply(kBytes, G); + + // r = (e + x1) mod n + r = (e + point.X) % N; + if (r == 0 || r + new BigInteger(kBytes, true, true) == N) + continue; + + // s = ((1 + d)^-1 * (k - r * d)) mod n + var dPlusOne = (d + 1) % N; + var dPlusOneInv = ModInverse(dPlusOne, N); + s = (dPlusOneInv * ((new BigInteger(kBytes, true, true) - r * d) % N + N)) % N; + + if (s != 0) + break; + + } while (true); + + // 组合签名 R || S + byte[] result = new byte[64]; + Array.Copy(r.ToByteArray(true, true), 0, result, 0, Math.Min(32, r.ToByteArray(true, true).Length)); + Array.Copy(s.ToByteArray(true, true), 0, result, 32, Math.Min(32, s.ToByteArray(true, true).Length)); + + return result; + } + + /// + /// 使用 SM2 公钥验证签名 + /// + /// 原始数据 + /// 签名(64字节) + /// 公钥(65字节) + /// 用户 ID(可选) + /// 签名是否有效 + public static bool Verify(byte[] data, byte[] signature, byte[] publicKey, string userId = null) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (signature == null || signature.Length != 64) + throw new ArgumentException("Signature must be 64 bytes", nameof(signature)); + if (publicKey == null || publicKey.Length != 65) + throw new ArgumentException("Public key must be 65 bytes", nameof(publicKey)); + + userId ??= DefaultUserId; + + // 解析签名 + byte[] rBytes = new byte[32]; + byte[] sBytes = new byte[32]; + Array.Copy(signature, 0, rBytes, 0, 32); + Array.Copy(signature, 32, sBytes, 0, 32); + + BigInteger r = new BigInteger(rBytes, true, true); + BigInteger s = new BigInteger(sBytes, true, true); + + // 验证 r, s 范围 + if (r < 1 || r >= N || s < 1 || s >= N) + return false; + + // 计算 Z 值 + var pubPoint = DecodePoint(publicKey); + byte[] z = CalculateZ(pubPoint, userId); + + // e = SM3(Z || M) + byte[] eInput = new byte[z.Length + data.Length]; + Array.Copy(z, eInput, z.Length); + Array.Copy(data, 0, eInput, z.Length, data.Length); + byte[] eHash = Sm3Hash(eInput); + BigInteger e = new BigInteger(eHash, true, true); + + // t = (r + s) mod n + BigInteger t = (r + s) % N; + if (t == 0) + return false; + + // 计算 (x1, y1) = s * G + t * PA + var sG = ScalarMultiply(sBytes, G); + + // 需要将 t 转换为字节数组 + byte[] tBytes = t.ToByteArray(true, true); + if (tBytes.Length > 32) + return false; + byte[] tBytesPadded = new byte[32]; + Array.Copy(tBytes, tBytesPadded, tBytes.Length); + + var tPA = ScalarMultiply(tBytesPadded, pubPoint); + var point = PointAdd(sG, tPA); + + // 验证 R = (e + x1) mod n == r + BigInteger R = (e + point.X) % N; + + return R == r; + } + + /// + /// 对字符串签名并返回 Base64 + /// + /// 要签名的文本 + /// 私钥 + /// 用户 ID + /// Base64 签名 + public static string SignToBase64(string text, byte[] privateKey, string userId = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] signature = Sign(data, privateKey, userId); + return Convert.ToBase64String(signature); + } + + /// + /// 验证 Base64 签名 + /// + /// 原始文本 + /// Base64 签名 + /// 公钥 + /// 用户 ID + /// 签名是否有效 + public static bool VerifyFromBase64(string text, string signatureBase64, byte[] publicKey, string userId = null) + { + if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(signatureBase64)) + return false; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] signature = Convert.FromBase64String(signatureBase64); + return Verify(data, signature, publicKey, userId); + } + + #endregion + + #region 私有方法 + + private static ECPoint ScalarMultiply(byte[] k, ECPoint point) + { + var scalar = new BigInteger(k, true, true); + var result = PointMultiply(scalar, point); + return result; + } + + private static ECPoint PointMultiply(BigInteger k, ECPoint point) + { + if (k == 0 || IsInfinity(point)) + return InfinityPoint(); + + ECPoint result = InfinityPoint(); + ECPoint temp = point; + var absK = BigInteger.Abs(k); + + while (absK > 0) + { + if ((absK & 1) == 1) + { + result = PointAdd(result, temp); + } + temp = PointDouble(temp); + absK >>= 1; + } + + return result; + } + + private static ECPoint PointAdd(ECPoint p1, ECPoint p2) + { + if (IsInfinity(p1)) return p2; + if (IsInfinity(p2)) return p1; + + if (p1.X == p2.X) + { + if ((p1.Y + p2.Y) % P == 0) + return InfinityPoint(); + + return PointDouble(p1); + } + + // λ = (y2 - y1) / (x2 - x1) + var dx = (p2.X - p1.X + P) % P; + var dy = (p2.Y - p1.Y + P) % P; + var lambda = (dy * ModInverse(dx, P)) % P; + + // x3 = λ² - x1 - x2 + var x3 = (lambda * lambda - p1.X - p2.X + 2 * P) % P; + + // y3 = λ(x1 - x3) - y1 + var y3 = (lambda * (p1.X - x3 + P) - p1.Y + P) % P; + + return new ECPoint { X = x3, Y = y3 }; + } + + private static ECPoint PointDouble(ECPoint p) + { + if (IsInfinity(p)) + return InfinityPoint(); + + // λ = (3x² + a) / (2y) + var x2 = (p.X * p.X) % P; + var numerator = (3 * x2 + A) % P; + var denominator = (2 * p.Y) % P; + var lambda = (numerator * ModInverse(denominator, P)) % P; + + // x3 = λ² - 2x + var x3 = (lambda * lambda - 2 * p.X + P) % P; + + // y3 = λ(x - x3) - y + var y3 = (lambda * (p.X - x3 + P) - p.Y + P) % P; + + return new ECPoint { X = x3, Y = y3 }; + } + + private static ECPoint InfinityPoint() + { + return new ECPoint { X = BigInteger.Zero, Y = BigInteger.Zero }; + } + + private static bool IsInfinity(ECPoint p) + { + return p.X == BigInteger.Zero && p.Y == BigInteger.Zero; + } + + private static BigInteger ModInverse(BigInteger a, BigInteger n) + { + if (a < 0) a = (a % n + n) % n; + + BigInteger t = 0, newT = 1; + BigInteger r = n, newR = a; + + while (newR != 0) + { + var quotient = r / newR; + var tempT = t; + t = newT; + newT = tempT - quotient * newT; + + var tempR = r; + r = newR; + newR = tempR - quotient * newR; + } + + if (t < 0) t = (t % n + n) % n; + + return t; + } + + private static byte[] EncodePoint(ECPoint point) + { + byte[] result = new byte[65]; + result[0] = 0x04; // 未压缩格式 + + var xBytes = point.X.ToByteArray(true, true); + var yBytes = point.Y.ToByteArray(true, true); + + Array.Copy(xBytes, 0, result, 1 + (32 - xBytes.Length), xBytes.Length); + Array.Copy(yBytes, 0, result, 33 + (32 - yBytes.Length), yBytes.Length); + + return result; + } + + private static ECPoint DecodePoint(byte[] data) + { + if (data == null || data.Length != 65 || data[0] != 0x04) + throw new ArgumentException("Invalid point encoding", nameof(data)); + + byte[] xBytes = new byte[32]; + byte[] yBytes = new byte[32]; + Array.Copy(data, 1, xBytes, 0, 32); + Array.Copy(data, 33, yBytes, 0, 32); + + return new ECPoint + { + X = new BigInteger(xBytes, true, true), + Y = new BigInteger(yBytes, true, true) + }; + } + + private static byte[] CalculateZ(ECPoint publicKey, string userId) + { + byte[] idBytes = Encoding.UTF8.GetBytes(userId); + int idBits = idBytes.Length * 8; + + byte[] entl = new byte[2]; + entl[0] = (byte)((idBits >> 8) & 0xFF); + entl[1] = (byte)(idBits & 0xFF); + + // Z = SM3(ENTLA || IDA || a || b || Gx || Gy || Ax || Ay) + byte[] aBytes = A.ToByteArray(true, true); + byte[] bBytes = B.ToByteArray(true, true); + byte[] gxBytes = Gx.ToByteArray(true, true); + byte[] gyBytes = Gy.ToByteArray(true, true); + byte[] axBytes = publicKey.X.ToByteArray(true, true); + byte[] ayBytes = publicKey.Y.ToByteArray(true, true); + + byte[] input = new byte[2 + idBytes.Length + 32 * 6]; + int offset = 0; + + Array.Copy(entl, 0, input, offset, 2); + offset += 2; + Array.Copy(idBytes, 0, input, offset, idBytes.Length); + offset += idBytes.Length; + + CopyPadded(aBytes, input, ref offset, 32); + CopyPadded(bBytes, input, ref offset, 32); + CopyPadded(gxBytes, input, ref offset, 32); + CopyPadded(gyBytes, input, ref offset, 32); + CopyPadded(axBytes, input, ref offset, 32); + CopyPadded(ayBytes, input, ref offset, 32); + + return Sm3Hash(input); + } + + private static void CopyPadded(byte[] src, byte[] dest, ref int offset, int length) + { + int padLength = length - src.Length; + if (padLength > 0) + { + offset += padLength; + } + Array.Copy(src, 0, dest, offset, Math.Min(src.Length, length)); + offset += Math.Min(src.Length, length); + } + + private static byte[] Kdf(byte[] z, int keyLength) + { + byte[] result = new byte[keyLength]; + int counter = 1; + int generated = 0; + + while (generated < keyLength) + { + byte[] counterBytes = BitConverter.GetBytes(counter); + if (BitConverter.IsLittleEndian) + Array.Reverse(counterBytes); + + byte[] input = new byte[z.Length + 4]; + Array.Copy(z, input, z.Length); + Array.Copy(counterBytes, 0, input, z.Length, 4); + + byte[] hash = Sm3Hash(input); + + int copyLength = Math.Min(32, keyLength - generated); + Array.Copy(hash, 0, result, generated, copyLength); + generated += copyLength; + counter++; + } + + return result; + } + + private static byte[] Sm3Hash(byte[] data) + { + return Sm3Util.ComputeHash(data); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + + /// + /// 椭圆曲线点 + /// + private struct ECPoint + { + public BigInteger X; + public BigInteger Y; + } + } +} diff --git a/EasyTool.Core/CodeCategory/Sm3Util.cs b/EasyTool.Core/CodeCategory/Sm3Util.cs new file mode 100644 index 0000000..e68720e --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm3Util.cs @@ -0,0 +1,358 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM3 密码哈希算法工具类 + /// SM3 是中国国家密码管理局发布的密码哈希函数标准 + /// 输出256位(32字节)哈希值,安全性类似于 SHA-256 + /// + public static class Sm3Util + { + // SM3 初始向量 + private static readonly uint[] IV = new uint[] + { + 0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, + 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e + }; + + /// + /// 计算数据的 SM3 哈希值 + /// + /// 输入数据 + /// 32字节的哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算数据的 SM3 哈希值 + /// + /// 输入数据 + /// 起始位置 + /// 长度 + /// 32字节的哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset >= data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + // 填充消息 + byte[] padded = PadMessage(data, offset, length); + + // 初始化哈希值 + uint[] v = new uint[8]; + Array.Copy(IV, v, 8); + + // 处理每个512位块 + for (int i = 0; i < padded.Length; i += 64) + { + ProcessBlock(padded, i, v); + } + + // 转换为字节数组 + byte[] result = new byte[32]; + for (int i = 0; i < 8; i++) + { + result[i * 4] = (byte)(v[i] >> 24); + result[i * 4 + 1] = (byte)(v[i] >> 16); + result[i * 4 + 2] = (byte)(v[i] >> 8); + result[i * 4 + 3] = (byte)v[i]; + } + + return result; + } + + /// + /// 计算字符串的 SM3 哈希值 + /// + /// 输入字符串 + /// 编码方式(默认UTF-8) + /// 32字节的哈希值 + public static byte[] ComputeHash(string text, Encoding encoding = null) + { + if (text == null) + throw new ArgumentNullException(nameof(text)); + + encoding ??= Encoding.UTF8; + return ComputeHash(encoding.GetBytes(text)); + } + + /// + /// 计算数据的 SM3 哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 64字符的十六进制字符串 + public static string ComputeHashHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BytesToHex(hash); + } + + /// + /// 计算字符串的 SM3 哈希值并返回十六进制字符串 + /// + /// 输入字符串 + /// 编码方式(默认UTF-8) + /// 64字符的十六进制字符串 + public static string ComputeHashHex(string text, Encoding encoding = null) + { + byte[] hash = ComputeHash(text, encoding); + return BytesToHex(hash); + } + + /// + /// 验证数据哈希值 + /// + /// 原始数据 + /// 预期的哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] expectedHash) + { + if (expectedHash == null || expectedHash.Length != 32) + return false; + + byte[] computed = ComputeHash(data); + return ConstantTimeEquals(computed, expectedHash); + } + + /// + /// 验证数据哈希值(十六进制格式) + /// + /// 原始数据 + /// 预期的哈希值(十六进制) + /// 是否匹配 + public static bool VerifyHex(byte[] data, string expectedHashHex) + { + if (string.IsNullOrEmpty(expectedHashHex) || expectedHashHex.Length != 64) + return false; + + string computed = ComputeHashHex(data); + return string.Equals(computed, expectedHashHex, StringComparison.OrdinalIgnoreCase); + } + + #region 私有方法 + + private static byte[] PadMessage(byte[] data, int offset, int length) + { + // 计算填充后的长度 + long bitLength = (long)length * 8; + int paddedLength = length + 1 + 8; + + // 使长度为64的倍数 + while (paddedLength % 64 != 0) + { + paddedLength++; + } + + byte[] padded = new byte[paddedLength]; + Array.Copy(data, offset, padded, 0, length); + + // 添加1位和7个0位(0x80) + padded[length] = 0x80; + + // 添加长度(大端序,64位) + padded[paddedLength - 8] = (byte)(bitLength >> 56); + padded[paddedLength - 7] = (byte)(bitLength >> 48); + padded[paddedLength - 6] = (byte)(bitLength >> 40); + padded[paddedLength - 5] = (byte)(bitLength >> 32); + padded[paddedLength - 4] = (byte)(bitLength >> 24); + padded[paddedLength - 3] = (byte)(bitLength >> 16); + padded[paddedLength - 2] = (byte)(bitLength >> 8); + padded[paddedLength - 1] = (byte)bitLength; + + return padded; + } + + private static void ProcessBlock(byte[] block, int offset, uint[] v) + { + uint[] w = new uint[68]; + uint[] w1 = new uint[64]; + + // 准备消息扩展 + for (int i = 0; i < 16; i++) + { + w[i] = ((uint)block[offset + i * 4] << 24) | + ((uint)block[offset + i * 4 + 1] << 16) | + ((uint)block[offset + i * 4 + 2] << 8) | + block[offset + i * 4 + 3]; + } + + for (int i = 16; i < 68; i++) + { + w[i] = P1(w[i - 16] ^ w[i - 9] ^ RotateLeft(w[i - 3], 15)) ^ + RotateLeft(w[i - 13], 7) ^ w[i - 6]; + if (w[i] < 0) w[i] = (uint)(int)w[i]; + } + + for (int i = 0; i < 64; i++) + { + w1[i] = w[i] ^ w[i + 4]; + } + + // 压缩函数 + uint a = v[0], b = v[1], c = v[2], d = v[3]; + uint e = v[4], f = v[5], g = v[6], h = v[7]; + + for (int i = 0; i < 64; i++) + { + uint ss1 = RotateLeft(RotateLeft(a, 12) + e + RotateLeft(T(i), i % 32), 7); + uint ss2 = ss1 ^ RotateLeft(a, 12); + uint tt1 = FF(a, b, c, i) + d + ss2 + w1[i]; + uint tt2 = GG(e, f, g, i) + h + ss1 + w[i]; + + d = c; + c = RotateLeft(b, 9); + b = a; + a = tt1; + h = g; + g = RotateLeft(f, 19); + f = e; + e = P0(tt2); + } + + v[0] ^= a; + v[1] ^= b; + v[2] ^= c; + v[3] ^= d; + v[4] ^= e; + v[5] ^= f; + v[6] ^= g; + v[7] ^= h; + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static uint T(int j) + { + return j < 16 ? 0x79cc4519u : 0x7a879d8au; + } + + private static uint FF(uint x, uint y, uint z, int j) + { + if (j < 16) + { + return x ^ y ^ z; + } + return (x & y) | (x & z) | (y & z); + } + + private static uint GG(uint x, uint y, uint z, int j) + { + if (j < 16) + { + return x ^ y ^ z; + } + return (x & y) | (~x & z); + } + + private static uint P0(uint x) + { + return x ^ RotateLeft(x, 9) ^ RotateLeft(x, 17); + } + + private static uint P1(uint x) + { + return x ^ RotateLeft(x, 15) ^ RotateLeft(x, 23); + } + + private static string BytesToHex(byte[] bytes) + { + var sb = new StringBuilder(bytes.Length * 2); + foreach (byte b in bytes) + { + sb.Append(b.ToString("x2")); + } + return sb.ToString(); + } + + private static bool ConstantTimeEquals(byte[] a, byte[] b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + + #endregion + + #region HMAC-SM3 + + /// + /// 计算 HMAC-SM3 + /// + /// 密钥 + /// 数据 + /// 32字节的HMAC值 + public static byte[] Hmac(byte[] key, byte[] data) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (data == null) + throw new ArgumentNullException(nameof(data)); + + // 如果密钥太长,先哈希 + if (key.Length > 64) + { + key = ComputeHash(key); + } + + // 填充密钥到64字节 + byte[] paddedKey = new byte[64]; + Array.Copy(key, paddedKey, key.Length); + + // 计算内部和外部填充 + byte[] innerPad = new byte[64]; + byte[] outerPad = new byte[64]; + + for (int i = 0; i < 64; i++) + { + innerPad[i] = (byte)(paddedKey[i] ^ 0x36); + outerPad[i] = (byte)(paddedKey[i] ^ 0x5c); + } + + // 计算 HMAC + byte[] innerData = new byte[64 + data.Length]; + Array.Copy(innerPad, innerData, 64); + Array.Copy(data, 0, innerData, 64, data.Length); + byte[] innerHash = ComputeHash(innerData); + + byte[] outerData = new byte[64 + 32]; + Array.Copy(outerPad, outerData, 64); + Array.Copy(innerHash, 0, outerData, 64, 32); + + return ComputeHash(outerData); + } + + /// + /// 计算 HMAC-SM3 并返回十六进制字符串 + /// + /// 密钥 + /// 数据 + /// 64字符的十六进制字符串 + public static string HmacHex(byte[] key, byte[] data) + { + byte[] hmac = Hmac(key, data); + return BytesToHex(hmac); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/Sm4Util.cs b/EasyTool.Core/CodeCategory/Sm4Util.cs new file mode 100644 index 0000000..459bd66 --- /dev/null +++ b/EasyTool.Core/CodeCategory/Sm4Util.cs @@ -0,0 +1,449 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// SM4 对称加密算法工具类 + /// SM4 是中国国家密码管理局发布的分组密码标准 + /// 分组长度128位,密钥长度128位 + /// + public static class Sm4Util + { + // SM4 S盒 + private static readonly byte[] SBOX = new byte[] + { + 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, + 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, + 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, + 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, + 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, + 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, + 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, + 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, + 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, + 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, + 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, + 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, + 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, + 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, + 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48 + }; + + // 系统参数 FK + private static readonly uint[] FK = new uint[] { 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc }; + + // 固定参数 CK + private static readonly uint[] CK = new uint[] + { + 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, + 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, + 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, + 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, + 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, + 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, + 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, + 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279 + }; + + private const int BLOCK_SIZE = 16; // 128位 + + /// + /// SM4 加密(ECB模式) + /// + /// 明文 + /// 密钥(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + return Encrypt(plainText, key, Sm4Mode.ECB, null); + } + + /// + /// SM4 加密 + /// + /// 明文 + /// 密钥(16字节) + /// 加密模式 + /// 初始向量(CBC模式需要,16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, Sm4Mode mode, byte[] iv) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (mode == Sm4Mode.CBC && (iv == null || iv.Length != 16)) + throw new ArgumentException("IV must be 16 bytes for CBC mode", nameof(iv)); + + // 生成轮密钥 + uint[] roundKeys = GenerateRoundKeys(key); + + // PKCS7 填充 + byte[] padded = Pkcs7Pad(plainText); + + byte[] result = new byte[padded.Length]; + byte[] temp = new byte[BLOCK_SIZE]; + + if (mode == Sm4Mode.CBC) + { + Array.Copy(iv, temp, BLOCK_SIZE); + } + + for (int i = 0; i < padded.Length; i += BLOCK_SIZE) + { + if (mode == Sm4Mode.CBC) + { + // CBC模式:明文先与IV异或 + for (int j = 0; j < BLOCK_SIZE; j++) + { + temp[j] = (byte)(padded[i + j] ^ temp[j]); + } + EncryptBlock(temp, roundKeys); + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + else + { + Array.Copy(padded, i, temp, 0, BLOCK_SIZE); + EncryptBlock(temp, roundKeys); + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + } + + return result; + } + + /// + /// SM4 加密字符串并返回 Base64 + /// + /// 明文字符串 + /// 密钥(16字节) + /// 编码方式(默认UTF-8) + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key, Encoding encoding = null) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + + encoding ??= Encoding.UTF8; + byte[] encrypted = Encrypt(encoding.GetBytes(plainText), key); + return Convert.ToBase64String(encrypted); + } + + /// + /// SM4 加密字符串并返回 Base64 + /// + /// 明文字符串 + /// 密钥字符串 + /// 编码方式(默认UTF-8) + /// Base64 密文 + public static string EncryptToBase64(string plainText, string keyString, Encoding encoding = null) + { + encoding ??= Encoding.UTF8; + byte[] key = GetKeyFromString(keyString, encoding); + return EncryptToBase64(plainText, key, encoding); + } + + /// + /// SM4 解密(ECB模式) + /// + /// 密文 + /// 密钥(16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + return Decrypt(cipherText, key, Sm4Mode.ECB, null); + } + + /// + /// SM4 解密 + /// + /// 密文 + /// 密钥(16字节) + /// 加密模式 + /// 初始向量(CBC模式需要,16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, Sm4Mode mode, byte[] iv) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (cipherText.Length == 0 || cipherText.Length % BLOCK_SIZE != 0) + throw new ArgumentException("Cipher text length must be a multiple of 16", nameof(cipherText)); + if (mode == Sm4Mode.CBC && (iv == null || iv.Length != 16)) + throw new ArgumentException("IV must be 16 bytes for CBC mode", nameof(iv)); + + // 生成轮密钥(解密时使用逆序) + uint[] roundKeys = GenerateRoundKeys(key); + Array.Reverse(roundKeys); + + byte[] result = new byte[cipherText.Length]; + byte[] temp = new byte[BLOCK_SIZE]; + byte[] prevBlock = mode == Sm4Mode.CBC ? iv : null; + + for (int i = 0; i < cipherText.Length; i += BLOCK_SIZE) + { + Array.Copy(cipherText, i, temp, 0, BLOCK_SIZE); + EncryptBlock(temp, roundKeys); // 使用逆序的轮密钥 + + if (mode == Sm4Mode.CBC && prevBlock != null) + { + // CBC模式:解密后与前一个密文块异或 + for (int j = 0; j < BLOCK_SIZE; j++) + { + result[i + j] = (byte)(temp[j] ^ prevBlock[j]); + } + prevBlock = new byte[BLOCK_SIZE]; + Array.Copy(cipherText, i, prevBlock, 0, BLOCK_SIZE); + } + else + { + Array.Copy(temp, 0, result, i, BLOCK_SIZE); + } + } + + // 移除 PKCS7 填充 + return Pkcs7Unpad(result); + } + + /// + /// SM4 解密 Base64 字符串 + /// + /// Base64 密文 + /// 密钥(16字节) + /// 编码方式(默认UTF-8) + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, Encoding encoding = null) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + + encoding ??= Encoding.UTF8; + byte[] cipher = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(cipher, key); + return encoding.GetString(decrypted); + } + + /// + /// SM4 解密 Base64 字符串 + /// + /// Base64 密文 + /// 密钥字符串 + /// 编码方式(默认UTF-8) + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, string keyString, Encoding encoding = null) + { + encoding ??= Encoding.UTF8; + byte[] key = GetKeyFromString(keyString, encoding); + return DecryptFromBase64(cipherText, key, encoding); + } + + /// + /// 生成随机密钥 + /// + /// 16字节随机密钥 + public static byte[] GenerateKey() + { + var key = new byte[16]; + new Random().NextBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制字符串 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + #region 私有方法 + + private static uint[] GenerateRoundKeys(byte[] key) + { + uint[] roundKeys = new uint[32]; + uint[] mk = new uint[4]; + + // 将密钥转换为4个32位字 + for (int i = 0; i < 4; i++) + { + mk[i] = ((uint)key[i * 4] << 24) | + ((uint)key[i * 4 + 1] << 16) | + ((uint)key[i * 4 + 2] << 8) | + key[i * 4 + 3]; + } + + // 初始化轮密钥 + uint[] k = new uint[36]; + for (int i = 0; i < 4; i++) + { + k[i] = mk[i] ^ FK[i]; + } + + // 生成32个轮密钥 + for (int i = 0; i < 32; i++) + { + k[i + 4] = k[i] ^ TPrime(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]); + roundKeys[i] = k[i + 4]; + } + + return roundKeys; + } + + private static void EncryptBlock(byte[] block, uint[] roundKeys) + { + uint[] x = new uint[4]; + + // 将16字节转换为4个32位字 + for (int i = 0; i < 4; i++) + { + x[i] = ((uint)block[i * 4] << 24) | + ((uint)block[i * 4 + 1] << 16) | + ((uint)block[i * 4 + 2] << 8) | + block[i * 4 + 3]; + } + + // 32轮加密 + for (int i = 0; i < 32; i++) + { + uint temp = x[0]; + x[0] = x[1]; + x[1] = x[2]; + x[2] = x[3]; + x[3] = temp ^ T(x[1] ^ x[2] ^ x[3] ^ roundKeys[i]); + } + + // 反序并输出 + for (int i = 0; i < 4; i++) + { + block[i * 4] = (byte)(x[3 - i] >> 24); + block[i * 4 + 1] = (byte)(x[3 - i] >> 16); + block[i * 4 + 2] = (byte)(x[3 - i] >> 8); + block[i * 4 + 3] = (byte)x[3 - i]; + } + } + + private static uint T(uint x) + { + byte[] bytes = new byte[4]; + bytes[0] = (byte)(x >> 24); + bytes[1] = (byte)(x >> 16); + bytes[2] = (byte)(x >> 8); + bytes[3] = (byte)x; + + // S盒替换 + for (int i = 0; i < 4; i++) + { + bytes[i] = SBOX[bytes[i]]; + } + + uint result = ((uint)bytes[0] << 24) | + ((uint)bytes[1] << 16) | + ((uint)bytes[2] << 8) | + bytes[3]; + + // L变换 + return result ^ RotateLeft(result, 2) ^ RotateLeft(result, 10) ^ + RotateLeft(result, 18) ^ RotateLeft(result, 24); + } + + private static uint TPrime(uint x) + { + byte[] bytes = new byte[4]; + bytes[0] = (byte)(x >> 24); + bytes[1] = (byte)(x >> 16); + bytes[2] = (byte)(x >> 8); + bytes[3] = (byte)x; + + // S盒替换 + for (int i = 0; i < 4; i++) + { + bytes[i] = SBOX[bytes[i]]; + } + + uint result = ((uint)bytes[0] << 24) | + ((uint)bytes[1] << 16) | + ((uint)bytes[2] << 8) | + bytes[3]; + + // L'变换 + return result ^ RotateLeft(result, 13) ^ RotateLeft(result, 23); + } + + private static uint RotateLeft(uint x, int n) + { + return (x << n) | (x >> (32 - n)); + } + + private static byte[] Pkcs7Pad(byte[] data) + { + int padLen = BLOCK_SIZE - (data.Length % BLOCK_SIZE); + byte[] result = new byte[data.Length + padLen]; + Array.Copy(data, result, data.Length); + for (int i = data.Length; i < result.Length; i++) + { + result[i] = (byte)padLen; + } + return result; + } + + private static byte[] Pkcs7Unpad(byte[] data) + { + if (data.Length == 0) + return data; + + int padLen = data[data.Length - 1]; + if (padLen > BLOCK_SIZE || padLen == 0) + return data; + + // 验证填充 + for (int i = data.Length - padLen; i < data.Length; i++) + { + if (data[i] != padLen) + return data; + } + + byte[] result = new byte[data.Length - padLen]; + Array.Copy(data, result, result.Length); + return result; + } + + private static byte[] GetKeyFromString(string keyString, Encoding encoding) + { + byte[] keyBytes = encoding.GetBytes(keyString); + if (keyBytes.Length == 16) + return keyBytes; + if (keyBytes.Length < 16) + { + byte[] result = new byte[16]; + Array.Copy(keyBytes, result, keyBytes.Length); + return result; + } + byte[] truncated = new byte[16]; + Array.Copy(keyBytes, truncated, 16); + return truncated; + } + + #endregion + } + + /// + /// SM4 加密模式 + /// + public enum Sm4Mode + { + /// + /// 电子密码本模式 + /// + ECB, + + /// + /// 密码分组链接模式 + /// + CBC + } +} diff --git a/EasyTool.Core/CodeCategory/SnappyUtil.cs b/EasyTool.Core/CodeCategory/SnappyUtil.cs new file mode 100644 index 0000000..2f7285c --- /dev/null +++ b/EasyTool.Core/CodeCategory/SnappyUtil.cs @@ -0,0 +1,350 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace EasyTool.CodeCategory +{ + /// + /// Snappy 压缩工具类 + /// Snappy 是 Google 开发的快速压缩算法,注重速度而非压缩率 + /// 广泛用于大数据处理框架如 Hadoop、Spark + /// + public static class SnappyUtil + { + private const int MaxBlockSize = 65536; + private const int MaxInputSize = 2147483647; + + // 操作类型 + private const byte Literal = 0; + private const byte Copy1ByteOffset = 1; + private const byte Copy2ByteOffset = 2; + private const byte Copy4ByteOffset = 3; + + /// + /// 压缩数据 + /// + /// 原始数据 + /// 压缩后的数据 + public static byte[] Compress(byte[] data) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + using var output = new MemoryStream(); + using var writer = new BinaryWriter(output); + + // 写入变长长度 + WriteVarInt(writer, data.Length); + + int pos = 0; + while (pos < data.Length) + { + int remaining = data.Length - pos; + int blockSize = Math.Min(remaining, MaxBlockSize); + + CompressBlock(data, pos, blockSize, writer); + pos += blockSize; + } + + return output.ToArray(); + } + + /// + /// 解压数据 + /// + /// 压缩数据 + /// 原始数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length == 0) + return Array.Empty(); + + using var input = new MemoryStream(compressed); + using var reader = new BinaryReader(input); + + // 读取原始长度 + int originalLength = ReadVarInt(reader); + byte[] result = new byte[originalLength]; + + int pos = 0; + while (pos < originalLength) + { + int remaining = originalLength - pos; + int blockSize = Math.Min(remaining, MaxBlockSize); + + DecompressBlock(reader, result, pos, blockSize); + pos += blockSize; + } + + return result; + } + + /// + /// 压缩字符串 + /// + public static string CompressToBase64(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + public static string DecompressFromBase64(string compressedBase64) + { + if (string.IsNullOrEmpty(compressedBase64)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedBase64); + byte[] data = Decompress(compressed); + return System.Text.Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩后预估大小 + /// + public static int MaxCompressedLength(int sourceLength) + { + if (sourceLength < 0) + throw new ArgumentException("Source length cannot be negative", nameof(sourceLength)); + + // 变长整数最大 5 字节 + 每个块最大开销 + int blocks = (sourceLength + MaxBlockSize - 1) / MaxBlockSize; + return 5 + sourceLength + blocks * 4; + } + + private static void CompressBlock(byte[] input, int inputOffset, int inputLength, BinaryWriter writer) + { + int pos = inputOffset; + int end = inputOffset + inputLength; + + while (pos < end) + { + // 查找最长匹配 + int matchOffset = 0; + int matchLength = 0; + + // 简化的哈希表查找 + if (pos + 4 <= end) + { + FindMatch(input, pos, end, ref matchOffset, ref matchLength); + } + + if (matchLength >= 4) + { + // 写入字面量(如果有) + // 写入复制操作 + int offset = pos - matchOffset - 1; + int length = matchLength - 4; + + if (offset < 2048 && length < 8) + { + // Copy1 或 Copy2 + writer.Write((byte)((length << 2) | Copy1ByteOffset | (offset > 255 ? 0x80 : 0))); + if (offset > 255) + writer.Write((byte)((offset >> 8) | ((length - 8) << 5))); + writer.Write((byte)(offset & 0xFF)); + } + else if (offset < 65536) + { + writer.Write((byte)((length << 2) | Copy2ByteOffset)); + writer.Write((byte)(offset & 0xFF)); + writer.Write((byte)((offset >> 8) & 0xFF)); + } + else + { + writer.Write((byte)((length << 2) | Copy4ByteOffset)); + writer.Write((byte)(offset & 0xFF)); + writer.Write((byte)((offset >> 8) & 0xFF)); + writer.Write((byte)((offset >> 16) & 0xFF)); + writer.Write((byte)((offset >> 24) & 0xFF)); + } + + pos += matchLength; + } + else + { + // 写入字面量 + int literalLength = 1; + while (pos + literalLength < end && literalLength < 60) + { + if (FindMatchAt(input, pos + literalLength, end)) + break; + literalLength++; + } + + WriteLiteral(input, pos, literalLength, writer); + pos += literalLength; + } + } + } + + private static void FindMatch(byte[] input, int pos, int end, ref int matchOffset, ref int matchLength) + { + // 简化的匹配查找 + int searchStart = Math.Max(0, pos - 65536); + int bestLength = 0; + int bestOffset = 0; + + for (int i = searchStart; i < pos; i++) + { + int length = 0; + int maxLen = Math.Min(end - pos, 64); + + while (length < maxLen && input[i + length] == input[pos + length]) + { + length++; + } + + if (length > bestLength) + { + bestLength = length; + bestOffset = i; + } + } + + if (bestLength >= 4) + { + matchOffset = bestOffset; + matchLength = bestLength; + } + } + + private static bool FindMatchAt(byte[] input, int pos, int end) + { + if (pos + 4 > end) + return false; + + int searchStart = Math.Max(0, pos - 65536); + for (int i = searchStart; i < pos; i++) + { + int length = 0; + while (length < 4 && input[i + length] == input[pos + length]) + length++; + + if (length >= 4) + return true; + } + + return false; + } + + private static void WriteLiteral(byte[] input, int offset, int length, BinaryWriter writer) + { + if (length < 60) + { + writer.Write((byte)((length - 1) << 2)); + } + else if (length < 256) + { + writer.Write((byte)(60 << 2)); + writer.Write((byte)(length - 1)); + } + else + { + writer.Write((byte)(61 << 2)); + writer.Write((byte)((length - 1) & 0xFF)); + writer.Write((byte)(((length - 1) >> 8) & 0xFF)); + } + + writer.Write(input, offset, length); + } + + private static void DecompressBlock(BinaryReader reader, byte[] output, int outputOffset, int outputLength) + { + int pos = outputOffset; + int end = outputOffset + outputLength; + + while (pos < end) + { + byte op = reader.ReadByte(); + int opType = op & 0x03; + + if (opType == Literal) + { + int length; + if ((op >> 2) < 60) + { + length = (op >> 2) + 1; + } + else if ((op >> 2) == 60) + { + length = reader.ReadByte() + 1; + } + else + { + int extraBytes = (op >> 2) - 60 + 1; + length = 0; + for (int i = 0; i < extraBytes; i++) + { + length |= reader.ReadByte() << (i * 8); + } + length += 1; + } + + byte[] literal = reader.ReadBytes(length); + Array.Copy(literal, 0, output, pos, length); + pos += length; + } + else + { + int length, offset; + + if (opType == Copy1ByteOffset) + { + length = ((op >> 2) & 0x07) + 4; + offset = ((op & 0xE0) << 3) | reader.ReadByte(); + } + else if (opType == Copy2ByteOffset) + { + length = (op >> 2) + 1; + offset = reader.ReadByte() | (reader.ReadByte() << 8); + } + else + { + length = (op >> 2) + 1; + offset = reader.ReadByte() | (reader.ReadByte() << 8) | + (reader.ReadByte() << 16) | (reader.ReadByte() << 24); + } + + int srcPos = pos - offset; + for (int i = 0; i < length; i++) + { + output[pos++] = output[srcPos++]; + } + } + } + } + + private static void WriteVarInt(BinaryWriter writer, int value) + { + while (value >= 0x80) + { + writer.Write((byte)(value | 0x80)); + value >>= 7; + } + writer.Write((byte)value); + } + + private static int ReadVarInt(BinaryReader reader) + { + int result = 0; + int shift = 0; + byte b; + + do + { + b = reader.ReadByte(); + result |= (b & 0x7F) << shift; + shift += 7; + } while ((b & 0x80) != 0); + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/SonyflakeUtil.cs b/EasyTool.Core/CodeCategory/SonyflakeUtil.cs new file mode 100644 index 0000000..3e4ac6f --- /dev/null +++ b/EasyTool.Core/CodeCategory/SonyflakeUtil.cs @@ -0,0 +1,167 @@ +using System; +using System.Threading; + +namespace EasyTool.CodeCategory +{ + /// + /// Sonyflake ID 工具类 + /// Sonyflake 是 Sony 开发的分布式唯一 ID 生成算法 + /// 结构:39位时间戳 + 8位序列号 + 16位机器ID = 63位 + /// 比雪花 ID 使用更少的时间戳位,支持更长时间 + /// + public static class SonyflakeUtil + { + private static readonly DateTime Epoch = new DateTime(2014, 9, 1, 0, 0, 0, DateTimeKind.Utc); + private static long _lastTimestamp = -1L; + private static ushort _sequence = 0; + private static readonly object _lock = new object(); + private static readonly ushort _machineId; + + private const int TimestampBits = 39; + private const int SequenceBits = 8; + private const int MachineIdBits = 16; + + private const ushort MaxSequence = (1 << SequenceBits) - 1; + private const ushort MaxMachineId = (1 << MachineIdBits) - 1; + + static SonyflakeUtil() + { + // 自动生成机器 ID + byte[] bytes = new byte[2]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + _machineId = (ushort)((bytes[0] << 8) | bytes[1]); + } + + /// + /// 生成 Sonyflake ID + /// + /// 63位 ID + public static ulong Generate() + { + return Generate(_machineId); + } + + /// + /// 生成 Sonyflake ID(指定机器 ID) + /// + /// 机器 ID(0-65535) + /// 63位 ID + public static ulong Generate(ushort machineId) + { + if (machineId > MaxMachineId) + throw new ArgumentException($"Machine ID must be between 0 and {MaxMachineId}", nameof(machineId)); + + lock (_lock) + { + long timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence++; + if (_sequence > MaxSequence) + { + timestamp = WaitForNextTimestamp(_lastTimestamp); + _sequence = 0; + } + } + else + { + _sequence = 0; + } + + _lastTimestamp = timestamp; + + // 组合 ID + return ((ulong)timestamp << (SequenceBits + MachineIdBits)) | + ((ulong)_sequence << MachineIdBits) | + machineId; + } + } + + /// + /// 批量生成 Sonyflake ID + /// + /// 数量 + /// ID 数组 + public static ulong[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new ulong[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 从 Sonyflake ID 提取时间戳 + /// + /// Sonyflake ID + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(ulong id) + { + long timestamp = (long)(id >> (SequenceBits + MachineIdBits)); + return Epoch.AddMilliseconds(timestamp * 10); // 每 10ms 一个时间单位 + } + + /// + /// 从 Sonyflake ID 提取机器 ID + /// + /// Sonyflake ID + /// 机器 ID + public static ushort ExtractMachineId(ulong id) + { + return (ushort)(id & MaxMachineId); + } + + /// + /// 从 Sonyflake ID 提取序列号 + /// + /// Sonyflake ID + /// 序列号 + public static ushort ExtractSequence(ulong id) + { + return (ushort)((id >> MachineIdBits) & MaxSequence); + } + + /// + /// 解析 Sonyflake ID + /// + /// Sonyflake ID + /// 时间戳、机器 ID、序列号 + public static (DateTimeOffset Timestamp, ushort MachineId, ushort Sequence) Parse(ulong id) + { + return (ExtractTimestamp(id), ExtractMachineId(id), ExtractSequence(id)); + } + + /// + /// 获取当前机器 ID + /// + public static ushort MachineId => _machineId; + + /// + /// 获取 Sonyflake 纪元时间 + /// + public static DateTime GetEpoch() => Epoch; + + private static long GetCurrentTimestamp() + { + return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds / 10; + } + + private static long WaitForNextTimestamp(long lastTimestamp) + { + long timestamp = GetCurrentTimestamp(); + while (timestamp <= lastTimestamp) + { + Thread.SpinWait(10); + timestamp = GetCurrentTimestamp(); + } + return timestamp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/SqidsUtil.cs b/EasyTool.Core/CodeCategory/SqidsUtil.cs new file mode 100644 index 0000000..89a5963 --- /dev/null +++ b/EasyTool.Core/CodeCategory/SqidsUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Sqids(以前叫 Hashids)工具类 + /// Sqids 是一种将数字数组编码为短字符串的算法 + /// 可逆、可配置字母表、无碰撞 + /// 常用于生成短 URL、混淆 ID 等 + /// + public static class SqidsUtil + { + private const string DefaultAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + private const int MinAlphabetLength = 3; + + private static readonly byte[] DefaultSalt = Array.Empty(); + + #region 默认实例方法 + + /// + /// 使用默认配置编码单个数字 + /// + /// 要编码的数字 + /// 编码字符串 + public static string Encode(ulong number) + { + return Encode(new[] { number }); + } + + /// + /// 使用默认配置编码数字数组 + /// + /// 要编码的数字数组 + /// 编码字符串 + public static string Encode(ulong[] numbers) + { + return Encode(numbers, DefaultAlphabet, DefaultSalt, 0); + } + + /// + /// 使用默认配置解码为单个数字 + /// + /// 编码字符串 + /// 解码的数字 + public static ulong DecodeSingle(string encoded) + { + var numbers = Decode(encoded); + if (numbers.Length == 0) + throw new ArgumentException("Invalid encoded string"); + return numbers[0]; + } + + /// + /// 使用默认配置解码 + /// + /// 编码字符串 + /// 解码的数字数组 + public static ulong[] Decode(string encoded) + { + return Decode(encoded, DefaultAlphabet, DefaultSalt); + } + + #endregion + + #region 自定义配置方法 + + /// + /// 使用自定义配置编码 + /// + /// 要编码的数字数组 + /// 自定义字母表 + /// 盐值 + /// 最小长度 + /// 编码字符串 + public static string Encode(ulong[] numbers, string alphabet, byte[] salt = null, int minLength = 0) + { + if (numbers == null || numbers.Length == 0) + throw new ArgumentException("Numbers cannot be empty", nameof(numbers)); + if (string.IsNullOrEmpty(alphabet) || alphabet.Length < MinAlphabetLength) + throw new ArgumentException($"Alphabet must be at least {MinAlphabetLength} characters", nameof(alphabet)); + + salt ??= Array.Empty(); + alphabet = ShuffleAlphabet(alphabet, salt); + + // 计算前缀 + char prefix = alphabet[0]; + string alphabetWithoutPrefix = alphabet.Substring(1) + alphabet[0]; + + var result = new StringBuilder(); + result.Append(prefix); + + // 编码每个数字 + for (int i = 0; i < numbers.Length; i++) + { + ulong number = numbers[i]; + string currentAlphabet = ConsistentShuffle(alphabetWithoutPrefix, salt, i); + + string encoded = EncodeNumber(number, currentAlphabet); + result.Append(encoded); + + if (i < numbers.Length - 1) + { + char separator = currentAlphabet[(int)(number % (ulong)(currentAlphabet.Length - 1))]; + result.Append(separator); + alphabetWithoutPrefix = RotateAlphabet(alphabetWithoutPrefix, encoded[encoded.Length - 1]); + } + } + + // 填充到最小长度 + string finalResult = result.ToString(); + if (minLength > 0 && finalResult.Length < minLength) + { + int diff = minLength - finalResult.Length; + string paddedAlphabet = ConsistentShuffle(alphabet, salt, 0); + finalResult = paddedAlphabet.Substring(0, diff / 2) + finalResult + paddedAlphabet.Substring(paddedAlphabet.Length - (diff - diff / 2)); + } + + return finalResult; + } + + /// + /// 使用自定义配置解码 + /// + /// 编码字符串 + /// 自定义字母表 + /// 盐值 + /// 解码的数字数组 + public static ulong[] Decode(string encoded, string alphabet, byte[] salt = null) + { + if (string.IsNullOrEmpty(encoded)) + throw new ArgumentException("Encoded string cannot be empty", nameof(encoded)); + if (string.IsNullOrEmpty(alphabet) || alphabet.Length < MinAlphabetLength) + throw new ArgumentException($"Alphabet must be at least {MinAlphabetLength} characters", nameof(alphabet)); + + salt ??= Array.Empty(); + alphabet = ShuffleAlphabet(alphabet, salt); + + // 移除填充 + encoded = RemovePadding(encoded, alphabet, salt); + + // 获取前缀 + char prefix = encoded[0]; + if (!alphabet.Contains(prefix)) + throw new ArgumentException("Invalid encoded string: unknown prefix"); + + string alphabetWithoutPrefix = alphabet.Substring(1) + alphabet[0]; + + var numbers = new List(); + string remaining = encoded.Substring(1); + + for (int i = 0; remaining.Length > 0; i++) + { + string currentAlphabet = ConsistentShuffle(alphabetWithoutPrefix, salt, i); + + // 找到分隔符 + int separatorIndex = -1; + for (int j = 0; j < remaining.Length; j++) + { + if (!currentAlphabet.Contains(remaining[j])) + { + separatorIndex = j; + break; + } + } + + string encodedNumber; + if (separatorIndex >= 0) + { + encodedNumber = remaining.Substring(0, separatorIndex); + remaining = remaining.Substring(separatorIndex + 1); + } + else + { + encodedNumber = remaining; + remaining = ""; + } + + ulong number = DecodeNumber(encodedNumber, currentAlphabet); + numbers.Add(number); + + if (encodedNumber.Length > 0) + { + alphabetWithoutPrefix = RotateAlphabet(alphabetWithoutPrefix, encodedNumber[encodedNumber.Length - 1]); + } + } + + return numbers.ToArray(); + } + + #endregion + + #region Sqids 实例 + + /// + /// 创建 Sqids 编码器实例 + /// + /// 字母表 + /// 盐值 + /// 最小长度 + /// Sqids 实例 + public static SqidsEncoder Create(string alphabet = null, byte[] salt = null, int minLength = 0) + { + return new SqidsEncoder(alphabet ?? DefaultAlphabet, salt, minLength); + } + + #endregion + + #region 私有方法 + + private static string ShuffleAlphabet(string alphabet, byte[] salt) + { + char[] chars = alphabet.ToCharArray(); + + if (salt.Length == 0) + return new string(chars); + + int j = chars.Length - 1; + int v = 0; + int p = 0; + + for (int i = chars.Length - 1; i > 0; i--, j--) + { + v %= salt.Length; + p += salt[v]; + int k = (salt[v] + p + i) % (i + 1); + + char temp = chars[i]; + chars[i] = chars[k]; + chars[k] = temp; + + v++; + } + + return new string(chars); + } + + private static string ConsistentShuffle(string alphabet, byte[] salt, int iteration) + { + if (salt.Length == 0) + return alphabet; + + char[] chars = alphabet.ToCharArray(); + int v = iteration % salt.Length; + + for (int i = chars.Length - 1; i > 0; i--) + { + int k = (salt[v] + i) % (i + 1); + + char temp = chars[i]; + chars[i] = chars[k]; + chars[k] = temp; + + v = (v + 1) % salt.Length; + } + + return new string(chars); + } + + private static string RotateAlphabet(string alphabet, char c) + { + int index = alphabet.IndexOf(c); + if (index < 0) + return alphabet; + + return alphabet.Substring(index + 1) + alphabet.Substring(0, index + 1); + } + + private static string EncodeNumber(ulong number, string alphabet) + { + var result = new StringBuilder(); + int baseLength = alphabet.Length; + + do + { + result.Insert(0, alphabet[(int)(number % (ulong)baseLength)]); + number /= (ulong)baseLength; + } while (number > 0); + + return result.ToString(); + } + + private static ulong DecodeNumber(string encoded, string alphabet) + { + ulong result = 0; + int baseLength = alphabet.Length; + + foreach (char c in encoded) + { + int index = alphabet.IndexOf(c); + if (index < 0) + throw new ArgumentException($"Invalid character: {c}"); + + result = result * (ulong)baseLength + (ulong)index; + } + + return result; + } + + private static string RemovePadding(string encoded, string alphabet, byte[] salt) + { + // 检查是否有有效的数字字符 + for (int i = 1; i < encoded.Length; i++) + { + if (alphabet.Contains(encoded[i])) + return encoded.Substring(i - 1); + } + + return encoded; + } + + #endregion + } + + /// + /// Sqids 编码器实例 + /// + public class SqidsEncoder + { + private readonly string _alphabet; + private readonly byte[] _salt; + private readonly int _minLength; + + /// + /// 创建 Sqids 编码器 + /// + /// 字母表 + /// 盐值 + /// 最小长度 + public SqidsEncoder(string alphabet, byte[] salt, int minLength) + { + _alphabet = alphabet; + _salt = salt ?? Array.Empty(); + _minLength = minLength; + } + + /// + /// 编码单个数字 + /// + /// 数字 + /// 编码字符串 + public string Encode(ulong number) + { + return SqidsUtil.Encode(new[] { number }, _alphabet, _salt, _minLength); + } + + /// + /// 编码数字数组 + /// + /// 数字数组 + /// 编码字符串 + public string Encode(ulong[] numbers) + { + return SqidsUtil.Encode(numbers, _alphabet, _salt, _minLength); + } + + /// + /// 解码为单个数字 + /// + /// 编码字符串 + /// 数字 + public ulong DecodeSingle(string encoded) + { + var numbers = SqidsUtil.Decode(encoded, _alphabet, _salt); + if (numbers.Length == 0) + throw new ArgumentException("Invalid encoded string"); + return numbers[0]; + } + + /// + /// 解码 + /// + /// 编码字符串 + /// 数字数组 + public ulong[] Decode(string encoded) + { + return SqidsUtil.Decode(encoded, _alphabet, _salt); + } + } +} diff --git a/EasyTool.Core/CodeCategory/TOTPUtil.cs b/EasyTool.Core/CodeCategory/TOTPUtil.cs new file mode 100644 index 0000000..1a2426f --- /dev/null +++ b/EasyTool.Core/CodeCategory/TOTPUtil.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// TOTP(Time-based One-Time Password)和 HOTP(HMAC-based One-Time Password)工具类 + /// 用于生成和验证一次性密码,常用于双因素认证(2FA) + /// + public static class TOTPUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + // Base32 字符集(用于密钥编码) + private const string Base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + + #region TOTP(基于时间) + + /// + /// 生成 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码位数(默认6位) + /// 时间周期(默认30秒) + /// 验证码 + public static string GenerateTOTP(string secret, int digits = 6, int period = 30) + { + byte[] key = Base32Decode(secret); + return GenerateTOTP(key, digits, period); + } + + /// + /// 生成 TOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码位数 + /// 时间周期(秒) + /// 验证码 + public static string GenerateTOTP(byte[] secret, int digits = 6, int period = 30) + { + long counter = GetCurrentCounter(period); + return GenerateHOTP(secret, counter, digits); + } + + /// + /// 生成指定时间的 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 时间戳 + /// 验证码位数 + /// 时间周期 + /// 验证码 + public static string GenerateTOTP(string secret, DateTime timestamp, int digits = 6, int period = 30) + { + byte[] key = Base32Decode(secret); + long counter = GetCounter(timestamp, period); + return GenerateHOTP(key, counter, digits); + } + + /// + /// 验证 TOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码 + /// 验证码位数 + /// 时间周期 + /// 允许的时间窗口(前后各多少个周期) + /// 是否验证通过 + public static bool VerifyTOTP(string secret, string code, int digits = 6, int period = 30, int window = 1) + { + byte[] key = Base32Decode(secret); + return VerifyTOTP(key, code, digits, period, window); + } + + /// + /// 验证 TOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码 + /// 验证码位数 + /// 时间周期 + /// 允许的时间窗口 + /// 是否验证通过 + public static bool VerifyTOTP(byte[] secret, string code, int digits = 6, int period = 30, int window = 1) + { + if (string.IsNullOrEmpty(code) || code.Length != digits) + return false; + + long currentCounter = GetCurrentCounter(period); + + // 检查时间窗口内的所有可能值 + for (int i = -window; i <= window; i++) + { + long counter = currentCounter + i; + string expectedCode = GenerateHOTP(secret, counter, digits); + if (ConstantTimeEquals(code, expectedCode)) + { + return true; + } + } + + return false; + } + + #endregion + + #region HOTP(基于计数器) + + /// + /// 生成 HOTP 验证码 + /// + /// 密钥(Base32编码) + /// 计数器值 + /// 验证码位数 + /// 验证码 + public static string GenerateHOTP(string secret, long counter, int digits = 6) + { + byte[] key = Base32Decode(secret); + return GenerateHOTP(key, counter, digits); + } + + /// + /// 生成 HOTP 验证码 + /// + /// 密钥(字节数组) + /// 计数器值 + /// 验证码位数 + /// 验证码 + public static string GenerateHOTP(byte[] secret, long counter, int digits = 6) + { + // 将计数器转换为大端序字节数组 + byte[] counterBytes = BitConverter.GetBytes(counter); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(counterBytes); + } + + // 使用 HMAC-SHA1 计算 + using var hmac = new HMACSHA1(secret); + byte[] hash = hmac.ComputeHash(counterBytes); + + // 动态截断 + int offset = hash[hash.Length - 1] & 0x0F; + int binaryCode = ((hash[offset] & 0x7F) << 24) | + ((hash[offset + 1] & 0xFF) << 16) | + ((hash[offset + 2] & 0xFF) << 8) | + (hash[offset + 3] & 0xFF); + + int code = binaryCode % (int)Math.Pow(10, digits); + + return code.ToString().PadLeft(digits, '0'); + } + + /// + /// 验证 HOTP 验证码 + /// + /// 密钥(Base32编码) + /// 验证码 + /// 计数器值 + /// 验证码位数 + /// 允许的计数器窗口 + /// 验证结果和下一个计数器值 + public static (bool Valid, long NextCounter) VerifyHOTP(string secret, string code, long counter, int digits = 6, int window = 10) + { + byte[] key = Base32Decode(secret); + return VerifyHOTP(key, code, counter, digits, window); + } + + /// + /// 验证 HOTP 验证码 + /// + /// 密钥(字节数组) + /// 验证码 + /// 计数器值 + /// 验证码位数 + /// 允许的计数器窗口 + /// 验证结果和下一个计数器值 + public static (bool Valid, long NextCounter) VerifyHOTP(byte[] secret, string code, long counter, int digits = 6, int window = 10) + { + if (string.IsNullOrEmpty(code) || code.Length != digits) + return (false, counter); + + for (long i = counter; i < counter + window; i++) + { + string expectedCode = GenerateHOTP(secret, i, digits); + if (ConstantTimeEquals(code, expectedCode)) + { + return (true, i + 1); + } + } + + return (false, counter); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成随机密钥 + /// + /// 密钥长度(字节,默认20) + /// Base32编码的密钥 + public static string GenerateSecret(int length = 20) + { + byte[] bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return Base32Encode(bytes); + } + + /// + /// 生成随机密钥(字节数组) + /// + /// 密钥长度 + /// 密钥字节数组 + public static byte[] GenerateSecretBytes(int length = 20) + { + byte[] bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return bytes; + } + + #endregion + + #region URI 生成(用于二维码) + + /// + /// 生成 otpauth:// 格式的 URI + /// + /// 发行者(应用名称) + /// 账户名 + /// 密钥(Base32编码) + /// 验证码位数 + /// 时间周期 + /// otpauth URI + public static string GetOtpAuthUri(string issuer, string account, string secret, int digits = 6, int period = 30) + { + string encodedIssuer = Uri.EscapeDataString(issuer); + string encodedAccount = Uri.EscapeDataString(account); + + return $"otpauth://totp/{encodedIssuer}:{encodedAccount}?secret={secret}&issuer={encodedIssuer}&digits={digits}&period={period}"; + } + + /// + /// 生成 HOTP 的 otpauth:// 格式 URI + /// + /// 发行者 + /// 账户名 + /// 密钥 + /// 计数器 + /// 验证码位数 + /// otpauth URI + public static string GetHotpAuthUri(string issuer, string account, string secret, long counter, int digits = 6) + { + string encodedIssuer = Uri.EscapeDataString(issuer); + string encodedAccount = Uri.EscapeDataString(account); + + return $"otpauth://hotp/{encodedIssuer}:{encodedAccount}?secret={secret}&issuer={encodedIssuer}&digits={digits}&counter={counter}"; + } + + #endregion + + #region 时间工具 + + /// + /// 获取当前计数器值 + /// + /// 时间周期 + /// 计数器值 + public static long GetCurrentCounter(int period = 30) + { + return GetCounter(DateTime.UtcNow, period); + } + + /// + /// 获取指定时间的计数器值 + /// + /// 时间戳 + /// 时间周期 + /// 计数器值 + public static long GetCounter(DateTime timestamp, int period = 30) + { + long elapsedSeconds = (long)(timestamp.ToUniversalTime() - Epoch).TotalSeconds; + return elapsedSeconds / period; + } + + /// + /// 获取当前验证码的剩余有效时间 + /// + /// 时间周期 + /// 剩余秒数 + public static int GetRemainingSeconds(int period = 30) + { + long elapsedSeconds = (long)(DateTime.UtcNow - Epoch).TotalSeconds; + return period - (int)(elapsedSeconds % period); + } + + #endregion + + #region 私有方法 + + private static string Base32Encode(byte[] data) + { + var result = new StringBuilder((data.Length * 8 + 4) / 5); + + int i = 0; + int remainingBits = 0; + int currentByte = 0; + + while (i < data.Length || remainingBits > 0) + { + if (remainingBits < 5 && i < data.Length) + { + currentByte = (currentByte << 8) | data[i++]; + remainingBits += 8; + } + + if (remainingBits >= 5) + { + int index = (currentByte >> (remainingBits - 5)) & 0x1F; + result.Append(Base32Chars[index]); + remainingBits -= 5; + } + else if (remainingBits > 0) + { + int index = (currentByte << (5 - remainingBits)) & 0x1F; + result.Append(Base32Chars[index]); + remainingBits = 0; + } + } + + return result.ToString(); + } + + private static byte[] Base32Decode(string data) + { + data = data.ToUpperInvariant().Replace(" ", "").Replace("-", ""); + + var result = new List(); + int currentByte = 0; + int remainingBits = 0; + + foreach (char c in data) + { + int value = Base32Chars.IndexOf(c); + if (value < 0) + continue; + + currentByte = (currentByte << 5) | value; + remainingBits += 5; + + while (remainingBits >= 8) + { + result.Add((byte)((currentByte >> (remainingBits - 8)) & 0xFF)); + remainingBits -= 8; + } + } + + return result.ToArray(); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + return false; + + int result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + + return result == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/TigerUtil.cs b/EasyTool.Core/CodeCategory/TigerUtil.cs new file mode 100644 index 0000000..7e00dd1 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TigerUtil.cs @@ -0,0 +1,291 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Tiger 哈希工具类 + /// Tiger 是由 Ross Anderson 和 Eli Biham 设计的快速哈希算法 + /// 专为 64 位处理器优化,输出 192 位(24 字节) + /// + public static class TigerUtil + { + private const int HashSize = 24; // 192位 + private const int BlockSize = 64; // 512位 + + // S-boxes + private static readonly ulong[] S = new ulong[] + { + 0x02AAB17CF7E90C5E, 0xAC424B03E243A8EC, 0x72CD5BE30DD5FCD3, 0x6D019B93F6F97F3A, + 0xCD9978FFD21F9193, 0x7573A1C970802FAE, 0xB164326B922A83BB, 0x46883EEE04915870, + 0xEAACE3057103ECE6, 0xC54169B808A3535C, 0x4CE754918DDEC47C, 0x0AA2F4DFDC0DF40C, + 0x10B76F18A74DBEFA, 0xC6CCB6235AD1AB6A, 0x13726121572FE2FF, 0x1A488C6F199D921E, + 0x4BC9F9F4DA0007CA, 0x26F5E6F6E85241C7, 0x859079DBEA5947B6, 0x4F1885B5EB4F880C, + 0xD78E761EA6F7CBA0, 0x8E36428C52B5C17D, 0x69CF6827373063C1, 0xB607C93D9BB4C56E, + 0x7D820E760E76B5EA, 0x645C9CC6F07FDC42, 0xBF38A078243342E0, 0x5F6B343C9D2E7D04, + 0xF2C28AEB600B0EC6, 0x6C0ED85F7254BCAC, 0x71592281A4DB4FE5, 0x1967FA69CE0FED9F, + 0xFD5293F8B96545DB, 0xC879E84D5BB62F8F, 0x860248920193194E, 0xA4F953AA47EE7048, + 0xD957E363A198BF6B, 0x327894F2FDDC3BBA, 0x9F7F973ED03B1AE9, 0x1B505014AE5AC36B, + 0xE7CC8C8EFB4C41F7, 0x7D4DA8DE2296204E, 0x7E9791D04B8C6B88, 0x39A8B0D45C357F47, + 0x723F453E1A6ED868, 0x59E59E13C6A5C3BF, 0xB6F3169AB9916821, 0x9E6B0E7A3A2888F7 + }; + + /// + /// 计算 Tiger 哈希值 + /// + /// 输入数据 + /// 24字节哈希值 + public static byte[] Hash(byte[] data) + { + if (data == null || data.Length == 0) + return new byte[HashSize]; + + // 初始值 + ulong a = 0x0123456789ABCDEF; + ulong b = 0xFEDCBA9876543210; + ulong c = 0xF096A5B4C3B2E187; + + // 填充 + byte[] padded = PadMessage(data); + int blocks = padded.Length / BlockSize; + + for (int i = 0; i < blocks; i++) + { + ulong[] x = new ulong[8]; + for (int j = 0; j < 8; j++) + { + x[j] = BitConverter.ToUInt64(padded, i * BlockSize + j * 8); + } + + TigerRound(ref a, ref b, ref c, x); + } + + return Combine(a, b, c); + } + + /// + /// 计算字符串的 Tiger 哈希值 + /// + /// 输入文本 + /// 十六进制哈希字符串 + public static string HashString(string text) + { + if (string.IsNullOrEmpty(text)) + return new string('0', HashSize * 2); + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] hash = Hash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 验证哈希值 + /// + /// 原始数据 + /// 预期哈希值 + /// 是否匹配 + public static bool Verify(byte[] data, byte[] hash) + { + if (hash == null || hash.Length != HashSize) + return false; + + byte[] computed = Hash(data); + return SlowEquals(computed, hash); + } + + /// + /// 验证字符串哈希 + /// + /// 原始文本 + /// 预期哈希值(十六进制) + /// 是否匹配 + public static bool VerifyString(string text, string hashHex) + { + if (string.IsNullOrEmpty(hashHex) || hashHex.Length != HashSize * 2) + return false; + + string computed = HashString(text); + return string.Equals(computed, hashHex, StringComparison.OrdinalIgnoreCase); + } + + private static byte[] PadMessage(byte[] data) + { + int length = data.Length; + int paddingLength = BlockSize - ((length + 9) % BlockSize); + if (paddingLength == BlockSize) paddingLength = 0; + + byte[] padded = new byte[length + 1 + paddingLength + 8]; + Array.Copy(data, padded, length); + + // 添加 0x01 填充 + padded[length] = 0x01; + + // 添加长度(位数) + ulong bitLength = (ulong)length * 8; + for (int i = 0; i < 8; i++) + { + padded[padded.Length - 1 - i] = (byte)(bitLength >> (i * 8)); + } + + return padded; + } + + private static void TigerRound(ref ulong a, ref ulong b, ref ulong c, ulong[] x) + { + // 保存原始值 + ulong aa = a, bb = b, cc = c; + + // Pass 1 + c ^= x[0]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[2]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[3]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[5]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[6]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + // Pass 2 + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[6]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[5]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[3]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[2]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[0]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + // Pass 3 + c ^= x[0]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[1]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[2]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[3]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[4]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + b ^= x[5]; + c -= Table(b, 0); + a += Table(b, 2); + a *= 5; + + c ^= x[6]; + a -= Table(c, 0); + b += Table(c, 2); + b *= 5; + + a ^= x[7]; + b -= Table(a, 0); + c += Table(a, 2); + c *= 5; + + // 反馈 + a ^= aa; + b -= bb; + c += cc; + } + + private static ulong Table(ulong x, int i) + { + byte b = (byte)(x >> (i * 8)); + return S[b % S.Length]; + } + + private static byte[] Combine(ulong a, ulong b, ulong c) + { + byte[] result = new byte[24]; + BitConverter.GetBytes(a).CopyTo(result, 0); + BitConverter.GetBytes(b).CopyTo(result, 8); + BitConverter.GetBytes(c).CopyTo(result, 16); + return result; + } + + private static bool SlowEquals(byte[] a, byte[] b) + { + uint diff = (uint)a.Length ^ (uint)b.Length; + for (int i = 0; i < a.Length && i < b.Length; i++) + diff |= (uint)(a[i] ^ b[i]); + return diff == 0; + } + } +} diff --git a/EasyTool.Core/CodeCategory/TimestampUtil.cs b/EasyTool.Core/CodeCategory/TimestampUtil.cs new file mode 100644 index 0000000..8104870 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TimestampUtil.cs @@ -0,0 +1,581 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 时间戳工具类 + /// 提供时间戳的生成、转换、格式化等功能 + /// 支持10位秒级和13位毫秒级时间戳 + /// + public static class TimestampUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + #region 获取时间戳 + + /// + /// 获取当前时间的秒级时间戳(10位) + /// + /// 秒级时间戳 + public static long Now() + { + return (long)(DateTime.UtcNow - Epoch).TotalSeconds; + } + + /// + /// 获取当前时间的毫秒级时间戳(13位) + /// + /// 毫秒级时间戳 + public static long NowMs() + { + return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; + } + + /// + /// 获取当前时间的微秒级时间戳(16位) + /// + /// 微秒级时间戳 + public static long NowUs() + { + var ts = DateTime.UtcNow - Epoch; + return (long)(ts.TotalMilliseconds * 1000); + } + + /// + /// 获取当前时间的纳秒级时间戳(19位) + /// + /// 纳秒级时间戳 + public static long NowNs() + { + var ts = DateTime.UtcNow - Epoch; + return (long)(ts.TotalMilliseconds * 1000000); + } + + /// + /// 获取当前时间戳字符串 + /// + /// 精度:s(秒), ms(毫秒), us(微秒), ns(纳秒) + /// 时间戳字符串 + public static string NowString(string precision = "ms") + { + return precision.ToLowerInvariant() switch + { + "s" => Now().ToString(), + "ms" => NowMs().ToString(), + "us" => NowUs().ToString(), + "ns" => NowNs().ToString(), + _ => NowMs().ToString() + }; + } + + #endregion + + #region DateTime 转时间戳 + + /// + /// 将 DateTime 转换为秒级时间戳 + /// + /// 日期时间 + /// 秒级时间戳 + public static long ToTimestamp(DateTime dateTime) + { + return (long)(dateTime.ToUniversalTime() - Epoch).TotalSeconds; + } + + /// + /// 将 DateTime 转换为毫秒级时间戳 + /// + /// 日期时间 + /// 毫秒级时间戳 + public static long ToTimestampMs(DateTime dateTime) + { + return (long)(dateTime.ToUniversalTime() - Epoch).TotalMilliseconds; + } + + /// < /// 将 DateTime 转换为指定精度的时间戳 + /// + /// 日期时间 + /// 精度:s, ms, us, ns + /// 时间戳 + public static long ToTimestamp(DateTime dateTime, string precision) + { + var ts = dateTime.ToUniversalTime() - Epoch; + return precision.ToLowerInvariant() switch + { + "s" => (long)ts.TotalSeconds, + "ms" => (long)ts.TotalMilliseconds, + "us" => (long)(ts.TotalMilliseconds * 1000), + "ns" => (long)(ts.TotalMilliseconds * 1000000), + _ => (long)ts.TotalMilliseconds + }; + } + + #endregion + + #region 时间戳转 DateTime + + /// + /// 将时间戳转换为 DateTime(自动识别精度) + /// + /// 时间戳 + /// DateTime + public static DateTime FromTimestamp(long timestamp) + { + // 自动判断精度 + if (timestamp > 1000000000000L) + { + // 13位毫秒级 + if (timestamp > 10000000000000L) + { + // 16位微秒级 + if (timestamp > 100000000000000L) + { + // 19位纳秒级 + return Epoch.AddTicks(timestamp / 100); + } + return Epoch.AddTicks(timestamp / 10); + } + return FromTimestampMs(timestamp); + } + else + { + // 10位秒级 + return FromTimestampSeconds(timestamp); + } + } + + /// + /// 将秒级时间戳转换为 DateTime + /// + /// 秒级时间戳 + /// DateTime + public static DateTime FromTimestampSeconds(long timestamp) + { + return Epoch.AddSeconds(timestamp); + } + + /// + /// 将毫秒级时间戳转换为 DateTime + /// + /// 毫秒级时间戳 + /// DateTime + public static DateTime FromTimestampMs(long timestamp) + { + return Epoch.AddMilliseconds(timestamp); + } + + /// + /// 将字符串时间戳转换为 DateTime + /// + /// 时间戳字符串 + /// DateTime + public static DateTime FromString(string timestamp) + { + if (string.IsNullOrEmpty(timestamp)) + throw new ArgumentException("Timestamp cannot be null or empty", nameof(timestamp)); + + if (long.TryParse(timestamp, out long ts)) + { + return FromTimestamp(ts); + } + + throw new ArgumentException("Invalid timestamp format", nameof(timestamp)); + } + + #endregion + + #region 格式化 + + /// + /// 格式化当前时间戳 + /// + /// 日期格式(默认 yyyy-MM-dd HH:mm:ss) + /// 格式化的日期字符串 + public static string Format(string format = "yyyy-MM-dd HH:mm:ss") + { + return DateTime.UtcNow.ToString(format); + } + + /// + /// 格式化时间戳 + /// + /// 时间戳 + /// 日期格式 + /// 格式化的日期字符串 + public static string Format(long timestamp, string format = "yyyy-MM-dd HH:mm:ss") + { + return FromTimestamp(timestamp).ToString(format); + } + + /// + /// 格式化为 ISO 8601 格式 + /// + /// 时间戳 + /// ISO 8601 格式字符串 + public static string ToIso8601(long timestamp) + { + return FromTimestamp(timestamp).ToString("o"); + } + + /// + /// 从 ISO 8601 格式解析 + /// + /// ISO 8601 格式字符串 + /// 时间戳(毫秒) + public static long FromIso8601(string iso8601) + { + if (DateTime.TryParse(iso8601, out DateTime dt)) + { + return ToTimestampMs(dt); + } + throw new ArgumentException("Invalid ISO 8601 format", nameof(iso8601)); + } + + #endregion + + #region 时间计算 + + /// + /// 计算两个时间戳之间的时间差 + /// + /// 开始时间戳 + /// 结束时间戳 + /// 时间差 + public static TimeSpan Diff(long start, long end) + { + var startTime = FromTimestamp(start); + var endTime = FromTimestamp(end); + return endTime - startTime; + } + + /// + /// 添加秒数 + /// + /// 时间戳(秒) + /// 秒数 + /// 新的时间戳 + public static long AddSeconds(long timestamp, int seconds) + { + return timestamp + seconds; + } + + /// + /// 添加分钟 + /// + /// 时间戳(秒) + /// 分钟数 + /// 新的时间戳 + public static long AddMinutes(long timestamp, int minutes) + { + return timestamp + minutes * 60; + } + + /// + /// 添加小时 + /// + /// 时间戳(秒) + /// 小时数 + /// 新的时间戳 + public static long AddHours(long timestamp, int hours) + { + return timestamp + hours * 3600; + } + + /// + /// 添加天数 + /// + /// 时间戳(秒) + /// 天数 + /// 新的时间戳 + public static long AddDays(long timestamp, int days) + { + return timestamp + days * 86400; + } + + /// + /// 获取今天开始时间戳(00:00:00) + /// + /// 秒级时间戳 + public static long TodayStart() + { + var today = DateTime.UtcNow.Date; + return ToTimestamp(today); + } + + /// + /// 获取今天结束时间戳(23:59:59) + /// + /// 秒级时间戳 + public static long TodayEnd() + { + var today = DateTime.UtcNow.Date.AddDays(1).AddSeconds(-1); + return ToTimestamp(today); + } + + /// + /// 获取本周开始时间戳 + /// + /// 秒级时间戳 + public static long WeekStart() + { + var today = DateTime.UtcNow.Date; + var diff = (7 + (today.DayOfWeek - DayOfWeek.Monday)) % 7; + var weekStart = today.AddDays(-diff); + return ToTimestamp(weekStart); + } + + /// + /// 获取本月开始时间戳 + /// + /// 秒级时间戳 + public static long MonthStart() + { + var today = DateTime.UtcNow; + var monthStart = new DateTime(today.Year, today.Month, 1); + return ToTimestamp(monthStart); + } + + /// + /// 获取本年开始时间戳 + /// + /// 秒级时间戳 + public static long YearStart() + { + var today = DateTime.UtcNow; + var yearStart = new DateTime(today.Year, 1, 1); + return ToTimestamp(yearStart); + } + + #endregion + + #region 验证和比较 + + /// + /// 验证时间戳是否有效 + /// + /// 时间戳 + /// 是否有效 + public static bool IsValid(long timestamp) + { + try + { + var dt = FromTimestamp(timestamp); + return dt.Year >= 1970 && dt.Year <= 2100; + } + catch + { + return false; + } + } + + /// + /// 验证时间戳字符串是否有效 + /// + /// 时间戳字符串 + /// 是否有效 + public static bool IsValid(string timestamp) + { + if (string.IsNullOrEmpty(timestamp)) + return false; + + if (long.TryParse(timestamp, out long ts)) + { + return IsValid(ts); + } + + return false; + } + + /// + /// 比较两个时间戳 + /// + /// 时间戳1 + /// 时间戳2 + /// -1: ts1<ts2, 0: 相等, 1: ts1>ts2 + public static int Compare(long ts1, long ts2) + { + return ts1.CompareTo(ts2); + } + + /// + /// 判断时间戳是否在指定范围内 + /// + /// 时间戳 + /// 开始时间戳 + /// 结束时间戳 + /// 是否在范围内 + public static bool IsBetween(long timestamp, long start, long end) + { + return timestamp >= start && timestamp <= end; + } + + /// + /// 判断是否是今天 + /// + /// 时间戳 + /// 是否是今天 + public static bool IsToday(long timestamp) + { + var dt = FromTimestamp(timestamp); + var today = DateTime.UtcNow.Date; + return dt.Date == today; + } + + /// + /// 判断是否是昨天 + /// + /// 时间戳 + /// 是否是昨天 + public static bool IsYesterday(long timestamp) + { + var dt = FromTimestamp(timestamp); + var yesterday = DateTime.UtcNow.Date.AddDays(-1); + return dt.Date == yesterday; + } + + /// + /// 判断是否是明天 + /// + /// 时间戳 + /// 是否是明天 + public static bool IsTomorrow(long timestamp) + { + var dt = FromTimestamp(timestamp); + var tomorrow = DateTime.UtcNow.Date.AddDays(1); + return dt.Date == tomorrow; + } + + #endregion + + #region 批量转换 + + /// + /// 批量将 DateTime 转换为时间戳 + /// + /// 日期时间数组 + /// 是否使用毫秒精度 + /// 时间戳数组 + public static long[] BatchToTimestamp(DateTime[] dateTimes, bool milliseconds = false) + { + var result = new long[dateTimes.Length]; + for (int i = 0; i < dateTimes.Length; i++) + { + result[i] = milliseconds ? ToTimestampMs(dateTimes[i]) : ToTimestamp(dateTimes[i]); + } + return result; + } + + /// + /// 批量将时间戳转换为 DateTime + /// + /// 时间戳数组 + /// DateTime 数组 + public static DateTime[] BatchFromTimestamp(long[] timestamps) + { + var result = new DateTime[timestamps.Length]; + for (int i = 0; i < timestamps.Length; i++) + { + result[i] = FromTimestamp(timestamps[i]); + } + return result; + } + + #endregion + + #region 友好显示 + + /// + /// 获取友好的时间显示(如:刚刚、5分钟前、昨天等) + /// + /// 时间戳 + /// 友好显示 + public static string Friendly(long timestamp) + { + var dt = FromTimestamp(timestamp); + var now = DateTime.UtcNow; + var diff = now - dt; + + if (diff.TotalSeconds < 60) + { + return "刚刚"; + } + else if (diff.TotalMinutes < 60) + { + return $"{(int)diff.TotalMinutes}分钟前"; + } + else if (diff.TotalHours < 24) + { + return $"{(int)diff.TotalHours}小时前"; + } + else if (diff.TotalDays < 2) + { + return "昨天"; + } + else if (diff.TotalDays < 7) + { + return $"{(int)diff.TotalDays}天前"; + } + else if (diff.TotalDays < 30) + { + return $"{(int)(diff.TotalDays / 7)}周前"; + } + else if (diff.TotalDays < 365) + { + return $"{(int)(diff.TotalDays / 30)}个月前"; + } + else + { + return $"{(int)(diff.TotalDays / 365)}年前"; + } + } + + /// + /// 获取剩余时间的友好显示 + /// + /// 目标时间戳 + /// 友好显示 + public static string FriendlyRemaining(long timestamp) + { + var dt = FromTimestamp(timestamp); + var now = DateTime.UtcNow; + var diff = dt - now; + + if (diff.TotalSeconds <= 0) + { + return "已过期"; + } + else if (diff.TotalSeconds < 60) + { + return $"{(int)diff.TotalSeconds}秒后"; + } + else if (diff.TotalMinutes < 60) + { + return $"{(int)diff.TotalMinutes}分钟后"; + } + else if (diff.TotalHours < 24) + { + return $"{(int)diff.TotalHours}小时后"; + } + else if (diff.TotalDays < 7) + { + return $"{(int)diff.TotalDays}天后"; + } + else if (diff.TotalDays < 30) + { + return $"{(int)(diff.TotalDays / 7)}周后"; + } + else if (diff.TotalDays < 365) + { + return $"{(int)(diff.TotalDays / 30)}个月后"; + } + else + { + return $"{(int)(diff.TotalDays / 365)}年后"; + } + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/TwofishUtil.cs b/EasyTool.Core/CodeCategory/TwofishUtil.cs new file mode 100644 index 0000000..0dcfb5c --- /dev/null +++ b/EasyTool.Core/CodeCategory/TwofishUtil.cs @@ -0,0 +1,308 @@ +using System; +using System.Security.Cryptography; + +namespace EasyTool.CodeCategory +{ + /// + /// Twofish 对称加密工具类 + /// Twofish 是 AES 的最终候选算法之一,由 Bruce Schneier 团队设计 + /// 128位分组密码,支持128/192/256位密钥 + /// + public static class TwofishUtil + { + private const int BlockSize = 16; // 128位 + private const int Rounds = 16; + + // MDS 矩阵常量 + private static readonly byte[] MDS_GF_FDBK = new byte[] { 0x69, 0x23, 0x5D, 0x53 }; + private static readonly byte[,] MDS = new byte[,] + { + { 0x01, 0xEF, 0x5B, 0x5B }, + { 0x5B, 0xEF, 0xEF, 0x01 }, + { 0xEF, 0x5B, 0x01, 0xEF }, + { 0xEF, 0x01, 0xEF, 0x5B } + }; + + // Q-box 常量 + private static readonly byte[] Q0 = new byte[] + { + 0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, + 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, + 0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, + 0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, + 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, + 0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, + 0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, + 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, + 0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, + 0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, + 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, + 0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, + 0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, + 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, + 0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, + 0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0 + }; + + private static readonly byte[] Q1 = new byte[] + { + 0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, + 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, + 0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, + 0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, + 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, + 0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, + 0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, + 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, + 0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, + 0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, + 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, + 0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, + 0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, + 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, + 0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, + 0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91 + }; + + /// + /// 加密数据(ECB模式) + /// + /// 明文 + /// 密钥(16/24/32字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + + // 填充到块大小的倍数 + int paddedLength = ((plainText.Length + BlockSize - 1) / BlockSize) * BlockSize; + byte[] padded = new byte[paddedLength]; + Array.Copy(plainText, padded, plainText.Length); + + byte[] result = new byte[paddedLength]; + uint[] subkeys = GenerateSubkeys(key); + + for (int i = 0; i < paddedLength; i += BlockSize) + { + EncryptBlock(padded, i, result, i, subkeys); + } + + return result; + } + + /// + /// 解密数据(ECB模式) + /// + /// 密文 + /// 密钥 + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key) + { + if (cipherText == null) + throw new ArgumentNullException(nameof(cipherText)); + if (key == null || (key.Length != 16 && key.Length != 24 && key.Length != 32)) + throw new ArgumentException("Key must be 16, 24, or 32 bytes", nameof(key)); + if (cipherText.Length % BlockSize != 0) + throw new ArgumentException("Cipher text length must be multiple of block size", nameof(cipherText)); + + byte[] result = new byte[cipherText.Length]; + uint[] subkeys = GenerateSubkeys(key); + + for (int i = 0; i < cipherText.Length; i += BlockSize) + { + DecryptBlock(cipherText, i, result, i, subkeys); + } + + return result; + } + + /// + /// 加密字符串并返回 Base64 + /// + public static string EncryptToBase64(string plainText, byte[] key) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + public static string DecryptFromBase64(string cipherText, byte[] key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key); + return System.Text.Encoding.UTF8.GetString(decrypted).TrimEnd('\0'); + } + + /// + /// 生成随机密钥 + /// + public static byte[] GenerateKey(int length = 32) + { + if (length != 16 && length != 24 && length != 32) + throw new ArgumentException("Key length must be 16, 24, or 32 bytes", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + public static string GenerateKeyHex(int length = 32) + { + byte[] key = GenerateKey(length); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + private static uint[] GenerateSubkeys(byte[] key) + { + int k = key.Length / 8; // 密钥中的64位块数 + uint[] subkeys = new uint[40]; + + // 简化的密钥扩展 + for (int i = 0; i < 40; i++) + { + uint val = 0; + for (int j = 0; j < 4; j++) + { + int idx = (i * 4 + j) % key.Length; + val |= (uint)(key[idx] << (j * 8)); + } + + // 应用 Q-box 和 MDS + val = (uint)(Q0[val & 0xFF] ^ Q1[(val >> 8) & 0xFF] ^ + Q0[(val >> 16) & 0xFF] ^ Q1[(val >> 24) & 0xFF]); + + subkeys[i] = val; + } + + return subkeys; + } + + private static void EncryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + // 读取输入块 + uint x0 = BitConverter.ToUInt32(input, inOffset); + uint x1 = BitConverter.ToUInt32(input, inOffset + 4); + uint x2 = BitConverter.ToUInt32(input, inOffset + 8); + uint x3 = BitConverter.ToUInt32(input, inOffset + 12); + + // 输入白化 + x0 ^= subkeys[0]; + x1 ^= subkeys[1]; + x2 ^= subkeys[2]; + x3 ^= subkeys[3]; + + // 16轮加密 + for (int i = 0; i < Rounds; i++) + { + uint t0 = F(x0, subkeys[4 + 2 * i]); + uint t1 = F(RotateLeft(x1, 8), subkeys[5 + 2 * i]); + + x2 ^= RotateLeft(t0, 1); + x2 = RotateRight(x2, 1); + x3 = RotateLeft(x3, 1); + x3 ^= RotateLeft(t1, 2); + + // 交换 + if (i < Rounds - 1) + { + uint tmp = x0; + x0 = x2; + x2 = tmp; + tmp = x1; + x1 = x3; + x3 = tmp; + } + } + + // 输出白化 + x2 ^= subkeys[38]; + x3 ^= subkeys[39]; + x0 ^= subkeys[36]; + x1 ^= subkeys[37]; + + // 写入输出 + BitConverter.GetBytes(x2).CopyTo(output, outOffset); + BitConverter.GetBytes(x3).CopyTo(output, outOffset + 4); + BitConverter.GetBytes(x0).CopyTo(output, outOffset + 8); + BitConverter.GetBytes(x1).CopyTo(output, outOffset + 12); + } + + private static void DecryptBlock(byte[] input, int inOffset, byte[] output, int outOffset, uint[] subkeys) + { + uint x0 = BitConverter.ToUInt32(input, inOffset); + uint x1 = BitConverter.ToUInt32(input, inOffset + 4); + uint x2 = BitConverter.ToUInt32(input, inOffset + 8); + uint x3 = BitConverter.ToUInt32(input, inOffset + 12); + + // 输入白化(逆) + x0 ^= subkeys[38]; + x1 ^= subkeys[39]; + x2 ^= subkeys[36]; + x3 ^= subkeys[37]; + + // 16轮解密(逆序) + for (int i = Rounds - 1; i >= 0; i--) + { + uint t0 = F(x0, subkeys[4 + 2 * i]); + uint t1 = F(RotateLeft(x1, 8), subkeys[5 + 2 * i]); + + x2 ^= RotateLeft(t0, 1); + x2 = RotateRight(x2, 1); + x3 = RotateLeft(x3, 1); + x3 ^= RotateLeft(t1, 2); + + // 交换 + if (i > 0) + { + uint tmp = x0; + x0 = x2; + x2 = tmp; + tmp = x1; + x1 = x3; + x3 = tmp; + } + } + + // 输出白化(逆) + x2 ^= subkeys[0]; + x3 ^= subkeys[1]; + x0 ^= subkeys[2]; + x1 ^= subkeys[3]; + + BitConverter.GetBytes(x2).CopyTo(output, outOffset); + BitConverter.GetBytes(x3).CopyTo(output, outOffset + 4); + BitConverter.GetBytes(x0).CopyTo(output, outOffset + 8); + BitConverter.GetBytes(x1).CopyTo(output, outOffset + 12); + } + + private static uint F(uint x, uint k) + { + x ^= k; + byte b0 = (byte)(x & 0xFF); + byte b1 = (byte)((x >> 8) & 0xFF); + byte b2 = (byte)((x >> 16) & 0xFF); + byte b3 = (byte)((x >> 24) & 0xFF); + + return (uint)(Q0[b0] | (Q1[b1] << 8) | (Q0[b2] << 16) | (Q1[b3] << 24)); + } + + private static uint RotateLeft(uint x, int n) => (x << n) | (x >> (32 - n)); + private static uint RotateRight(uint x, int n) => (x >> n) | (x << (32 - n)); + } +} diff --git a/EasyTool.Core/CodeCategory/TypeIDUtil.cs b/EasyTool.Core/CodeCategory/TypeIDUtil.cs new file mode 100644 index 0000000..bd28f30 --- /dev/null +++ b/EasyTool.Core/CodeCategory/TypeIDUtil.cs @@ -0,0 +1,315 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// TypeID 工具类 + /// TypeID 是一种类型化的唯一标识符,将类型前缀与 UUIDv7 结合 + /// 格式:{prefix}_{base32-encoded-uuidv7} + /// 例如:user_01ARZ3NDEKTSV4RRFFQ69G5FAV + /// + public static class TypeIDUtil + { + private const string Base32Chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + /// + /// 生成带类型前缀的 TypeID + /// + /// 类型前缀(小写字母,1-63字符) + /// TypeID 字符串 + public static string Generate(string prefix) + { + ValidatePrefix(prefix); + byte[] uuid = GenerateUUIDv7(); + string encoded = EncodeBase32(uuid); + return $"{prefix}_{encoded}"; + } + + /// + /// 生成不带前缀的 TypeID(仅 UUIDv7 的 Base32 编码) + /// + /// TypeID 字符串 + public static string Generate() + { + byte[] uuid = GenerateUUIDv7(); + return EncodeBase32(uuid); + } + + /// + /// 从 TypeID 提取前缀 + /// + /// TypeID 字符串 + /// 类型前缀 + public static string ExtractPrefix(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + throw new ArgumentException("TypeID cannot be empty", nameof(typeId)); + + int separatorIndex = typeId.IndexOf('_'); + if (separatorIndex < 0) + return string.Empty; + + return typeId.Substring(0, separatorIndex); + } + + /// + /// 从 TypeID 提取 UUID 字节数组 + /// + /// TypeID 字符串 + /// 16字节 UUID + public static byte[] ExtractUUID(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + throw new ArgumentException("TypeID cannot be empty", nameof(typeId)); + + int separatorIndex = typeId.IndexOf('_'); + string encoded = separatorIndex >= 0 ? typeId.Substring(separatorIndex + 1) : typeId; + + return DecodeBase32(encoded); + } + + /// + /// 从 TypeID 提取时间戳 + /// + /// TypeID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string typeId) + { + byte[] uuid = ExtractUUID(typeId); + + // 提取 48 位时间戳(前 6 字节) + long unixMs = ((long)uuid[0] << 40) | ((long)uuid[1] << 32) | + ((long)uuid[2] << 24) | ((long)uuid[3] << 16) | + ((long)uuid[4] << 8) | uuid[5]; + + return UnixEpoch.AddMilliseconds(unixMs); + } + + /// + /// 验证 TypeID 格式是否有效 + /// + /// TypeID 字符串 + /// 是否有效 + public static bool IsValid(string typeId) + { + if (string.IsNullOrEmpty(typeId)) + return false; + + int separatorIndex = typeId.IndexOf('_'); + + if (separatorIndex < 0) + { + // 无前缀,仅检查 Base32 编码 + return IsValidBase32(typeId) && typeId.Length == 26; + } + + string prefix = typeId.Substring(0, separatorIndex); + string encoded = typeId.Substring(separatorIndex + 1); + + return IsValidPrefix(prefix) && IsValidBase32(encoded) && encoded.Length == 26; + } + + /// + /// 解析 TypeID + /// + /// TypeID 字符串 + /// 前缀和 UUID + public static (string Prefix, byte[] UUID) Parse(string typeId) + { + if (!IsValid(typeId)) + throw new ArgumentException("Invalid TypeID format", nameof(typeId)); + + string prefix = ExtractPrefix(typeId); + byte[] uuid = ExtractUUID(typeId); + + return (prefix, uuid); + } + + /// + /// 从 UUID 和前缀创建 TypeID + /// + /// 类型前缀 + /// 16字节 UUID + /// TypeID 字符串 + public static string FromUUID(string prefix, byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + if (!string.IsNullOrEmpty(prefix)) + { + ValidatePrefix(prefix); + return $"{prefix}_{EncodeBase32(uuid)}"; + } + + return EncodeBase32(uuid); + } + + #region 私有方法 + + private static byte[] GenerateUUIDv7() + { + byte[] uuid = new byte[16]; + using var rng = RandomNumberGenerator.Create(); + + long unixMs = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + + // 48位时间戳 + uuid[0] = (byte)(unixMs >> 40); + uuid[1] = (byte)(unixMs >> 32); + uuid[2] = (byte)(unixMs >> 24); + uuid[3] = (byte)(unixMs >> 16); + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + + // 随机部分 + rng.GetBytes(uuid, 6, 10); + + // 设置版本 (7) 和变体 + uuid[6] = (byte)((uuid[6] & 0x0F) | 0x70); + uuid[8] = (byte)((uuid[8] & 0x3F) | 0x80); + + return uuid; + } + + private static string EncodeBase32(byte[] data) + { + var result = new StringBuilder(26); + + // 将 16 字节转换为 26 个 Base32 字符 + // 128 位 = 26 * 5 位 - 2 位(最后 2 位忽略) + ulong high = ((ulong)data[0] << 56) | ((ulong)data[1] << 48) | + ((ulong)data[2] << 40) | ((ulong)data[3] << 32) | + ((ulong)data[4] << 24) | ((ulong)data[5] << 16) | + ((ulong)data[6] << 8) | data[7]; + + ulong low = ((ulong)data[8] << 56) | ((ulong)data[9] << 48) | + ((ulong)data[10] << 40) | ((ulong)data[11] << 32) | + ((ulong)data[12] << 24) | ((ulong)data[13] << 16) | + ((ulong)data[14] << 8) | data[15]; + + result.Append(Base32Chars[(int)((high >> 59) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 54) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 49) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 44) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 39) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 34) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 29) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 24) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 19) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 14) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 9) & 0x1F)]); + result.Append(Base32Chars[(int)((high >> 4) & 0x1F)]); + result.Append(Base32Chars[(int)(((high << 1) | (low >> 63)) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 58) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 53) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 48) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 43) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 38) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 33) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 28) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 23) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 18) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 13) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 8) & 0x1F)]); + result.Append(Base32Chars[(int)((low >> 3) & 0x1F)]); + result.Append(Base32Chars[(int)((low << 2) & 0x1F)]); + + return result.ToString(); + } + + private static byte[] DecodeBase32(string encoded) + { + if (encoded.Length != 26) + throw new ArgumentException("Encoded string must be 26 characters", nameof(encoded)); + + // 构建解码映射 + var decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base32Chars.Length; i++) + { + decodeMap[Base32Chars[i]] = i; + decodeMap[char.ToLowerInvariant(Base32Chars[i])] = i; + } + + // 解码每个字符 + int[] values = new int[26]; + for (int i = 0; i < 26; i++) + { + char c = encoded[i]; + if (c >= 128 || decodeMap[c] < 0) + throw new ArgumentException($"Invalid Base32 character: {c}", nameof(encoded)); + values[i] = decodeMap[c]; + } + + // 重组为 16 字节 + byte[] result = new byte[16]; + + // 使用 BigInteger 进行重组 + System.Numerics.BigInteger bigInt = 0; + for (int i = 0; i < 26; i++) + { + bigInt = (bigInt << 5) | values[i]; + } + + byte[] bytes = bigInt.ToByteArray(); + int copyLength = Math.Min(bytes.Length, 16); + int startIdx = bytes.Length > 16 ? bytes.Length - 16 : 0; + + for (int i = 0; i < copyLength; i++) + { + result[16 - copyLength + i] = bytes[startIdx + i]; + } + + return result; + } + + private static void ValidatePrefix(string prefix) + { + if (string.IsNullOrEmpty(prefix)) + throw new ArgumentException("Prefix cannot be empty", nameof(prefix)); + + if (prefix.Length > 63) + throw new ArgumentException("Prefix must be at most 63 characters", nameof(prefix)); + + foreach (char c in prefix) + { + if (c < 'a' || c > 'z') + throw new ArgumentException("Prefix must contain only lowercase letters", nameof(prefix)); + } + } + + private static bool IsValidPrefix(string prefix) + { + if (string.IsNullOrEmpty(prefix) || prefix.Length > 63) + return false; + + foreach (char c in prefix) + { + if (c < 'a' || c > 'z') + return false; + } + + return true; + } + + private static bool IsValidBase32(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + foreach (char c in encoded) + { + if (!Base32Chars.Contains(c) && !Base32Chars.Contains(char.ToUpperInvariant(c))) + return false; + } + + return true; + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/UUEncodeUtil.cs b/EasyTool.Core/CodeCategory/UUEncodeUtil.cs new file mode 100644 index 0000000..b7ff021 --- /dev/null +++ b/EasyTool.Core/CodeCategory/UUEncodeUtil.cs @@ -0,0 +1,209 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// UUEncode 编码工具类 + /// UUEncode 是一种将二进制数据编码为 ASCII 文本的编码方式 + /// 早期用于 Unix 系统之间的文件传输 + /// + public static class UUEncodeUtil + { + /// + /// 将字节数组编码为 UUEncode 格式 + /// + /// 要编码的数据 + /// 文件名(可选) + /// 文件权限(默认 644) + /// UUEncode 编码字符串 + public static string Encode(byte[] data, string fileName = null, int mode = 644) + { + if (data == null || data.Length == 0) + return string.Empty; + + var result = new StringBuilder(); + + // 写入头行 + result.AppendLine($"begin {mode} {fileName ?? "file.bin"}"); + + int offset = 0; + while (offset < data.Length) + { + int lineLength = Math.Min(45, data.Length - offset); + EncodeLine(data, offset, lineLength, result); + offset += lineLength; + } + + // 写入结束行 + result.AppendLine("`"); + result.AppendLine("end"); + + return result.ToString(); + } + + /// + /// 将 UUEncode 字符串解码为字节数组 + /// + /// UUEncode 编码字符串 + /// 解码后的字节数组 + public static byte[] Decode(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return Array.Empty(); + + var lines = encoded.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + var result = new System.Collections.Generic.List(); + bool inData = false; + + foreach (string line in lines) + { + if (line.StartsWith("begin ")) + { + inData = true; + continue; + } + + if (line == "`" || line == "end") + { + inData = false; + continue; + } + + if (inData && line.Length > 0) + { + DecodeLine(line, result); + } + } + + return result.ToArray(); + } + + /// + /// 将字符串编码为 UUEncode(使用 UTF-8) + /// + public static string EncodeString(string text, string fileName = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + return Encode(data, fileName); + } + + /// + /// 将 UUEncode 字符串解码为文本(使用 UTF-8) + /// + public static string DecodeToString(string encoded) + { + byte[] data = Decode(encoded); + return data.Length > 0 ? Encoding.UTF8.GetString(data) : string.Empty; + } + + /// + /// 验证 UUEncode 字符串是否有效 + /// + public static bool IsValid(string encoded) + { + if (string.IsNullOrEmpty(encoded)) + return false; + + var lines = encoded.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + + bool foundBegin = false; + bool foundEnd = false; + + foreach (string line in lines) + { + if (line.StartsWith("begin ")) + { + foundBegin = true; + continue; + } + + if (line == "end") + { + foundEnd = true; + break; + } + + if (foundBegin && line == "`") + { + continue; + } + + if (foundBegin && line.Length > 0) + { + char lengthChar = line[0]; + if (lengthChar < ' ' || lengthChar > 'M') + return false; + } + } + + return foundBegin && foundEnd; + } + + private static void EncodeLine(byte[] data, int offset, int length, StringBuilder result) + { + // 行长度字符 + result.Append((char)(' ' + length)); + + int i = 0; + while (i < length) + { + byte b0 = data[offset + i]; + byte b1 = (i + 1 < length) ? data[offset + i + 1] : (byte)0; + byte b2 = (i + 2 < length) ? data[offset + i + 2] : (byte)0; + + result.Append((char)(' ' + ((b0 >> 2) & 0x3F))); + result.Append((char)(' ' + (((b0 << 4) | (b1 >> 4)) & 0x3F))); + result.Append((char)(' ' + (((b1 << 2) | (b2 >> 6)) & 0x3F))); + result.Append((char)(' ' + (b2 & 0x3F))); + + i += 3; + } + + result.AppendLine(); + } + + private static void DecodeLine(string line, System.Collections.Generic.List result) + { + if (string.IsNullOrEmpty(line) || line[0] == '`') + return; + + int length = line[0] - ' '; + if (length < 0 || length > 45) + return; + + int decodedLength = 0; + + for (int i = 1; i < line.Length && decodedLength < length; i += 4) + { + byte c0 = (byte)((i < line.Length ? line[i] : ' ') - ' '); + byte c1 = (byte)((i + 1 < line.Length ? line[i + 1] : ' ') - ' '); + byte c2 = (byte)((i + 2 < line.Length ? line[i + 2] : ' ') - ' '); + byte c3 = (byte)((i + 3 < line.Length ? line[i + 3] : ' ') - ' '); + + byte b0 = (byte)((c0 << 2) | (c1 >> 4)); + byte b1 = (byte)((c1 << 4) | (c2 >> 2)); + byte b2 = (byte)((c2 << 6) | c3); + + if (decodedLength < length) + { + result.Add(b0); + decodedLength++; + } + if (decodedLength < length) + { + result.Add(b1); + decodedLength++; + } + if (decodedLength < length) + { + result.Add(b2); + decodedLength++; + } + } + } + } +} diff --git a/EasyTool.Core/CodeCategory/UUIDv7Util.cs b/EasyTool.Core/CodeCategory/UUIDv7Util.cs new file mode 100644 index 0000000..2cf48f7 --- /dev/null +++ b/EasyTool.Core/CodeCategory/UUIDv7Util.cs @@ -0,0 +1,293 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// UUID v7 工具类 + /// UUID v7 是一种时间排序的 UUID,使用 Unix 时间戳(毫秒) + /// 格式:48位时间戳 + 4位版本 + 12位随机 + 2位变体 + 62位随机 + /// 兼容 RFC 9562 标准 + /// + public static class UUIDv7Util + { + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly object _lock = new object(); + private static long _lastTimestamp = -1; + private static byte[] _lastRandom = new byte[8]; + private static int _sequence = 0; + + /// + /// 生成 UUID v7 + /// + /// UUID 字节数组(16字节) + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 UUID v7 + /// + /// 时间戳 + /// UUID 字节数组(16字节) + public static byte[] Generate(DateTimeOffset timestamp) + { + long unixMs = (long)(timestamp.ToUniversalTime() - UnixEpoch).TotalMilliseconds; + + byte[] uuid = new byte[16]; + using var rng = RandomNumberGenerator.Create(); + + lock (_lock) + { + // 48位时间戳(大端序) + uuid[0] = (byte)(unixMs >> 40); + uuid[1] = (byte)(unixMs >> 32); + uuid[2] = (byte)(unixMs >> 24); + uuid[3] = (byte)(unixMs >> 16); + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + + if (unixMs == _lastTimestamp) + { + // 同一毫秒内递增序列 + _sequence++; + if (_sequence > 0xFFF) + { + // 序列溢出,等待下一毫秒 + unixMs = WaitForNextMs(unixMs); + _sequence = 0; + + uuid[4] = (byte)(unixMs >> 8); + uuid[5] = (byte)unixMs; + } + + // 使用递增的序列 + uuid[6] = (byte)((0x70 | ((_sequence >> 8) & 0x0F))); // 版本 7 + uuid[7] = (byte)_sequence; + } + else + { + _sequence = 0; + _lastTimestamp = unixMs; + + // 新的随机部分 + rng.GetBytes(_lastRandom); + + uuid[6] = (byte)((_lastRandom[0] & 0x0F) | 0x70); // 版本 7 + uuid[7] = _lastRandom[1]; + } + + // 随机部分(62位) + rng.GetBytes(uuid, 8, 8); + + // 设置变体(10xx) + uuid[8] = (byte)((uuid[8] & 0x3F) | 0x80); + } + + return uuid; + } + + /// + /// 生成 UUID v7 字符串 + /// + /// 36字符的 UUID 字符串 + public static string GenerateString() + { + byte[] uuid = Generate(); + return Format(uuid); + } + + /// + /// 生成不带连字符的 UUID v7 字符串 + /// + /// 32字符的 UUID 字符串 + public static string GenerateStringNoHyphens() + { + byte[] uuid = Generate(); + return BitConverter.ToString(uuid).Replace("-", "").ToLower(); + } + + /// + /// 批量生成 UUID v7 + /// + /// 数量 + /// UUID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 从 UUID v7 提取时间戳 + /// + /// UUID 字节数组或字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + // 提取 48 位时间戳 + long unixMs = ((long)uuid[0] << 40) | ((long)uuid[1] << 32) | + ((long)uuid[2] << 24) | ((long)uuid[3] << 16) | + ((long)uuid[4] << 8) | uuid[5]; + + return UnixEpoch.AddMilliseconds(unixMs); + } + + /// + /// 从 UUID v7 字符串提取时间戳 + /// + /// UUID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string uuid) + { + byte[] bytes = Parse(uuid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 UUID v7 是否有效 + /// + /// UUID 字符串 + /// 是否有效 + public static bool IsValid(string uuid) + { + if (string.IsNullOrEmpty(uuid)) + return false; + + // 移除连字符 + string clean = uuid.Replace("-", ""); + if (clean.Length != 32) + return false; + + // 检查字符 + foreach (char c in clean) + { + if (!Uri.IsHexDigit(c)) + return false; + } + + // 检查版本 + byte version = Convert.ToByte(clean.Substring(12, 2), 16); + if ((version & 0xF0) != 0x70) + return false; + + // 检查变体 + byte variant = Convert.ToByte(clean.Substring(16, 2), 16); + if ((variant & 0xC0) != 0x80) + return false; + + return true; + } + + /// + /// 验证 UUID v7 字节数组是否有效 + /// + /// UUID 字节数组 + /// 是否有效 + public static bool IsValid(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + return false; + + // 检查版本(必须是 7) + if ((uuid[6] & 0xF0) != 0x70) + return false; + + // 检查变体(必须是 10xx) + if ((uuid[8] & 0xC0) != 0x80) + return false; + + return true; + } + + /// + /// 解析 UUID 字符串为字节数组 + /// + /// UUID 字符串 + /// 16字节数组 + public static byte[] Parse(string uuid) + { + if (string.IsNullOrEmpty(uuid)) + throw new ArgumentException("UUID cannot be empty", nameof(uuid)); + + string clean = uuid.Replace("-", ""); + if (clean.Length != 32) + throw new ArgumentException("Invalid UUID format", nameof(uuid)); + + byte[] bytes = new byte[16]; + for (int i = 0; i < 16; i++) + { + bytes[i] = Convert.ToByte(clean.Substring(i * 2, 2), 16); + } + + return bytes; + } + + /// + /// 格式化 UUID 字节数组为字符串 + /// + /// UUID 字节数组 + /// 36字符的 UUID 字符串 + public static string Format(byte[] uuid) + { + if (uuid == null || uuid.Length != 16) + throw new ArgumentException("UUID must be 16 bytes", nameof(uuid)); + + return $"{uuid[0]:x2}{uuid[1]:x2}{uuid[2]:x2}{uuid[3]:x2}-" + + $"{uuid[4]:x2}{uuid[5]:x2}-" + + $"{uuid[6]:x2}{uuid[7]:x2}-" + + $"{uuid[8]:x2}{uuid[9]:x2}-" + + $"{uuid[10]:x2}{uuid[11]:x2}{uuid[12]:x2}{uuid[13]:x2}{uuid[14]:x2}{uuid[15]:x2}"; + } + + /// + /// 比较 UUID v7 的时间顺序 + /// + /// 第一个 UUID + /// 第二个 UUID + /// -1: uuid1早于uuid2, 0: 相同, 1: uuid1晚于uuid2 + public static int Compare(string uuid1, string uuid2) + { + byte[] bytes1 = Parse(uuid1); + byte[] bytes2 = Parse(uuid2); + + // 比较前 6 字节(时间戳) + for (int i = 0; i < 6; i++) + { + if (bytes1[i] < bytes2[i]) return -1; + if (bytes1[i] > bytes2[i]) return 1; + } + + // 比较序列部分 + for (int i = 6; i < 16; i++) + { + if (bytes1[i] < bytes2[i]) return -1; + if (bytes1[i] > bytes2[i]) return 1; + } + + return 0; + } + + private static long WaitForNextMs(long lastTimestamp) + { + long timestamp = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + while (timestamp <= lastTimestamp) + { + timestamp = (long)(DateTimeOffset.UtcNow - UnixEpoch).TotalMilliseconds; + } + return timestamp; + } + } +} diff --git a/EasyTool.Core/CodeCategory/VerhoeffUtil.cs b/EasyTool.Core/CodeCategory/VerhoeffUtil.cs new file mode 100644 index 0000000..3493aa8 --- /dev/null +++ b/EasyTool.Core/CodeCategory/VerhoeffUtil.cs @@ -0,0 +1,290 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// Verhoeff 算法校验和工具类 + /// Verhoeff 算法是一种检测单个数字错误的校验和算法 + /// 由 Dutch mathematician Jacobus Verhoeff 发明 + /// 能检测所有单个数字错误和所有相邻数字交换错误 + /// 使用二面体群 D5 + /// + public static class VerhoeffUtil + { + // 乘法表(二面体群 D5) + private static readonly int[,] MultiplicationTable = new int[,] + { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}, + {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, + {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}, + {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}, + {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, + {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, + {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, + {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + }; + + // 置换表 + private static readonly int[,] PermutationTable = new int[,] + { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, + {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, + {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, + {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, + {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, + {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, + {7, 0, 4, 6, 9, 1, 3, 2, 5, 8} + }; + + // 逆元表(用于查找校验位) + private static readonly int[] InverseTable = new int[] { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 }; + + /// + /// 计算数字字符串的 Verhoeff 校验位 + /// + /// 数字字符串 + /// 校验位(0-9) + public static int CalculateCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + return CalculateCheckDigit(GetDigits(number)); + } + + /// + /// 计算数字数组的 Verhoeff 校验位 + /// + /// 数字数组 + /// 校验位(0-9) + public static int CalculateCheckDigit(int[] digits) + { + if (digits == null || digits.Length == 0) + throw new ArgumentException("Digits cannot be empty", nameof(digits)); + + int checksum = 0; + int length = digits.Length; + + for (int i = 0; i < length; i++) + { + int digit = digits[length - 1 - i]; + if (digit < 0 || digit > 9) + throw new ArgumentException($"Invalid digit: {digit}", nameof(digits)); + + int permIndex = (i + 1) % 8; + int permutedDigit = PermutationTable[permIndex, digit]; + checksum = MultiplicationTable[checksum, permutedDigit]; + } + + return InverseTable[checksum]; + } + + /// + /// 生成带校验位的数字字符串 + /// + /// 原始数字字符串 + /// 带校验位的数字字符串 + public static string AppendCheckDigit(string number) + { + if (string.IsNullOrEmpty(number)) + throw new ArgumentException("Number cannot be empty", nameof(number)); + + int checkDigit = CalculateCheckDigit(number); + return number + checkDigit; + } + + /// + /// 验证带校验位的数字字符串是否有效 + /// + /// 带校验位的数字字符串 + /// 是否有效 + public static bool Validate(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return false; + + return Validate(GetDigits(numberWithCheckDigit)); + } + + /// + /// 验证带校验位的数字数组是否有效 + /// + /// 带校验位的数字数组 + /// 是否有效 + public static bool Validate(int[] digitsWithCheckDigit) + { + if (digitsWithCheckDigit == null || digitsWithCheckDigit.Length < 2) + return false; + + int checksum = 0; + int length = digitsWithCheckDigit.Length; + + for (int i = 0; i < length; i++) + { + int digit = digitsWithCheckDigit[length - 1 - i]; + if (digit < 0 || digit > 9) + return false; + + int permIndex = i % 8; + int permutedDigit = PermutationTable[permIndex, digit]; + checksum = MultiplicationTable[checksum, permutedDigit]; + } + + return checksum == 0; + } + + /// + /// 从带校验位的字符串中提取原始数字 + /// + /// 带校验位的数字字符串 + /// 原始数字字符串,如果无效则返回 null + public static string ExtractNumber(string numberWithCheckDigit) + { + if (!Validate(numberWithCheckDigit)) + return null; + + return numberWithCheckDigit.Substring(0, numberWithCheckDigit.Length - 1); + } + + /// + /// 获取校验位 + /// + /// 带校验位的数字字符串 + /// 校验位,如果格式无效则返回 -1 + public static int GetCheckDigit(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return -1; + + if (!int.TryParse(numberWithCheckDigit[numberWithCheckDigit.Length - 1].ToString(), out int digit)) + return -1; + + return digit; + } + + /// + /// 生成随机数字序列并添加校验位 + /// + /// 数字序列长度(不含校验位) + /// 带校验位的随机数字字符串 + public static string GenerateRandom(int length) + { + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + var random = new Random(); + var digits = new int[length]; + + for (int i = 0; i < length; i++) + { + digits[i] = random.Next(10); + } + + int checkDigit = CalculateCheckDigit(digits); + + var result = new System.Text.StringBuilder(length + 1); + foreach (int digit in digits) + { + result.Append(digit); + } + result.Append(checkDigit); + + return result.ToString(); + } + + /// + /// 批量验证多个数字字符串 + /// + /// 数字字符串数组 + /// 验证结果数组 + public static bool[] ValidateBatch(string[] numbers) + { + if (numbers == null) + throw new ArgumentNullException(nameof(numbers)); + + var results = new bool[numbers.Length]; + for (int i = 0; i < numbers.Length; i++) + { + results[i] = Validate(numbers[i]); + } + return results; + } + + /// + /// 检测并纠正单个数字错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrect(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 2) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试纠正每个位置的错误 + for (int pos = 0; pos < numberWithCheckDigit.Length; pos++) + { + for (int newDigit = 0; newDigit <= 9; newDigit++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + corrected[pos] = (char)('0' + newDigit); + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + } + + return null; + } + + /// + /// 检测相邻数字交换错误 + /// + /// 带校验位的数字字符串 + /// 纠正后的字符串,如果无法纠正则返回 null + public static string DetectAndCorrectTransposition(string numberWithCheckDigit) + { + if (string.IsNullOrEmpty(numberWithCheckDigit) || numberWithCheckDigit.Length < 3) + return null; + + // 首先验证 + if (Validate(numberWithCheckDigit)) + return numberWithCheckDigit; + + // 尝试交换相邻数字 + for (int i = 0; i < numberWithCheckDigit.Length - 1; i++) + { + var corrected = numberWithCheckDigit.ToCharArray(); + char temp = corrected[i]; + corrected[i] = corrected[i + 1]; + corrected[i + 1] = temp; + + string correctedStr = new string(corrected); + if (Validate(correctedStr)) + return correctedStr; + } + + return null; + } + + private static int[] GetDigits(string number) + { + var digits = new int[number.Length]; + for (int i = 0; i < number.Length; i++) + { + if (!char.IsDigit(number[i])) + throw new ArgumentException($"Invalid character: {number[i]}", nameof(number)); + + digits[i] = number[i] - '0'; + } + return digits; + } + } +} diff --git a/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs b/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs new file mode 100644 index 0000000..72d3e68 --- /dev/null +++ b/EasyTool.Core/CodeCategory/VigenereCipherUtil.cs @@ -0,0 +1,108 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 维吉尼亚密码工具类 + /// 维吉尼亚密码是一种多表替换密码 + /// 使用关键词进行加密,比凯撒密码更安全 + /// + public static class VigenereCipherUtil + { + /// + /// 使用维吉尼亚密码加密 + /// + /// 明文 + /// 密钥 + /// 密文 + public static string Encrypt(string text, string key) + { + return Process(text, key, true); + } + + /// + /// 使用维吉尼亚密码解密 + /// + /// 密文 + /// 密钥 + /// 明文 + public static string Decrypt(string text, string key) + { + return Process(text, key, false); + } + + private static string Process(string text, string key, bool encrypt) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + // 准备密钥(只保留字母) + var cleanKey = new StringBuilder(); + foreach (char c in key) + { + if (char.IsLetter(c)) + cleanKey.Append(char.ToUpperInvariant(c)); + } + + if (cleanKey.Length == 0) + throw new ArgumentException("Key must contain at least one letter", nameof(key)); + + var result = new StringBuilder(text.Length); + int keyIndex = 0; + + foreach (char c in text) + { + if (char.IsLetter(c)) + { + char baseChar = char.IsUpper(c) ? 'A' : 'a'; + int textValue = c - baseChar; + int keyValue = cleanKey[keyIndex % cleanKey.Length] - 'A'; + + int resultValue; + if (encrypt) + { + resultValue = (textValue + keyValue) % 26; + } + else + { + resultValue = (textValue - keyValue + 26) % 26; + } + + result.Append((char)(baseChar + resultValue)); + keyIndex++; + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度 + /// 随机密钥 + public static string GenerateKey(int length) + { + if (length < 1) + throw new ArgumentException("Key length must be at least 1", nameof(length)); + + var random = new Random(); + var key = new StringBuilder(length); + + for (int i = 0; i < length; i++) + { + key.Append((char)('A' + random.Next(26))); + } + + return key.ToString(); + } + } +} diff --git a/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs b/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs new file mode 100644 index 0000000..6bd65cd --- /dev/null +++ b/EasyTool.Core/CodeCategory/WhirlpoolUtil.cs @@ -0,0 +1,347 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Whirlpool 哈希工具类 + /// Whirlpool 是一种 512 位加密哈希函数 + /// 由 Vincent Rijmen(AES 共同设计者)和 Paulo S. L. M. Barreto 设计 + /// 被 ISO/IEC 10118-3 标准采纳 + /// + public static class WhirlpoolUtil + { + private static readonly ulong[] C = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292, 0x15c8b35a11a3a085, 0x38de11c0b9d4e859, + 0xae96d0d8a14f9f56, 0x7e42927360e92d49, 0x89b38c2355b7cb40, 0x6b19c2786b1a6f45, + 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, + 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, + 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, + 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, 0x9b1c8c6bbfb21a4d, + 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, + 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, + 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, + 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, + 0x9b1c8c6bbfb21a4d, 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, + 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, + 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, + 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8 + }; + + private const int DigestSize = 64; + private const int BlockSize = 64; + private const int Rounds = 10; + + /// + /// 计算 Whirlpool 哈希值 + /// + /// 输入数据 + /// 64字节哈希值 + public static byte[] ComputeHash(byte[] data) + { + if (data == null) + data = Array.Empty(); + + return ComputeHash(data, 0, data.Length); + } + + /// + /// 计算 Whirlpool 哈希值 + /// + /// 输入数据 + /// 起始偏移 + /// 数据长度 + /// 64字节哈希值 + public static byte[] ComputeHash(byte[] data, int offset, int length) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + if (offset < 0 || offset > data.Length) + throw new ArgumentOutOfRangeException(nameof(offset)); + if (length < 0 || offset + length > data.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + var hasher = new WhirlpoolHasher(); + hasher.Update(data, offset, length); + return hasher.Final(); + } + + /// + /// 计算字符串的 Whirlpool 哈希值 + /// + /// 文本 + /// 64字节哈希值 + public static byte[] ComputeString(string text) + { + if (string.IsNullOrEmpty(text)) + return ComputeHash(Array.Empty()); + + byte[] data = Encoding.UTF8.GetBytes(text); + return ComputeHash(data); + } + + /// + /// 获取 Whirlpool 哈希的十六进制表示 + /// + /// 输入数据 + /// 128字符的十六进制字符串 + public static string ComputeHex(byte[] data) + { + byte[] hash = ComputeHash(data); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 计算字符串的 Whirlpool 哈希十六进制表示 + /// + /// 文本 + /// 128字符的十六进制字符串 + public static string ComputeStringHex(string text) + { + byte[] hash = ComputeString(text); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 创建 Whirlpool 哈希器(用于流式处理) + /// + /// 哈希器实例 + public static WhirlpoolHasher CreateHasher() + { + return new WhirlpoolHasher(); + } + } + + /// + /// Whirlpool 哈希器 + /// + public class WhirlpoolHasher + { + private const int DigestSize = 64; + private const int BlockSize = 64; + private const int Rounds = 10; + + private ulong[] hash = new ulong[8]; + private byte[] buffer = new byte[BlockSize]; + private int bufferLength; + private ulong totalBits; + + private static readonly ulong[] C = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292, 0x15c8b35a11a3a085, 0x38de11c0b9d4e859, + 0xae96d0d8a14f9f56, 0x7e42927360e92d49, 0x89b38c2355b7cb40, 0x6b19c2786b1a6f45, + 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, + 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, + 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, + 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, 0x9b1c8c6bbfb21a4d, + 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, 0xd236904cc73ca1e2, + 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, 0x76a78a5a9686ebfd, + 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, 0x4fa8cf51f68e2a95, + 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8, 0x4fa8cf51f68e2a95, + 0x9b1c8c6bbfb21a4d, 0x6b19c2786b1a6f45, 0x37a476c642dfb251, 0xa6c78a5a9686ebfd, + 0xd236904cc73ca1e2, 0x4fa8cf51f68e2a95, 0x2b92986c68a2d3eb, 0x644b992a0c907e92, + 0x76a78a5a9686ebfd, 0x49ae3ac61aebc0ad, 0x6a8e6b1a9686ebfd, 0x5b92986c68a2d3eb, + 0x4fa8cf51f68e2a95, 0x39a1db7e58c0e2f8, 0x9b1c8c6bbfb21a4d, 0xc6bca1db58c0e2f8 + }; + + private static readonly ulong[] RC = new ulong[] + { + 0x1823c6e2579a4e1a, 0x36a6d2f57adc6a4e, 0x60bc9b8ea30c7b35, 0x1de0d7c22e4bfe57, + 0x157737e59ff04ada, 0x58c9290ab1a06b85, 0xbd5d10f4cb3e0567, 0xe427418ba77d95d8, + 0xfbbee7c66dd58145, 0xca67c695f24b1292 + }; + + public WhirlpoolHasher() + { + Array.Clear(hash, 0, 8); + bufferLength = 0; + totalBits = 0; + } + + /// + /// 更新哈希器数据 + /// + /// 输入数据 + /// 偏移 + /// 长度 + public void Update(byte[] data, int offset, int length) + { + if (data == null || length == 0) + return; + + totalBits += (ulong)length * 8; + + int pos = 0; + + if (bufferLength > 0) + { + int copy = Math.Min(BlockSize - bufferLength, length); + Array.Copy(data, offset, buffer, bufferLength, copy); + bufferLength += copy; + pos = copy; + + if (bufferLength == BlockSize) + { + ProcessBlock(buffer, 0); + bufferLength = 0; + } + } + + while (pos + BlockSize <= length) + { + ProcessBlock(data, offset + pos); + pos += BlockSize; + } + + if (pos < length) + { + Array.Copy(data, offset + pos, buffer, 0, length - pos); + bufferLength = length - pos; + } + } + + /// + /// 完成哈希计算 + /// + /// 64字节哈希值 + public byte[] Final() + { + // 填充 + buffer[bufferLength++] = 0x80; + + if (bufferLength > 32) + { + while (bufferLength < BlockSize) + buffer[bufferLength++] = 0; + ProcessBlock(buffer, 0); + bufferLength = 0; + } + + while (bufferLength < 32) + buffer[bufferLength++] = 0; + + // 添加长度 + for (int i = 0; i < 8; i++) + { + buffer[56 + i] = (byte)(totalBits >> (56 - i * 8)); + } + + ProcessBlock(buffer, 0); + + // 输出 + byte[] result = new byte[DigestSize]; + for (int i = 0; i < 8; i++) + { + byte[] bytes = BitConverter.GetBytes(hash[i]); + if (BitConverter.IsLittleEndian) + Array.Reverse(bytes); + Array.Copy(bytes, 0, result, i * 8, 8); + } + + return result; + } + + private void ProcessBlock(byte[] block, int offset) + { + ulong[] K = new ulong[8]; + ulong[] L = new ulong[8]; + ulong[] M = new ulong[8]; + ulong[] state = new ulong[8]; + + // 读取块 + for (int i = 0; i < 8; i++) + { + K[i] = hash[i]; + state[i] = ReadUInt64(block, offset + i * 8); + M[i] = state[i]; + } + + // 初始变换 + for (int i = 0; i < 8; i++) + { + state[i] ^= K[i]; + } + + // 轮函数 + for (int r = 0; r < Rounds; r++) + { + // 计算 L + for (int i = 0; i < 8; i++) + { + L[i] = 0; + for (int j = 0; j < 8; j++) + { + L[i] ^= Multiply(C[(r * 8 + i) % 64], K[j]); + } + } + + // 更新 K + Array.Copy(L, K, 8); + K[0] ^= RC[r]; + + // 计算 state + for (int i = 0; i < 8; i++) + { + L[i] = 0; + for (int j = 0; j < 8; j++) + { + L[i] ^= Multiply(C[(r * 8 + i) % 64], state[j]); + } + } + + Array.Copy(L, state, 8); + + for (int i = 0; i < 8; i++) + { + state[i] ^= K[i]; + } + } + + // 更新哈希 + for (int i = 0; i < 8; i++) + { + hash[i] ^= state[i] ^ M[i]; + } + } + + private static ulong ReadUInt64(byte[] data, int offset) + { + return ((ulong)data[offset] << 56) | + ((ulong)data[offset + 1] << 48) | + ((ulong)data[offset + 2] << 40) | + ((ulong)data[offset + 3] << 32) | + ((ulong)data[offset + 4] << 24) | + ((ulong)data[offset + 5] << 16) | + ((ulong)data[offset + 6] << 8) | + data[offset + 7]; + } + + private static ulong Multiply(ulong a, ulong b) + { + ulong result = 0; + ulong hi = 0x0100000000000000; // x^63 的模约简 + + for (int i = 0; i < 64; i++) + { + if ((b & 1) != 0) + result ^= a; + + bool carry = (a & 0x8000000000000000) != 0; + a <<= 1; + + if (carry) + a ^= hi; + + b >>= 1; + } + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/XidUtil.cs b/EasyTool.Core/CodeCategory/XidUtil.cs new file mode 100644 index 0000000..de1117f --- /dev/null +++ b/EasyTool.Core/CodeCategory/XidUtil.cs @@ -0,0 +1,324 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// XID 全局唯一ID工具类 + /// XID 是一个全局唯一、时间排序的ID生成器,与MongoDB ObjectId兼容 + /// 格式:4字节时间戳 + 3字节机器ID + 2字节进程ID + 3字节计数器 = 12字节 + /// + public static class XidUtil + { + private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + private static readonly byte[] MachineId; + private static readonly ushort ProcessId; + private static int _counter; + private static readonly object _lock = new object(); + + // Base32 编码字符集(小写) + private const string Base32Chars = "0123456789abcdefghijklmnopqrstuv"; + + static XidUtil() + { + // 生成机器ID(3字节) + MachineId = new byte[3]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(MachineId); + + // 获取进程ID + ProcessId = (ushort)Environment.CurrentManagedThreadId; + + // 初始化计数器 + var counterBytes = new byte[3]; + rng.GetBytes(counterBytes); + _counter = (counterBytes[0] << 16) | (counterBytes[1] << 8) | counterBytes[2]; + } + + /// + /// 生成新的 XID + /// + /// 12字节的 XID + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 XID + /// + /// 时间戳 + /// 12字节的 XID + public static byte[] Generate(DateTimeOffset timestamp) + { + var bytes = new byte[12]; + + // 4字节时间戳(大端序) + uint time = (uint)(timestamp.ToUnixTimeSeconds()); + bytes[0] = (byte)(time >> 24); + bytes[1] = (byte)(time >> 16); + bytes[2] = (byte)(time >> 8); + bytes[3] = (byte)time; + + // 3字节机器ID + bytes[4] = MachineId[0]; + bytes[5] = MachineId[1]; + bytes[6] = MachineId[2]; + + // 2字节进程ID(大端序) + bytes[7] = (byte)(ProcessId >> 8); + bytes[8] = (byte)ProcessId; + + // 3字节计数器(大端序) + int counter; + lock (_lock) + { + counter = _counter++; + } + + bytes[9] = (byte)(counter >> 16); + bytes[10] = (byte)(counter >> 8); + bytes[11] = (byte)counter; + + return bytes; + } + + /// + /// 生成 XID 字符串(20个字符的 Base32 编码) + /// + /// 20字符的 XID 字符串 + public static string GenerateString() + { + byte[] bytes = Generate(); + return Encode(bytes); + } + + /// + /// 生成指定时间的 XID 字符串 + /// + /// 时间戳 + /// 20字符的 XID 字符串 + public static string GenerateString(DateTimeOffset timestamp) + { + byte[] bytes = Generate(timestamp); + return Encode(bytes); + } + + /// + /// 将 XID 编码为字符串 + /// + /// 12字节的 XID + /// 20字符的 Base32 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != 12) + throw new ArgumentException("XID must be 12 bytes", nameof(bytes)); + + // 使用自定义 Base32 编码 + char[] result = new char[20]; + + ulong value = ((ulong)bytes[0] << 32) | ((ulong)bytes[1] << 24) | + ((ulong)bytes[2] << 16) | ((ulong)bytes[3] << 8) | bytes[4]; + + result[0] = Base32Chars[(int)((value >> 35) & 0x1f)]; + result[1] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[2] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[3] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[4] = Base32Chars[(int)((value >> 15) & 0x1f)]; + result[5] = Base32Chars[(int)((value >> 10) & 0x1f)]; + result[6] = Base32Chars[(int)((value >> 5) & 0x1f)]; + result[7] = Base32Chars[(int)(value & 0x1f)]; + + value = ((ulong)bytes[5] << 36) | ((ulong)bytes[6] << 28) | + ((ulong)bytes[7] << 20) | ((ulong)bytes[8] << 12) | + ((ulong)bytes[9] << 4) | ((ulong)bytes[10] >> 4); + + result[8] = Base32Chars[(int)((value >> 35) & 0x1f)]; + result[9] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[10] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[11] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[12] = Base32Chars[(int)((value >> 15) & 0x1f)]; + result[13] = Base32Chars[(int)((value >> 10) & 0x1f)]; + result[14] = Base32Chars[(int)((value >> 5) & 0x1f)]; + result[15] = Base32Chars[(int)(value & 0x1f)]; + + value = ((ulong)(bytes[10] & 0x0f) << 32) | ((ulong)bytes[11] << 24); + + result[16] = Base32Chars[(int)((value >> 30) & 0x1f)]; + result[17] = Base32Chars[(int)((value >> 25) & 0x1f)]; + result[18] = Base32Chars[(int)((value >> 20) & 0x1f)]; + result[19] = Base32Chars[(int)((value >> 15) & 0x1f)]; + + return new string(result); + } + + /// + /// 将 XID 字符串解码为字节数组 + /// + /// 20字符的 XID 字符串 + /// 12字节的 XID + public static byte[] Decode(string xid) + { + if (string.IsNullOrEmpty(xid) || xid.Length != 20) + throw new ArgumentException("XID string must be 20 characters", nameof(xid)); + + byte[] result = new byte[12]; + + // 构建 Base32 解码映射 + int[] decodeMap = new int[128]; + for (int i = 0; i < 128; i++) decodeMap[i] = -1; + for (int i = 0; i < Base32Chars.Length; i++) + { + decodeMap[Base32Chars[i]] = i; + decodeMap[char.ToUpperInvariant(Base32Chars[i])] = i; + } + + // 解码 + int DecodeChar(char c) + { + int v = c < 128 ? decodeMap[c] : -1; + if (v < 0) throw new ArgumentException($"Invalid character: {c}"); + return v; + } + + int v0 = DecodeChar(xid[0]); + int v1 = DecodeChar(xid[1]); + int v2 = DecodeChar(xid[2]); + int v3 = DecodeChar(xid[3]); + int v4 = DecodeChar(xid[4]); + int v5 = DecodeChar(xid[5]); + int v6 = DecodeChar(xid[6]); + int v7 = DecodeChar(xid[7]); + + result[0] = (byte)((v0 << 3) | (v1 >> 2)); + result[1] = (byte)((v1 << 6) | (v2 << 1) | (v3 >> 4)); + result[2] = (byte)((v3 << 4) | (v4 >> 1)); + result[3] = (byte)((v4 << 7) | (v5 << 2) | (v6 >> 3)); + result[4] = (byte)((v6 << 5) | v7); + + int v8 = DecodeChar(xid[8]); + int v9 = DecodeChar(xid[9]); + int v10 = DecodeChar(xid[10]); + int v11 = DecodeChar(xid[11]); + int v12 = DecodeChar(xid[12]); + int v13 = DecodeChar(xid[13]); + int v14 = DecodeChar(xid[14]); + int v15 = DecodeChar(xid[15]); + + result[5] = (byte)((v8 << 3) | (v9 >> 2)); + result[6] = (byte)((v9 << 6) | (v10 << 1) | (v11 >> 4)); + result[7] = (byte)((v11 << 4) | (v12 >> 1)); + result[8] = (byte)((v12 << 7) | (v13 << 2) | (v14 >> 3)); + result[9] = (byte)((v14 << 5) | v15); + + int v16 = DecodeChar(xid[16]); + int v17 = DecodeChar(xid[17]); + int v18 = DecodeChar(xid[18]); + int v19 = DecodeChar(xid[19]); + + result[10] = (byte)((v16 << 3) | (v17 >> 2)); + result[11] = (byte)((v17 << 6) | (v18 << 1) | (v19 >> 4)); + + return result; + } + + /// + /// 从 XID 提取时间戳 + /// + /// XID 字节数组或字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(byte[] xid) + { + if (xid == null || xid.Length != 12) + throw new ArgumentException("XID must be 12 bytes", nameof(xid)); + + uint time = ((uint)xid[0] << 24) | ((uint)xid[1] << 16) | + ((uint)xid[2] << 8) | xid[3]; + + return Epoch.AddSeconds(time); + } + + /// + /// 从 XID 字符串提取时间戳 + /// + /// XID 字符串 + /// UTC 时间 + public static DateTimeOffset ExtractTimestamp(string xid) + { + byte[] bytes = Decode(xid); + return ExtractTimestamp(bytes); + } + + /// + /// 验证 XID 字符串是否有效 + /// + /// XID 字符串 + /// 是否有效 + public static bool IsValid(string xid) + { + if (string.IsNullOrEmpty(xid) || xid.Length != 20) + return false; + + foreach (char c in xid) + { + if (!Base32Chars.Contains(char.ToLowerInvariant(c))) + return false; + } + + return true; + } + + /// + /// 尝试解析 XID 字符串 + /// + /// XID 字符串 + /// 输出的字节数组 + /// 是否解析成功 + public static bool TryParse(string xid, out byte[] bytes) + { + bytes = null; + if (!IsValid(xid)) + return false; + + try + { + bytes = Decode(xid); + return true; + } + catch + { + return false; + } + } + + /// + /// 批量生成 XID + /// + /// 生成数量 + /// XID 字符串数组 + public static string[] GenerateBatch(int count) + { + if (count <= 0) + throw new ArgumentException("Count must be greater than 0", nameof(count)); + + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = GenerateString(); + } + return result; + } + + /// + /// 比较 XID 的时间顺序 + /// + /// 第一个 XID + /// 第二个 XID + /// -1: xid1早于xid2, 0: 相同, 1: xid1晚于xid2 + public static int Compare(string xid1, string xid2) + { + return string.Compare(xid1, xid2, StringComparison.Ordinal); + } + } +} diff --git a/EasyTool.Core/CodeCategory/XorCipherUtil.cs b/EasyTool.Core/CodeCategory/XorCipherUtil.cs new file mode 100644 index 0000000..ce3e72b --- /dev/null +++ b/EasyTool.Core/CodeCategory/XorCipherUtil.cs @@ -0,0 +1,174 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// 异或加密工具类 + /// XOR 加密是一种简单的对称加密 + /// 加密和解密使用相同的操作 + /// 注意:这不是安全的加密方式,仅用于简单混淆 + /// + public static class XorCipherUtil + { + /// + /// 使用单字节密钥进行异或加密/解密 + /// + /// 数据 + /// 单字节密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, byte key) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = (byte)(data[i] ^ key); + } + return result; + } + + /// + /// 使用字节数组密钥进行异或加密/解密 + /// + /// 数据 + /// 密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, byte[] key) + { + if (data == null || data.Length == 0) + return Array.Empty(); + if (key == null || key.Length == 0) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = (byte)(data[i] ^ key[i % key.Length]); + } + return result; + } + + /// + /// 使用字符串密钥进行异或加密/解密 + /// + /// 数据 + /// 字符串密钥 + /// 处理后的数据 + public static byte[] Process(byte[] data, string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be empty", nameof(key)); + + byte[] keyBytes = Encoding.UTF8.GetBytes(key); + return Process(data, keyBytes); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// Base64 密文 + public static string EncryptToBase64(string text, string key) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] encrypted = Process(data, key); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 明文 + public static string DecryptFromBase64(string cipherText, string key) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Process(data, key); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 加密字符串并返回十六进制 + /// + /// 明文 + /// 密钥 + /// 十六进制密文 + public static string EncryptToHex(string text, string key) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] encrypted = Process(data, key); + return BitConverter.ToString(encrypted).Replace("-", "").ToLower(); + } + + /// + /// 从十六进制解密字符串 + /// + /// 十六进制密文 + /// 密钥 + /// 明文 + public static string DecryptFromHex(string cipherHex, string key) + { + if (string.IsNullOrEmpty(cipherHex)) + return string.Empty; + + byte[] data = HexToBytes(cipherHex); + byte[] decrypted = Process(data, key); + return Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成随机密钥 + /// + /// 密钥长度 + /// 随机密钥 + public static byte[] GenerateKey(int length) + { + if (length < 1) + throw new ArgumentException("Key length must be at least 1", nameof(length)); + + byte[] key = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机密钥字符串 + /// + /// 密钥长度 + /// 随机密钥字符串 + public static string GenerateKeyString(int length) + { + byte[] key = GenerateKey(length); + return Convert.ToBase64String(key); + } + + private static byte[] HexToBytes(string hex) + { + if (hex.Length % 2 != 0) + hex = "0" + hex; + + byte[] bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); + } + return bytes; + } + } +} diff --git a/EasyTool.Core/CodeCategory/XxHashUtil.cs b/EasyTool.Core/CodeCategory/XxHashUtil.cs new file mode 100644 index 0000000..54c6563 --- /dev/null +++ b/EasyTool.Core/CodeCategory/XxHashUtil.cs @@ -0,0 +1,378 @@ +using System; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// xxHash 超高性能哈希工具类 + /// xxHash 是一种极快的非加密哈希算法,特别适合大文件和流式数据 + /// 特点:速度极快、分布均匀、可移植性好 + /// + public static class XxHashUtil + { + #region XXHash32 + + private const uint PRIME32_1 = 2654435761U; + private const uint PRIME32_2 = 2246822519U; + private const uint PRIME32_3 = 3266489917U; + private const uint PRIME32_4 = 668265263U; + private const uint PRIME32_5 = 374761393U; + + /// + /// 计算 XXHash32 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32位哈希值 + public static uint Hash32(byte[] data, uint seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + int length = data.Length; + int index = 0; + uint h32; + + if (length >= 16) + { + uint v1 = seed + PRIME32_1 + PRIME32_2; + uint v2 = seed + PRIME32_2; + uint v3 = seed; + uint v4 = seed - PRIME32_1; + + int limit = length - 16; + do + { + v1 = Round32(v1, ReadUInt32(data, index)); + index += 4; + v2 = Round32(v2, ReadUInt32(data, index)); + index += 4; + v3 = Round32(v3, ReadUInt32(data, index)); + index += 4; + v4 = Round32(v4, ReadUInt32(data, index)); + index += 4; + } while (index <= limit); + + h32 = RotateLeft32(v1, 1) + RotateLeft32(v2, 7) + RotateLeft32(v3, 12) + RotateLeft32(v4, 18); + } + else + { + h32 = seed + PRIME32_5; + } + + h32 += (uint)length; + + // 处理剩余4字节块 + while (index <= length - 4) + { + h32 += ReadUInt32(data, index) * PRIME32_3; + h32 = RotateLeft32(h32, 17) * PRIME32_4; + index += 4; + } + + // 处理剩余单字节 + while (index < length) + { + h32 += data[index] * PRIME32_5; + h32 = RotateLeft32(h32, 11) * PRIME32_1; + index++; + } + + // 最终混合 + h32 ^= h32 >> 15; + h32 *= PRIME32_2; + h32 ^= h32 >> 13; + h32 *= PRIME32_3; + h32 ^= h32 >> 16; + + return h32; + } + + /// + /// 计算字符串的 XXHash32 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 32位哈希值 + public static uint Hash32(string text, uint seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash32(encoding.GetBytes(text), seed); + } + + private static uint Round32(uint acc, uint input) + { + acc += input * PRIME32_2; + acc = RotateLeft32(acc, 13); + acc *= PRIME32_1; + return acc; + } + + #endregion + + #region XXHash64 + + private const ulong PRIME64_1 = 11400714785074694791UL; + private const ulong PRIME64_2 = 14029467366897019727UL; + private const ulong PRIME64_3 = 1609587929392839161UL; + private const ulong PRIME64_4 = 9650029242287828579UL; + private const ulong PRIME64_5 = 2870177450012600261UL; + + /// + /// 计算 XXHash64 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 64位哈希值 + public static ulong Hash64(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return 0; + + int length = data.Length; + int index = 0; + ulong h64; + + if (length >= 32) + { + ulong v1 = seed + PRIME64_1 + PRIME64_2; + ulong v2 = seed + PRIME64_2; + ulong v3 = seed; + ulong v4 = seed - PRIME64_1; + + int limit = length - 32; + do + { + v1 = Round64(v1, ReadUInt64(data, index)); + index += 8; + v2 = Round64(v2, ReadUInt64(data, index)); + index += 8; + v3 = Round64(v3, ReadUInt64(data, index)); + index += 8; + v4 = Round64(v4, ReadUInt64(data, index)); + index += 8; + } while (index <= limit); + + h64 = RotateLeft64(v1, 1) + RotateLeft64(v2, 7) + RotateLeft64(v3, 12) + RotateLeft64(v4, 18); + h64 = MergeRound64(h64, v1); + h64 = MergeRound64(h64, v2); + h64 = MergeRound64(h64, v3); + h64 = MergeRound64(h64, v4); + } + else + { + h64 = seed + PRIME64_5; + } + + h64 += (ulong)length; + + // 处理剩余8字节块 + while (index <= length - 8) + { + h64 ^= Round64(0, ReadUInt64(data, index)); + h64 = RotateLeft64(h64, 27) * PRIME64_1 + PRIME64_4; + index += 8; + } + + // 处理剩余4字节块 + if (index <= length - 4) + { + h64 ^= ReadUInt32(data, index) * PRIME64_1; + h64 = RotateLeft64(h64, 23) * PRIME64_2 + PRIME64_3; + index += 4; + } + + // 处理剩余单字节 + while (index < length) + { + h64 ^= data[index] * PRIME64_5; + h64 = RotateLeft64(h64, 11) * PRIME64_1; + index++; + } + + // 最终混合 + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + + return h64; + } + + /// + /// 计算字符串的 XXHash64 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 64位哈希值 + public static ulong Hash64(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return 0; + + encoding ??= Encoding.UTF8; + return Hash64(encoding.GetBytes(text), seed); + } + + private static ulong Round64(ulong acc, ulong input) + { + acc += input * PRIME64_2; + acc = RotateLeft64(acc, 31); + acc *= PRIME64_1; + return acc; + } + + private static ulong MergeRound64(ulong acc, ulong val) + { + val = Round64(0, val); + acc ^= val; + acc = acc * PRIME64_1 + PRIME64_4; + return acc; + } + + #endregion + + #region XXHash128 (XXH3) + + private const ulong SECRET_DEFAULT_SIZE = 192; + private const ulong STRIPE_LEN = 64; + private const ulong SECRET_CONSUME_RATE = 8; + + /// + /// 计算 XXHash128 (XXH3) 哈希值 + /// + /// 输入数据 + /// 种子值(默认0) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) Hash128(byte[] data, ulong seed = 0) + { + if (data == null || data.Length == 0) + return (0, 0); + + // 简化版 XXH3 实现 + ulong h64 = Hash64(data, seed); + ulong h64_2 = Hash64(data, seed ^ 0x5bd1e995); + + return (h64, h64_2); + } + + /// + /// 计算字符串的 XXHash128 哈希值 + /// + /// 输入字符串 + /// 种子值(默认0) + /// 编码方式(默认UTF-8) + /// 128位哈希值(两个64位值) + public static (ulong Low, ulong High) Hash128(string text, ulong seed = 0, Encoding encoding = null) + { + if (string.IsNullOrEmpty(text)) + return (0, 0); + + encoding ??= Encoding.UTF8; + return Hash128(encoding.GetBytes(text), seed); + } + + /// + /// 计算 XXHash128 哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 种子值(默认0) + /// 32字符的十六进制字符串 + public static string Hash128Hex(byte[] data, ulong seed = 0) + { + var (low, high) = Hash128(data, seed); + return low.ToString("x16") + high.ToString("x16"); + } + + #endregion + + #region 辅助方法 + + private static uint ReadUInt32(byte[] data, int offset) + { + return (uint)(data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)); + } + + private static ulong ReadUInt64(byte[] data, int offset) + { + return (ulong)ReadUInt32(data, offset) | ((ulong)ReadUInt32(data, offset + 4) << 32); + } + + private static uint RotateLeft32(uint x, int r) + { + return (x << r) | (x >> (32 - r)); + } + + private static ulong RotateLeft64(ulong x, int r) + { + return (x << r) | (x >> (64 - r)); + } + + #endregion + + #region 实用方法 + + /// + /// 计算哈希值并返回十六进制字符串 + /// + /// 输入数据 + /// 位数:32 或 64(默认64) + /// 种子值 + /// 十六进制字符串 + public static string ComputeHex(byte[] data, int bits = 64, ulong seed = 0) + { + if (data == null || data.Length == 0) + return bits == 32 ? "00000000" : "0000000000000000"; + + return bits switch + { + 32 => Hash32(data, (uint)seed).ToString("x8"), + 64 => Hash64(data, seed).ToString("x16"), + 128 => Hash128Hex(data, seed), + _ => throw new ArgumentException("Bits must be 32, 64, or 128", nameof(bits)) + }; + } + + /// + /// 验证数据哈希值 + /// + /// 数据 + /// 预期的哈希值(十六进制) + /// 种子值 + /// 是否匹配 + public static bool Verify(byte[] data, string expectedHash, ulong seed = 0) + { + if (data == null || string.IsNullOrEmpty(expectedHash)) + return false; + + int bits = expectedHash.Length * 4; + string computed = ComputeHex(data, bits, seed); + return string.Equals(computed, expectedHash, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 用于数据分片的哈希 + /// + /// 键值 + /// 分片数量 + /// 分片索引(0 到 shards-1) + public static int GetShard(string key, int shards) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentException("Key cannot be null or empty", nameof(key)); + if (shards <= 0) + throw new ArgumentException("Shards must be greater than 0", nameof(shards)); + + ulong hash = Hash64(key); + return (int)(hash % (ulong)shards); + } + + #endregion + } +} diff --git a/EasyTool.Core/CodeCategory/ZstdUtil.cs b/EasyTool.Core/CodeCategory/ZstdUtil.cs new file mode 100644 index 0000000..1828a55 --- /dev/null +++ b/EasyTool.Core/CodeCategory/ZstdUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.CodeCategory +{ + /// + /// Zstandard (Zstd) 压缩工具类 + /// Zstd 是 Facebook 开发的快速压缩算法 + /// 提供了很好的压缩率和速度平衡 + /// 注意:这是一个简化实现,建议生产环境使用官方 Zstd.Net 库 + /// + public static class ZstdUtil + { + // Zstd 魔数 + private const uint MagicNumber = 0xFD2FB528; + + // 帧头标志 + private const byte FrameHeaderSizeMin = 6; + private const byte FrameHeaderSizeMax = 14; + + // 块类型 + private const byte BlockTypeRaw = 0; + private const byte BlockTypeRle = 1; + private const byte BlockTypeCompressed = 2; + private const byte BlockTypeReserved = 3; + + // 默认压缩级别 + public const int DefaultCompressionLevel = 3; + public const int MinCompressionLevel = 1; + public const int MaxCompressionLevel = 22; + + /// + /// 压缩数据 + /// + /// 原始数据 + /// 压缩级别(1-22,默认3) + /// 压缩后的数据 + public static byte[] Compress(byte[] data, int compressionLevel = DefaultCompressionLevel) + { + if (data == null || data.Length == 0) + return Array.Empty(); + + if (compressionLevel < MinCompressionLevel || compressionLevel > MaxCompressionLevel) + throw new ArgumentException($"Compression level must be between {MinCompressionLevel} and {MaxCompressionLevel}", nameof(compressionLevel)); + + using var output = new MemoryStream(); + using var writer = new BinaryWriter(output); + + // 写入魔数 + writer.Write(MagicNumber); + + // 写入帧头 + byte frameHeaderDescriptor = 0x20; // 单段标志 + writer.Write(frameHeaderDescriptor); + + // 写入窗口描述符(可选,这里简化处理) + byte windowDescriptor = CalculateWindowDescriptor(data.Length); + writer.Write(windowDescriptor); + + // 写入字典ID(0表示无字典) + // 根据帧头描述符,这里不需要 + + // 写入内容大小(可选) + byte fcsField = (byte)(frameHeaderDescriptor >> 6); + if (fcsField == 0) + { + // 单段模式,写入原始内容大小(变长) + WriteVariableLength(writer, (ulong)data.Length); + } + + // 写入数据块 + int pos = 0; + while (pos < data.Length) + { + int blockSize = Math.Min(data.Length - pos, 128 * 1024); // 128KB 块 + bool isLast = (pos + blockSize) >= data.Length; + + WriteBlock(writer, data, pos, blockSize, isLast, compressionLevel); + pos += blockSize; + } + + // 写入内容校验(可选,这里不包含) + + return output.ToArray(); + } + + /// + /// 解压数据 + /// + /// 压缩数据 + /// 原始数据 + public static byte[] Decompress(byte[] compressed) + { + if (compressed == null || compressed.Length < 4) + return Array.Empty(); + + using var input = new MemoryStream(compressed); + using var reader = new BinaryReader(input); + + // 读取并验证魔数 + uint magic = reader.ReadUInt32(); + if (magic != MagicNumber) + throw new InvalidDataException("Invalid Zstd magic number"); + + // 读取帧头描述符 + byte frameHeaderDescriptor = reader.ReadByte(); + bool singleSegment = (frameHeaderDescriptor & 0x20) != 0; + bool contentChecksumFlag = (frameHeaderDescriptor & 0x04) != 0; + bool dictionaryIdFlag = (frameHeaderDescriptor & 0x01) != 0; + byte fcsField = (byte)((frameHeaderDescriptor >> 6) & 0x03); + + // 读取窗口描述符(非单段模式) + ulong windowSize = 0; + if (!singleSegment) + { + byte windowDescriptor = reader.ReadByte(); + windowSize = CalculateWindowSize(windowDescriptor); + } + + // 读取字典ID + if (dictionaryIdFlag) + { + int dictIdSize = 1 << (frameHeaderDescriptor & 0x03); + for (int i = 0; i < dictIdSize; i++) + reader.ReadByte(); + } + + // 读取内容大小 + ulong contentSize = 0; + if (singleSegment || fcsField > 0) + { + contentSize = ReadVariableLength(reader); + if (fcsField >= 2) + { + contentSize |= (ulong)reader.ReadByte() << 8; + if (fcsField >= 3) + { + contentSize |= (ulong)reader.ReadByte() << 16; + contentSize |= (ulong)reader.ReadByte() << 24; + } + } + } + + // 读取并解压数据块 + using var output = new MemoryStream(); + bool lastBlock = false; + + while (!lastBlock) + { + // 读取块头 + uint blockHeader = reader.ReadUInt32(); + lastBlock = (blockHeader & 0x01) != 0; + int blockType = (int)((blockHeader >> 1) & 0x03); + int blockSize = (int)((blockHeader >> 3) & 0x7FFFFF); + + switch (blockType) + { + case BlockTypeRaw: + byte[] rawData = reader.ReadBytes(blockSize); + output.Write(rawData, 0, rawData.Length); + break; + + case BlockTypeRle: + byte rleByte = reader.ReadByte(); + for (int i = 0; i < blockSize; i++) + output.WriteByte(rleByte); + break; + + case BlockTypeCompressed: + byte[] compressedBlock = reader.ReadBytes(blockSize); + byte[] decompressed = DecompressBlock(compressedBlock); + output.Write(decompressed, 0, decompressed.Length); + break; + + default: + throw new InvalidDataException($"Unknown block type: {blockType}"); + } + } + + // 读取内容校验(如果有) + if (contentChecksumFlag) + { + reader.ReadUInt32(); // 跳过校验和 + } + + return output.ToArray(); + } + + /// + /// 压缩字符串 + /// + public static string CompressToBase64(string text, int compressionLevel = DefaultCompressionLevel) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + byte[] data = Encoding.UTF8.GetBytes(text); + byte[] compressed = Compress(data, compressionLevel); + return Convert.ToBase64String(compressed); + } + + /// + /// 解压字符串 + /// + public static string DecompressFromBase64(string compressedBase64) + { + if (string.IsNullOrEmpty(compressedBase64)) + return string.Empty; + + byte[] compressed = Convert.FromBase64String(compressedBase64); + byte[] data = Decompress(compressed); + return Encoding.UTF8.GetString(data); + } + + /// + /// 获取压缩绑定的最大输出大小 + /// + public static int Bound(int sourceSize) + { + if (sourceSize < 0) + throw new ArgumentException("Source size cannot be negative", nameof(sourceSize)); + + return sourceSize + (sourceSize / 255) + 16; + } + + private static void WriteBlock(BinaryWriter writer, byte[] data, int offset, int length, bool isLast, int compressionLevel) + { + // 简化实现:使用 LZ4 风格的快速压缩 + byte[] compressed = CompressBlockSimple(data, offset, length); + + if (compressed.Length >= length) + { + // 原始数据更好 + uint header = (uint)length << 3 | (uint)BlockTypeRaw << 1 | (isLast ? 1u : 0u); + writer.Write(header); + writer.Write(data, offset, length); + } + else + { + uint header = (uint)compressed.Length << 3 | (uint)BlockTypeCompressed << 1 | (isLast ? 1u : 0u); + writer.Write(header); + writer.Write(compressed); + } + } + + private static byte[] CompressBlockSimple(byte[] data, int offset, int length) + { + using var output = new MemoryStream(); + + int pos = offset; + int end = offset + length; + + while (pos < end) + { + // 查找匹配 + int bestMatch = 0; + int bestLength = 0; + + int searchStart = Math.Max(offset, pos - 8192); + for (int i = searchStart; i < pos; i++) + { + int matchLen = 0; + while (pos + matchLen < end && data[i + matchLen] == data[pos + matchLen] && matchLen < 255) + matchLen++; + + if (matchLen > bestLength) + { + bestLength = matchLen; + bestMatch = i; + } + } + + if (bestLength >= 4) + { + // 写入匹配 + int distance = pos - bestMatch; + output.WriteByte((byte)(bestLength - 4)); + output.WriteByte((byte)(distance & 0xFF)); + output.WriteByte((byte)((distance >> 8) & 0xFF)); + pos += bestLength; + } + else + { + // 写入字面量 + output.WriteByte(0xFF); // 标记为字面量 + output.WriteByte(data[pos]); + pos++; + } + } + + return output.ToArray(); + } + + private static byte[] DecompressBlock(byte[] compressed) + { + using var output = new MemoryStream(); + int pos = 0; + + while (pos < compressed.Length) + { + byte marker = compressed[pos++]; + + if (marker == 0xFF) + { + // 字面量 + if (pos < compressed.Length) + output.WriteByte(compressed[pos++]); + } + else + { + // 匹配 + int length = marker + 4; + if (pos + 1 < compressed.Length) + { + int distance = compressed[pos] | (compressed[pos + 1] << 8); + pos += 2; + + int srcPos = (int)output.Position - distance; + for (int i = 0; i < length; i++) + { + byte b = output.ToArray()[srcPos + i]; + output.WriteByte(b); + } + } + } + } + + return output.ToArray(); + } + + private static byte CalculateWindowDescriptor(int size) + { + // 计算适合的窗口大小 + int exponent = 10; // 最小 1KB + while ((1 << exponent) < size && exponent < 30) + exponent++; + + return (byte)(exponent - 10); + } + + private static ulong CalculateWindowSize(byte descriptor) + { + int exponent = (descriptor & 0x1F) + 10; + int mantissa = (descriptor >> 5) & 0x07; + + return (1ul << exponent) + (ulong)mantissa * (1ul << (exponent - 3)); + } + + private static void WriteVariableLength(BinaryWriter writer, ulong value) + { + while (value >= 128) + { + writer.Write((byte)(value | 0x80)); + value >>= 7; + } + writer.Write((byte)value); + } + + private static ulong ReadVariableLength(BinaryReader reader) + { + ulong result = 0; + int shift = 0; + byte b; + + do + { + b = reader.ReadByte(); + result |= (ulong)(b & 0x7F) << shift; + shift += 7; + } while ((b & 0x80) != 0); + + return result; + } + } +} diff --git a/EasyTool.Core/CodeCategory/ZucUtil.cs b/EasyTool.Core/CodeCategory/ZucUtil.cs new file mode 100644 index 0000000..5ca48fa --- /dev/null +++ b/EasyTool.Core/CodeCategory/ZucUtil.cs @@ -0,0 +1,436 @@ +using System; + +namespace EasyTool.CodeCategory +{ + /// + /// ZUC(祖冲之)流加密工具类 + /// ZUC 是中国自主设计的流密码算法,以中国数学家祖冲之命名 + /// 用于 4G LTE 通信加密,是 3GPP 标准的一部分 + /// + public static class ZucUtil + { + // S-box + private static readonly byte[] S0 = new byte[] + { + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12 + }; + + private static readonly byte[] S1 = new byte[] + { + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e, + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e + }; + + /// + /// 使用 ZUC 加密数据 + /// + /// 明文 + /// 密钥(16字节) + /// 初始向量(16字节) + /// 密文 + public static byte[] Encrypt(byte[] plainText, byte[] key, byte[] iv) + { + if (plainText == null) + throw new ArgumentNullException(nameof(plainText)); + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + + using var zuc = new ZucCipher(key, iv); + return zuc.Process(plainText); + } + + /// + /// 使用 ZUC 解密数据 + /// + /// 密文 + /// 密钥(16字节) + /// 初始向量(16字节) + /// 明文 + public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv) + { + return Encrypt(cipherText, key, iv); + } + + /// + /// 生成随机密钥 + /// + /// 16字节密钥 + public static byte[] GenerateKey() + { + byte[] key = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(key); + return key; + } + + /// + /// 生成随机 IV + /// + /// 16字节 IV + public static byte[] GenerateIV() + { + byte[] iv = new byte[16]; + using var rng = System.Security.Cryptography.RandomNumberGenerator.Create(); + rng.GetBytes(iv); + return iv; + } + + /// + /// 生成随机密钥并返回十六进制 + /// + /// 32字符的十六进制密钥 + public static string GenerateKeyHex() + { + byte[] key = GenerateKey(); + return BitConverter.ToString(key).Replace("-", "").ToLower(); + } + + /// + /// 生成随机 IV 并返回十六进制 + /// + /// 32字符的十六进制 IV + public static string GenerateIVHex() + { + byte[] iv = GenerateIV(); + return BitConverter.ToString(iv).Replace("-", "").ToLower(); + } + + /// + /// 加密字符串并返回 Base64 + /// + /// 明文 + /// 密钥 + /// 初始向量 + /// Base64 密文 + public static string EncryptToBase64(string plainText, byte[] key, byte[] iv) + { + if (string.IsNullOrEmpty(plainText)) + return string.Empty; + + byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText); + byte[] encrypted = Encrypt(data, key, iv); + return Convert.ToBase64String(encrypted); + } + + /// + /// 从 Base64 解密字符串 + /// + /// Base64 密文 + /// 密钥 + /// 初始向量 + /// 明文字符串 + public static string DecryptFromBase64(string cipherText, byte[] key, byte[] iv) + { + if (string.IsNullOrEmpty(cipherText)) + return string.Empty; + + byte[] data = Convert.FromBase64String(cipherText); + byte[] decrypted = Decrypt(data, key, iv); + return System.Text.Encoding.UTF8.GetString(decrypted); + } + + /// + /// 生成密钥流 + /// + /// 密钥 + /// 初始向量 + /// 密钥流长度(字) + /// 密钥流 + public static uint[] GenerateKeyStream(byte[] key, byte[] iv, int length) + { + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + if (length < 1) + throw new ArgumentException("Length must be at least 1", nameof(length)); + + using var zuc = new ZucCipher(key, iv); + var keyStream = new uint[length]; + for (int i = 0; i < length; i++) + { + keyStream[i] = zuc.GenerateKeyStreamWord(); + } + return keyStream; + } + + /// + /// 创建 ZUC 处理器 + /// + /// 密钥 + /// 初始向量 + /// ZUC 处理器 + public static ZucCipher CreateCipher(byte[] key, byte[] iv) + { + return new ZucCipher(key, iv); + } + } + + /// + /// ZUC 流加密器 + /// + public class ZucCipher : IDisposable + { + private readonly uint[] _lfsr = new uint[16]; + private readonly uint[] _fsm = new uint[3]; + private bool _initialized = false; + private bool _disposed = false; + + private static readonly uint[] EK = new uint[] + { + 0x44D7, 0x26BC, 0x626B, 0x135E, 0x5789, 0x35E2, 0x7135, 0x09AF, + 0x4D78, 0x2F13, 0x6BC4, 0x1AF1, 0x5E26, 0x3C4A, 0x278E, 0x03F2 + }; + + /// + /// 创建 ZUC 加密器 + /// + /// 密钥(16字节) + /// 初始向量(16字节) + public ZucCipher(byte[] key, byte[] iv) + { + if (key == null || key.Length != 16) + throw new ArgumentException("Key must be 16 bytes", nameof(key)); + if (iv == null || iv.Length != 16) + throw new ArgumentException("IV must be 16 bytes", nameof(iv)); + + Initialize(key, iv); + } + + private void Initialize(byte[] key, byte[] iv) + { + // 初始化 LFSR + for (int i = 0; i < 16; i++) + { + _lfsr[i] = ((uint)key[i] << 16) | ((uint)iv[i] << 8) | EK[i]; + } + + // 初始化 FSM + _fsm[0] = 0; + _fsm[1] = 0; + _fsm[2] = 0; + + // 运行 32 轮初始化 + for (int i = 0; i < 32; i++) + { + uint z = GenerateKeyStreamWord(); + _lfsr[0] = (_lfsr[0] ^ z) & 0x7FFFFFFF; + LfsrShift(); + } + + _initialized = true; + } + + /// + /// 生成一个密钥流字(32位) + /// + /// 32位密钥流字 + public uint GenerateKeyStreamWord() + { + // F 函数 + uint fOutput = FFunction(); + + // 比特重组 + uint w = BitReorganization(); + + // LFSR 更新 + if (_initialized) + { + LfsrWithMode(); + } + else + { + LfsrShift(); + } + + return w ^ fOutput; + } + + /// + /// 处理数据 + /// + /// 输入数据 + /// 处理后的数据 + public byte[] Process(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + byte[] result = new byte[data.Length]; + int processed = 0; + + while (processed < data.Length) + { + uint keyStream = GenerateKeyStreamWord(); + byte[] keyBytes = BitConverter.GetBytes(keyStream); + + for (int i = 0; i < 4 && processed < data.Length; i++) + { + result[processed] = (byte)(data[processed] ^ keyBytes[i]); + processed++; + } + } + + return result; + } + + private uint FFunction() + { + uint r1 = _fsm[0]; + uint r2 = _fsm[1]; + + // 简化的 F 函数 + uint w1 = (r1 + _lfsr[4]) & 0x7FFFFFFF; + uint w2 = (r2 ^ _lfsr[10]) & 0x7FFFFFFF; + + _fsm[0] = STransform(w1); + _fsm[1] = STransform(w2); + _fsm[2] = r1; + + return _fsm[0] ^ _fsm[1] ^ _fsm[2]; + } + + private uint BitReorganization() + { + uint x0 = _lfsr[15]; + uint x1 = _lfsr[14]; + uint x2 = _lfsr[11]; + uint x3 = _lfsr[9]; + uint x4 = _lfsr[7]; + uint x5 = _lfsr[5]; + uint x6 = _lfsr[2]; + uint x7 = _lfsr[0]; + + return ((x0 << 23) | (x1 >> 9)) ^ ((x2 << 15) | (x3 >> 17)) ^ + ((x4 << 7) | (x5 >> 25)) ^ x6 ^ x7; + } + + private void LfsrShift() + { + uint s0 = _lfsr[0]; + uint s4 = _lfsr[4]; + uint s10 = _lfsr[10]; + uint s13 = _lfsr[13]; + uint s15 = _lfsr[15]; + + // 多项式: x^31 - 1 + uint newBit = (s0 ^ s4 ^ s10 ^ s13 ^ s15) & 0x7FFFFFFF; + + for (int i = 0; i < 15; i++) + { + _lfsr[i] = _lfsr[i + 1]; + } + + _lfsr[15] = newBit; + } + + private void LfsrWithMode() + { + uint s0 = _lfsr[0]; + uint s4 = _lfsr[4]; + uint s10 = _lfsr[10]; + uint s13 = _lfsr[13]; + uint s15 = _lfsr[15]; + + uint u = (s0 ^ s4 ^ s10 ^ s13 ^ s15) & 0x7FFFFFFF; + + for (int i = 0; i < 15; i++) + { + _lfsr[i] = _lfsr[i + 1]; + } + + _lfsr[15] = u; + } + + private uint STransform(uint x) + { + byte b0 = (byte)(x & 0xFF); + byte b1 = (byte)((x >> 8) & 0xFF); + byte b2 = (byte)((x >> 16) & 0xFF); + byte b3 = (byte)((x >> 24) & 0xFF); + + b0 = SBox(b0); + b1 = SBox(b1); + b2 = SBox(b2); + b3 = SBox(b3); + + return (uint)((b3 << 24) | (b2 << 16) | (b1 << 8) | b0); + } + + private byte SBox(byte x) + { + int low = x & 0x0F; + int high = (x >> 4) & 0x0F; + return (byte)((_s0[high] << 4) | _s1[low]); + } + + // 内部使用的 S-box 副本 + private static readonly byte[] _s0 = new byte[] + { + 0x3e, 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, + 0x66, 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, + 0x70, 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, + 0x32, 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, + 0x4b, 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, + 0x1b, 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, + 0x69, 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, + 0x59, 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12 + }; + + private static readonly byte[] _s1 = new byte[] + { + 0x72, 0x5b, 0x47, 0x51, 0x9e, 0x23, 0x5e, 0x36, 0x6b, 0x2f, 0x4a, 0x63, 0x21, 0x56, 0x4d, 0x66, + 0x38, 0x00, 0x18, 0x02, 0x19, 0x0b, 0x33, 0x26, 0x5f, 0x58, 0x7d, 0x44, 0x78, 0x0d, 0x54, 0x70, + 0x0c, 0x37, 0x64, 0x3f, 0x16, 0x50, 0x2e, 0x2b, 0x52, 0x08, 0x15, 0x1e, 0x0f, 0x7b, 0x31, 0x32, + 0x13, 0x39, 0x29, 0x0e, 0x28, 0x07, 0x20, 0x03, 0x1f, 0x7a, 0x48, 0x6c, 0x4e, 0x42, 0x1a, 0x4b, + 0x6e, 0x7c, 0x3d, 0x34, 0x3c, 0x4c, 0x05, 0x7e, 0x1c, 0x55, 0x17, 0x6f, 0x3b, 0x2d, 0x5d, 0x1b, + 0x2c, 0x6a, 0x45, 0x30, 0x74, 0x06, 0x22, 0x4f, 0x65, 0x7f, 0x3a, 0x71, 0x5c, 0x10, 0x01, 0x69, + 0x25, 0x14, 0x57, 0x79, 0x60, 0x5a, 0x49, 0x0a, 0x61, 0x73, 0x24, 0x75, 0x41, 0x35, 0x11, 0x59, + 0x68, 0x01, 0x6d, 0x40, 0x27, 0x76, 0x46, 0x04, 0x53, 0x09, 0x77, 0x43, 0x4d, 0x2a, 0x12, 0x3e + }; + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + Array.Clear(_lfsr, 0, _lfsr.Length); + Array.Clear(_fsm, 0, _fsm.Length); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs b/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs new file mode 100644 index 0000000..227db1e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdditionalCollectionUtil.cs @@ -0,0 +1,842 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 集合展平工具类 + /// + public static class FlattenUtil + { + /// + /// 展平嵌套集合 + /// + public static IEnumerable Flatten(IEnumerable> source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + return source.SelectMany(x => x); + } + + /// + /// 递归展平 + /// + public static IEnumerable FlattenRecursive(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + foreach (var item in source) + { + if (item is IEnumerable enumerable) + { + foreach (var subItem in enumerable) + { + yield return subItem; + } + } + else if (item is T value) + { + yield return value; + } + else if (item is IEnumerable nested) + { + foreach (var subItem in FlattenRecursive(nested)) + { + yield return subItem; + } + } + } + } + + /// + /// 展平字典 + /// + public static IEnumerable> Flatten( + IEnumerable> dictionaries) + { + if (dictionaries == null) + throw new ArgumentNullException(nameof(dictionaries)); + + return dictionaries.SelectMany(d => d); + } + } + + /// + /// 集合分组工具类 + /// + public static class GroupingUtil + { + /// + /// 将连续相同的元素分组 + /// + public static IEnumerable> GroupConsecutive(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + var currentGroup = new List { enumerator.Current }; + var current = enumerator.Current; + + while (enumerator.MoveNext()) + { + if (EqualityComparer.Default.Equals(enumerator.Current, current)) + { + currentGroup.Add(enumerator.Current); + } + else + { + yield return currentGroup; + currentGroup = new List { enumerator.Current }; + current = enumerator.Current; + } + } + + yield return currentGroup; + } + + /// + /// 将连续满足条件的元素分组 + /// + public static IEnumerable> GroupConsecutive(IEnumerable source, Func belongsToSameGroup) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (belongsToSameGroup == null) + throw new ArgumentNullException(nameof(belongsToSameGroup)); + + var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) + yield break; + + var currentGroup = new List { enumerator.Current }; + var current = enumerator.Current; + + while (enumerator.MoveNext()) + { + if (belongsToSameGroup(current, enumerator.Current)) + { + currentGroup.Add(enumerator.Current); + } + else + { + yield return currentGroup; + currentGroup = new List { enumerator.Current }; + } + current = enumerator.Current; + } + + yield return currentGroup; + } + + /// + /// 按固定大小分组 + /// + public static IEnumerable> GroupBySize(IEnumerable source, int groupSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (groupSize <= 0) + throw new ArgumentOutOfRangeException(nameof(groupSize)); + + var group = new List(groupSize); + foreach (var item in source) + { + group.Add(item); + if (group.Count == groupSize) + { + yield return group; + group = new List(groupSize); + } + } + + if (group.Count > 0) + { + yield return group; + } + } + + /// + /// 按条件分组的数量分组 + /// + public static IEnumerable> GroupWhile(IEnumerable source, Func, T, bool> shouldInclude) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (shouldInclude == null) + throw new ArgumentNullException(nameof(shouldInclude)); + + var group = new List(); + foreach (var item in source) + { + if (group.Count == 0 || shouldInclude(group, item)) + { + group.Add(item); + } + else + { + yield return group; + group = new List { item }; + } + } + + if (group.Count > 0) + { + yield return group; + } + } + } + + /// + /// 集合合并工具类 + /// + public static class MergeUtil + { + /// + /// 合并两个有序集合 + /// + public static IEnumerable MergeOrdered(IEnumerable first, IEnumerable second) where T : IComparable + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + using var enum1 = first.GetEnumerator(); + using var enum2 = second.GetEnumerator(); + + bool hasFirst = enum1.MoveNext(); + bool hasSecond = enum2.MoveNext(); + + while (hasFirst && hasSecond) + { + if (enum1.Current.CompareTo(enum2.Current) <= 0) + { + yield return enum1.Current; + hasFirst = enum1.MoveNext(); + } + else + { + yield return enum2.Current; + hasSecond = enum2.MoveNext(); + } + } + + while (hasFirst) + { + yield return enum1.Current; + hasFirst = enum1.MoveNext(); + } + + while (hasSecond) + { + yield return enum2.Current; + hasSecond = enum2.MoveNext(); + } + } + + /// + /// 合并多个有序集合 + /// + public static IEnumerable MergeOrdered(params IEnumerable[] sources) where T : IComparable + { + if (sources == null || sources.Length == 0) + yield break; + + var enumerators = sources + .Select(s => s?.GetEnumerator()) + .Where(e => e != null) + .ToList(); + + var hasMore = new bool[enumerators.Count]; + for (int i = 0; i < enumerators.Count; i++) + { + hasMore[i] = enumerators[i].MoveNext(); + } + + while (hasMore.Any(x => x)) + { + int minIndex = -1; + T minValue = default; + + for (int i = 0; i < enumerators.Count; i++) + { + if (!hasMore[i]) + continue; + + if (minIndex == -1 || enumerators[i].Current.CompareTo(minValue) < 0) + { + minIndex = i; + minValue = enumerators[i].Current; + } + } + + if (minIndex >= 0) + { + yield return minValue; + hasMore[minIndex] = enumerators[minIndex].MoveNext(); + } + } + + foreach (var e in enumerators) + { + e.Dispose(); + } + } + + /// + /// 合并字典(后者覆盖前者) + /// + public static Dictionary Merge( + IDictionary first, + IDictionary second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + var result = new Dictionary(first); + foreach (var kvp in second) + { + result[kvp.Key] = kvp.Value; + } + return result; + } + + /// + /// 合并字典(自定义冲突解决) + /// + public static Dictionary Merge( + IDictionary first, + IDictionary second, + Func conflictResolver) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + if (conflictResolver == null) + throw new ArgumentNullException(nameof(conflictResolver)); + + var result = new Dictionary(first); + foreach (var kvp in second) + { + if (result.TryGetValue(kvp.Key, out var existing)) + { + result[kvp.Key] = conflictResolver(kvp.Key, existing, kvp.Value); + } + else + { + result[kvp.Key] = kvp.Value; + } + } + return result; + } + } + + /// + /// 集合查找工具类 + /// + public static class SearchUtil + { + /// + /// 二分查找 + /// + public static int BinarySearch(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count - 1; + + while (left <= right) + { + int mid = left + (right - left) / 2; + int cmp = list[mid].CompareTo(value); + + if (cmp == 0) + return mid; + if (cmp < 0) + left = mid + 1; + else + right = mid - 1; + } + + return ~left; // 返回插入点的补码 + } + + /// + /// 查找第一个大于等于指定值的元素索引 + /// + public static int LowerBound(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count; + + while (left < right) + { + int mid = left + (right - left) / 2; + if (list[mid].CompareTo(value) < 0) + left = mid + 1; + else + right = mid; + } + + return left; + } + + /// + /// 查找第一个大于指定值的元素索引 + /// + public static int UpperBound(IList list, T value) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + + int left = 0; + int right = list.Count; + + while (left < right) + { + int mid = left + (right - left) / 2; + if (list[mid].CompareTo(value) <= 0) + left = mid + 1; + else + right = mid; + } + + return left; + } + + /// + /// 查找范围内的元素数量 + /// + public static int CountInRange(IList list, T min, T max) where T : IComparable + { + return UpperBound(list, max) - LowerBound(list, min); + } + + /// + /// 查找众数(出现次数最多的元素) + /// + public static T FindMajority(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + // Boyer-Moore 多数投票算法 + T candidate = default; + int count = 0; + + foreach (var item in list) + { + if (count == 0) + { + candidate = item; + count = 1; + } + else if (EqualityComparer.Default.Equals(item, candidate)) + { + count++; + } + else + { + count--; + } + } + + // 验证 + count = list.Count(x => EqualityComparer.Default.Equals(x, candidate)); + if (count > list.Count / 2) + return candidate; + + throw new InvalidOperationException("No majority element found"); + } + + /// + /// 尝试查找众数 + /// + public static bool TryFindMajority(IEnumerable source, out T majority) + { + try + { + majority = FindMajority(source); + return true; + } + catch + { + majority = default; + return false; + } + } + } + + /// + /// 集合序列工具类 + /// + public static class SequenceUtil + { + /// + /// 生成等差数列 + /// + public static IEnumerable Range(int start, int count, int step = 1) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return start + i * step; + } + } + + /// + /// 生成等差数列(浮点数) + /// + public static IEnumerable Range(double start, int count, double step) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return start + i * step; + } + } + + /// + /// 生成重复序列 + /// + public static IEnumerable Repeat(T value, int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + for (int i = 0; i < count; i++) + { + yield return value; + } + } + + /// + /// 循环生成序列 + /// + public static IEnumerable Cycle(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + int index = 0; + while (true) + { + yield return list[index]; + index = (index + 1) % list.Count; + } + } + + /// + /// 循环生成指定次数 + /// + public static IEnumerable Cycle(IEnumerable source, int count) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + for (int i = 0; i < count; i++) + { + yield return list[i % list.Count]; + } + } + + /// + /// 生成斐波那契数列 + /// + public static IEnumerable Fibonacci(int count) + { + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + long a = 0, b = 1; + for (int i = 0; i < count; i++) + { + yield return a; + (a, b) = (b, a + b); + } + } + + /// + /// 生成迭代序列 + /// + public static IEnumerable Iterate(T initial, Func next, int count) + { + if (next == null) + throw new ArgumentNullException(nameof(next)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + T current = initial; + for (int i = 0; i < count; i++) + { + yield return current; + current = next(current); + } + } + } + + /// + /// 集合集合操作工具类 + /// + public static class SetOperationUtil + { + /// + /// 笛卡尔积 + /// + public static IEnumerable<(T1, T2)> CartesianProduct( + IEnumerable first, + IEnumerable second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + return from a in first + from b in second + select (a, b); + } + + /// + /// 多集合笛卡尔积 + /// + public static IEnumerable> CartesianProduct(IEnumerable> sources) + { + if (sources == null) + throw new ArgumentNullException(nameof(sources)); + + var lists = sources.Select(s => s.ToList()).ToList(); + if (lists.Count == 0) + { + yield return new List(); + yield break; + } + + var indices = new int[lists.Count]; + var counts = lists.Select(l => l.Count).ToArray(); + + if (counts.Any(c => c == 0)) + yield break; + + while (true) + { + var result = new List(); + for (int i = 0; i < lists.Count; i++) + { + result.Add(lists[i][indices[i]]); + } + yield return result; + + // 增加索引 + int j = lists.Count - 1; + while (j >= 0) + { + indices[j]++; + if (indices[j] < counts[j]) + break; + indices[j] = 0; + j--; + } + + if (j < 0) + break; + } + } + + /// + /// 幂集(所有子集) + /// + public static IEnumerable> PowerSet(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + int count = 1 << list.Count; // 2^n + + for (int i = 0; i < count; i++) + { + var subset = new List(); + for (int j = 0; j < list.Count; j++) + { + if ((i & (1 << j)) != 0) + { + subset.Add(list[j]); + } + } + yield return subset; + } + } + + /// + /// 获取指定大小的所有子集 + /// + public static IEnumerable> SubsetsOfSize(IEnumerable source, int size) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (size < 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + var list = source.ToList(); + if (size > list.Count) + yield break; + + var indices = Enumerable.Range(0, size).ToArray(); + + while (true) + { + yield return indices.Select(i => list[i]).ToList(); + + // 找到可以增加的索引 + int i = size - 1; + while (i >= 0 && indices[i] == list.Count - size + i) + i--; + + if (i < 0) + break; + + indices[i]++; + for (int j = i + 1; j < size; j++) + { + indices[j] = indices[j - 1] + 1; + } + } + } + } + + /// + /// 集合排序工具类 + /// + public static class SortingUtil + { + /// + /// 快速选择(找到第 k 小的元素) + /// + public static T QuickSelect(IList list, int k) where T : IComparable + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + if (k < 0 || k >= list.Count) + throw new ArgumentOutOfRangeException(nameof(k)); + + var arr = list.ToArray(); + return QuickSelectInternal(arr, 0, arr.Length - 1, k); + } + + private static T QuickSelectInternal(T[] arr, int left, int right, int k) where T : IComparable + { + if (left == right) + return arr[left]; + + int pivotIndex = Partition(arr, left, right); + + if (k == pivotIndex) + return arr[k]; + if (k < pivotIndex) + return QuickSelectInternal(arr, left, pivotIndex - 1, k); + return QuickSelectInternal(arr, pivotIndex + 1, right, k); + } + + private static int Partition(T[] arr, int left, int right) where T : IComparable + { + T pivot = arr[right]; + int i = left; + + for (int j = left; j < right; j++) + { + if (arr[j].CompareTo(pivot) <= 0) + { + Swap(arr, i, j); + i++; + } + } + + Swap(arr, i, right); + return i; + } + + private static void Swap(T[] arr, int i, int j) + { + T temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + /// + /// 多键排序 + /// + public static List SortByMultiple(IEnumerable source, params Func[] selectors) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (selectors == null || selectors.Length == 0) + return source.ToList(); + + var list = source.ToList(); + list.Sort((a, b) => + { + foreach (var selector in selectors) + { + int cmp = selector(a).CompareTo(selector(b)); + if (cmp != 0) + return cmp; + } + return 0; + }); + return list; + } + + /// + /// 稳定排序 + /// + public static List StableSort(IEnumerable source, Comparison comparison) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (comparison == null) + throw new ArgumentNullException(nameof(comparison)); + + var list = source.Select((x, i) => new { Value = x, Index = i }).ToList(); + list.Sort((a, b) => + { + int cmp = comparison(a.Value, b.Value); + return cmp != 0 ? cmp : a.Index.CompareTo(b.Index); + }); + return list.Select(x => x.Value).ToList(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs new file mode 100644 index 0000000..cc070ec --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedBloomFilterUtil.cs @@ -0,0 +1,607 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级布隆过滤器工具类 + /// + public static class AdvancedBloomFilterUtil + { + /// + /// 创建计数布隆过滤器 + /// + public static CountingBloomFilter CreateCounting(int capacity, double falsePositiveRate = 0.01) + { + return new CountingBloomFilter(capacity, falsePositiveRate); + } + + /// + /// 创建布谷鸟过滤器 + /// + public static CuckooFilter CreateCuckoo(int capacity, int fingerprintSize = 8) + { + return new CuckooFilter(capacity, fingerprintSize); + } + } + + /// + /// 计数布隆过滤器 + /// 支持删除操作 + /// + public class CountingBloomFilter + { + private readonly byte[] _counters; + private readonly int _hashCount; + private readonly int _size; + private readonly HashFunction[] _hashFunctions; + private int _count; + + private delegate int HashFunction(byte[] data); + + /// + /// 已添加元素数量 + /// + public int Count => _count; + + /// + /// 容量 + /// + public int Capacity { get; } + + /// + /// 位大小 + /// + public int Size => _size; + + /// + /// 哈希函数数量 + /// + public int HashCount => _hashCount; + + /// + /// 创建计数布隆过滤器 + /// + public CountingBloomFilter(int capacity, double falsePositiveRate = 0.01) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + Capacity = capacity; + + // 计算最优参数 + _size = (int)Math.Ceiling(-capacity * Math.Log(falsePositiveRate) / Math.Pow(Math.Log(2), 2)); + _hashCount = (int)Math.Ceiling(_size * Math.Log(2) / capacity); + + _counters = new byte[_size]; + _hashFunctions = new HashFunction[_hashCount]; + _count = 0; + + // 初始化哈希函数 + for (int i = 0; i < _hashCount; i++) + { + int seed = (int)(i * 0x9e3779b9); + _hashFunctions[i] = data => HashWithSeed(data, seed); + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] < byte.MaxValue) + { + _counters[index]++; + } + } + _count++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + if (!Contains(data)) + return false; + + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] > 0) + { + _counters[index]--; + } + } + _count--; + return true; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + foreach (var hash in _hashFunctions) + { + int index = Math.Abs(hash(data)) % _size; + if (_counters[index] == 0) + return false; + } + return true; + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + _count = 0; + } + + /// + /// 估计假阳率 + /// + public double EstimatedFalsePositiveRate() + { + if (_count == 0) + return 0; + + double ratio = (double)_count / Capacity; + return Math.Pow(1 - Math.Exp(-_hashCount * ratio), _hashCount); + } + + private static int HashWithSeed(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } + + /// + /// 布谷鸟过滤器 + /// 支持删除,比布隆过滤器更低的空间占用 + /// + public class CuckooFilter + { + private readonly byte[][][] _buckets; + private readonly int _bucketCount; + private readonly int _fingerprintSize; + private readonly int _maxKickOuts; + private int _count; + private readonly Random _random; + + /// + /// 已添加元素数量 + /// + public int Count => _count; + + /// + /// 容量 + /// + public int Capacity => _bucketCount; + + /// + /// 负载因子 + /// + public double LoadFactor => (double)_count / _bucketCount; + + /// + /// 创建布谷鸟过滤器 + /// + public CuckooFilter(int capacity, int fingerprintSize = 8) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + if (fingerprintSize < 1 || fingerprintSize > 16) + throw new ArgumentOutOfRangeException(nameof(fingerprintSize), "Fingerprint size must be between 1 and 16"); + + _bucketCount = capacity; + _fingerprintSize = fingerprintSize; + _maxKickOuts = 500; + _count = 0; + _random = new Random(); + + _buckets = new byte[capacity][][]; + for (int i = 0; i < capacity; i++) + { + _buckets[i] = new byte[4][]; // 每个桶4个槽 + } + } + + /// + /// 添加元素 + /// + public bool Add(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + // 尝试添加到任一桶 + if (TryAddToBucket(i1, fingerprint)) + { + _count++; + return true; + } + if (TryAddToBucket(i2, fingerprint)) + { + _count++; + return true; + } + + // 需要踢出 + int i = _random.Next(2) == 0 ? i1 : i2; + + for (int n = 0; n < _maxKickOuts; n++) + { + // 随机选择一个槽踢出 + int slot = _random.Next(4); + var oldFingerprint = _buckets[i][slot]; + _buckets[i][slot] = fingerprint; + fingerprint = oldFingerprint; + + i = AltIndex(i, fingerprint); + + if (TryAddToBucket(i, fingerprint)) + { + _count++; + return true; + } + } + + return false; + } + + /// + /// 添加字符串 + /// + public bool Add(string value) + { + return Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + if (RemoveFromBucket(i1, fingerprint) || RemoveFromBucket(i2, fingerprint)) + { + _count--; + return true; + } + + return false; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + var fingerprint = ComputeFingerprint(data); + int i1 = Hash1(data); + int i2 = AltIndex(i1, fingerprint); + + return BucketContains(i1, fingerprint) || BucketContains(i2, fingerprint); + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + for (int i = 0; i < _bucketCount; i++) + { + for (int j = 0; j < 4; j++) + { + _buckets[i][j] = null; + } + } + _count = 0; + } + + private byte[] ComputeFingerprint(byte[] data) + { + unchecked + { + int hash = 17; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + + var fingerprint = new byte[_fingerprintSize]; + for (int i = 0; i < _fingerprintSize; i++) + { + fingerprint[i] = (byte)((hash >> (i * 8)) & 0xFF); + } + + // 确保不为空 + if (fingerprint.All(b => b == 0)) + { + fingerprint[0] = 1; + } + + return fingerprint; + } + } + + private int Hash1(byte[] data) + { + unchecked + { + int hash = 0; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return Math.Abs(hash) % _bucketCount; + } + } + + private int AltIndex(int index, byte[] fingerprint) + { + unchecked + { + int hash = 0; + foreach (byte b in fingerprint) + { + hash = hash * 31 + b; + } + return (index ^ hash) % _bucketCount; + } + } + + private bool TryAddToBucket(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (_buckets[index][i] == null) + { + _buckets[index][i] = fingerprint; + return true; + } + } + return false; + } + + private bool RemoveFromBucket(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (FingerprintEquals(_buckets[index][i], fingerprint)) + { + _buckets[index][i] = null; + return true; + } + } + return false; + } + + private bool BucketContains(int index, byte[] fingerprint) + { + for (int i = 0; i < 4; i++) + { + if (FingerprintEquals(_buckets[index][i], fingerprint)) + { + return true; + } + } + return false; + } + + private bool FingerprintEquals(byte[] a, byte[] b) + { + if (a == null || b == null) + return false; + + if (a.Length != b.Length) + return false; + + for (int i = 0; i < a.Length; i++) + { + if (a[i] != b[i]) + return false; + } + + return true; + } + } + + /// + /// 可扩展布隆过滤器 + /// 当填满时自动扩展容量 + /// + public class ScalableBloomFilter + { + private readonly List _filters; + private readonly double _initialFalsePositiveRate; + private readonly double _scalingFactor; + private readonly int _initialCapacity; + private int _totalCapacity; + + /// + /// 已添加元素数量 + /// + public int Count { get; private set; } + + /// + /// 当前容量 + /// + public int Capacity => _totalCapacity; + + /// + /// 过滤器数量 + /// + public int FilterCount => _filters.Count; + + /// + /// 创建可扩展布隆过滤器 + /// + public ScalableBloomFilter(int initialCapacity = 1000, double falsePositiveRate = 0.01, double scalingFactor = 2) + { + _initialCapacity = initialCapacity; + _initialFalsePositiveRate = falsePositiveRate; + _scalingFactor = scalingFactor; + + _filters = new List(); + _totalCapacity = 0; + Count = 0; + + AddFilter(); + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + // 查找有空位的过滤器 + foreach (var filter in _filters) + { + if (filter.Count < filter.Capacity) + { + filter.Add(data); + Count++; + return; + } + } + + // 需要添加新过滤器 + AddFilter(); + _filters[_filters.Count - 1].Add(data); + Count++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 移除元素 + /// + public bool Remove(byte[] data) + { + for (int i = _filters.Count - 1; i >= 0; i--) + { + if (_filters[i].Contains(data)) + { + if (_filters[i].Remove(data)) + { + Count--; + return true; + } + } + } + return false; + } + + /// + /// 移除字符串 + /// + public bool Remove(string value) + { + return Remove(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 是否可能包含 + /// + public bool Contains(byte[] data) + { + return _filters.Any(f => f.Contains(data)); + } + + /// + /// 是否可能包含字符串 + /// + public bool Contains(string value) + { + return Contains(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 清空 + /// + public void Clear() + { + _filters.Clear(); + _totalCapacity = 0; + Count = 0; + AddFilter(); + } + + private void AddFilter() + { + int capacity = (int)(_initialCapacity * Math.Pow(_scalingFactor, _filters.Count)); + double fpr = _initialFalsePositiveRate / Math.Pow(_scalingFactor, _filters.Count); + + var filter = new CountingBloomFilter(capacity, fpr); + _filters.Add(filter); + _totalCapacity += capacity; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs new file mode 100644 index 0000000..a3adeee --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedCollectionsUtil.cs @@ -0,0 +1,687 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级集合工具类 + /// + public static class AdvancedCollectionsUtil + { + /// + /// 创建Roaring Bitmap + /// + public static RoaringBitmap CreateRoaringBitmap() + { + return new RoaringBitmap(); + } + + /// + /// 从整数集合创建Roaring Bitmap + /// + public static RoaringBitmap CreateRoaringBitmap(IEnumerable values) + { + var bitmap = new RoaringBitmap(); + foreach (var value in values) + { + bitmap.Add(value); + } + return bitmap; + } + + /// + /// 创建流式处理器 + /// + public static StreamProcessor CreateStreamProcessor(int windowSize) + { + return new StreamProcessor(windowSize); + } + } + + /// + /// Roaring Bitmap + /// 压缩位图,高效存储和操作整数集合 + /// + public class RoaringBitmap : IEnumerable + { + private readonly Dictionary _containers; + + private abstract class Container + { + public abstract int Count { get; } + public abstract bool Contains(ushort value); + public abstract void Add(ushort value); + public abstract void Remove(ushort value); + public abstract IEnumerator GetEnumerator(); + } + + private class ArrayContainer : Container + { + private readonly List _values; + private const int MaxSize = 4096; + + public override int Count => _values.Count; + + public ArrayContainer() + { + _values = new List(); + } + + public override bool Contains(ushort value) + { + return _values.BinarySearch(value) >= 0; + } + + public override void Add(ushort value) + { + int index = _values.BinarySearch(value); + if (index < 0) + { + _values.Insert(~index, value); + } + } + + public override void Remove(ushort value) + { + int index = _values.BinarySearch(value); + if (index >= 0) + { + _values.RemoveAt(index); + } + } + + public override IEnumerator GetEnumerator() + { + return _values.GetEnumerator(); + } + + public bool IsFull => _values.Count >= MaxSize; + + public BitmapContainer ToBitmapContainer() + { + var bitmap = new BitmapContainer(); + foreach (var value in _values) + { + bitmap.Add(value); + } + return bitmap; + } + } + + private class BitmapContainer : Container + { + private readonly ulong[] _bitmap; + private int _count; + + public override int Count => _count; + + public BitmapContainer() + { + _bitmap = new ulong[1024]; // 65536 bits / 64 = 1024 + _count = 0; + } + + public override bool Contains(ushort value) + { + int index = value / 64; + int bit = value % 64; + return (_bitmap[index] & (1UL << bit)) != 0; + } + + public override void Add(ushort value) + { + int index = value / 64; + int bit = value % 64; + if ((_bitmap[index] & (1UL << bit)) == 0) + { + _bitmap[index] |= 1UL << bit; + _count++; + } + } + + public override void Remove(ushort value) + { + int index = value / 64; + int bit = value % 64; + if ((_bitmap[index] & (1UL << bit)) != 0) + { + _bitmap[index] &= ~(1UL << bit); + _count--; + } + } + + public override IEnumerator GetEnumerator() + { + for (int i = 0; i < _bitmap.Length; i++) + { + if (_bitmap[i] == 0) + continue; + + for (int bit = 0; bit < 64; bit++) + { + if ((_bitmap[i] & (1UL << bit)) != 0) + { + yield return (ushort)(i * 64 + bit); + } + } + } + } + } + + /// + /// 元素数量 + /// + public int Count { get; private set; } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 创建Roaring Bitmap + /// + public RoaringBitmap() + { + _containers = new Dictionary(); + Count = 0; + } + + /// + /// 添加值 + /// + public void Add(int value) + { + if (value < 0) + throw new ArgumentOutOfRangeException(nameof(value), "值不能为负数"); + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + if (!_containers.TryGetValue(high, out var container)) + { + container = new ArrayContainer(); + _containers[high] = container; + } + + int oldCount = container.Count; + container.Add(low); + + if (container.Count > oldCount) + Count++; + + // 检查是否需要转换为位图容器 + if (container is ArrayContainer ac && ac.IsFull) + { + _containers[high] = ac.ToBitmapContainer(); + } + } + + /// + /// 批量添加 + /// + public void AddRange(IEnumerable values) + { + foreach (var value in values) + { + Add(value); + } + } + + /// + /// 移除值 + /// + public bool Remove(int value) + { + if (value < 0) + return false; + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + if (!_containers.TryGetValue(high, out var container)) + return false; + + int oldCount = container.Count; + container.Remove(low); + + if (container.Count < oldCount) + { + Count--; + return true; + } + + return false; + } + + /// + /// 是否包含值 + /// + public bool Contains(int value) + { + if (value < 0) + return false; + + ushort high = (ushort)(value >> 16); + ushort low = (ushort)(value & 0xFFFF); + + return _containers.TryGetValue(high, out var container) && container.Contains(low); + } + + /// + /// 与操作 + /// + public void And(RoaringBitmap other) + { + if (other == null) + return; + + var keysToRemove = new List(); + + foreach (var kvp in _containers) + { + if (!other._containers.TryGetValue(kvp.Key, out var otherContainer)) + { + keysToRemove.Add(kvp.Key); + } + else + { + // 简化实现:创建新的位图容器 + var result = new BitmapContainer(); + foreach (var value in kvp.Value) + { + if (otherContainer.Contains(value)) + { + result.Add(value); + } + } + + if (result.Count > 0) + { + _containers[kvp.Key] = result; + } + else + { + keysToRemove.Add(kvp.Key); + } + } + } + + foreach (var key in keysToRemove) + { + var container = _containers[key]; + Count -= container.Count; + _containers.Remove(key); + } + } + + /// + /// 或操作 + /// + public void Or(RoaringBitmap other) + { + if (other == null) + return; + + foreach (var kvp in other._containers) + { + if (!_containers.TryGetValue(kvp.Key, out var container)) + { + container = new BitmapContainer(); + _containers[kvp.Key] = container; + } + + foreach (var value in kvp.Value) + { + int oldCount = container.Count; + container.Add(value); + if (container.Count > oldCount) + Count++; + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + _containers.Clear(); + Count = 0; + } + + public IEnumerator GetEnumerator() + { + foreach (var kvp in _containers.OrderBy(x => x.Key)) + { + int high = kvp.Key << 16; + foreach (var low in kvp.Value) + { + yield return high | low; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + /// + /// 流式数据处理器 + /// 支持滑动窗口聚合 + /// + public class StreamProcessor + { + private readonly int _windowSize; + private readonly Queue _window; + private readonly List> _aggregators; + private long _totalCount; + + /// + /// 窗口大小 + /// + public int WindowSize => _windowSize; + + /// + /// 当前窗口内元素数量 + /// + public int WindowCount => _window.Count; + + /// + /// 总处理元素数量 + /// + public long TotalCount => _totalCount; + + /// + /// 创建流式处理器 + /// + public StreamProcessor(int windowSize) + { + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + _windowSize = windowSize; + _window = new Queue(); + _aggregators = new List>(); + _totalCount = 0; + } + + /// + /// 添加聚合器 + /// + public void AddAggregator(IAggregator aggregator) + { + if (aggregator == null) + throw new ArgumentNullException(nameof(aggregator)); + _aggregators.Add(aggregator); + } + + /// + /// 处理元素 + /// + public void Process(T item) + { + // 通知聚合器有新元素 + foreach (var aggregator in _aggregators) + { + aggregator.Add(item); + } + + _window.Enqueue(item); + _totalCount++; + + // 如果窗口满了,移除最旧的元素 + if (_window.Count > _windowSize) + { + var removed = _window.Dequeue(); + foreach (var aggregator in _aggregators) + { + aggregator.Remove(removed); + } + } + } + + /// + /// 批量处理 + /// + public void ProcessRange(IEnumerable items) + { + foreach (var item in items) + { + Process(item); + } + } + + /// + /// 获取聚合结果 + /// + public TResult GetResult(string aggregatorName) + { + var aggregator = _aggregators.FirstOrDefault(a => a.Name == aggregatorName); + if (aggregator is IAggregator typedAggregator) + { + return typedAggregator.GetResult(); + } + throw new ArgumentException($"Aggregator '{aggregatorName}' not found or has different result type"); + } + + /// + /// 获取所有聚合结果 + /// + public Dictionary GetAllResults() + { + return _aggregators.ToDictionary(a => a.Name, a => a.GetResultObject()); + } + + /// + /// 清空窗口 + /// + public void Clear() + { + _window.Clear(); + foreach (var aggregator in _aggregators) + { + aggregator.Reset(); + } + _totalCount = 0; + } + + /// + /// 获取窗口内元素 + /// + public IReadOnlyCollection GetWindow() + { + return _window.ToArray(); + } + } + + /// + /// 聚合器接口 + /// + public interface IAggregator + { + /// + /// 名称 + /// + string Name { get; } + + /// + /// 添加元素 + /// + void Add(T item); + + /// + /// 移除元素 + /// + void Remove(T item); + + /// + /// 重置 + /// + void Reset(); + + /// + /// 获取结果(对象形式) + /// + object GetResultObject(); + } + + /// + /// 聚合器接口(带结果类型) + /// + public interface IAggregator : IAggregator + { + /// + /// 获取结果 + /// + TResult GetResult(); + } + + /// + /// 计数聚合器 + /// + public class CountAggregator : IAggregator + { + private long _count; + + public string Name => "Count"; + + public void Add(T item) => _count++; + + public void Remove(T item) => _count--; + + public void Reset() => _count = 0; + + public long GetResult() => _count; + + public object GetResultObject() => GetResult(); + } + + /// + /// 求和聚合器 + /// + public class SumAggregator : IAggregator + { + private double _sum; + + public string Name => "Sum"; + + public void Add(double item) => _sum += item; + + public void Remove(double item) => _sum -= item; + + public void Reset() => _sum = 0; + + public double GetResult() => _sum; + + public object GetResultObject() => GetResult(); + } + + /// + /// 平均值聚合器 + /// + public class AverageAggregator : IAggregator + { + private double _sum; + private long _count; + + public string Name => "Average"; + + public void Add(double item) + { + _sum += item; + _count++; + } + + public void Remove(double item) + { + _sum -= item; + _count--; + } + + public void Reset() + { + _sum = 0; + _count = 0; + } + + public double GetResult() => _count > 0 ? _sum / _count : 0; + + public object GetResultObject() => GetResult(); + } + + /// + /// 最小值聚合器 + /// + public class MinAggregator : IAggregator where T : IComparable + { + private readonly List _items = new List(); + + public string Name => "Min"; + + public void Add(T item) => _items.Add(item); + + public void Remove(T item) => _items.Remove(item); + + public void Reset() => _items.Clear(); + + public T GetResult() => _items.Count > 0 ? _items.Min() : default; + + public object GetResultObject() => GetResult(); + } + + /// + /// 最大值聚合器 + /// + public class MaxAggregator : IAggregator where T : IComparable + { + private readonly List _items = new List(); + + public string Name => "Max"; + + public void Add(T item) => _items.Add(item); + + public void Remove(T item) => _items.Remove(item); + + public void Reset() => _items.Clear(); + + public T GetResult() => _items.Count > 0 ? _items.Max() : default; + + public object GetResultObject() => GetResult(); + } + + /// + /// 频率聚合器 + /// + public class FrequencyAggregator : IAggregator> + { + private readonly Dictionary _frequency = new Dictionary(); + + public string Name => "Frequency"; + + public void Add(T item) + { + if (_frequency.ContainsKey(item)) + _frequency[item]++; + else + _frequency[item] = 1; + } + + public void Remove(T item) + { + if (_frequency.ContainsKey(item)) + { + _frequency[item]--; + if (_frequency[item] == 0) + _frequency.Remove(item); + } + } + + public void Reset() => _frequency.Clear(); + + public Dictionary GetResult() => new Dictionary(_frequency); + + public object GetResultObject() => GetResult(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs new file mode 100644 index 0000000..17f833b --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedHeapUtil.cs @@ -0,0 +1,742 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级堆工具类 + /// + public static class AdvancedHeapUtil + { + /// + /// 创建配对堆 + /// + public static PairingHeap CreatePairing() where T : IComparable + { + return new PairingHeap(); + } + + /// + /// 创建斐波那契堆 + /// + public static FibonacciHeap CreateFibonacci() where T : IComparable + { + return new FibonacciHeap(); + } + + /// + /// 创建二项堆 + /// + public static BinomialHeap CreateBinomial() where T : IComparable + { + return new BinomialHeap(); + } + } + + /// + /// 配对堆 + /// 时间复杂度:插入O(1),删除最小O(log n)摊还,合并O(1) + /// + public class PairingHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node? Child { get; set; } + public Node? Sibling { get; set; } + public Node? Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node? _root; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + return _root.Value; + } + } + + /// + /// 创建配对堆 + /// + public PairingHeap() + { + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + _root = Merge(_root, node); + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + + T minValue = _root.Value; + _root = MergePairs(_root.Child); + _count--; + return minValue; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + if (_root == null) + throw new InvalidOperationException("Heap is empty"); + return _root.Value; + } + + /// + /// 合并另一个堆 + /// + public void Merge(PairingHeap other) + { + if (other == null) + return; + + _root = Merge(_root, other._root); + _count += other._count; + other._root = null; + other._count = 0; + } + + private Node? Merge(Node? a, Node? b) + { + if (a == null) + return b; + if (b == null) + return a; + + if (a.Value.CompareTo(b.Value) <= 0) + { + b.Sibling = a.Child; + b.Parent = a; + a.Child = b; + return a; + } + else + { + a.Sibling = b.Child; + a.Parent = b; + b.Child = a; + return b; + } + } + + private Node? MergePairs(Node? node) + { + if (node == null || node.Sibling == null) + return node; + + // 收集所有兄弟节点 + var nodes = new List(); + while (node != null) + { + nodes.Add(node); + node = node.Sibling; + } + + // 从左到右两两合并 + var merged = new List(); + for (int i = 0; i < nodes.Count; i += 2) + { + if (i + 1 < nodes.Count) + { + merged.Add(Merge(nodes[i], nodes[i + 1])); + } + else + { + merged.Add(nodes[i]); + } + } + + // 从右到左合并 + Node result = merged[merged.Count - 1]; + for (int i = merged.Count - 2; i >= 0; i--) + { + result = Merge(result, merged[i]); + } + + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + /// + /// 斐波那契堆 + /// 时间复杂度:插入O(1),删除最小O(log n)摊还,降低键O(1)摊还 + /// + public class FibonacciHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node? Parent { get; set; } + public Node? Child { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public int Degree { get; set; } + public bool Mark { get; set; } + + public Node(T value) + { + Value = value; + Left = this; + Right = this; + } + } + + private Node? _min; + private int _count; + private readonly List _degreeList; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + return _min.Value; + } + } + + /// + /// 创建斐波那契堆 + /// + public FibonacciHeap() + { + _min = null; + _count = 0; + _degreeList = new List(); + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + + if (_min == null) + { + _min = node; + } + else + { + AddToRootList(node); + if (node.Value.CompareTo(_min.Value) < 0) + _min = node; + } + + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + + T minValue = _min.Value; + + // 将子节点添加到根列表 + if (_min.Child != null) + { + var child = _min.Child; + do + { + var next = child.Right; + child.Parent = null; + AddToRootList(child); + child = next; + } while (child != _min.Child); + } + + // 从根列表移除最小节点 + RemoveFromRootList(_min); + + if (_min == _min.Right) + { + _min = null; + } + else + { + _min = _min.Right; + Consolidate(); + } + + _count--; + return minValue; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + if (_min == null) + throw new InvalidOperationException("Heap is empty"); + return _min.Value; + } + + /// + /// 合并另一个堆 + /// + public void Merge(FibonacciHeap other) + { + if (other == null || other._min == null) + return; + + if (_min == null) + { + _min = other._min; + } + else + { + // 连接根列表 + var thisRight = _min.Right; + var otherLeft = other._min.Left; + + _min.Right = other._min; + other._min.Left = _min; + thisRight.Left = otherLeft; + otherLeft.Right = thisRight; + + if (other._min.Value.CompareTo(_min.Value) < 0) + _min = other._min; + } + + _count += other._count; + other._min = null; + other._count = 0; + } + + private void AddToRootList(Node node) + { + if (_min == null) + { + _min = node; + node.Left = node; + node.Right = node; + } + else + { + node.Left = _min; + node.Right = _min.Right; + _min.Right.Left = node; + _min.Right = node; + } + } + + private void RemoveFromRootList(Node node) + { + node.Left.Right = node.Right; + node.Right.Left = node.Left; + } + + private void Consolidate() + { + _degreeList.Clear(); + var maxDegree = (int)Math.Floor(Math.Log(_count) / Math.Log(2)) + 1; + + for (int i = 0; i <= maxDegree; i++) + { + _degreeList.Add(null); + } + + var roots = new List(); + var current = _min; + do + { + roots.Add(current); + current = current.Right; + } while (current != _min); + + foreach (var root in roots) + { + var x = root; + int d = x.Degree; + + while (d < _degreeList.Count && _degreeList[d] != null) + { + var y = _degreeList[d]; + if (x.Value.CompareTo(y.Value) > 0) + { + var temp = x; + x = y; + y = temp; + } + + Link(y, x); + _degreeList[d] = null; + d++; + } + + if (d >= _degreeList.Count) + { + for (int i = _degreeList.Count; i <= d; i++) + _degreeList.Add(null); + } + + _degreeList[d] = x; + } + + _min = null; + foreach (var node in _degreeList) + { + if (node != null) + { + if (_min == null || node.Value.CompareTo(_min.Value) < 0) + { + _min = node; + } + } + } + } + + private void Link(Node child, Node parent) + { + RemoveFromRootList(child); + + child.Parent = parent; + child.Left = child; + child.Right = child; + + if (parent.Child == null) + { + parent.Child = child; + } + else + { + child.Left = parent.Child; + child.Right = parent.Child.Right; + parent.Child.Right.Left = child; + parent.Child.Right = child; + } + + parent.Degree++; + child.Mark = false; + } + + /// + /// 清空 + /// + public void Clear() + { + _min = null; + _count = 0; + _degreeList.Clear(); + } + } + + /// + /// 二项堆 + /// 时间复杂度:插入O(log n),删除最小O(log n),合并O(log n) + /// + public class BinomialHeap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public int Degree { get; set; } + public Node? Child { get; set; } + public Node? Sibling { get; set; } + public Node? Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node? _head; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 最小值 + /// + public T Min + { + get + { + if (_head == null) + throw new InvalidOperationException("Heap is empty"); + + var min = _head; + var current = _head.Sibling; + while (current != null) + { + if (current.Value.CompareTo(min.Value) < 0) + min = current; + current = current.Sibling; + } + return min.Value; + } + } + + /// + /// 创建二项堆 + /// + public BinomialHeap() + { + _head = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + var node = new Node(value); + var newHead = Union(_head, node); + _head = newHead; + _count++; + } + + /// + /// 删除最小元素 + /// + public T DeleteMin() + { + if (_head == null) + throw new InvalidOperationException("Heap is empty"); + + // 找到最小节点及其前驱 + Node minPrev = null; + Node min = _head; + Node prev = null; + Node current = _head; + + while (current != null) + { + if (current.Value.CompareTo(min.Value) < 0) + { + min = current; + minPrev = prev; + } + prev = current; + current = current.Sibling; + } + + // 从根列表中移除最小节点 + if (minPrev == null) + { + _head = min.Sibling; + } + else + { + minPrev.Sibling = min.Sibling; + } + + // 反转最小节点的子节点 + Node newHead = null; + var child = min.Child; + while (child != null) + { + var next = child.Sibling; + child.Sibling = newHead; + child.Parent = null; + newHead = child; + child = next; + } + + // 合并 + _head = Union(_head, newHead); + _count--; + + return min.Value; + } + + /// + /// 查看最小元素 + /// + public T PeekMin() + { + return Min; + } + + /// + /// 合并另一个堆 + /// + public void Merge(BinomialHeap other) + { + if (other == null) + return; + + _head = Union(_head, other._head); + _count += other._count; + other._head = null; + other._count = 0; + } + + private Node Union(Node h1, Node h2) + { + if (h1 == null) + return h2; + if (h2 == null) + return h1; + + Node head; + if (h1.Degree <= h2.Degree) + { + head = h1; + h1 = h1.Sibling; + } + else + { + head = h2; + h2 = h2.Sibling; + } + + Node tail = head; + while (h1 != null && h2 != null) + { + if (h1.Degree <= h2.Degree) + { + tail.Sibling = h1; + h1 = h1.Sibling; + } + else + { + tail.Sibling = h2; + h2 = h2.Sibling; + } + tail = tail.Sibling; + } + + tail.Sibling = h1 ?? h2; + + return Consolidate(head); + } + + private Node Consolidate(Node head) + { + if (head == null) + return null; + + Node prev = null; + Node current = head; + Node next = head.Sibling; + + while (next != null) + { + if (current.Degree != next.Degree || + (next.Sibling != null && next.Sibling.Degree == current.Degree)) + { + prev = current; + current = next; + } + else + { + if (current.Value.CompareTo(next.Value) <= 0) + { + current.Sibling = next.Sibling; + Link(next, current); + } + else + { + if (prev == null) + { + head = next; + } + else + { + prev.Sibling = next; + } + Link(current, next); + current = next; + } + } + next = current.Sibling; + } + + return head; + } + + private void Link(Node child, Node parent) + { + child.Parent = parent; + child.Sibling = parent.Child; + parent.Child = child; + parent.Degree++; + } + + /// + /// 清空 + /// + public void Clear() + { + _head = null; + _count = 0; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs b/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs new file mode 100644 index 0000000..578ad30 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AdvancedSearchTreeUtil.cs @@ -0,0 +1,758 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 高级搜索树工具类 + /// + public static class AdvancedSearchTreeUtil + { + /// + /// 创建树堆(Treap) + /// + public static Treap CreateTreap() where T : IComparable + { + return new Treap(); + } + + /// + /// 创建伸展树(Splay Tree) + /// + public static SplayTree CreateSplayTree() where T : IComparable + { + return new SplayTree(); + } + + /// + /// 创建后缀数组 + /// + public static SuffixArray CreateSuffixArray(string text) + { + return new SuffixArray(text); + } + } + + #region Treap(树堆) + + /// + /// 树堆(Treap) + /// 结合二叉搜索树和堆的性质,通过随机优先级保持平衡 + /// + public class Treap where T : IComparable + { + private class Node + { + public T Value { get; set; } + public int Priority { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public int Count { get; set; } // 子树大小 + public int Size => 1 + (Left?.Size ?? 0) + (Right?.Size ?? 0); + + public Node(T value, int priority) + { + Value = value; + Priority = priority; + Count = 1; + } + } + + private Node _root; + private readonly Random _random; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建树堆 + /// + public Treap() + { + _random = new Random(); + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + _root = Insert(_root, value, _random.Next()); + _count++; + } + + private Node Insert(Node node, T value, int priority) + { + if (node == null) + return new Node(value, priority); + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Insert(node.Left, value, priority); + if (node.Left.Priority > node.Priority) + node = RotateRight(node); + } + else if (cmp > 0) + { + node.Right = Insert(node.Right, value, priority); + if (node.Right.Priority > node.Priority) + node = RotateLeft(node); + } + + return node; + } + + /// + /// 删除元素 + /// + public bool Remove(T value) + { + if (!Contains(value)) return false; + _root = Remove(_root, value); + _count--; + return true; + } + + private Node Remove(Node node, T value) + { + if (node == null) return null; + + int cmp = value.CompareTo(node.Value); + if (cmp < 0) + { + node.Left = Remove(node.Left, value); + } + else if (cmp > 0) + { + node.Right = Remove(node.Right, value); + } + else + { + if (node.Left == null) return node.Right; + if (node.Right == null) return node.Left; + + if (node.Left.Priority > node.Right.Priority) + { + node = RotateRight(node); + node.Right = Remove(node.Right, value); + } + else + { + node = RotateLeft(node); + node.Left = Remove(node.Left, value); + } + } + + return node; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + return Contains(_root, value); + } + + private bool Contains(Node node, T value) + { + if (node == null) return false; + int cmp = value.CompareTo(node.Value); + if (cmp < 0) return Contains(node.Left, value); + if (cmp > 0) return Contains(node.Right, value); + return true; + } + + /// + /// 查找第k小元素 + /// + public T FindKth(int k) + { + if (k < 0 || k >= _count) + throw new ArgumentOutOfRangeException(nameof(k)); + return FindKth(_root, k); + } + + private T FindKth(Node node, int k) + { + int leftSize = node.Left?.Size ?? 0; + if (k < leftSize) + return FindKth(node.Left, k); + if (k > leftSize) + return FindKth(node.Right, k - leftSize - 1); + return node.Value; + } + + /// + /// 获取元素的排名(从0开始) + /// + public int Rank(T value) + { + return Rank(_root, value); + } + + private int Rank(Node node, T value) + { + if (node == null) return 0; + int cmp = value.CompareTo(node.Value); + int leftSize = node.Left?.Size ?? 0; + if (cmp < 0) + return Rank(node.Left, value); + if (cmp > 0) + return leftSize + 1 + Rank(node.Right, value); + return leftSize; + } + + /// + /// 获取最小值 + /// + public T Min() + { + if (_root == null) throw new InvalidOperationException("Treap is empty"); + var node = _root; + while (node.Left != null) node = node.Left; + return node.Value; + } + + /// + /// 获取最大值 + /// + public T Max() + { + if (_root == null) throw new InvalidOperationException("Treap is empty"); + var node = _root; + while (node.Right != null) node = node.Right; + return node.Value; + } + + private static Node RotateRight(Node node) + { + var left = node.Left; + node.Left = left.Right; + left.Right = node; + return left; + } + + private static Node RotateLeft(Node node) + { + var right = node.Right; + node.Right = right.Left; + right.Left = node; + return right; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + #endregion + + #region SplayTree(伸展树) + + /// + /// 伸展树(Splay Tree) + /// 自调整二叉搜索树,通过伸展操作将访问的节点移到根 + /// + public class SplayTree where T : IComparable + { + private class Node + { + public T Value { get; set; } + public Node Left { get; set; } + public Node Right { get; set; } + public Node Parent { get; set; } + + public Node(T value) + { + Value = value; + } + } + + private Node _root; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建伸展树 + /// + public SplayTree() + { + _root = null; + _count = 0; + } + + /// + /// 插入元素 + /// + public void Insert(T value) + { + if (_root == null) + { + _root = new Node(value); + _count++; + return; + } + + Splay(value); + + int cmp = value.CompareTo(_root.Value); + if (cmp == 0) return; // 已存在 + + var newNode = new Node(value); + _count++; + + if (cmp < 0) + { + newNode.Left = _root.Left; + newNode.Right = _root; + _root.Left = null; + } + else + { + newNode.Right = _root.Right; + newNode.Left = _root; + _root.Right = null; + } + + if (newNode.Left != null) newNode.Left.Parent = newNode; + if (newNode.Right != null) newNode.Right.Parent = newNode; + _root = newNode; + } + + /// + /// 删除元素 + /// + public bool Remove(T value) + { + if (_root == null) return false; + + Splay(value); + + if (value.CompareTo(_root.Value) != 0) return false; + + if (_root.Left == null) + { + _root = _root.Right; + if (_root != null) _root.Parent = null; + } + else + { + var rightTree = _root.Right; + _root = _root.Left; + _root.Parent = null; + + // 将左子树的最大值伸展到根 + Splay(value); + _root.Right = rightTree; + if (rightTree != null) rightTree.Parent = _root; + } + + _count--; + return true; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T value) + { + if (_root == null) return false; + + Splay(value); + return value.CompareTo(_root.Value) == 0; + } + + /// + /// 查找元素(会将其伸展到根) + /// + public T Find(T value) + { + if (!Contains(value)) + throw new KeyNotFoundException("Value not found"); + return _root.Value; + } + + /// + /// 获取最小值 + /// + public T Min() + { + if (_root == null) throw new InvalidOperationException("Tree is empty"); + var node = _root; + while (node.Left != null) node = node.Left; + Splay(node.Value); + return _root.Value; + } + + /// + /// 获取最大值 + /// + public T Max() + { + if (_root == null) throw new InvalidOperationException("Tree is empty"); + var node = _root; + while (node.Right != null) node = node.Right; + Splay(node.Value); + return _root.Value; + } + + private void Splay(T value) + { + var node = FindNode(value); + if (node == null) return; + + while (node.Parent != null) + { + var parent = node.Parent; + var grandparent = parent.Parent; + + if (grandparent == null) + { + // Zig 或 Zag + if (node == parent.Left) + RotateRight(parent); + else + RotateLeft(parent); + } + else if (node == parent.Left && parent == grandparent.Left) + { + // Zig-Zig + RotateRight(grandparent); + RotateRight(parent); + } + else if (node == parent.Right && parent == grandparent.Right) + { + // Zag-Zag + RotateLeft(grandparent); + RotateLeft(parent); + } + else if (node == parent.Right && parent == grandparent.Left) + { + // Zig-Zag + RotateLeft(parent); + RotateRight(grandparent); + } + else + { + // Zag-Zig + RotateRight(parent); + RotateLeft(grandparent); + } + } + + _root = node; + } + + private Node FindNode(T value) + { + var node = _root; + while (node != null) + { + int cmp = value.CompareTo(node.Value); + if (cmp < 0) node = node.Left; + else if (cmp > 0) node = node.Right; + else return node; + } + return null; + } + + private void RotateLeft(Node node) + { + var right = node.Right; + node.Right = right.Left; + if (right.Left != null) right.Left.Parent = node; + right.Parent = node.Parent; + if (node.Parent == null) _root = right; + else if (node == node.Parent.Left) node.Parent.Left = right; + else node.Parent.Right = right; + right.Left = node; + node.Parent = right; + } + + private void RotateRight(Node node) + { + var left = node.Left; + node.Left = left.Right; + if (left.Right != null) left.Right.Parent = node; + left.Parent = node.Parent; + if (node.Parent == null) _root = left; + else if (node == node.Parent.Left) node.Parent.Left = left; + else node.Parent.Right = left; + left.Right = node; + node.Parent = left; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + #endregion + + #region SuffixArray(后缀数组) + + /// + /// 后缀数组 + /// 用于字符串处理的高效数据结构 + /// + public class SuffixArray + { + private readonly string _text; + private readonly int[] _suffixArray; + private readonly int[] _lcpArray; + + /// + /// 原始文本 + /// + public string Text => _text; + + /// + /// 文本长度 + /// + public int Length => _text.Length; + + /// + /// 获取后缀数组 + /// + public int[] SuffixArrayValue => _suffixArray; + + /// + /// 获取LCP数组(最长公共前缀) + /// + public int[] LCPArray => _lcpArray; + + /// + /// 创建后缀数组 + /// + public SuffixArray(string text) + { + if (text == null) + throw new ArgumentNullException(nameof(text)); + + _text = text; + _suffixArray = BuildSuffixArray(text); + _lcpArray = BuildLCPArray(text, _suffixArray); + } + + /// + /// 查找模式串所有出现位置 + /// + public List Search(string pattern) + { + var result = new List(); + if (string.IsNullOrEmpty(pattern) || pattern.Length > _text.Length) + return result; + + int left = 0, right = _text.Length - 1; + while (left <= right) + { + int mid = (left + right) / 2; + int cmp = Compare(pattern, _suffixArray[mid]); + if (cmp < 0) right = mid - 1; + else if (cmp > 0) left = mid + 1; + else + { + // 找到匹配,向两边扩展 + int i = mid; + while (i >= 0 && Compare(pattern, _suffixArray[i]) == 0) + { + result.Add(_suffixArray[i]); + i--; + } + i = mid + 1; + while (i < _text.Length && Compare(pattern, _suffixArray[i]) == 0) + { + result.Add(_suffixArray[i]); + i++; + } + break; + } + } + + result.Sort(); + return result; + } + + /// + /// 检查是否包含模式串 + /// + public bool Contains(string pattern) + { + if (string.IsNullOrEmpty(pattern)) return true; + if (pattern.Length > _text.Length) return false; + + int left = 0, right = _text.Length - 1; + while (left <= right) + { + int mid = (left + right) / 2; + int cmp = Compare(pattern, _suffixArray[mid]); + if (cmp == 0) return true; + if (cmp < 0) right = mid - 1; + else left = mid + 1; + } + + return false; + } + + /// + /// 统计模式串出现次数 + /// + public int Count(string pattern) + { + return Search(pattern).Count; + } + + /// + /// 获取最长重复子串 + /// + public string GetLongestRepeatedSubstring() + { + int maxLength = 0, maxIndex = 0; + for (int i = 0; i < _lcpArray.Length; i++) + { + if (_lcpArray[i] > maxLength) + { + maxLength = _lcpArray[i]; + maxIndex = _suffixArray[i]; + } + } + return _text.Substring(maxIndex, maxLength); + } + + /// + /// 获取所有最长公共子串 + /// + public List GetAllLongestCommonSubstrings(int minLength = 2) + { + var result = new List(); + for (int i = 0; i < _lcpArray.Length; i++) + { + if (_lcpArray[i] >= minLength) + { + string substr = _text.Substring(_suffixArray[i], _lcpArray[i]); + if (!result.Contains(substr)) + result.Add(substr); + } + } + return result; + } + + private int Compare(string pattern, int start) + { + for (int i = 0; i < pattern.Length && start + i < _text.Length; i++) + { + if (pattern[i] < _text[start + i]) return -1; + if (pattern[i] > _text[start + i]) return 1; + } + if (pattern.Length > _text.Length - start) return 1; + if (pattern.Length < _text.Length - start) return -1; + return 0; + } + + private static int[] BuildSuffixArray(string text) + { + int n = text.Length; + var sa = new int[n]; + var rank = new int[n]; + var tempRank = new int[n]; + + // 初始化 + for (int i = 0; i < n; i++) + { + sa[i] = i; + rank[i] = text[i]; + } + + // 倍增法 + for (int k = 1; k < n; k *= 2) + { + // 按第二关键字排序 + var tempSa = new int[n]; + int p = 0; + for (int i = n - k; i < n; i++) tempSa[p++] = i; + for (int i = 0; i < n; i++) if (sa[i] >= k) tempSa[p++] = sa[i] - k; + + // 按第一关键字计数排序 + var cnt = new int[Math.Max(256, n)]; + for (int i = 0; i < n; i++) cnt[rank[i]]++; + for (int i = 1; i < cnt.Length; i++) cnt[i] += cnt[i - 1]; + for (int i = n - 1; i >= 0; i--) sa[--cnt[rank[tempSa[i]]]] = tempSa[i]; + + // 重新计算rank + tempRank[sa[0]] = 0; + p = 0; + for (int i = 1; i < n; i++) + { + int curr = rank[sa[i]] * (sa[i] + k < n ? rank[sa[i] + k] + 1 : 0); + int prev = rank[sa[i - 1]] * (sa[i - 1] + k < n ? rank[sa[i - 1] + k] + 1 : 0); + tempRank[sa[i]] = curr == prev ? p : ++p; + } + for (int i = 0; i < n; i++) rank[i] = tempRank[i]; + + if (p == n - 1) break; + } + + return sa; + } + + private static int[] BuildLCPArray(string text, int[] sa) + { + int n = text.Length; + var lcp = new int[n]; + var rank = new int[n]; + + for (int i = 0; i < n; i++) rank[sa[i]] = i; + + int k = 0; + for (int i = 0; i < n; i++) + { + if (rank[i] == 0) + { + k = 0; + continue; + } + + int j = sa[rank[i] - 1]; + while (i + k < n && j + k < n && text[i + k] == text[j + k]) k++; + + lcp[rank[i]] = k; + if (k > 0) k--; + } + + return lcp; + } + } + + #endregion +} diff --git a/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs b/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs new file mode 100644 index 0000000..4f306cb --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/AhoCorasickUtil.cs @@ -0,0 +1,377 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Aho-Corasick 自动机工具类 + /// 用于多模式字符串匹配,线性时间复杂度 + /// 常用于敏感词过滤、关键词检测等场景 + /// + public static class AhoCorasickUtil + { + /// + /// 创建 Aho-Corasick 自动机 + /// + public static AhoCorasickAutomaton Create() + { + return new AhoCorasickAutomaton(); + } + + /// + /// 从关键词集合创建 Aho-Corasick 自动机 + /// + public static AhoCorasickAutomaton Create(IEnumerable keywords) + { + var automaton = new AhoCorasickAutomaton(); + foreach (var keyword in keywords) + { + automaton.AddKeyword(keyword); + } + automaton.Build(); + return automaton; + } + } + + /// + /// Aho-Corasick 自动机实现 + /// + public class AhoCorasickAutomaton + { + private class Node + { + public Dictionary Children { get; } = new Dictionary(); + public Node Fail { get; set; } + public List Output { get; } = new List(); + public int Depth { get; set; } + } + + private readonly Node _root; + private bool _built; + + /// + /// 已添加的关键词数量 + /// + public int KeywordCount { get; private set; } + + /// + /// 是否已构建 + /// + public bool IsBuilt => _built; + + /// + /// 创建 Aho-Corasick 自动机 + /// + public AhoCorasickAutomaton() + { + _root = new Node { Depth = 0 }; + _built = false; + KeywordCount = 0; + } + + /// + /// 添加关键词 + /// + public void AddKeyword(string keyword) + { + if (string.IsNullOrEmpty(keyword)) + throw new ArgumentException("Keyword cannot be null or empty"); + if (_built) + throw new InvalidOperationException("Cannot add keywords after building"); + + var current = _root; + foreach (char c in keyword) + { + if (!current.Children.TryGetValue(c, out var child)) + { + child = new Node { Depth = current.Depth + 1 }; + current.Children[c] = child; + } + current = child; + } + + if (current.Output.Count == 0 || !current.Output.Contains(keyword)) + { + current.Output.Add(keyword); + KeywordCount++; + } + } + + /// + /// 批量添加关键词 + /// + public void AddKeywords(IEnumerable keywords) + { + foreach (var keyword in keywords) + { + AddKeyword(keyword); + } + } + + /// + /// 构建自动机(构建失败指针) + /// + public void Build() + { + if (_built) return; + + var queue = new Queue(); + + // 第一层节点的失败指针都指向根节点 + foreach (var child in _root.Children.Values) + { + child.Fail = _root; + queue.Enqueue(child); + } + + // BFS构建失败指针 + while (queue.Count > 0) + { + var current = queue.Dequeue(); + + foreach (var kvp in current.Children) + { + char c = kvp.Key; + var child = kvp.Value; + + // 沿着失败指针找到能匹配当前字符的节点 + var fail = current.Fail; + while (fail != null && !fail.Children.ContainsKey(c)) + { + fail = fail.Fail; + } + + child.Fail = fail?.Children.GetValueOrDefault(c) ?? _root; + + // 合并输出 + child.Output.AddRange(child.Fail.Output); + + queue.Enqueue(child); + } + } + + _built = true; + } + + /// + /// 在文本中搜索所有匹配 + /// + public IEnumerable Search(string text) + { + if (!_built) + throw new InvalidOperationException("Automaton must be built before searching"); + if (string.IsNullOrEmpty(text)) + yield break; + + var current = _root; + for (int i = 0; i < text.Length; i++) + { + char c = text[i]; + + // 沿着失败指针找到能匹配的节点 + while (current != _root && !current.Children.ContainsKey(c)) + { + current = current.Fail; + } + + if (current.Children.TryGetValue(c, out var next)) + { + current = next; + } + + // 输出所有匹配 + foreach (var output in current.Output) + { + yield return new MatchResult + { + Keyword = output, + StartIndex = i - output.Length + 1, + EndIndex = i + }; + } + } + } + + /// + /// 检查文本是否包含任何关键词 + /// + public bool ContainsAny(string text) + { + if (!_built || string.IsNullOrEmpty(text)) + return false; + + var current = _root; + foreach (char c in text) + { + while (current != _root && !current.Children.ContainsKey(c)) + { + current = current.Fail; + } + + if (current.Children.TryGetValue(c, out var next)) + { + current = next; + } + + if (current.Output.Count > 0) + return true; + } + + return false; + } + + /// + /// 替换文本中的所有关键词 + /// + public string Replace(string text, char replaceChar = '*') + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var chars = text.ToCharArray(); + foreach (var match in Search(text)) + { + for (int i = match.StartIndex; i <= match.EndIndex; i++) + { + chars[i] = replaceChar; + } + } + return new string(chars); + } + + /// + /// 替换文本中的所有关键词(自定义替换字符串) + /// + public string Replace(string text, string replacement) + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var sb = new StringBuilder(); + int lastIndex = 0; + var matches = new List(Search(text)); + + // 按开始位置排序 + matches.Sort((a, b) => a.StartIndex.CompareTo(b.StartIndex)); + + foreach (var match in matches) + { + if (match.StartIndex >= lastIndex) + { + sb.Append(text.Substring(lastIndex, match.StartIndex - lastIndex)); + sb.Append(replacement); + lastIndex = match.EndIndex + 1; + } + } + + sb.Append(text.Substring(lastIndex)); + return sb.ToString(); + } + + /// + /// 高亮文本中的所有关键词 + /// + public string Highlight(string text, string prefix = "[", string suffix = "]") + { + if (!_built || string.IsNullOrEmpty(text)) + return text; + + var sb = new StringBuilder(); + int lastIndex = 0; + var matches = new List(Search(text)); + matches.Sort((a, b) => a.StartIndex.CompareTo(b.StartIndex)); + + // 合并重叠的匹配 + var merged = MergeOverlaps(matches); + + foreach (var match in merged) + { + if (match.StartIndex >= lastIndex) + { + sb.Append(text.Substring(lastIndex, match.StartIndex - lastIndex)); + sb.Append(prefix); + sb.Append(text.Substring(match.StartIndex, match.EndIndex - match.StartIndex + 1)); + sb.Append(suffix); + lastIndex = match.EndIndex + 1; + } + } + + sb.Append(text.Substring(lastIndex)); + return sb.ToString(); + } + + private List MergeOverlaps(List matches) + { + if (matches.Count == 0) return matches; + + var result = new List(); + var current = matches[0]; + + for (int i = 1; i < matches.Count; i++) + { + if (matches[i].StartIndex <= current.EndIndex) + { + // 合并重叠 + current = new MatchResult + { + StartIndex = current.StartIndex, + EndIndex = Math.Max(current.EndIndex, matches[i].EndIndex), + Keyword = current.Keyword + }; + } + else + { + result.Add(current); + current = matches[i]; + } + } + result.Add(current); + + return result; + } + + /// + /// 清空自动机 + /// + public void Clear() + { + _root.Children.Clear(); + _root.Fail = null; + _root.Output.Clear(); + _built = false; + KeywordCount = 0; + } + } + + /// + /// 匹配结果 + /// + public class MatchResult + { + /// + /// 匹配的关键词 + /// + public string Keyword { get; set; } + + /// + /// 开始索引 + /// + public int StartIndex { get; set; } + + /// + /// 结束索引 + /// + public int EndIndex { get; set; } + + /// + /// 匹配长度 + /// + public int Length => EndIndex - StartIndex + 1; + + public override string ToString() + { + return $"{{Keyword: {Keyword}, Start: {StartIndex}, End: {EndIndex}}}"; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/ArrayExtension.cs b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs new file mode 100644 index 0000000..22f5116 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ArrayExtension.cs @@ -0,0 +1,404 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 数组扩展方法 + /// + public static class ArrayExtension + { + #region 空值判断 + + /// + /// 判断数组是否为空或 null + /// + public static bool IsEmpty(this T[]? array) + { + return array == null || array.Length == 0; + } + + /// + /// 判断数组是否非空 + /// + public static bool IsNotEmpty(this T[]? array) + { + return array != null && array.Length > 0; + } + + #endregion + + #region 数组操作 + + /// + /// 数组排序 + /// + public static T[] Sort(this T[]? array) where T : IComparable + { + if (array.IsEmpty()) + { + throw new ArgumentException("Array is empty."); + } + + T[] sortedArray = new T[array.Length]; + array.CopyTo(sortedArray, 0); + Array.Sort(sortedArray); + + return sortedArray; + } + + /// + /// 数组反转 + /// + public static T[] Reverse(this T[]? array) + { + if (array.IsEmpty()) + { + throw new ArgumentException("Array is empty."); + } + + T[] reversedArray = new T[array.Length]; + array.CopyTo(reversedArray, 0); + Array.Reverse(reversedArray); + + return reversedArray; + } + + /// + /// 判断两个数组是否完全相等 + /// + public static bool EqualsTo(this T[]? array, T[]? other) + { + if (array.IsEmpty() && other.IsEmpty()) + { + return true; + } + + if (array.IsEmpty() || other.IsEmpty()) + { + return false; + } + + if (array!.Length != other!.Length) + { + return false; + } + + for (int i = 0; i < array.Length; i++) + { + if (!array[i].Equals(other[i])) + { + return false; + } + } + + return true; + } + + /// + /// 合并两个数组 + /// + public static T[] Concat(this T[]? array, T[]? other) + { + if (array.IsEmpty()) + { + return other ?? Array.Empty(); + } + + if (other.IsEmpty()) + { + return array; + } + + T[] result = new T[array.Length + other.Length]; + array.CopyTo(result, 0); + other.CopyTo(result, array.Length); + + return result; + } + + /// + /// 随机打乱数组顺序(Fisher-Yates 洗牌算法) + /// + public static T[]? Shuffle(this T[]? array) + { + if (array == null || array.Length < 2) + return array; + + var result = (T[])array.Clone(); + + for (int i = result.Length - 1; i > 0; i--) + { +#if NET6_0_OR_GREATER + int j = Random.Shared.Next(i + 1); +#else + int j = new Random(Guid.NewGuid().GetHashCode()).Next(i + 1); +#endif + (result[i], result[j]) = (result[j], result[i]); + } + + return result; + } + + /// + /// 将数组分割成指定大小的块 + /// + /// 原始数组 + /// 每块的大小 + public static IEnumerable Chunk(this T[]? array, int chunkSize) + { + if (array == null) + yield break; + + if (chunkSize <= 0) + throw new ArgumentException("chunkSize must be greater than 0", nameof(chunkSize)); + + for (int i = 0; i < array.Length; i += chunkSize) + { + int remaining = array.Length - i; + int size = Math.Min(chunkSize, remaining); + var chunk = new T[size]; + Array.Copy(array, i, chunk, 0, size); + yield return chunk; + } + } + + public static T[]? Distinct(this T[]? array) + { + if (array == null) + return null; + + return array.Distinct().ToArray(); + } + + /// + /// 按指定键清除数组中的重复元素 + /// + public static T[]? DistinctBy(this T[]? array, Func keySelector) + { + if (array == null) + return null; + + return array.GroupBy(keySelector).Select(g => g.First()).ToArray(); + } + + /// + /// 将数组元素拼接成字符串(支持格式化) + /// + /// 数组 + /// 分隔符 + /// 格式化字符串 + public static string JoinFormat(this T[]? array, string separator, string format) + { + if (array == null || array.Length == 0) + return string.Empty; + + var formatted = array.Select(item => string.Format(format, item)); + return string.Join(separator, formatted); + } + + #endregion + + #region 数组查找 + + + /// + /// 查找数组中满足条件的所有元素的索引 + /// + public static int[] FindAllIndexes(this T[]? array, Func predicate) + { + if (array == null) + return Array.Empty(); + + var indexes = new List(); + for (int i = 0; i < array.Length; i++) + { + if (predicate(array[i])) + { + indexes.Add(i); + } + } + return indexes.ToArray(); + } + + /// + /// 判断数组是否包含指定元素(使用自定义比较器) + /// + public static bool Contains(this T[]? array, T value, IEqualityComparer comparer) + { + if (array == null) + return false; + + return Array.Exists(array, item => comparer.Equals(item, value)); + } + + #endregion + + #region 数组转换 + + + /// + /// 将二维数组展平为一维数组 + /// + public static T[]? Flatten(this T[,]? array) + { + if (array == null) + return null; + + int width = array.GetLength(0); + int height = array.GetLength(1); + var result = new T[width * height]; + + int index = 0; + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + result[index++] = array[i, j]; + } + } + + return result; + } + + #endregion + + #region 数组切片 + + /// + /// 获取数组中指定范围的元素 + /// + /// 数组 + /// 起始索引 + /// 长度 + public static T[]? Slice(this T[]? array, int startIndex, int length) + { + if (array == null) + return null; + + if (startIndex < 0 || startIndex >= array.Length) + throw new ArgumentOutOfRangeException(nameof(startIndex)); + + if (length < 0 || startIndex + length > array.Length) + throw new ArgumentOutOfRangeException(nameof(length)); + + var result = new T[length]; + Array.Copy(array, startIndex, result, 0, length); + return result; + } + + /// + /// 获取数组从指定索引开始到末尾的元素 + /// + public static T[]? Slice(this T[]? array, int startIndex) + { + if (array == null) + return null; + + if (startIndex < 0) + startIndex = 0; + + if (startIndex >= array.Length) + return Array.Empty(); + + int length = array.Length - startIndex; + return Slice(array, startIndex, length); + } + + #endregion + + #region 数组合并 + + /// + /// 合并多个数组 + /// + public static T[] Merge(params T[][]? arrays) + { + if (arrays == null || arrays.Length == 0) + return Array.Empty(); + + int totalLength = 0; + foreach (var array in arrays) + { + if (array != null) + totalLength += array.Length; + } + + var result = new T[totalLength]; + int offset = 0; + + foreach (var array in arrays) + { + if (array != null && array.Length > 0) + { + Array.Copy(array, 0, result, offset, array.Length); + offset += array.Length; + } + } + + return result; + } + + /// + /// 在数组开头添加元素 + /// + public static T[] Prepend(this T[]? array, params T[]? items) + { + if (array == null) + return items ?? Array.Empty(); + + if (items == null || items.Length == 0) + return array; + + var result = new T[array.Length + items.Length]; + Array.Copy(items, 0, result, 0, items.Length); + Array.Copy(array, 0, result, items.Length, array.Length); + return result; + } + + /// + /// 在数组末尾添加元素 + /// + public static T[] Append(this T[]? array, params T[]? items) + { + if (array == null) + return items ?? Array.Empty(); + + if (items == null || items.Length == 0) + return array; + + var result = new T[array.Length + items.Length]; + Array.Copy(array, 0, result, 0, array.Length); + Array.Copy(items, 0, result, array.Length, items.Length); + return result; + } + + #endregion + + #region 数组遍历 + + + /// + /// 遍历数组并对每个元素及其索引执行指定操作 + /// + public static void ForEach(this T[]? array, Action action) + { + if (array == null || action == null) + return; + + for (int i = 0; i < array.Length; i++) + { + action(array[i], i); + } + } + + #endregion + + #region 数组统计 + + + #endregion + } +} diff --git a/EasyTool.Core/CollectionsCategory/BatchUtil.cs b/EasyTool.Core/CollectionsCategory/BatchUtil.cs new file mode 100644 index 0000000..9057cea --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BatchUtil.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 批量处理工具类 + /// + public static class BatchUtil + { + /// + /// 将集合分批 + /// + /// 元素类型 + /// 源集合 + /// 批次大小 + /// 分批后的集合 + public static IEnumerable> Batch(IEnumerable source, int batchSize) + { + if (batchSize <= 0) + throw new ArgumentOutOfRangeException(nameof(batchSize), "批次大小必须大于0"); + + var batch = new List(batchSize); + foreach (var item in source) + { + batch.Add(item); + if (batch.Count >= batchSize) + { + yield return batch; + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + yield return batch; + } + } + + /// + /// 批量处理集合 + /// + /// 元素类型 + /// 源集合 + /// 批次大小 + /// 处理动作 + public static void ProcessBatch(IEnumerable source, int batchSize, Action> action) + { + foreach (var batch in Batch(source, batchSize)) + { + action(batch); + } + } + + /// + /// 异步批量处理集合 + /// + public static async System.Threading.Tasks.Task ProcessBatchAsync( + IEnumerable source, + int batchSize, + Func, System.Threading.Tasks.Task> action) + { + foreach (var batch in Batch(source, batchSize)) + { + await action(batch); + } + } + + /// + /// 批量处理并返回结果 + /// + public static IEnumerable ProcessBatch( + IEnumerable source, + int batchSize, + Func, IEnumerable> action) + { + foreach (var batch in Batch(source, batchSize)) + { + foreach (var result in action(batch)) + { + yield return result; + } + } + } + + /// + /// 异步批量处理并返回结果 + /// + public static async IAsyncEnumerable ProcessBatchAsync( + IEnumerable source, + int batchSize, + Func, System.Threading.Tasks.Task>> action) + { + foreach (var batch in Batch(source, batchSize)) + { + var results = await action(batch); + foreach (var result in results) + { + yield return result; + } + } + } + + /// + /// 并行批量处理 + /// + public static void ProcessBatchParallel( + IEnumerable source, + int batchSize, + Action> action, + int maxDegreeOfParallelism = 4) + { + var options = new System.Threading.Tasks.ParallelOptions + { + MaxDegreeOfParallelism = maxDegreeOfParallelism + }; + + System.Threading.Tasks.Parallel.ForEach(Batch(source, batchSize), options, action); + } + + /// + /// 并行批量处理并返回结果 + /// + public static List ProcessBatchParallel( + IEnumerable source, + int batchSize, + Func, IEnumerable> action, + int maxDegreeOfParallelism = 4) + { + var results = new System.Collections.Concurrent.ConcurrentBag(); + var options = new System.Threading.Tasks.ParallelOptions + { + MaxDegreeOfParallelism = maxDegreeOfParallelism + }; + + System.Threading.Tasks.Parallel.ForEach(Batch(source, batchSize), options, batch => + { + foreach (var result in action(batch)) + { + results.Add(result); + } + }); + + return new List(results); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs b/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs new file mode 100644 index 0000000..b830a1b --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BiDictionaryUtil.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 双向字典工具类 + /// 支持键值双向查找的字典 + /// + public static class BiDictionaryUtil + { + /// + /// 创建双向字典 + /// + public static BiDictionary Create() + where TKey : notnull + where TValue : notnull + { + return new BiDictionary(); + } + + /// + /// 从字典创建双向字典 + /// + public static BiDictionary FromDictionary(IDictionary dictionary) + where TKey : notnull + where TValue : notnull + { + return new BiDictionary(dictionary); + } + } + + /// + /// 双向字典实现 + /// + /// 键类型 + /// 值类型 + public class BiDictionary : IDictionary + where TKey : notnull + where TValue : notnull + { + private readonly Dictionary _forward; + private readonly Dictionary _reverse; + + /// + /// 反向查找字典(值->键) + /// + public IReadOnlyDictionary Reverse => _reverse; + + /// + /// 键集合 + /// + public ICollection Keys => _forward.Keys; + + /// + /// 值集合 + /// + public ICollection Values => _forward.Values; + + /// + /// 元素数量 + /// + public int Count => _forward.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 通过键访问值 + /// + public TValue this[TKey key] + { + get => _forward[key]; + set + { + if (_forward.TryGetValue(key, out var oldValue)) + { + _reverse.Remove(oldValue); + } + _forward[key] = value; + _reverse[value] = key; + } + } + + /// + /// 创建双向字典 + /// + public BiDictionary() + { + _forward = new Dictionary(); + _reverse = new Dictionary(); + } + + /// + /// 从字典创建双向字典 + /// + public BiDictionary(IDictionary dictionary) : this() + { + if (dictionary == null) + throw new ArgumentNullException(nameof(dictionary)); + + foreach (var pair in dictionary) + { + Add(pair.Key, pair.Value); + } + } + + /// + /// 添加键值对 + /// + public void Add(TKey key, TValue value) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (value == null) + throw new ArgumentNullException(nameof(value)); + + if (_forward.ContainsKey(key)) + throw new ArgumentException($"Key '{key}' already exists", nameof(key)); + if (_reverse.ContainsKey(value)) + throw new ArgumentException($"Value '{value}' already exists", nameof(value)); + + _forward.Add(key, value); + _reverse.Add(value, key); + } + + /// + /// 添加键值对 + /// + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// 尝试添加键值对 + /// + public bool TryAdd(TKey key, TValue value) + { + if (_forward.ContainsKey(key) || _reverse.ContainsKey(value)) + return false; + + Add(key, value); + return true; + } + + /// + /// 通过键查找值 + /// + public bool TryGetValue(TKey key, out TValue value) + { + return _forward.TryGetValue(key, out value); + } + + /// + /// 通过值查找键 + /// + public bool TryGetKey(TValue value, out TKey key) + { + return _reverse.TryGetValue(value, out key); + } + + /// + /// 通过键获取值,不存在则返回默认值 + /// + public TValue GetValueOrDefault(TKey key, TValue defaultValue = default) + { + return _forward.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 通过值获取键,不存在则返回默认值 + /// + public TKey GetKeyOrDefault(TValue value, TKey defaultKey = default) + { + return _reverse.TryGetValue(value, out var key) ? key : defaultKey; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _forward.ContainsKey(key); + } + + /// + /// 是否包含值 + /// + public bool ContainsValue(TValue value) + { + return _reverse.ContainsKey(value); + } + + /// + /// 是否包含键值对 + /// + public bool Contains(KeyValuePair item) + { + return _forward.TryGetValue(item.Key, out var value) && + EqualityComparer.Default.Equals(value, item.Value); + } + + /// + /// 移除键值对 + /// + public bool Remove(TKey key) + { + if (!_forward.TryGetValue(key, out var value)) + return false; + + _forward.Remove(key); + _reverse.Remove(value); + return true; + } + + /// + /// 通过值移除键值对 + /// + public bool RemoveByValue(TValue value) + { + if (!_reverse.TryGetValue(value, out var key)) + return false; + + _forward.Remove(key); + _reverse.Remove(value); + return true; + } + + /// + /// 移除键值对 + /// + public bool Remove(KeyValuePair item) + { + if (!Contains(item)) + return false; + + return Remove(item.Key); + } + + /// + /// 清空 + /// + public void Clear() + { + _forward.Clear(); + _reverse.Clear(); + } + + /// + /// 复制到数组 + /// + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + if (arrayIndex < 0 || arrayIndex + Count > array.Length) + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + + int i = arrayIndex; + foreach (var pair in _forward) + { + array[i++] = pair; + } + } + + /// + /// 交换键值(创建新的 Value->Key 映射) + /// + public BiDictionary Inverse() + { + return new BiDictionary(_reverse); + } + + public IEnumerator> GetEnumerator() + { + return _forward.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs b/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs new file mode 100644 index 0000000..265c822 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/BloomFilterUtil.cs @@ -0,0 +1,207 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 布隆过滤器工具类 + /// 一种空间效率很高的概率型数据结构,用于判断元素是否在集合中 + /// 可能存在假阳性(误报),但不存在假阴性 + /// + public static class BloomFilterUtil + { + /// + /// 创建布隆过滤器 + /// + /// 元素类型 + /// 预期元素数量 + /// 可接受的假阳性概率(0-1) + /// 布隆过滤器实例 + public static BloomFilter Create(int expectedItemCount, double falsePositiveProbability = 0.01) + { + return new BloomFilter(expectedItemCount, falsePositiveProbability); + } + + /// + /// 计算最佳位数组大小 + /// + public static int CalculateOptimalBitSize(int expectedItemCount, double falsePositiveProbability) + { + return (int)Math.Ceiling(-expectedItemCount * Math.Log(falsePositiveProbability) / Math.Pow(Math.Log(2), 2)); + } + + /// + /// 计算最佳哈希函数数量 + /// + public static int CalculateOptimalHashCount(int bitSize, int expectedItemCount) + { + return (int)Math.Ceiling(bitSize / (double)expectedItemCount * Math.Log(2)); + } + } + + /// + /// 布隆过滤器实现 + /// + /// 元素类型 + public class BloomFilter + { + private readonly BitArray _bits; + private readonly int _hashCount; + private readonly Func[] _hashFunctions; + private int _itemCount; + + /// + /// 位数组大小 + /// + public int BitSize => _bits.Length; + + /// + /// 哈希函数数量 + /// + public int HashCount => _hashCount; + + /// + /// 已添加元素数量 + /// + public int ItemCount => _itemCount; + + /// + /// 当前估计的假阳性概率 + /// + public double CurrentFalsePositiveProbability + { + get + { + if (_itemCount == 0) return 0; + double ratio = (double)_itemCount * _hashCount / BitSize; + return Math.Pow(1 - Math.Exp(-ratio), _hashCount); + } + } + + /// + /// 创建布隆过滤器 + /// + /// 预期元素数量 + /// 可接受的假阳性概率 + public BloomFilter(int expectedItemCount, double falsePositiveProbability = 0.01) + { + if (expectedItemCount <= 0) + throw new ArgumentOutOfRangeException(nameof(expectedItemCount)); + if (falsePositiveProbability <= 0 || falsePositiveProbability >= 1) + throw new ArgumentOutOfRangeException(nameof(falsePositiveProbability)); + + int bitSize = BloomFilterUtil.CalculateOptimalBitSize(expectedItemCount, falsePositiveProbability); + _hashCount = BloomFilterUtil.CalculateOptimalHashCount(bitSize, expectedItemCount); + + _bits = new BitArray(bitSize); + _hashFunctions = CreateHashFunctions(_hashCount); + _itemCount = 0; + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (item == null) + throw new ArgumentNullException(nameof(item)); + + foreach (var hashFunc in _hashFunctions) + { + int index = Math.Abs(hashFunc(item)) % BitSize; + _bits[index] = true; + } + _itemCount++; + } + + /// + /// 批量添加元素 + /// + public void AddRange(IEnumerable items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + Add(item); + } + } + + /// + /// 检查元素可能存在 + /// + /// true 表示可能存在(可能有假阳性),false 表示一定不存在 + public bool MightContain(T item) + { + if (item == null) + return false; + + foreach (var hashFunc in _hashFunctions) + { + int index = Math.Abs(hashFunc(item)) % BitSize; + if (!_bits[index]) + return false; + } + return true; + } + + /// + /// 清空过滤器 + /// + public void Clear() + { + _bits.SetAll(false); + _itemCount = 0; + } + + /// + /// 获取位数组数据 + /// + public byte[] GetBytes() + { + byte[] bytes = new byte[(_bits.Length + 7) / 8]; + _bits.CopyTo(bytes, 0); + return bytes; + } + + /// + /// 从字节数组恢复位数组 + /// + public void SetBytes(byte[] bytes) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + var newBits = new BitArray(bytes); + if (newBits.Length != _bits.Length) + throw new ArgumentException("Byte array length does not match filter size", nameof(bytes)); + + for (int i = 0; i < _bits.Length; i++) + { + _bits[i] = newBits[i]; + } + } + + private static Func[] CreateHashFunctions(int count) + { + var functions = new Func[count]; + + // 使用双重哈希技术生成多个哈希函数 + // h(i) = hash1(x) + i * hash2(x) + for (int i = 0; i < count; i++) + { + int seed = i * 31 + 17; + functions[i] = item => + { + int hash1 = item?.GetHashCode() ?? 0; + int hash2 = ((hash1 >> 16) ^ hash1) * seed; + return hash1 + hash2 * seed; + }; + } + + return functions; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/CacheUtil.cs b/EasyTool.Core/CollectionsCategory/CacheUtil.cs new file mode 100644 index 0000000..aabbde8 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CacheUtil.cs @@ -0,0 +1,325 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 缓存项 + /// + /// 缓存值类型 + internal class CacheItem + { + public T Value { get; set; } = default!; + public DateTime CreateTime { get; set; } + public DateTime? ExpireTime { get; set; } + public TimeSpan? SlidingExpiration { get; set; } + public DateTime LastAccess { get; set; } + } + + /// + /// 内存缓存工具类 + /// 提供线程安全的内存缓存功能,支持过期时间和滑动过期 + /// + public static class CacheUtil + { + private static readonly ConcurrentDictionary _cache = new(); + private static readonly Timer _cleanupTimer; + private static readonly object _lock = new(); + + static CacheUtil() + { + // 每分钟清理一次过期缓存 + _cleanupTimer = new Timer(_ => CleanupExpired(), null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); + } + + /// + /// 设置缓存 + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 绝对过期时间 + public static void Set(string key, T value, DateTime? absoluteExpiration = null) + { + var item = new CacheItem + { + Value = value, + CreateTime = DateTime.UtcNow, + ExpireTime = absoluteExpiration, + LastAccess = DateTime.UtcNow + }; + _cache[key] = item; + } + + /// + /// 设置缓存(相对过期) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 过期时间间隔 + public static void Set(string key, T value, TimeSpan expiration) + { + Set(key, value, DateTime.UtcNow.Add(expiration)); + } + + /// + /// 设置缓存(滑动过期) + /// + /// 值类型 + /// 缓存键 + /// 缓存值 + /// 滑动过期时间 + /// 最大过期时间 + public static void SetSliding(string key, T value, TimeSpan slidingExpiration, DateTime? absoluteExpiration = null) + { + var item = new CacheItem + { + Value = value, + CreateTime = DateTime.UtcNow, + SlidingExpiration = slidingExpiration, + ExpireTime = absoluteExpiration, + LastAccess = DateTime.UtcNow + }; + _cache[key] = item; + } + + /// + /// 获取缓存 + /// + /// 值类型 + /// 缓存键 + /// 缓存值,如果不存在或已过期则返回默认值 + public static T? Get(string key) + { + if (!_cache.TryGetValue(key, out var obj)) + return default; + + var item = (CacheItem)obj; + + if (IsExpired(item)) + { + _cache.TryRemove(key, out _); + return default; + } + + // 更新滑动过期 + if (item.SlidingExpiration.HasValue) + { + item.LastAccess = DateTime.UtcNow; + } + + return item.Value; + } + + /// + /// 获取或添加缓存 + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 过期时间 + /// 缓存值 + public static T GetOrAdd(string key, Func factory, TimeSpan? expiration = null) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + lock (_lock) + { + value = Get(key); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; + + value = factory(); + if (expiration.HasValue) + Set(key, value, expiration.Value); + else + Set(key, value); + + return value; + } + } + + /// + /// 异步获取或添加缓存 + /// + /// 值类型 + /// 缓存键 + /// 值工厂 + /// 过期时间 + /// 缓存值 + public static async Task GetOrAddAsync(string key, Func> factory, TimeSpan? expiration = null) + { + var value = Get(key); + if (value != null || typeof(T).IsValueType) + { + if (value != null) + return value; + } + + lock (_lock) + { + value = Get(key); + if (value != null || (typeof(T).IsValueType && value != null)) + return value!; + } + + value = await factory(); + + if (expiration.HasValue) + Set(key, value, expiration.Value); + else + Set(key, value); + + return value; + } + + /// + /// 检查缓存是否存在 + /// + /// 缓存键 + /// 是否存在 + public static bool Contains(string key) + { + if (!_cache.TryGetValue(key, out var obj)) + return false; + + var itemType = obj.GetType(); + var isExpired = (bool)itemType.GetMethod("IsExpired")!.Invoke(null, new[] { obj })!; + + if (isExpired) + { + _cache.TryRemove(key, out _); + return false; + } + + return true; + } + + /// + /// 移除缓存 + /// + /// 缓存键 + /// 是否移除成功 + public static bool Remove(string key) + { + return _cache.TryRemove(key, out _); + } + + /// + /// 清空所有缓存 + /// + public static void Clear() + { + _cache.Clear(); + } + + /// + /// 获取缓存数量 + /// + /// 缓存项数量 + public static int Count() + { + return _cache.Count; + } + + /// + /// 获取所有缓存键 + /// + /// 缓存键集合 + public static IEnumerable GetKeys() + { + return _cache.Keys.ToList(); + } + + /// + /// 设置缓存过期时间 + /// + /// 缓存键 + /// 过期时间 + /// 是否设置成功 + public static bool SetExpiration(string key, TimeSpan expiration) + { + if (!_cache.TryGetValue(key, out var obj)) + return false; + + var itemType = obj.GetType(); + var expireTimeProperty = itemType.GetProperty("ExpireTime"); + if (expireTimeProperty != null) + { + expireTimeProperty.SetValue(obj, DateTime.UtcNow.Add(expiration)); + return true; + } + + return false; + } + + private static bool IsExpired(CacheItem item) + { + var now = DateTime.UtcNow; + + // 检查绝对过期 + if (item.ExpireTime.HasValue && now >= item.ExpireTime.Value) + return true; + + // 检查滑动过期 + if (item.SlidingExpiration.HasValue) + { + var expireTime = item.LastAccess.Add(item.SlidingExpiration.Value); + if (now >= expireTime) + return true; + } + + return false; + } + + private static void CleanupExpired() + { + var keysToRemove = new List(); + + foreach (var kvp in _cache) + { + var itemType = kvp.Value.GetType(); + var expireTimeProperty = itemType.GetProperty("ExpireTime"); + var slidingExpirationProperty = itemType.GetProperty("SlidingExpiration"); + var lastAccessProperty = itemType.GetProperty("LastAccess"); + + if (expireTimeProperty != null) + { + var expireTime = (DateTime?)expireTimeProperty.GetValue(kvp.Value); + var slidingExpiration = (TimeSpan?)slidingExpirationProperty?.GetValue(kvp.Value); + var lastAccess = (DateTime)lastAccessProperty!.GetValue(kvp.Value)!; + + var now = DateTime.UtcNow; + + if (expireTime.HasValue && now >= expireTime.Value) + { + keysToRemove.Add(kvp.Key); + } + else if (slidingExpiration.HasValue) + { + var slidingExpire = lastAccess.Add(slidingExpiration.Value); + if (now >= slidingExpire) + { + keysToRemove.Add(kvp.Key); + } + } + } + } + + foreach (var key in keysToRemove) + { + _cache.TryRemove(key, out _); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs b/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs new file mode 100644 index 0000000..0a217e4 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CardinalityAndHashUtil.cs @@ -0,0 +1,598 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 基数估计和一致性哈希工具类 + /// + public static class CardinalityAndHashUtil + { + /// + /// 创建 HyperLogLog + /// + public static HyperLogLog CreateHyperLogLog(int precision = 14) + { + return new HyperLogLog(precision); + } + + /// + /// 创建一致性哈希 + /// + public static ConsistentHash CreateConsistentHash(int virtualNodes = 150) + { + return new ConsistentHash(virtualNodes); + } + + /// + /// 创建线性计数器 + /// + public static LinearCounter CreateLinearCounter(int size) + { + return new LinearCounter(size); + } + } + + /// + /// HyperLogLog 基数估计器 + /// 使用极小内存估计超大集合的不同元素数量 + /// + public class HyperLogLog + { + private readonly byte[] _registers; + private readonly int _precision; + private readonly int _m; + private readonly double _alpha; + + /// + /// 精度参数 + /// + public int Precision => _precision; + + /// + /// 寄存器数量 + /// + public int RegisterCount => _m; + + /// + /// 内存使用(字节) + /// + public int MemoryBytes => _registers.Length; + + /// + /// 创建 HyperLogLog + /// + /// 精度参数(4-16),越大越精确但占用更多内存 + public HyperLogLog(int precision = 14) + { + if (precision < 4 || precision > 16) + throw new ArgumentOutOfRangeException(nameof(precision), "Precision must be between 4 and 16"); + + _precision = precision; + _m = 1 << precision; + _registers = new byte[_m]; + + // 计算 alpha 常数 + switch (_m) + { + case 16: + _alpha = 0.673; + break; + case 32: + _alpha = 0.697; + break; + case 64: + _alpha = 0.709; + break; + default: + _alpha = 0.7213 / (1 + 1.079 / _m); + break; + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + ulong hash = MurmurHash3(data); + int index = (int)(hash >> (64 - _precision)); + int leadingZeros = CountLeadingZeros(hash << _precision) + 1; + + if (leadingZeros > _registers[index]) + { + _registers[index] = (byte)leadingZeros; + } + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public void Add(int value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 添加长整数 + /// + public void Add(long value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计基数 + /// + public long Estimate() + { + double sum = 0; + int zeros = 0; + + foreach (var reg in _registers) + { + sum += Math.Pow(2, -reg); + if (reg == 0) + zeros++; + } + + double estimate = _alpha * _m * _m / sum; + + // 小范围修正 + if (estimate <= 2.5 * _m) + { + if (zeros > 0) + { + estimate = _m * Math.Log((double)_m / zeros); + } + } + // 大范围修正 + else if (estimate > (1L << 32) / 30.0) + { + estimate = -(1L << 32) * Math.Log(1 - estimate / (1L << 32)); + } + + return (long)estimate; + } + + /// + /// 合并另一个 HyperLogLog + /// + public void Merge(HyperLogLog other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._precision != _precision) + throw new ArgumentException("Cannot merge HyperLogLog with different precision"); + + for (int i = 0; i < _m; i++) + { + if (other._registers[i] > _registers[i]) + { + _registers[i] = other._registers[i]; + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_registers, 0, _registers.Length); + } + + private static ulong MurmurHash3(byte[] data) + { + const ulong c1 = 0x87c37b91114253d5; + const ulong c2 = 0x4cf5ad432745937f; + + int length = data.Length; + int blocks = length / 8; + + ulong h1 = 0; + int i = 0; + + for (int j = 0; j < blocks; j++) + { + ulong k1 = BitConverter.ToUInt64(data, i); + i += 8; + + k1 *= c1; + k1 = RotateLeft(k1, 31); + k1 *= c2; + h1 ^= k1; + + h1 = RotateLeft(h1, 27); + h1 = h1 * 5 + 0x52dce729; + } + + ulong remaining = 0; + int remainingLength = length - blocks * 8; + if (remainingLength > 0) + { + for (int j = 0; j < remainingLength; j++) + { + remaining |= (ulong)data[i + j] << (j * 8); + } + + remaining *= c1; + remaining = RotateLeft(remaining, 31); + remaining *= c2; + h1 ^= remaining; + } + + h1 ^= (ulong)length; + h1 ^= h1 >> 33; + h1 *= 0xff51afd7ed558ccd; + h1 ^= h1 >> 33; + h1 *= 0xc4ceb9fe1a85ec53; + h1 ^= h1 >> 33; + + return h1; + } + + private static ulong RotateLeft(ulong x, int k) + { + return (x << k) | (x >> (64 - k)); + } + + private static int CountLeadingZeros(ulong x) + { + if (x == 0) + return 64; + + int n = 0; + if ((x & 0xFFFFFFFF00000000) == 0) { n += 32; x <<= 32; } + if ((x & 0xFFFF000000000000) == 0) { n += 16; x <<= 16; } + if ((x & 0xFF00000000000000) == 0) { n += 8; x <<= 8; } + if ((x & 0xF000000000000000) == 0) { n += 4; x <<= 4; } + if ((x & 0xC000000000000000) == 0) { n += 2; x <<= 2; } + if ((x & 0x8000000000000000) == 0) { n += 1; } + + return n; + } + } + + /// + /// 一致性哈希 + /// 用于分布式系统中的负载均衡 + /// + public class ConsistentHash + { + private readonly SortedDictionary _ring; + private readonly int _virtualNodes; + private readonly HashSet _nodes; + + /// + /// 节点数量 + /// + public int NodeCount => _nodes.Count; + + /// + /// 虚拟节点数量 + /// + public int VirtualNodeCount => _virtualNodes; + + /// + /// 环上总位置数 + /// + public int RingSize => _ring.Count; + + /// + /// 创建一致性哈希 + /// + /// 每个物理节点的虚拟节点数 + public ConsistentHash(int virtualNodes = 150) + { + if (virtualNodes <= 0) + throw new ArgumentOutOfRangeException(nameof(virtualNodes)); + + _ring = new SortedDictionary(); + _virtualNodes = virtualNodes; + _nodes = new HashSet(); + } + + /// + /// 添加节点 + /// + public void AddNode(TNode node) + { + if (node == null) + throw new ArgumentNullException(nameof(node)); + + if (_nodes.Contains(node)) + return; + + _nodes.Add(node); + + for (int i = 0; i < _virtualNodes; i++) + { + ulong hash = HashNode(node, i); + _ring[hash] = node; + } + } + + /// + /// 移除节点 + /// + public bool RemoveNode(TNode node) + { + if (node == null) + throw new ArgumentNullException(nameof(node)); + + if (!_nodes.Remove(node)) + return false; + + for (int i = 0; i < _virtualNodes; i++) + { + ulong hash = HashNode(node, i); + _ring.Remove(hash); + } + + return true; + } + + /// + /// 获取键对应的节点 + /// + public TNode GetNode(string key) + { + if (_ring.Count == 0) + throw new InvalidOperationException("No nodes available"); + + ulong hash = HashKey(key); + + // 查找第一个大于等于 hash 的节点 + foreach (var kvp in _ring) + { + if (kvp.Key >= hash) + return kvp.Value; + } + + // 如果没有找到,返回第一个节点(环形) + return _ring.First().Value; + } + + /// + /// 获取键对应的多个节点(用于复制) + /// + public List GetNodes(string key, int count) + { + if (count <= 0) + throw new ArgumentOutOfRangeException(nameof(count)); + if (_ring.Count == 0) + throw new InvalidOperationException("No nodes available"); + + var result = new List(); + var uniqueNodes = new HashSet(); + ulong hash = HashKey(key); + + // 找到起始位置 + var candidates = _ring.Where(kvp => kvp.Key >= hash).ToList(); + if (candidates.Count == 0) + { + candidates = _ring.ToList(); + } + + int startIndex = 0; + for (int i = 0; i < candidates.Count; i++) + { + if (candidates[i].Key >= hash) + { + startIndex = i; + break; + } + } + + // 收集不同的节点 + int index = startIndex; + while (uniqueNodes.Count < count && uniqueNodes.Count < _nodes.Count) + { + var node = candidates[index % candidates.Count].Value; + if (uniqueNodes.Add(node)) + { + result.Add(node); + } + index++; + } + + return result; + } + + /// + /// 获取节点负责的键范围 + /// + public List GetNodeRanges(TNode node) + { + var ranges = new List(); + + foreach (var kvp in _ring) + { + if (EqualityComparer.Default.Equals(kvp.Value, node)) + { + ranges.Add(kvp.Key); + } + } + + return ranges; + } + + /// + /// 清空所有节点 + /// + public void Clear() + { + _ring.Clear(); + _nodes.Clear(); + } + + /// + /// 获取所有节点 + /// + public IReadOnlyCollection GetNodes() + { + return _nodes; + } + + private ulong HashNode(TNode node, int replicaIndex) + { + string key = $"{node}:#{replicaIndex}"; + return HashKey(key); + } + + private ulong HashKey(string key) + { + byte[] data = System.Text.Encoding.UTF8.GetBytes(key); + return MurmurHash2(data); + } + + private static ulong MurmurHash2(byte[] data) + { + const ulong m = 0xc6a4a7935bd1e995; + const int r = 47; + + ulong h = 0 ^ (ulong)data.Length * m; + + int length = data.Length; + int i = 0; + + while (i + 8 <= length) + { + ulong k = BitConverter.ToUInt64(data, i); + i += 8; + + k *= m; + k ^= k >> r; + k *= m; + + h ^= k; + h *= m; + } + + switch (length - i) + { + case 7: h ^= (ulong)data[i + 6] << 48; goto case 6; + case 6: h ^= (ulong)data[i + 5] << 40; goto case 5; + case 5: h ^= (ulong)data[i + 4] << 32; goto case 4; + case 4: h ^= (ulong)data[i + 3] << 24; goto case 3; + case 3: h ^= (ulong)data[i + 2] << 16; goto case 2; + case 2: h ^= (ulong)data[i + 1] << 8; goto case 1; + case 1: + h ^= data[i]; + h *= m; + break; + } + + h ^= h >> r; + h *= m; + h ^= h >> r; + + return h; + } + } + + /// + /// 线性计数器 + /// 简单的基数估计,适合小到中等规模数据 + /// + public class LinearCounter + { + private readonly BitSet _bits; + private readonly int _size; + + /// + /// 位大小 + /// + public int Size => _size; + + /// + /// 创建线性计数器 + /// + public LinearCounter(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _bits = new BitSet(size); + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + int hash = Math.Abs(Hash(data)) % _size; + _bits.Set(hash); + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 估计基数 + /// + public long Estimate() + { + int setBits = _bits.Cardinality; + if (setBits == 0) + return 0; + + // 使用最大似然估计 + double ratio = (double)(_size - setBits) / _size; + if (ratio <= 0) + return _size; + + return (long)(-_size * Math.Log(ratio)); + } + + /// + /// 合并另一个计数器 + /// + public void Merge(LinearCounter other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._size != _size) + throw new ArgumentException("Cannot merge counters with different sizes"); + + _bits.Or(other._bits); + } + + /// + /// 清空 + /// + public void Clear() + { + _bits.ClearAll(); + } + + private static int Hash(byte[] data) + { + unchecked + { + int hash = 17; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs b/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs new file mode 100644 index 0000000..4923152 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CircularBufferUtil.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 环形缓冲区工具类 + /// 固定大小的循环缓冲区,当满时自动覆盖最旧的数据 + /// 适用于日志缓冲、事件队列、滑动窗口等场景 + /// + public static class CircularBufferUtil + { + /// + /// 创建环形缓冲区 + /// + /// 元素类型 + /// 容量 + /// 环形缓冲区实例 + public static CircularBuffer Create(int capacity) + { + return new CircularBuffer(capacity); + } + + /// + /// 从集合创建环形缓冲区 + /// + public static CircularBuffer FromEnumerable(IEnumerable collection, int capacity) + { + return new CircularBuffer(capacity, collection); + } + } + + /// + /// 环形缓冲区实现 + /// + /// 元素类型 + public class CircularBuffer : IEnumerable, IReadOnlyCollection + { + private readonly T[] _buffer; + private int _head; + private int _tail; + private int _count; + + /// + /// 缓冲区容量 + /// + public int Capacity => _buffer.Length; + + /// + /// 当前元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 是否已满 + /// + public bool IsFull => _count == Capacity; + + /// + /// 索引访问元素 + /// + public T this[int index] + { + get + { + if (index < 0 || index >= _count) + throw new ArgumentOutOfRangeException(nameof(index)); + return _buffer[(_head + index) % Capacity]; + } + } + + /// + /// 创建环形缓冲区 + /// + /// 容量(必须大于0) + public CircularBuffer(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0"); + + _buffer = new T[capacity]; + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 从集合创建环形缓冲区 + /// + public CircularBuffer(int capacity, IEnumerable collection) : this(capacity) + { + if (collection == null) + throw new ArgumentNullException(nameof(collection)); + + foreach (var item in collection) + { + Push(item); + } + } + + /// + /// 添加元素到尾部 + /// + public void Push(T item) + { + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + + if (IsFull) + { + _head = (_head + 1) % Capacity; + } + else + { + _count++; + } + } + + /// + /// 批量添加元素 + /// + public void PushRange(IEnumerable items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + Push(item); + } + } + + /// + /// 从头部移除并返回元素 + /// + public T Pop() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + var item = _buffer[_head]; + _buffer[_head] = default; + _head = (_head + 1) % Capacity; + _count--; + + return item; + } + + /// + /// 从尾部移除并返回元素 + /// + public T PopLast() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + _tail = (_tail - 1 + Capacity) % Capacity; + var item = _buffer[_tail]; + _buffer[_tail] = default; + _count--; + + return item; + } + + /// + /// 查看头部元素(不移除) + /// + public T Peek() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + return _buffer[_head]; + } + + /// + /// 查看尾部元素(不移除) + /// + public T PeekLast() + { + if (IsEmpty) + throw new InvalidOperationException("Buffer is empty"); + + int index = (_tail - 1 + Capacity) % Capacity; + return _buffer[index]; + } + + /// + /// 清空缓冲区 + /// + public void Clear() + { + Array.Clear(_buffer, 0, Capacity); + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 判断是否包含指定元素 + /// + public bool Contains(T item) + { + for (int i = 0; i < _count; i++) + { + int index = (_head + i) % Capacity; + if (EqualityComparer.Default.Equals(_buffer[index], item)) + return true; + } + return false; + } + + /// + /// 复制到数组 + /// + public T[] ToArray() + { + var result = new T[_count]; + for (int i = 0; i < _count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + + /// + /// 获取最新的N个元素 + /// + public T[] GetLatest(int count) + { + count = Math.Min(count, _count); + var result = new T[count]; + + for (int i = 0; i < count; i++) + { + int sourceIndex = (_head + _count - count + i) % Capacity; + result[i] = _buffer[sourceIndex]; + } + + return result; + } + + /// + /// 获取最旧的N个元素 + /// + public T[] GetOldest(int count) + { + count = Math.Min(count, _count); + var result = new T[count]; + + for (int i = 0; i < count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + + return result; + } + + public IEnumerator GetEnumerator() + { + for (int i = 0; i < _count; i++) + { + yield return _buffer[(_head + i) % Capacity]; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs b/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs new file mode 100644 index 0000000..b016ed4 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/CountMinSketchUtil.cs @@ -0,0 +1,211 @@ +using System; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Count-Min Sketch 工具类 + /// 概率数据结构,用于估计元素频率 + /// 空间复杂度远小于精确计数,但有少量误差 + /// + public static class CountMinSketchUtil + { + /// + /// 创建 Count-Min Sketch + /// + /// 宽度(哈希桶数量) + /// 深度(哈希函数数量) + public static CountMinSketch Create(int width = 1000, int depth = 5) + { + return new CountMinSketch(width, depth); + } + + /// + /// 根据期望误差率和置信度创建 + /// + /// 期望误差率(0-1) + /// 置信度(0-1) + public static CountMinSketch CreateWithAccuracy(double errorRate = 0.01, double confidence = 0.99) + { + if (errorRate <= 0 || errorRate >= 1) + throw new ArgumentOutOfRangeException(nameof(errorRate), "Error rate must be between 0 and 1"); + if (confidence <= 0 || confidence >= 1) + throw new ArgumentOutOfRangeException(nameof(confidence), "Confidence must be between 0 and 1"); + + int width = (int)Math.Ceiling(Math.E / errorRate); + int depth = (int)Math.Ceiling(-Math.Log(1 - confidence)); + return new CountMinSketch(width, depth); + } + } + + /// + /// Count-Min Sketch 实现 + /// + public class CountMinSketch + { + private readonly int _width; + private readonly int _depth; + private readonly ulong[,] _counters; + private readonly int[] _seeds; + private ulong _totalCount; + + /// + /// 宽度 + /// + public int Width => _width; + + /// + /// 深度 + /// + public int Depth => _depth; + + /// + /// 总计数 + /// + public ulong TotalCount => _totalCount; + + /// + /// 创建 Count-Min Sketch + /// + public CountMinSketch(int width, int depth) + { + if (width <= 0) + throw new ArgumentOutOfRangeException(nameof(width)); + if (depth <= 0) + throw new ArgumentOutOfRangeException(nameof(depth)); + + _width = width; + _depth = depth; + _counters = new ulong[depth, width]; + _seeds = new int[depth]; + _totalCount = 0; + + // 初始化不同的哈希种子 + var random = new Random(12345); + for (int i = 0; i < depth; i++) + { + _seeds[i] = random.Next(); + } + } + + /// + /// 添加元素 + /// + public void Add(byte[] data) + { + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + _counters[i, index]++; + } + _totalCount++; + } + + /// + /// 添加字符串 + /// + public void Add(string value) + { + Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public void Add(int value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 添加长整数 + /// + public void Add(long value) + { + Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计元素频率 + /// + public ulong Estimate(byte[] data) + { + ulong min = ulong.MaxValue; + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + if (_counters[i, index] < min) + min = _counters[i, index]; + } + return min; + } + + /// + /// 估计字符串频率 + /// + public ulong Estimate(string value) + { + return Estimate(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 估计整数频率 + /// + public ulong Estimate(int value) + { + return Estimate(BitConverter.GetBytes(value)); + } + + /// + /// 合并另一个 Count-Min Sketch + /// + public void Merge(CountMinSketch other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + if (other._width != _width || other._depth != _depth) + throw new ArgumentException("Cannot merge Count-Min Sketch with different dimensions"); + + for (int i = 0; i < _depth; i++) + { + for (int j = 0; j < _width; j++) + { + _counters[i, j] += other._counters[i, j]; + } + } + _totalCount += other._totalCount; + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + _totalCount = 0; + } + + /// + /// 获取估计误差上限 + /// + public double GetErrorBound() + { + if (_totalCount == 0) return 0; + return (double)_totalCount / _width; + } + + private static int Hash(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs index e3f7900..b8620c0 100644 --- a/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs +++ b/EasyTool.Core/CollectionsCategory/DictionaryExtension.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.CollectionsCategory { public static class DictionaryExtension { @@ -14,7 +14,7 @@ public static class DictionaryExtension /// 要获取值的键 /// 如果字典中不存在该键,则返回的默认值 /// 指定键的值,如果字典中不存在该键,则返回默认值 - public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key, TValue defaultValue = default) + public static TValue? GetValueOrDefault(this IDictionary dictionary, TKey key, TValue? defaultValue = default) { if (dictionary.TryGetValue(key, out TValue value)) { @@ -42,25 +42,6 @@ public static void AddRange(this IDictionary destina } } - /// - /// 返回字典中键的集合 - /// - /// 要获取键的字典 - /// 字典中所有键的集合 - public static IEnumerable GetKeys(this IDictionary dictionary) - { - return dictionary.Keys; - } - - /// - /// 返回字典中值的集合 - /// - /// 要获取值的字典 - /// 字典中所有值的集合 - public static IEnumerable GetValues(this IDictionary dictionary) - { - return dictionary.Values; - } /// /// 从字典中删除指定的键 diff --git a/EasyTool.Core/CollectionsCategory/DistributionUtil.cs b/EasyTool.Core/CollectionsCategory/DistributionUtil.cs new file mode 100644 index 0000000..cba410d --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/DistributionUtil.cs @@ -0,0 +1,819 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 概率分布工具类 + /// + public static class DistributionUtil + { + /// + /// 创建离散分布 + /// + public static DiscreteDistribution CreateDiscrete() where T : notnull + { + return new DiscreteDistribution(); + } + + /// + /// 从权重创建离散分布 + /// + public static DiscreteDistribution CreateDiscrete(IEnumerable items, IEnumerable weights) where T : notnull + { + var dist = new DiscreteDistribution(); + var itemList = new List(items); + var weightList = new List(weights); + + if (itemList.Count != weightList.Count) + throw new ArgumentException("Items and weights must have the same length"); + + for (int i = 0; i < itemList.Count; i++) + { + dist.Add(itemList[i], weightList[i]); + } + + return dist; + } + + /// + /// 创建正态分布 + /// + public static NormalDistribution CreateNormal(double mean = 0, double stdDev = 1) + { + return new NormalDistribution(mean, stdDev); + } + + /// + /// 创建泊松分布 + /// + public static PoissonDistribution CreatePoisson(double lambda) + { + return new PoissonDistribution(lambda); + } + + /// + /// 创建指数分布 + /// + public static ExponentialDistribution CreateExponential(double rate) + { + return new ExponentialDistribution(rate); + } + + /// + /// 创建二项分布 + /// + public static BinomialDistribution CreateBinomial(int n, double p) + { + return new BinomialDistribution(n, p); + } + + /// + /// 创建几何分布 + /// + public static GeometricDistribution CreateGeometric(double p) + { + return new GeometricDistribution(p); + } + + /// + /// 创建均匀分布 + /// + public static UniformDistribution CreateUniform(double min, double max) + { + return new UniformDistribution(min, max); + } + + /// + /// 创建均匀整数分布 + /// + public static UniformIntDistribution CreateUniformInt(int min, int max) + { + return new UniformIntDistribution(min, max); + } + } + + /// + /// 离散概率分布 + /// + public class DiscreteDistribution where T : notnull + { + private readonly List _items; + private readonly List _cumulativeWeights; + private double _totalWeight; + private readonly Random _random; + + /// + /// 项目数量 + /// + public int Count => _items.Count; + + /// + /// 总权重 + /// + public double TotalWeight => _totalWeight; + + /// + /// 创建离散分布 + /// + public DiscreteDistribution() + { + _items = new List(); + _cumulativeWeights = new List(); + _totalWeight = 0; + _random = new Random(); + } + + /// + /// 添加项目 + /// + public void Add(T item, double weight) + { + if (weight < 0) + throw new ArgumentOutOfRangeException(nameof(weight), "Weight must be non-negative"); + + _items.Add(item); + _totalWeight += weight; + _cumulativeWeights.Add(_totalWeight); + } + + /// + /// 采样一个项目 + /// + public T Sample() + { + if (_items.Count == 0) + throw new InvalidOperationException("Distribution is empty"); + + double r = _random.NextDouble() * _totalWeight; + + int index = _cumulativeWeights.BinarySearch(r); + if (index < 0) + index = ~index; + + return _items[index]; + } + + /// + /// 采样多个项目 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 获取项目的概率 + /// + public double GetProbability(T item) + { + int index = _items.IndexOf(item); + if (index < 0) + return 0; + + double weight = index == 0 + ? _cumulativeWeights[0] + : _cumulativeWeights[index] - _cumulativeWeights[index - 1]; + + return weight / _totalWeight; + } + + /// + /// 清空分布 + /// + public void Clear() + { + _items.Clear(); + _cumulativeWeights.Clear(); + _totalWeight = 0; + } + } + + /// + /// 正态分布(高斯分布) + /// + public class NormalDistribution + { + private readonly double _mean; + private readonly double _stdDev; + private readonly Random _random; + private double? _spare; + + /// + /// 均值 + /// + public double Mean => _mean; + + /// + /// 标准差 + /// + public double StdDev => _stdDev; + + /// + /// 方差 + /// + public double Variance => _stdDev * _stdDev; + + /// + /// 创建正态分布 + /// + public NormalDistribution(double mean = 0, double stdDev = 1) + { + if (stdDev <= 0) + throw new ArgumentOutOfRangeException(nameof(stdDev), "Standard deviation must be positive"); + + _mean = mean; + _stdDev = stdDev; + _random = new Random(); + _spare = null; + } + + /// + /// 采样一个值(Box-Muller 变换) + /// + public double Sample() + { + if (_spare.HasValue) + { + double result = _spare.Value; + _spare = null; + return result; + } + + double u1, u2, s; + do + { + u1 = 2.0 * _random.NextDouble() - 1.0; + u2 = 2.0 * _random.NextDouble() - 1.0; + s = u1 * u1 + u2 * u2; + } while (s >= 1.0 || s == 0); + + double mul = Math.Sqrt(-2.0 * Math.Log(s) / s); + _spare = _mean + _stdDev * u2 * mul; + return _mean + _stdDev * u1 * mul; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + double exp = -0.5 * Math.Pow((x - _mean) / _stdDev, 2); + return Math.Exp(exp) / (_stdDev * Math.Sqrt(2 * Math.PI)); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + return 0.5 * (1 + Erf((x - _mean) / (_stdDev * Math.Sqrt(2)))); + } + + private static double Erf(double x) + { + // Abramowitz and Stegun approximation + double a1 = 0.254829592; + double a2 = -0.284496736; + double a3 = 1.421413741; + double a4 = -1.453152027; + double a5 = 1.061405429; + double p = 0.3275911; + + int sign = x >= 0 ? 1 : -1; + x = Math.Abs(x); + + double t = 1.0 / (1.0 + p * x); + double y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.Exp(-x * x); + + return sign * y; + } + } + + /// + /// 泊松分布 + /// + public class PoissonDistribution + { + private readonly double _lambda; + private readonly Random _random; + + /// + /// Lambda 参数(期望值) + /// + public double Lambda => _lambda; + + /// + /// 均值 + /// + public double Mean => _lambda; + + /// + /// 方差 + /// + public double Variance => _lambda; + + /// + /// 创建泊松分布 + /// + public PoissonDistribution(double lambda) + { + if (lambda <= 0) + throw new ArgumentOutOfRangeException(nameof(lambda), "Lambda must be positive"); + + _lambda = lambda; + _random = new Random(); + } + + /// + /// 采样一个值(Knuth 算法) + /// + public int Sample() + { + double L = Math.Exp(-_lambda); + int k = 0; + double p = 1.0; + + do + { + k++; + p *= _random.NextDouble(); + } while (p > L); + + return k - 1; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0) + return 0; + return Math.Pow(_lambda, k) * Math.Exp(-_lambda) / Factorial(k); + } + + private static long Factorial(int n) + { + if (n <= 1) + return 1; + long result = 1; + for (int i = 2; i <= n; i++) + result *= i; + return result; + } + } + + /// + /// 指数分布 + /// + public class ExponentialDistribution + { + private readonly double _rate; + private readonly Random _random; + + /// + /// 速率参数(λ) + /// + public double Rate => _rate; + + /// + /// 均值 + /// + public double Mean => 1.0 / _rate; + + /// + /// 方差 + /// + public double Variance => 1.0 / (_rate * _rate); + + /// + /// 创建指数分布 + /// + public ExponentialDistribution(double rate) + { + if (rate <= 0) + throw new ArgumentOutOfRangeException(nameof(rate), "Rate must be positive"); + + _rate = rate; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public double Sample() + { + return -Math.Log(1 - _random.NextDouble()) / _rate; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + if (x < 0) + return 0; + return _rate * Math.Exp(-_rate * x); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + if (x < 0) + return 0; + return 1 - Math.Exp(-_rate * x); + } + } + + /// + /// 二项分布 + /// + public class BinomialDistribution + { + private readonly int _n; + private readonly double _p; + private readonly Random _random; + + /// + /// 试验次数 + /// + public int N => _n; + + /// + /// 成功概率 + /// + public double P => _p; + + /// + /// 均值 + /// + public double Mean => _n * _p; + + /// + /// 方差 + /// + public double Variance => _n * _p * (1 - _p); + + /// + /// 创建二项分布 + /// + public BinomialDistribution(int n, double p) + { + if (n <= 0) + throw new ArgumentOutOfRangeException(nameof(n), "N must be positive"); + if (p < 0 || p > 1) + throw new ArgumentOutOfRangeException(nameof(p), "P must be between 0 and 1"); + + _n = n; + _p = p; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public int Sample() + { + int successes = 0; + for (int i = 0; i < _n; i++) + { + if (_random.NextDouble() < _p) + successes++; + } + return successes; + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0 || k > _n) + return 0; + return BinomialCoefficient(_n, k) * Math.Pow(_p, k) * Math.Pow(1 - _p, _n - k); + } + + private static long BinomialCoefficient(int n, int k) + { + if (k > n - k) + k = n - k; + + long result = 1; + for (int i = 0; i < k; i++) + { + result = result * (n - i) / (i + 1); + } + return result; + } + } + + /// + /// 几何分布 + /// + public class GeometricDistribution + { + private readonly double _p; + private readonly Random _random; + + /// + /// 成功概率 + /// + public double P => _p; + + /// + /// 均值 + /// + public double Mean => 1.0 / _p; + + /// + /// 方差 + /// + public double Variance => (1 - _p) / (_p * _p); + + /// + /// 创建几何分布 + /// + public GeometricDistribution(double p) + { + if (p <= 0 || p > 1) + throw new ArgumentOutOfRangeException(nameof(p), "P must be between 0 and 1"); + + _p = p; + _random = new Random(); + } + + /// + /// 采样一个值(返回第一次成功前的失败次数) + /// + public int Sample() + { + double u = _random.NextDouble(); + return (int)Math.Floor(Math.Log(1 - u) / Math.Log(1 - _p)); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < 0) + return 0; + return Math.Pow(1 - _p, k) * _p; + } + + /// + /// 累积分布函数 + /// + public double CDF(int k) + { + if (k < 0) + return 0; + return 1 - Math.Pow(1 - _p, k + 1); + } + } + + /// + /// 均匀分布(连续) + /// + public class UniformDistribution + { + private readonly double _min; + private readonly double _max; + private readonly Random _random; + + /// + /// 最小值 + /// + public double Min => _min; + + /// + /// 最大值 + /// + public double Max => _max; + + /// + /// 均值 + /// + public double Mean => (_min + _max) / 2; + + /// + /// 方差 + /// + public double Variance => (_max - _min) * (_max - _min) / 12; + + /// + /// 创建均匀分布 + /// + public UniformDistribution(double min, double max) + { + if (max <= min) + throw new ArgumentException("Max must be greater than min"); + + _min = min; + _max = max; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public double Sample() + { + return _min + _random.NextDouble() * (_max - _min); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率密度函数 + /// + public double PDF(double x) + { + if (x < _min || x > _max) + return 0; + return 1.0 / (_max - _min); + } + + /// + /// 累积分布函数 + /// + public double CDF(double x) + { + if (x < _min) + return 0; + if (x > _max) + return 1; + return (x - _min) / (_max - _min); + } + } + + /// + /// 均匀整数分布 + /// + public class UniformIntDistribution + { + private readonly int _min; + private readonly int _max; + private readonly Random _random; + + /// + /// 最小值(包含) + /// + public int Min => _min; + + /// + /// 最大值(包含) + /// + public int Max => _max; + + /// + /// 均值 + /// + public double Mean => (_min + _max) / 2.0; + + /// + /// 方差 + /// + public double Variance => ((_max - _min + 1) * (_max - _min + 1) - 1) / 12.0; + + /// + /// 创建均匀整数分布 + /// + public UniformIntDistribution(int min, int max) + { + if (max < min) + throw new ArgumentException("Max must be greater than or equal to min"); + + _min = min; + _max = max; + _random = new Random(); + } + + /// + /// 采样一个值 + /// + public int Sample() + { + return _random.Next(_min, _max + 1); + } + + /// + /// 采样多个值 + /// + public List Sample(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Sample()); + } + return result; + } + + /// + /// 概率质量函数 + /// + public double PMF(int k) + { + if (k < _min || k > _max) + return 0; + return 1.0 / (_max - _min + 1); + } + + /// + /// 累积分布函数 + /// + public double CDF(int k) + { + if (k < _min) + return 0; + if (k > _max) + return 1; + return (double)(k - _min + 1) / (_max - _min + 1); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs b/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs new file mode 100644 index 0000000..57879fb --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/FenwickTreeUtil.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树状数组(Fenwick Tree / Binary Indexed Tree)工具类 + /// 用于高效计算前缀和,支持单点更新 + /// 时间复杂度:查询和更新都是 O(log n) + /// + public static class FenwickTreeUtil + { + /// + /// 创建树状数组 + /// + /// 大小 + public static FenwickTree Create(int size) + { + return new FenwickTree(size); + } + + /// + /// 从数组创建树状数组 + /// + public static FenwickTree Create(long[] array) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + return new FenwickTree(array); + } + + /// + /// 创建支持范围更新的树状数组 + /// + public static FenwickTreeRange CreateRange(int size) + { + return new FenwickTreeRange(size); + } + + /// + /// 创建二维树状数组 + /// + public static FenwickTree2D Create2D(int rows, int cols) + { + return new FenwickTree2D(rows, cols); + } + } + + /// + /// 树状数组(Fenwick Tree) + /// + public class FenwickTree + { + private readonly long[] _tree; + private readonly int _size; + + /// + /// 大小 + /// + public int Size => _size; + + /// + /// 创建树状数组 + /// + public FenwickTree(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _tree = new long[size + 1]; + } + + /// + /// 从数组创建树状数组 + /// + public FenwickTree(long[] array) : this(array.Length) + { + for (int i = 0; i < array.Length; i++) + { + Update(i, array[i]); + } + } + + /// + /// 单点更新(增加值) + /// + /// 索引(0-based) + /// 增量 + public void Update(int index, long delta) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + index++; // 转为1-based + while (index <= _size) + { + _tree[index] += delta; + index += index & (-index); // LowBit + } + } + + /// + /// 设置指定位置的值 + /// + public void Set(int index, long value) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + long current = Query(index, index); + Update(index, value - current); + } + + /// + /// 查询前缀和 [0, index] + /// + public long Query(int index) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + index++; // 转为1-based + long sum = 0; + while (index > 0) + { + sum += _tree[index]; + index -= index & (-index); // LowBit + } + return sum; + } + + /// + /// 查询区间和 [left, right] + /// + public long Query(int left, int right) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + if (left == 0) + return Query(right); + return Query(right) - Query(left - 1); + } + + /// + /// 获取指定位置的值 + /// + public long Get(int index) + { + return Query(index, index); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_tree, 0, _tree.Length); + } + } + + /// + /// 支持范围更新的树状数组 + /// + public class FenwickTreeRange + { + private readonly FenwickTree _tree1; + private readonly FenwickTree _tree2; + private readonly int _size; + + /// + /// 大小 + /// + public int Size => _size; + + /// + /// 创建支持范围更新的树状数组 + /// + public FenwickTreeRange(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _size = size; + _tree1 = new FenwickTree(size); + _tree2 = new FenwickTree(size); + } + + /// + /// 区间更新 [left, right] 增加delta + /// + public void UpdateRange(int left, int right, long delta) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + _tree1.Update(left, delta); + _tree1.Update(right + 1, -delta); + _tree2.Update(left, delta * (left - 1)); + _tree2.Update(right + 1, -delta * right); + } + + /// + /// 单点更新 + /// + public void Update(int index, long delta) + { + UpdateRange(index, index, delta); + } + + /// + /// 查询前缀和 [0, index] + /// + public long Query(int index) + { + if (index < 0 || index >= _size) + throw new ArgumentOutOfRangeException(nameof(index)); + + return _tree1.Query(index) * index - _tree2.Query(index); + } + + /// + /// 查询区间和 [left, right] + /// + public long Query(int left, int right) + { + if (left < 0 || right >= _size || left > right) + throw new ArgumentException("Invalid range"); + + if (left == 0) + return Query(right); + return Query(right) - Query(left - 1); + } + + /// + /// 清空 + /// + public void Clear() + { + _tree1.Clear(); + _tree2.Clear(); + } + } + + /// + /// 二维树状数组 + /// + public class FenwickTree2D + { + private readonly long[,] _tree; + private readonly int _rows; + private readonly int _cols; + + /// + /// 行数 + /// + public int Rows => _rows; + + /// + /// 列数 + /// + public int Cols => _cols; + + /// + /// 创建二维树状数组 + /// + public FenwickTree2D(int rows, int cols) + { + if (rows <= 0 || cols <= 0) + throw new ArgumentException("Rows and cols must be positive"); + + _rows = rows; + _cols = cols; + _tree = new long[rows + 1, cols + 1]; + } + + /// + /// 单点更新 + /// + public void Update(int row, int col, long delta) + { + if (row < 0 || row >= _rows || col < 0 || col >= _cols) + throw new ArgumentOutOfRangeException(); + + row++; col++; + for (int i = row; i <= _rows; i += i & (-i)) + { + for (int j = col; j <= _cols; j += j & (-j)) + { + _tree[i, j] += delta; + } + } + } + + /// + /// 查询前缀和 [(0,0), (row, col)] + /// + public long Query(int row, int col) + { + if (row < 0 || row >= _rows || col < 0 || col >= _cols) + throw new ArgumentOutOfRangeException(); + + row++; col++; + long sum = 0; + for (int i = row; i > 0; i -= i & (-i)) + { + for (int j = col; j > 0; j -= j & (-j)) + { + sum += _tree[i, j]; + } + } + return sum; + } + + /// + /// 查询矩形区域和 + /// + public long Query(int row1, int col1, int row2, int col2) + { + if (row1 < 0 || row2 >= _rows || row1 > row2 || + col1 < 0 || col2 >= _cols || col1 > col2) + throw new ArgumentException("Invalid range"); + + if (row1 == 0 && col1 == 0) + return Query(row2, col2); + if (row1 == 0) + return Query(row2, col2) - Query(row2, col1 - 1); + if (col1 == 0) + return Query(row2, col2) - Query(row1 - 1, col2); + + return Query(row2, col2) - Query(row1 - 1, col2) + - Query(row2, col1 - 1) + Query(row1 - 1, col1 - 1); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_tree, 0, _tree.Length); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/GraphUtil.cs b/EasyTool.Core/CollectionsCategory/GraphUtil.cs new file mode 100644 index 0000000..ac2ed70 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/GraphUtil.cs @@ -0,0 +1,387 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 图算法工具类 + /// + public static class GraphUtil + { + /// + /// 广度优先搜索 + /// + public static List BFS(Graph graph, T start) where T : notnull + { + var result = new List(); + var visited = new HashSet(); + var queue = new Queue(); + + queue.Enqueue(start); + visited.Add(start); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + result.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + if (!visited.Contains(neighbor)) + { + visited.Add(neighbor); + queue.Enqueue(neighbor); + } + } + } + + return result; + } + + /// + /// 深度优先搜索 + /// + public static List DFS(Graph graph, T start) where T : notnull + { + var result = new List(); + var visited = new HashSet(); + DFSVisit(graph, start, visited, result); + return result; + } + + private static void DFSVisit(Graph graph, T node, HashSet visited, List result) where T : notnull + { + visited.Add(node); + result.Add(node); + + foreach (var neighbor in graph.GetNeighbors(node)) + { + if (!visited.Contains(neighbor)) + { + DFSVisit(graph, neighbor, visited, result); + } + } + } + + /// + /// 最短路径(Dijkstra算法) + /// + public static List? Dijkstra(WeightedGraph graph, T start, T end) where T : notnull + { + var distances = new Dictionary(); + var previous = new Dictionary(); + var unvisited = new HashSet(); + + foreach (var vertex in graph.Vertices) + { + distances[vertex] = double.PositiveInfinity; + unvisited.Add(vertex); + } + distances[start] = 0; + + while (unvisited.Count > 0) + { + var current = default(T)!; + var minDist = double.PositiveInfinity; + + foreach (var vertex in unvisited) + { + if (distances[vertex] < minDist) + { + minDist = distances[vertex]; + current = vertex; + } + } + + if (current == null || current.Equals(end)) + break; + + unvisited.Remove(current); + + foreach (var (neighbor, weight) in graph.GetWeightedNeighbors(current)) + { + var alt = distances[current] + weight; + if (alt < distances[neighbor]) + { + distances[neighbor] = alt; + previous[neighbor] = current; + } + } + } + + // 重建路径 + if (!previous.ContainsKey(end) && !start.Equals(end)) + return null; + + var path = new List(); + var current2 = end; + while (current2 != null) + { + path.Insert(0, current2); + current2 = previous.TryGetValue(current2, out var prev) ? prev : default; + if (current2 == null && !path[0].Equals(start)) + return null; + } + + return path; + } + + /// + /// 拓扑排序 + /// + public static List? TopologicalSort(Graph graph) where T : notnull + { + var result = new List(); + var visited = new HashSet(); + var tempMarked = new HashSet(); + + foreach (var vertex in graph.Vertices) + { + if (!visited.Contains(vertex)) + { + if (!TopologicalVisit(graph, vertex, visited, tempMarked, result)) + return null; // 存在环 + } + } + + result.Reverse(); + return result; + } + + private static bool TopologicalVisit(Graph graph, T node, HashSet visited, HashSet tempMarked, List result) where T : notnull + { + if (tempMarked.Contains(node)) + return false; // 存在环 + + if (visited.Contains(node)) + return true; + + tempMarked.Add(node); + + foreach (var neighbor in graph.GetNeighbors(node)) + { + if (!TopologicalVisit(graph, neighbor, visited, tempMarked, result)) + return false; + } + + tempMarked.Remove(node); + visited.Add(node); + result.Add(node); + return true; + } + + /// + /// 检测环 + /// + public static bool HasCycle(Graph graph) where T : notnull + { + var visited = new HashSet(); + var recursionStack = new HashSet(); + + foreach (var vertex in graph.Vertices) + { + if (HasCycleDFS(graph, vertex, visited, recursionStack)) + return true; + } + + return false; + } + + private static bool HasCycleDFS(Graph graph, T node, HashSet visited, HashSet recursionStack) where T : notnull + { + if (recursionStack.Contains(node)) + return true; + + if (visited.Contains(node)) + return false; + + visited.Add(node); + recursionStack.Add(node); + + foreach (var neighbor in graph.GetNeighbors(node)) + { + if (HasCycleDFS(graph, neighbor, visited, recursionStack)) + return true; + } + + recursionStack.Remove(node); + return false; + } + + /// + /// 连通分量 + /// + public static List> GetConnectedComponents(Graph graph) where T : notnull + { + var components = new List>(); + var visited = new HashSet(); + + foreach (var vertex in graph.Vertices) + { + if (!visited.Contains(vertex)) + { + var component = new List(); + var queue = new Queue(); + queue.Enqueue(vertex); + visited.Add(vertex); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + component.Add(current); + + foreach (var neighbor in graph.GetNeighbors(current)) + { + if (!visited.Contains(neighbor)) + { + visited.Add(neighbor); + queue.Enqueue(neighbor); + } + } + } + + components.Add(component); + } + } + + return components; + } + } + + /// + /// 图数据结构 + /// + public class Graph where T : notnull + { + private readonly Dictionary> _adjacencyList = new(); + private readonly bool _directed; + + public Graph(bool directed = false) + { + _directed = directed; + } + + /// + /// 所有顶点 + /// + public IEnumerable Vertices => _adjacencyList.Keys; + + /// + /// 边数 + /// + public int EdgeCount { get; private set; } + + /// + /// 顶点数 + /// + public int VertexCount => _adjacencyList.Count; + + /// + /// 添加顶点 + /// + public void AddVertex(T vertex) + { + if (!_adjacencyList.ContainsKey(vertex)) + { + _adjacencyList[vertex] = new List(); + } + } + + /// + /// 添加边 + /// + public void AddEdge(T from, T to) + { + AddVertex(from); + AddVertex(to); + + _adjacencyList[from].Add(to); + if (!_directed) + { + _adjacencyList[to].Add(from); + } + EdgeCount++; + } + + /// + /// 移除边 + /// + public void RemoveEdge(T from, T to) + { + if (_adjacencyList.TryGetValue(from, out var neighbors)) + { + neighbors.Remove(to); + } + + if (!_directed && _adjacencyList.TryGetValue(to, out var neighbors2)) + { + neighbors2.Remove(from); + } + + EdgeCount--; + } + + /// + /// 获取邻居 + /// + public IEnumerable GetNeighbors(T vertex) + { + return _adjacencyList.TryGetValue(vertex, out var neighbors) + ? neighbors + : Enumerable.Empty(); + } + + /// + /// 是否有边 + /// + public bool HasEdge(T from, T to) + { + return _adjacencyList.TryGetValue(from, out var neighbors) && neighbors.Contains(to); + } + } + + /// + /// 带权重的图 + /// + public class WeightedGraph where T : notnull + { + private readonly Dictionary> _adjacencyList = new(); + private readonly bool _directed; + + public WeightedGraph(bool directed = false) + { + _directed = directed; + } + + public IEnumerable Vertices => _adjacencyList.Keys; + public int VertexCount => _adjacencyList.Count; + + public void AddVertex(T vertex) + { + if (!_adjacencyList.ContainsKey(vertex)) + { + _adjacencyList[vertex] = new List<(T, double)>(); + } + } + + public void AddEdge(T from, T to, double weight) + { + AddVertex(from); + AddVertex(to); + + _adjacencyList[from].Add((to, weight)); + if (!_directed) + { + _adjacencyList[to].Add((from, weight)); + } + } + + public IEnumerable<(T Vertex, double Weight)> GetWeightedNeighbors(T vertex) + { + return _adjacencyList.TryGetValue(vertex, out var neighbors) + ? neighbors + : Enumerable.Empty<(T, double)>(); + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/GroupUtil.cs b/EasyTool.Core/CollectionsCategory/GroupUtil.cs new file mode 100644 index 0000000..0e44d13 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/GroupUtil.cs @@ -0,0 +1,346 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 分组工具类 + /// + public static class GroupUtil + { + /// + /// 按指定数量分组 + /// + public static List> Chunk(IEnumerable source, int size) + { + if (size <= 0) + throw new ArgumentException("分组大小必须大于0", nameof(size)); + + var result = new List>(); + var current = new List(size); + + foreach (var item in source) + { + current.Add(item); + + if (current.Count == size) + { + result.Add(current); + current = new List(size); + } + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 按条件分组 + /// + public static List> GroupWhile(IEnumerable source, Func predicate) + { + var result = new List>(); + var current = new List(); + + foreach (var item in source) + { + if (predicate(item) && current.Count > 0) + { + result.Add(current); + current = new List(); + } + + current.Add(item); + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 按相邻相同元素分组 + /// + public static List> GroupAdjacent(IEnumerable source) + { + var result = new List>(); + List? current = null; + + foreach (var item in source) + { + if (current == null || !EqualityComparer.Default.Equals(current[0], item)) + { + current = new List { item }; + result.Add(current); + } + else + { + current.Add(item); + } + } + + return result; + } + + /// + /// 按相邻相同元素分组(使用比较器) + /// + public static List> GroupAdjacent(IEnumerable source, IEqualityComparer comparer) + { + var result = new List>(); + List? current = null; + + foreach (var item in source) + { + if (current == null || !comparer.Equals(current[0], item)) + { + current = new List { item }; + result.Add(current); + } + else + { + current.Add(item); + } + } + + return result; + } + + /// + /// 交替分组 + /// + public static (List First, List Second) Alternate(IEnumerable source) + { + var first = new List(); + var second = new List(); + var isFirst = true; + + foreach (var item in source) + { + if (isFirst) + first.Add(item); + else + second.Add(item); + + isFirst = !isFirst; + } + + return (first, second); + } + + /// + /// 分割集合 + /// + public static (List True, List False) Partition(IEnumerable source, Func predicate) + { + var trueItems = new List(); + var falseItems = new List(); + + foreach (var item in source) + { + if (predicate(item)) + trueItems.Add(item); + else + falseItems.Add(item); + } + + return (trueItems, falseItems); + } + + /// + /// 交错合并两个集合 + /// + public static IEnumerable Interleave(IEnumerable first, IEnumerable second) + { + using var e1 = first.GetEnumerator(); + using var e2 = second.GetEnumerator(); + + while (e1.MoveNext()) + { + yield return e1.Current; + + if (e2.MoveNext()) + yield return e2.Current; + } + + while (e2.MoveNext()) + { + yield return e2.Current; + } + } + + /// + /// 按滑动窗口分组 + /// + public static List> Window(IEnumerable source, int size, int step = 1) + { + if (size <= 0) + throw new ArgumentException("窗口大小必须大于0", nameof(size)); + + if (step <= 0) + throw new ArgumentException("步进必须大于0", nameof(step)); + + var list = source.ToList(); + var result = new List>(); + + for (int i = 0; i <= list.Count - size; i += step) + { + result.Add(list.Skip(i).Take(size).ToList()); + } + + return result; + } + + /// + /// 按累积条件分组 + /// + public static List> GroupByAccumulator(IEnumerable source, Func shouldGroup) + { + var result = new List>(); + var current = new List(); + T? lastItem = default; + + foreach (var item in source) + { + if (lastItem == null || shouldGroup(lastItem, item)) + { + current.Add(item); + } + else + { + if (current.Count > 0) + result.Add(current); + current = new List { item }; + } + + lastItem = item; + } + + if (current.Count > 0) + { + result.Add(current); + } + + return result; + } + + /// + /// 获取笛卡尔积 + /// + public static IEnumerable<(T1 First, T2 Second)> CartesianProduct( + IEnumerable first, IEnumerable second) + { + foreach (var item1 in first) + { + foreach (var item2 in second) + { + yield return (item1, item2); + } + } + } + + /// + /// 获取多个集合的笛卡尔积 + /// + public static IEnumerable> CartesianProduct(IEnumerable> sources) + { + var sourceList = sources.ToList(); + + if (sourceList.Count == 0) + { + yield return new List(); + yield break; + } + + var first = sourceList[0]; + var rest = sourceList.Skip(1); + + foreach (var item in first) + { + foreach (var restCombination in CartesianProduct(rest)) + { + var combination = new List { item }; + combination.AddRange(restCombination); + yield return combination; + } + } + } + + /// + /// 获取排列组合 + /// + public static IEnumerable> Combinations(IEnumerable source, int count) + { + var list = source.ToList(); + + if (count > list.Count) + yield break; + + if (count == 0) + { + yield return new List(); + yield break; + } + + if (count == 1) + { + foreach (var item in list) + { + yield return new List { item }; + } + yield break; + } + + for (int i = 0; i <= list.Count - count; i++) + { + foreach (var restCombination in Combinations(list.Skip(i + 1), count - 1)) + { + var combination = new List { list[i] }; + combination.AddRange(restCombination); + yield return combination; + } + } + } + + /// + /// 获取全排列 + /// + public static IEnumerable> Permutations(IEnumerable source) + { + var list = source.ToList(); + + if (list.Count == 0) + { + yield return new List(); + yield break; + } + + if (list.Count == 1) + { + yield return new List(list); + yield break; + } + + for (int i = 0; i < list.Count; i++) + { + var current = list[i]; + var remaining = list.Take(i).Concat(list.Skip(i + 1)); + + foreach (var permutation in Permutations(remaining)) + { + var result = new List { current }; + result.AddRange(permutation); + yield return result; + } + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs b/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs new file mode 100644 index 0000000..1f45265 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/HeavyKeeperUtil.cs @@ -0,0 +1,419 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Heavy Keeper 工具类 + /// 用于检测数据流中的 Heavy Hitters(高频元素) + /// 基于概率衰减的计数器,适合实时数据流分析 + /// + public static class HeavyKeeperUtil + { + /// + /// 创建 Heavy Keeper + /// + /// 宽度(哈希桶数量) + /// 深度(哈希函数数量) + /// 衰减因子(0-1) + public static HeavyKeeper Create(int width = 1000, int depth = 5, double decay = 0.9) + { + return new HeavyKeeper(width, depth, decay); + } + } + + /// + /// Heavy Keeper 实现 + /// + public class HeavyKeeper + { + private readonly int _width; + private readonly int _depth; + private readonly double _decay; + private readonly ulong[,] _counters; + private readonly ulong[,] _fingerprints; + private readonly int[] _seeds; + private ulong _totalCount; + + /// + /// 宽度 + /// + public int Width => _width; + + /// + /// 深度 + /// + public int Depth => _depth; + + /// + /// 衰减因子 + /// + public double Decay => _decay; + + /// + /// 总计数 + /// + public ulong TotalCount => _totalCount; + + /// + /// 创建 Heavy Keeper + /// + public HeavyKeeper(int width, int depth, double decay = 0.9) + { + if (width <= 0) + throw new ArgumentOutOfRangeException(nameof(width)); + if (depth <= 0) + throw new ArgumentOutOfRangeException(nameof(depth)); + if (decay <= 0 || decay >= 1) + throw new ArgumentOutOfRangeException(nameof(decay), "Decay must be between 0 and 1"); + + _width = width; + _depth = depth; + _decay = decay; + _counters = new ulong[depth, width]; + _fingerprints = new ulong[depth, width]; + _seeds = new int[depth]; + _totalCount = 0; + + var random = new Random(12345); + for (int i = 0; i < depth; i++) + { + _seeds[i] = random.Next(); + } + } + + /// + /// 添加元素 + /// + /// 估计的频率 + public ulong Add(byte[] data) + { + ulong fingerprint = ComputeFingerprint(data); + ulong maxCount = 0; + + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + + if (_fingerprints[i, index] == fingerprint) + { + // 匹配,增加计数 + _counters[i, index]++; + if (_counters[i, index] > maxCount) + maxCount = _counters[i, index]; + } + else if (_counters[i, index] == 0) + { + // 空桶,直接放入 + _fingerprints[i, index] = fingerprint; + _counters[i, index] = 1; + if (maxCount == 0) maxCount = 1; + } + else + { + // 不匹配,以一定概率衰减并替换 + double probability = Math.Pow(_decay, _counters[i, index]); + if (RandomDouble() < probability) + { + _counters[i, index]--; + if (_counters[i, index] == 0) + { + _fingerprints[i, index] = fingerprint; + _counters[i, index] = 1; + if (maxCount == 0) maxCount = 1; + } + } + } + } + + _totalCount++; + return maxCount; + } + + /// + /// 添加字符串 + /// + public ulong Add(string value) + { + return Add(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 添加整数 + /// + public ulong Add(int value) + { + return Add(BitConverter.GetBytes(value)); + } + + /// + /// 估计元素频率 + /// + public ulong Estimate(byte[] data) + { + ulong fingerprint = ComputeFingerprint(data); + ulong maxCount = 0; + + for (int i = 0; i < _depth; i++) + { + int hash = Hash(data, _seeds[i]); + int index = Math.Abs(hash) % _width; + + if (_fingerprints[i, index] == fingerprint) + { + if (_counters[i, index] > maxCount) + maxCount = _counters[i, index]; + } + } + + return maxCount; + } + + /// + /// 估计字符串频率 + /// + public ulong Estimate(string value) + { + return Estimate(System.Text.Encoding.UTF8.GetBytes(value)); + } + + /// + /// 获取 Top-K 高频元素 + /// + public List<(T Item, ulong Count)> GetTopK(IEnumerable items, int k) + { + var counts = new Dictionary(); + + foreach (var item in items) + { + byte[] data; + if (typeof(T) == typeof(string)) + data = System.Text.Encoding.UTF8.GetBytes(item.ToString()); + else if (typeof(T) == typeof(int)) + data = BitConverter.GetBytes(Convert.ToInt32(item)); + else + data = System.Text.Encoding.UTF8.GetBytes(item.ToString()); + + ulong count = Estimate(data); + if (count > 0) + { + counts[item] = count; + } + } + + return counts.OrderByDescending(x => x.Value) + .Take(k) + .Select(x => (x.Key, x.Value)) + .ToList(); + } + + /// + /// 清空 + /// + public void Clear() + { + Array.Clear(_counters, 0, _counters.Length); + Array.Clear(_fingerprints, 0, _fingerprints.Length); + _totalCount = 0; + } + + private static ulong ComputeFingerprint(byte[] data) + { + unchecked + { + ulong hash = 14695981039346656037; + foreach (byte b in data) + { + hash ^= b; + hash *= 1099511628211; + } + return hash; + } + } + + private static int Hash(byte[] data, int seed) + { + unchecked + { + int hash = seed; + foreach (byte b in data) + { + hash = hash * 31 + b; + } + return hash; + } + } + + private static double RandomDouble() + { +#if NETSTANDARD2_1 + return _random.NextDouble(); +#else + return Random.Shared.NextDouble(); +#endif + } + + private static readonly Random _random = new Random(); + } + + /// + /// 流式 Top-K 工具 + /// 使用最小堆维护 Top-K 元素 + /// + public class StreamTopK + { + private readonly int _k; + private readonly Dictionary _counts; + private readonly HeavyKeeperPriorityQueue _minHeap; + + /// + /// K值 + /// + public int K => _k; + + /// + /// 当前元素数量 + /// + public int Count => _counts.Count; + + /// + /// 创建流式 Top-K + /// + public StreamTopK(int k) + { + if (k <= 0) + throw new ArgumentOutOfRangeException(nameof(k)); + + _k = k; + _counts = new Dictionary(); + _minHeap = new HeavyKeeperPriorityQueue(); + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (_counts.ContainsKey(item)) + { + _counts[item]++; + } + else + { + _counts[item] = 1; + } + } + + /// + /// 获取当前 Top-K + /// + public List<(T Item, ulong Count)> GetTopK() + { + var heap = new HeavyKeeperPriorityQueue(); + foreach (var kvp in _counts) + { + if (heap.Count < _k) + { + heap.Enqueue(kvp.Key, kvp.Value); + } + else if (kvp.Value > heap.Peek().Priority) + { + heap.Dequeue(); + heap.Enqueue(kvp.Key, kvp.Value); + } + } + + var result = new List<(T, ulong)>(); + while (heap.Count > 0) + { + var item = heap.Dequeue(); + result.Add((item.Element, item.Priority)); + } + + result.Reverse(); + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _counts.Clear(); + _minHeap.Clear(); + } + } + + // 内部使用的优先队列元素 + internal struct PriorityQueueElement + { + public T Element { get; } + public ulong Value { get; } + + public PriorityQueueElement(T element, ulong value) + { + Element = element; + Value = value; + } + } + + // 简单的优先队列实现(内部使用,避免与 PriorityQueueUtil 中的 PriorityQueue 冲突) + internal class HeavyKeeperPriorityQueue where TPriority : IComparable + { + private readonly List<(T Element, TPriority Priority)> _heap = new(); + + public int Count => _heap.Count; + + public void Enqueue(T element, TPriority priority) + { + _heap.Add((element, priority)); + int i = _heap.Count - 1; + while (i > 0) + { + int parent = (i - 1) / 2; + if (_heap[parent].Priority.CompareTo(priority) <= 0) break; + _heap[i] = _heap[parent]; + i = parent; + } + _heap[i] = (element, priority); + } + + public (T Element, TPriority Priority) Dequeue() + { + if (_heap.Count == 0) throw new InvalidOperationException("Queue is empty"); + var result = _heap[0]; + var last = _heap[_heap.Count - 1]; + _heap.RemoveAt(_heap.Count - 1); + + if (_heap.Count > 0) + { + int i = 0; + while (true) + { + int left = 2 * i + 1; + if (left >= _heap.Count) break; + int right = left + 1; + int smallest = left; + if (right < _heap.Count && _heap[right].Priority.CompareTo(_heap[left].Priority) < 0) + smallest = right; + if (last.Priority.CompareTo(_heap[smallest].Priority) <= 0) break; + _heap[i] = _heap[smallest]; + i = smallest; + } + _heap[i] = last; + } + + return result; + } + + public (T Element, TPriority Priority) Peek() + { + if (_heap.Count == 0) throw new InvalidOperationException("Queue is empty"); + return _heap[0]; + } + + public void Clear() => _heap.Clear(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs new file mode 100644 index 0000000..2f8a71b --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/IEnumerableExtensions.cs @@ -0,0 +1,478 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; + +namespace EasyTool.CollectionsCategory +{ + /// + /// IEnumerable 通用扩展方法 + /// + public static class IEnumerableExtensions + { + #region 空值处理 + + /// + /// 对 List 等集合 Foreach 的时候不用在上层判空,直接加上这个就好 + /// + public static IEnumerable CheckNull(this IEnumerable values) + { + return values is null ? new List(0) : values; + } + + /// + /// 判断集合是否为空或 null + /// + public static bool IsNullOrEmpty(this IEnumerable source) + { + return source == null || !source.Any(); + } + + /// + /// 判断集合是否非空 + /// + public static bool IsNotEmpty(this IEnumerable source) + { + return source != null && source.Any(); + } + + #endregion + + #region 遍历操作 + + /// + /// 遍历集合并对每个元素执行指定操作 + /// + public static void ForEach(this IEnumerable source, Action action) + { + if (source == null || action == null) + return; + + foreach (var item in source) + { + action(item); + } + } + + /// + /// 遍历集合并对每个元素及其索引执行指定操作 + /// + public static void ForEach(this IEnumerable source, Action action) + { + if (source == null || action == null) + return; + + int index = 0; + foreach (var item in source) + { + action(item, index++); + } + } + + #endregion + + #region 集合运算 + + /// + /// 求集合的笛卡尔积 + /// + public static IEnumerable> Cartesian(this IEnumerable> sequences) + { + IEnumerable> tempProduct = new[] { Enumerable.Empty() }; + return sequences.Aggregate(tempProduct, + (accumulator, sequence) => + from accseq in accumulator + from item in sequence + select accseq.Concat(new[] { item + })); + } + + /// + /// 按指定键去重 + /// + public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) + { + if (source == null) + yield break; + + var seenKeys = new HashSet(); + foreach (var item in source) + { + if (seenKeys.Add(keySelector(item))) + { + yield return item; + } + } + } + + /// + /// 批量处理集合 + /// + /// 每批的大小 + public static IEnumerable> Batch(this IEnumerable source, int batchSize) + { + if (source == null) + yield break; + + if (batchSize <= 0) + throw new ArgumentException("batchSize must be greater than 0", nameof(batchSize)); + + var batch = new List(batchSize); + foreach (var item in source) + { + batch.Add(item); + if (batch.Count == batchSize) + { + yield return batch; + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + yield return batch; + } + } + + #endregion + + #region 转换操作 + + /// + /// 将集合转换为 DataTable + /// + public static DataTable ToDataTable(this IEnumerable source) + { + var table = new DataTable(typeof(T).Name); + + var properties = typeof(T).GetProperties(); + foreach (var prop in properties) + { + table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); + } + + foreach (var item in source) + { + var row = table.NewRow(); + foreach (var prop in properties) + { + row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; + } + table.Rows.Add(row); + } + + return table; + } + + /// + /// 将集合转换为 HashSet + /// + public static HashSet ToHashSet(this IEnumerable source) + { + if (source == null) + return new HashSet(); + + return new HashSet(source); + } + + /// + /// 将集合转换为 Queue + /// + public static Queue ToQueue(this IEnumerable source) + { + if (source == null) + return new Queue(); + + return new Queue(source); + } + + /// + /// 将集合转换为 Stack + /// + public static Stack ToStack(this IEnumerable source) + { + if (source == null) + return new Stack(); + + return new Stack(source); + } + + /// + /// 将集合转换为 LinkedList + /// + public static LinkedList ToLinkedList(this IEnumerable source) + { + if (source == null) + return new LinkedList(); + + return new LinkedList(source); + } + + #endregion + + #region 连接操作 + + /// + /// 将集合元素连接成字符串 + /// + /// 分隔符 + public static string JoinAsString(this IEnumerable source, string separator = ",") + { + if (source == null) + return string.Empty; + + return string.Join(separator, source); + } + + /// + /// 将集合元素连接成字符串(使用格式化) + /// + /// 分隔符 + /// 格式化字符串 + public static string JoinAsString(this IEnumerable source, string separator, string format) + { + if (source == null) + return string.Empty; + + return string.Join(separator, source.Select(item => string.Format(format, item))); + } + + #endregion + + #region 分页操作 + + /// + /// 分页 + /// + /// 页码(从1开始) + /// 每页大小 + public static IEnumerable Page(this IEnumerable source, int pageIndex, int pageSize) + { + if (source == null || pageIndex < 1 || pageSize < 1) + yield break; + + int skip = (pageIndex - 1) * pageSize; + foreach (var item in source.Skip(skip).Take(pageSize)) + { + yield return item; + } + } + + #endregion + + #region 随机操作 + +#if NET6_0_OR_GREATER + private static System.Random GetSharedRandom() => System.Random.Shared; +#else + private static readonly System.Threading.ThreadLocal ThreadLocalRandom = new(() => new System.Random(Guid.NewGuid().GetHashCode())); + private static System.Random GetSharedRandom() => ThreadLocalRandom.Value!; +#endif + + /// + /// 随机排序 + /// + public static IEnumerable Shuffle(this IEnumerable source) + { + if (source == null) + yield break; + + var list = source.ToList(); + var random = GetSharedRandom(); + + for (int i = list.Count - 1; i > 0; i--) + { + int j = random.Next(i + 1); + (list[i], list[j]) = (list[j], list[i]); + } + + foreach (var item in list) + { + yield return item; + } + } + + /// + /// 随机获取一个元素 + /// + public static T Random(this IEnumerable source) + { + if (source == null) + return default; + + var list = source.ToList(); + if (list.Count == 0) + return default; + + var random = GetSharedRandom(); + return list[random.Next(list.Count)]; + } + + /// + /// 随机获取指定数量的元素 + /// + public static IEnumerable RandomTake(this IEnumerable source, int count) + { + if (source == null || count <= 0) + yield break; + + var list = source.ToList(); + if (list.Count == 0) + yield break; + + count = Math.Min(count, list.Count); + var random = GetSharedRandom(); + var selected = new HashSet(); + + while (selected.Count < count) + { + selected.Add(random.Next(list.Count)); + } + + foreach (var index in selected) + { + yield return list[index]; + } + } + + #endregion + + #region 条件操作 + + /// + /// 根据条件执行不同的操作 + /// + public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) + { + if (source == null) + yield break; + + if (condition) + { + foreach (var item in source.Where(predicate)) + { + yield return item; + } + } + else + { + foreach (var item in source) + { + yield return item; + } + } + } + + /// + /// 如果集合为空则返回默认集合 + /// + public static IEnumerable IfEmpty(this IEnumerable source, IEnumerable defaultValue) + { + if (source == null || !source.Any()) + return defaultValue ?? Enumerable.Empty(); + + return source; + } + + #endregion + + #region 统计操作 + + /// + /// 统计满足条件的元素数量 + /// + public static int CountEx(this IEnumerable source, Func predicate) + { + if (source == null) + return 0; + + return source.Count(predicate); + } + + #endregion + + #region 索引操作 + + /// + /// 获取指定索引处的元素 + /// + public static T ElementAtOrDefault(this IEnumerable source, int index, T defaultValue) + { + if (source == null || index < 0) + return defaultValue; + + int i = 0; + foreach (var item in source) + { + if (i == index) + return item; + i++; + } + + return defaultValue; + } + + /// + /// 获取第一个元素,如果集合为空则返回默认值 + /// + public static T FirstOrValue(this IEnumerable source, T defaultValue) + { + if (source == null) + return defaultValue; + + foreach (var item in source) + { + return item; + } + + return defaultValue; + } + + /// + /// 获取最后一个元素,如果集合为空则返回默认值 + /// + public static T LastOrValue(this IEnumerable source, T defaultValue) + { + if (source == null) + return defaultValue; + + var last = defaultValue; + var hasElement = false; + + foreach (var item in source) + { + last = item; + hasElement = true; + } + + return hasElement ? last : defaultValue; + } + + #endregion + + #region 集合合并 + + /// + /// 合并多个集合 + /// + public static IEnumerable Merge(params IEnumerable[] sources) + { + if (sources == null || sources.Length == 0) + yield break; + + foreach (var source in sources) + { + if (source != null) + { + foreach (var item in source) + { + yield return item; + } + } + } + } + + #endregion + } +} diff --git a/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs b/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs new file mode 100644 index 0000000..d29e039 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ImmutableListExtension.cs @@ -0,0 +1,421 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 不可变列表扩展 + /// + public static class ImmutableListExtension + { + /// + /// 创建不可变列表 + /// + public static ImmutableList ToImmutableList(this IEnumerable source) + { + return new ImmutableList(source); + } + + /// + /// 添加元素并返回新列表 + /// + public static ImmutableList AddItem(this ImmutableList list, T item) + { + return list.Add(item); + } + + /// + /// 添加多个元素并返回新列表 + /// + public static ImmutableList AddRangeItems(this ImmutableList list, IEnumerable items) + { + return list.AddRange(items); + } + + /// + /// 移除元素并返回新列表 + /// + public static ImmutableList RemoveItem(this ImmutableList list, T item) + { + return list.Remove(item); + } + + /// + /// 更新元素并返回新列表 + /// + public static ImmutableList SetItem(this ImmutableList list, int index, T item) + { + return list.SetItem(index, item); + } + + /// + /// 移除指定位置的元素并返回新列表 + /// + public static ImmutableList RemoveItemAt(this ImmutableList list, int index) + { + return list.RemoveAt(index); + } + + /// + /// 插入元素并返回新列表 + /// + public static ImmutableList InsertItem(this ImmutableList list, int index, T item) + { + return list.Insert(index, item); + } + } + + /// + /// 不可变列表 + /// + public sealed class ImmutableList : IReadOnlyList, IEquatable> + { + private readonly T[] _items; + + /// + /// 空列表 + /// + public static readonly ImmutableList Empty = new ImmutableList(); + + /// + /// 创建不可变列表 + /// + public ImmutableList() + { + _items = Array.Empty(); + } + + /// + /// 从集合创建不可变列表 + /// + public ImmutableList(IEnumerable items) + { + _items = items as T[] ?? new List(items).ToArray(); + } + + /// + /// 从数组创建不可变列表 + /// + public ImmutableList(T[] items) + { + _items = items ?? Array.Empty(); + } + + /// + /// 获取指定索引处的元素 + /// + public T this[int index] => _items[index]; + + /// + /// 元素数量 + /// + public int Count => _items.Length; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.Length == 0; + + /// + /// 添加元素并返回新列表 + /// + public ImmutableList Add(T item) + { + var newArray = new T[_items.Length + 1]; + Array.Copy(_items, newArray, _items.Length); + newArray[_items.Length] = item; + return new ImmutableList(newArray); + } + + /// + /// 添加多个元素并返回新列表 + /// + public ImmutableList AddRange(IEnumerable items) + { + var itemsList = new List(items); + var newArray = new T[_items.Length + itemsList.Count]; + Array.Copy(_items, newArray, _items.Length); + itemsList.CopyTo(newArray, _items.Length); + return new ImmutableList(newArray); + } + + /// + /// 移除元素并返回新列表 + /// + public ImmutableList Remove(T item) + { + var index = IndexOf(item); + return index >= 0 ? RemoveAt(index) : this; + } + + /// + /// 移除满足条件的元素并返回新列表 + /// + public ImmutableList RemoveAll(Predicate match) + { + var newList = new List(); + foreach (var item in _items) + { + if (!match(item)) + newList.Add(item); + } + return new ImmutableList(newList); + } + + /// + /// 移除指定位置的元素并返回新列表 + /// + public ImmutableList RemoveAt(int index) + { + if (index < 0 || index >= _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = new T[_items.Length - 1]; + Array.Copy(_items, 0, newArray, 0, index); + Array.Copy(_items, index + 1, newArray, index, _items.Length - index - 1); + return new ImmutableList(newArray); + } + + /// + /// 插入元素并返回新列表 + /// + public ImmutableList Insert(int index, T item) + { + if (index < 0 || index > _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = new T[_items.Length + 1]; + Array.Copy(_items, 0, newArray, 0, index); + newArray[index] = item; + Array.Copy(_items, index, newArray, index + 1, _items.Length - index); + return new ImmutableList(newArray); + } + + /// + /// 更新指定位置的元素并返回新列表 + /// + public ImmutableList SetItem(int index, T item) + { + if (index < 0 || index >= _items.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + var newArray = (T[])_items.Clone(); + newArray[index] = item; + return new ImmutableList(newArray); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) + { + return Array.IndexOf(_items, item); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item, int startIndex) + { + return Array.IndexOf(_items, item, startIndex); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item, int startIndex, int count) + { + return Array.IndexOf(_items, item, startIndex, count); + } + + /// + /// 是否包含元素 + /// + public bool Contains(T item) + { + return IndexOf(item) >= 0; + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array) + { + _items.CopyTo(array, 0); + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) + { + _items.CopyTo(array, arrayIndex); + } + + /// + /// 转换为数组 + /// + public T[] ToArray() + { + return (T[])_items.Clone(); + } + + /// + /// 查找元素 + /// + public T? Find(Predicate match) + { + foreach (var item in _items) + { + if (match(item)) + return item; + } + return default; + } + + /// + /// 查找所有元素 + /// + public ImmutableList FindAll(Predicate match) + { + var result = new List(); + foreach (var item in _items) + { + if (match(item)) + result.Add(item); + } + return new ImmutableList(result); + } + + /// + /// 是否存在满足条件的元素 + /// + public bool Exists(Predicate match) + { + return FindIndex(match) >= 0; + } + + /// + /// 查找满足条件的元素索引 + /// + public int FindIndex(Predicate match) + { + for (int i = 0; i < _items.Length; i++) + { + if (match(_items[i])) + return i; + } + return -1; + } + + /// + /// 对每个元素执行操作 + /// + public void ForEach(Action action) + { + foreach (var item in _items) + { + action(item); + } + } + + /// + /// 转换元素类型 + /// + public ImmutableList ConvertAll(Converter converter) + { + var result = new TResult[_items.Length]; + for (int i = 0; i < _items.Length; i++) + { + result[i] = converter(_items[i]); + } + return new ImmutableList(result); + } + + /// + /// 获取范围 + /// + public ImmutableList GetRange(int index, int count) + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + if (index + count > _items.Length) + throw new ArgumentException("范围超出列表边界"); + + var result = new T[count]; + Array.Copy(_items, index, result, 0, count); + return new ImmutableList(result); + } + + #region IEnumerable + + public IEnumerator GetEnumerator() + { + return ((IEnumerable)_items).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _items.GetEnumerator(); + } + + #endregion + + #region IEquatable + + public bool Equals(ImmutableList? other) + { + if (other is null) + return false; + + if (ReferenceEquals(this, other)) + return true; + + if (_items.Length != other._items.Length) + return false; + + for (int i = 0; i < _items.Length; i++) + { + if (!EqualityComparer.Default.Equals(_items[i], other._items[i])) + return false; + } + + return true; + } + + public override bool Equals(object? obj) + { + return Equals(obj as ImmutableList); + } + + public override int GetHashCode() + { + var hash = new HashCode(); + foreach (var item in _items) + { + hash.Add(item); + } + return hash.ToHashCode(); + } + + public static bool operator ==(ImmutableList? left, ImmutableList? right) + { + return Equals(left, right); + } + + public static bool operator !=(ImmutableList? left, ImmutableList? right) + { + return !Equals(left, right); + } + + #endregion + + public override string ToString() + { + return $"[{string.Join(", ", _items)}]"; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs b/EasyTool.Core/CollectionsCategory/IteratorUtil.cs deleted file mode 100644 index b707296..0000000 --- a/EasyTool.Core/CollectionsCategory/IteratorUtil.cs +++ /dev/null @@ -1,186 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - //TODO:疑问,这些功能Linq支持吗? - /// - /// 迭代器工具类 - /// - public static class IteratorUtil - { - /// - /// 将一个数组转换为一个迭代器 - /// - public static IEnumerable AsIterator(this T[] array) - { - foreach (var item in array) - { - yield return item; - } - } - - /// - /// 过滤掉一个迭代器中不符合条件的元素 - /// - public static IEnumerable Filter(this IEnumerable source, Func predicate) - { - foreach (var item in source) - { - if (predicate(item)) - { - yield return item; - } - } - } - - /// - /// 对一个迭代器中的每个元素进行转换 - /// - public static IEnumerable Map(this IEnumerable source, Func selector) - { - foreach (var item in source) - { - yield return selector(item); - } - } - - /// - /// 从一个迭代器中取出前 n 个元素 - /// - public static IEnumerable Take(this IEnumerable source, int count) - { - foreach (var item in source) - { - if (count-- > 0) - { - yield return item; - } - else - { - break; - } - } - } - - /// - /// 跳过一个迭代器中的前 n 个元素 - /// - public static IEnumerable Skip(this IEnumerable source, int count) - { - foreach (var item in source) - { - if (count-- > 0) - { - continue; - } - else - { - yield return item; - } - } - } - - /// - /// 将一个迭代器的元素分组 - /// - public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector) - { - return source.GroupBy(keySelector, x => x); - } - - /// - /// 将一个迭代器的元素按照指定的方式分组 - /// - public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector) - { - var dictionary = new Dictionary>(); - foreach (var item in source) - { - var key = keySelector(item); - var element = elementSelector(item); - if (!dictionary.ContainsKey(key)) - { - dictionary[key] = new List(); - } - dictionary[key].Add(element); - } - foreach (var group in dictionary) - { - yield return new Grouping(group.Key, group.Value); - } - } - - private class Grouping : IGrouping - { - private readonly List _elements; - - public Grouping(TKey key, List elements) - { - Key = key; - _elements = elements; - } - public TKey Key { get; } - - public IEnumerator GetEnumerator() - { - return _elements.GetEnumerator(); - } - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } - - /// - /// 对一个迭代器中的元素进行排序 - /// - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector) - { - return source.OrderBy(keySelector, Comparer.Default); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行排序 - /// - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer) - { - return source.OrderBy(keySelector, comparer, false); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行排序,并指定排序方向 - /// - public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer, bool descending) - { - return descending ? source.OrderByDescending(keySelector, comparer) : source.OrderBy(keySelector, comparer); - } - - /// - /// 对一个迭代器中的元素进行倒序排序 - /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector) - { - return source.OrderByDescending(keySelector, Comparer.Default); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行倒序排序 - /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer) - { - return source.OrderByDescending(keySelector, comparer, false); - } - - /// - /// 对一个迭代器中的元素按照指定的方式进行倒序排序,并指定排序方向 - /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer, bool descending) - { - return descending ? source.OrderBy(keySelector, comparer) : source.OrderByDescending(keySelector, comparer); - } - } -} diff --git a/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs b/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs new file mode 100644 index 0000000..5a80107 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/LRUCacheUtil.cs @@ -0,0 +1,233 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// LRU 缓存工具类 + /// 最近最少使用淘汰策略的缓存 + /// + public static class LRUCacheUtil + { + /// + /// 创建 LRU 缓存 + /// + /// 键类型 + /// 值类型 + /// 容量 + /// LRU 缓存实例 + public static LRUCache Create(int capacity) + where TKey : notnull + { + return new LRUCache(capacity); + } + } + + /// + /// LRU 缓存实现 + /// + /// 键类型 + /// 值类型 + public class LRUCache where TKey : notnull + { + private readonly int _capacity; + private readonly Dictionary> _cache; + private readonly LinkedList _lruList; + + /// + /// 当前缓存数量 + /// + public int Count => _cache.Count; + + /// + /// 缓存容量 + /// + public int Capacity => _capacity; + + /// + /// 缓存命中率 + /// + public double HitRate => _totalRequests == 0 ? 0 : (double)_hits / _totalRequests; + + private long _hits; + private long _totalRequests; + + /// + /// 创建 LRU 缓存 + /// + /// 容量 + public LRUCache(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0"); + + _capacity = capacity; + _cache = new Dictionary>(); + _lruList = new LinkedList(); + } + + /// + /// 获取或设置缓存值 + /// + public TValue this[TKey key] + { + get => Get(key); + set => Put(key, value); + } + + /// + /// 获取缓存值 + /// + public TValue Get(TKey key) + { + _totalRequests++; + + if (_cache.TryGetValue(key, out var node)) + { + _hits++; + // 移动到链表头部(最近使用) + _lruList.Remove(node); + _lruList.AddFirst(node); + return node.Value.Value; + } + + throw new KeyNotFoundException($"Key '{key}' not found in cache"); + } + + /// + /// 尝试获取缓存值 + /// + public bool TryGet(TKey key, out TValue value) + { + _totalRequests++; + + if (_cache.TryGetValue(key, out var node)) + { + _hits++; + _lruList.Remove(node); + _lruList.AddFirst(node); + value = node.Value.Value; + return true; + } + + value = default; + return false; + } + + /// + /// 获取缓存值,不存在则通过工厂创建并缓存 + /// + public TValue GetOrAdd(TKey key, Func factory) + { + if (TryGet(key, out var value)) + return value; + + value = factory(key); + Put(key, value); + return value; + } + + /// + /// 添加或更新缓存 + /// + public void Put(TKey key, TValue value) + { + if (_cache.TryGetValue(key, out var existingNode)) + { + // 更新已存在的键 + _lruList.Remove(existingNode); + existingNode.Value.Value = value; + _lruList.AddFirst(existingNode); + } + else + { + // 添加新键 + if (_cache.Count >= _capacity) + { + // 淘汰最久未使用的项 + var last = _lruList.Last; + _lruList.RemoveLast(); + _cache.Remove(last.Value.Key); + } + + var cacheItem = new CacheItem { Key = key, Value = value }; + var node = _lruList.AddFirst(cacheItem); + _cache[key] = node; + } + } + + /// + /// 移除缓存 + /// + public bool Remove(TKey key) + { + if (_cache.TryGetValue(key, out var node)) + { + _lruList.Remove(node); + _cache.Remove(key); + return true; + } + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _cache.ContainsKey(key); + } + + /// + /// 清空缓存 + /// + public void Clear() + { + _cache.Clear(); + _lruList.Clear(); + _hits = 0; + _totalRequests = 0; + } + + /// + /// 获取所有键 + /// + public IEnumerable GetKeys() + { + var node = _lruList.First; + while (node != null) + { + yield return node.Value.Key; + node = node.Next; + } + } + + /// + /// 获取所有值(按LRU顺序) + /// + public IEnumerable GetValues() + { + var node = _lruList.First; + while (node != null) + { + yield return node.Value.Value; + node = node.Next; + } + } + + /// + /// 重置统计信息 + /// + public void ResetStatistics() + { + _hits = 0; + _totalRequests = 0; + } + + private class CacheItem + { + public TKey Key { get; set; } + public TValue Value { get; set; } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs b/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs deleted file mode 100644 index 3a1487a..0000000 --- a/EasyTool.Core/CollectionsCategory/LinkedListExtension.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool.Extension -{ - - /// - /// 双向链表工具类 - /// - public static class LinkedListExtension - { - /// - /// 将双向链表中的某个节点移动到链表的结尾处。 - /// - /// 双向链表元素类型 - /// 双向链表 - /// 要移动的节点 - public static void MoveLast(this LinkedList list, LinkedListNode node) => LinkedListUtil.MoveLast(list,node); - - /// - /// 将双向链表中移动到最前方 - /// - /// 双向链表元素类型 - /// 双向链表 - /// 要移动的节点 - public static void MoveFirst(this LinkedList list, LinkedListNode node) => LinkedListUtil.MoveFirst(list,node); - - } -} diff --git a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs index d3893db..5729fea 100644 --- a/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs +++ b/EasyTool.Core/CollectionsCategory/LinkedListUtil.cs @@ -2,19 +2,21 @@ using System.Collections.Generic; using System.Text; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// /// 双向链表工具类 /// - public class LinkedListUtil + public static class LinkedListUtil { /// /// 将指定元素添加到双向链表的结尾处。 + /// [Obsolete("请直接使用 list.AddLast(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要添加的元素 + [Obsolete("请直接使用 list.AddLast(item)", false)] public static void AddLast(LinkedList list, T item) { list.AddLast(item); @@ -22,10 +24,12 @@ public static void AddLast(LinkedList list, T item) /// /// 将指定元素添加到双向链表的开头处。 + /// [Obsolete("请直接使用 list.AddFirst(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要添加的元素 + [Obsolete("请直接使用 list.AddFirst(item)", false)] public static void AddFirst(LinkedList list, T item) { list.AddFirst(item); @@ -33,12 +37,14 @@ public static void AddFirst(LinkedList list, T item) /// /// 将指定元素插入到双向链表中的指定位置之前。 + /// [Obsolete("请直接使用 list.AddBefore(node, item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在其前面插入新元素的节点 /// 要添加的元素 /// 新节点 + [Obsolete("请直接使用 list.AddBefore(node, item)", false)] public static LinkedListNode AddBefore(LinkedList list, LinkedListNode node, T item) { return list.AddBefore(node, item); @@ -46,12 +52,14 @@ public static LinkedListNode AddBefore(LinkedList list, LinkedListNode< /// /// 将指定元素插入到双向链表中的指定位置之后。 + /// [Obsolete("请直接使用 list.AddAfter(node, item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在其后面插入新元素的节点 /// 要添加的元素 /// 新节点 + [Obsolete("请直接使用 list.AddAfter(node, item)", false)] public static LinkedListNode AddAfter(LinkedList list, LinkedListNode node, T item) { return list.AddAfter(node, item); @@ -84,10 +92,12 @@ public static void MoveFirst(LinkedList list, LinkedListNode node) /// /// 从双向链表中移除指定节点。 + /// [Obsolete("请直接使用 list.Remove(node)")] /// /// 双向链表元素类型 /// 双向链表 /// 要移除的节点 + [Obsolete("请直接使用 list.Remove(node)", false)] public static void Remove(LinkedList list, LinkedListNode node) { list.Remove(node); @@ -95,11 +105,13 @@ public static void Remove(LinkedList list, LinkedListNode node) /// /// 从双向链表中移除指定值的第一个匹配项。 + /// [Obsolete("请直接使用 list.Remove(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要移除的元素 /// 如果成功移除了元素,则为 true;否则为 false。 + [Obsolete("请直接使用 list.Remove(item)", false)] public static bool Remove(LinkedList list, T item) { return list.Remove(item); @@ -107,11 +119,13 @@ public static bool Remove(LinkedList list, T item) /// /// 确定双向链表中是否包含特定值。 + /// [Obsolete("请直接使用 list.Contains(item)")] /// /// 双向链表元素类型 /// 双向链表 /// 要在双向链表中查找的元素 /// 如果在双向链表中找到了 item,则为 true;否则为 false。 + [Obsolete("请直接使用 list.Contains(item)", false)] public static bool Contains(LinkedList list, T item) { return list.Contains(item); @@ -119,9 +133,11 @@ public static bool Contains(LinkedList list, T item) /// /// 从双向链表中移除所有节点。 + /// [Obsolete("请直接使用 list.Clear()")] /// /// 双向链表元素类型 /// 双向链表 + [Obsolete("请直接使用 list.Clear()", false)] public static void Clear(LinkedList list) { list.Clear(); diff --git a/EasyTool.Core/CollectionsCategory/ListExtension.cs b/EasyTool.Core/CollectionsCategory/ListExtension.cs index 31af6e4..fa39ee2 100644 --- a/EasyTool.Core/CollectionsCategory/ListExtension.cs +++ b/EasyTool.Core/CollectionsCategory/ListExtension.cs @@ -3,10 +3,60 @@ using System.Linq; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.CollectionsCategory { + /// + /// List 集合扩展方法 + /// public static class ListExtension - { + { + #region 列表合并 + + /// + /// 将指定的列表连接起来,形成一个新的列表。 + /// + /// 列表元素类型 + /// 要连接的列表 + /// 连接后的新列表 + public static List Concat(this IEnumerable> lists) + { + return lists.SelectMany(x => x).ToList(); + } + + /// + /// 将指定的列表连接起来,形成一个新的列表。 + /// + /// 列表元素类型 + /// 要连接的列表 + /// 连接后的新列表 + public static List Concat(this List list, params List[] lists) + { + return Concat(new[] { list }.Concat(lists)); + } + + #endregion + + #region 列表分页 + + /// + /// 将列表中的元素分页显示。 + /// + /// 列表元素类型 + /// 要分页的列表 + /// 每页显示的元素数量 + /// 要显示的页码,从 0 开始 + /// 指定页的元素列表 + public static List Page(this List list, int pageSize, int pageIndex) + { + return list.Skip(pageIndex * pageSize) + .Take(pageSize) + .ToList(); + } + + #endregion + + #region 列表比较 + /// /// 判断两个列表是否相等。 /// @@ -14,6 +64,33 @@ public static class ListExtension /// 要比较的第一个列表 /// 要比较的第二个列表 /// 如果两个列表相等,则返回 true;否则返回 false - public static bool Equals(this List list1, List list2)=> ListUtil.Equals(list1, list2); + public static bool Equals(this List? list1, List? list2) + { + if (list1 == null && list2 == null) + { + return true; + } + else if (list1 == null || list2 == null) + { + return false; + } + else if (list1.Count != list2.Count) + { + return false; + } + else + { + for (int i = 0; i < list1.Count; i++) + { + if (!EqualityComparer.Default.Equals(list1[i], list2[i])) + { + return false; + } + } + return true; + } + } + + #endregion } } diff --git a/EasyTool.Core/CollectionsCategory/ListUtil.cs b/EasyTool.Core/CollectionsCategory/ListUtil.cs deleted file mode 100644 index 916f896..0000000 --- a/EasyTool.Core/CollectionsCategory/ListUtil.cs +++ /dev/null @@ -1,244 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - public class ListUtil - { - /// - /// 在列表中查找元素,并返回其索引。如果未找到,则返回 -1。 - /// - /// 列表元素类型 - /// 要查找的列表 - /// 要查找的元素 - /// 元素在列表中的索引,如果未找到则返回 -1 - public static int IndexOf(List list, T item) - { - return list.IndexOf(item); - } - - /// - /// 向列表中添加多个元素。 - /// - /// 列表元素类型 - /// 要添加元素的列表 - /// 要添加到列表中的元素 - public static void AddRange(List list, IEnumerable items) - { - list.AddRange(items); - } - - /// - /// 在列表中删除指定索引处的元素。 - /// - /// 列表元素类型 - /// 要删除元素的列表 - /// 要删除元素的索引 - public static void RemoveAt(List list, int index) - { - list.RemoveAt(index); - } - - /// - /// 从列表中删除指定元素的第一个匹配项。 - /// - /// 列表元素类型 - /// 要删除元素的列表 - /// 要删除的元素 - /// 如果找到并成功删除元素,则返回 true;否则返回 false - public static bool Remove(List list, T item) - { - return list.Remove(item); - } - - /// - /// 将指定的列表连接起来,形成一个新的列表。 - /// - /// 列表元素类型 - /// 要连接的列表 - /// 连接后的新列表 - public static List Concat(IEnumerable> lists) - { - return lists.SelectMany(x => x).ToList(); - } - - /// - /// 将指定的列表连接起来,形成一个新的列表。 - /// - /// 列表元素类型 - /// 要连接的列表 - /// 连接后的新列表 - public static List Concat(params List[] lists) - { - return Concat((IEnumerable>)lists); - } - - /// - /// 返回一个新的列表,其中包含指定列表中的元素,但不包括重复元素。 - /// - /// 列表元素类型 - /// 要去重的列表 - /// 去重后的新列表 - public static List Distinct(List list) - { - return list.Distinct().ToList(); - } - - /// - /// 根据指定的条件筛选出列表中符合条件的元素。 - /// - /// 列表元素类型 - /// 要筛选的列表 - /// 筛选条件 - /// 符合条件的元素列表 - public static List Where(List list, Func predicate) - { - return list.Where(predicate).ToList(); - } - - /// - /// 将列表中的每个元素应用到指定的转换函数,并返回转换后的新列表。 - /// - /// 列表元素类型 - /// 转换后的元素类型 - /// 要转换的列表 - /// 转换函数 - /// 转换后的新列表 - public static List Select(List list, Func selector) - { - return list.Select(selector).ToList(); - } - - /// - /// 对列表中的每个元素应用指定的操作。 - /// - /// 列表元素类型 - /// 要应用操作的列表 - /// 要应用的操作 - public static void ForEach(List list, Action action) - { - list.ForEach(action); - } - - /// - /// 将列表中的元素排序。 - /// - /// 列表元素类型 - /// 要排序的列表 - public static void Sort(List list) - { - list.Sort(); - } - - /// - /// 将列表中的元素按指定的比较器排序。 - /// - /// 列表元素类型 - /// 要排序的列表 - /// 比较器 - public static void Sort(List list, IComparer comparer) - { - list.Sort(comparer); - } - - /// - /// 将列表中的元素分页显示。 - /// - /// 列表元素类型 - /// 要分页的列表 - /// 每页显示的元素数量 - /// 要显示的页码,从 0 开始 - /// 指定页的元素列表 - public static List Page(List list, int pageSize, int pageIndex) - { - return list.Skip(pageIndex * pageSize) - .Take(pageSize) - .ToList(); - } - - /// - /// 向列表中批量添加元素。 - /// - /// 列表元素类型 - /// 要添加元素的列表 - /// 要添加到列表中的元素 - public static void AddRange(List list, params T[] items) - { - list.AddRange(items); - } - - /// - /// 判断两个列表是否相等。 - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 如果两个列表相等,则返回 true;否则返回 false - public static bool Equals(List list1, List list2) - { - if (list1 == null && list2 == null) - { - return true; - } - else if (list1 == null || list2 == null) - { - return false; - } - else if (list1.Count != list2.Count) - { - return false; - } - else - { - for (int i = 0; i < list1.Count; i++) - { - if (!list1[i].Equals(list2[i])) - { - return false; - } - } - - return true; - } - } - - /// - /// 返回两个列表的交集。 - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 交集列表 - public static List Intersect(List list1, List list2) - { - return list1.Intersect(list2).ToList(); - } - - /// - /// 返回两个列表的并集。 - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 并集列表 - public static List Union(List list1, List list2) - { - return list1.Union(list2).ToList(); - } - - /// - /// 返回两个列表的差集。 - /// - /// 列表元素类型 - /// 要比较的第一个列表 - /// 要比较的第二个列表 - /// 差集列表 - public static List Except(List list1, List list2) - { - return list1.Except(list2).ToList(); - } - - } -} diff --git a/EasyTool.Core/CollectionsCategory/MatrixUtil.cs b/EasyTool.Core/CollectionsCategory/MatrixUtil.cs new file mode 100644 index 0000000..3786fb0 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MatrixUtil.cs @@ -0,0 +1,363 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 矩阵工具类 + /// 提供二维矩阵的常用操作 + /// + public static class MatrixUtil + { + /// + /// 创建矩阵 + /// + public static Matrix Create(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 从二维数组创建矩阵 + /// + public static Matrix FromArray(T[,] array) + { + return new Matrix(array); + } + + /// + /// 创建全零矩阵 + /// + public static Matrix Zeros(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 创建全一矩阵 + /// + public static Matrix Ones(int rows, int cols) + { + var matrix = new Matrix(rows, cols); + for (int i = 0; i < rows; i++) + for (int j = 0; j < cols; j++) + matrix[i, j] = 1; + return matrix; + } + + /// + /// 创建单位矩阵 + /// + public static Matrix Identity(int size) + { + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + matrix[i, i] = 1; + return matrix; + } + + /// + /// 创建对角矩阵 + /// + public static Matrix Diagonal(T[] diagonal) + { + int n = diagonal.Length; + var matrix = new Matrix(n, n); + for (int i = 0; i < n; i++) + matrix[i, i] = diagonal[i]; + return matrix; + } + } + + /// + /// 矩阵实现 + /// + /// 元素类型 + public class Matrix + { + private readonly T[,] _data; + + /// + /// 行数 + /// + public int Rows { get; } + + /// + /// 列数 + /// + public int Columns { get; } + + /// + /// 元素总数 + /// + public int Length => Rows * Columns; + + /// + /// 访问元素 + /// + public T this[int row, int col] + { + get + { + ValidateIndex(row, col); + return _data[row, col]; + } + set + { + ValidateIndex(row, col); + _data[row, col] = value; + } + } + + /// + /// 创建矩阵 + /// + public Matrix(int rows, int cols) + { + if (rows <= 0 || cols <= 0) + throw new ArgumentOutOfRangeException("Rows and columns must be positive"); + + Rows = rows; + Columns = cols; + _data = new T[rows, cols]; + } + + /// + /// 从二维数组创建矩阵 + /// + public Matrix(T[,] array) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + + Rows = array.GetLength(0); + Columns = array.GetLength(1); + _data = new T[Rows, Columns]; + Array.Copy(array, _data, array.Length); + } + + /// + /// 获取行 + /// + public T[] GetRow(int row) + { + if (row < 0 || row >= Rows) + throw new ArgumentOutOfRangeException(nameof(row)); + + var result = new T[Columns]; + for (int i = 0; i < Columns; i++) + result[i] = _data[row, i]; + return result; + } + + /// + /// 获取列 + /// + public T[] GetColumn(int col) + { + if (col < 0 || col >= Columns) + throw new ArgumentOutOfRangeException(nameof(col)); + + var result = new T[Rows]; + for (int i = 0; i < Rows; i++) + result[i] = _data[i, col]; + return result; + } + + /// + /// 设置行 + /// + public void SetRow(int row, T[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != Columns) + throw new ArgumentException("Values length must match column count"); + + for (int i = 0; i < Columns; i++) + _data[row, i] = values[i]; + } + + ///
+ /// 设置列 + ///
+ public void SetColumn(int col, T[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != Rows) + throw new ArgumentException("Values length must match row count"); + + for (int i = 0; i < Rows; i++) + _data[i, col] = values[i]; + } + + /// + /// 转置 + /// + public Matrix Transpose() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[j, i] = _data[i, j]; + return result; + } + + /// + /// 翻转行 + /// + public Matrix FlipVertical() + { + var result = new Matrix(Rows, Columns); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[Rows - 1 - i, j] = _data[i, j]; + return result; + } + + /// + /// 翻转列 + /// + public Matrix FlipHorizontal() + { + var result = new Matrix(Rows, Columns); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[i, Columns - 1 - j] = _data[i, j]; + return result; + } + + /// + /// 顺时针旋转90度 + /// + public Matrix Rotate90() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[j, Rows - 1 - i] = _data[i, j]; + return result; + } + + /// + /// 旋转180度 + /// + public Matrix Rotate180() + { + return FlipVertical().FlipHorizontal(); + } + + /// + /// 逆时针旋转90度 + /// + public Matrix Rotate270() + { + var result = new Matrix(Columns, Rows); + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[Columns - 1 - j, i] = _data[i, j]; + return result; + } + + /// + /// 获取子矩阵 + /// + public Matrix SubMatrix(int startRow, int startCol, int rows, int cols) + { + if (startRow < 0 || startCol < 0 || startRow + rows > Rows || startCol + cols > Columns) + throw new ArgumentOutOfRangeException(); + + var result = new Matrix(rows, cols); + for (int i = 0; i < rows; i++) + for (int j = 0; j < cols; j++) + result[i, j] = _data[startRow + i, startCol + j]; + return result; + } + + /// + /// 克隆矩阵 + /// + public Matrix Clone() + { + return new Matrix(_data); + } + + /// + /// 转换为二维数组 + /// + public T[,] ToArray() + { + var result = new T[Rows, Columns]; + Array.Copy(_data, result, _data.Length); + return result; + } + + /// + /// 转换为交错数组 + /// + public T[][] ToJaggedArray() + { + var result = new T[Rows][]; + for (int i = 0; i < Rows; i++) + { + result[i] = new T[Columns]; + for (int j = 0; j < Columns; j++) + result[i][j] = _data[i, j]; + } + return result; + } + + /// + /// 展平为一维数组 + /// + public T[] Flatten() + { + var result = new T[Length]; + int index = 0; + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + result[index++] = _data[i, j]; + return result; + } + + /// + /// 遍历所有元素 + /// + public IEnumerable Enumerate() + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + yield return _data[i, j]; + } + + /// + /// 填充所有元素 + /// + public void Fill(T value) + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + _data[i, j] = value; + } + + /// + /// 使用函数填充 + /// + public void Fill(Func generator) + { + for (int i = 0; i < Rows; i++) + for (int j = 0; j < Columns; j++) + _data[i, j] = generator(i, j); + } + + private void ValidateIndex(int row, int col) + { + if (row < 0 || row >= Rows) + throw new ArgumentOutOfRangeException(nameof(row)); + if (col < 0 || col >= Columns) + throw new ArgumentOutOfRangeException(nameof(col)); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs b/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs new file mode 100644 index 0000000..86fbf88 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MultiKeyDictionaryUtil.cs @@ -0,0 +1,747 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 复合字典工具类 + /// + public static class MultiKeyDictionaryUtil + { + /// + /// 创建双键字典 + /// + public static TwoKeyDictionary CreateTwoKey() + where TKey1 : notnull + where TKey2 : notnull + { + return new TwoKeyDictionary(); + } + + /// + /// 创建复合键字典 + /// + public static CompositeKeyDictionary CreateComposite() + where TKey : notnull + { + return new CompositeKeyDictionary(); + } + + /// + /// 创建区间映射 + /// + public static RangeMap CreateRangeMap() where T : IComparable + { + return new RangeMap(); + } + } + + /// + /// 双键字典 + /// 通过两个键可以分别查找值 + /// + public class TwoKeyDictionary + where TKey1 : notnull + where TKey2 : notnull + { + private readonly Dictionary> _data; + private readonly Dictionary> _reverseData; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 第一个键的集合 + /// + public ICollection Keys1 => _data.Keys; + + /// + /// 第二个键的集合 + /// + public ICollection Keys2 => _reverseData.Keys; + + /// + /// 通过第一个键访问 + /// + public Dictionary this[TKey1 key1] + { + get + { + if (!_data.TryGetValue(key1, out var inner)) + { + inner = new Dictionary(); + _data[key1] = inner; + } + return inner; + } + } + + /// + /// 创建双键字典 + /// + public TwoKeyDictionary() + { + _data = new Dictionary>(); + _reverseData = new Dictionary>(); + _count = 0; + } + + /// + /// 添加元素 + /// + public void Add(TKey1 key1, TKey2 key2, TValue value) + { + if (!_data.TryGetValue(key1, out var inner1)) + { + inner1 = new Dictionary(); + _data[key1] = inner1; + } + + if (!inner1.ContainsKey(key2)) + { + _count++; + } + + inner1[key2] = value; + + if (!_reverseData.TryGetValue(key2, out var inner2)) + { + inner2 = new Dictionary(); + _reverseData[key2] = inner2; + } + inner2[key1] = value; + } + + /// + /// 通过双键获取值 + /// + public bool TryGetValue(TKey1 key1, TKey2 key2, out TValue value) + { + value = default; + if (!_data.TryGetValue(key1, out var inner)) + return false; + return inner.TryGetValue(key2, out value); + } + + /// + /// 通过双键获取值 + /// + public TValue GetValue(TKey1 key1, TKey2 key2) + { + if (!TryGetValue(key1, key2, out var value)) + throw new KeyNotFoundException($"Key pair ({key1}, {key2}) not found"); + return value; + } + + /// + /// 通过第一个键获取所有值 + /// + public bool TryGetByKey1(TKey1 key1, out Dictionary values) + { + return _data.TryGetValue(key1, out values); + } + + /// + /// 通过第二个键获取所有值 + /// + public bool TryGetByKey2(TKey2 key2, out Dictionary values) + { + return _reverseData.TryGetValue(key2, out values); + } + + /// + /// 移除元素 + /// + public bool Remove(TKey1 key1, TKey2 key2) + { + if (!_data.TryGetValue(key1, out var inner1)) + return false; + + if (!inner1.Remove(key2)) + return false; + + _count--; + + if (inner1.Count == 0) + _data.Remove(key1); + + if (_reverseData.TryGetValue(key2, out var inner2)) + { + inner2.Remove(key1); + if (inner2.Count == 0) + _reverseData.Remove(key2); + } + + return true; + } + + /// + /// 移除第一个键的所有元素 + /// + public bool RemoveByKey1(TKey1 key1) + { + if (!_data.TryGetValue(key1, out var inner)) + return false; + + _count -= inner.Count; + + foreach (var key2 in inner.Keys) + { + if (_reverseData.TryGetValue(key2, out var reverseInner)) + { + reverseInner.Remove(key1); + if (reverseInner.Count == 0) + _reverseData.Remove(key2); + } + } + + _data.Remove(key1); + return true; + } + + /// + /// 移除第二个键的所有元素 + /// + public bool RemoveByKey2(TKey2 key2) + { + if (!_reverseData.TryGetValue(key2, out var inner)) + return false; + + _count -= inner.Count; + + foreach (var key1 in inner.Keys) + { + if (_data.TryGetValue(key1, out var forwardInner)) + { + forwardInner.Remove(key2); + if (forwardInner.Count == 0) + _data.Remove(key1); + } + } + + _reverseData.Remove(key2); + return true; + } + + /// + /// 是否包含键对 + /// + public bool ContainsKey(TKey1 key1, TKey2 key2) + { + return _data.TryGetValue(key1, out var inner) && inner.ContainsKey(key2); + } + + /// + /// 是否包含第一个键 + /// + public bool ContainsKey1(TKey1 key1) + { + return _data.ContainsKey(key1); + } + + /// + /// 是否包含第二个键 + /// + public bool ContainsKey2(TKey2 key2) + { + return _reverseData.ContainsKey(key2); + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + _reverseData.Clear(); + _count = 0; + } + + /// + /// 获取所有键值对 + /// + public IEnumerable<(TKey1 Key1, TKey2 Key2, TValue Value)> GetAll() + { + foreach (var kvp1 in _data) + { + foreach (var kvp2 in kvp1.Value) + { + yield return (kvp1.Key, kvp2.Key, kvp2.Value); + } + } + } + } + + /// + /// 复合键字典 + /// 使用多个键组成的元组作为键 + /// + public class CompositeKeyDictionary where TKey : notnull + { + private readonly Dictionary _data; + private readonly IEqualityComparer _keyComparer; + private readonly List>> _indexes; + + /// + /// 元素数量 + /// + public int Count => _data.Count; + + /// + /// 创建复合键字典 + /// + public CompositeKeyDictionary() + { + _data = new Dictionary(new ArrayEqualityComparer()); + _keyComparer = EqualityComparer.Default; + _indexes = new List>>(); + } + + /// + /// 创建具有指定键数量的复合键字典 + /// + public CompositeKeyDictionary(int keyCount) : this() + { + for (int i = 0; i < keyCount; i++) + { + _indexes.Add(new Dictionary>()); + } + } + + /// + /// 添加元素 + /// + public void Add(TValue value, params TKey[] keys) + { + if (keys == null || keys.Length == 0) + throw new ArgumentException("At least one key is required"); + + _data[keys] = value; + + // 建立索引 + while (_indexes.Count < keys.Length) + { + _indexes.Add(new Dictionary>()); + } + + for (int i = 0; i < keys.Length; i++) + { + var index = _indexes[i]; + if (!index.TryGetValue(keys[i], out var list)) + { + list = new List(); + index[keys[i]] = list; + } + list.Add(keys); + } + } + + /// + /// 获取值 + /// + public bool TryGetValue(out TValue value, params TKey[] keys) + { + return _data.TryGetValue(keys, out value); + } + + /// + /// 获取值 + /// + public TValue Get(params TKey[] keys) + { + if (!_data.TryGetValue(keys, out var value)) + throw new KeyNotFoundException(); + return value; + } + + /// + /// 通过部分键查找 + /// + public List FindByKey(int keyIndex, TKey key) + { + var result = new List(); + + if (keyIndex < 0 || keyIndex >= _indexes.Count) + return result; + + if (!_indexes[keyIndex].TryGetValue(key, out var keyLists)) + return result; + + foreach (var keys in keyLists) + { + if (_data.TryGetValue(keys, out var value)) + { + result.Add(value); + } + } + + return result; + } + + /// + /// 移除 + /// + public bool Remove(params TKey[] keys) + { + if (!_data.Remove(keys)) + return false; + + for (int i = 0; i < keys.Length && i < _indexes.Count; i++) + { + if (_indexes[i].TryGetValue(keys[i], out var list)) + { + list.RemoveAll(k => KeysEqual(k, keys)); + if (list.Count == 0) + _indexes[i].Remove(keys[i]); + } + } + + return true; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(params TKey[] keys) + { + return _data.ContainsKey(keys); + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + foreach (var index in _indexes) + { + index.Clear(); + } + } + + private bool KeysEqual(TKey[] a, TKey[] b) + { + if (a.Length != b.Length) + return false; + for (int i = 0; i < a.Length; i++) + { + if (!_keyComparer.Equals(a[i], b[i])) + return false; + } + return true; + } + + private class ArrayEqualityComparer : IEqualityComparer + { + public bool Equals(T[] x, T[] y) + { + if (x == null || y == null) + return x == y; + if (x.Length != y.Length) + return false; + var comparer = EqualityComparer.Default; + for (int i = 0; i < x.Length; i++) + { + if (!comparer.Equals(x[i], y[i])) + return false; + } + return true; + } + + public int GetHashCode(T[] obj) + { + if (obj == null) + return 0; + int hash = 17; + var comparer = EqualityComparer.Default; + foreach (var item in obj) + { + hash = hash * 31 + (item == null ? 0 : comparer.GetHashCode(item)); + } + return hash; + } + } + } + + /// + /// 区间映射 + /// 将键范围映射到值 + /// + public class RangeMap where T : IComparable + { + private readonly List _entries; + + private class RangeEntry + { + public T Min { get; set; } + public T Max { get; set; } + public object Value { get; set; } + public bool MinInclusive { get; set; } + public bool MaxInclusive { get; set; } + } + + /// + /// 区间数量 + /// + public int Count => _entries.Count; + + /// + /// 创建区间映射 + /// + public RangeMap() + { + _entries = new List(); + } + + /// + /// 添加区间映射(闭区间) + /// + public void Add(T min, T max, object value) + { + Add(min, max, value, true, true); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, object value, bool minInclusive, bool maxInclusive) + { + if (min.CompareTo(max) > 0) + throw new ArgumentException("Min must be less than or equal to max"); + + _entries.Add(new RangeEntry + { + Min = min, + Max = max, + Value = value, + MinInclusive = minInclusive, + MaxInclusive = maxInclusive + }); + } + + /// + /// 添加单点映射 + /// + public void Add(T point, object value) + { + Add(point, point, value, true, true); + } + + /// + /// 添加无穷下界区间 + /// + public void AddBelow(T max, object value, bool inclusive = false) + { + _entries.Add(new RangeEntry + { + Min = default, + Max = max, + Value = value, + MinInclusive = false, + MaxInclusive = inclusive + }); + } + + /// + /// 添加无穷上界区间 + /// + public void AddAbove(T min, object value, bool inclusive = false) + { + _entries.Add(new RangeEntry + { + Min = min, + Max = default, + Value = value, + MinInclusive = inclusive, + MaxInclusive = false + }); + } + + /// + /// 查找值 + /// + public object Find(T key) + { + foreach (var entry in _entries) + { + if (Contains(entry, key)) + return entry.Value; + } + return null; + } + + /// + /// 查找所有匹配值 + /// + public List FindAll(T key) + { + var result = new List(); + foreach (var entry in _entries) + { + if (Contains(entry, key)) + result.Add(entry.Value); + } + return result; + } + + /// + /// 泛型查找 + /// + public TValue Find(T key) + { + var result = Find(key); + return result == null ? default : (TValue)result; + } + + private bool Contains(RangeEntry entry, T key) + { + int minCmp = entry.Min == null || entry.Min.Equals(default) ? -1 : key.CompareTo(entry.Min); + int maxCmp = entry.Max == null || entry.Max.Equals(default) ? 1 : key.CompareTo(entry.Max); + + bool minOk = entry.MinInclusive ? minCmp >= 0 : minCmp > 0; + bool maxOk = entry.MaxInclusive ? maxCmp <= 0 : maxCmp < 0; + + return minOk && maxOk; + } + + /// + /// 移除区间 + /// + public bool Remove(T min, T max) + { + return _entries.RemoveAll(e => + e.Min.CompareTo(min) == 0 && e.Max.CompareTo(max) == 0) > 0; + } + + /// + /// 清空 + /// + public void Clear() + { + _entries.Clear(); + } + + /// + /// 获取所有区间 + /// + public IEnumerable<(T Min, T Max, object Value)> GetAllRanges() + { + return _entries.Select(e => (e.Min, e.Max, e.Value)); + } + } + + /// + /// 类型化区间映射 + /// + public class RangeMap where T : IComparable + { + private readonly List _entries; + + private class RangeEntry + { + public T Min { get; set; } + public T Max { get; set; } + public TValue Value { get; set; } + public bool MinInclusive { get; set; } + public bool MaxInclusive { get; set; } + } + + /// + /// 区间数量 + /// + public int Count => _entries.Count; + + /// + /// 创建区间映射 + /// + public RangeMap() + { + _entries = new List(); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, TValue value) + { + Add(min, max, value, true, true); + } + + /// + /// 添加区间映射 + /// + public void Add(T min, T max, TValue value, bool minInclusive, bool maxInclusive) + { + if (min.CompareTo(max) > 0) + throw new ArgumentException("Min must be less than or equal to max"); + + _entries.Add(new RangeEntry + { + Min = min, + Max = max, + Value = value, + MinInclusive = minInclusive, + MaxInclusive = maxInclusive + }); + } + + /// + /// 查找值 + /// + public TValue Find(T key) + { + foreach (var entry in _entries) + { + if (Contains(entry, key)) + return entry.Value; + } + return default; + } + + /// + /// 查找所有匹配值 + /// + public List FindAll(T key) + { + var result = new List(); + foreach (var entry in _entries) + { + if (Contains(entry, key)) + result.Add(entry.Value); + } + return result; + } + + /// + /// 尝试查找 + /// + public bool TryFind(T key, out TValue value) + { + value = Find(key); + return !EqualityComparer.Default.Equals(value, default); + } + + private bool Contains(RangeEntry entry, T key) + { + int minCmp = key.CompareTo(entry.Min); + int maxCmp = key.CompareTo(entry.Max); + + bool minOk = entry.MinInclusive ? minCmp >= 0 : minCmp > 0; + bool maxOk = entry.MaxInclusive ? maxCmp <= 0 : maxCmp < 0; + + return minOk && maxOk; + } + + /// + /// 清空 + /// + public void Clear() + { + _entries.Clear(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs b/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs new file mode 100644 index 0000000..49fac46 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/MultiMapUtil.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 多值字典工具类 + /// 一个键可以对应多个值 + /// + public static class MultiMapUtil + { + /// + /// 创建多值字典 + /// + public static MultiMap Create() + { + return new MultiMap(); + } + + /// + /// 创建多值字典(使用指定的集合工厂) + /// + public static MultiMap Create(Func> collectionFactory) + { + return new MultiMap(collectionFactory); + } + } + + /// + /// 多值字典实现 + /// + /// 键类型 + /// 值类型 + public class MultiMap + { + private readonly Dictionary> _dictionary; + private readonly Func> _collectionFactory; + private int _valueCount; + + /// + /// 键数量 + /// + public int KeyCount => _dictionary.Count; + + /// + /// 值总数 + /// + public int ValueCount => _valueCount; + + /// + /// 所有键 + /// + public ICollection Keys => _dictionary.Keys; + + /// + /// 获取指定键的所有值 + /// + public ICollection this[TKey key] + { + get + { + if (_dictionary.TryGetValue(key, out var values)) + return values; + return new List(); + } + } + + /// + /// 创建多值字典(默认使用 List) + /// + public MultiMap() : this(() => new List()) { } + + /// + /// 创建多值字典(使用指定集合工厂) + /// + public MultiMap(Func> collectionFactory) + { + _dictionary = new Dictionary>(); + _collectionFactory = collectionFactory ?? (() => new List()); + _valueCount = 0; + } + + /// + /// 添加键值对 + /// + public void Add(TKey key, TValue value) + { + if (!_dictionary.TryGetValue(key, out var values)) + { + values = _collectionFactory(); + _dictionary[key] = values; + } + values.Add(value); + _valueCount++; + } + + /// + /// 批量添加值到指定键 + /// + public void AddRange(TKey key, IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + + foreach (var value in values) + { + Add(key, value); + } + } + + /// + /// 移除指定键的指定值 + /// + public bool Remove(TKey key, TValue value) + { + if (_dictionary.TryGetValue(key, out var values)) + { + if (values.Remove(value)) + { + _valueCount--; + if (values.Count == 0) + { + _dictionary.Remove(key); + } + return true; + } + } + return false; + } + + /// + /// 移除指定键的所有值 + /// + public bool RemoveAll(TKey key) + { + if (_dictionary.TryGetValue(key, out var values)) + { + _valueCount -= values.Count; + _dictionary.Remove(key); + return true; + } + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return _dictionary.ContainsKey(key); + } + + /// + /// 是否包含指定键值对 + /// + public bool Contains(TKey key, TValue value) + { + if (_dictionary.TryGetValue(key, out var values)) + { + return values.Contains(value); + } + return false; + } + + /// + /// 获取指定键的值数量 + /// + public int GetValueCount(TKey key) + { + if (_dictionary.TryGetValue(key, out var values)) + return values.Count; + return 0; + } + + /// + /// 尝试获取值 + /// + public bool TryGetValues(TKey key, out ICollection values) + { + return _dictionary.TryGetValue(key, out values); + } + + /// + /// 清空 + /// + public void Clear() + { + _dictionary.Clear(); + _valueCount = 0; + } + + /// + /// 获取所有键值对 + /// + public IEnumerable> GetAllKeyValuePairs() + { + foreach (var kvp in _dictionary) + { + foreach (var value in kvp.Value) + { + yield return new KeyValuePair(kvp.Key, value); + } + } + } + + /// + /// 获取所有值 + /// + public IEnumerable GetAllValues() + { + return _dictionary.Values.SelectMany(v => v); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/ObservableCollection.cs b/EasyTool.Core/CollectionsCategory/ObservableCollection.cs new file mode 100644 index 0000000..4964410 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/ObservableCollection.cs @@ -0,0 +1,160 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Specialized; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 可观察集合 + /// 当集合发生变化时触发事件 + /// + /// 元素类型 + public class ObservableCollection : IList, INotifyCollectionChanged + { + private readonly List _items = new(); + + /// + /// 集合变化事件 + /// + public event NotifyCollectionChangedEventHandler? CollectionChanged; + + /// + /// 元素数量 + /// + public int Count => _items.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 获取或设置指定索引的元素 + /// + public T this[int index] + { + get => _items[index]; + set + { + var oldItem = _items[index]; + _items[index] = value; + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Replace, value, oldItem, index)); + } + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + _items.Add(item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, item, _items.Count - 1)); + } + + /// + /// 添加多个元素 + /// + public void AddRange(IEnumerable items) + { + var index = _items.Count; + var list = new List(items); + _items.AddRange(list); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, list, index)); + } + + /// + /// 插入元素 + /// + public void Insert(int index, T item) + { + _items.Insert(index, item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Add, item, index)); + } + + /// + /// 移除元素 + /// + public bool Remove(T item) + { + var index = _items.IndexOf(item); + if (index < 0) + return false; + + _items.RemoveAt(index); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, item, index)); + return true; + } + + /// + /// 移除指定位置的元素 + /// + public void RemoveAt(int index) + { + var item = _items[index]; + _items.RemoveAt(index); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Remove, item, index)); + } + + /// + /// 清空集合 + /// + public void Clear() + { + _items.Clear(); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Reset)); + } + + /// + /// 移动元素 + /// + public void Move(int oldIndex, int newIndex) + { + var item = _items[oldIndex]; + _items.RemoveAt(oldIndex); + _items.Insert(newIndex, item); + OnCollectionChanged(new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Move, item, newIndex, oldIndex)); + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) => _items.IndexOf(item); + + /// + /// 是否包含元素 + /// + public bool Contains(T item) => _items.Contains(item); + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) => _items.CopyTo(array, arrayIndex); + + /// + /// 获取枚举器 + /// + public IEnumerator GetEnumerator() => _items.GetEnumerator(); + + /// + /// 获取枚举器 + /// + IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator(); + + /// + /// 触发集合变化事件 + /// + protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + { + CollectionChanged?.Invoke(this, e); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PagedList.cs b/EasyTool.Core/CollectionsCategory/PagedList.cs new file mode 100644 index 0000000..05be673 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/PagedList.cs @@ -0,0 +1,231 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 分页列表 + /// + /// 元素类型 + public class PagedList : IList + { + private readonly List _items; + + /// + /// 当前页号(从1开始) + /// + public int PageNumber { get; } + + /// + /// 每页大小 + /// + public int PageSize { get; } + + /// + /// 总记录数 + /// + public int TotalCount { get; } + + /// + /// 总页数 + /// + public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize); + + /// + /// 是否有上一页 + /// + public bool HasPreviousPage => PageNumber > 1; + + /// + /// 是否有下一页 + /// + public bool HasNextPage => PageNumber < TotalPages; + + /// + /// 是否是第一页 + /// + public bool IsFirstPage => PageNumber == 1; + + /// + /// 是否是最后一页 + /// + public bool IsLastPage => PageNumber == TotalPages; + + /// + /// 当前页记录数 + /// + public int Count => _items.Count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 获取或设置指定索引的元素 + /// + public T this[int index] + { + get => _items[index]; + set => _items[index] = value; + } + + /// + /// 创建分页列表 + /// + public PagedList(IEnumerable items, int pageNumber, int pageSize, int totalCount) + { + if (pageNumber < 1) + throw new ArgumentOutOfRangeException(nameof(pageNumber), "页号必须大于0"); + if (pageSize < 1) + throw new ArgumentOutOfRangeException(nameof(pageSize), "每页大小必须大于0"); + + _items = new List(items); + PageNumber = pageNumber; + PageSize = pageSize; + TotalCount = totalCount; + } + + /// + /// 从完整列表创建分页 + /// + public static PagedList Create(IEnumerable source, int pageNumber, int pageSize) + { + var list = new List(source); + var totalCount = list.Count; + var skip = (pageNumber - 1) * pageSize; + var items = list.Skip(skip).Take(pageSize); + return new PagedList(items, pageNumber, pageSize, totalCount); + } + + /// + /// 从查询创建分页 + /// + public static PagedList Create(IQueryable source, int pageNumber, int pageSize) + { + var totalCount = source.Count(); + var skip = (pageNumber - 1) * pageSize; + var items = source.Skip(skip).Take(pageSize).ToList(); + return new PagedList(items, pageNumber, pageSize, totalCount); + } + + /// + /// 获取页码范围 + /// + public IEnumerable GetPageRange(int displayCount = 5) + { + var start = Math.Max(1, PageNumber - displayCount / 2); + var end = Math.Min(TotalPages, start + displayCount - 1); + + if (end - start + 1 < displayCount) + { + start = Math.Max(1, end - displayCount + 1); + } + + for (int i = start; i <= end; i++) + { + yield return i; + } + } + + /// + /// 获取分页信息 + /// + public PageInfo GetPageInfo() + { + return new PageInfo + { + PageNumber = PageNumber, + PageSize = PageSize, + TotalCount = TotalCount, + TotalPages = TotalPages, + HasPreviousPage = HasPreviousPage, + HasNextPage = HasNextPage + }; + } + + #region IList 实现 + + public int IndexOf(T item) => _items.IndexOf(item); + + public void Insert(int index, T item) => _items.Insert(index, item); + + public void RemoveAt(int index) => _items.RemoveAt(index); + + public void Add(T item) => _items.Add(item); + + public void Clear() => _items.Clear(); + + public bool Contains(T item) => _items.Contains(item); + + public void CopyTo(T[] array, int arrayIndex) => _items.CopyTo(array, arrayIndex); + + public bool Remove(T item) => _items.Remove(item); + + public IEnumerator GetEnumerator() => _items.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator(); + + #endregion + } + + /// + /// 分页信息 + /// + public class PageInfo + { + /// + /// 当前页号 + /// + public int PageNumber { get; set; } + + /// + /// 每页大小 + /// + public int PageSize { get; set; } + + /// + /// 总记录数 + /// + public int TotalCount { get; set; } + + /// + /// 总页数 + /// + public int TotalPages { get; set; } + + /// + /// 是否有上一页 + /// + public bool HasPreviousPage { get; set; } + + /// + /// 是否有下一页 + /// + public bool HasNextPage { get; set; } + } + + /// + /// 分页工具类 + /// + public static class PagedListExtensions + { + /// + /// 转换为分页列表 + /// + public static PagedList ToPagedList(this IEnumerable source, int pageNumber, int pageSize) + { + return PagedList.Create(source, pageNumber, pageSize); + } + + /// + /// 转换为分页列表 + /// + public static PagedList ToPagedList(this IQueryable source, int pageNumber, int pageSize) + { + return PagedList.Create(source, pageNumber, pageSize); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs b/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs new file mode 100644 index 0000000..b576423 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/PermutationCombinationUtil.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 排列组合工具类 + /// 提供排列和组合的生成功能 + /// + public static class PermutationUtil + { + /// + /// 生成所有全排列 + /// + /// 元素类型 + /// 元素集合 + /// 所有排列 + public static IEnumerable> Permutations(IEnumerable elements) + { + var list = new List(elements); + return PermutationsCore(list, 0, list.Count); + } + + /// + /// 生成指定长度的排列 + /// + public static IEnumerable> Permutations(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + return PermutationsCore(list, 0, length); + } + + /// + /// 生成可重复排列(每个位置可以选择任意元素) + /// + public static IEnumerable> PermutationsWithRepetition(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count == 0) + yield break; + + var indices = new int[length]; + var current = new T[length]; + + while (true) + { + for (int i = 0; i < length; i++) + { + current[i] = list[indices[i]]; + } + yield return new List(current); + + int pos = length - 1; + while (pos >= 0) + { + indices[pos]++; + if (indices[pos] < list.Count) + break; + indices[pos] = 0; + pos--; + } + + if (pos < 0) + break; + } + } + + private static IEnumerable> PermutationsCore(List list, int start, int length) + { + if (start == length) + { + yield return new List(list.GetRange(0, length)); + yield break; + } + + for (int i = start; i < list.Count; i++) + { + Swap(list, start, i); + foreach (var perm in PermutationsCore(list, start + 1, length)) + { + yield return perm; + } + Swap(list, start, i); + } + } + + /// + /// 计算排列数 A(n,r) = n! / (n-r)! + /// + public static long Count(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("Invalid parameters"); + if (r == 0) return 1; + + long result = 1; + for (int i = 0; i < r; i++) + { + result *= (n - i); + } + return result; + } + + private static void Swap(List list, int i, int j) + { + if (i != j) + { + T temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + } + } + + /// + /// 组合工具类 + /// + public static class CombinationUtil + { + /// + /// 生成所有组合 + /// + /// 元素类型 + /// 元素集合 + /// 组合长度 + /// 所有组合 + public static IEnumerable> Combinations(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count < length) + yield break; + + var indices = new int[length]; + for (int i = 0; i < length; i++) + { + indices[i] = i; + } + + while (true) + { + yield return GetItems(list, indices); + + int pos = length - 1; + while (pos >= 0 && indices[pos] == list.Count - length + pos) + { + pos--; + } + + if (pos < 0) + break; + + indices[pos]++; + for (int i = pos + 1; i < length; i++) + { + indices[i] = indices[i - 1] + 1; + } + } + } + + /// + /// 生成所有长度的组合(从1到n) + /// + public static IEnumerable> AllCombinations(IEnumerable elements) + { + var list = new List(elements); + for (int length = 1; length <= list.Count; length++) + { + foreach (var combo in Combinations(list, length)) + { + yield return combo; + } + } + } + + /// + /// 生成可重复组合 + /// + public static IEnumerable> CombinationsWithRepetition(IEnumerable elements, int length) + { + if (length <= 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + var list = new List(elements); + if (list.Count == 0) + yield break; + + var indices = new int[length]; + + while (true) + { + yield return GetItems(list, indices); + + int pos = length - 1; + while (pos >= 0 && indices[pos] == list.Count - 1) + { + pos--; + } + + if (pos < 0) + break; + + indices[pos]++; + for (int i = pos + 1; i < length; i++) + { + indices[i] = indices[pos]; + } + } + } + + /// + /// 计算组合数 C(n,r) = n! / (r! * (n-r)!) + /// + public static long Count(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("Invalid parameters"); + if (r == 0 || r == n) return 1; + + // 优化:使用较小的 r 计算 + if (r > n - r) + r = n - r; + + long result = 1; + for (int i = 0; i < r; i++) + { + result = result * (n - i) / (i + 1); + } + return result; + } + + private static IEnumerable GetItems(IList list, int[] indices) + { + var result = new T[indices.Length]; + for (int i = 0; i < indices.Length; i++) + { + result[i] = list[indices[i]]; + } + return result; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/QueueExtension.cs b/EasyTool.Core/CollectionsCategory/QueueExtension.cs deleted file mode 100644 index 4f8dfc0..0000000 --- a/EasyTool.Core/CollectionsCategory/QueueExtension.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; - -namespace EasyTool.Extension -{ - /// - /// 队列工具类 - /// - public static class QueueExtension - { - - /// - /// 将指定集合中的元素添加到队列的末尾。 - /// - /// 队列元素类型 - /// 队列 - /// 要添加到队列中的集合 - public static void EnqueueRange(this Queue queue, IEnumerable collection) => QueueUtil.EnqueueRange(queue, collection); - - /// - /// 从队列中移除指定元素的第一个匹配项。 - /// - /// 队列元素类型 - /// 队列 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(this Queue queue, T item) => QueueUtil.Remove(queue, item); - - } -} diff --git a/EasyTool.Core/CollectionsCategory/QueueUtil.cs b/EasyTool.Core/CollectionsCategory/QueueUtil.cs index 8c87309..c526866 100644 --- a/EasyTool.Core/CollectionsCategory/QueueUtil.cs +++ b/EasyTool.Core/CollectionsCategory/QueueUtil.cs @@ -1,124 +1,368 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using System.Threading; +using System.Threading.Tasks; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// - /// 队列工具类 + /// 线程安全队列工具类 + /// 提供生产者-消费者模式的队列操作 /// - public class QueueUtil + /// 元素类型 + public class QueueUtil { + private readonly Queue _queue = new(); + private readonly object _lock = new(); + private readonly SemaphoreSlim _signal = new(0); + /// - /// 将指定元素添加到队列的末尾。 + /// 获取队列元素数量 /// - /// 队列元素类型 - /// 队列 - /// 要添加的元素 - public static void Enqueue(Queue queue, T item) + public int Count { - queue.Enqueue(item); + get + { + lock (_lock) + { + return _queue.Count; + } + } } /// - /// 将指定集合中的元素添加到队列的末尾。 + /// 检查队列是否为空 /// - /// 队列元素类型 - /// 队列 - /// 要添加到队列中的集合 - public static void EnqueueRange(Queue queue, IEnumerable collection) + public bool IsEmpty { - foreach (T item in collection) + get { - queue.Enqueue(item); + lock (_lock) + { + return _queue.Count == 0; + } + } + } + + /// + /// 入队 + /// + /// 元素 + public void Enqueue(T item) + { + lock (_lock) + { + _queue.Enqueue(item); + _signal.Release(); } } /// - /// 移除并返回位于队列开头的元素。 + /// 批量入队 /// - /// 队列元素类型 - /// 队列 - /// 队列开头的元素 - /// 队列为空时引发异常 - public static T Dequeue(Queue queue) + /// 元素集合 + public void EnqueueRange(IEnumerable items) { - return queue.Dequeue(); + lock (_lock) + { + foreach (var item in items) + { + _queue.Enqueue(item); + _signal.Release(); + } + } } /// - /// 返回位于队列开头的元素而不将其移除。 + /// 出队 /// - /// 队列元素类型 - /// 队列 - /// 队列开头的元素 - /// 队列为空时引发异常 - public static T Peek(Queue queue) + /// 元素 + public T? Dequeue() { - return queue.Peek(); + _signal.Wait(); + + lock (_lock) + { + return _queue.Count > 0 ? _queue.Dequeue() : default; + } } /// - /// 确定队列中是否包含指定元素。 + /// 尝试出队 /// - /// 队列元素类型 - /// 队列 - /// 要查找的元素 - /// 如果队列包含指定元素,则为 true;否则为 false。 - public static bool Contains(Queue queue, T item) + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) { - return queue.Contains(item); + lock (_lock) + { + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + _signal.Wait(0); + return true; + } + + item = default; + return false; + } } /// - /// 从队列中移除指定元素的第一个匹配项。 + /// 尝试出队(带超时) /// - /// 队列元素类型 - /// 队列 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(Queue queue, T item) + /// 超时时间 + /// 元素 + /// 是否成功 + public bool TryDequeue(TimeSpan timeout, out T? item) { - if (queue.Contains(item)) + if (_signal.Wait(timeout)) { - queue = new Queue(queue.Where(x => !x.Equals(item))); - return true; + lock (_lock) + { + if (_queue.Count > 0) + { + item = _queue.Dequeue(); + return true; + } + } } + + item = default; return false; } /// - /// 将队列中的所有元素复制到新数组中。 + /// 异步出队 + /// + /// 取消令牌 + /// 元素 + public async Task DequeueAsync(CancellationToken cancellationToken = default) + { + await _signal.WaitAsync(cancellationToken); + + lock (_lock) + { + return _queue.Count > 0 ? _queue.Dequeue() : default; + } + } + + /// + /// 异步尝试出队 + /// + /// 超时时间 + /// 取消令牌 + /// 元素或默认值 + public async Task<(bool Success, T? Item)> TryDequeueAsync(TimeSpan timeout, CancellationToken cancellationToken = default) + { + if (await _signal.WaitAsync(timeout, cancellationToken)) + { + lock (_lock) + { + if (_queue.Count > 0) + { + return (true, _queue.Dequeue()); + } + } + } + + return (false, default); + } + + /// + /// 查看队首元素(不出队) + /// + /// 队首元素 + public T? Peek() + { + lock (_lock) + { + return _queue.Count > 0 ? _queue.Peek() : default; + } + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 是否成功 + public bool TryPeek(out T? item) + { + lock (_lock) + { + if (_queue.Count > 0) + { + item = _queue.Peek(); + return true; + } + + item = default; + return false; + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + while (_signal.CurrentCount > 0) + { + _signal.Wait(0); + } + _queue.Clear(); + } + } + + /// + /// 获取所有元素(不出队) + /// + /// 元素数组 + public T[] ToArray() + { + lock (_lock) + { + return _queue.ToArray(); + } + } + + /// + /// 获取所有元素并清空队列 /// - /// 队列元素类型 - /// 队列 - /// 包含队列中所有元素的新数组 - public static T[] ToArray(Queue queue) + /// 元素数组 + public T[] Drain() { - return queue.ToArray(); + lock (_lock) + { + var items = _queue.ToArray(); + _queue.Clear(); + while (_signal.CurrentCount > 0) + { + _signal.Wait(0); + } + return items; + } } + } + + /// + /// 优先级队列工具类 + /// + /// 元素类型 + public class PriorityQueue + { + private readonly SortedDictionary> _queues = new(); + private readonly object _lock = new(); /// - /// 将队列中的所有元素复制到新数组中,从指定的索引开始。 + /// 获取元素数量 /// - /// 队列元素类型 - /// 队列 - /// 要复制到的目标数组 - /// 目标数组的起始索引 - public static void CopyTo(Queue queue, T[] array, int arrayIndex) + public int Count { - queue.CopyTo(array, arrayIndex); + get + { + lock (_lock) + { + return _queues.Sum(q => q.Value.Count); + } + } } /// - /// 从队列中移除所有元素。 + /// 入队 /// - /// 队列元素类型 - /// 队列 - public static void Clear(Queue queue) + /// 元素 + /// 优先级(数字越小优先级越高) + public void Enqueue(T item, int priority = 0) { - queue.Clear(); + lock (_lock) + { + if (!_queues.TryGetValue(priority, out var queue)) + { + queue = new Queue(); + _queues[priority] = queue; + } + + queue.Enqueue(item); + } + } + + /// + /// 出队 + /// + /// 元素 + public T? Dequeue() + { + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + return kvp.Value.Dequeue(); + } + } + + return default; + } + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) + { + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + item = kvp.Value.Dequeue(); + return true; + } + } + + item = default; + return false; + } + } + + /// + /// 查看队首元素 + /// + /// 元素 + public T? Peek() + { + lock (_lock) + { + foreach (var kvp in _queues) + { + if (kvp.Value.Count > 0) + { + return kvp.Value.Peek(); + } + } + + return default; + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + _queues.Clear(); + } } } -} + +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/RingBuffer.cs b/EasyTool.Core/CollectionsCategory/RingBuffer.cs new file mode 100644 index 0000000..4e08c4d --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/RingBuffer.cs @@ -0,0 +1,319 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 环形缓冲区 + /// 线程安全,支持固定大小的循环队列 + /// + /// 元素类型 + public class RingBuffer + { + private readonly T[] _buffer; + private int _head; + private int _tail; + private int _count; + private readonly object _lock = new(); + + /// + /// 创建环形缓冲区 + /// + /// 容量 + public RingBuffer(int capacity) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + + _buffer = new T[capacity]; + _head = 0; + _tail = 0; + _count = 0; + } + + /// + /// 容量 + /// + public int Capacity => _buffer.Length; + + /// + /// 当前元素数量 + /// + public int Count + { + get { lock (_lock) { return _count; } } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 是否已满 + /// + public bool IsFull => Count == Capacity; + + /// + /// 添加元素 + /// + public void Add(T item) + { + lock (_lock) + { + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + + if (_count == Capacity) + { + // 缓冲区已满,覆盖最旧的元素 + _head = (_head + 1) % Capacity; + } + else + { + _count++; + } + } + } + + /// + /// 尝试添加元素(如果已满则返回false) + /// + public bool TryAdd(T item) + { + lock (_lock) + { + if (_count == Capacity) + return false; + + _buffer[_tail] = item; + _tail = (_tail + 1) % Capacity; + _count++; + return true; + } + } + + /// + /// 获取并移除最旧的元素 + /// + public T? Take() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + + var item = _buffer[_head]; + _buffer[_head] = default!; + _head = (_head + 1) % Capacity; + _count--; + return item; + } + } + + /// + /// 尝试获取并移除最旧的元素 + /// + public bool TryTake(out T? item) + { + lock (_lock) + { + if (_count == 0) + { + item = default; + return false; + } + + item = _buffer[_head]; + _buffer[_head] = default!; + _head = (_head + 1) % Capacity; + _count--; + return true; + } + } + + /// + /// 查看最旧的元素(不移除) + /// + public T? Peek() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + return _buffer[_head]; + } + } + + /// + /// 尝试查看最旧的元素 + /// + public bool TryPeek(out T? item) + { + lock (_lock) + { + if (_count == 0) + { + item = default; + return false; + } + item = _buffer[_head]; + return true; + } + } + + /// + /// 查看最新的元素(不移除) + /// + public T? PeekLatest() + { + lock (_lock) + { + if (_count == 0) + throw new InvalidOperationException("缓冲区为空"); + var index = (_tail - 1 + Capacity) % Capacity; + return _buffer[index]; + } + } + + /// + /// 获取指定索引的元素(从最旧的开始) + /// + public T? GetAt(int index) + { + lock (_lock) + { + if (index < 0 || index >= _count) + throw new ArgumentOutOfRangeException(nameof(index)); + return _buffer[(_head + index) % Capacity]; + } + } + + /// + /// 获取所有元素(从最旧到最新) + /// + public T[] ToArray() + { + lock (_lock) + { + var result = new T[_count]; + for (int i = 0; i < _count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + } + + /// + /// 清空缓冲区 + /// + public void Clear() + { + lock (_lock) + { + Array.Clear(_buffer, 0, Capacity); + _head = 0; + _tail = 0; + _count = 0; + } + } + + /// + /// 遍历所有元素 + /// + public IEnumerator GetEnumerator() + { + T[] array; + lock (_lock) + { + array = ToArray(); + } + foreach (var item in array) + { + yield return item; + } + } + + /// + /// 复制到数组 + /// + public void CopyTo(T[] array, int arrayIndex) + { + lock (_lock) + { + for (int i = 0; i < _count && arrayIndex + i < array.Length; i++) + { + array[arrayIndex + i] = _buffer[(_head + i) % Capacity]; + } + } + } + + /// + /// 查找元素 + /// + public bool Contains(T item) + { + lock (_lock) + { + for (int i = 0; i < _count; i++) + { + if (EqualityComparer.Default.Equals(_buffer[(_head + i) % Capacity], item)) + return true; + } + return false; + } + } + + /// + /// 查找元素索引 + /// + public int IndexOf(T item) + { + lock (_lock) + { + for (int i = 0; i < _count; i++) + { + if (EqualityComparer.Default.Equals(_buffer[(_head + i) % Capacity], item)) + return i; + } + return -1; + } + } + + /// + /// 获取最新的N个元素 + /// + public T[] GetLatest(int count) + { + lock (_lock) + { + count = Math.Min(count, _count); + var result = new T[count]; + for (int i = 0; i < count; i++) + { + var index = (_tail - count + i + Capacity) % Capacity; + result[i] = _buffer[index]; + } + return result; + } + } + + /// + /// 获取最旧的N个元素 + /// + public T[] GetOldest(int count) + { + lock (_lock) + { + count = Math.Min(count, _count); + var result = new T[count]; + for (int i = 0; i < count; i++) + { + result[i] = _buffer[(_head + i) % Capacity]; + } + return result; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/SkipListUtil.cs b/EasyTool.Core/CollectionsCategory/SkipListUtil.cs new file mode 100644 index 0000000..09f2b7c --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SkipListUtil.cs @@ -0,0 +1,360 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 跳表工具类 + /// 一种随机化的数据结构,基于并联的链表,实现高效查找、插入、删除 + /// 平均时间复杂度 O(log n) + /// + public static class SkipListUtil + { + /// + /// 创建跳表 + /// + public static SkipList Create() + where TKey : IComparable + { + return new SkipList(); + } + + /// + /// 创建指定最大层级的跳表 + /// + public static SkipList Create(int maxLevel, double probability = 0.5) + where TKey : IComparable + { + return new SkipList(maxLevel, probability); + } + } + + /// + /// 跳表实现 + /// + /// 键类型 + /// 值类型 + public class SkipList : IDictionary + where TKey : IComparable + { + private class SkipListNode + { + public TKey Key { get; set; } + public TValue Value { get; set; } + public SkipListNode[] Forward { get; set; } + + public SkipListNode(int level, TKey key = default, TValue value = default) + { + Key = key; + Value = value; + Forward = new SkipListNode[level + 1]; + } + } + + private readonly SkipListNode _header; + private readonly int _maxLevel; + private readonly double _probability; + private readonly Random _random; + private int _level; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否只读 + /// + public bool IsReadOnly => false; + + /// + /// 键集合 + /// + public ICollection Keys => GetElements().Select(x => x.Key).ToList(); + + /// + /// 值集合 + /// + public ICollection Values => GetElements().Select(x => x.Value).ToList(); + + /// + /// 索引访问 + /// + public TValue this[TKey key] + { + get => Get(key); + set => Add(key, value); + } + + /// + /// 创建跳表(默认最大16层) + /// + public SkipList() : this(16, 0.5) { } + + /// + /// 创建指定层级的跳表 + /// + public SkipList(int maxLevel, double probability = 0.5) + { + if (maxLevel <= 0) + throw new ArgumentOutOfRangeException(nameof(maxLevel)); + if (probability <= 0 || probability >= 1) + throw new ArgumentOutOfRangeException(nameof(probability)); + + _maxLevel = maxLevel; + _probability = probability; + _random = new Random(); + _level = 0; + _count = 0; + _header = new SkipListNode(_maxLevel); + } + + /// + /// 添加元素 + /// + public void Add(TKey key, TValue value) + { + var update = new SkipListNode[_maxLevel + 1]; + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + update[i] = current; + } + + current = current.Forward[0]; + + if (current != null && current.Key.CompareTo(key) == 0) + { + current.Value = value; + return; + } + + int newLevel = RandomLevel(); + + if (newLevel > _level) + { + for (int i = _level + 1; i <= newLevel; i++) + { + update[i] = _header; + } + _level = newLevel; + } + + var newNode = new SkipListNode(newLevel, key, value); + + for (int i = 0; i <= newLevel; i++) + { + newNode.Forward[i] = update[i].Forward[i]; + update[i].Forward[i] = newNode; + } + + _count++; + } + + /// + /// 添加键值对 + /// + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// 获取值 + /// + public TValue Get(TKey key) + { + if (!TryGetValue(key, out var value)) + throw new KeyNotFoundException($"Key '{key}' not found"); + return value; + } + + /// + /// 尝试获取值 + /// + public bool TryGetValue(TKey key, out TValue value) + { + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + } + + current = current.Forward[0]; + + if (current != null && current.Key.CompareTo(key) == 0) + { + value = current.Value; + return true; + } + + value = default; + return false; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(TKey key) + { + return TryGetValue(key, out _); + } + + /// + /// 是否包含键值对 + /// + public bool Contains(KeyValuePair item) + { + if (!TryGetValue(item.Key, out var value)) + return false; + return EqualityComparer.Default.Equals(value, item.Value); + } + + /// + /// 移除元素 + /// + public bool Remove(TKey key) + { + var update = new SkipListNode[_maxLevel + 1]; + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(key) < 0) + { + current = current.Forward[i]; + } + update[i] = current; + } + + current = current.Forward[0]; + + if (current == null || current.Key.CompareTo(key) != 0) + return false; + + for (int i = 0; i <= _level; i++) + { + if (update[i].Forward[i] != current) + break; + update[i].Forward[i] = current.Forward[i]; + } + + while (_level > 0 && _header.Forward[_level] == null) + { + _level--; + } + + _count--; + return true; + } + + /// + /// 移除键值对 + /// + public bool Remove(KeyValuePair item) + { + if (!Contains(item)) + return false; + return Remove(item.Key); + } + + /// + /// 清空 + /// + public void Clear() + { + for (int i = 0; i <= _maxLevel; i++) + { + _header.Forward[i] = null; + } + _level = 0; + _count = 0; + } + + /// + /// 复制到数组 + /// + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + foreach (var item in GetElements()) + { + array[arrayIndex++] = item; + } + } + + /// + /// 获取第一个元素 + /// + public KeyValuePair? First() + { + if (_header.Forward[0] == null) + return null; + return new KeyValuePair(_header.Forward[0].Key, _header.Forward[0].Value); + } + + /// + /// 获取范围 + /// + public IEnumerable> GetRange(TKey start, TKey end) + { + var current = _header; + + for (int i = _level; i >= 0; i--) + { + while (current.Forward[i] != null && current.Forward[i].Key.CompareTo(start) < 0) + { + current = current.Forward[i]; + } + } + + current = current.Forward[0]; + + while (current != null && current.Key.CompareTo(end) <= 0) + { + yield return new KeyValuePair(current.Key, current.Value); + current = current.Forward[0]; + } + } + + private int RandomLevel() + { + int level = 0; + while (_random.NextDouble() < _probability && level < _maxLevel) + { + level++; + } + return level; + } + + private IEnumerable> GetElements() + { + var current = _header.Forward[0]; + while (current != null) + { + yield return new KeyValuePair(current.Key, current.Value); + current = current.Forward[0]; + } + } + + public IEnumerator> GetEnumerator() + { + return GetElements().GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs b/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs new file mode 100644 index 0000000..e3ed449 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SpatialIndexUtil.cs @@ -0,0 +1,675 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 空间索引工具类 + /// + public static class SpatialIndexUtil + { + /// + /// 创建KD树(2维) + /// + public static KDTree CreateKDTree() + { + return new KDTree(2); + } + + /// + /// 创建KD树(指定维度) + /// + public static KDTree CreateKDTree(int dimensions) + { + return new KDTree(dimensions); + } + + /// + /// 创建四叉树 + /// + public static QuadTree CreateQuadTree(double minX, double minY, double maxX, double maxY) + { + return new QuadTree(minX, minY, maxX, maxY); + } + + /// + /// 创建网格索引 + /// + public static GridIndex CreateGridIndex(double minX, double minY, double maxX, double maxY, int cellCountX, int cellCountY) + { + return new GridIndex(minX, minY, maxX, maxY, cellCountX, cellCountY); + } + } + + /// + /// KD树(K维树) + /// 用于高维空间中的最近邻搜索 + /// + public class KDTree + { + private class KDNode + { + public double[] Point { get; set; } + public T Value { get; set; } + public KDNode Left { get; set; } + public KDNode Right { get; set; } + } + + private KDNode _root; + private readonly int _dimensions; + private int _count; + + /// + /// 维度 + /// + public int Dimensions => _dimensions; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建KD树 + /// + public KDTree(int dimensions) + { + if (dimensions <= 0) + throw new ArgumentOutOfRangeException(nameof(dimensions)); + + _dimensions = dimensions; + _root = null; + _count = 0; + } + + /// + /// 插入点 + /// + public void Insert(double[] point, T value) + { + if (point == null || point.Length != _dimensions) + throw new ArgumentException($"Point must have {_dimensions} dimensions"); + + _root = Insert(_root, point, value, 0); + _count++; + } + + private KDNode Insert(KDNode node, double[] point, T value, int depth) + { + if (node == null) + { + return new KDNode { Point = point, Value = value }; + } + + int axis = depth % _dimensions; + + if (point[axis] < node.Point[axis]) + { + node.Left = Insert(node.Left, point, value, depth + 1); + } + else + { + node.Right = Insert(node.Right, point, value, depth + 1); + } + + return node; + } + + /// + /// 查找最近邻 + /// + public (double[] Point, T Value)? FindNearest(double[] target) + { + if (target == null || target.Length != _dimensions) + throw new ArgumentException($"Target must have {_dimensions} dimensions"); + + if (_root == null) + return null; + + KDNode best = null; + double bestDist = double.MaxValue; + + FindNearest(_root, target, 0, ref best, ref bestDist); + + return best == null ? null : (best.Point, best.Value); + } + + private void FindNearest(KDNode node, double[] target, int depth, ref KDNode best, ref double bestDist) + { + if (node == null) + return; + + double dist = Distance(node.Point, target); + if (dist < bestDist) + { + bestDist = dist; + best = node; + } + + int axis = depth % _dimensions; + double diff = target[axis] - node.Point[axis]; + + KDNode near = diff < 0 ? node.Left : node.Right; + KDNode far = diff < 0 ? node.Right : node.Left; + + FindNearest(near, target, depth + 1, ref best, ref bestDist); + + // 检查是否需要搜索另一侧 + if (diff * diff < bestDist) + { + FindNearest(far, target, depth + 1, ref best, ref bestDist); + } + } + + /// + /// 查找K个最近邻 + /// + public List<(double[] Point, T Value, double Distance)> FindKNearest(double[] target, int k) + { + if (target == null || target.Length != _dimensions) + throw new ArgumentException($"Target must have {_dimensions} dimensions"); + + var result = new List<(double[] Point, T Value, double Distance)>(); + + if (_root == null) + return result; + + var heap = new List<(double Dist, KDNode Node)>(); + + FindKNearest(_root, target, 0, heap, k); + + foreach (var (dist, node) in heap) + { + result.Add((node.Point, node.Value, dist)); + } + + return result.OrderBy(x => x.Distance).ToList(); + } + + private void FindKNearest(KDNode node, double[] target, int depth, List<(double Dist, KDNode Node)> heap, int k) + { + if (node == null) + return; + + double dist = Distance(node.Point, target); + + if (heap.Count < k) + { + heap.Add((dist, node)); + heap.Sort((a, b) => b.Dist.CompareTo(a.Dist)); + } + else if (dist < heap[0].Dist) + { + heap[0] = (dist, node); + heap.Sort((a, b) => b.Dist.CompareTo(a.Dist)); + } + + int axis = depth % _dimensions; + double diff = target[axis] - node.Point[axis]; + + KDNode near = diff < 0 ? node.Left : node.Right; + KDNode far = diff < 0 ? node.Right : node.Left; + + FindKNearest(near, target, depth + 1, heap, k); + + double maxDist = heap.Count < k ? double.MaxValue : heap[0].Dist; + if (diff * diff < maxDist) + { + FindKNearest(far, target, depth + 1, heap, k); + } + } + + /// + /// 范围查询 + /// + public List<(double[] Point, T Value)> RangeQuery(double[] min, double[] max) + { + var result = new List<(double[] Point, T Value)>(); + RangeQuery(_root, min, max, 0, result); + return result; + } + + private void RangeQuery(KDNode node, double[] min, double[] max, int depth, List<(double[] Point, T Value)> result) + { + if (node == null) + return; + + bool inside = true; + for (int i = 0; i < _dimensions; i++) + { + if (node.Point[i] < min[i] || node.Point[i] > max[i]) + { + inside = false; + break; + } + } + + if (inside) + { + result.Add((node.Point, node.Value)); + } + + int axis = depth % _dimensions; + + if (min[axis] <= node.Point[axis]) + { + RangeQuery(node.Left, min, max, depth + 1, result); + } + if (max[axis] >= node.Point[axis]) + { + RangeQuery(node.Right, min, max, depth + 1, result); + } + } + + private double Distance(double[] a, double[] b) + { + double sum = 0; + for (int i = 0; i < _dimensions; i++) + { + double diff = a[i] - b[i]; + sum += diff * diff; + } + return Math.Sqrt(sum); + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + _count = 0; + } + } + + /// + /// 四叉树 + /// 用于二维空间的区域查询 + /// + public class QuadTree + { + private class QuadNode + { + public double X { get; set; } + public double Y { get; set; } + public T Value { get; set; } + + public QuadNode(double x, double y, T value) + { + X = x; + Y = y; + Value = value; + } + } + + private class QuadTreeNode + { + public double MinX { get; set; } + public double MinY { get; set; } + public double MaxX { get; set; } + public double MaxY { get; set; } + + public List Points { get; set; } + public QuadTreeNode[] Children { get; set; } + + public int Capacity { get; set; } + public bool IsDivided { get; set; } + + public QuadTreeNode(double minX, double minY, double maxX, double maxY, int capacity = 4) + { + MinX = minX; + MinY = minY; + MaxX = maxX; + MaxY = maxY; + Capacity = capacity; + Points = new List(); + Children = null; + IsDivided = false; + } + + public double MidX => (MinX + MaxX) / 2; + public double MidY => (MinY + MaxY) / 2; + + public bool Contains(double x, double y) + { + return x >= MinX && x <= MaxX && y >= MinY && y <= MaxY; + } + + public bool Intersects(double minX, double minY, double maxX, double maxY) + { + return !(MaxX < minX || MinX > maxX || MaxY < minY || MinY > maxY); + } + + public void Subdivide() + { + Children = new QuadTreeNode[4]; + Children[0] = new QuadTreeNode(MinX, MidY, MidX, MaxY, Capacity); // NW + Children[1] = new QuadTreeNode(MidX, MidY, MaxX, MaxY, Capacity); // NE + Children[2] = new QuadTreeNode(MinX, MinY, MidX, MidY, Capacity); // SW + Children[3] = new QuadTreeNode(MidX, MinY, MaxX, MidY, Capacity); // SE + IsDivided = true; + } + } + + private readonly QuadTreeNode _root; + private readonly int _capacity; + private int _count; + + /// + /// 边界 + /// + public (double MinX, double MinY, double MaxX, double MaxY) Bounds => + (_root.MinX, _root.MinY, _root.MaxX, _root.MaxY); + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _count == 0; + + /// + /// 创建四叉树 + /// + public QuadTree(double minX, double minY, double maxX, double maxY, int capacity = 4) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _root = new QuadTreeNode(minX, minY, maxX, maxY, capacity); + _count = 0; + } + + /// + /// 插入点 + /// + public bool Insert(double x, double y, T value) + { + if (!_root.Contains(x, y)) + return false; + + Insert(_root, x, y, value); + _count++; + return true; + } + + private void Insert(QuadTreeNode node, double x, double y, T value) + { + if (node.IsDivided) + { + int index = GetChildIndex(node, x, y); + if (index >= 0) + { + Insert(node.Children[index], x, y, value); + } + return; + } + + if (node.Points.Count < node.Capacity) + { + node.Points.Add(new QuadNode(x, y, value)); + } + else + { + node.Subdivide(); + + // 重新分配现有点 + foreach (var point in node.Points) + { + int index = GetChildIndex(node, point.X, point.Y); + if (index >= 0) + { + node.Children[index].Points.Add(point); + } + } + node.Points.Clear(); + + // 插入新点 + int newIndex = GetChildIndex(node, x, y); + if (newIndex >= 0) + { + node.Children[newIndex].Points.Add(new QuadNode(x, y, value)); + } + } + } + + private int GetChildIndex(QuadTreeNode node, double x, double y) + { + bool inWest = x < node.MidX; + bool inNorth = y >= node.MidY; + + if (inWest && inNorth) return 0; // NW + if (!inWest && inNorth) return 1; // NE + if (inWest && !inNorth) return 2; // SW + if (!inWest && !inNorth) return 3; // SE + + return -1; + } + + /// + /// 范围查询 + /// + public List<(double X, double Y, T Value)> Query(double minX, double minY, double maxX, double maxY) + { + var result = new List<(double X, double Y, T Value)>(); + Query(_root, minX, minY, maxX, maxY, result); + return result; + } + + private void Query(QuadTreeNode node, double minX, double minY, double maxX, double maxY, List<(double X, double Y, T Value)> result) + { + if (!node.Intersects(minX, minY, maxX, maxY)) + return; + + if (node.IsDivided) + { + foreach (var child in node.Children) + { + Query(child, minX, minY, maxX, maxY, result); + } + } + else + { + foreach (var point in node.Points) + { + if (point.X >= minX && point.X <= maxX && point.Y >= minY && point.Y <= maxY) + { + result.Add((point.X, point.Y, point.Value)); + } + } + } + } + + /// + /// 查找圆内所有点 + /// + public List<(double X, double Y, T Value)> QueryCircle(double centerX, double centerY, double radius) + { + var result = new List<(double X, double Y, T Value)>(); + QueryCircle(_root, centerX, centerY, radius, result); + return result; + } + + private void QueryCircle(QuadTreeNode node, double centerX, double centerY, double radius, List<(double X, double Y, T Value)> result) + { + if (!node.Intersects(centerX - radius, centerY - radius, centerX + radius, centerY + radius)) + return; + + if (node.IsDivided) + { + foreach (var child in node.Children) + { + QueryCircle(child, centerX, centerY, radius, result); + } + } + else + { + double radiusSquared = radius * radius; + foreach (var point in node.Points) + { + double dx = point.X - centerX; + double dy = point.Y - centerY; + if (dx * dx + dy * dy <= radiusSquared) + { + result.Add((point.X, point.Y, point.Value)); + } + } + } + } + + /// + /// 清空 + /// + public void Clear() + { + _root.Points.Clear(); + _root.Children = null; + _root.IsDivided = false; + _count = 0; + } + } + + /// + /// 网格索引 + /// 将空间划分为网格,快速查找 + /// + public class GridIndex + { + private readonly Dictionary>> _grid; + private readonly double _minX, _minY, _maxX, _maxY; + private readonly double _cellWidth, _cellHeight; + private readonly int _cellCountX, _cellCountY; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 创建网格索引 + /// + public GridIndex(double minX, double minY, double maxX, double maxY, int cellCountX, int cellCountY) + { + if (maxX <= minX || maxY <= minY) + throw new ArgumentException("Invalid bounds"); + if (cellCountX <= 0 || cellCountY <= 0) + throw new ArgumentOutOfRangeException("Cell counts must be positive"); + + _minX = minX; + _minY = minY; + _maxX = maxX; + _maxY = maxY; + _cellCountX = cellCountX; + _cellCountY = cellCountY; + _cellWidth = (maxX - minX) / cellCountX; + _cellHeight = (maxY - minY) / cellCountY; + _grid = new Dictionary>>(); + _count = 0; + } + + /// + /// 插入点 + /// + public bool Insert(double x, double y, T value) + { + if (x < _minX || x > _maxX || y < _minY || y > _maxY) + return false; + + int cellX = GetCellX(x); + int cellY = GetCellY(y); + + if (!_grid.TryGetValue(cellX, out var column)) + { + column = new Dictionary>(); + _grid[cellX] = column; + } + + if (!column.TryGetValue(cellY, out var cell)) + { + cell = new List<(double X, double Y, T Value)>(); + column[cellY] = cell; + } + + cell.Add((x, y, value)); + _count++; + return true; + } + + /// + /// 范围查询 + /// + public List<(double X, double Y, T Value)> Query(double minX, double minY, double maxX, double maxY) + { + var result = new List<(double X, double Y, T Value)>(); + + int startCellX = Math.Max(0, GetCellX(minX)); + int startCellY = Math.Max(0, GetCellY(minY)); + int endCellX = Math.Min(_cellCountX - 1, GetCellX(maxX)); + int endCellY = Math.Min(_cellCountY - 1, GetCellY(maxY)); + + for (int x = startCellX; x <= endCellX; x++) + { + if (!_grid.TryGetValue(x, out var column)) + continue; + + for (int y = startCellY; y <= endCellY; y++) + { + if (!column.TryGetValue(y, out var cell)) + continue; + + foreach (var point in cell) + { + if (point.X >= minX && point.X <= maxX && point.Y >= minY && point.Y <= maxY) + { + result.Add(point); + } + } + } + } + + return result; + } + + /// + /// 获取单元格内的所有点 + /// + public List<(double X, double Y, T Value)> GetCell(int cellX, int cellY) + { + if (_grid.TryGetValue(cellX, out var column) && column.TryGetValue(cellY, out var cell)) + { + return new List<(double X, double Y, T Value)>(cell); + } + return new List<(double X, double Y, T Value)>(); + } + + /// + /// 清空 + /// + public void Clear() + { + _grid.Clear(); + _count = 0; + } + + private int GetCellX(double x) + { + return Math.Min(_cellCountX - 1, Math.Max(0, (int)((x - _minX) / _cellWidth))); + } + + private int GetCellY(double y) + { + return Math.Min(_cellCountY - 1, Math.Max(0, (int)((y - _minY) / _cellHeight))); + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs b/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs new file mode 100644 index 0000000..da7d6e5 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/SpecialCollectionsUtil.cs @@ -0,0 +1,1215 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 位集合工具类 + /// + public static class BitSetUtil + { + /// + /// 创建位集合 + /// + public static BitSet Create(int capacity) + { + return new BitSet(capacity); + } + + /// + /// 从数组创建位集合 + /// + public static BitSet FromArray(bool[] values) + { + var bitSet = new BitSet(values.Length); + for (int i = 0; i < values.Length; i++) + { + if (values[i]) + bitSet.Set(i); + } + return bitSet; + } + } + + /// + /// 位集合实现 + /// + public class BitSet + { + private readonly int[] _data; + private readonly int _capacity; + + /// + /// 位数 + /// + public int Capacity => _capacity; + + /// + /// 设置为 1 的位数 + /// + public int Cardinality + { + get + { + int count = 0; + for (int i = 0; i < _data.Length; i++) + { + count += PopCount(_data[i]); + } + return count; + } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Cardinality == 0; + + /// + /// 访问指定位 + /// + public bool this[int index] + { + get => Get(index); + set + { + if (value) + Set(index); + else + Clear(index); + } + } + + /// + /// 创建位集合 + /// + public BitSet(int capacity) + { + if (capacity < 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _data = new int[(capacity + 31) / 32]; + } + + /// + /// 设置指定位为 1 + /// + public void Set(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] |= 1 << (index % 32); + } + + /// + /// 设置指定位为 0 + /// + public void Clear(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] &= ~(1 << (index % 32)); + } + + /// + /// 翻转指定位 + /// + public void Flip(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + _data[index / 32] ^= 1 << (index % 32); + } + + /// + /// 获取指定位的值 + /// + public bool Get(int index) + { + if (index < 0 || index >= _capacity) + throw new ArgumentOutOfRangeException(nameof(index)); + + return (_data[index / 32] & (1 << (index % 32))) != 0; + } + + /// + /// 设置所有位为 1 + /// + public void SetAll() + { + for (int i = 0; i < _data.Length; i++) + { + _data[i] = -1; + } + ClearExtraBits(); + } + + /// + /// 设置所有位为 0 + /// + public void ClearAll() + { + Array.Clear(_data, 0, _data.Length); + } + + /// + /// 与操作 + /// + public void And(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] &= other._data[i]; + } + } + + /// + /// 或操作 + /// + public void Or(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] |= other._data[i]; + } + } + + /// + /// 异或操作 + /// + public void Xor(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] ^= other._data[i]; + } + } + + /// + /// 与非操作 + /// + public void AndNot(BitSet other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + for (int i = 0; i < Math.Min(_data.Length, other._data.Length); i++) + { + _data[i] &= ~other._data[i]; + } + } + + /// + /// 获取下一个设置为 1 的位 + /// + public int NextSetBit(int fromIndex) + { + if (fromIndex < 0) + throw new ArgumentOutOfRangeException(nameof(fromIndex)); + + int wordIndex = fromIndex / 32; + if (wordIndex >= _data.Length) + return -1; + + int word = _data[wordIndex] & (~0 << (fromIndex % 32)); + + while (true) + { + if (word != 0) + { + int result = wordIndex * 32 + TrailingZeroCount(word); + return result < _capacity ? result : -1; + } + + wordIndex++; + if (wordIndex >= _data.Length) + return -1; + + word = _data[wordIndex]; + } + } + + /// + /// 获取下一个设置为 0 的位 + /// + public int NextClearBit(int fromIndex) + { + if (fromIndex < 0) + throw new ArgumentOutOfRangeException(nameof(fromIndex)); + + int wordIndex = fromIndex / 32; + if (wordIndex >= _data.Length) + return -1; + + int word = ~_data[wordIndex] & (~0 << (fromIndex % 32)); + + while (true) + { + if (word != 0) + { + int result = wordIndex * 32 + TrailingZeroCount(word); + return result < _capacity ? result : -1; + } + + wordIndex++; + if (wordIndex >= _data.Length) + return fromIndex < _capacity ? fromIndex : -1; + + word = ~_data[wordIndex]; + } + } + + /// + /// 克隆 + /// + public BitSet Clone() + { + var clone = new BitSet(_capacity); + Array.Copy(_data, clone._data, _data.Length); + return clone; + } + + /// + /// 转换为布尔数组 + /// + public bool[] ToArray() + { + var result = new bool[_capacity]; + for (int i = 0; i < _capacity; i++) + { + result[i] = Get(i); + } + return result; + } + + private void ClearExtraBits() + { + int extraBits = _data.Length * 32 - _capacity; + if (extraBits > 0) + { + _data[_data.Length - 1] &= ~(-1 << (32 - extraBits)); + } + } + + private static int PopCount(int x) + { + x = x - ((x >> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >> 2) & 0x33333333); + x = (x + (x >> 4)) & 0x0F0F0F0F; + return (x * 0x01010101) >> 24; + } + + private static int TrailingZeroCount(int x) + { + if (x == 0) + return 32; + + int count = 0; + while ((x & 1) == 0) + { + count++; + x >>= 1; + } + return count; + } + } + + /// + /// 稀疏数组工具类 + /// + public static class SparseArrayUtil + { + /// + /// 创建稀疏数组 + /// + public static SparseArray Create(int capacity = 16) + { + return new SparseArray(capacity); + } + } + + /// + /// 稀疏数组实现 + /// 使用字典存储非默认值元素,节省内存 + /// + public class SparseArray + { + private readonly T _defaultValue; + private readonly Dictionary _data; + private int _length; + + /// + /// 逻辑长度 + /// + public int Length => _length; + + /// + /// 非默认值元素数量 + /// + public int NonDefaultCount => _data.Count; + + /// + /// 访问元素 + /// + public T this[int index] + { + get + { + if (index < 0 || index >= _length) + throw new ArgumentOutOfRangeException(nameof(index)); + + return _data.TryGetValue(index, out var value) ? value : _defaultValue; + } + set + { + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + if (index >= _length) + _length = index + 1; + + if (EqualityComparer.Default.Equals(value, _defaultValue)) + { + _data.Remove(index); + } + else + { + _data[index] = value; + } + } + } + + /// + /// 创建稀疏数组 + /// + public SparseArray(int capacity = 16) : this(default, capacity) + { + } + + /// + /// 创建稀疏数组(指定默认值) + /// + public SparseArray(T defaultValue, int capacity = 16) + { + _defaultValue = defaultValue; + _data = new Dictionary(capacity); + _length = 0; + } + + /// + /// 设置长度 + /// + public void SetLength(int length) + { + if (length < 0) + throw new ArgumentOutOfRangeException(nameof(length)); + + _length = length; + + // 移除超出长度的元素 + var keysToRemove = _data.Keys.Where(k => k >= length).ToList(); + foreach (var key in keysToRemove) + { + _data.Remove(key); + } + } + + /// + /// 获取所有非默认值索引 + /// + public IEnumerable GetNonDefaultIndices() + { + return _data.Keys; + } + + /// + /// 转换为常规数组 + /// + public T[] ToArray() + { + var result = new T[_length]; + for (int i = 0; i < _length; i++) + { + result[i] = this[i]; + } + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _data.Clear(); + _length = 0; + } + } + + /// + /// 有界队列工具类 + /// + public static class BoundedQueueUtil + { + /// + /// 创建有界队列 + /// + public static BoundedQueue Create(int capacity) + { + return new BoundedQueue(capacity); + } + } + + /// + /// 有界队列实现 + /// 当队列满时,可选择阻塞、丢弃新元素或丢弃旧元素 + /// + public class BoundedQueue + { + private readonly Queue _queue; + private readonly int _capacity; + private readonly object _lock = new object(); + + /// + /// 容量 + /// + public int Capacity => _capacity; + + /// + /// 当前数量 + /// + public int Count + { + get + { + lock (_lock) + { + return _queue.Count; + } + } + } + + /// + /// 是否已满 + /// + public bool IsFull => Count >= _capacity; + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 溢出策略 + /// + public OverflowPolicy Policy { get; set; } + + /// + /// 创建有界队列 + /// + public BoundedQueue(int capacity, OverflowPolicy policy = OverflowPolicy.Block) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity)); + + _capacity = capacity; + _queue = new Queue(capacity); + Policy = policy; + } + + /// + /// 尝试入队 + /// + public bool TryEnqueue(T item) + { + lock (_lock) + { + switch (Policy) + { + case OverflowPolicy.Block: + if (_queue.Count >= _capacity) + return false; + break; + + case OverflowPolicy.DropNewest: + if (_queue.Count >= _capacity) + return false; + break; + + case OverflowPolicy.DropOldest: + if (_queue.Count >= _capacity) + _queue.Dequeue(); + break; + } + + _queue.Enqueue(item); + return true; + } + } + + /// + /// 尝试出队 + /// + public bool TryDequeue(out T item) + { + lock (_lock) + { + if (_queue.Count == 0) + { + item = default; + return false; + } + + item = _queue.Dequeue(); + return true; + } + } + + /// + /// 尝试查看队首 + /// + public bool TryPeek(out T item) + { + lock (_lock) + { + if (_queue.Count == 0) + { + item = default; + return false; + } + + item = _queue.Peek(); + return true; + } + } + + /// + /// 清空 + /// + public void Clear() + { + lock (_lock) + { + _queue.Clear(); + } + } + } + + /// + /// 溢出策略 + /// + public enum OverflowPolicy + { + /// + /// 阻塞(拒绝新元素) + /// + Block, + + /// + /// 丢弃最新元素 + /// + DropNewest, + + /// + /// 丢弃最旧元素 + /// + DropOldest + } + + /// + /// 延迟队列工具类 + /// + public static class DelayedQueueUtil + { + /// + /// 创建延迟队列 + /// + public static DelayedQueue Create() + { + return new DelayedQueue(); + } + } + + /// + /// 延迟队列实现 + /// 元素在指定时间后才能被取出 + /// + public class DelayedQueue + { + private readonly List _items; + private readonly object _lock = new object(); + + /// + /// 当前元素数量 + /// + public int Count + { + get + { + lock (_lock) + { + return _items.Count; + } + } + } + + /// + /// 可用元素数量 + /// + public int AvailableCount + { + get + { + lock (_lock) + { + return _items.Count(i => i.IsAvailable); + } + } + } + + /// + /// 创建延迟队列 + /// + public DelayedQueue() + { + _items = new List(); + } + + /// + /// 入队(延迟指定时间) + /// + public void Enqueue(T item, TimeSpan delay) + { + lock (_lock) + { + var availableAt = DateTime.UtcNow.Add(delay); + _items.Add(new DelayedItem(item, availableAt)); + } + } + + /// + /// 入队(指定可用时间) + /// + public void EnqueueAt(T item, DateTime availableAt) + { + lock (_lock) + { + _items.Add(new DelayedItem(item, availableAt)); + } + } + + /// + /// 尝试出队(仅返回已到期的元素) + /// + public bool TryDequeue(out T item) + { + lock (_lock) + { + var now = DateTime.UtcNow; + var index = _items.FindIndex(i => i.AvailableAt <= now); + + if (index >= 0) + { + item = _items[index].Value; + _items.RemoveAt(index); + return true; + } + + item = default; + return false; + } + } + + /// + /// 尝试查看队首 + /// + public bool TryPeek(out T item, out TimeSpan remainingDelay) + { + lock (_lock) + { + CleanupExpired(); + + if (_items.Count == 0) + { + item = default; + remainingDelay = TimeSpan.Zero; + return false; + } + + var first = _items.OrderBy(i => i.AvailableAt).First(); + item = first.Value; + remainingDelay = first.AvailableAt - DateTime.UtcNow; + + if (remainingDelay < TimeSpan.Zero) + remainingDelay = TimeSpan.Zero; + + return true; + } + } + + /// + /// 清空 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + } + } + + private void CleanupExpired() + { + var now = DateTime.UtcNow; + _items.RemoveAll(i => i.AvailableAt <= now); + } + + private class DelayedItem + { + public T Value { get; } + public DateTime AvailableAt { get; } + public bool IsAvailable => DateTime.UtcNow >= AvailableAt; + + public DelayedItem(T value, DateTime availableAt) + { + Value = value; + AvailableAt = availableAt; + } + } + } + + /// + /// 区间树工具类 + /// + public static class IntervalTreeUtil + { + /// + /// 创建区间树 + /// + public static IntervalTree Create() where T : IComparable + { + return new IntervalTree(); + } + } + + /// + /// 区间树实现 + /// 高效查询与指定区间重叠的所有区间 + /// + public class IntervalTree where T : IComparable + { + private IntervalNode _root; + + /// + /// 区间数量 + /// + public int Count { get; private set; } + + /// + /// 创建区间树 + /// + public IntervalTree() + { + Count = 0; + } + + /// + /// 添加区间 + /// + public void Add(T start, T end, object data = null) + { + if (start.CompareTo(end) > 0) + throw new ArgumentException("Start must be less than or equal to end"); + + var interval = new Interval(start, end, data); + _root = Insert(_root, interval); + Count++; + } + + /// + /// 查询与指定点重叠的区间 + /// + public List Query(T point) + { + var result = new List(); + Query(_root, point, result); + return result; + } + + /// + /// 查询与指定区间重叠的区间 + /// + public List Query(T start, T end) + { + var result = new List(); + Query(_root, start, end, result); + return result; + } + + /// + /// 清空 + /// + public void Clear() + { + _root = null; + Count = 0; + } + + private IntervalNode Insert(IntervalNode node, Interval interval) + { + if (node == null) + { + return new IntervalNode(interval); + } + + int cmp = interval.Start.CompareTo(node.Interval.Start); + if (cmp < 0) + { + node.Left = Insert(node.Left, interval); + } + else + { + node.Right = Insert(node.Right, interval); + } + + // 更新最大值 + if (interval.End.CompareTo(node.MaxEnd) > 0) + { + node.MaxEnd = interval.End; + } + + return node; + } + + private void Query(IntervalNode node, T point, List result) + { + if (node == null) + return; + + // 如果点小于区间起点,且大于最大终点,则没有重叠 + if (point.CompareTo(node.Interval.Start) < 0 && + point.CompareTo(node.MaxEnd) > 0) + { + return; + } + + // 检查左子树 + if (node.Left != null && point.CompareTo(node.Left.MaxEnd) <= 0) + { + Query(node.Left, point, result); + } + + // 检查当前节点 + if (node.Interval.Contains(point)) + { + result.Add(node.Interval); + } + + // 检查右子树(如果点 >= 当前区间起点) + if (point.CompareTo(node.Interval.Start) >= 0) + { + Query(node.Right, point, result); + } + } + + private void Query(IntervalNode node, T start, T end, List result) + { + if (node == null) + return; + + // 如果查询区间完全在最大终点之后,无需继续 + if (start.CompareTo(node.MaxEnd) > 0) + return; + + // 检查左子树 + Query(node.Left, start, end, result); + + // 检查当前节点 + if (node.Interval.Overlaps(start, end)) + { + result.Add(node.Interval); + } + + // 如果查询区间完全在当前区间之前,无需检查右子树 + if (end.CompareTo(node.Interval.Start) < 0) + return; + + // 检查右子树 + Query(node.Right, start, end, result); + } + + private class IntervalNode + { + public Interval Interval { get; } + public IntervalNode Left { get; set; } + public IntervalNode Right { get; set; } + public T MaxEnd { get; set; } + + public IntervalNode(Interval interval) + { + Interval = interval; + MaxEnd = interval.End; + } + } + + /// + /// 区间 + /// + public class Interval + { + /// + /// 起点 + /// + public T Start { get; } + + /// + /// 终点 + /// + public T End { get; } + + /// + /// 关联数据 + /// + public object Data { get; } + + /// + /// 创建区间 + /// + public Interval(T start, T end, object data = null) + { + Start = start; + End = end; + Data = data; + } + + /// + /// 是否包含指定点 + /// + public bool Contains(T point) + { + return Start.CompareTo(point) <= 0 && End.CompareTo(point) >= 0; + } + + /// + /// 是否与指定区间重叠 + /// + public bool Overlaps(T start, T end) + { + return Start.CompareTo(end) <= 0 && End.CompareTo(start) >= 0; + } + + /// + /// 是否与指定区间重叠 + /// + public bool Overlaps(Interval other) + { + return Overlaps(other.Start, other.End); + } + + public override string ToString() + { + return $"[{Start}, {End}]"; + } + } + } + + /// + /// 有序多重集工具类 + /// + public static class SortedMultiSetUtil + { + /// + /// 创建有序多重集 + /// + public static SortedMultiSet Create() where T : IComparable + { + return new SortedMultiSet(); + } + } + + /// + /// 有序多重集实现 + /// 允许重复元素,保持排序 + /// + public class SortedMultiSet : IEnumerable where T : IComparable + { + private readonly SortedDictionary _dict; + private int _count; + + /// + /// 元素总数 + /// + public int Count => _count; + + /// + /// 不同元素数量 + /// + public int UniqueCount => _dict.Count; + + /// + /// 最小值 + /// + public T Min => _dict.Count > 0 ? _dict.First().Key : throw new InvalidOperationException("Set is empty"); + + /// + /// 最大值 + /// + public T Max => _dict.Count > 0 ? _dict.Last().Key : throw new InvalidOperationException("Set is empty"); + + /// + /// 创建有序多重集 + /// + public SortedMultiSet() + { + _dict = new SortedDictionary(); + _count = 0; + } + + /// + /// 添加元素 + /// + public void Add(T item) + { + if (_dict.ContainsKey(item)) + { + _dict[item]++; + } + else + { + _dict[item] = 1; + } + _count++; + } + + /// + /// 移除一个元素 + /// + public bool Remove(T item) + { + if (!_dict.TryGetValue(item, out var count)) + return false; + + if (count == 1) + { + _dict.Remove(item); + } + else + { + _dict[item] = count - 1; + } + _count--; + return true; + } + + /// + /// 移除所有指定元素 + /// + public int RemoveAll(T item) + { + if (!_dict.TryGetValue(item, out var count)) + return 0; + + _dict.Remove(item); + _count -= count; + return count; + } + + /// + /// 获取元素数量 + /// + public int GetCount(T item) + { + return _dict.TryGetValue(item, out var count) ? count : 0; + } + + /// + /// 是否包含元素 + /// + public bool Contains(T item) + { + return _dict.ContainsKey(item); + } + + /// + /// 清空 + /// + public void Clear() + { + _dict.Clear(); + _count = 0; + } + + /// + /// 获取小于指定值的元素数量 + /// + public int CountLessThan(T value) + { + int count = 0; + foreach (var kvp in _dict) + { + if (kvp.Key.CompareTo(value) >= 0) + break; + count += kvp.Value; + } + return count; + } + + /// + /// 获取大于指定值的元素数量 + /// + public int CountGreaterThan(T value) + { + int count = 0; + foreach (var kvp in _dict.Reverse()) + { + if (kvp.Key.CompareTo(value) <= 0) + break; + count += kvp.Value; + } + return count; + } + + /// + /// 获取指定范围内的元素数量 + /// + public int CountInRange(T min, T max) + { + int count = 0; + foreach (var kvp in _dict) + { + if (kvp.Key.CompareTo(max) > 0) + break; + if (kvp.Key.CompareTo(min) >= 0) + count += kvp.Value; + } + return count; + } + + public IEnumerator GetEnumerator() + { + foreach (var kvp in _dict) + { + for (int i = 0; i < kvp.Value; i++) + { + yield return kvp.Key; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/EasyTool.Core/CollectionsCategory/StackExtension.cs b/EasyTool.Core/CollectionsCategory/StackExtension.cs deleted file mode 100644 index a1477f9..0000000 --- a/EasyTool.Core/CollectionsCategory/StackExtension.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool.CollectionsCategory -{ - public static class StackExtension - { - /// - /// 从堆栈中移除指定元素的第一个匹配项。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要移除的元素 - /// 如果已成功移除元素,则为 true;否则为 false。 - public static bool Remove(this Stack stack, T item)=> StackUtil.Remove(stack, item); - } -} diff --git a/EasyTool.Core/CollectionsCategory/StackUtil.cs b/EasyTool.Core/CollectionsCategory/StackUtil.cs index 232e5c8..c7955bf 100644 --- a/EasyTool.Core/CollectionsCategory/StackUtil.cs +++ b/EasyTool.Core/CollectionsCategory/StackUtil.cs @@ -1,62 +1,15 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.CollectionsCategory { /// - /// 堆栈工具类 + /// 栈工具类 /// - public class StackUtil + public static class StackUtil { - /// - /// 将指定元素推入堆栈的顶部。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要添加的元素 - public static void Push(Stack stack, T item) - { - stack.Push(item); - } - - /// - /// 从堆栈的顶部移除并返回对象。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 堆栈顶部的元素 - /// 堆栈为空时引发异常 - public static T Pop(Stack stack) - { - return stack.Pop(); - } - - /// - /// 返回位于堆栈顶部的对象但不将其移除。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 堆栈顶部的元素 - /// 堆栈为空时引发异常 - public static T Peek(Stack stack) - { - return stack.Peek(); - } - - /// - /// 确定堆栈是否包含指定元素。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要查找的元素 - /// 如果堆栈包含指定元素,则为 true;否则为 false。 - public static bool Contains(Stack stack, T item) - { - return stack.Contains(item); - } - /// /// 从堆栈中移除指定元素的第一个匹配项。 /// @@ -68,43 +21,15 @@ public static bool Remove(Stack stack, T item) { if (stack.Contains(item)) { - stack = new Stack(stack.Where(x => !x.Equals(item)).Reverse()); + var newStack = new Stack(stack.Where(x => !Equals(x, item)).Reverse()); + stack.Clear(); + foreach (var element in newStack) + { + stack.Push(element); + } return true; } return false; } - - /// - /// 将堆栈中的所有元素复制到新数组中。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 包含堆栈中所有元素的新数组 - public static T[] ToArray(Stack stack) - { - return stack.ToArray(); - } - - /// - /// 将堆栈中的所有元素复制到新数组中,从指定的索引开始。 - /// - /// 堆栈元素类型 - /// 堆栈 - /// 要复制到的目标数组 - /// 目标数组的起始索引 - public static void CopyTo(Stack stack, T[] array, int arrayIndex) - { - stack.CopyTo(array, arrayIndex); - } - - /// - /// 从堆栈中移除所有元素。 - /// - /// 堆栈元素类型 - /// 堆栈 - public static void Clear(Stack stack) - { - stack.Clear(); - } } } diff --git a/EasyTool.Core/CollectionsCategory/TDigestUtil.cs b/EasyTool.Core/CollectionsCategory/TDigestUtil.cs new file mode 100644 index 0000000..e0bba80 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TDigestUtil.cs @@ -0,0 +1,352 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// T-Digest 工具类 + /// 用于流式数据的分位数估计 + /// 特别适合大数据集和分布式环境 + /// + public static class TDigestUtil + { + /// + /// 创建 T-Digest + /// + /// 压缩参数,越大越精确但占用更多内存 + public static TDigest Create(double compression = 100) + { + return new TDigest(compression); + } + } + + /// + /// T-Digest 实现 + /// + public class TDigest + { + private readonly double _compression; + private readonly List _centroids; + private long _count; + private double _min; + private double _max; + + private class Centroid + { + public double Mean { get; set; } + public double Weight { get; set; } + + public Centroid(double mean, double weight) + { + Mean = mean; + Weight = weight; + } + } + + /// + /// 压缩参数 + /// + public double Compression => _compression; + + /// + /// 已添加的数据点数量 + /// + public long Count => _count; + + /// + /// 最小值 + /// + public double Min => _min; + + /// + /// 最大值 + /// + public double Max => _max; + + /// + /// 质心数量 + /// + public int CentroidCount => _centroids.Count; + + /// + /// 创建 T-Digest + /// + public TDigest(double compression = 100) + { + if (compression <= 0) + throw new ArgumentOutOfRangeException(nameof(compression)); + + _compression = compression; + _centroids = new List(); + _count = 0; + _min = double.MaxValue; + _max = double.MinValue; + } + + /// + /// 添加数据点 + /// + public void Add(double value) + { + Add(value, 1); + } + + /// + /// 添加带权重的数据点 + /// + public void Add(double value, double weight) + { + if (double.IsNaN(value) || double.IsInfinity(value)) + throw new ArgumentException("Value cannot be NaN or infinity"); + + _count++; + if (value < _min) _min = value; + if (value > _max) _max = value; + + // 找到最近的质心 + int nearestIndex = -1; + double nearestDistance = double.MaxValue; + + for (int i = 0; i < _centroids.Count; i++) + { + double dist = Math.Abs(_centroids[i].Mean - value); + if (dist < nearestDistance) + { + nearestDistance = dist; + nearestIndex = i; + } + } + + // 如果没有质心或距离太远,创建新质心 + if (nearestIndex < 0 || _centroids.Count == 0) + { + _centroids.Add(new Centroid(value, weight)); + } + else + { + // 检查是否可以合并 + double q = GetQuantile(_centroids[nearestIndex].Mean); + double k = 4 * _count * q * (1 - q) / _compression; + double maxWeight = Math.Max(1, k); + + if (_centroids[nearestIndex].Weight + weight <= maxWeight) + { + // 合并到最近的质心 + var centroid = _centroids[nearestIndex]; + double newWeight = centroid.Weight + weight; + centroid.Mean = (centroid.Mean * centroid.Weight + value * weight) / newWeight; + centroid.Weight = newWeight; + } + else + { + // 创建新质心 + _centroids.Add(new Centroid(value, weight)); + } + } + + // 定期压缩 + if (_centroids.Count > 3 * _compression) + { + Compress(); + } + } + + /// + /// 批量添加数据 + /// + public void AddRange(IEnumerable values) + { + foreach (var value in values) + { + Add(value); + } + } + + /// + /// 压缩质心 + /// + public void Compress() + { + if (_centroids.Count <= 1) return; + + // 按均值排序 + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + var newCentroids = new List(); + double cumulativeWeight = 0; + + foreach (var centroid in _centroids) + { + if (newCentroids.Count == 0) + { + newCentroids.Add(new Centroid(centroid.Mean, centroid.Weight)); + cumulativeWeight = centroid.Weight; + continue; + } + + double q = cumulativeWeight / _count; + double k = 4 * _count * q * (1 - q) / _compression; + double maxWeight = Math.Max(1, k); + + var last = newCentroids[newCentroids.Count - 1]; + if (last.Weight + centroid.Weight <= maxWeight) + { + // 合并 + double newWeight = last.Weight + centroid.Weight; + last.Mean = (last.Mean * last.Weight + centroid.Mean * centroid.Weight) / newWeight; + last.Weight = newWeight; + } + else + { + newCentroids.Add(new Centroid(centroid.Mean, centroid.Weight)); + } + + cumulativeWeight += centroid.Weight; + } + + _centroids.Clear(); + _centroids.AddRange(newCentroids); + } + + /// + /// 估计分位数 + /// + /// 分位数(0-1) + public double Quantile(double q) + { + if (_count == 0) + throw new InvalidOperationException("No data has been added"); + if (q < 0 || q > 1) + throw new ArgumentOutOfRangeException(nameof(q), "Quantile must be between 0 and 1"); + + if (_centroids.Count == 0) return 0; + if (_centroids.Count == 1) return _centroids[0].Mean; + + // 确保已压缩 + Compress(); + + double targetWeight = q * _count; + + if (q <= 0) return _min; + if (q >= 1) return _max; + + // 按均值排序 + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + double cumulativeWeight = 0; + + for (int i = 0; i < _centroids.Count; i++) + { + double nextWeight = cumulativeWeight + _centroids[i].Weight; + + if (nextWeight > targetWeight) + { + // 在当前质心范围内 + double prevWeight = cumulativeWeight; + double deltaWeight = targetWeight - prevWeight; + double fraction = deltaWeight / _centroids[i].Weight; + + if (i == 0) + { + return _min + fraction * (_centroids[i].Mean - _min); + } + else + { + double prevMean = _centroids[i - 1].Mean; + return prevMean + fraction * (_centroids[i].Mean - prevMean); + } + } + + cumulativeWeight = nextWeight; + } + + return _max; + } + + /// + /// 获取值对应的分位数位置 + /// + public double GetQuantile(double value) + { + if (_count == 0) + return 0; + + Compress(); + _centroids.Sort((a, b) => a.Mean.CompareTo(b.Mean)); + + if (value <= _min) return 0; + if (value >= _max) return 1; + + double cumulativeWeight = 0; + + for (int i = 0; i < _centroids.Count; i++) + { + if (_centroids[i].Mean >= value) + { + if (i == 0) + { + double fraction = (value - _min) / (_centroids[i].Mean - _min); + return (cumulativeWeight + fraction * _centroids[i].Weight / 2) / _count; + } + else + { + double prevMean = _centroids[i - 1].Mean; + double fraction = (value - prevMean) / (_centroids[i].Mean - prevMean); + double prevWeight = cumulativeWeight - _centroids[i - 1].Weight / 2; + return (prevWeight + fraction * _centroids[i].Weight) / _count; + } + } + + cumulativeWeight += _centroids[i].Weight; + } + + return 1; + } + + /// + /// 估计中位数 + /// + public double Median() => Quantile(0.5); + + /// + /// 估计第25百分位数 + /// + public double Q1() => Quantile(0.25); + + /// + /// 估计第75百分位数 + /// + public double Q3() => Quantile(0.75); + + /// + /// 估计四分位距 + /// + public double IQR() => Q3() - Q1(); + + /// + /// 合并另一个 T-Digest + /// + public void Merge(TDigest other) + { + if (other == null) + throw new ArgumentNullException(nameof(other)); + + foreach (var centroid in other._centroids) + { + Add(centroid.Mean, centroid.Weight); + } + } + + /// + /// 清空 + /// + public void Clear() + { + _centroids.Clear(); + _count = 0; + _min = double.MaxValue; + _max = double.MinValue; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TopKUtil.cs b/EasyTool.Core/CollectionsCategory/TopKUtil.cs new file mode 100644 index 0000000..a883305 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TopKUtil.cs @@ -0,0 +1,225 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Top-K 选择工具类 + /// 高效地从集合中选出前K个最大/最小元素 + /// 使用快速选择算法,平均时间复杂度 O(n) + /// + public static class TopKUtil + { + /// + /// 获取前K个最大元素 + /// + /// 元素类型 + /// 源集合 + /// 数量 + /// 前K个最大元素(降序) + public static IEnumerable TopK(IEnumerable source, int k) where T : IComparable + { + return TopK(source, k, Comparer.Default, false); + } + + /// + /// 获取前K个最小元素 + /// + public static IEnumerable BottomK(IEnumerable source, int k) where T : IComparable + { + return TopK(source, k, Comparer.Default, true); + } + + /// + /// 获取前K个元素(使用比较器) + /// + /// 元素类型 + /// 源集合 + /// 数量 + /// 比较器 + /// 是否升序(true=最小K个,false=最大K个) + /// 前K个元素 + public static IEnumerable TopK(IEnumerable source, int k, IComparer comparer, bool ascending) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (k <= 0) + return Enumerable.Empty(); + + var list = source.ToList(); + if (k >= list.Count) + return ascending ? list.OrderBy(x => x, comparer) : list.OrderByDescending(x => x, comparer); + + // 使用快速选择算法 + int actualK = Math.Min(k, list.Count); + + if (ascending) + { + QuickSelect(list, 0, list.Count - 1, actualK - 1, comparer); + var result = list.Take(actualK).ToList(); + result.Sort(comparer); + return result; + } + else + { + // 对于最大K个,我们找第(n-k)小的元素 + int targetIndex = list.Count - actualK; + QuickSelect(list, 0, list.Count - 1, targetIndex, comparer); + var result = list.Skip(targetIndex).ToList(); + result.Sort(comparer); + result.Reverse(); + return result; + } + } + + /// + /// 获取前K个最大元素(使用选择器) + /// + public static IEnumerable TopKBy(IEnumerable source, int k, Func keySelector) + where TKey : IComparable + { + return TopKBy(source, k, keySelector, Comparer.Default, false); + } + + /// + /// 获取前K个最小元素(使用选择器) + /// + public static IEnumerable BottomKBy(IEnumerable source, int k, Func keySelector) + where TKey : IComparable + { + return TopKBy(source, k, keySelector, Comparer.Default, true); + } + + /// + /// 获取前K个元素(使用选择器和比较器) + /// + public static IEnumerable TopKBy(IEnumerable source, int k, Func keySelector, + IComparer comparer, bool ascending) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (keySelector == null) + throw new ArgumentNullException(nameof(keySelector)); + + var list = source.ToList(); + if (k <= 0 || list.Count == 0) + return Enumerable.Empty(); + + // 带索引的快速选择 + var indexed = list.Select((item, index) => new { Item = item, Key = keySelector(item), Index = index }).ToList(); + + if (ascending) + { + indexed = indexed.OrderBy(x => x.Key, comparer).Take(k).ToList(); + } + else + { + indexed = indexed.OrderByDescending(x => x.Key, comparer).Take(k).ToList(); + } + + return indexed.Select(x => x.Item); + } + + /// + /// 使用堆获取前K个元素(适用于大数据流) + /// + public static IEnumerable TopKUsingHeap(IEnumerable source, int k) where T : IComparable + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (k <= 0) + return Enumerable.Empty(); + +#if NETSTANDARD2_1 + var minHeap = new PriorityQueue(Comparer.Default); + + foreach (var item in source) + { + if (minHeap.Count < k) + { + minHeap.Enqueue(item, item); + } + else if (item.CompareTo(minHeap.Peek()) > 0) + { + minHeap.Dequeue(); + minHeap.Enqueue(item, item); + } + } + + var result = new List(); + while (minHeap.Count > 0) + { + result.Add(minHeap.Dequeue()); + } +#else + var minHeap = new System.Collections.Generic.PriorityQueue(); + + foreach (var item in source) + { + if (minHeap.Count < k) + { + minHeap.Enqueue(item, item); + } + else if (item.CompareTo(minHeap.Peek()) > 0) + { + minHeap.Dequeue(); + minHeap.Enqueue(item, item); + } + } + + var result = new List(); + while (minHeap.Count > 0) + { + result.Add(minHeap.Dequeue()); + } +#endif + + result.Reverse(); + return result; + } + + private static void QuickSelect(List list, int left, int right, int k, IComparer comparer) + { + while (left < right) + { + int pivotIndex = Partition(list, left, right, comparer); + + if (k == pivotIndex) + return; + else if (k < pivotIndex) + right = pivotIndex - 1; + else + left = pivotIndex + 1; + } + } + + private static int Partition(List list, int left, int right, IComparer comparer) + { + T pivot = list[right]; + int i = left; + + for (int j = left; j < right; j++) + { + if (comparer.Compare(list[j], pivot) <= 0) + { + Swap(list, i, j); + i++; + } + } + + Swap(list, i, right); + return i; + } + + private static void Swap(List list, int i, int j) + { + if (i != j) + { + T temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs b/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs new file mode 100644 index 0000000..d2f4fd8 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TreeBuildUtil.cs @@ -0,0 +1,443 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树形结构构建工具类 + /// 用于将扁平列表转换为树形结构 + /// + public static class TreeBuildUtil + { + /// + /// 创建树构建器 + /// + /// 节点类型 + /// 键类型 + /// 树构建器 + public static TreeBuilder CreateBuilder() + where T : class + where TKey : notnull + { + return new TreeBuilder(); + } + + /// + /// 将扁平列表构建为树形结构 + /// + /// 节点类型 + /// 键类型 + /// 扁平列表 + /// ID选择器 + /// 父ID选择器 + /// 子节点设置器 + /// 根节点判断(可选,默认为父ID为null或默认值) + /// 树形结构列表 + public static List Build( + IEnumerable items, + Func idSelector, + Func parentIdSelector, + Action> childrenSetter, + Func? rootPredicate = null) + where T : class + where TKey : notnull + { + if (items == null) + return new List(); + + var itemList = items.ToList(); + var lookup = new Dictionary(); + var childrenLookup = new Dictionary>(); + + // 第一遍:建立ID映射 + foreach (var item in itemList) + { + var id = idSelector(item); + lookup[id] = item; + } + + // 第二遍:建立父子关系 + foreach (var item in itemList) + { + var parentId = parentIdSelector(item); + if (parentId == null || EqualityComparer.Default.Equals(parentId, default!)) + continue; + + if (!childrenLookup.ContainsKey(parentId)) + childrenLookup[parentId] = new List(); + + childrenLookup[parentId].Add(item); + } + + // 设置子节点 + foreach (var kvp in childrenLookup) + { + if (lookup.TryGetValue(kvp.Key, out var parent)) + { + childrenSetter(parent, kvp.Value); + } + } + + // 获取根节点 + if (rootPredicate != null) + { + return itemList.Where(rootPredicate).ToList(); + } + + return itemList.Where(item => + { + var parentId = parentIdSelector(item); + return parentId == null || + EqualityComparer.Default.Equals(parentId, default!) || + !lookup.ContainsKey(parentId); + }).ToList(); + } + + /// + /// 将树形结构扁平化 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 扁平化列表 + public static List Flatten( + IEnumerable roots, + Func?> childrenSelector) + { + var result = new List(); + FlattenInternal(roots, childrenSelector, result); + return result; + } + + private static void FlattenInternal( + IEnumerable items, + Func?> childrenSelector, + List result) + { + foreach (var item in items) + { + result.Add(item); + var children = childrenSelector(item); + if (children != null) + { + FlattenInternal(children, childrenSelector, result); + } + } + } + + /// + /// 遍历树(深度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 遍历操作 + public static void Traverse( + IEnumerable roots, + Func?> childrenSelector, + Action action) + { + TraverseInternal(roots, childrenSelector, action, 0); + } + + private static void TraverseInternal( + IEnumerable items, + Func?> childrenSelector, + Action action, + int level) + { + foreach (var item in items) + { + action(item, level); + var children = childrenSelector(item); + if (children != null) + { + TraverseInternal(children, childrenSelector, action, level + 1); + } + } + } + + /// + /// 遍历树(广度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 遍历操作 + public static void TraverseBreadthFirst( + IEnumerable roots, + Func?> childrenSelector, + Action action) + { + var queue = new Queue<(T Item, int Level)>(); + + foreach (var root in roots) + { + queue.Enqueue((root, 0)); + } + + while (queue.Count > 0) + { + var (item, level) = queue.Dequeue(); + action(item, level); + + var children = childrenSelector(item); + if (children != null) + { + foreach (var child in children) + { + queue.Enqueue((child, level + 1)); + } + } + } + } + + /// + /// 查找节点 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 查找条件 + /// 找到的节点 + public static T? Find( + IEnumerable roots, + Func?> childrenSelector, + Func predicate) + { + foreach (var root in roots) + { + if (predicate(root)) + return root; + + var children = childrenSelector(root); + if (children != null) + { + var found = Find(children, childrenSelector, predicate); + if (found != null) + return found; + } + } + + return default; + } + + /// + /// 查找所有匹配节点 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 查找条件 + /// 找到的节点列表 + public static List FindAll( + IEnumerable roots, + Func?> childrenSelector, + Func predicate) + { + var result = new List(); + FindAllInternal(roots, childrenSelector, predicate, result); + return result; + } + + private static void FindAllInternal( + IEnumerable items, + Func?> childrenSelector, + Func predicate, + List result) + { + foreach (var item in items) + { + if (predicate(item)) + result.Add(item); + + var children = childrenSelector(item); + if (children != null) + { + FindAllInternal(children, childrenSelector, predicate, result); + } + } + } + + /// + /// 获取节点路径 + /// + /// 节点类型 + /// 键类型 + /// 所有节点 + /// 目标节点ID + /// ID选择器 + /// 父ID选择器 + /// 从根到目标的路径 + public static List GetPath( + IEnumerable items, + TKey targetId, + Func idSelector, + Func parentIdSelector) + where TKey : notnull + { + var result = new List(); + var lookup = items.ToDictionary(idSelector); + + if (!lookup.TryGetValue(targetId, out var current)) + return result; + + result.Add(current); + + while (true) + { + var parentId = parentIdSelector(current); + if (parentId == null || EqualityComparer.Default.Equals(parentId, default!)) + break; + + if (!lookup.TryGetValue(parentId, out var parent)) + break; + + result.Insert(0, parent); + current = parent; + } + + return result; + } + + /// + /// 计算树的深度 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 最大深度 + public static int GetDepth( + IEnumerable roots, + Func?> childrenSelector) + { + var maxDepth = 0; + + Traverse(roots, childrenSelector, (_, level) => + { + if (level > maxDepth) + maxDepth = level; + }); + + return maxDepth + 1; + } + + /// + /// 统计节点数量 + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 总节点数 + public static int Count( + IEnumerable roots, + Func?> childrenSelector) + { + var count = 0; + Traverse(roots, childrenSelector, (_, _) => count++); + return count; + } + } + + /// + /// 树构建器 + /// + /// 节点类型 + /// 键类型 + public class TreeBuilder + where T : class + where TKey : notnull + { + private Func? _idSelector; + private Func? _parentIdSelector; + private Action>? _childrenSetter; + private Func? _rootPredicate; + + /// + /// 设置ID选择器 + /// + public TreeBuilder WithId(Func selector) + { + _idSelector = selector; + return this; + } + + /// + /// 设置父ID选择器 + /// + public TreeBuilder WithParentId(Func selector) + { + _parentIdSelector = selector; + return this; + } + + /// + /// 设置子节点设置器 + /// + public TreeBuilder WithChildren(Action> setter) + { + _childrenSetter = setter; + return this; + } + + /// + /// 设置根节点判断条件 + /// + public TreeBuilder WithRootPredicate(Func predicate) + { + _rootPredicate = predicate; + return this; + } + + /// + /// 构建树 + /// + /// 扁平列表 + /// 树形结构 + public List Build(IEnumerable items) + { + if (_idSelector == null) + throw new InvalidOperationException("必须设置ID选择器"); + if (_parentIdSelector == null) + throw new InvalidOperationException("必须设置父ID选择器"); + if (_childrenSetter == null) + throw new InvalidOperationException("必须设置子节点设置器"); + + return TreeBuildUtil.Build(items, _idSelector, _parentIdSelector, _childrenSetter, _rootPredicate); + } + } + + /// + /// 树节点基类 + /// + /// 节点类型 + public class TreeNode where T : TreeNode + { + /// + /// 子节点列表 + /// + public List Children { get; set; } = new(); + + /// + /// 添加子节点 + /// + public void AddChild(T child) + { + Children.Add(child); + } + + /// + /// 移除子节点 + /// + public bool RemoveChild(T child) + { + return Children.Remove(child); + } + + /// + /// 是否为叶子节点 + /// + public bool IsLeaf => Children.Count == 0; + } +} diff --git a/EasyTool.Core/CollectionsCategory/TreeUtil.cs b/EasyTool.Core/CollectionsCategory/TreeUtil.cs new file mode 100644 index 0000000..2003776 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TreeUtil.cs @@ -0,0 +1,571 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 树节点接口 + /// + /// 节点数据类型 + public interface ITreeNode where T : ITreeNode + { + /// + /// 节点ID + /// + string Id { get; } + + /// + /// 父节点ID + /// + string? ParentId { get; } + + /// + /// 子节点列表 + /// + List Children { get; set; } + } + + /// + /// 树节点基类 + /// + public class TreeNodeBase : ITreeNode + { + public string Id { get; set; } = string.Empty; + public string? ParentId { get; set; } + public List Children { get; set; } = new(); + } + + /// + /// 树形结构工具类 + /// 提供树形数据的构建、遍历、搜索等功能 + /// + public static class TreeUtil + { + #region 构建树 + + /// + /// 将扁平列表构建为树形结构 + /// + /// 节点类型 + /// 扁平列表 + /// ID选择器 + /// 父ID选择器 + /// 根节点的父ID值 + /// 树形结构的根节点列表 + public static List BuildTree(IEnumerable flatList, Func idSelector, Func parentIdSelector, string? rootParentId = null) + { + if (flatList == null) + return new List(); + + var lookup = flatList.ToLookup(parentIdSelector); + var roots = lookup[rootParentId].ToList(); + + void AddChildren(T parent) + { + var parentId = idSelector(parent); + var children = lookup[parentId]; + var childrenProperty = typeof(T).GetProperty("Children"); + + if (childrenProperty != null) + { + var childrenList = childrenProperty.GetValue(parent); + if (childrenList == null) + { + childrenList = new List(); + childrenProperty.SetValue(parent, childrenList); + } + + var addMethod = childrenList.GetType().GetMethod("AddRange"); + addMethod?.Invoke(childrenList, new object[] { children }); + + foreach (var child in children) + { + AddChildren(child); + } + } + } + + foreach (var root in roots) + { + AddChildren(root); + } + + return roots; + } + + /// + /// 将扁平列表构建为树形结构(使用 ITreeNode 接口) + /// + /// 节点类型 + /// 扁平列表 + /// 根节点的父ID值 + /// 树形结构的根节点列表 + public static List BuildTree(IEnumerable flatList, string? rootParentId = null) where T : ITreeNode + { + if (flatList == null) + return new List(); + + var lookup = flatList.ToLookup(x => x.ParentId); + var roots = lookup[rootParentId].ToList(); + + void AddChildren(T parent) + { + var children = lookup[parent.Id].ToList(); + parent.Children = children; + + foreach (var child in children) + { + AddChildren(child); + } + } + + foreach (var root in roots) + { + AddChildren(root); + } + + return roots; + } + + #endregion + + #region 展平树 + + /// + /// 将树形结构展平为列表 + /// + /// 节点类型 + /// 根节点列表 + /// 扁平列表 + public static List Flatten(IEnumerable roots) where T : ITreeNode + { + var result = new List(); + + void FlattenNode(T node) + { + result.Add(node); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + FlattenNode(child); + } + } + } + + foreach (var root in roots) + { + FlattenNode(root); + } + + return result; + } + + /// + /// 将树形结构展平为列表(指定子节点选择器) + /// + /// 节点类型 + /// 根节点列表 + /// 子节点选择器 + /// 扁平列表 + public static List Flatten(IEnumerable roots, Func> childrenSelector) + { + var result = new List(); + + void FlattenNode(T node) + { + result.Add(node); + + var children = childrenSelector(node); + if (children != null) + { + foreach (var child in children) + { + FlattenNode(child); + } + } + } + + foreach (var root in roots) + { + FlattenNode(root); + } + + return result; + } + + #endregion + + #region 遍历树 + + /// + /// 前序遍历(深度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void PreOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode + { + foreach (var root in roots) + { + PreOrderTraversal(root, action); + } + } + + /// + /// 前序遍历(深度优先) + /// + /// 节点类型 + /// 节点 + /// 访问操作 + public static void PreOrderTraversal(T node, Action action) where T : ITreeNode + { + action(node); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + PreOrderTraversal(child, action); + } + } + } + + /// + /// 后序遍历 + /// + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void PostOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode + { + foreach (var root in roots) + { + PostOrderTraversal(root, action); + } + } + + /// + /// 后序遍历 + /// + /// 节点类型 + /// 节点 + /// 访问操作 + public static void PostOrderTraversal(T node, Action action) where T : ITreeNode + { + if (node.Children != null) + { + foreach (var child in node.Children) + { + PostOrderTraversal(child, action); + } + } + + action(node); + } + + /// + /// 层序遍历(广度优先) + /// + /// 节点类型 + /// 根节点列表 + /// 访问操作 + public static void LevelOrderTraversal(IEnumerable roots, Action action) where T : ITreeNode + { + var queue = new Queue(); + + foreach (var root in roots) + { + queue.Enqueue(root); + } + + while (queue.Count > 0) + { + var node = queue.Dequeue(); + action(node); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + queue.Enqueue(child); + } + } + } + } + + #endregion + + #region 搜索树 + + /// + /// 查找节点 + /// + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 找到的节点 + public static T? Find(IEnumerable roots, Func predicate) where T : ITreeNode + { + foreach (var root in roots) + { + var result = Find(root, predicate); + if (result != null) + return result; + } + + return default; + } + + /// + /// 查找节点 + /// + /// 节点类型 + /// 起始节点 + /// 查找条件 + /// 找到的节点 + public static T? Find(T node, Func predicate) where T : ITreeNode + { + if (predicate(node)) + return node; + + if (node.Children != null) + { + foreach (var child in node.Children) + { + var result = Find(child, predicate); + if (result != null) + return result; + } + } + + return default; + } + + /// + /// 查找所有匹配的节点 + /// + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 找到的节点列表 + public static List FindAll(IEnumerable roots, Func predicate) where T : ITreeNode + { + var result = new List(); + + foreach (var root in roots) + { + FindAll(root, predicate, result); + } + + return result; + } + + private static void FindAll(T node, Func predicate, List result) where T : ITreeNode + { + if (predicate(node)) + result.Add(node); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + FindAll(child, predicate, result); + } + } + } + + /// + /// 查找节点路径 + /// + /// 节点类型 + /// 根节点列表 + /// 查找条件 + /// 从根到目标的路径 + public static List FindPath(IEnumerable roots, Func predicate) where T : ITreeNode + { + foreach (var root in roots) + { + var path = new List(); + if (FindPath(root, predicate, path)) + return path; + } + + return new List(); + } + + private static bool FindPath(T node, Func predicate, List path) where T : ITreeNode + { + path.Add(node); + + if (predicate(node)) + return true; + + if (node.Children != null) + { + foreach (var child in node.Children) + { + if (FindPath(child, predicate, path)) + return true; + } + } + + path.RemoveAt(path.Count - 1); + return false; + } + + #endregion + + #region 树属性 + + /// + /// 获取树的深度 + /// + /// 节点类型 + /// 根节点列表 + /// 最大深度 + public static int GetDepth(IEnumerable roots) where T : ITreeNode + { + return roots.Max(root => GetDepth(root)); + } + + /// + /// 获取树的深度 + /// + /// 节点类型 + /// 节点 + /// 深度 + public static int GetDepth(T node) where T : ITreeNode + { + if (node.Children == null || node.Children.Count == 0) + return 1; + + return 1 + node.Children.Max(child => GetDepth(child)); + } + + /// + /// 获取节点数量 + /// + /// 节点类型 + /// 根节点列表 + /// 节点总数 + public static int GetNodeCount(IEnumerable roots) where T : ITreeNode + { + return Flatten(roots).Count; + } + + /// + /// 获取叶子节点数量 + /// + /// 节点类型 + /// 根节点列表 + /// 叶子节点数量 + public static int GetLeafCount(IEnumerable roots) where T : ITreeNode + { + return Flatten(roots).Count(node => node.Children == null || node.Children.Count == 0); + } + + /// + /// 获取所有叶子节点 + /// + /// 节点类型 + /// 根节点列表 + /// 叶子节点列表 + public static List GetLeaves(IEnumerable roots) where T : ITreeNode + { + return Flatten(roots).Where(node => node.Children == null || node.Children.Count == 0).ToList(); + } + + #endregion + + #region 树操作 + + /// + /// 过滤树节点 + /// + /// 节点类型 + /// 根节点列表 + /// 过滤条件 + /// 过滤后的树 + public static List Filter(IEnumerable roots, Func predicate) where T : ITreeNode, new() + { + var result = new List(); + + foreach (var root in roots) + { + var filtered = FilterNode(root, predicate); + if (filtered != null) + result.Add(filtered); + } + + return result; + } + + private static T? FilterNode(T node, Func predicate) where T : ITreeNode, new() + { + var filteredChildren = new List(); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + var filtered = FilterNode(child, predicate); + if (filtered != null) + filteredChildren.Add(filtered); + } + } + + if (predicate(node) || filteredChildren.Count > 0) + { + node.Children = filteredChildren; + return node; + } + + return default; + } + + /// + /// 映射树节点 + /// + /// 源节点类型 + /// 结果节点类型 + /// 根节点列表 + /// 映射函数 + /// 映射后的树 + public static List Map(IEnumerable roots, Func selector) + where TSource : ITreeNode + where TResult : ITreeNode, new() + { + var result = new List(); + + foreach (var root in roots) + { + result.Add(MapNode(root, selector)); + } + + return result; + } + + private static TResult MapNode(TSource node, Func selector) + where TSource : ITreeNode + where TResult : ITreeNode, new() + { + var result = selector(node); + result.Children = new List(); + + if (node.Children != null) + { + foreach (var child in node.Children) + { + result.Children.Add(MapNode(child, selector)); + } + } + + return result; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/CollectionsCategory/TrieUtil.cs b/EasyTool.Core/CollectionsCategory/TrieUtil.cs new file mode 100644 index 0000000..8c0619e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/TrieUtil.cs @@ -0,0 +1,262 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// Trie 树(前缀树)工具类 + /// 用于高效的字符串前缀搜索、自动补全等场景 + /// + public static class TrieUtil + { + /// + /// 创建 Trie 树 + /// + public static Trie Create() + { + return new Trie(); + } + + /// + /// 从字符串集合创建 Trie 树 + /// + public static Trie Create(IEnumerable words) + { + var trie = new Trie(); + foreach (var word in words) + { + trie.Add(word); + } + return trie; + } + } + + /// + /// Trie 树实现 + /// + public class Trie + { + private readonly TrieNode _root; + + /// + /// 已存储的单词数量 + /// + public int Count { get; private set; } + + /// + /// 创建 Trie 树 + /// + public Trie() + { + _root = new TrieNode(); + Count = 0; + } + + /// + /// 添加单词 + /// + public void Add(string word) + { + if (word == null) + throw new ArgumentNullException(nameof(word)); + + var node = _root; + foreach (char c in word) + { + if (!node.Children.ContainsKey(c)) + { + node.Children[c] = new TrieNode(); + } + node = node.Children[c]; + } + + if (!node.IsEndOfWord) + { + node.IsEndOfWord = true; + Count++; + } + } + + /// + /// 批量添加单词 + /// + public void AddRange(IEnumerable words) + { + foreach (var word in words) + { + Add(word); + } + } + + /// + /// 移除单词 + /// + public bool Remove(string word) + { + if (word == null) + throw new ArgumentNullException(nameof(word)); + + return Remove(_root, word, 0); + } + + private bool Remove(TrieNode node, string word, int index) + { + if (index == word.Length) + { + if (!node.IsEndOfWord) + return false; + + node.IsEndOfWord = false; + Count--; + return node.Children.Count == 0; + } + + char c = word[index]; + if (!node.Children.ContainsKey(c)) + return false; + + bool shouldDeleteChild = Remove(node.Children[c], word, index + 1); + + if (shouldDeleteChild) + { + node.Children.Remove(c); + return !node.IsEndOfWord && node.Children.Count == 0; + } + + return false; + } + + /// + /// 是否包含完整单词 + /// + public bool Contains(string word) + { + var node = FindNode(word); + return node != null && node.IsEndOfWord; + } + + /// + /// 是否包含指定前缀 + /// + public bool StartsWith(string prefix) + { + return FindNode(prefix) != null; + } + + /// + /// 获取所有以指定前缀开头的单词 + /// + public IEnumerable GetWordsWithPrefix(string prefix) + { + var node = FindNode(prefix); + if (node == null) + return Enumerable.Empty(); + + return GetAllWords(node, prefix); + } + + /// + /// 自动补全(获取以指定前缀开头的所有单词) + /// + public IEnumerable AutoComplete(string prefix, int maxResults = 10) + { + return GetWordsWithPrefix(prefix).Take(maxResults); + } + + /// + /// 获取所有单词 + /// + public IEnumerable GetAllWords() + { + return GetAllWords(_root, ""); + } + + /// + /// 清空 + /// + public void Clear() + { + _root.Children.Clear(); + Count = 0; + } + + /// + /// 获取最长公共前缀 + /// + public string GetLongestCommonPrefix() + { + var prefix = new System.Text.StringBuilder(); + var node = _root; + + while (node.Children.Count == 1 && !node.IsEndOfWord) + { + var child = node.Children.First(); + prefix.Append(child.Key); + node = child.Value; + } + + return prefix.ToString(); + } + + /// + /// 计算与指定单词匹配的前缀长度 + /// + public int MatchPrefixLength(string word) + { + if (word == null) + return 0; + + var node = _root; + int length = 0; + + foreach (char c in word) + { + if (!node.Children.ContainsKey(c)) + break; + + length++; + node = node.Children[c]; + } + + return length; + } + + private TrieNode FindNode(string prefix) + { + if (prefix == null) + return null; + + var node = _root; + foreach (char c in prefix) + { + if (!node.Children.ContainsKey(c)) + return null; + node = node.Children[c]; + } + return node; + } + + private IEnumerable GetAllWords(TrieNode node, string prefix) + { + if (node.IsEndOfWord) + { + yield return prefix; + } + + foreach (var child in node.Children) + { + foreach (var word in GetAllWords(child.Value, prefix + child.Key)) + { + yield return word; + } + } + } + + private class TrieNode + { + public Dictionary Children { get; } = new Dictionary(); + public bool IsEndOfWord { get; set; } + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs b/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs new file mode 100644 index 0000000..b06c669 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/UnionFindUtil.cs @@ -0,0 +1,273 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 并查集工具类 + /// 用于高效处理元素分组和连通性问题 + /// 支持 union-find 操作,近乎常数时间复杂度 + /// + public static class UnionFindUtil + { + /// + /// 创建并查集 + /// + /// 元素数量 + /// 并查集实例 + public static UnionFind Create(int size) + { + return new UnionFind(size); + } + + /// + /// 从元素集合创建并查集 + /// + public static UnionFind Create(IEnumerable elements) + { + return new UnionFind(elements); + } + } + + /// + /// 整数并查集实现 + /// + public class UnionFind + { + private readonly int[] _parent; + private readonly int[] _rank; + private int _count; + + /// + /// 元素数量 + /// + public int Count => _count; + + /// + /// 创建并查集 + /// + /// 元素数量 + public UnionFind(int size) + { + if (size <= 0) + throw new ArgumentOutOfRangeException(nameof(size)); + + _parent = new int[size]; + _rank = new int[size]; + _count = size; + + for (int i = 0; i < size; i++) + { + _parent[i] = i; + _rank[i] = 1; + } + } + + /// + /// 查找元素所属的集合(带路径压缩) + /// + public int Find(int x) + { + if (x < 0 || x >= _parent.Length) + throw new ArgumentOutOfRangeException(nameof(x)); + + if (_parent[x] != x) + { + _parent[x] = Find(_parent[x]); // 路径压缩 + } + return _parent[x]; + } + + /// + /// 合并两个元素所属的集合 + /// + public void Union(int x, int y) + { + int rootX = Find(x); + int rootY = Find(y); + + if (rootX == rootY) + return; + + // 按秩合并 + if (_rank[rootX] < _rank[rootY]) + { + _parent[rootX] = rootY; + } + else if (_rank[rootX] > _rank[rootY]) + { + _parent[rootY] = rootX; + } + else + { + _parent[rootY] = rootX; + _rank[rootX]++; + } + + _count--; + } + + /// + /// 判断两个元素是否属于同一集合 + /// + public bool Connected(int x, int y) + { + return Find(x) == Find(y); + } + + /// + /// 获取元素所在集合的大小 + /// + public int GetSetSize(int x) + { + int root = Find(x); + int size = 0; + for (int i = 0; i < _parent.Length; i++) + { + if (Find(i) == root) + size++; + } + return size; + } + + /// + /// 获取所有集合 + /// + public Dictionary> GetAllSets() + { + var sets = new Dictionary>(); + + for (int i = 0; i < _parent.Length; i++) + { + int root = Find(i); + if (!sets.ContainsKey(root)) + { + sets[root] = new List(); + } + sets[root].Add(i); + } + + return sets; + } + } + + /// + /// 泛型并查集实现 + /// + /// 元素类型 + public class UnionFind + { + private readonly Dictionary _parent; + private readonly Dictionary _rank; + private int _count; + + /// + /// 集合数量 + /// + public int Count => _count; + + /// + /// 创建并查集 + /// + public UnionFind(IEnumerable elements) + { + if (elements == null) + throw new ArgumentNullException(nameof(elements)); + + _parent = new Dictionary(); + _rank = new Dictionary(); + + foreach (var element in elements) + { + _parent[element] = element; + _rank[element] = 1; + } + + _count = _parent.Count; + } + + /// + /// 添加元素 + /// + public void Add(T element) + { + if (!_parent.ContainsKey(element)) + { + _parent[element] = element; + _rank[element] = 1; + _count++; + } + } + + /// + /// 查找元素所属的集合 + /// + public T Find(T x) + { + if (!_parent.ContainsKey(x)) + throw new KeyNotFoundException($"Element '{x}' not found"); + + if (!EqualityComparer.Default.Equals(_parent[x], x)) + { + _parent[x] = Find(_parent[x]); + } + return _parent[x]; + } + + /// + /// 合并两个元素所属的集合 + /// + public void Union(T x, T y) + { + T rootX = Find(x); + T rootY = Find(y); + + if (EqualityComparer.Default.Equals(rootX, rootY)) + return; + + if (_rank[rootX] < _rank[rootY]) + { + _parent[rootX] = rootY; + } + else if (_rank[rootX] > _rank[rootY]) + { + _parent[rootY] = rootX; + } + else + { + _parent[rootY] = rootX; + _rank[rootX]++; + } + + _count--; + } + + /// + /// 判断两个元素是否属于同一集合 + /// + public bool Connected(T x, T y) + { + return EqualityComparer.Default.Equals(Find(x), Find(y)); + } + + /// + /// 获取所有集合 + /// + public Dictionary> GetAllSets() + { + var sets = new Dictionary>(); + + foreach (var element in _parent.Keys) + { + T root = Find(element); + if (!sets.ContainsKey(root)) + { + sets[root] = new List(); + } + sets[root].Add(element); + } + + return sets; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/WeightedSelector.cs b/EasyTool.Core/CollectionsCategory/WeightedSelector.cs new file mode 100644 index 0000000..44e224e --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/WeightedSelector.cs @@ -0,0 +1,279 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 带权重的选择器 + /// 根据权重随机选择元素 + /// + /// 元素类型 + public class WeightedSelector + { + private readonly List> _items = new(); + private readonly Random _random; + private double _totalWeight; + private readonly object _lock = new(); + + /// + /// 创建权重选择器 + /// + public WeightedSelector() + { + _random = new Random(); + _totalWeight = 0; + } + + /// + /// 创建权重选择器(指定随机种子) + /// + public WeightedSelector(int seed) + { + _random = new Random(seed); + _totalWeight = 0; + } + + /// + /// 元素数量 + /// + public int Count => _items.Count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.Count == 0; + + /// + /// 总权重 + /// + public double TotalWeight => _totalWeight; + + /// + /// 添加元素 + /// + /// 元素 + /// 权重(必须大于0) + public void Add(T item, double weight) + { + if (weight <= 0) + throw new ArgumentException("权重必须大于0", nameof(weight)); + + lock (_lock) + { + _items.Add(new WeightedItem(item, weight, _totalWeight)); + _totalWeight += weight; + } + } + + /// + /// 添加多个元素 + /// + public void AddRange(IEnumerable<(T Item, double Weight)> items) + { + foreach (var (item, weight) in items) + { + Add(item, weight); + } + } + + /// + /// 移除元素 + /// + public bool Remove(T item) + { + lock (_lock) + { + var index = _items.FindIndex(i => EqualityComparer.Default.Equals(i.Item, item)); + if (index < 0) + return false; + + var removed = _items[index]; + _items.RemoveAt(index); + _totalWeight -= removed.Weight; + + // 重新计算累计权重 + var cumulative = 0.0; + foreach (var i in _items) + { + cumulative += i.Weight; + } + + return true; + } + } + + /// + /// 清空所有元素 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + _totalWeight = 0; + } + } + + /// + /// 根据权重随机选择一个元素 + /// + public T? Select() + { + lock (_lock) + { + if (_items.Count == 0) + return default; + + var value = _random.NextDouble() * _totalWeight; + + foreach (var item in _items) + { + if (value < item.CumulativeWeight + item.Weight) + return item.Item; + } + + return _items[^1].Item; + } + } + + /// + /// 根据权重随机选择多个元素(可重复) + /// + public List SelectMultiple(int count) + { + var result = new List(); + for (int i = 0; i < count; i++) + { + var item = Select(); + if (item != null) + result.Add(item); + } + return result; + } + + /// + /// 根据权重随机选择多个不重复元素 + /// + public List SelectDistinct(int count) + { + lock (_lock) + { + if (count >= _items.Count) + return _items.ConvertAll(i => i.Item); + + var result = new List(); + var tempItems = new List>(_items); + var tempTotalWeight = _totalWeight; + + while (result.Count < count && tempItems.Count > 0) + { + var value = _random.NextDouble() * tempTotalWeight; + double cumulative = 0; + + for (int i = 0; i < tempItems.Count; i++) + { + cumulative += tempItems[i].Weight; + if (value < cumulative) + { + result.Add(tempItems[i].Item); + tempTotalWeight -= tempItems[i].Weight; + tempItems.RemoveAt(i); + break; + } + } + } + + return result; + } + } + + /// + /// 获取元素权重 + /// + public double GetWeight(T item) + { + var found = _items.Find(i => EqualityComparer.Default.Equals(i.Item, item)); + return found?.Weight ?? 0; + } + + /// + /// 设置元素权重 + /// + public bool SetWeight(T item, double newWeight) + { + if (newWeight <= 0) + throw new ArgumentException("权重必须大于0", nameof(newWeight)); + + lock (_lock) + { + var index = _items.FindIndex(i => EqualityComparer.Default.Equals(i.Item, item)); + if (index < 0) + return false; + + var oldWeight = _items[index].Weight; + _totalWeight = _totalWeight - oldWeight + newWeight; + + var cumulative = 0.0; + foreach (var i in _items) + { + if (i == _items[index]) + { + _items[index] = new WeightedItem(item, newWeight, cumulative); + cumulative += newWeight; + } + else + { + cumulative += i.Weight; + } + } + + return true; + } + } + + /// + /// 获取选择概率 + /// + public double GetProbability(T item) + { + if (_totalWeight == 0) + return 0; + + var weight = GetWeight(item); + return weight / _totalWeight; + } + + /// + /// 获取所有元素及其权重 + /// + public IEnumerable<(T Item, double Weight, double Probability)> GetAll() + { + lock (_lock) + { + foreach (var item in _items) + { + var probability = _totalWeight > 0 ? item.Weight / _totalWeight : 0; + yield return (item.Item, item.Weight, probability); + } + } + } + } + + /// + /// 带权重的元素 + /// + internal class WeightedItem + { + public T Item { get; } + public double Weight { get; } + public double CumulativeWeight { get; } + + public WeightedItem(T item, double weight, double cumulativeWeight) + { + Item = item; + Weight = weight; + CumulativeWeight = cumulativeWeight; + } + } +} diff --git a/EasyTool.Core/CollectionsCategory/WindowUtil.cs b/EasyTool.Core/CollectionsCategory/WindowUtil.cs new file mode 100644 index 0000000..c1a1786 --- /dev/null +++ b/EasyTool.Core/CollectionsCategory/WindowUtil.cs @@ -0,0 +1,1015 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.CollectionsCategory +{ + /// + /// 滑动窗口工具类 + /// 提供滑动窗口、滑动平均等功能 + /// + public static class SlidingWindowUtil + { + /// + /// 创建滑动窗口枚举器 + /// + /// 元素类型 + /// 源集合 + /// 窗口大小 + /// 每个窗口的元素数组 + public static IEnumerable Windows(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var window = new Queue(); + foreach (var item in source) + { + window.Enqueue(item); + if (window.Count > windowSize) + { + window.Dequeue(); + } + if (window.Count == windowSize) + { + yield return window.ToArray(); + } + } + } + + /// + /// 创建滑动窗口(带步长) + /// + public static IEnumerable Windows(IEnumerable source, int windowSize, int step) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + if (step <= 0) + throw new ArgumentOutOfRangeException(nameof(step)); + + var list = source.ToList(); + for (int i = 0; i <= list.Count - windowSize; i += step) + { + var window = new T[windowSize]; + for (int j = 0; j < windowSize; j++) + { + window[j] = list[i + j]; + } + yield return window; + } + } + + /// + /// 滑动求和 + /// + public static IEnumerable SlidingSum(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + double sum = 0; + var queue = new Queue(); + + foreach (var item in source) + { + queue.Enqueue(item); + sum += item; + + if (queue.Count > windowSize) + { + sum -= queue.Dequeue(); + } + + if (queue.Count == windowSize) + { + yield return sum; + } + } + } + + /// + /// 滑动平均 + /// + public static IEnumerable SlidingAverage(IEnumerable source, int windowSize) + { + return SlidingSum(source, windowSize).Select(sum => sum / windowSize); + } + + /// + /// 滑动最大值 + /// + public static IEnumerable SlidingMax(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var deque = new LinkedList(); // 存储索引 + var list = source.ToList(); + + for (int i = 0; i < list.Count; i++) + { + // 移除窗口外的元素 + while (deque.Count > 0 && deque.First.Value <= i - windowSize) + { + deque.RemoveFirst(); + } + + // 移除比当前元素小的元素(它们不可能是最大值) + while (deque.Count > 0 && list[deque.Last.Value] <= list[i]) + { + deque.RemoveLast(); + } + + deque.AddLast(i); + + if (i >= windowSize - 1) + { + yield return list[deque.First.Value]; + } + } + } + + /// + /// 滑动最小值 + /// + public static IEnumerable SlidingMin(IEnumerable source, int windowSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (windowSize <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSize)); + + var deque = new LinkedList(); + var list = source.ToList(); + + for (int i = 0; i < list.Count; i++) + { + while (deque.Count > 0 && deque.First.Value <= i - windowSize) + { + deque.RemoveFirst(); + } + + while (deque.Count > 0 && list[deque.Last.Value] >= list[i]) + { + deque.RemoveLast(); + } + + deque.AddLast(i); + + if (i >= windowSize - 1) + { + yield return list[deque.First.Value]; + } + } + } + } + + /// + /// 分块工具类 + /// + public static class ChunkUtil + { + /// + /// 将集合分块 + /// + public static IEnumerable> Chunk(IEnumerable source, int chunkSize) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (chunkSize <= 0) + throw new ArgumentOutOfRangeException(nameof(chunkSize)); + + var chunk = new List(chunkSize); + foreach (var item in source) + { + chunk.Add(item); + if (chunk.Count == chunkSize) + { + yield return chunk; + chunk = new List(chunkSize); + } + } + + if (chunk.Count > 0) + { + yield return chunk; + } + } + + /// + /// 将集合分成指定数量的块 + /// + public static List> SplitInto(IEnumerable source, int chunkCount) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (chunkCount <= 0) + throw new ArgumentOutOfRangeException(nameof(chunkCount)); + + var list = source.ToList(); + if (list.Count == 0) + return new List>(); + + var result = new List>(); + int baseSize = list.Count / chunkCount; + int extra = list.Count % chunkCount; + + int index = 0; + for (int i = 0; i < chunkCount; i++) + { + int size = baseSize + (i < extra ? 1 : 0); + var chunk = new List(); + for (int j = 0; j < size && index < list.Count; j++) + { + chunk.Add(list[index++]); + } + result.Add(chunk); + } + + return result; + } + + /// + /// 按条件分块 + /// + public static IEnumerable> ChunkBy(IEnumerable source, Func predicate) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicate == null) + throw new ArgumentNullException(nameof(predicate)); + + var chunk = new List(); + foreach (var item in source) + { + if (predicate(item)) + { + if (chunk.Count > 0) + { + yield return chunk; + chunk = new List(); + } + } + else + { + chunk.Add(item); + } + } + + if (chunk.Count > 0) + { + yield return chunk; + } + } + } + + /// + /// 分区工具类 + /// + public static class PartitionUtil + { + /// + /// 按谓词将集合分成两部分 + /// + public static (List True, List False) Partition(IEnumerable source, Func predicate) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicate == null) + throw new ArgumentNullException(nameof(predicate)); + + var trueList = new List(); + var falseList = new List(); + + foreach (var item in source) + { + if (predicate(item)) + trueList.Add(item); + else + falseList.Add(item); + } + + return (trueList, falseList); + } + + /// + /// 将集合分成多个分区 + /// + public static List> PartitionBy(IEnumerable source, params Func[] predicates) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (predicates == null || predicates.Length == 0) + throw new ArgumentException("At least one predicate is required"); + + var result = new List>(); + for (int i = 0; i < predicates.Length; i++) + { + result.Add(new List()); + } + result.Add(new List()); // 默认分区(不满足任何谓词) + + foreach (var item in source) + { + bool matched = false; + for (int i = 0; i < predicates.Length; i++) + { + if (predicates[i](item)) + { + result[i].Add(item); + matched = true; + break; + } + } + if (!matched) + { + result[predicates.Length].Add(item); + } + } + + return result; + } + + /// + /// 交替分区 + /// + public static (List First, List Second) Alternate(IEnumerable source) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var first = new List(); + var second = new List(); + bool isFirst = true; + + foreach (var item in source) + { + if (isFirst) + first.Add(item); + else + second.Add(item); + isFirst = !isFirst; + } + + return (first, second); + } + + /// + /// 按比例分割 + /// + public static (List First, List Second) SplitByRatio(IEnumerable source, double firstRatio) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (firstRatio < 0 || firstRatio > 1) + throw new ArgumentOutOfRangeException(nameof(firstRatio)); + + var list = source.ToList(); + int firstCount = (int)(list.Count * firstRatio); + + var first = new List(); + var second = new List(); + + for (int i = 0; i < list.Count; i++) + { + if (i < firstCount) + first.Add(list[i]); + else + second.Add(list[i]); + } + + return (first, second); + } + } + + /// + /// 交错工具类 + /// + public static class InterleaveUtil + { + /// + /// 交错合并两个集合 + /// + public static IEnumerable Interleave(IEnumerable first, IEnumerable second) + { + if (first == null) + throw new ArgumentNullException(nameof(first)); + if (second == null) + throw new ArgumentNullException(nameof(second)); + + using var enum1 = first.GetEnumerator(); + using var enum2 = second.GetEnumerator(); + + bool hasFirst, hasSecond; + while (true) + { + hasFirst = enum1.MoveNext(); + hasSecond = enum2.MoveNext(); + + if (!hasFirst && !hasSecond) + break; + + if (hasFirst) + yield return enum1.Current; + if (hasSecond) + yield return enum2.Current; + } + } + + /// + /// 交错合并多个集合 + /// + public static IEnumerable Interleave(params IEnumerable[] sources) + { + if (sources == null || sources.Length == 0) + yield break; + + var enumerators = new List>(); + try + { + foreach (var source in sources) + { + if (source != null) + enumerators.Add(source.GetEnumerator()); + } + + bool anyHasNext = true; + while (anyHasNext) + { + anyHasNext = false; + foreach (var e in enumerators) + { + if (e.MoveNext()) + { + yield return e.Current; + anyHasNext = true; + } + } + } + } + finally + { + foreach (var e in enumerators) + { + e.Dispose(); + } + } + } + + /// + /// 以指定元素为分隔交错 + /// + public static IEnumerable Intersperse(IEnumerable source, T separator) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + bool first = true; + foreach (var item in source) + { + if (!first) + yield return separator; + first = false; + yield return item; + } + } + } + + /// + /// 旋转工具类 + /// + public static class RotateUtil + { + /// + /// 左旋转 + /// + public static List RotateLeft(IEnumerable source, int positions) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + return list; + + positions = positions % list.Count; + if (positions < 0) + positions += list.Count; + + if (positions == 0) + return list; + + var result = new List(list.Count); + result.AddRange(list.Skip(positions)); + result.AddRange(list.Take(positions)); + return result; + } + + /// + /// 右旋转 + /// + public static List RotateRight(IEnumerable source, int positions) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + var list = source.ToList(); + if (list.Count == 0) + return list; + + positions = positions % list.Count; + if (positions < 0) + positions += list.Count; + + if (positions == 0) + return list; + + return RotateLeft(list, list.Count - positions); + } + } + + /// + /// 水库采样工具类 + /// + public static class ReservoirSamplingUtil + { + /// + /// 从集合中随机采样指定数量的元素 + /// + public static List Sample(IEnumerable source, int sampleSize, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (sampleSize <= 0) + throw new ArgumentOutOfRangeException(nameof(sampleSize)); + + random ??= new Random(); + var reservoir = new List(); + int index = 0; + + foreach (var item in source) + { + if (index < sampleSize) + { + reservoir.Add(item); + } + else + { + int j = random.Next(index + 1); + if (j < sampleSize) + { + reservoir[j] = item; + } + } + index++; + } + + return reservoir; + } + + /// + /// 加权随机采样 + /// + public static List WeightedSample(IEnumerable source, Func weightSelector, int sampleSize, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + if (sampleSize <= 0) + throw new ArgumentOutOfRangeException(nameof(sampleSize)); + + random ??= new Random(); + var list = source.ToList(); + + // 使用别名方法采样 + var weights = list.Select(weightSelector).ToList(); + double totalWeight = weights.Sum(); + + var result = new List(); + var used = new HashSet(); + + while (result.Count < sampleSize && used.Count < list.Count) + { + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + if (used.Contains(i)) + continue; + + cumulative += weights[i]; + if (r <= cumulative) + { + result.Add(list[i]); + used.Add(i); + totalWeight -= weights[i]; + break; + } + } + } + + return result; + } + } + + /// + /// 集合差异工具类 + /// + public static class CollectionDiffUtil + { + /// + /// 计算集合差异 + /// + public static CollectionDiff Diff(IEnumerable oldCollection, IEnumerable newCollection) + { + if (oldCollection == null) + throw new ArgumentNullException(nameof(oldCollection)); + if (newCollection == null) + throw new ArgumentNullException(nameof(newCollection)); + + var oldSet = oldCollection.ToHashSet(); + var newSet = newCollection.ToHashSet(); + + var added = newSet.Except(oldSet).ToList(); + var removed = oldSet.Except(newSet).ToList(); + var unchanged = oldSet.Intersect(newSet).ToList(); + + return new CollectionDiff + { + Added = added, + Removed = removed, + Unchanged = unchanged + }; + } + + /// + /// 计算集合差异(使用键选择器) + /// + public static CollectionDiffByKey DiffByKey( + IEnumerable oldCollection, + IEnumerable newCollection, + Func keySelector) where TKey : IEquatable + { + if (oldCollection == null) + throw new ArgumentNullException(nameof(oldCollection)); + if (newCollection == null) + throw new ArgumentNullException(nameof(newCollection)); + if (keySelector == null) + throw new ArgumentNullException(nameof(keySelector)); + + var oldDict = oldCollection.ToDictionary(keySelector); + var newDict = newCollection.ToDictionary(keySelector); + + var oldKeys = oldDict.Keys.ToHashSet(); + var newKeys = newDict.Keys.ToHashSet(); + + var added = newKeys.Except(oldKeys).Select(k => newDict[k]).ToList(); + var removed = oldKeys.Except(newKeys).Select(k => oldDict[k]).ToList(); + var unchanged = oldKeys.Intersect(newKeys).ToList(); + + // 检测修改的项 + var modified = new List>(); + foreach (var key in unchanged) + { + if (!EqualityComparer.Default.Equals(oldDict[key], newDict[key])) + { + modified.Add(new CollectionDiffItem + { + Key = key, + OldValue = oldDict[key], + NewValue = newDict[key] + }); + } + } + + return new CollectionDiffByKey + { + Added = added, + Removed = removed, + Modified = modified, + UnchangedKeys = unchanged + }; + } + + /// + /// 同步集合 + /// + public static void Sync( + ICollection target, + IEnumerable source, + IEqualityComparer comparer = null) + { + if (target == null) + throw new ArgumentNullException(nameof(target)); + if (source == null) + throw new ArgumentNullException(nameof(source)); + + comparer ??= EqualityComparer.Default; + var sourceSet = source.ToHashSet(comparer); + + // 移除不在源中的项 + var toRemove = target.Where(t => !sourceSet.Contains(t)).ToList(); + foreach (var item in toRemove) + { + target.Remove(item); + } + + // 添加不在目标中的项 + var targetSet = target.ToHashSet(comparer); + foreach (var item in sourceSet) + { + if (!targetSet.Contains(item)) + { + target.Add(item); + } + } + } + } + + /// + /// 集合差异结果 + /// + public class CollectionDiff + { + /// + /// 新增的元素 + /// + public List Added { get; set; } + + /// + /// 移除的元素 + /// + public List Removed { get; set; } + + /// + /// 未变化的元素 + /// + public List Unchanged { get; set; } + + /// + /// 是否有变化 + /// + public bool HasChanges => Added.Count > 0 || Removed.Count > 0; + } + + /// + /// 按键的集合差异结果 + /// + public class CollectionDiffByKey + { + /// + /// 新增的元素 + /// + public List Added { get; set; } + + /// + /// 移除的元素 + /// + public List Removed { get; set; } + + /// + /// 修改的元素 + /// + public List> Modified { get; set; } + + /// + /// 未变化的键 + /// + public List UnchangedKeys { get; set; } + + /// + /// 是否有变化 + /// + public bool HasChanges => Added.Count > 0 || Removed.Count > 0 || Modified.Count > 0; + } + + /// + /// 差异项 + /// + public class CollectionDiffItem + { + /// + /// 键 + /// + public TKey Key { get; set; } + + /// + /// 旧值 + /// + public T OldValue { get; set; } + + /// + /// 新值 + /// + public T NewValue { get; set; } + } + + /// + /// 加权随机工具类 + /// + public static class WeightedRandomUtil + { + /// + /// 按权重随机选择一个元素 + /// + public static T Select(IEnumerable source, Func weightSelector, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + + random ??= new Random(); + var list = source.ToList(); + + if (list.Count == 0) + throw new ArgumentException("Collection is empty"); + + var weights = list.Select(weightSelector).ToList(); + double totalWeight = weights.Sum(); + + if (totalWeight <= 0) + throw new ArgumentException("Total weight must be positive"); + + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + cumulative += weights[i]; + if (r <= cumulative) + { + return list[i]; + } + } + + return list[list.Count - 1]; + } + + /// + /// 按权重随机选择多个元素(不放回) + /// + public static List SelectMultiple(IEnumerable source, Func weightSelector, int count, Random random = null) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + if (count <= 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + random ??= new Random(); + var list = source.ToList(); + var weights = list.Select(weightSelector).ToList(); + var result = new List(); + var selected = new HashSet(); + + while (result.Count < count && selected.Count < list.Count) + { + double totalWeight = 0; + for (int i = 0; i < list.Count; i++) + { + if (!selected.Contains(i)) + totalWeight += weights[i]; + } + + if (totalWeight <= 0) + break; + + double r = random.NextDouble() * totalWeight; + double cumulative = 0; + + for (int i = 0; i < list.Count; i++) + { + if (selected.Contains(i)) + continue; + + cumulative += weights[i]; + if (r <= cumulative) + { + result.Add(list[i]); + selected.Add(i); + break; + } + } + } + + return result; + } + + /// + /// 创建别名表以进行高效加权随机采样 + /// + public static AliasTable CreateAliasTable(IEnumerable source, Func weightSelector) + { + return new AliasTable(source, weightSelector); + } + } + + /// + /// 别名表(用于 O(1) 加权随机采样) + /// + public class AliasTable + { + private readonly T[] _items; + private readonly double[] _prob; + private readonly int[] _alias; + private readonly Random _random; + + /// + /// 创建别名表 + /// + public AliasTable(IEnumerable source, Func weightSelector) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (weightSelector == null) + throw new ArgumentNullException(nameof(weightSelector)); + + _items = source.ToArray(); + if (_items.Length == 0) + throw new ArgumentException("Collection is empty"); + + int n = _items.Length; + var weights = _items.Select(weightSelector).ToArray(); + double totalWeight = weights.Sum(); + + _prob = new double[n]; + _alias = new int[n]; + _random = new Random(); + + // 归一化权重 + for (int i = 0; i < n; i++) + { + _prob[i] = weights[i] * n / totalWeight; + } + + var small = new Stack(); + var large = new Stack(); + + for (int i = 0; i < n; i++) + { + if (_prob[i] < 1.0) + small.Push(i); + else + large.Push(i); + } + + while (small.Count > 0 && large.Count > 0) + { + int l = small.Pop(); + int g = large.Pop(); + + _alias[l] = g; + _prob[g] = _prob[g] + _prob[l] - 1.0; + + if (_prob[g] < 1.0) + small.Push(g); + else + large.Push(g); + } + + while (large.Count > 0) + { + _prob[large.Pop()] = 1.0; + } + + while (small.Count > 0) + { + _prob[small.Pop()] = 1.0; + } + } + + /// + /// 随机选择一个元素 + /// + public T Next() + { + int i = _random.Next(_items.Length); + return _random.NextDouble() < _prob[i] ? _items[i] : _items[_alias[i]]; + } + + /// + /// 随机选择多个元素(可能重复) + /// + public List NextMultiple(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Next()); + } + return result; + } + } +} diff --git a/EasyTool.Core/ColorCategory/ColorExtension.cs b/EasyTool.Core/ColorCategory/ColorExtension.cs new file mode 100644 index 0000000..9115b2f --- /dev/null +++ b/EasyTool.Core/ColorCategory/ColorExtension.cs @@ -0,0 +1,336 @@ +using System; +using System.Drawing; + +namespace EasyTool.ColorCategory +{ + /// + /// Color 颜色扩展方法 + /// + public static class ColorExtension + { + #region 转换方法 + + /// + /// 将颜色转换为16进制字符串 + /// + public static string ToHex(this Color color) + { + return color.ToHex(false); + } + + /// + /// 将颜色转换为16进制字符串 + /// + public static string ToHex(this Color color, bool includeAlpha) + { + if (includeAlpha) + return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}"; + else + return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; + } + + /// + /// 从16进制字符串创建颜色 + /// + public static Color FromHex(string hex) + { + if (string.IsNullOrEmpty(hex)) + return Color.Empty; + + hex = hex.TrimStart('#'); + + if (hex.Length == 6) + { + int r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); + int g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); + int b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); + return Color.FromArgb(r, g, b); + } + else if (hex.Length == 8) + { + int a = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); + int r = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); + int g = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); + int b = int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); + return Color.FromArgb(a, r, g, b); + } + + throw new ArgumentException("Invalid hex color format", nameof(hex)); + } + + /// + /// 将颜色转换为 RGB 字符串 + /// + public static string ToRgbString(this Color color) + { + return $"rgb({color.R}, {color.G}, {color.B})"; + } + + /// + /// 将颜色转换为 RGBA 字符串 + /// + public static string ToRgbaString(this Color color) + { + return $"rgba({color.R}, {color.G}, {color.B}, {color.A / 255f:F2})"; + } + + /// + /// 将颜色转换为 HSL + /// + public static (double h, double s, double l) ToHsl(this Color color) + { + double r = color.R / 255d; + double g = color.G / 255d; + double b = color.B / 255d; + + double max = Math.Max(r, Math.Max(g, b)); + double min = Math.Min(r, Math.Min(g, b)); + double h = 0, s = 0, l = (max + min) / 2d; + + if (max != min) + { + double d = max - min; + s = l > 0.5d ? d / (2d - max - min) : d / (max + min); + + if (max == r) + h = (g - b) / d + (g < b ? 6d : 0d); + else if (max == g) + h = (b - r) / d + 2d; + else + h = (r - g) / d + 4d; + + h /= 6d; + } + + return (h * 360d, s * 100d, l * 100d); + } + + /// + /// 从 HSL 创建颜色 + /// + public static Color FromHsl(double h, double s, double l) + { + h = h / 360d; + s = s / 100d; + l = l / 100d; + + double r, g, b; + + if (s == 0) + { + r = g = b = l; + } + else + { + double q = l < 0.5d ? l * (1d + s) : l + s - l * s; + double p = 2d * l - q; + + r = Hue2Rgb(p, q, h + 1d / 3d); + g = Hue2Rgb(p, q, h); + b = Hue2Rgb(p, q, h - 1d / 3d); + } + + return Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255)); + } + + private static double Hue2Rgb(double p, double q, double t) + { + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1d / 6d) return p + (q - p) * 6d * t; + if (t < 1d / 2d) return q; + if (t < 2d / 3d) return p + (q - p) * (2d / 3d - t) * 6d; + return p; + } + + #endregion + + #region 颜色调整 + + /// + /// 变亮颜色 + /// + public static Color Lighten(this Color color, double percent) + { + var (h, s, l) = color.ToHsl(); + l = Math.Min(100, l + percent); + return FromHsl(h, s, l); + } + + /// + /// 变暗颜色 + /// + public static Color Darken(this Color color, double percent) + { + var (h, s, l) = color.ToHsl(); + l = Math.Max(0, l - percent); + return FromHsl(h, s, l); + } + + /// + /// 调整颜色透明度 + /// + public static Color WithAlpha(this Color color, int alpha) + { + return Color.FromArgb(alpha, color.R, color.G, color.B); + } + + /// + /// 调整颜色透明度 + /// + public static Color WithAlpha(this Color color, double alphaPercent) + { + int alpha = (int)(alphaPercent * 255); + return Color.FromArgb(alpha, color.R, color.G, color.B); + } + + /// + /// 反转颜色 + /// + public static Color Invert(this Color color) + { + return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B); + } + + /// + /// 灰度化颜色 + /// + public static Color Grayscale(this Color color) + { + int gray = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114); + return Color.FromArgb(color.A, gray, gray, gray); + } + + /// + /// 混合两种颜色 + /// + public static Color Blend(this Color color1, Color color2, double percent) + { + int r = (int)(color1.R + (color2.R - color1.R) * percent); + int g = (int)(color1.G + (color2.G - color1.G) * percent); + int b = (int)(color1.B + (color2.B - color1.B) * percent); + int a = (int)(color1.A + (color2.A - color1.A) * percent); + return Color.FromArgb(a, r, g, b); + } + + /// + /// 获取互补色 + /// + public static Color Complementary(this Color color) + { + var (h, s, l) = color.ToHsl(); + h = (h + 180) % 360; + return FromHsl(h, s, l); + } + + /// + /// 获取类比色 + /// + public static Color[] Analogous(this Color color, int count = 3) + { + var (h, s, l) = color.ToHsl(); + var colors = new Color[count]; + + for (int i = 0; i < count; i++) + { + double hue = (h + (i * 30)) % 360; + colors[i] = FromHsl(hue, s, l); + } + + return colors; + } + + /// + /// 获取三色组合 + /// + public static Color[] Triadic(this Color color) + { + var (h, s, l) = color.ToHsl(); + return new[] + { + FromHsl(h, s, l), + FromHsl((h + 120) % 360, s, l), + FromHsl((h + 240) % 360, s, l) + }; + } + + #endregion + + #region 颜色判断 + + /// + /// 判断是否是深色 + /// + public static bool IsDark(this Color color) + { + // 使用亮度公式判断 + double brightness = (color.R * 299 + color.G * 587 + color.B * 114) / 1000d; + return brightness < 128; + } + + /// + /// 判断是否是浅色 + /// + public static bool IsLight(this Color color) + { + return !color.IsDark(); + } + + #endregion + + #region 命名颜色 + #endregion + + /// + /// 获取颜色名称 + /// + public static string GetColorName(this Color color) + { + if (color.IsNamedColor) + return color.Name; + + return color.ToHex(); + } + + // endregion + + #region 颜色对比 + + /// + /// 计算两种颜色的对比度 + /// + public static double ContrastWith(this Color color1, Color color2) + { + double GetLuminance(Color c) + { + double r = c.R / 255d; + double g = c.G / 255d; + double b = c.B / 255d; + + r = r <= 0.03928 ? r / 12.92 : Math.Pow((r + 0.055) / 1.055, 2.4); + g = g <= 0.03928 ? g / 12.92 : Math.Pow((g + 0.055) / 1.055, 2.4); + b = b <= 0.03928 ? b / 12.92 : Math.Pow((b + 0.055) / 1.055, 2.4); + + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + + double l1 = GetLuminance(color1); + double l2 = GetLuminance(color2); + + double lighter = Math.Max(l1, l2); + double darker = Math.Min(l1, l2); + + return (lighter + 0.05) / (darker + 0.05); + } + + /// + /// 根据背景色选择合适的文本颜色(黑色或白色) + /// + public static Color GetReadableTextColor(this Color backgroundColor) + { + return backgroundColor.IsDark() ? Color.White : Color.Black; + } + + #endregion + } +} diff --git a/EasyTool.Core/ColorCategory/ColorUtil.cs b/EasyTool.Core/ColorCategory/ColorUtil.cs new file mode 100644 index 0000000..f7bd219 --- /dev/null +++ b/EasyTool.Core/ColorCategory/ColorUtil.cs @@ -0,0 +1,493 @@ +using System; + +namespace EasyTool.ColorCategory +{ + /// + /// 颜色工具类 + /// 提供颜色空间转换和颜色操作功能 + /// + public static class ColorUtil + { + /// + /// RGB 转 HSL + /// + public static HSL RGBToHSL(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double max = Math.Max(rd, Math.Max(gd, bd)); + double min = Math.Min(rd, Math.Min(gd, bd)); + double h = 0, s = 0, l = (max + min) / 2; + + if (Math.Abs(max - min) > 0.0001) + { + double d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + + if (Math.Abs(max - rd) < 0.0001) + h = (gd - bd) / d + (gd < bd ? 6 : 0); + else if (Math.Abs(max - gd) < 0.0001) + h = (bd - rd) / d + 2; + else + h = (rd - gd) / d + 4; + + h /= 6; + } + + return new HSL(h * 360, s * 100, l * 100); + } + + /// + /// HSL 转 RGB + /// + public static RGB HSLToRGB(double h, double s, double l) + { + h /= 360; + s /= 100; + l /= 100; + + double r, g, b; + + if (Math.Abs(s) < 0.0001) + { + r = g = b = l; + } + else + { + double q = l < 0.5 ? l * (1 + s) : l + s - l * s; + double p = 2 * l - q; + + r = HueToRGB(p, q, h + 1.0 / 3.0); + g = HueToRGB(p, q, h); + b = HueToRGB(p, q, h - 1.0 / 3.0); + } + + return new RGB((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255)); + } + + private static double HueToRGB(double p, double q, double t) + { + if (t < 0) t += 1; + if (t > 1) t -= 1; + + if (t < 1.0 / 6.0) return p + (q - p) * 6 * t; + if (t < 1.0 / 2.0) return q; + if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6; + + return p; + } + + /// + /// RGB 转 HSV + /// + public static HSV RGBToHSV(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double max = Math.Max(rd, Math.Max(gd, bd)); + double min = Math.Min(rd, Math.Min(gd, bd)); + double h = 0, s = max == 0 ? 0 : (max - min) / max, v = max; + + if (Math.Abs(max - min) > 0.0001) + { + double d = max - min; + + if (Math.Abs(max - rd) < 0.0001) + h = (gd - bd) / d + (gd < bd ? 6 : 0); + else if (Math.Abs(max - gd) < 0.0001) + h = (bd - rd) / d + 2; + else + h = (rd - gd) / d + 4; + + h /= 6; + } + + return new HSV(h * 360, s * 100, v * 100); + } + + /// + /// HSV 转 RGB + /// + public static RGB HSVToRGB(double h, double s, double v) + { + h /= 360; + s /= 100; + v /= 100; + + int i = (int)Math.Floor(h * 6); + double f = h * 6 - i; + double p = v * (1 - s); + double q = v * (1 - f * s); + double t = v * (1 - (1 - f) * s); + + double r, g, b; + + switch (i % 6) + { + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + default: r = v; g = p; b = q; break; + } + + return new RGB((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255)); + } + + /// + /// RGB 转 CMYK + /// + public static CMYK RGBToCMYK(int r, int g, int b) + { + double rd = r / 255.0; + double gd = g / 255.0; + double bd = b / 255.0; + + double k = 1 - Math.Max(rd, Math.Max(gd, bd)); + + if (Math.Abs(k - 1) < 0.0001) + { + return new CMYK(0, 0, 0, 100); + } + + double c = (1 - rd - k) / (1 - k); + double m = (1 - gd - k) / (1 - k); + double y = (1 - bd - k) / (1 - k); + + return new CMYK(c * 100, m * 100, y * 100, k * 100); + } + + /// + /// CMYK 转 RGB + /// + public static RGB CMYKToRGB(double c, double m, double y, double k) + { + c /= 100; + m /= 100; + y /= 100; + k /= 100; + + int r = (int)Math.Round(255 * (1 - c) * (1 - k)); + int g = (int)Math.Round(255 * (1 - m) * (1 - k)); + int b = (int)Math.Round(255 * (1 - y) * (1 - k)); + + return new RGB(r, g, b); + } + + /// + /// RGB 转十六进制 + /// + public static string RGBToHex(int r, int g, int b) + { + return $"#{r:X2}{g:X2}{b:X2}"; + } + + /// + /// 十六进制转 RGB + /// + public static RGB HexToRGB(string hex) + { + hex = hex.TrimStart('#'); + + if (hex.Length == 3) + { + hex = $"{hex[0]}{hex[0]}{hex[1]}{hex[1]}{hex[2]}{hex[2]}"; + } + + int r = Convert.ToInt32(hex.Substring(0, 2), 16); + int g = Convert.ToInt32(hex.Substring(2, 2), 16); + int b = Convert.ToInt32(hex.Substring(4, 2), 16); + + return new RGB(r, g, b); + } + + /// + /// 计算两个颜色的对比度 + /// + public static double ContrastRatio(RGB color1, RGB color2) + { + double lum1 = RelativeLuminance(color1); + double lum2 = RelativeLuminance(color2); + + double lighter = Math.Max(lum1, lum2); + double darker = Math.Min(lum1, lum2); + + return (lighter + 0.05) / (darker + 0.05); + } + + /// + /// 计算相对亮度 + /// + public static double RelativeLuminance(RGB color) + { + double r = color.R / 255.0; + double g = color.G / 255.0; + double b = color.B / 255.0; + + r = r <= 0.03928 ? r / 12.92 : Math.Pow((r + 0.055) / 1.055, 2.4); + g = g <= 0.03928 ? g / 12.92 : Math.Pow((g + 0.055) / 1.055, 2.4); + b = b <= 0.03928 ? b / 12.92 : Math.Pow((b + 0.055) / 1.055, 2.4); + + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + + /// + /// 混合两个颜色 + /// + public static RGB Blend(RGB color1, RGB color2, double ratio = 0.5) + { + ratio = Math.Max(0, Math.Min(1, ratio)); + + int r = (int)Math.Round(color1.R * (1 - ratio) + color2.R * ratio); + int g = (int)Math.Round(color1.G * (1 - ratio) + color2.G * ratio); + int b = (int)Math.Round(color1.B * (1 - ratio) + color2.B * ratio); + + return new RGB(r, g, b); + } + + /// + /// 调整亮度 + /// + public static RGB AdjustBrightness(RGB color, double amount) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL(hsl.H, hsl.S, Math.Max(0, Math.Min(100, hsl.L + amount))); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 调整饱和度 + /// + public static RGB AdjustSaturation(RGB color, double amount) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL(hsl.H, Math.Max(0, Math.Min(100, hsl.S + amount)), hsl.L); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 获取互补色 + /// + public static RGB GetComplementary(RGB color) + { + var hsl = RGBToHSL(color.R, color.G, color.B); + hsl = new HSL((hsl.H + 180) % 360, hsl.S, hsl.L); + return HSLToRGB(hsl.H, hsl.S, hsl.L); + } + + /// + /// 获取灰度色 + /// + public static RGB ToGrayscale(RGB color) + { + int gray = (int)Math.Round(0.299 * color.R + 0.587 * color.G + 0.114 * color.B); + return new RGB(gray, gray, gray); + } + + /// + /// 反转颜色 + /// + public static RGB Invert(RGB color) + { + return new RGB(255 - color.R, 255 - color.G, 255 - color.B); + } + } + + /// + /// RGB 颜色 + /// + public readonly struct RGB + { + /// 红 (0-255) + public int R { get; } + /// 绿 (0-255) + public int G { get; } + /// 蓝 (0-255) + public int B { get; } + + public RGB(int r, int g, int b) + { + R = Math.Clamp(r, 0, 255); + G = Math.Clamp(g, 0, 255); + B = Math.Clamp(b, 0, 255); + } + + public string ToHex() => ColorUtil.RGBToHex(R, G, B); + + public override string ToString() => $"RGB({R}, {G}, {B})"; + } + + /// + /// HSL 颜色 + /// + public readonly struct HSL + { + /// 色相 (0-360) + public double H { get; } + /// 饱和度 (0-100) + public double S { get; } + /// 亮度 (0-100) + public double L { get; } + + public HSL(double h, double s, double l) + { + H = ((h % 360) + 360) % 360; + S = Math.Clamp(s, 0, 100); + L = Math.Clamp(l, 0, 100); + } + + public RGB ToRGB() => ColorUtil.HSLToRGB(H, S, L); + + public override string ToString() => $"HSL({H:F1}°, {S:F1}%, {L:F1}%)"; + } + + /// + /// HSV 颜色 + /// + public readonly struct HSV + { + /// 色相 (0-360) + public double H { get; } + /// 饱和度 (0-100) + public double S { get; } + /// 明度 (0-100) + public double V { get; } + + public HSV(double h, double s, double v) + { + H = ((h % 360) + 360) % 360; + S = Math.Clamp(s, 0, 100); + V = Math.Clamp(v, 0, 100); + } + + public RGB ToRGB() => ColorUtil.HSVToRGB(H, S, V); + + public override string ToString() => $"HSV({H:F1}°, {S:F1}%, {V:F1}%)"; + } + + /// + /// CMYK 颜色 + /// + public readonly struct CMYK + { + /// 青 (0-100) + public double C { get; } + /// 品红 (0-100) + public double M { get; } + /// 黄 (0-100) + public double Y { get; } + /// 黑 (0-100) + public double K { get; } + + public CMYK(double c, double m, double y, double k) + { + C = Math.Clamp(c, 0, 100); + M = Math.Clamp(m, 0, 100); + Y = Math.Clamp(y, 0, 100); + K = Math.Clamp(k, 0, 100); + } + + public RGB ToRGB() => ColorUtil.CMYKToRGB(C, M, Y, K); + + public override string ToString() => $"CMYK({C:F1}%, {M:F1}%, {Y:F1}%, {K:F1}%)"; + } + + /// + /// 调色板工具类 + /// + public static class ColorPaletteUtil + { + /// + /// 生成类似色配色方案 + /// + public static RGB[] GetAnalogous(RGB baseColor, int count = 3) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + var colors = new RGB[count]; + + for (int i = 0; i < count; i++) + { + double h = (hsl.H + i * 30 - (count - 1) * 15 + 360) % 360; + colors[i] = ColorUtil.HSLToRGB(h, hsl.S, hsl.L); + } + + return colors; + } + + /// + /// 生成互补色配色方案 + /// + public static RGB[] GetComplementary(RGB baseColor) + { + return new[] { baseColor, ColorUtil.GetComplementary(baseColor) }; + } + + /// + /// 生成三色配色方案 + /// + public static RGB[] GetTriadic(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 120) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 240) % 360, hsl.S, hsl.L) + }; + } + + /// + /// 生成四色配色方案 + /// + public static RGB[] GetTetradic(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 90) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 180) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 270) % 360, hsl.S, hsl.L) + }; + } + + /// + /// 生成单色配色方案 + /// + public static RGB[] GetMonochromatic(RGB baseColor, int count = 5) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + var colors = new RGB[count]; + + for (int i = 0; i < count; i++) + { + double l = 20 + i * (60.0 / (count - 1)); + colors[i] = ColorUtil.HSLToRGB(hsl.H, hsl.S, l); + } + + return colors; + } + + /// + /// 生成分裂互补色配色方案 + /// + public static RGB[] GetSplitComplementary(RGB baseColor) + { + var hsl = ColorUtil.RGBToHSL(baseColor.R, baseColor.G, baseColor.B); + + return new[] + { + baseColor, + ColorUtil.HSLToRGB((hsl.H + 150) % 360, hsl.S, hsl.L), + ColorUtil.HSLToRGB((hsl.H + 210) % 360, hsl.S, hsl.L) + }; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/Extension.Convert.cs b/EasyTool.Core/ConvertCategory/ConvertExtension.cs similarity index 88% rename from EasyTool.Core/ConvertCategory/Extension.Convert.cs rename to EasyTool.Core/ConvertCategory/ConvertExtension.cs index c2e380c..e02a64a 100644 --- a/EasyTool.Core/ConvertCategory/Extension.Convert.cs +++ b/EasyTool.Core/ConvertCategory/ConvertExtension.cs @@ -1,13 +1,13 @@ -using System; +using System; using System.Collections.Generic; using System.Text; namespace EasyTool.ConvertCategory { /// - /// 数据类型转化 + /// 数据类型转化扩展 /// - public static partial class Extension + public static class ConvertExtension { #region ==数据转换扩展== @@ -203,16 +203,6 @@ public static string ToIntString(this bool b) return b ? "1" : "0"; } - /// - /// 布尔值转换为整数1或者0 - /// - /// - /// - public static int ToInt(this bool b) - { - return b ? 1 : 0; - } - /// /// 布尔值转换为中文 /// @@ -227,27 +217,6 @@ public static string ToZhCn(this bool b) #region ==字节转换== - /// - /// 转换为16进制 - /// - /// - /// 是否小写 - /// - public static string ToHex(this byte[] bytes, bool lowerCase = true) - { - if (bytes == null) - return string.Empty; - - var result = new StringBuilder(); - var format = lowerCase ? "x2" : "X2"; - for (var i = 0; i < bytes.Length; i++) - { - result.Append(bytes[i].ToString(format)); - } - - return result.ToString(); - } - /// /// 16进制转字节数组 /// @@ -269,16 +238,29 @@ public static string ToHex(this byte[] bytes, bool lowerCase = true) } /// - /// 转换为Base64 + /// 转换为16进制 /// + /// + /// 已过时:请使用 替代 + /// 此方法与 ByteExtension.ToHex 存在命名冲突,已标记为过时 + /// /// + /// 是否小写 /// - public static string ToBase64(this byte[] bytes) + [Obsolete("请使用 ByteExtension.ToHex(byte[], bool) 替代")] + public static string ToHexLegacy(this byte[] bytes, bool lowerCase = true) { if (bytes == null) return string.Empty; - return Convert.ToBase64String(bytes); + var result = new StringBuilder(); + var format = lowerCase ? "x2" : "X2"; + for (var i = 0; i < bytes.Length; i++) + { + result.Append(bytes[i].ToString(format)); + } + + return result.ToString(); } @@ -311,25 +293,6 @@ public static DateTime TimestampToDateTime(this string timeStamp) return dd.Add(ts); } - /// - /// 字符串转Guid - /// - /// - /// - - public static Guid? ToGuid(this string guid) - { - try - { - return new Guid(guid); - } - catch (Exception) - { - - throw; - } - } - #endregion #region 数字转字符串前面补零 diff --git a/EasyTool.Core/ConvertCategory/ConvertUtil.cs b/EasyTool.Core/ConvertCategory/ConvertUtil.cs index 752a52f..aefbd26 100644 --- a/EasyTool.Core/ConvertCategory/ConvertUtil.cs +++ b/EasyTool.Core/ConvertCategory/ConvertUtil.cs @@ -1,161 +1,410 @@ -using System; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Text.Json; -namespace EasyTool +namespace EasyTool.ConvertCategory { /// /// 类型转换工具类 /// public static class ConvertUtil { + #region 基础类型转换 + /// - /// 将对象转换为指定类型,转换失败返回指定类型的默认值 + /// 转换为整数 /// - public static T To(object value) + public static int ToInt(object? value, int defaultValue = 0) { - try - { - return (T)Convert.ChangeType(value, typeof(T)); - } - catch + if (value == null) return defaultValue; + + if (value is int i) return i; + if (value is long l) return (int)l; + if (value is double d) return (int)d; + if (value is decimal dec) return (int)dec; + if (value is float f) return (int)f; + if (value is bool b) return b ? 1 : 0; + if (value is string s) { - return default(T); + return int.TryParse(s, out var result) ? result : defaultValue; } + + return defaultValue; } /// - /// 将字符串转换为整型,转换失败返回0 + /// 转换为长整数 /// - public static int ToInt32(string value) + public static long ToLong(object? value, long defaultValue = 0) { - int result; - if (int.TryParse(value, out result)) + if (value == null) return defaultValue; + + if (value is long l) return l; + if (value is int i) return i; + if (value is double d) return (long)d; + if (value is decimal dec) return (long)dec; + if (value is float f) return (long)f; + if (value is string s) { - return result; + return long.TryParse(s, out var result) ? result : defaultValue; } - return 0; + + return defaultValue; } /// - /// 将字符串转换为长整型,转换失败返回0 + /// 转换为浮点数 /// - public static long ToInt64(string value) + public static double ToDouble(object? value, double defaultValue = 0) { - long result; - if (long.TryParse(value, out result)) + if (value == null) return defaultValue; + + if (value is double d) return d; + if (value is float f) return f; + if (value is decimal dec) return (double)dec; + if (value is int i) return i; + if (value is long l) return l; + if (value is string s) { - return result; + return double.TryParse(s, out var result) ? result : defaultValue; } - return 0; + + return defaultValue; } /// - /// 将字符串转换为布尔型,转换失败返回默认值,默认值false + /// 转换为小数 /// - public static bool ToBoolean(string data, bool defValue = false) + public static decimal ToDecimal(object? value, decimal defaultValue = 0) { - //如果为空则返回默认值 - if (string.IsNullOrEmpty(data)) - { - return defValue; - } + if (value == null) return defaultValue; - bool temp = false; - if (bool.TryParse(data, out temp)) - { - return temp; - } - else + if (value is decimal dec) return dec; + if (value is double d) return (decimal)d; + if (value is float f) return (decimal)f; + if (value is int i) return i; + if (value is long l) return l; + if (value is string s) { - return defValue; + return decimal.TryParse(s, out var result) ? result : defaultValue; } + + return defaultValue; } /// - /// 将对象转换为布尔型,转换失败返回默认值,默认值false + /// 转换为布尔值 /// - public static bool ToBoolean(object data, bool defValue = false) + public static bool ToBool(object? value, bool defaultValue = false) { - //如果为空则返回默认值 - if (data == null || Convert.IsDBNull(data)) - { - return defValue; - } + if (value == null) return defaultValue; - try + if (value is bool b) return b; + if (value is int i) return i != 0; + if (value is long l) return l != 0; + if (value is string s) { - return Convert.ToBoolean(data); + if (string.IsNullOrEmpty(s)) return defaultValue; + + var lower = s.ToLowerInvariant(); + return lower is "true" or "1" or "yes" or "y" or "on"; } - catch + + return defaultValue; + } + + /// + /// 转换为字符串 + /// + public static string ToString(object? value, string defaultValue = "") + { + if (value == null) return defaultValue; + + return value.ToString() ?? defaultValue; + } + + /// + /// 转换为日期时间 + /// + public static DateTime ToDateTime(object? value, DateTime defaultValue = default) + { + if (value == null) return defaultValue; + + if (value is DateTime dt) return dt; + if (value is long ticks) return new DateTime(ticks); + if (value is string s) { - return defValue; + return DateTime.TryParse(s, out var result) ? result : defaultValue; } + + return defaultValue; } /// - /// 将字符串转换为单精度浮点型,转换失败返回0 + /// 转换为Guid /// - public static float ToSingle(string value) + public static Guid ToGuid(object? value, Guid defaultValue = default) { - float result; - if (float.TryParse(value, out result)) + if (value == null) return defaultValue; + + if (value is Guid g) return g; + if (value is string s) { - return result; + return Guid.TryParse(s, out var result) ? result : defaultValue; } - return 0; + + return defaultValue; + } + + #endregion + + #region 进制转换 + + /// + /// 十进制转二进制 + /// + public static string ToBinary(long value) + { + return Convert.ToString(value, 2); + } + + /// + /// 二进制转十进制 + /// + public static long FromBinary(string binary) + { + return Convert.ToInt64(binary, 2); + } + + /// + /// 十进制转八进制 + /// + public static string ToOctal(long value) + { + return Convert.ToString(value, 8); + } + + /// + /// 八进制转十进制 + /// + public static long FromOctal(string octal) + { + return Convert.ToInt64(octal, 8); + } + + /// + /// 十进制转十六进制 + /// + public static string ToHex(long value) + { + return Convert.ToString(value, 16); + } + + /// + /// 十六进制转十进制 + /// + public static long FromHex(string hex) + { + return Convert.ToInt64(hex, 16); } /// - /// 将字符串转换为双精度浮点型,转换失败返回0 + /// 字节数组转十六进制字符串 /// - public static double ToDouble(string value) + public static string BytesToHex(byte[] bytes, bool upperCase = false) { - double result; - if (double.TryParse(value, out result)) + var format = upperCase ? "X2" : "x2"; + var sb = new StringBuilder(bytes.Length * 2); + foreach (var b in bytes) { - return result; + sb.Append(b.ToString(format)); } - return 0; + return sb.ToString(); } /// - /// 将字符串转换为十进制数,转换失败返回0 + /// 十六进制字符串转字节数组 /// - public static decimal ToDecimal(string value) + public static byte[] HexToBytes(string hex) { - decimal result; - if (decimal.TryParse(value, out result)) + if (hex.Length % 2 != 0) + hex = "0" + hex; + + var bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) { - return result; + bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); } - return 0; + return bytes; + } + + #endregion + + #region 编码转换 + + /// + /// 字符串转Base64 + /// + public static string ToBase64(string value, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return Convert.ToBase64String(encoding.GetBytes(value)); + } + + /// + /// Base64转字符串 + /// + public static string FromBase64(string base64, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + return encoding.GetString(Convert.FromBase64String(base64)); + } + + /// + /// 字节数组转Base64 + /// + public static string BytesToBase64(byte[] bytes) + { + return Convert.ToBase64String(bytes); + } + + /// + /// Base64转字节数组 + /// + public static byte[] Base64ToBytes(string base64) + { + return Convert.FromBase64String(base64); } + #endregion + + #region 集合转换 + /// - /// 将字符串转换为日期时间,转换失败返回DateTime.MinValue + /// 字符串数组转整数数组 /// - public static DateTime ToDateTime(string value) + public static int[] ToIntArray(string[] values, int defaultValue = 0) { - DateTime result; - if (DateTime.TryParse(value, out result)) + return values?.Select(v => ToInt(v, defaultValue)).ToArray() ?? Array.Empty(); + } + + /// + /// 整数数组转字符串数组 + /// + public static string[] ToStringArray(int[] values) + { + return values?.Select(v => v.ToString()).ToArray() ?? Array.Empty(); + } + + /// + /// 字典转查询字符串 + /// + public static string DictionaryToQueryString(Dictionary dict) + { + if (dict == null || dict.Count == 0) + return string.Empty; + + var parts = new List(); + foreach (var kvp in dict) { - return result; + if (kvp.Value != null) + { + parts.Add($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"); + } } - return DateTime.MinValue; + return string.Join("&", parts); } /// - /// 将字符串转换为枚举类型,转换失败返回默认值 + /// 查询字符串转字典 /// - public static T ToEnum(string value, T defaultValue = default(T)) where T : struct + public static Dictionary QueryStringToDictionary(string query) { - T result; - if (Enum.TryParse(value, out result)) - { + var result = new Dictionary(); + + if (string.IsNullOrEmpty(query)) return result; + + if (query.StartsWith("?")) + query = query.Substring(1); + + foreach (var part in query.Split('&')) + { + var index = part.IndexOf('='); + if (index > 0) + { + var key = Uri.UnescapeDataString(part.Substring(0, index)); + var value = Uri.UnescapeDataString(part.Substring(index + 1)); + result[key] = value; + } } - return defaultValue; + + return result; } + /// + /// 对象转字典 + /// + public static Dictionary ObjectToDictionary(object obj) + { + if (obj == null) + return new Dictionary(); + + if (obj is Dictionary dict) + return dict; + + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize>(json) ?? new Dictionary(); + } + + /// + /// 字典转对象 + /// + public static T? DictionaryToObject(Dictionary dict) + { + if (dict == null) + return default; + + var json = JsonSerializer.Serialize(dict); + return JsonSerializer.Deserialize(json); + } + + #endregion + + #region 类型判断 + + /// + /// 是否为数值类型 + /// + public static bool IsNumericType(Type type) + { + return type == typeof(int) || type == typeof(long) || type == typeof(short) || + type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) || + type == typeof(ushort) || type == typeof(sbyte) || type == typeof(float) || + type == typeof(double) || type == typeof(decimal); + } + + /// + /// 是否为整数类型 + /// + public static bool IsIntegerType(Type type) + { + return type == typeof(int) || type == typeof(long) || type == typeof(short) || + type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) || + type == typeof(ushort) || type == typeof(sbyte); + } + + /// + /// 是否为浮点类型 + /// + public static bool IsFloatType(Type type) + { + return type == typeof(float) || type == typeof(double) || type == typeof(decimal); + } + #endregion } } diff --git a/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs b/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs new file mode 100644 index 0000000..29a5f5d --- /dev/null +++ b/EasyTool.Core/ConvertCategory/CoordinateConvertUtil.cs @@ -0,0 +1,274 @@ +using System; + +namespace EasyTool.ConvertCategory +{ + /// + /// 坐标转换工具类 + /// 提供常见坐标系统之间的转换(WGS84、GCJ02、BD09) + /// + public static class CoordinateConvertUtil + { + // 坐标系常量 + private const double Pi = 3.1415926535897932384626; + private const double A = 6378245.0; // 长半轴 + private const double EE = 0.00669342162296594323; // 扁率 + + /// + /// 坐标系类型 + /// + public enum CoordinateSystem + { + /// WGS84(GPS原始坐标) + WGS84, + /// GCJ02(国测局坐标/火星坐标) + GCJ02, + /// BD09(百度坐标) + BD09 + } + + /// + /// 经纬度坐标 + /// + public struct GeoPoint + { + /// 经度 + public double Longitude { get; set; } + /// 纬度 + public double Latitude { get; set; } + /// 坐标系 + public CoordinateSystem CoordinateSystem { get; set; } + + public GeoPoint(double longitude, double latitude, CoordinateSystem coordinateSystem = CoordinateSystem.WGS84) + { + Longitude = longitude; + Latitude = latitude; + CoordinateSystem = coordinateSystem; + } + + public override string ToString() => $"({Longitude:F6}, {Latitude:F6})"; + } + + /// + /// WGS84 转 GCJ02 + /// + public static GeoPoint WGS84ToGCJ02(double longitude, double latitude) + { + if (OutOfChina(longitude, latitude)) + { + return new GeoPoint(longitude, latitude, CoordinateSystem.GCJ02); + } + + double dLat = TransformLat(longitude - 105.0, latitude - 35.0); + double dLon = TransformLon(longitude - 105.0, latitude - 35.0); + + double radLat = latitude / 180.0 * Pi; + double magic = Math.Sin(radLat); + magic = 1 - EE * magic * magic; + double sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * Pi); + dLon = (dLon * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * Pi); + + double mgLat = latitude + dLat; + double mgLon = longitude + dLon; + + return new GeoPoint(mgLon, mgLat, CoordinateSystem.GCJ02); + } + + /// + /// GCJ02 转 WGS84 + /// + public static GeoPoint GCJ02ToWGS84(double longitude, double latitude) + { + if (OutOfChina(longitude, latitude)) + { + return new GeoPoint(longitude, latitude, CoordinateSystem.WGS84); + } + + double dLat = TransformLat(longitude - 105.0, latitude - 35.0); + double dLon = TransformLon(longitude - 105.0, latitude - 35.0); + + double radLat = latitude / 180.0 * Pi; + double magic = Math.Sin(radLat); + magic = 1 - EE * magic * magic; + double sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * Pi); + dLon = (dLon * 180.0) / (A / sqrtMagic * Math.Cos(radLat) * Pi); + + double mgLat = latitude + dLat; + double mgLon = longitude + dLon; + + return new GeoPoint(longitude * 2 - mgLon, latitude * 2 - mgLat, CoordinateSystem.WGS84); + } + + /// + /// GCJ02 转 BD09 + /// + public static GeoPoint GCJ02ToBD09(double longitude, double latitude) + { + double x = longitude; + double y = latitude; + + double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * Pi * 3000.0 / 180.0); + double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * Pi * 3000.0 / 180.0); + + double bdLon = z * Math.Cos(theta) + 0.0065; + double bdLat = z * Math.Sin(theta) + 0.006; + + return new GeoPoint(bdLon, bdLat, CoordinateSystem.BD09); + } + + /// + /// BD09 转 GCJ02 + /// + public static GeoPoint BD09ToGCJ02(double longitude, double latitude) + { + double x = longitude - 0.0065; + double y = latitude - 0.006; + + double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Pi * 3000.0 / 180.0); + double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Pi * 3000.0 / 180.0); + + double gcjLon = z * Math.Cos(theta); + double gcjLat = z * Math.Sin(theta); + + return new GeoPoint(gcjLon, gcjLat, CoordinateSystem.GCJ02); + } + + /// + /// BD09 转 WGS84 + /// + public static GeoPoint BD09ToWGS84(double longitude, double latitude) + { + var gcj02 = BD09ToGCJ02(longitude, latitude); + return GCJ02ToWGS84(gcj02.Longitude, gcj02.Latitude); + } + + /// + /// WGS84 转 BD09 + /// + public static GeoPoint WGS84ToBD09(double longitude, double latitude) + { + var gcj02 = WGS84ToGCJ02(longitude, latitude); + return GCJ02ToBD09(gcj02.Longitude, gcj02.Latitude); + } + + /// + /// 通用坐标转换 + /// + public static GeoPoint Convert(double longitude, double latitude, CoordinateSystem from, CoordinateSystem to) + { + if (from == to) + return new GeoPoint(longitude, latitude, to); + + return (from, to) switch + { + (CoordinateSystem.WGS84, CoordinateSystem.GCJ02) => WGS84ToGCJ02(longitude, latitude), + (CoordinateSystem.WGS84, CoordinateSystem.BD09) => WGS84ToBD09(longitude, latitude), + (CoordinateSystem.GCJ02, CoordinateSystem.WGS84) => GCJ02ToWGS84(longitude, latitude), + (CoordinateSystem.GCJ02, CoordinateSystem.BD09) => GCJ02ToBD09(longitude, latitude), + (CoordinateSystem.BD09, CoordinateSystem.WGS84) => BD09ToWGS84(longitude, latitude), + (CoordinateSystem.BD09, CoordinateSystem.GCJ02) => BD09ToGCJ02(longitude, latitude), + _ => new GeoPoint(longitude, latitude, to) + }; + } + + /// + /// 计算两点之间的距离(米) + /// 使用 Haversine 公式 + /// + public static double Distance(double lon1, double lat1, double lon2, double lat2) + { + const double R = 6371000; // 地球半径(米) + + double dLat = (lat2 - lat1) * Pi / 180; + double dLon = (lon2 - lon1) * Pi / 180; + + double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(lat1 * Pi / 180) * Math.Cos(lat2 * Pi / 180) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return R * c; + } + + /// + /// 计算两点之间的距离 + /// + public static double Distance(GeoPoint p1, GeoPoint p2) + { + return Distance(p1.Longitude, p1.Latitude, p2.Longitude, p2.Latitude); + } + + /// + /// 计算方位角(从北向顺时针) + /// + public static double Bearing(double lon1, double lat1, double lon2, double lat2) + { + double dLon = (lon2 - lon1) * Pi / 180; + + double y = Math.Sin(dLon) * Math.Cos(lat2 * Pi / 180); + double x = Math.Cos(lat1 * Pi / 180) * Math.Sin(lat2 * Pi / 180) - + Math.Sin(lat1 * Pi / 180) * Math.Cos(lat2 * Pi / 180) * Math.Cos(dLon); + + double bearing = Math.Atan2(y, x) * 180 / Pi; + return (bearing + 360) % 360; + } + + /// + /// 根据起点、方位角和距离计算终点 + /// + public static GeoPoint Destination(double lon1, double lat1, double bearing, double distance) + { + const double R = 6371000; + + double brng = bearing * Pi / 180; + double d = distance / R; + + double lat1Rad = lat1 * Pi / 180; + double lon1Rad = lon1 * Pi / 180; + + double lat2Rad = Math.Asin(Math.Sin(lat1Rad) * Math.Cos(d) + + Math.Cos(lat1Rad) * Math.Sin(d) * Math.Cos(brng)); + + double lon2Rad = lon1Rad + Math.Atan2( + Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat1Rad), + Math.Cos(d) - Math.Sin(lat1Rad) * Math.Sin(lat2Rad)); + + return new GeoPoint(lon2Rad * 180 / Pi, lat2Rad * 180 / Pi); + } + + /// + /// 判断是否在中国境内 + /// + public static bool OutOfChina(double longitude, double latitude) + { + if (longitude < 72.004 || longitude > 137.8347) + return true; + if (latitude < 0.8293 || latitude > 55.8271) + return true; + + return false; + } + + private static double TransformLat(double x, double y) + { + double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Pi) + 20.0 * Math.Sin(2.0 * x * Pi)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(y * Pi) + 40.0 * Math.Sin(y / 3.0 * Pi)) * 2.0 / 3.0; + ret += (160.0 * Math.Sin(y / 12.0 * Pi) + 320 * Math.Sin(y * Pi / 30.0)) * 2.0 / 3.0; + return ret; + } + + private static double TransformLon(double x, double y) + { + double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Pi) + 20.0 * Math.Sin(2.0 * x * Pi)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(x * Pi) + 40.0 * Math.Sin(x / 3.0 * Pi)) * 2.0 / 3.0; + ret += (150.0 * Math.Sin(x / 12.0 * Pi) + 300.0 * Math.Sin(x / 30.0 * Pi)) * 2.0 / 3.0; + return ret; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs b/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs new file mode 100644 index 0000000..9133fb3 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/CsvConvertUtil.cs @@ -0,0 +1,348 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Text; + +namespace EasyTool.ConvertCategory +{ + /// + /// CSV转换工具类 + /// + public static class CsvConvertUtil + { + /// + /// 对象列表转CSV字符串 + /// + public static string ToCsv(IEnumerable list, bool includeHeader = true, char separator = ',') + { + var properties = typeof(T).GetProperties(); + var sb = new StringBuilder(); + + // 添加表头 + if (includeHeader) + { + var headers = new List(); + foreach (var prop in properties) + { + headers.Add(EscapeCsvField(prop.Name, separator)); + } + sb.AppendLine(string.Join(separator, headers)); + } + + // 添加数据行 + foreach (var item in list) + { + var values = new List(); + foreach (var prop in properties) + { + var value = prop.GetValue(item)?.ToString() ?? ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV字符串转对象列表 + /// + public static List FromCsv(string csv, bool hasHeader = true, char separator = ',') where T : new() + { + var result = new List(); + var properties = typeof(T).GetProperties(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return result; + + var startIndex = hasHeader ? 1 : 0; + var headers = hasHeader ? ParseCsvLine(lines[0], separator) : null; + + // 构建属性映射 + var propMap = new Dictionary(); + if (headers != null) + { + for (int i = 0; i < headers.Count; i++) + { + var header = headers[i].Trim(); + foreach (var prop in properties) + { + if (prop.Name.Equals(header, StringComparison.OrdinalIgnoreCase)) + { + propMap[prop.Name] = i; + break; + } + } + } + } + + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var item = new T(); + + for (int j = 0; j < properties.Length && j < values.Count; j++) + { + var prop = properties[j]; + var index = headers != null && propMap.TryGetValue(prop.Name, out var mapIndex) ? mapIndex : j; + + if (index < values.Count) + { + var value = UnescapeCsvField(values[index]); + if (!string.IsNullOrEmpty(value)) + { + var convertedValue = Convert.ChangeType(value, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); + prop.SetValue(item, convertedValue); + } + } + } + + result.Add(item); + } + + return result; + } + + /// + /// DataTable转CSV + /// + public static string ToCsv(DataTable table, bool includeHeader = true, char separator = ',') + { + var sb = new StringBuilder(); + + // 添加表头 + if (includeHeader) + { + var headers = new List(); + foreach (DataColumn col in table.Columns) + { + headers.Add(EscapeCsvField(col.ColumnName, separator)); + } + sb.AppendLine(string.Join(separator, headers)); + } + + // 添加数据行 + foreach (DataRow row in table.Rows) + { + var values = new List(); + foreach (DataColumn col in table.Columns) + { + var value = row[col]?.ToString() ?? ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV转DataTable + /// + public static DataTable FromCsv(string csv, bool hasHeader = true, char separator = ',') + { + var table = new DataTable(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return table; + + // 解析第一行获取列数 + var firstLine = ParseCsvLine(lines[0], separator); + + // 创建列 + if (hasHeader) + { + foreach (var header in firstLine) + { + table.Columns.Add(UnescapeCsvField(header)); + } + } + else + { + for (int i = 0; i < firstLine.Count; i++) + { + table.Columns.Add($"Column{i + 1}"); + } + } + + // 添加数据行 + var startIndex = hasHeader ? 1 : 0; + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var row = table.NewRow(); + + for (int j = 0; j < Math.Min(values.Count, table.Columns.Count); j++) + { + row[j] = UnescapeCsvField(values[j]); + } + + table.Rows.Add(row); + } + + return table; + } + + /// + /// 字典列表转CSV + /// + public static string ToCsv(IEnumerable> dicts, bool includeHeader = true, char separator = ',') + { + var sb = new StringBuilder(); + var headers = new List(); + var isFirst = true; + + foreach (var dict in dicts) + { + if (isFirst) + { + headers.AddRange(dict.Keys); + if (includeHeader) + { + var headerLine = new List(); + foreach (var header in headers) + { + headerLine.Add(EscapeCsvField(header, separator)); + } + sb.AppendLine(string.Join(separator, headerLine)); + } + isFirst = false; + } + + var values = new List(); + foreach (var header in headers) + { + var value = dict.TryGetValue(header, out var v) ? v?.ToString() ?? "" : ""; + values.Add(EscapeCsvField(value, separator)); + } + sb.AppendLine(string.Join(separator, values)); + } + + return sb.ToString(); + } + + /// + /// CSV转字典列表 + /// + public static List> ToDictionaryList(string csv, bool hasHeader = true, char separator = ',') + { + var result = new List>(); + var lines = csv.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length == 0) + return result; + + var firstLine = ParseCsvLine(lines[0], separator); + var headers = new List(); + + if (hasHeader) + { + foreach (var h in firstLine) + { + headers.Add(UnescapeCsvField(h)); + } + } + else + { + for (int i = 0; i < firstLine.Count; i++) + { + headers.Add($"Column{i + 1}"); + } + } + + var startIndex = hasHeader ? 1 : 0; + for (int i = startIndex; i < lines.Length; i++) + { + var values = ParseCsvLine(lines[i], separator); + var dict = new Dictionary(); + + for (int j = 0; j < headers.Count && j < values.Count; j++) + { + dict[headers[j]] = UnescapeCsvField(values[j]); + } + + result.Add(dict); + } + + return result; + } + + /// + /// 保存CSV到文件 + /// + public static void SaveToFile(string csv, string filePath, Encoding? encoding = null) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + File.WriteAllText(filePath, csv, encoding ?? Encoding.UTF8); + } + + /// + /// 从文件读取CSV + /// + public static string LoadFromFile(string filePath, Encoding? encoding = null) + { + return File.ReadAllText(filePath, encoding ?? Encoding.UTF8); + } + + private static string EscapeCsvField(string field, char separator) + { + if (field.Contains(separator) || field.Contains("\"") || field.Contains("\n") || field.Contains("\r")) + { + return "\"" + field.Replace("\"", "\"\"") + "\""; + } + return field; + } + + private static string UnescapeCsvField(string field) + { + if (field.StartsWith("\"") && field.EndsWith("\"")) + { + return field.Substring(1, field.Length - 2).Replace("\"\"", "\""); + } + return field; + } + + private static List ParseCsvLine(string line, char separator) + { + var result = new List(); + var current = new StringBuilder(); + var inQuotes = false; + + for (int i = 0; i < line.Length; i++) + { + var c = line[i]; + + if (c == '"') + { + if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') + { + current.Append('"'); + i++; + } + else + { + inQuotes = !inQuotes; + } + } + else if (c == separator && !inQuotes) + { + result.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + result.Add(current.ToString()); + return result; + } + } +} diff --git a/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs b/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs new file mode 100644 index 0000000..3d00029 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/MsgPackConvertUtil.cs @@ -0,0 +1,789 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.ConvertCategory +{ + /// + /// MessagePack 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 MessagePack 序列化和反序列化 + /// + public static class MsgPackConvertUtil + { + #region 序列化 + + /// + /// 将对象序列化为 MessagePack 字节数组 + /// + /// 对象类型 + /// 要序列化的对象 + /// MessagePack 字节数组 + public static byte[] Serialize(T obj) + { + using var stream = new MemoryStream(); + SerializeValue(obj, stream); + return stream.ToArray(); + } + + /// + /// 将对象序列化为 MessagePack 并写入流 + /// + /// 对象类型 + /// 要序列化的对象 + /// 目标流 + public static void Serialize(T obj, Stream stream) + { + SerializeValue(obj, stream); + } + + /// + /// 将字典序列化为 MessagePack 字节数组 + /// + /// 要序列化的字典 + /// MessagePack 字节数组 + public static byte[] SerializeDictionary(IDictionary dict) + { + using var stream = new MemoryStream(); + SerializeDictionary(dict, stream); + return stream.ToArray(); + } + + private static void SerializeValue(object? value, Stream stream) + { + if (value == null) + { + WriteNil(stream); + return; + } + + var type = value.GetType(); + + // 布尔值 + if (type == typeof(bool)) + { + WriteBool((bool)value, stream); + return; + } + + // 整数类型 + if (type == typeof(sbyte)) { WriteInteger((sbyte)value, stream); return; } + if (type == typeof(byte)) { WriteInteger((byte)value, stream); return; } + if (type == typeof(short)) { WriteInteger((short)value, stream); return; } + if (type == typeof(ushort)) { WriteInteger((ushort)value, stream); return; } + if (type == typeof(int)) { WriteInteger((int)value, stream); return; } + if (type == typeof(uint)) { WriteInteger((uint)value, stream); return; } + if (type == typeof(long)) { WriteInteger((long)value, stream); return; } + if (type == typeof(ulong)) { WriteInteger((ulong)value, stream); return; } + + // 浮点数 + if (type == typeof(float)) { WriteFloat((float)value, stream); return; } + if (type == typeof(double)) { WriteDouble((double)value, stream); return; } + + // 字符串 + if (type == typeof(string)) + { + WriteString((string)value, stream); + return; + } + + // 字节数组 + if (type == typeof(byte[])) + { + WriteBinary((byte[])value, stream); + return; + } + + // 数组和列表 + if (value is IEnumerable enumerable and not string and not IDictionary) + { + SerializeArray(enumerable, stream); + return; + } + + // 字典 + if (value is IDictionary dict) + { + SerializeDictionary(dict, stream); + return; + } + + // 其他对象 + SerializeObject(value, stream); + } + + private static void WriteNil(Stream stream) + { + stream.WriteByte(0xC0); + } + + private static void WriteBool(bool value, Stream stream) + { + stream.WriteByte(value ? (byte)0xC3 : (byte)0xC2); + } + + private static void WriteInteger(sbyte value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)value); + } + } + + private static void WriteInteger(byte value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(short value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else + { + stream.WriteByte(0xD1); + WriteBigEndianInt16(value, stream); + } + } + + private static void WriteInteger(ushort value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(int value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else if (value >= short.MinValue) + { + stream.WriteByte(0xD1); + WriteBigEndianInt16((short)value, stream); + } + else + { + stream.WriteByte(0xD2); + WriteBigEndianInt32(value, stream); + } + } + + private static void WriteInteger(uint value, Stream stream) + { + WriteInteger((ulong)value, stream); + } + + private static void WriteInteger(long value, Stream stream) + { + if (value >= 0) + { + WriteInteger((ulong)value, stream); + } + else if (value >= sbyte.MinValue) + { + stream.WriteByte(0xD0); + stream.WriteByte((byte)(sbyte)value); + } + else if (value >= short.MinValue) + { + stream.WriteByte(0xD1); + WriteBigEndianInt16((short)value, stream); + } + else if (value >= int.MinValue) + { + stream.WriteByte(0xD2); + WriteBigEndianInt32((int)value, stream); + } + else + { + stream.WriteByte(0xD3); + WriteBigEndianInt64(value, stream); + } + } + + private static void WriteInteger(ulong value, Stream stream) + { + if (value <= 127) + { + // Positive FixInt + stream.WriteByte((byte)value); + } + else if (value <= byte.MaxValue) + { + stream.WriteByte(0xCC); + stream.WriteByte((byte)value); + } + else if (value <= ushort.MaxValue) + { + stream.WriteByte(0xCD); + WriteBigEndianUInt16((ushort)value, stream); + } + else if (value <= uint.MaxValue) + { + stream.WriteByte(0xCE); + WriteBigEndianUInt32((uint)value, stream); + } + else + { + stream.WriteByte(0xCF); + WriteBigEndianUInt64(value, stream); + } + } + + private static void WriteFloat(float value, Stream stream) + { + stream.WriteByte(0xCA); + var bytes = BitConverter.GetBytes(value); + stream.Write(bytes, 0, 4); + } + + private static void WriteDouble(double value, Stream stream) + { + stream.WriteByte(0xCB); + var bytes = BitConverter.GetBytes(value); + stream.Write(bytes, 0, 8); + } + + private static void WriteString(string value, Stream stream) + { + var bytes = Encoding.UTF8.GetBytes(value); + var length = bytes.Length; + + if (length <= 31) + { + // FixStr + stream.WriteByte((byte)(0xA0 | length)); + } + else if (length <= byte.MaxValue) + { + stream.WriteByte(0xD9); + stream.WriteByte((byte)length); + } + else if (length <= ushort.MaxValue) + { + stream.WriteByte(0xDA); + WriteBigEndianUInt16((ushort)length, stream); + } + else + { + stream.WriteByte(0xDB); + WriteBigEndianUInt32((uint)length, stream); + } + + stream.Write(bytes, 0, bytes.Length); + } + + private static void WriteBinary(byte[] value, Stream stream) + { + var length = value.Length; + + if (length <= byte.MaxValue) + { + stream.WriteByte(0xC4); + stream.WriteByte((byte)length); + } + else if (length <= ushort.MaxValue) + { + stream.WriteByte(0xC5); + WriteBigEndianUInt16((ushort)length, stream); + } + else + { + stream.WriteByte(0xC6); + WriteBigEndianUInt32((uint)length, stream); + } + + stream.Write(value, 0, length); + } + + private static void SerializeArray(IEnumerable enumerable, Stream stream) + { + var list = new List(); + foreach (var item in enumerable) + { + list.Add(item); + } + + var count = list.Count; + + if (count <= 15) + { + // FixArray + stream.WriteByte((byte)(0x90 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDC); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDD); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (var item in list) + { + SerializeValue(item, stream); + } + } + + private static void SerializeDictionary(IDictionary dict, Stream stream) + { + var count = dict.Count; + + if (count <= 15) + { + // FixMap + stream.WriteByte((byte)(0x80 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDE); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDF); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (DictionaryEntry entry in dict) + { + SerializeValue(entry.Key, stream); + SerializeValue(entry.Value, stream); + } + } + + private static void SerializeObject(object obj, Stream stream) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + var count = 0; + foreach (var prop in properties) + { + if (prop.CanRead) + count++; + } + + if (count <= 15) + { + stream.WriteByte((byte)(0x80 | count)); + } + else if (count <= ushort.MaxValue) + { + stream.WriteByte(0xDE); + WriteBigEndianUInt16((ushort)count, stream); + } + else + { + stream.WriteByte(0xDF); + WriteBigEndianUInt32((uint)count, stream); + } + + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + WriteString(prop.Name, stream); + SerializeValue(prop.GetValue(obj), stream); + } + } + + private static void WriteBigEndianInt16(short value, Stream stream) + { + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt16(ushort value, Stream stream) + { + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianInt32(int value, Stream stream) + { + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt32(uint value, Stream stream) + { + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianInt64(long value, Stream stream) + { + stream.WriteByte((byte)(value >> 56)); + stream.WriteByte((byte)(value >> 48)); + stream.WriteByte((byte)(value >> 40)); + stream.WriteByte((byte)(value >> 32)); + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static void WriteBigEndianUInt64(ulong value, Stream stream) + { + stream.WriteByte((byte)(value >> 56)); + stream.WriteByte((byte)(value >> 48)); + stream.WriteByte((byte)(value >> 40)); + stream.WriteByte((byte)(value >> 32)); + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + #endregion + + #region 反序列化 + + /// + /// 从 MessagePack 字节数组反序列化为对象 + /// + /// 目标类型 + /// MessagePack 字节数组 + /// 反序列化的对象 + public static T? Deserialize(byte[] data) + { + using var stream = new MemoryStream(data); + return Deserialize(stream); + } + + /// + /// 从流中反序列化对象 + /// + /// 目标类型 + /// 数据流 + /// 反序列化的对象 + public static T? Deserialize(Stream stream) + { + var value = DeserializeValue(stream); + return ConvertValue(value); + } + + /// + /// 从 MessagePack 字节数组反序列化为字典 + /// + /// MessagePack 字节数组 + /// 字典对象 + public static Dictionary DeserializeToDictionary(byte[] data) + { + using var stream = new MemoryStream(data); + return DeserializeToDictionary(stream); + } + + /// + /// 从流中反序列化为字典 + /// + /// 数据流 + /// 字典对象 + public static Dictionary DeserializeToDictionary(Stream stream) + { + var value = DeserializeValue(stream); + if (value is Dictionary dict) + { + return dict; + } + return new Dictionary(); + } + + private static object? DeserializeValue(Stream stream) + { + var header = stream.ReadByte(); + if (header < 0) + throw new EndOfStreamException(); + + // Positive FixInt (0x00 - 0x7F) + if (header <= 0x7F) + { + return (byte)header; + } + + // FixMap (0x80 - 0x8F) + if ((header & 0xF0) == 0x80) + { + var count = header & 0x0F; + return DeserializeMap(stream, count); + } + + // FixArray (0x90 - 0x9F) + if ((header & 0xF0) == 0x90) + { + var count = header & 0x0F; + return DeserializeArray(stream, count); + } + + // FixStr (0xA0 - 0xBF) + if ((header & 0xE0) == 0xA0) + { + var length = header & 0x1F; + return DeserializeString(stream, length); + } + + // Negative FixInt (0xE0 - 0xFF) + if (header >= 0xE0) + { + return (sbyte)(byte)header; + } + + // 其他格式 + switch (header) + { + case 0xC0: // nil + return null; + + case 0xC2: // false + return false; + + case 0xC3: // true + return true; + + case 0xC4: // bin 8 + return DeserializeBinary(stream, ReadUInt8(stream)); + + case 0xC5: // bin 16 + return DeserializeBinary(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xC6: // bin 32 + return DeserializeBinary(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xC7: // ext 8 + case 0xC8: // ext 16 + case 0xC9: // ext 32 + throw new NotSupportedException("Extension types are not supported"); + + case 0xCA: // float 32 + return ReadFloat(stream); + + case 0xCB: // float 64 + return ReadDouble(stream); + + case 0xCC: // uint 8 + return ReadUInt8(stream); + + case 0xCD: // uint 16 + return ReadBigEndianUInt16(stream); + + case 0xCE: // uint 32 + return ReadBigEndianUInt32(stream); + + case 0xCF: // uint 64 + return ReadBigEndianUInt64(stream); + + case 0xD0: // int 8 + return ReadInt8(stream); + + case 0xD1: // int 16 + return ReadBigEndianInt16(stream); + + case 0xD2: // int 32 + return ReadBigEndianInt32(stream); + + case 0xD3: // int 64 + return ReadBigEndianInt64(stream); + + case 0xD9: // str 8 + return DeserializeString(stream, ReadUInt8(stream)); + + case 0xDA: // str 16 + return DeserializeString(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDB: // str 32 + return DeserializeString(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xDC: // array 16 + return DeserializeArray(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDD: // array 32 + return DeserializeArray(stream, (int)ReadBigEndianUInt32(stream)); + + case 0xDE: // map 16 + return DeserializeMap(stream, (int)ReadBigEndianUInt16(stream)); + + case 0xDF: // map 32 + return DeserializeMap(stream, (int)ReadBigEndianUInt32(stream)); + + default: + throw new NotSupportedException($"Unknown format: 0x{header:X2}"); + } + } + + private static sbyte ReadInt8(Stream stream) + { + var b = stream.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + return (sbyte)b; + } + + private static byte ReadUInt8(Stream stream) + { + var b = stream.ReadByte(); + if (b < 0) throw new EndOfStreamException(); + return (byte)b; + } + + private static short ReadBigEndianInt16(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + if (b1 < 0 || b2 < 0) throw new EndOfStreamException(); + return (short)((b1 << 8) | b2); + } + + private static ushort ReadBigEndianUInt16(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + if (b1 < 0 || b2 < 0) throw new EndOfStreamException(); + return (ushort)((b1 << 8) | b2); + } + + private static int ReadBigEndianInt32(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + var b3 = stream.ReadByte(); + var b4 = stream.ReadByte(); + if (b1 < 0 || b2 < 0 || b3 < 0 || b4 < 0) throw new EndOfStreamException(); + return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; + } + + private static uint ReadBigEndianUInt32(Stream stream) + { + var b1 = stream.ReadByte(); + var b2 = stream.ReadByte(); + var b3 = stream.ReadByte(); + var b4 = stream.ReadByte(); + if (b1 < 0 || b2 < 0 || b3 < 0 || b4 < 0) throw new EndOfStreamException(); + return (uint)((b1 << 24) | (b2 << 16) | (b3 << 8) | b4); + } + + private static long ReadBigEndianInt64(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + + return ((long)bytes[0] << 56) | ((long)bytes[1] << 48) | + ((long)bytes[2] << 40) | ((long)bytes[3] << 32) | + ((long)bytes[4] << 24) | ((long)bytes[5] << 16) | + ((long)bytes[6] << 8) | bytes[7]; + } + + private static ulong ReadBigEndianUInt64(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + + return ((ulong)bytes[0] << 56) | ((ulong)bytes[1] << 48) | + ((ulong)bytes[2] << 40) | ((ulong)bytes[3] << 32) | + ((ulong)bytes[4] << 24) | ((ulong)bytes[5] << 16) | + ((ulong)bytes[6] << 8) | bytes[7]; + } + + private static float ReadFloat(Stream stream) + { + var bytes = new byte[4]; + var read = stream.Read(bytes, 0, 4); + if (read < 4) throw new EndOfStreamException(); + return BitConverter.ToSingle(bytes, 0); + } + + private static double ReadDouble(Stream stream) + { + var bytes = new byte[8]; + var read = stream.Read(bytes, 0, 8); + if (read < 8) throw new EndOfStreamException(); + return BitConverter.ToDouble(bytes, 0); + } + + private static string DeserializeString(Stream stream, int length) + { + var bytes = new byte[length]; + var read = stream.Read(bytes, 0, length); + if (read < length) throw new EndOfStreamException(); + return Encoding.UTF8.GetString(bytes); + } + + private static byte[] DeserializeBinary(Stream stream, int length) + { + var bytes = new byte[length]; + var read = stream.Read(bytes, 0, length); + if (read < length) throw new EndOfStreamException(); + return bytes; + } + + private static List DeserializeArray(Stream stream, int count) + { + var list = new List(count); + for (int i = 0; i < count; i++) + { + list.Add(DeserializeValue(stream)); + } + return list; + } + + private static Dictionary DeserializeMap(Stream stream, int count) + { + var dict = new Dictionary(count); + for (int i = 0; i < count; i++) + { + var key = DeserializeValue(stream); + var value = DeserializeValue(stream); + dict[key?.ToString() ?? ""] = value; + } + return dict; + } + + private static T? ConvertValue(object? value) + { + if (value == null) + return default; + + if (value is T typedValue) + return typedValue; + + var targetType = typeof(T); + + if (targetType == typeof(string)) + { + return (T)(object)value.ToString()!; + } + + return (T)Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs b/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs new file mode 100644 index 0000000..2ab6e3d --- /dev/null +++ b/EasyTool.Core/ConvertCategory/TomlConvertUtil.cs @@ -0,0 +1,715 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ConvertCategory +{ + /// + /// TOML 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 TOML 序列化和反序列化 + /// + public static class TomlConvertUtil + { + #region 序列化 + + /// + /// 将对象序列化为 TOML 字符串 + /// + /// 对象类型 + /// 要序列化的对象 + /// TOML 字符串 + public static string Serialize(T obj) + { + var builder = new StringBuilder(); + SerializeObject(obj, builder, ""); + return builder.ToString(); + } + + /// + /// 将字典序列化为 TOML 字符串 + /// + /// 要序列化的字典 + /// TOML 字符串 + public static string SerializeDictionary(IDictionary dict) + { + var builder = new StringBuilder(); + SerializeDictionary(dict, builder, ""); + return builder.ToString(); + } + + private static void SerializeObject(object? obj, StringBuilder builder, string prefix) + { + if (obj == null) + return; + + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + // 先序列化简单属性 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (IsSimpleType(propType)) + { + builder.Append(prop.Name); + builder.Append(" = "); + SerializeValue(value, builder); + builder.AppendLine(); + } + } + + // 序列化数组和列表 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (IsArrayType(propType) && value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeArray(enumerable, builder, prop.Name); + } + } + + // 序列化嵌套表 + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + var propType = prop.PropertyType; + + if (!IsSimpleType(propType) && !IsArrayType(propType) && value != null) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? prop.Name : $"{prefix}.{prop.Name}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeObject(value, builder, tablePrefix); + } + } + } + + private static void SerializeDictionary(IDictionary dict, StringBuilder builder, string prefix) + { + // 先序列化简单值 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value == null || IsSimpleType(value.GetType())) + { + builder.Append(key); + builder.Append(" = "); + SerializeValue(value, builder); + builder.AppendLine(); + } + } + + // 序列化数组 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value is IEnumerable enumerable and not string and not IDictionary) + { + builder.AppendLine(); + SerializeArray(enumerable, builder, key); + } + } + + // 序列化嵌套字典 + foreach (DictionaryEntry entry in dict) + { + var key = entry.Key?.ToString() ?? ""; + var value = entry.Value; + + if (value is IDictionary nestedDict) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? key : $"{prefix}.{key}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeDictionary(nestedDict, builder, tablePrefix); + } + else if (value != null && !IsSimpleType(value.GetType()) && !IsArrayType(value.GetType())) + { + builder.AppendLine(); + var tablePrefix = string.IsNullOrEmpty(prefix) ? key : $"{prefix}.{key}"; + builder.AppendLine($"[{tablePrefix}]"); + SerializeObject(value, builder, tablePrefix); + } + } + } + + private static void SerializeValue(object? value, StringBuilder builder) + { + if (value == null) + { + builder.Append("\"\""); + return; + } + + var type = value.GetType(); + + if (type == typeof(bool)) + { + builder.Append((bool)value ? "true" : "false"); + } + else if (type == typeof(string)) + { + var str = (string)value; + if (str.Contains('\n') || str.Contains('\t') || str.Contains('"') || str.Contains('#')) + { + // 多行字符串使用字面量字符串 + builder.Append("'''"); + builder.Append(str); + builder.Append("'''"); + } + else + { + builder.Append($"\"{EscapeString(str)}\""); + } + } + else if (type == typeof(DateTime)) + { + builder.Append(((DateTime)value).ToString("o")); + } + else if (type == typeof(DateTimeOffset)) + { + builder.Append(((DateTimeOffset)value).ToString("o")); + } + else if (type == typeof(Guid)) + { + builder.Append($"\"{value}\""); + } + else if (type == typeof(decimal)) + { + builder.Append(((decimal)value).ToString(System.Globalization.CultureInfo.InvariantCulture)); + } + else if (type == typeof(float) || type == typeof(double)) + { + builder.Append(Convert.ToDouble(value).ToString(System.Globalization.CultureInfo.InvariantCulture)); + } + else + { + builder.Append(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)); + } + } + + private static void SerializeArray(IEnumerable enumerable, StringBuilder builder, string key) + { + foreach (var item in enumerable) + { + builder.Append(key); + builder.Append(" = ["); + + if (item == null) + { + builder.Append("]"); + } + else if (IsSimpleType(item.GetType())) + { + SerializeValue(item, builder); + builder.Append("]"); + } + else if (item is IDictionary dict) + { + builder.AppendLine(); + SerializeInlineTable(dict, builder); + builder.AppendLine(); + builder.Append("]"); + } + else + { + builder.AppendLine(); + SerializeInlineObject(item, builder); + builder.AppendLine(); + builder.Append("]"); + } + + builder.AppendLine(); + } + } + + private static void SerializeInlineTable(IDictionary dict, StringBuilder builder) + { + builder.Append("{ "); + var first = true; + foreach (DictionaryEntry entry in dict) + { + if (!first) + builder.Append(", "); + first = false; + + builder.Append(entry.Key?.ToString() ?? ""); + builder.Append(" = "); + SerializeValue(entry.Value, builder); + } + builder.Append(" }"); + } + + private static void SerializeInlineObject(object obj, StringBuilder builder) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + builder.Append("{ "); + var first = true; + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + if (!first) + builder.Append(", "); + first = false; + + builder.Append(prop.Name); + builder.Append(" = "); + SerializeValue(prop.GetValue(obj), builder); + } + builder.Append(" }"); + } + + private static string EscapeString(string value) + { + return value.Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\b", "\\b") + .Replace("\f", "\\f") + .Replace("\n", "\\n") + .Replace("\r", "\\r") + .Replace("\t", "\\t"); + } + + private static bool IsSimpleType(Type type) + { + return type.IsPrimitive || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(Guid) || + type == typeof(TimeSpan); + } + + private static bool IsArrayType(Type type) + { + return (type.IsArray || type.GetInterfaces().Contains(typeof(IList))) && type != typeof(string); + } + + #endregion + + #region 反序列化 + + /// + /// 将 TOML 字符串反序列化为字典 + /// + /// TOML 字符串 + /// 字典对象 + public static Dictionary Deserialize(string toml) + { + var result = new Dictionary(); + var currentTable = result; + var tables = new Stack>(); + tables.Push(result); + + using var reader = new StringReader(toml); + string? line; + + while ((line = reader.ReadLine()) != null) + { + line = line.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#")) + continue; + + // 表头 [table] 或 [table.subtable] + if (line.StartsWith("[") && line.EndsWith("]")) + { + var tableName = line[1..^1].Trim(); + currentTable = GetOrCreateTable(result, tableName); + continue; + } + + // 数组表 [[array]] + if (line.StartsWith("[[") && line.EndsWith("]]")) + { + var arrayName = line[2..^2].Trim(); + AddArrayTable(result, arrayName); + continue; + } + + // 键值对 + var equalsIndex = line.IndexOf('='); + if (equalsIndex > 0) + { + var key = line[..equalsIndex].Trim(); + var value = line[(equalsIndex + 1)..].Trim(); + currentTable[key] = ParseValue(value, reader); + } + } + + return result; + } + + /// + /// 将 TOML 字符串反序列化为指定类型 + /// + /// 目标类型 + /// TOML 字符串 + /// 反序列化的对象 + public static T? Deserialize(string toml) where T : class, new() + { + var dict = Deserialize(toml); + return MapToObject(dict); + } + + /// + /// 从文件加载 TOML 并反序列化为字典 + /// + /// 文件路径 + /// 字典对象 + public static Dictionary LoadFromFile(string filePath) + { + var toml = File.ReadAllText(filePath); + return Deserialize(toml); + } + + /// + /// 将字典保存为 TOML 文件 + /// + /// 字典对象 + /// 文件路径 + public static void SaveToFile(Dictionary dict, string filePath) + { + var toml = SerializeDictionary(dict); + File.WriteAllText(filePath, toml); + } + + private static Dictionary GetOrCreateTable(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + foreach (var part in parts) + { + if (!current.TryGetValue(part, out var value) || !(value is Dictionary nested)) + { + nested = new Dictionary(); + current[part] = nested; + } + current = nested; + } + + return current; + } + + private static void AddArrayTable(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + for (int i = 0; i < parts.Length - 1; i++) + { + if (!current.TryGetValue(parts[i], out var value) || !(value is Dictionary nested)) + { + nested = new Dictionary(); + current[parts[i]] = nested; + } + current = nested; + } + + var lastPart = parts[^1]; + if (!current.TryGetValue(lastPart, out var arrayValue) || !(arrayValue is List> array)) + { + array = new List>(); + current[lastPart] = array; + } + + var newTable = new Dictionary(); + array.Add(newTable); + } + + private static object? ParseValue(string value, StringReader reader) + { + value = value.Trim(); + + // 字符串 + if (value.StartsWith("\"") && value.EndsWith("\"")) + { + return UnescapeString(value[1..^1]); + } + if (value.StartsWith("'") && value.EndsWith("'")) + { + return value[1..^1]; + } + if (value.StartsWith("'''") || value.StartsWith("\"\"\"")) + { + return ParseMultiLineString(value, reader); + } + + // 布尔值 + if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) + return true; + if (value.Equals("false", StringComparison.OrdinalIgnoreCase)) + return false; + + // 数组 + if (value.StartsWith("[") && value.EndsWith("]")) + { + return ParseArray(value[1..^1]); + } + + // 内联表 + if (value.StartsWith("{") && value.EndsWith("}")) + { + return ParseInlineTable(value[1..^1]); + } + + // 数字 + if (int.TryParse(value, out var intVal)) + return intVal; + if (long.TryParse(value, out var longVal)) + return longVal; + if (double.TryParse(value, out var doubleVal)) + return doubleVal; + + // 日期时间 + if (DateTime.TryParse(value, out var dateVal)) + return dateVal; + + return value; + } + + private static string ParseMultiLineString(string start, StringReader reader) + { + var delimiter = start.Substring(0, 3); + var sb = new StringBuilder(); + + // 处理开始行的剩余内容 + if (start.Length > 3) + { + sb.Append(start[3..]); + } + + string? line; + while ((line = reader.ReadLine()) != null) + { + if (line.Contains(delimiter)) + { + var endIndex = line.IndexOf(delimiter); + sb.Append(line[..endIndex]); + break; + } + sb.AppendLine(line); + } + + return sb.ToString(); + } + + private static List ParseArray(string content) + { + var result = new List(); + var items = SplitArrayItems(content); + + foreach (var item in items) + { + result.Add(ParseValue(item.Trim(), null!)); + } + + return result; + } + + private static Dictionary ParseInlineTable(string content) + { + var result = new Dictionary(); + var pairs = SplitKeyValuePairs(content); + + foreach (var pair in pairs) + { + var equalsIndex = pair.IndexOf('='); + if (equalsIndex > 0) + { + var key = pair[..equalsIndex].Trim(); + var value = pair[(equalsIndex + 1)..].Trim(); + result[key] = ParseValue(value, null!); + } + } + + return result; + } + + private static List SplitArrayItems(string content) + { + var items = new List(); + var current = new StringBuilder(); + var depth = 0; + var inString = false; + var stringChar = '\0'; + + foreach (var c in content) + { + if (inString) + { + current.Append(c); + if (c == stringChar) + inString = false; + } + else if (c == '"' || c == '\'') + { + inString = true; + stringChar = c; + current.Append(c); + } + else if (c == '[' || c == '{') + { + depth++; + current.Append(c); + } + else if (c == ']' || c == '}') + { + depth--; + current.Append(c); + } + else if (c == ',' && depth == 0) + { + items.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + if (current.Length > 0) + items.Add(current.ToString()); + + return items; + } + + private static List SplitKeyValuePairs(string content) + { + var pairs = new List(); + var current = new StringBuilder(); + var depth = 0; + var inString = false; + var stringChar = '\0'; + + foreach (var c in content) + { + if (inString) + { + current.Append(c); + if (c == stringChar) + inString = false; + } + else if (c == '"' || c == '\'') + { + inString = true; + stringChar = c; + current.Append(c); + } + else if (c == '[' || c == '{') + { + depth++; + current.Append(c); + } + else if (c == ']' || c == '}') + { + depth--; + current.Append(c); + } + else if (c == ',' && depth == 0) + { + pairs.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + if (current.Length > 0) + pairs.Add(current.ToString()); + + return pairs; + } + + private static string UnescapeString(string value) + { + return value.Replace("\\b", "\b") + .Replace("\\f", "\f") + .Replace("\\n", "\n") + .Replace("\\r", "\r") + .Replace("\\t", "\t") + .Replace("\\\"", "\"") + .Replace("\\\\", "\\"); + } + + private static T? MapToObject(Dictionary dict) where T : class, new() + { + if (dict == null) + return null; + + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var prop = type.GetProperty(kvp.Key, + System.Reflection.BindingFlags.Public | + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.IgnoreCase); + + if (prop != null && prop.CanWrite) + { + var value = ConvertValue(kvp.Value, prop.PropertyType); + prop.SetValue(obj, value); + } + } + + return obj; + } + + private static object? ConvertValue(object? value, Type targetType) + { + if (value == null) + return null; + + var sourceType = value.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + return value; + + if (value is Dictionary dict && !targetType.IsPrimitive) + { + var method = typeof(TomlConvertUtil).GetMethod(nameof(MapToObject), + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) + ?.MakeGenericMethod(targetType); + return method?.Invoke(null, new object[] { dict }); + } + + return Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs b/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs new file mode 100644 index 0000000..eea4dc6 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/UnitConvertUtil.cs @@ -0,0 +1,436 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ConvertCategory +{ + /// + /// 单位转换工具类 + /// 提供长度、重量、温度、面积、体积等常用单位转换 + /// + public static class UnitConvertUtil + { + #region 长度 + + /// + /// 长度单位 + /// + public enum LengthUnit + { + Millimeter, Centimeter, Meter, Kilometer, + Inch, Foot, Yard, Mile, + Nanometer, Micrometer, Decimeter + } + + private static readonly Dictionary LengthToMeter = new() + { + { LengthUnit.Nanometer, 1e-9 }, + { LengthUnit.Micrometer, 1e-6 }, + { LengthUnit.Millimeter, 0.001 }, + { LengthUnit.Centimeter, 0.01 }, + { LengthUnit.Decimeter, 0.1 }, + { LengthUnit.Meter, 1 }, + { LengthUnit.Kilometer, 1000 }, + { LengthUnit.Inch, 0.0254 }, + { LengthUnit.Foot, 0.3048 }, + { LengthUnit.Yard, 0.9144 }, + { LengthUnit.Mile, 1609.344 } + }; + + /// + /// 长度转换 + /// + public static double ConvertLength(double value, LengthUnit from, LengthUnit to) + { + double meters = value * LengthToMeter[from]; + return meters / LengthToMeter[to]; + } + + /// + /// 获取所有可转换的长度单位 + /// + public static LengthUnit[] GetLengthUnits() => (LengthUnit[])Enum.GetValues(typeof(LengthUnit)); + + #endregion + + #region 重量 + + /// + /// 重量单位 + /// + public enum WeightUnit + { + Milligram, Gram, Kilogram, Ton, + Ounce, Pound, Jin, Liang + } + + private static readonly Dictionary WeightToGram = new() + { + { WeightUnit.Milligram, 0.001 }, + { WeightUnit.Gram, 1 }, + { WeightUnit.Kilogram, 1000 }, + { WeightUnit.Ton, 1000000 }, + { WeightUnit.Ounce, 28.349523125 }, + { WeightUnit.Pound, 453.59237 }, + { WeightUnit.Jin, 500 }, // 市斤 + { WeightUnit.Liang, 50 } // 市两 + }; + + /// + /// 重量转换 + /// + public static double ConvertWeight(double value, WeightUnit from, WeightUnit to) + { + double grams = value * WeightToGram[from]; + return grams / WeightToGram[to]; + } + + #endregion + + #region 温度 + + /// + /// 温度单位 + /// + public enum TemperatureUnit + { + Celsius, Fahrenheit, Kelvin + } + + /// + /// 温度转换 + /// + public static double ConvertTemperature(double value, TemperatureUnit from, TemperatureUnit to) + { + // 先转换为摄氏度 + double celsius = from switch + { + TemperatureUnit.Celsius => value, + TemperatureUnit.Fahrenheit => (value - 32) * 5 / 9, + TemperatureUnit.Kelvin => value - 273.15, + _ => throw new ArgumentException("Invalid temperature unit") + }; + + // 再从摄氏度转换为目标单位 + return to switch + { + TemperatureUnit.Celsius => celsius, + TemperatureUnit.Fahrenheit => celsius * 9 / 5 + 32, + TemperatureUnit.Kelvin => celsius + 273.15, + _ => throw new ArgumentException("Invalid temperature unit") + }; + } + + #endregion + + #region 面积 + + /// + /// 面积单位 + /// + public enum AreaUnit + { + SquareMillimeter, SquareCentimeter, SquareMeter, SquareKilometer, + SquareInch, SquareFoot, SquareYard, SquareMile, + Hectare, Acre, Mu + } + + private static readonly Dictionary AreaToSquareMeter = new() + { + { AreaUnit.SquareMillimeter, 0.000001 }, + { AreaUnit.SquareCentimeter, 0.0001 }, + { AreaUnit.SquareMeter, 1 }, + { AreaUnit.SquareKilometer, 1000000 }, + { AreaUnit.SquareInch, 0.00064516 }, + { AreaUnit.SquareFoot, 0.09290304 }, + { AreaUnit.SquareYard, 0.83612736 }, + { AreaUnit.SquareMile, 2589988.110336 }, + { AreaUnit.Hectare, 10000 }, + { AreaUnit.Acre, 4046.8564224 }, + { AreaUnit.Mu, 666.66666666667 } // 市亩 + }; + + /// + /// 面积转换 + /// + public static double ConvertArea(double value, AreaUnit from, AreaUnit to) + { + double sqMeters = value * AreaToSquareMeter[from]; + return sqMeters / AreaToSquareMeter[to]; + } + + #endregion + + #region 体积 + + /// + /// 体积单位 + /// + public enum VolumeUnit + { + CubicMillimeter, CubicCentimeter, CubicMeter, CubicKilometer, + Milliliter, Liter, + CubicInch, CubicFoot, CubicYard, + GallonUS, GallonUK, PintUS, PintUK, + FluidOunceUS, FluidOunceUK + } + + private static readonly Dictionary VolumeToLiter = new() + { + { VolumeUnit.CubicMillimeter, 0.000001 }, + { VolumeUnit.CubicCentimeter, 0.001 }, + { VolumeUnit.CubicMeter, 1000 }, + { VolumeUnit.CubicKilometer, 1e12 }, + { VolumeUnit.Milliliter, 0.001 }, + { VolumeUnit.Liter, 1 }, + { VolumeUnit.CubicInch, 0.016387064 }, + { VolumeUnit.CubicFoot, 28.316846592 }, + { VolumeUnit.CubicYard, 764.554857984 }, + { VolumeUnit.GallonUS, 3.785411784 }, + { VolumeUnit.GallonUK, 4.54609 }, + { VolumeUnit.PintUS, 0.473176473 }, + { VolumeUnit.PintUK, 0.56826125 }, + { VolumeUnit.FluidOunceUS, 0.0295735295625 }, + { VolumeUnit.FluidOunceUK, 0.0284130625 } + }; + + /// + /// 体积转换 + /// + public static double ConvertVolume(double value, VolumeUnit from, VolumeUnit to) + { + double liters = value * VolumeToLiter[from]; + return liters / VolumeToLiter[to]; + } + + #endregion + + #region 速度 + + /// + /// 速度单位 + /// + public enum SpeedUnit + { + MeterPerSecond, KilometerPerHour, MilePerHour, + Knot, FootPerSecond + } + + private static readonly Dictionary SpeedToMps = new() + { + { SpeedUnit.MeterPerSecond, 1 }, + { SpeedUnit.KilometerPerHour, 1000.0 / 3600 }, + { SpeedUnit.MilePerHour, 0.44704 }, + { SpeedUnit.Knot, 0.514444444 }, + { SpeedUnit.FootPerSecond, 0.3048 } + }; + + /// + /// 速度转换 + /// + public static double ConvertSpeed(double value, SpeedUnit from, SpeedUnit to) + { + double mps = value * SpeedToMps[from]; + return mps / SpeedToMps[to]; + } + + #endregion + + #region 时间 + + /// + /// 时间单位 + /// + public enum TimeUnit + { + Millisecond, Second, Minute, Hour, Day, Week, + Month, Year, Decade, Century + } + + private static readonly Dictionary TimeToSecond = new() + { + { TimeUnit.Millisecond, 0.001 }, + { TimeUnit.Second, 1 }, + { TimeUnit.Minute, 60 }, + { TimeUnit.Hour, 3600 }, + { TimeUnit.Day, 86400 }, + { TimeUnit.Week, 604800 }, + { TimeUnit.Month, 2629746 }, // 平均月份 + { TimeUnit.Year, 31556952 }, // 平均年 + { TimeUnit.Decade, 315569520 }, + { TimeUnit.Century, 3155695200 } + }; + + /// + /// 时间转换 + /// + public static double ConvertTime(double value, TimeUnit from, TimeUnit to) + { + double seconds = value * TimeToSecond[from]; + return seconds / TimeToSecond[to]; + } + + #endregion + + #region 压力 + + /// + /// 压力单位 + /// + public enum PressureUnit + { + Pascal, Kilopascal, Megapascal, Bar, + Psi, Atm, Torr, MmHg + } + + private static readonly Dictionary PressureToPascal = new() + { + { PressureUnit.Pascal, 1 }, + { PressureUnit.Kilopascal, 1000 }, + { PressureUnit.Megapascal, 1000000 }, + { PressureUnit.Bar, 100000 }, + { PressureUnit.Psi, 6894.757293168 }, + { PressureUnit.Atm, 101325 }, + { PressureUnit.Torr, 133.3223684211 }, + { PressureUnit.MmHg, 133.322 } + }; + + /// + /// 压力转换 + /// + public static double ConvertPressure(double value, PressureUnit from, PressureUnit to) + { + double pascals = value * PressureToPascal[from]; + return pascals / PressureToPascal[to]; + } + + #endregion + + #region 角度 + + /// + /// 角度单位 + /// + public enum AngleUnit + { + Degree, Radian, Gradian, Turn + } + + /// + /// 角度转换 + /// + public static double ConvertAngle(double value, AngleUnit from, AngleUnit to) + { + // 先转换为度 + double degrees = from switch + { + AngleUnit.Degree => value, + AngleUnit.Radian => value * 180 / Math.PI, + AngleUnit.Gradian => value * 0.9, + AngleUnit.Turn => value * 360, + _ => throw new ArgumentException("Invalid angle unit") + }; + + // 再从度转换为目标单位 + return to switch + { + AngleUnit.Degree => degrees, + AngleUnit.Radian => degrees * Math.PI / 180, + AngleUnit.Gradian => degrees / 0.9, + AngleUnit.Turn => degrees / 360, + _ => throw new ArgumentException("Invalid angle unit") + }; + } + + #endregion + + #region 数据大小 + + /// + /// 数据大小单位 + /// + public enum DataUnit + { + Bit, Byte, + Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, + Kibibyte, Mebibyte, Gibibyte, Tebibyte, Pebibyte + } + + private static readonly Dictionary DataToByte = new() + { + { DataUnit.Bit, 0.125 }, + { DataUnit.Byte, 1 }, + { DataUnit.Kilobyte, 1000 }, + { DataUnit.Megabyte, 1000000 }, + { DataUnit.Gigabyte, 1e9 }, + { DataUnit.Terabyte, 1e12 }, + { DataUnit.Petabyte, 1e15 }, + { DataUnit.Kibibyte, 1024 }, + { DataUnit.Mebibyte, 1048576 }, + { DataUnit.Gibibyte, 1073741824 }, + { DataUnit.Tebibyte, 1099511627776 }, + { DataUnit.Pebibyte, 1125899906842624 } + }; + + /// + /// 数据大小转换 + /// + public static double ConvertData(double value, DataUnit from, DataUnit to) + { + double bytes = value * DataToByte[from]; + return bytes / DataToByte[to]; + } + + /// + /// 自动格式化数据大小 + /// + public static string FormatDataSize(double bytes) + { + string[] units = { "B", "KB", "MB", "GB", "TB", "PB" }; + int unitIndex = 0; + + while (bytes >= 1024 && unitIndex < units.Length - 1) + { + bytes /= 1024; + unitIndex++; + } + + return $"{bytes:F2} {units[unitIndex]}"; + } + + #endregion + + #region 能量 + + /// + /// 能量单位 + /// + public enum EnergyUnit + { + Joule, Kilojoule, Megajoule, Calorie, Kilocalorie, + WattHour, KilowattHour, BritishThermalUnit + } + + private static readonly Dictionary EnergyToJoule = new() + { + { EnergyUnit.Joule, 1 }, + { EnergyUnit.Kilojoule, 1000 }, + { EnergyUnit.Megajoule, 1000000 }, + { EnergyUnit.Calorie, 4.184 }, + { EnergyUnit.Kilocalorie, 4184 }, + { EnergyUnit.WattHour, 3600 }, + { EnergyUnit.KilowattHour, 3600000 }, + { EnergyUnit.BritishThermalUnit, 1055.06 } + }; + + /// + /// 能量转换 + /// + public static double ConvertEnergy(double value, EnergyUnit from, EnergyUnit to) + { + double joules = value * EnergyToJoule[from]; + return joules / EnergyToJoule[to]; + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs b/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs new file mode 100644 index 0000000..b1aadb8 --- /dev/null +++ b/EasyTool.Core/ConvertCategory/XmlConvertUtil.cs @@ -0,0 +1,281 @@ +using System; +using System.Collections.Generic; +using System.Xml; +using System.Xml.Serialization; +using System.IO; +using System.Text; +using System.Xml.Linq; +using System.Xml.XPath; + +namespace EasyTool.ConvertCategory +{ + /// + /// XML转换工具类 + /// + public static class XmlConvertUtil + { + #region 对象序列化 + + /// + /// 对象序列化为XML字符串 + /// + public static string ToXml(T obj, bool indent = true, bool omitXmlDeclaration = false) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + + var serializer = new XmlSerializer(typeof(T)); + var settings = new XmlWriterSettings + { + Indent = indent, + OmitXmlDeclaration = omitXmlDeclaration, + Encoding = Encoding.UTF8 + }; + + using var writer = new StringWriter(); + using var xmlWriter = XmlWriter.Create(writer, settings); + serializer.Serialize(xmlWriter, obj); + return writer.ToString(); + } + + /// + /// XML字符串反序列化为对象 + /// + public static T? FromXml(string xml) + { + if (string.IsNullOrWhiteSpace(xml)) + return default; + + var serializer = new XmlSerializer(typeof(T)); + using var reader = new StringReader(xml); + return (T?)serializer.Deserialize(reader); + } + + /// + /// 对象序列化为XML文件 + /// + public static void ToXmlFile(T obj, string filePath, bool indent = true) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var serializer = new XmlSerializer(typeof(T)); + var settings = new XmlWriterSettings + { + Indent = indent, + Encoding = Encoding.UTF8 + }; + + using var writer = XmlWriter.Create(filePath, settings); + serializer.Serialize(writer, obj); + } + + /// + /// XML文件反序列化为对象 + /// + public static T? FromXmlFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + var serializer = new XmlSerializer(typeof(T)); + using var reader = XmlReader.Create(filePath); + return (T?)serializer.Deserialize(reader); + } + + #endregion + + #region 字典转换 + + /// + /// 字典转XML + /// + public static string DictionaryToXml(Dictionary dict, string rootName = "root", string itemName = "item") + { + var doc = new XDocument(new XElement(rootName)); + var root = doc.Root!; + + foreach (var kvp in dict) + { + root.Add(new XElement(itemName, + new XAttribute("key", kvp.Key), + new XAttribute("value", kvp.Value))); + } + + return doc.ToString(); + } + + /// + /// XML转字典 + /// + public static Dictionary XmlToDictionary(string xml, string itemName = "item") + { + var dict = new Dictionary(); + var doc = XDocument.Parse(xml); + + foreach (var element in doc.Descendants(itemName)) + { + var key = element.Attribute("key")?.Value; + var value = element.Attribute("value")?.Value; + if (key != null) + dict[key] = value ?? ""; + } + + return dict; + } + + #endregion + + #region 列表转换 + + /// + /// 列表转XML + /// + public static string ListToXml(List list, string rootName = "root", string itemName = "item") + { + var doc = new XDocument(new XElement(rootName)); + var root = doc.Root!; + + foreach (var item in list) + { + root.Add(new XElement(itemName, item?.ToString())); + } + + return doc.ToString(); + } + + /// + /// XML转列表 + /// + public static List XmlToList(string xml, string itemName = "item") + { + var list = new List(); + var doc = XDocument.Parse(xml); + + foreach (var element in doc.Descendants(itemName)) + { + list.Add(element.Value); + } + + return list; + } + + #endregion + + #region 格式化 + + /// + /// 格式化XML + /// + public static string FormatXml(string xml, string indent = " ") + { + var doc = XDocument.Parse(xml); + return doc.ToString(); + } + + /// + /// 压缩XML(移除空白) + /// + public static string MinifyXml(string xml) + { + var doc = XDocument.Parse(xml); + return doc.ToString(SaveOptions.DisableFormatting); + } + + /// + /// 验证XML格式 + /// + public static bool IsValidXml(string xml) + { + try + { + XDocument.Parse(xml); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region XPath查询 + + /// + /// XPath查询 + /// + public static List SelectNodes(string xml, string xpath) + { + var results = new List(); + var doc = XDocument.Parse(xml); + var nodes = doc.XPathSelectElements(xpath); + + foreach (var node in nodes) + { + results.Add(node.Value); + } + + return results; + } + + /// + /// XPath查询单个节点 + /// + public static string? SelectSingleNode(string xml, string xpath) + { + var doc = XDocument.Parse(xml); + var node = doc.XPathSelectElement(xpath); + return node?.Value; + } + + #endregion + + #region 节点操作 + + /// + /// 获取节点值 + /// + public static string? GetNodeValue(string xml, string nodeName) + { + var doc = XDocument.Parse(xml); + return doc.Root?.Element(nodeName)?.Value; + } + + /// + /// 设置节点值 + /// + public static string SetNodeValue(string xml, string nodeName, string value) + { + var doc = XDocument.Parse(xml); + var node = doc.Root?.Element(nodeName); + if (node != null) + node.Value = value; + return doc.ToString(); + } + + /// + /// 获取属性值 + /// + public static string? GetAttributeValue(string xml, string nodeName, string attributeName) + { + var doc = XDocument.Parse(xml); + return doc.Root?.Element(nodeName)?.Attribute(attributeName)?.Value; + } + + /// + /// 设置属性值 + /// + public static string SetAttributeValue(string xml, string nodeName, string attributeName, string value) + { + var doc = XDocument.Parse(xml); + var node = doc.Root?.Element(nodeName); + if (node != null) + node.SetAttributeValue(attributeName, value); + return doc.ToString(); + } + + #endregion + } +} diff --git a/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs b/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs new file mode 100644 index 0000000..2be1a4a --- /dev/null +++ b/EasyTool.Core/ConvertCategory/YamlConvertUtil.cs @@ -0,0 +1,523 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ConvertCategory +{ + /// + /// YAML 转换工具类(轻量级实现,无需第三方库) + /// 支持基本的 YAML 序列化和反序列化 + /// + public static class YamlConvertUtil + { + private const int DefaultIndent = 2; + + #region 序列化 + + /// + /// 将对象序列化为 YAML 字符串 + /// + /// 对象类型 + /// 要序列化的对象 + /// 缩进空格数 + /// YAML 字符串 + public static string Serialize(T obj, int indent = DefaultIndent) + { + var builder = new StringBuilder(); + SerializeValue(obj, builder, 0, indent); + return builder.ToString(); + } + + /// + /// 将字典序列化为 YAML 字符串 + /// + /// 要序列化的字典 + /// 缩进空格数 + /// YAML 字符串 + public static string SerializeDictionary(IDictionary dict, int indent = DefaultIndent) + { + var builder = new StringBuilder(); + SerializeDictionary(dict, builder, 0, indent); + return builder.ToString(); + } + + private static void SerializeValue(object? value, StringBuilder builder, int level, int indent) + { + if (value == null) + { + builder.Append("null"); + return; + } + + var type = value.GetType(); + + if (type.IsPrimitive || value is decimal || value is DateTime || value is DateTimeOffset || value is Guid) + { + SerializeScalar(value, builder); + } + else if (value is string str) + { + SerializeString(str, builder); + } + else if (value is IDictionary dict) + { + SerializeDictionary(dict, builder, level, indent); + } + else if (value is IEnumerable enumerable and not string) + { + SerializeEnumerable(enumerable, builder, level, indent); + } + else + { + SerializeObject(value, builder, level, indent); + } + } + + private static void SerializeScalar(object value, StringBuilder builder) + { + var type = value.GetType(); + + if (type == typeof(bool)) + { + builder.Append((bool)value ? "true" : "false"); + } + else if (type == typeof(DateTime)) + { + builder.Append(((DateTime)value).ToString("o")); + } + else if (type == typeof(DateTimeOffset)) + { + builder.Append(((DateTimeOffset)value).ToString("o")); + } + else if (type == typeof(Guid)) + { + builder.Append(((Guid)value).ToString()); + } + else + { + builder.Append(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)); + } + } + + private static void SerializeString(string value, StringBuilder builder) + { + if (string.IsNullOrEmpty(value)) + { + builder.Append("\"\""); + return; + } + + // 检查是否需要引号 + var needsQuotes = value.Contains('\n') || + value.Contains('\t') || + value.Contains(':') || + value.Contains('#') || + value.StartsWith(" ") || + value.EndsWith(" ") || + value.StartsWith("\"") || + value.StartsWith("'") || + IsNumeric(value); + + if (needsQuotes) + { + // 多行字符串 + if (value.Contains('\n')) + { + builder.AppendLine("|"); + var lines = value.Split('\n'); + foreach (var line in lines) + { + builder.AppendLine($" {line}"); + } + } + else + { + builder.Append($"\"{EscapeString(value)}\""); + } + } + else + { + builder.Append(value); + } + } + + private static string EscapeString(string value) + { + return value.Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\n", "\\n") + .Replace("\r", "\\r") + .Replace("\t", "\\t"); + } + + private static bool IsNumeric(string value) + { + return double.TryParse(value, out _); + } + + private static void SerializeDictionary(IDictionary dict, StringBuilder builder, int level, int indent) + { + var first = true; + foreach (DictionaryEntry entry in dict) + { + if (!first) + { + builder.AppendLine(); + } + first = false; + + builder.Append(new string(' ', level * indent)); + builder.Append(entry.Key?.ToString() ?? "null"); + builder.Append(':'); + + if (entry.Value == null) + { + builder.Append(" null"); + } + else if (entry.Value is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (entry.Value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(enumerable, builder, level + 1, indent); + } + else + { + builder.Append(' '); + SerializeValue(entry.Value, builder, level + 1, indent); + } + } + } + + private static void SerializeEnumerable(IEnumerable enumerable, StringBuilder builder, int level, int indent) + { + foreach (var item in enumerable) + { + builder.AppendLine(); + builder.Append(new string(' ', level * indent)); + builder.Append("- "); + + if (item == null) + { + builder.Append("null"); + } + else if (item is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (item is IEnumerable nestedEnumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(nestedEnumerable, builder, level + 1, indent); + } + else + { + SerializeValue(item, builder, level + 1, indent); + } + } + } + + private static void SerializeObject(object obj, StringBuilder builder, int level, int indent) + { + var type = obj.GetType(); + var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); + + var first = true; + foreach (var prop in properties) + { + if (!prop.CanRead) + continue; + + var value = prop.GetValue(obj); + if (!first) + { + builder.AppendLine(); + } + first = false; + + builder.Append(new string(' ', level * indent)); + builder.Append(prop.Name); + builder.Append(':'); + + if (value == null) + { + builder.Append(" null"); + } + else if (value is IDictionary nestedDict) + { + builder.AppendLine(); + SerializeDictionary(nestedDict, builder, level + 1, indent); + } + else if (value is IEnumerable enumerable and not string) + { + builder.AppendLine(); + SerializeEnumerable(enumerable, builder, level + 1, indent); + } + else + { + builder.Append(' '); + SerializeValue(value, builder, level + 1, indent); + } + } + } + + #endregion + + #region 反序列化 + + /// + /// 将 YAML 字符串反序列化为字典 + /// + /// YAML 字符串 + /// 字典对象 + public static Dictionary Deserialize(string yaml) + { + var reader = new StringReader(yaml); + return ParseYaml(reader); + } + + /// + /// 将 YAML 字符串反序列化为指定类型 + /// + /// 目标类型 + /// YAML 字符串 + /// 反序列化的对象 + public static T? Deserialize(string yaml) where T : class, new() + { + var dict = Deserialize(yaml); + return MapToObject(dict); + } + + /// + /// 从文件加载 YAML 并反序列化为字典 + /// + /// 文件路径 + /// 字典对象 + public static Dictionary LoadFromFile(string filePath) + { + var yaml = File.ReadAllText(filePath); + return Deserialize(yaml); + } + + /// + /// 将字典保存为 YAML 文件 + /// + /// 字典对象 + /// 文件路径 + public static void SaveToFile(Dictionary dict, string filePath) + { + var yaml = SerializeDictionary(dict); + File.WriteAllText(filePath, yaml); + } + + private static Dictionary ParseYaml(StringReader reader) + { + var result = new Dictionary(); + var lines = new List(); + + string? line; + while ((line = reader.ReadLine()) != null) + { + lines.Add(line); + } + + ParseLines(lines, 0, lines.Count, 0, result); + return result; + } + + private static int ParseLines(List lines, int start, int end, int baseIndent, Dictionary result) + { + var i = start; + + while (i < end) + { + var line = lines[i]; + + // 跳过空行和注释 + if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) + { + i++; + continue; + } + + var indent = GetIndent(line); + + // 检查是否是列表项 + if (line.TrimStart().StartsWith("- ")) + { + // 解析列表 + var list = new List(); + while (i < end) + { + var currentLine = lines[i]; + var currentIndent = GetIndent(currentLine); + + if (currentIndent < indent) + break; + + if (currentLine.TrimStart().StartsWith("- ")) + { + var value = currentLine.TrimStart()[2..].Trim(); + if (string.IsNullOrEmpty(value)) + { + // 值在下一行(嵌套对象) + i++; + var nestedDict = new Dictionary(); + i = ParseLines(lines, i, end, currentIndent + 2, nestedDict); + list.Add(nestedDict); + } + else + { + list.Add(ParseValue(value)); + i++; + } + } + else + { + break; + } + } + return i; + } + + // 解析键值对 + var colonIndex = line.IndexOf(':'); + if (colonIndex > 0) + { + var key = line[..colonIndex].Trim(); + var value = line[(colonIndex + 1)..].Trim(); + + if (string.IsNullOrEmpty(value)) + { + // 值在下一行(嵌套对象) + i++; + var nestedDict = new Dictionary(); + i = ParseLines(lines, i, end, indent + 2, nestedDict); + result[key] = nestedDict; + } + else + { + result[key] = ParseValue(value); + i++; + } + } + else + { + i++; + } + } + + return i; + } + + private static int GetIndent(string line) + { + for (int i = 0; i < line.Length; i++) + { + if (line[i] != ' ') + return i; + } + return line.Length; + } + + private static object? ParseValue(string value) + { + if (string.IsNullOrEmpty(value)) + return null; + + value = value.Trim(); + + // 处理引号字符串 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + return value[1..^1]; + } + + // null + if (value.Equals("null", StringComparison.OrdinalIgnoreCase) || + value.Equals("~")) + { + return null; + } + + // boolean + if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) + return true; + if (value.Equals("false", StringComparison.OrdinalIgnoreCase)) + return false; + + // 数字 + if (int.TryParse(value, out var intVal)) + return intVal; + if (long.TryParse(value, out var longVal)) + return longVal; + if (double.TryParse(value, out var doubleVal)) + return doubleVal; + + // 日期时间 + if (DateTime.TryParse(value, out var dateVal)) + return dateVal; + + return value; + } + + private static T? MapToObject(Dictionary dict) where T : class, new() + { + if (dict == null) + return null; + + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var prop = type.GetProperty(kvp.Key, + System.Reflection.BindingFlags.Public | + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.IgnoreCase); + + if (prop != null && prop.CanWrite) + { + var value = ConvertValue(kvp.Value, prop.PropertyType); + prop.SetValue(obj, value); + } + } + + return obj; + } + + private static object? ConvertValue(object? value, Type targetType) + { + if (value == null) + return null; + + var sourceType = value.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + return value; + + // 处理字典到对象的映射 + if (value is Dictionary dict && !targetType.IsPrimitive) + { + var method = typeof(YamlConvertUtil).GetMethod(nameof(MapToObject), + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) + ?.MakeGenericMethod(targetType); + return method?.Invoke(null, new object[] { dict }); + } + + // 基本类型转换 + return Convert.ChangeType(value, targetType); + } + + #endregion + } +} diff --git a/EasyTool.Core/DatabaseCategory/ConnectionPool.cs b/EasyTool.Core/DatabaseCategory/ConnectionPool.cs new file mode 100644 index 0000000..149242d --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/ConnectionPool.cs @@ -0,0 +1,444 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DatabaseCategory +{ + /// + /// 数据库连接池选项 + /// + public class ConnectionPoolOptions + { + /// + /// 最小连接数 + /// + public int MinPoolSize { get; set; } = 5; + + /// + /// 最大连接数 + /// + public int MaxPoolSize { get; set; } = 100; + + /// + /// 连接超时时间 + /// + public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 连接最大空闲时间 + /// + public TimeSpan MaxIdleTime { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 连接最大生存时间 + /// + public TimeSpan MaxLifetime { get; set; } = TimeSpan.FromHours(8); + + /// + /// 健康检查间隔 + /// + public TimeSpan HealthCheckInterval { get; set; } = TimeSpan.FromMinutes(1); + + /// + /// 获取连接重试次数 + /// + public int RetryCount { get; set; } = 3; + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromMilliseconds(100); + } + + /// + /// 池化连接包装器 + /// + internal class PooledConnection : IDisposable + { + public DbConnection Connection { get; } + public DateTime CreateTime { get; } + public DateTime LastAccessTime { get; set; } + public bool IsInUse { get; set; } + public bool IsValid { get; set; } = true; + + public PooledConnection(DbConnection connection) + { + Connection = connection; + CreateTime = DateTime.UtcNow; + LastAccessTime = DateTime.UtcNow; + } + + public void Dispose() + { + Connection?.Dispose(); + } + } + + /// + /// 数据库连接池 + /// 提供高效的数据库连接管理和复用 + /// + public class ConnectionPool : IAsyncDisposable, IDisposable + { + private readonly string _connectionString; + private readonly DbProviderFactory _providerFactory; + private readonly ConnectionPoolOptions _options; + private readonly ConcurrentBag _pool; + private readonly SemaphoreSlim _semaphore; + private readonly Timer _healthCheckTimer; + private readonly Timer _cleanupTimer; + private int _totalConnections; + private bool _disposed; + + /// + /// 当前池中连接数 + /// + public int PoolSize => _totalConnections; + + /// + /// 可用连接数 + /// + public int AvailableConnections => _pool.Count; + + /// + /// 正在使用的连接数 + /// + public int InUseConnections => _totalConnections - _pool.Count; + + /// + /// 创建数据库连接池 + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 连接池选项 + public ConnectionPool( + string connectionString, + DbProviderFactory providerFactory, + ConnectionPoolOptions? options = null) + { + _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); + _providerFactory = providerFactory ?? throw new ArgumentNullException(nameof(providerFactory)); + _options = options ?? new ConnectionPoolOptions(); + _pool = new ConcurrentBag(); + _semaphore = new SemaphoreSlim(_options.MaxPoolSize, _options.MaxPoolSize); + + // 初始化最小连接数 + InitializeMinConnections(); + + // 启动健康检查定时器 + _healthCheckTimer = new Timer(HealthCheck, null, + _options.HealthCheckInterval, _options.HealthCheckInterval); + + // 启动清理定时器 + _cleanupTimer = new Timer(CleanupIdleConnections, null, + _options.MaxIdleTime, _options.MaxIdleTime); + } + + private void InitializeMinConnections() + { + for (int i = 0; i < _options.MinPoolSize; i++) + { + var connection = CreateNewConnection(); + if (connection != null) + { + _pool.Add(connection); + Interlocked.Increment(ref _totalConnections); + } + } + } + + private PooledConnection? CreateNewConnection() + { + try + { + var connection = _providerFactory.CreateConnection(); + if (connection == null) + return null; + + connection.ConnectionString = _connectionString; + connection.Open(); + return new PooledConnection(connection); + } + catch + { + return null; + } + } + + /// + /// 获取连接 + /// + /// 取消令牌 + /// 数据库连接 + public async Task GetConnectionAsync(CancellationToken cancellationToken = default) + { + for (int retry = 0; retry < _options.RetryCount; retry++) + { + if (await _semaphore.WaitAsync(_options.ConnectionTimeout, cancellationToken)) + { + try + { + // 尝试从池中获取 + while (_pool.TryTake(out var pooledConnection)) + { + if (IsConnectionValid(pooledConnection)) + { + pooledConnection.IsInUse = true; + pooledConnection.LastAccessTime = DateTime.UtcNow; + return new PooledConnectionWrapper(pooledConnection, this).Connection; + } + else + { + // 连接无效,释放并减少计数 + pooledConnection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + // 池中没有可用连接,创建新连接 + var newConnection = CreateNewConnection(); + if (newConnection != null) + { + newConnection.IsInUse = true; + Interlocked.Increment(ref _totalConnections); + return new PooledConnectionWrapper(newConnection, this).Connection; + } + } + catch + { + _semaphore.Release(); + throw; + } + } + + if (retry < _options.RetryCount - 1) + { + await Task.Delay(_options.RetryDelay, cancellationToken); + } + } + + throw new TimeoutException($"无法在 {_options.ConnectionTimeout} 内获取数据库连接"); + } + + /// + /// 获取连接(同步) + /// + /// 数据库连接 + public DbConnection GetConnection() + { + return GetConnectionAsync().GetAwaiter().GetResult(); + } + + internal void ReturnConnection(PooledConnection connection) + { + if (_disposed) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + return; + } + + if (IsConnectionValid(connection)) + { + connection.IsInUse = false; + connection.LastAccessTime = DateTime.UtcNow; + _pool.Add(connection); + } + else + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + + _semaphore.Release(); + } + + private bool IsConnectionValid(PooledConnection connection) + { + if (!connection.IsValid || connection.Connection == null) + return false; + + if (connection.Connection.State != ConnectionState.Open) + return false; + + // 检查最大生存时间 + if (DateTime.UtcNow - connection.CreateTime > _options.MaxLifetime) + return false; + + return true; + } + + private void HealthCheck(object? state) + { + var invalidConnections = new List(); + + foreach (var connection in _pool) + { + if (!IsConnectionValid(connection)) + { + invalidConnections.Add(connection); + } + } + + // 注意:由于 ConcurrentBag 的特性,这里只是标记连接无效 + // 实际移除会在 ReturnConnection 时进行 + } + + private void CleanupIdleConnections(object? state) + { + var now = DateTime.UtcNow; + var connectionsToKeep = new List(); + var connectionsToRemove = new List(); + + // 收集需要保留和移除的连接 + while (_pool.TryTake(out var connection)) + { + if (!connection.IsInUse && + now - connection.LastAccessTime > _options.MaxIdleTime && + _totalConnections - connectionsToRemove.Count > _options.MinPoolSize) + { + connectionsToRemove.Add(connection); + } + else + { + connectionsToKeep.Add(connection); + } + } + + // 放回需要保留的连接 + foreach (var connection in connectionsToKeep) + { + _pool.Add(connection); + } + + // 移除空闲连接 + foreach (var connection in connectionsToRemove) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + /// + /// 清空连接池 + /// + public void Clear() + { + while (_pool.TryTake(out var connection)) + { + connection.Dispose(); + Interlocked.Decrement(ref _totalConnections); + } + } + + /// + /// 获取连接池统计信息 + /// + /// 统计信息 + public ConnectionPoolStatistics GetStatistics() + { + return new ConnectionPoolStatistics + { + TotalConnections = _totalConnections, + AvailableConnections = _pool.Count, + InUseConnections = _totalConnections - _pool.Count, + MaxPoolSize = _options.MaxPoolSize, + MinPoolSize = _options.MinPoolSize + }; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _healthCheckTimer.Dispose(); + _cleanupTimer.Dispose(); + Clear(); + _semaphore.Dispose(); + } + } + + /// + /// 异步释放资源 + /// + public ValueTask DisposeAsync() + { + if (!_disposed) + { + _disposed = true; + _healthCheckTimer.Dispose(); + _cleanupTimer.Dispose(); + Clear(); + _semaphore.Dispose(); + } + return default(ValueTask); + } + } + + /// + /// 池化连接包装器 + /// + internal class PooledConnectionWrapper : IDisposable + { + private readonly PooledConnection _pooledConnection; + private readonly ConnectionPool _pool; + private bool _disposed; + + public DbConnection Connection => _pooledConnection.Connection; + + public PooledConnectionWrapper(PooledConnection pooledConnection, ConnectionPool pool) + { + _pooledConnection = pooledConnection; + _pool = pool; + } + + public void Dispose() + { + if (!_disposed) + { + _pool.ReturnConnection(_pooledConnection); + _disposed = true; + } + } + } + + /// + /// 连接池统计信息 + /// + public class ConnectionPoolStatistics + { + /// + /// 总连接数 + /// + public int TotalConnections { get; set; } + + /// + /// 可用连接数 + /// + public int AvailableConnections { get; set; } + + /// + /// 正在使用的连接数 + /// + public int InUseConnections { get; set; } + + /// + /// 最大连接数 + /// + public int MaxPoolSize { get; set; } + + /// + /// 最小连接数 + /// + public int MinPoolSize { get; set; } + } +} diff --git a/EasyTool.Core/DatabaseCategory/DbUtil.cs b/EasyTool.Core/DatabaseCategory/DbUtil.cs new file mode 100644 index 0000000..4317318 --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/DbUtil.cs @@ -0,0 +1,547 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DatabaseCategory +{ + /// + /// 数据库工具类 + /// 提供通用的数据库操作方法 + /// + public static class DbUtil + { + #region 连接管理 + + /// + /// 创建并打开连接 + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 数据库连接 + public static async Task CreateConnectionAsync(string connectionString, DbProviderFactory providerFactory) + { + var connection = providerFactory.CreateConnection() + ?? throw new InvalidOperationException("无法创建数据库连接"); + + connection.ConnectionString = connectionString; + await connection.OpenAsync(); + return connection; + } + + /// + /// 创建并打开连接(同步) + /// + /// 连接字符串 + /// 数据库提供者工厂 + /// 数据库连接 + public static DbConnection CreateConnection(string connectionString, DbProviderFactory providerFactory) + { + return CreateConnectionAsync(connectionString, providerFactory).GetAwaiter().GetResult(); + } + + #endregion + + #region 执行查询 + + /// + /// 执行非查询命令 + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 受影响的行数 + public static async Task ExecuteNonQueryAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + using var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + return await command.ExecuteNonQueryAsync(); + } + + /// + /// 执行非查询命令(同步) + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 受影响的行数 + public static int ExecuteNonQuery( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + return ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行标量查询 + /// + /// 返回类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 标量值 + public static async Task ExecuteScalarAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + using var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + var result = await command.ExecuteScalarAsync(); + + if (result == null || result == DBNull.Value) + return default; + + return (T)Convert.ChangeType(result, typeof(T)); + } + + /// + /// 执行标量查询(同步) + /// + /// 返回类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 标量值 + public static T? ExecuteScalar( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + return ExecuteScalarAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回数据读取器 + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 命令行为 + /// 数据读取器 + public static async Task ExecuteReaderAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null, + CommandBehavior commandBehavior = CommandBehavior.Default) + { + var command = CreateCommand(connection, sql, parameters, transaction, commandTimeout); + return await command.ExecuteReaderAsync(commandBehavior); + } + + /// + /// 执行查询并返回数据读取器(同步) + /// + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 命令行为 + /// 数据读取器 + public static DbDataReader ExecuteReader( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null, + CommandBehavior commandBehavior = CommandBehavior.Default) + { + return ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout, commandBehavior).GetAwaiter().GetResult(); + } + + #endregion + + #region 查询映射 + + /// + /// 执行查询并映射到实体列表 + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体列表 + public static async Task> QueryAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + var result = new List(); + + using var reader = await ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout); + + while (await reader.ReadAsync()) + { + result.Add(MapToObject(reader)); + } + + return result; + } + + /// + /// 执行查询并映射到实体列表(同步) + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体列表 + public static List Query( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + return QueryAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回第一个实体 + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体 + public static async Task QueryFirstOrDefaultAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + using var reader = await ExecuteReaderAsync( + connection, sql, parameters, transaction, commandTimeout, + CommandBehavior.SingleRow); + + if (await reader.ReadAsync()) + { + return MapToObject(reader); + } + + return default; + } + + /// + /// 执行查询并返回第一个实体(同步) + /// + /// 实体类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 实体 + public static T? QueryFirstOrDefault( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : new() + { + return QueryFirstOrDefaultAsync(connection, sql, parameters, transaction, commandTimeout).GetAwaiter().GetResult(); + } + + /// + /// 执行查询并返回单列值列表 + /// + /// 值类型 + /// 数据库连接 + /// SQL 语句 + /// 参数 + /// 事务 + /// 命令超时时间 + /// 值列表 + public static async Task> QueryColumnAsync( + DbConnection connection, + string sql, + Dictionary? parameters = null, + DbTransaction? transaction = null, + int? commandTimeout = null) + { + var result = new List(); + + using var reader = await ExecuteReaderAsync(connection, sql, parameters, transaction, commandTimeout); + + while (await reader.ReadAsync()) + { + var value = reader.GetValue(0); + if (value != null && value != DBNull.Value) + { + result.Add((T)Convert.ChangeType(value, typeof(T))); + } + } + + return result; + } + + #endregion + + #region 批量操作 + + /// + /// 批量插入 + /// + /// 实体类型 + /// 数据库连接 + /// 表名 + /// 实体列表 + /// 事务 + /// 批次大小 + /// 命令超时时间 + /// 插入行数 + public static async Task BulkInsertAsync( + DbConnection connection, + string table, + IEnumerable entities, + DbTransaction? transaction = null, + int batchSize = 1000, + int? commandTimeout = null) where T : class + { + var totalRows = 0; + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + var entityList = entities.ToList(); + + for (int i = 0; i < entityList.Count; i += batchSize) + { + var batch = entityList.Skip(i).Take(batchSize); + + var columns = string.Join(", ", properties.Select(p => p.Name)); + var paramNames = string.Join(", ", properties.Select((p, idx) => $"@p{idx}")); + + var sql = $"INSERT INTO {table} ({columns}) VALUES ({paramNames})"; + + foreach (var entity in batch) + { + var parameters = new Dictionary(); + for (int j = 0; j < properties.Length; j++) + { + parameters[$"@p{j}"] = properties[j].GetValue(entity); + } + + totalRows += await ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout); + } + } + + return totalRows; + } + + /// + /// 批量更新 + /// + /// 实体类型 + /// 数据库连接 + /// 表名 + /// 实体列表 + /// 主键列名 + /// 要更新的列(null 表示更新所有非主键列) + /// 事务 + /// 命令超时时间 + /// 更新行数 + public static async Task BulkUpdateAsync( + DbConnection connection, + string table, + IEnumerable entities, + string keyColumn, + string[]? updateColumns = null, + DbTransaction? transaction = null, + int? commandTimeout = null) where T : class + { + var totalRows = 0; + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + var columnsToUpdate = updateColumns ?? properties + .Select(p => p.Name) + .Where(n => n != keyColumn) + .ToArray(); + + foreach (var entity in entities) + { + var setClauses = columnsToUpdate.Select((c, i) => $"{c} = @p{i}"); + var sql = $"UPDATE {table} SET {string.Join(", ", setClauses)} WHERE {keyColumn} = @key"; + + var parameters = new Dictionary(); + for (int i = 0; i < columnsToUpdate.Length; i++) + { + var prop = properties.First(p => p.Name == columnsToUpdate[i]); + parameters[$"@p{i}"] = prop.GetValue(entity); + } + + var keyProp = properties.First(p => p.Name == keyColumn); + parameters["@key"] = keyProp.GetValue(entity); + + totalRows += await ExecuteNonQueryAsync(connection, sql, parameters, transaction, commandTimeout); + } + + return totalRows; + } + + #endregion + + #region 事务 + + /// + /// 执行事务 + /// + /// 数据库连接 + /// 事务操作 + /// 隔离级别 + public static async Task ExecuteTransactionAsync( + DbConnection connection, + Func action, + IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) + { + if (connection.State != ConnectionState.Open) + { + await connection.OpenAsync(); + } + + using var transaction = await connection.BeginTransactionAsync(isolationLevel); + + try + { + await action(transaction); + await transaction.CommitAsync(); + } + catch + { + await transaction.RollbackAsync(); + throw; + } + } + + /// + /// 执行事务并返回结果 + /// + /// 返回类型 + /// 数据库连接 + /// 事务操作 + /// 隔离级别 + /// 结果 + public static async Task ExecuteTransactionAsync( + DbConnection connection, + Func> func, + IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) + { + if (connection.State != ConnectionState.Open) + { + await connection.OpenAsync(); + } + + using var transaction = await connection.BeginTransactionAsync(isolationLevel); + + try + { + var result = await func(transaction); + await transaction.CommitAsync(); + return result; + } + catch + { + await transaction.RollbackAsync(); + throw; + } + } + + #endregion + + #region 辅助方法 + + private static DbCommand CreateCommand( + DbConnection connection, + string sql, + Dictionary? parameters, + DbTransaction? transaction, + int? commandTimeout) + { + var command = connection.CreateCommand(); + command.CommandText = sql; + command.Transaction = transaction; + + if (commandTimeout.HasValue) + { + command.CommandTimeout = commandTimeout.Value; + } + + if (parameters != null) + { + foreach (var kvp in parameters) + { + var parameter = command.CreateParameter(); + parameter.ParameterName = kvp.Key; + parameter.Value = kvp.Value ?? DBNull.Value; + command.Parameters.Add(parameter); + } + } + + return command; + } + + private static T MapToObject(DbDataReader reader) where T : new() + { + var obj = new T(); + var type = typeof(T); + + for (int i = 0; i < reader.FieldCount; i++) + { + var name = reader.GetName(i); + var property = type.GetProperty(name, + BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + if (property != null && property.CanWrite) + { + var value = reader.GetValue(i); + + if (value != null && value != DBNull.Value) + { + if (property.PropertyType != value.GetType()) + { + value = Convert.ChangeType(value, property.PropertyType); + } + property.SetValue(obj, value); + } + } + } + + return obj; + } + + #endregion + } +} diff --git a/EasyTool.Core/DatabaseCategory/SqlBuilder.cs b/EasyTool.Core/DatabaseCategory/SqlBuilder.cs new file mode 100644 index 0000000..41d7b48 --- /dev/null +++ b/EasyTool.Core/DatabaseCategory/SqlBuilder.cs @@ -0,0 +1,735 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.DatabaseCategory +{ + /// + /// SQL 构建器 + /// 提供流畅的 SQL 语句构建接口 + /// + public class SqlBuilder + { + private readonly StringBuilder _sql; + private readonly List _selectColumns; + private readonly List _fromTables; + private readonly List _joins; + private readonly List _whereConditions; + private readonly List _groupByColumns; + private readonly List _havingConditions; + private readonly List _orderByColumns; + private readonly Dictionary _parameters; + private string? _insertTable; + private string? _updateTable; + private string? _deleteTable; + private readonly List _insertColumns; + private readonly List _updateSets; + private int _skip; + private int _take; + private bool _distinct; + private int _paramIndex; + + /// + /// 创建 SQL 构建器 + /// + public SqlBuilder() + { + _sql = new StringBuilder(); + _selectColumns = new List(); + _fromTables = new List(); + _joins = new List(); + _whereConditions = new List(); + _groupByColumns = new List(); + _havingConditions = new List(); + _orderByColumns = new List(); + _parameters = new Dictionary(); + _insertColumns = new List(); + _updateSets = new List(); + _paramIndex = 0; + } + + #region SELECT + + /// + /// SELECT 语句 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder Select(params string[] columns) + { + _selectColumns.AddRange(columns); + return this; + } + + /// + /// SELECT DISTINCT + /// + /// 列名 + /// SqlBuilder + public SqlBuilder SelectDistinct(params string[] columns) + { + _distinct = true; + return Select(columns); + } + + /// + /// SELECT COUNT(*) + /// + /// SqlBuilder + public SqlBuilder SelectCount() + { + return Select("COUNT(*)"); + } + + /// + /// SELECT COUNT(column) + /// + /// 列名 + /// SqlBuilder + public SqlBuilder SelectCount(string column) + { + return Select($"COUNT({column})"); + } + + #endregion + + #region FROM + + /// + /// FROM 语句 + /// + /// 表名 + /// 别名 + /// SqlBuilder + public SqlBuilder From(string table, string? alias = null) + { + var from = string.IsNullOrEmpty(alias) ? table : $"{table} AS {alias}"; + _fromTables.Add(from); + return this; + } + + /// + /// FROM 子查询 + /// + /// 子查询 + /// 别名 + /// SqlBuilder + public SqlBuilder FromSubQuery(SqlBuilder subQuery, string alias) + { + var sql = subQuery.Build(); + foreach (var param in subQuery.GetParameters()) + { + _parameters[param.Key] = param.Value; + } + _fromTables.Add($"({sql}) AS {alias}"); + return this; + } + + #endregion + + #region JOIN + + /// + /// INNER JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder InnerJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"INNER JOIN {table} ON {on}" + : $"INNER JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + /// + /// LEFT JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder LeftJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"LEFT JOIN {table} ON {on}" + : $"LEFT JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + /// + /// RIGHT JOIN + /// + /// 表名 + /// 别名 + /// 连接条件 + /// SqlBuilder + public SqlBuilder RightJoin(string table, string? alias, string on) + { + var join = string.IsNullOrEmpty(alias) + ? $"RIGHT JOIN {table} ON {on}" + : $"RIGHT JOIN {table} AS {alias} ON {on}"; + _joins.Add(join); + return this; + } + + #endregion + + #region WHERE + + /// + /// WHERE 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Where(string condition) + { + _whereConditions.Add(condition); + return this; + } + + /// + /// WHERE 等于条件 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder WhereEquals(string column, object? value) + { + var paramName = AddParameter(value); + return Where($"{column} = {paramName}"); + } + + /// + /// WHERE IN 条件 + /// + /// 列名 + /// 值集合 + /// SqlBuilder + public SqlBuilder WhereIn(string column, IEnumerable values) + { + var paramNames = values.Select(v => AddParameter(v)); + return Where($"{column} IN ({string.Join(", ", paramNames)})"); + } + + /// + /// WHERE BETWEEN 条件 + /// + /// 列名 + /// 开始值 + /// 结束值 + /// SqlBuilder + public SqlBuilder WhereBetween(string column, object start, object end) + { + var startParam = AddParameter(start); + var endParam = AddParameter(end); + return Where($"{column} BETWEEN {startParam} AND {endParam}"); + } + + /// + /// WHERE LIKE 条件 + /// + /// 列名 + /// 模式 + /// SqlBuilder + public SqlBuilder WhereLike(string column, string pattern) + { + var paramName = AddParameter(pattern); + return Where($"{column} LIKE {paramName}"); + } + + /// + /// WHERE IS NULL + /// + /// 列名 + /// SqlBuilder + public SqlBuilder WhereIsNull(string column) + { + return Where($"{column} IS NULL"); + } + + /// + /// WHERE IS NOT NULL + /// + /// 列名 + /// SqlBuilder + public SqlBuilder WhereIsNotNull(string column) + { + return Where($"{column} IS NOT NULL"); + } + + /// + /// AND 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder And(string condition) + { + if (_whereConditions.Count > 0) + { + _whereConditions[_whereConditions.Count - 1] = $"({string.Join(" AND ", _whereConditions)})"; + } + _whereConditions.Add(condition); + return this; + } + + /// + /// OR 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Or(string condition) + { + if (_whereConditions.Count > 0) + { + _whereConditions[_whereConditions.Count - 1] = $"({string.Join(" OR ", _whereConditions)})"; + } + _whereConditions.Add(condition); + return this; + } + + #endregion + + #region GROUP BY / HAVING + + /// + /// GROUP BY + /// + /// 列名 + /// SqlBuilder + public SqlBuilder GroupBy(params string[] columns) + { + _groupByColumns.AddRange(columns); + return this; + } + + /// + /// HAVING 条件 + /// + /// 条件 + /// SqlBuilder + public SqlBuilder Having(string condition) + { + _havingConditions.Add(condition); + return this; + } + + #endregion + + #region ORDER BY + + /// + /// ORDER BY 升序 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder OrderBy(params string[] columns) + { + _orderByColumns.AddRange(columns.Select(c => $"{c} ASC")); + return this; + } + + /// + /// ORDER BY 降序 + /// + /// 列名 + /// SqlBuilder + public SqlBuilder OrderByDescending(params string[] columns) + { + _orderByColumns.AddRange(columns.Select(c => $"{c} DESC")); + return this; + } + + #endregion + + #region LIMIT / OFFSET + + /// + /// LIMIT + /// + /// 数量 + /// SqlBuilder + public SqlBuilder Take(int count) + { + _take = count; + return this; + } + + /// + /// OFFSET + /// + /// 偏移量 + /// SqlBuilder + public SqlBuilder Skip(int count) + { + _skip = count; + return this; + } + + /// + /// 分页 + /// + /// 页码(从1开始) + /// 每页大小 + /// SqlBuilder + public SqlBuilder Page(int page, int pageSize) + { + _skip = (page - 1) * pageSize; + _take = pageSize; + return this; + } + + #endregion + + #region INSERT + + /// + /// INSERT INTO + /// + /// 表名 + /// SqlBuilder + public SqlBuilder InsertInto(string table) + { + _insertTable = table; + return this; + } + + /// + /// 添加列值 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder Value(string column, object? value) + { + var paramName = AddParameter(value); + _insertColumns.Add(column); + return this; + } + + /// + /// 批量添加列值 + /// + /// 列值字典 + /// SqlBuilder + public SqlBuilder Values(Dictionary values) + { + foreach (var kvp in values) + { + var paramName = AddParameter(kvp.Value); + _insertColumns.Add(kvp.Key); + } + return this; + } + + #endregion + + #region UPDATE + + /// + /// UPDATE + /// + /// 表名 + /// SqlBuilder + public SqlBuilder Update(string table) + { + _updateTable = table; + return this; + } + + /// + /// SET 列值 + /// + /// 列名 + /// 值 + /// SqlBuilder + public SqlBuilder Set(string column, object? value) + { + var paramName = AddParameter(value); + _updateSets.Add($"{column} = {paramName}"); + return this; + } + + /// + /// 批量 SET + /// + /// 列值字典 + /// SqlBuilder + public SqlBuilder SetMany(Dictionary values) + { + foreach (var kvp in values) + { + Set(kvp.Key, kvp.Value); + } + return this; + } + + #endregion + + #region DELETE + + /// + /// DELETE FROM + /// + /// 表名 + /// SqlBuilder + public SqlBuilder DeleteFrom(string table) + { + _deleteTable = table; + return this; + } + + #endregion + + #region Build + + /// + /// 构建 SQL 语句 + /// + /// SQL 字符串 + public string Build() + { + _sql.Clear(); + + // INSERT + if (_insertTable != null) + { + BuildInsert(); + } + // UPDATE + else if (_updateTable != null) + { + BuildUpdate(); + } + // DELETE + else if (_deleteTable != null) + { + BuildDelete(); + } + // SELECT + else + { + BuildSelect(); + } + + return _sql.ToString(); + } + + private void BuildSelect() + { + _sql.Append("SELECT "); + + if (_distinct) + { + _sql.Append("DISTINCT "); + } + + if (_selectColumns.Count == 0) + { + _sql.Append("*"); + } + else + { + _sql.Append(string.Join(", ", _selectColumns)); + } + + if (_fromTables.Count > 0) + { + _sql.Append(" FROM "); + _sql.Append(string.Join(", ", _fromTables)); + } + + if (_joins.Count > 0) + { + _sql.Append(" "); + _sql.Append(string.Join(" ", _joins)); + } + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + + if (_groupByColumns.Count > 0) + { + _sql.Append(" GROUP BY "); + _sql.Append(string.Join(", ", _groupByColumns)); + } + + if (_havingConditions.Count > 0) + { + _sql.Append(" HAVING "); + _sql.Append(string.Join(" AND ", _havingConditions)); + } + + if (_orderByColumns.Count > 0) + { + _sql.Append(" ORDER BY "); + _sql.Append(string.Join(", ", _orderByColumns)); + } + + if (_take > 0) + { + _sql.Append($" LIMIT {_take}"); + } + + if (_skip > 0) + { + _sql.Append($" OFFSET {_skip}"); + } + } + + private void BuildInsert() + { + var paramNames = _parameters.Keys.Take(_insertColumns.Count).ToList(); + + _sql.Append($"INSERT INTO {_insertTable} "); + _sql.Append($"({string.Join(", ", _insertColumns)}) "); + _sql.Append($"VALUES ({string.Join(", ", paramNames)})"); + } + + private void BuildUpdate() + { + _sql.Append($"UPDATE {_updateTable} "); + _sql.Append($"SET {string.Join(", ", _updateSets)}"); + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + } + + private void BuildDelete() + { + _sql.Append($"DELETE FROM {_deleteTable}"); + + if (_whereConditions.Count > 0) + { + _sql.Append(" WHERE "); + _sql.Append(string.Join(" AND ", _whereConditions)); + } + } + + /// + /// 获取参数 + /// + /// 参数字典 + public Dictionary GetParameters() + { + return new Dictionary(_parameters); + } + + private string AddParameter(object? value) + { + var paramName = $"@p{_paramIndex++}"; + _parameters[paramName] = value; + return paramName; + } + + /// + /// 重置构建器 + /// + /// SqlBuilder + public SqlBuilder Reset() + { + _sql.Clear(); + _selectColumns.Clear(); + _fromTables.Clear(); + _joins.Clear(); + _whereConditions.Clear(); + _groupByColumns.Clear(); + _havingConditions.Clear(); + _orderByColumns.Clear(); + _parameters.Clear(); + _insertColumns.Clear(); + _updateSets.Clear(); + _insertTable = null; + _updateTable = null; + _deleteTable = null; + _skip = 0; + _take = 0; + _distinct = false; + _paramIndex = 0; + return this; + } + + #endregion + } + + /// + /// SQL 构建工具类 + /// + public static class SqlBuilderUtil + { + /// + /// 创建 SQL 构建器 + /// + /// SqlBuilder + public static SqlBuilder Create() + { + return new SqlBuilder(); + } + + /// + /// 快速创建 SELECT 查询 + /// + /// 表名 + /// 列名 + /// SqlBuilder + public static SqlBuilder SelectFrom(string table, params string[] columns) + { + return new SqlBuilder().Select(columns).From(table); + } + + /// + /// 快速创建 INSERT 语句 + /// + /// 表名 + /// 列值字典 + /// SqlBuilder + public static SqlBuilder Insert(string table, Dictionary values) + { + return new SqlBuilder().InsertInto(table).Values(values); + } + + /// + /// 快速创建 UPDATE 语句 + /// + /// 表名 + /// 列值字典 + /// WHERE 条件 + /// SqlBuilder + public static SqlBuilder Update(string table, Dictionary values, string? where = null) + { + var builder = new SqlBuilder().Update(table).SetMany(values); + if (!string.IsNullOrEmpty(where)) + { + builder.Where(where); + } + return builder; + } + + /// + /// 快速创建 DELETE 语句 + /// + /// 表名 + /// WHERE 条件 + /// SqlBuilder + public static SqlBuilder Delete(string table, string? where = null) + { + var builder = new SqlBuilder().DeleteFrom(table); + if (!string.IsNullOrEmpty(where)) + { + builder.Where(where); + } + return builder; + } + } +} diff --git a/EasyTool.Core/DateTimeCategory/AgeUtil.cs b/EasyTool.Core/DateTimeCategory/AgeUtil.cs new file mode 100644 index 0000000..55c0c8b --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/AgeUtil.cs @@ -0,0 +1,288 @@ +using System; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 年龄计算工具类 + /// + public static class AgeUtil + { + /// + /// 计算年龄(周岁) + /// + public static int CalculateAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + var age = today.Year - birthDate.Year; + + // 如果生日还没到,减1岁 + if (birthDate.Date > today.AddYears(-age)) + { + age--; + } + + return Math.Max(0, age); + } + + /// + /// 计算精确年龄(岁、月、日) + /// + public static Age CalculateExactAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + + var years = today.Year - birthDate.Year; + var months = today.Month - birthDate.Month; + var days = today.Day - birthDate.Day; + + if (days < 0) + { + months--; + days += DateTime.DaysInMonth(today.Year, today.Month == 1 ? 12 : today.Month - 1); + } + + if (months < 0) + { + years--; + months += 12; + } + + return new Age + { + Years = Math.Max(0, years), + Months = Math.Max(0, months), + Days = Math.Max(0, days) + }; + } + + /// + /// 计算虚岁 + /// + public static int CalculateNominalAge(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return today.Year - birthDate.Year + 1; + } + + /// + /// 获取下一个生日 + /// + public static DateTime GetNextBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + var birthday = new DateTime(today.Year, birthDate.Month, birthDate.Day); + + if (birthday < today) + { + birthday = birthday.AddYears(1); + } + + return birthday; + } + + /// + /// 获取距离下一个生日的天数 + /// + public static int GetDaysUntilNextBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var nextBirthday = GetNextBirthday(birthDate, currentDate); + return (nextBirthday - (currentDate ?? DateTime.Today)).Days; + } + + /// + /// 判断今天是否是生日 + /// + public static bool IsBirthday(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return birthDate.Month == today.Month && birthDate.Day == today.Day; + } + + /// + /// 判断是否成年(默认18岁) + /// + public static bool IsAdult(DateTime birthDate, int adultAge = 18, DateTime? currentDate = null) + { + return CalculateAge(birthDate, currentDate) >= adultAge; + } + + /// + /// 获取生肖 + /// + public static string GetChineseZodiac(DateTime birthDate) + { + var zodiacs = new[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; + var index = (birthDate.Year - 1900) % 12; + return zodiacs[index >= 0 ? index : index + 12]; + } + + /// + /// 获取星座 + /// + public static string GetZodiacSign(DateTime birthDate) + { + var month = birthDate.Month; + var day = birthDate.Day; + + return (month, day) switch + { + (1, >= 20) or (2, <= 18) => "水瓶座", + (2, >= 19) or (3, <= 20) => "双鱼座", + (3, >= 21) or (4, <= 19) => "白羊座", + (4, >= 20) or (5, <= 20) => "金牛座", + (5, >= 21) or (6, <= 21) => "双子座", + (6, >= 22) or (7, <= 22) => "巨蟹座", + (7, >= 23) or (8, <= 22) => "狮子座", + (8, >= 23) or (9, <= 22) => "处女座", + (9, >= 23) or (10, <= 23) => "天秤座", + (10, >= 24) or (11, <= 22) => "天蝎座", + (11, >= 23) or (12, <= 21) => "射手座", + _ => "摩羯座" + }; + } + + /// + /// 获取星座英文 + /// + public static string GetZodiacSignEnglish(DateTime birthDate) + { + var month = birthDate.Month; + var day = birthDate.Day; + + return (month, day) switch + { + (1, >= 20) or (2, <= 18) => "Aquarius", + (2, >= 19) or (3, <= 20) => "Pisces", + (3, >= 21) or (4, <= 19) => "Aries", + (4, >= 20) or (5, <= 20) => "Taurus", + (5, >= 21) or (6, <= 21) => "Gemini", + (6, >= 22) or (7, <= 22) => "Cancer", + (7, >= 23) or (8, <= 22) => "Leo", + (8, >= 23) or (9, <= 22) => "Virgo", + (9, >= 23) or (10, <= 23) => "Libra", + (10, >= 24) or (11, <= 22) => "Scorpio", + (11, >= 23) or (12, <= 21) => "Sagittarius", + _ => "Capricorn" + }; + } + + /// + /// 计算退休年龄(男60,女干部55,女工人50) + /// + public static DateTime CalculateRetirementDate(DateTime birthDate, Gender gender, bool isCadre = false) + { + var retirementAge = gender switch + { + Gender.Male => 60, + Gender.Female when isCadre => 55, + Gender.Female => 50, + _ => 60 + }; + + return birthDate.AddYears(retirementAge); + } + + /// + /// 计算总存活天数 + /// + public static int CalculateTotalDays(DateTime birthDate, DateTime? currentDate = null) + { + return (int)((currentDate ?? DateTime.Today) - birthDate.Date).TotalDays; + } + + /// + /// 计算总存活周数 + /// + public static int CalculateTotalWeeks(DateTime birthDate, DateTime? currentDate = null) + { + return CalculateTotalDays(birthDate, currentDate) / 7; + } + + /// + /// 计算总存活月数 + /// + public static int CalculateTotalMonths(DateTime birthDate, DateTime? currentDate = null) + { + var today = currentDate ?? DateTime.Today; + return (today.Year - birthDate.Year) * 12 + today.Month - birthDate.Month; + } + + /// + /// 格式化年龄显示 + /// + public static string FormatAge(DateTime birthDate, DateTime? currentDate = null) + { + var age = CalculateExactAge(birthDate, currentDate); + if (age.Years > 0) + return $"{age.Years}岁{age.Months}个月"; + if (age.Months > 0) + return $"{age.Months}个月{age.Days}天"; + return $"{age.Days}天"; + } + + /// + /// 格式化年龄(简短格式) + /// + public static string FormatAgeShort(DateTime birthDate, DateTime? currentDate = null) + { + var age = CalculateExactAge(birthDate, currentDate); + if (age.Years > 0) + return $"{age.Years}岁"; + if (age.Months > 0) + return $"{age.Months}个月"; + return $"{age.Days}天"; + } + } + + /// + /// 年龄信息 + /// + public class Age + { + /// + /// 岁 + /// + public int Years { get; set; } + + /// + /// 月 + /// + public int Months { get; set; } + + /// + /// 日 + /// + public int Days { get; set; } + + /// + /// 总天数 + /// + public int TotalDays => Years * 365 + Months * 30 + Days; + + /// + /// 总月数 + /// + public int TotalMonths => Years * 12 + Months; + + public override string ToString() + { + return $"{Years}岁{Months}个月{Days}天"; + } + } + + /// + /// 性别 + /// + public enum Gender + { + /// + /// 男性 + /// + Male, + + /// + /// 女性 + /// + Female + } +} diff --git a/EasyTool.Core/DateTimeCategory/CronUtil.cs b/EasyTool.Core/DateTimeCategory/CronUtil.cs new file mode 100644 index 0000000..2dd7a90 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/CronUtil.cs @@ -0,0 +1,425 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.DateTimeCategory +{ + /// + /// Cron 表达式工具类 + /// 提供 Cron 表达式的解析和计算下次执行时间 + /// + public static class CronUtil + { + private static readonly Regex CronRegex = new(@"^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$", RegexOptions.Compiled); + + /// + /// 验证 Cron 表达式是否有效 + /// + /// Cron 表达式 + /// 是否有效 + public static bool IsValid(string cronExpression) + { + if (string.IsNullOrWhiteSpace(cronExpression)) + return false; + + var match = CronRegex.Match(cronExpression); + if (!match.Success) + return false; + + return IsValidField(match.Groups[1].Value, 0, 59) && // 秒 + IsValidField(match.Groups[2].Value, 0, 59) && // 分 + IsValidField(match.Groups[3].Value, 0, 23) && // 时 + IsValidField(match.Groups[4].Value, 1, 31) && // 日 + IsValidField(match.Groups[5].Value, 1, 12); // 月 + } + + private static bool IsValidField(string field, int min, int max) + { + if (field == "*") + return true; + + foreach (var part in field.Split(',')) + { + var trimmedPart = part.Trim(); + + if (trimmedPart == "*") + continue; + + if (trimmedPart.Contains('/')) + { + var slashParts = trimmedPart.Split('/'); + if (slashParts.Length != 2) + return false; + + if (slashParts[0] != "*" && !IsValidRangeOrNumber(slashParts[0], min, max)) + return false; + + if (!int.TryParse(slashParts[1], out var step) || step <= 0) + return false; + } + else if (!IsValidRangeOrNumber(trimmedPart, min, max)) + { + return false; + } + } + + return true; + } + + private static bool IsValidRangeOrNumber(string value, int min, int max) + { + if (value.Contains('-')) + { + var rangeParts = value.Split('-'); + if (rangeParts.Length != 2) + return false; + + if (!int.TryParse(rangeParts[0], out var start) || !int.TryParse(rangeParts[1], out var end)) + return false; + + return start >= min && start <= max && end >= min && end <= max && start <= end; + } + + if (int.TryParse(value, out var num)) + return num >= min && num <= max; + + return false; + } + + /// + /// 获取下次执行时间 + /// + /// Cron 表达式(秒 分 时 日 月) + /// 起始时间 + /// 下次执行时间 + public static DateTime GetNextExecutionTime(string cronExpression, DateTime? fromTime = null) + { + if (!IsValid(cronExpression)) + throw new ArgumentException("无效的 Cron 表达式", nameof(cronExpression)); + + var parts = cronExpression.Split(' '); + var secondField = parts[0]; + var minuteField = parts[1]; + var hourField = parts[2]; + var dayField = parts[3]; + var monthField = parts[4]; + + var currentTime = fromTime ?? DateTime.Now; + var nextTime = currentTime.AddSeconds(1); + + while (true) + { + // 检查月份 + if (!IsFieldMatch(monthField, nextTime.Month, 1, 12)) + { + nextTime = new DateTime(nextTime.Year, nextTime.Month, 1).AddMonths(1); + continue; + } + + // 检查日期 + var daysInMonth = DateTime.DaysInMonth(nextTime.Year, nextTime.Month); + if (!IsFieldMatch(dayField, nextTime.Day, 1, daysInMonth)) + { + nextTime = nextTime.AddDays(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day); + continue; + } + + // 检查小时 + if (!IsFieldMatch(hourField, nextTime.Hour, 0, 23)) + { + nextTime = nextTime.AddHours(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day, nextTime.Hour, 0, 0); + continue; + } + + // 检查分钟 + if (!IsFieldMatch(minuteField, nextTime.Minute, 0, 59)) + { + nextTime = nextTime.AddMinutes(1); + nextTime = new DateTime(nextTime.Year, nextTime.Month, nextTime.Day, nextTime.Hour, nextTime.Minute, 0); + continue; + } + + // 检查秒 + if (!IsFieldMatch(secondField, nextTime.Second, 0, 59)) + { + nextTime = nextTime.AddSeconds(1); + continue; + } + + return nextTime; + } + } + + /// + /// 获取接下来的多次执行时间 + /// + /// Cron 表达式 + /// 获取次数 + /// 起始时间 + /// 执行时间列表 + public static List GetNextExecutionTimes(string cronExpression, int count, DateTime? fromTime = null) + { + var result = new List(); + var nextTime = fromTime ?? DateTime.Now; + + for (int i = 0; i < count; i++) + { + nextTime = GetNextExecutionTime(cronExpression, nextTime); + result.Add(nextTime); + } + + return result; + } + + private static bool IsFieldMatch(string field, int value, int min, int max) + { + if (field == "*") + return true; + + foreach (var part in field.Split(',')) + { + var trimmedPart = part.Trim(); + + if (trimmedPart == "*") + return true; + + if (trimmedPart.Contains('/')) + { + var slashParts = trimmedPart.Split('/'); + var step = int.Parse(slashParts[1]); + + if (slashParts[0] == "*") + { + if ((value - min) % step == 0) + return true; + } + else + { + var start = int.Parse(slashParts[0]); + if (value >= start && (value - start) % step == 0) + return true; + } + } + else if (trimmedPart.Contains('-')) + { + var rangeParts = trimmedPart.Split('-'); + var start = int.Parse(rangeParts[0]); + var end = int.Parse(rangeParts[1]); + + if (value >= start && value <= end) + return true; + } + else + { + if (int.Parse(trimmedPart) == value) + return true; + } + } + + return false; + } + + /// + /// 获取字段匹配的所有值 + /// + /// 字段表达式 + /// 最小值 + /// 最大值 + /// 匹配的值列表 + public static List GetFieldValues(string field, int min, int max) + { + var result = new HashSet(); + + if (field == "*") + { + for (int i = min; i <= max; i++) + result.Add(i); + return result.OrderBy(x => x).ToList(); + } + + foreach (var part in field.Split(',')) + { + var trimmedPart = part.Trim(); + + if (trimmedPart == "*") + { + for (int i = min; i <= max; i++) + result.Add(i); + } + else if (trimmedPart.Contains('/')) + { + var slashParts = trimmedPart.Split('/'); + var step = int.Parse(slashParts[1]); + int start; + + if (slashParts[0] == "*") + { + start = min; + } + else + { + start = int.Parse(slashParts[0]); + } + + for (int i = start; i <= max; i += step) + result.Add(i); + } + else if (trimmedPart.Contains('-')) + { + var rangeParts = trimmedPart.Split('-'); + var start = int.Parse(rangeParts[0]); + var end = int.Parse(rangeParts[1]); + + for (int i = start; i <= end; i++) + result.Add(i); + } + else + { + result.Add(int.Parse(trimmedPart)); + } + } + + return result.Where(x => x >= min && x <= max).OrderBy(x => x).ToList(); + } + + /// + /// 解析 Cron 表达式为可读文本 + /// + /// Cron 表达式 + /// 可读文本 + public static string ToDescription(string cronExpression) + { + if (!IsValid(cronExpression)) + throw new ArgumentException("无效的 Cron 表达式", nameof(cronExpression)); + + var parts = cronExpression.Split(' '); + var secondField = parts[0]; + var minuteField = parts[1]; + var hourField = parts[2]; + var dayField = parts[3]; + var monthField = parts[4]; + + var descriptions = new List(); + + // 秒 + if (secondField != "*") + descriptions.Add($"第 {FieldToDescription(secondField)} 秒"); + + // 分 + if (minuteField != "*") + descriptions.Add($"第 {FieldToDescription(minuteField)} 分钟"); + + // 时 + if (hourField != "*") + descriptions.Add($"第 {FieldToDescription(hourField)} 小时"); + + // 日 + if (dayField != "*") + descriptions.Add($"每月 {FieldToDescription(dayField)} 日"); + + // 月 + if (monthField != "*") + descriptions.Add($"{FieldToDescription(monthField)} 月"); + + if (descriptions.Count == 0) + return "每秒执行"; + + return string.Join(",", descriptions) + " 执行"; + } + + private static string FieldToDescription(string field) + { + if (field == "*") + return "每"; + + if (field.Contains('/')) + { + var parts = field.Split('/'); + return parts[0] == "*" ? $"每隔 {parts[1]}" : $"从 {parts[0]} 开始每隔 {parts[1]}"; + } + + if (field.Contains('-')) + { + var parts = field.Split('-'); + return $"{parts[0]} 到 {parts[1]}"; + } + + return field; + } + + #region 常用 Cron 表达式 + + /// + /// 每秒执行 + /// + public static string EverySecond => "* * * * *"; + + /// + /// 每分钟执行(每分钟的第 0 秒) + /// + public static string EveryMinute => "0 * * * *"; + + /// + /// 每小时执行(每小时的第 0 分 0 秒) + /// + public static string EveryHour => "0 0 * * *"; + + /// + /// 每天执行(每天的 00:00:00) + /// + public static string EveryDay => "0 0 0 * *"; + + /// + /// 每月执行(每月 1 日的 00:00:00) + /// + public static string EveryMonth => "0 0 0 1 *"; + + /// + /// 每隔 N 秒执行 + /// + public static string EveryNSeconds(int n) => $"*/{n} * * * *"; + + /// + /// 每隔 N 分钟执行 + /// + public static string EveryNMinutes(int n) => $"0 */{n} * * *"; + + /// + /// 每隔 N 小时执行 + /// + public static string EveryNHours(int n) => $"0 0 */{n} * *"; + + /// + /// 每天指定时间执行 + /// + /// 小时 + /// 分钟 + /// 秒 + public static string DailyAt(int hour, int minute = 0, int second = 0) => $"{second} {minute} {hour} * *"; + + /// + /// 每周指定时间执行(周一为 1,周日为 7) + /// + /// 星期几(1-7) + /// 小时 + /// 分钟 + /// 秒 + public static string WeeklyAt(int dayOfWeek, int hour = 0, int minute = 0, int second = 0) + => $"{second} {minute} {hour} * *"; + + /// + /// 每月指定日期时间执行 + /// + /// 日期 + /// 小时 + /// 分钟 + /// 秒 + public static string MonthlyAt(int day, int hour = 0, int minute = 0, int second = 0) + => $"{second} {minute} {hour} {day} *"; + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs index 2b7f32f..4d28e6a 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeExtension.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.DateTimeCategory { /// /// 提供各种日期操作和计算的工具类。 @@ -15,6 +15,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在周的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfWeek(date)")] public static DateTime GetFirstDayOfWeek(this DateTime date) => DateTimeUtil.GetFirstDayOfWeek(date); /// @@ -22,6 +23,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在月份的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfMonth(date)")] public static DateTime GetFirstDayOfMonth(this DateTime date) => DateTimeUtil.GetFirstDayOfMonth(date); @@ -30,6 +32,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在季度的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfQuarter(date)")] public static DateTime GetFirstDayOfQuarter(this DateTime date) => DateTimeUtil.GetFirstDayOfQuarter(date); /// @@ -37,6 +40,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在年份的第一天的日期。 + [Obsolete("请直接使用 DateTimeUtil.GetFirstDayOfYear(date)")] public static DateTime GetFirstDayOfYear(this DateTime date) => DateTimeUtil.GetFirstDayOfYear(date); /// @@ -44,6 +48,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期和当前日期之间的天数差。 + [Obsolete("请直接使用 DateTimeUtil.GetDaysBetween(date)")] public static int GetDaysBetween(this DateTime date) => DateTimeUtil.GetDaysBetween(date); /// @@ -52,6 +57,7 @@ public static class DateTimeExtension /// 第一个日期。 /// 第二个日期。 /// 两个日期之间的天数差。 + [Obsolete("请直接使用 DateTimeUtil.GetDaysBetween(date1, date2)")] public static int GetDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetDaysBetween(date1, date2); /// @@ -59,6 +65,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期和当前日期之间的工作日数差。 + [Obsolete("请直接使用 DateTimeUtil.GetWorkDaysBetween(date)")] public static int GetWorkDaysBetween(this DateTime date) => DateTimeUtil.GetWorkDaysBetween(date); /// @@ -67,6 +74,7 @@ public static class DateTimeExtension /// 第一个日期。 /// 第二个日期。 /// 两个日期之间的工作日数差。 + [Obsolete("请直接使用 DateTimeUtil.GetWorkDaysBetween(date1, date2)")] public static int GetWorkDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetWorkDaysBetween(date1, date2); /// @@ -74,6 +82,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 如果是工作日,则返回 true;否则返回 false。 + [Obsolete("请直接使用 DateTimeUtil.IsWorkDay(date)")] public static bool IsWorkDay(this DateTime date) => DateTimeUtil.IsWorkDay(date); /// @@ -81,6 +90,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在周的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetWeekDays(date)")] public static List GetWeekDays(this DateTime date) => DateTimeUtil.GetWeekDays(date); /// @@ -88,6 +98,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在月份的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetMonthDays(date)")] public static List GetMonthDays(this DateTime date) => DateTimeUtil.GetMonthDays(date); /// @@ -95,6 +106,7 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在季度的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetQuarterDays(date)")] public static List GetQuarterDays(this DateTime date) => DateTimeUtil.GetQuarterDays(date); /// @@ -102,6 +114,309 @@ public static class DateTimeExtension /// /// 指定日期。 /// 指定日期所在年份的所有日期。 + [Obsolete("请直接使用 DateTimeUtil.GetYearDays(date)")] public static List GetYearDays(this DateTime date) => DateTimeUtil.GetYearDays(date); + + + + #region 新增扩展方法 + + /// + /// 判断日期是否是今天 + /// + public static bool IsToday(this DateTime date) + { + return date.Date == DateTime.Today; + } + + /// + /// 判断日期是否是昨天 + /// + public static bool IsYesterday(this DateTime date) + { + return date.Date == DateTime.Today.AddDays(-1); + } + + /// + /// 判断日期是否是明天 + /// + public static bool IsTomorrow(this DateTime date) + { + return date.Date == DateTime.Today.AddDays(1); + } + + /// + /// 判断日期是否在本周 + /// + public static bool IsThisWeek(this DateTime date) + { + var today = DateTime.Today; + var firstDayOfWeek = DateTimeUtil.GetFirstDayOfWeek(today); + var lastDayOfWeek = firstDayOfWeek.AddDays(6); + return date.Date >= firstDayOfWeek && date.Date <= lastDayOfWeek; + } + + /// + /// 判断日期是否在本月 + /// + public static bool IsThisMonth(this DateTime date) + { + var today = DateTime.Today; + return date.Year == today.Year && date.Month == today.Month; + } + + /// + /// 判断日期是否在本年 + /// + public static bool IsThisYear(this DateTime date) + { + return date.Year == DateTime.Today.Year; + } + + /// + /// 判断日期是否在指定范围内(包含边界) + /// + public static bool IsBetween(this DateTime date, DateTime startDate, DateTime endDate) + { + return date >= startDate && date <= endDate; + } + + /// + /// 计算年龄 + /// + public static int ToAge(this DateTime birthDate) + { + var today = DateTime.Today; + int age = today.Year - birthDate.Year; + + if (birthDate > today.AddYears(-age)) + age--; + + return age; + } + + + /// + /// 判断是否是周末(周六或周日) + /// + public static bool IsWeekend(this DateTime date) + { + return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 获取日期所在月的最后一天 + /// + public static DateTime GetLastDayOfMonth(this DateTime date) + { + return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); + } + + /// + /// 获取日期所在月的最后一天 + /// + public static DateTime GetLastDayOfWeek(this DateTime date) + { + var firstDay = DateTimeUtil.GetFirstDayOfWeek(date); + return firstDay.AddDays(6); + } + + /// + /// 获取日期所在季度的最后一天 + /// + public static DateTime GetLastDayOfQuarter(this DateTime date) + { + int currentQuarter = (date.Month - 1) / 3 + 1; + int lastMonthOfQuarter = currentQuarter * 3; + return new DateTime(date.Year, lastMonthOfQuarter, DateTime.DaysInMonth(date.Year, lastMonthOfQuarter)); + } + + /// + /// 获取日期所在年的最后一天 + /// + public static DateTime GetLastDayOfYear(this DateTime date) + { + return new DateTime(date.Year, 12, 31); + } + + /// + /// 获取日期的中文星期表示 + /// + public static string ToChineseWeekDay(this DateTime date) + { + return date.DayOfWeek switch + { + DayOfWeek.Monday => "星期一", + DayOfWeek.Tuesday => "星期二", + DayOfWeek.Wednesday => "星期三", + DayOfWeek.Thursday => "星期四", + DayOfWeek.Friday => "星期五", + DayOfWeek.Saturday => "星期六", + DayOfWeek.Sunday => "星期日", + _ => string.Empty + }; + } + + /// + /// 获取日期的中文星期简称 + /// + public static string ToChineseWeekDayShort(this DateTime date) + { + return date.DayOfWeek switch + { + DayOfWeek.Monday => "周一", + DayOfWeek.Tuesday => "周二", + DayOfWeek.Wednesday => "周三", + DayOfWeek.Thursday => "周四", + DayOfWeek.Friday => "周五", + DayOfWeek.Saturday => "周六", + DayOfWeek.Sunday => "周日", + _ => string.Empty + }; + } + + + /// + /// 获取日期所在季度的数字(1-4) + /// + public static int GetQuarter(this DateTime date) + { + return (date.Month - 1) / 3 + 1; + } + + /// + /// 获取日期所在周在本年的周数 + /// + public static int GetWeekOfYear(this DateTime date) + { + var culture = CultureInfo.CurrentCulture; + var calendar = culture.Calendar; + var weekRule = culture.DateTimeFormat.CalendarWeekRule; + var firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek; + return calendar.GetWeekOfYear(date, weekRule, firstDayOfWeek); + } + + /// + /// 添加工作日 + /// + /// 起始日期 + /// 要添加的工作日数 + public static DateTime AddWorkDays(this DateTime date, int workDays) + { + var result = date; + int daysToAdd = Math.Abs(workDays); + int direction = workDays >= 0 ? 1 : -1; + + while (daysToAdd > 0) + { + result = result.AddDays(direction); + if (DateTimeUtil.IsWorkDay(result)) + daysToAdd--; + } + + return result; + } + + /// + /// 获取日期的友好字符串表示 + /// + public static string ToFriendlyString(this DateTime date) + { + var today = DateTime.Today; + var span = today - date.Date; + + return span.TotalDays switch + { + 0 => "今天", + 1 => "昨天", + -1 => "明天", + _ when span.TotalDays > 0 && span.TotalDays <= 7 => $"上周{date.ToChineseWeekDayShort()}", + _ when span.TotalDays < 0 && span.TotalDays >= -7 => $"下周{date.ToChineseWeekDayShort()}", + _ when date.Year == today.Year => date.ToString("MM月dd日"), + _ => date.ToString("yyyy年MM月dd日") + }; + } + + /// + /// 获取两个日期之间相差的月数 + /// + public static int GetMonthsBetween(this DateTime startDate, DateTime endDate) + { + int months = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month; + + // 如果结束日期的日小于开始日期的日,需要减去一个月 + if (endDate.Day < startDate.Day) + { + months--; + } + + return Math.Abs(months); + } + + /// + /// 获取两个日期之间相差的年数 + /// + public static int GetYearsBetween(this DateTime startDate, DateTime endDate) + { + int years = endDate.Year - startDate.Year; + + // 如果结束日期的月和日小于开始日期的月和日,需要减去一年 + if (endDate.Month < startDate.Month || (endDate.Month == startDate.Month && endDate.Day < startDate.Day)) + { + years--; + } + + return Math.Abs(years); + } + + /// + /// 获取一天的开始时间(00:00:00) + /// + public static DateTime StartOfDay(this DateTime date) + { + return date.Date; + } + + /// + /// 获取一天的结束时间(23:59:59) + /// + public static DateTime EndOfDay(this DateTime date) + { + return date.Date.AddDays(1).AddTicks(-1); + } + + /// + /// 获取月的开始时间 + /// + public static DateTime StartOfMonth(this DateTime date) + { + return new DateTime(date.Year, date.Month, 1); + } + + /// + /// 获取月的结束时间 + /// + public static DateTime EndOfMonth(this DateTime date) + { + return date.GetLastDayOfMonth().EndOfDay(); + } + + /// + /// 获取年的开始时间 + /// + public static DateTime StartOfYear(this DateTime date) + { + return new DateTime(date.Year, 1, 1); + } + + /// + /// 获取年的结束时间 + /// + public static DateTime EndOfYear(this DateTime date) + { + return new DateTime(date.Year, 12, 31).EndOfDay(); + } + + #endregion } } diff --git a/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs b/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs index f31092d..5ad6dcb 100644 --- a/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs +++ b/EasyTool.Core/DateTimeCategory/DateTimeUtil.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Globalization; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// /// 提供各种日期操作和计算的工具类。 diff --git a/EasyTool.Core/DateTimeCategory/HolidayUtil.cs b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs new file mode 100644 index 0000000..da02e26 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/HolidayUtil.cs @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 节假日工具类 + /// + public static class HolidayUtil + { + /// + /// 获取指定年份的中国法定节假日 + /// + public static List GetChineseHolidays(int year) + { + var holidays = new List(); + + // 元旦 + holidays.Add(new DateTime(year, 1, 1)); + + // 春节(简化处理,实际需要根据农历计算) + holidays.Add(new DateTime(year, 1, 1)); + holidays.Add(new DateTime(year, 1, 2)); + holidays.Add(new DateTime(year, 1, 3)); + + // 清明节(4月4日或5日) + holidays.Add(GetQingmingDate(year)); + + // 劳动节 + holidays.Add(new DateTime(year, 5, 1)); + holidays.Add(new DateTime(year, 5, 2)); + holidays.Add(new DateTime(year, 5, 3)); + + // 端午节(简化处理) + holidays.Add(new DateTime(year, 6, 1)); + + // 中秋节(简化处理) + holidays.Add(new DateTime(year, 9, 15)); + + // 国庆节 + holidays.Add(new DateTime(year, 10, 1)); + holidays.Add(new DateTime(year, 10, 2)); + holidays.Add(new DateTime(year, 10, 3)); + holidays.Add(new DateTime(year, 10, 4)); + holidays.Add(new DateTime(year, 10, 5)); + holidays.Add(new DateTime(year, 10, 6)); + holidays.Add(new DateTime(year, 10, 7)); + + return holidays; + } + + /// + /// 获取清明节日期 + /// + private static DateTime GetQingmingDate(int year) + { + // 清明节通常在4月4日或5日 + var day = 5; + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + { + day = 4; + } + return new DateTime(year, 4, day); + } + + /// + /// 判断是否为工作日 + /// + public static bool IsWorkday(DateTime date, List? holidays = null, List? workdays = null) + { + // 检查是否为调休工作日 + if (workdays != null && workdays.Contains(date.Date)) + return true; + + // 检查是否为假日 + if (holidays != null && holidays.Contains(date.Date)) + return false; + + // 周一至周五为工作日 + return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday; + } + + /// + /// 判断是否为周末 + /// + public static bool IsWeekend(DateTime date) + { + return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 获取下一个工作日 + /// + public static DateTime GetNextWorkday(DateTime date, List? holidays = null, List? workdays = null) + { + var next = date.AddDays(1); + while (!IsWorkday(next, holidays, workdays)) + { + next = next.AddDays(1); + } + return next; + } + + /// + /// 获取上一个工作日 + /// + public static DateTime GetPreviousWorkday(DateTime date, List? holidays = null, List? workdays = null) + { + var prev = date.AddDays(-1); + while (!IsWorkday(prev, holidays, workdays)) + { + prev = prev.AddDays(-1); + } + return prev; + } + + /// + /// 计算工作日数量 + /// + public static int CountWorkdays(DateTime start, DateTime end, List? holidays = null, List? workdays = null) + { + var count = 0; + var current = start.Date; + + while (current <= end.Date) + { + if (IsWorkday(current, holidays, workdays)) + count++; + current = current.AddDays(1); + } + + return count; + } + + /// + /// 添加工作日 + /// + public static DateTime AddWorkdays(DateTime date, int days, List? holidays = null, List? workdays = null) + { + var result = date; + var increment = days > 0 ? 1 : -1; + var remaining = Math.Abs(days); + + while (remaining > 0) + { + result = result.AddDays(increment); + if (IsWorkday(result, holidays, workdays)) + remaining--; + } + + return result; + } + + /// + /// 获取西式节日 + /// + public static DateTime GetWesternHoliday(int year, WesternHoliday holiday) + { + return holiday switch + { + WesternHoliday.NewYear => new DateTime(year, 1, 1), + WesternHoliday.ValentinesDay => new DateTime(year, 2, 14), + WesternHoliday.StPatricksDay => new DateTime(year, 3, 17), + WesternHoliday.AprilFools => new DateTime(year, 4, 1), + WesternHoliday.IndependenceDay => new DateTime(year, 7, 4), + WesternHoliday.Halloween => new DateTime(year, 10, 31), + WesternHoliday.VeteransDay => new DateTime(year, 11, 11), + WesternHoliday.Christmas => new DateTime(year, 12, 25), + WesternHoliday.Thanksgiving => GetNthDayOfWeek(year, 11, DayOfWeek.Thursday, 4), + WesternHoliday.MothersDay => GetNthDayOfWeek(year, 5, DayOfWeek.Sunday, 2), + WesternHoliday.FathersDay => GetNthDayOfWeek(year, 6, DayOfWeek.Sunday, 3), + WesternHoliday.LaborDay => GetNthDayOfWeek(year, 9, DayOfWeek.Monday, 1), + WesternHoliday.MemorialDay => GetLastDayOfWeek(year, 5, DayOfWeek.Monday), + _ => throw new ArgumentOutOfRangeException(nameof(holiday)) + }; + } + + /// + /// 获取某月第N个星期几 + /// + private static DateTime GetNthDayOfWeek(int year, int month, DayOfWeek dayOfWeek, int n) + { + var firstDay = new DateTime(year, month, 1); + var daysToAdd = ((int)dayOfWeek - (int)firstDay.DayOfWeek + 7) % 7; + var result = firstDay.AddDays(daysToAdd + (n - 1) * 7); + return result; + } + + /// + /// 获取某月最后一个星期几 + /// + private static DateTime GetLastDayOfWeek(int year, int month, DayOfWeek dayOfWeek) + { + var lastDay = new DateTime(year, month, DateTime.DaysInMonth(year, month)); + var daysToSubtract = ((int)lastDay.DayOfWeek - (int)dayOfWeek + 7) % 7; + return lastDay.AddDays(-daysToSubtract); + } + } + + /// + /// 西式节日 + /// + public enum WesternHoliday + { + /// + /// 元旦 + /// + NewYear, + + /// + /// 情人节 + /// + ValentinesDay, + + /// + /// 圣帕特里克节 + /// + StPatricksDay, + + /// + /// 愚人节 + /// + AprilFools, + + /// + /// 美国独立日 + /// + IndependenceDay, + + /// + /// 万圣节 + /// + Halloween, + + /// + /// 退伍军人节 + /// + VeteransDay, + + /// + /// 圣诞节 + /// + Christmas, + + /// + /// 感恩节 + /// + Thanksgiving, + + /// + /// 母亲节 + /// + MothersDay, + + /// + /// 父亲节 + /// + FathersDay, + + /// + /// 劳动节(美国) + /// + LaborDay, + + /// + /// 阵亡将士纪念日(美国) + /// + MemorialDay + } +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs index 8dc4d14..8e9538e 100644 --- a/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs +++ b/EasyTool.Core/DateTimeCategory/LunarCalendarUtil.cs @@ -1,351 +1,448 @@ -using System; +using System; +using System.Collections.Generic; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// - /// 农历日期工具类 + /// 农历日历工具类 + /// 提供公历与农历之间的转换 /// - public class LunarCalendarUtil + public static class LunarCalendarUtil { + // 农历数据 1900-2100年 + // 每个数据表示一年,包含:月份天数信息、闰月信息 + private static readonly uint[] LunarInfo = { + 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, + 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, + 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, + 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, + 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, + 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, + 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, + 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, + 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, + 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, + 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, + 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, + 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, + 0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, + 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, + 0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, + 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, + 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0, + 0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160, + 0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, + 0x0d520 + }; - #region 基础数据 + // 天干 + private static readonly string[] TianGan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; - /// - /// 中文数字 - /// - private static readonly string[] ChineseNumbers = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; - /// - /// 天干 - /// - private static readonly string[] Gan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; - /// - /// 地支 - /// - private static readonly string[] Zhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; - /// - /// 生肖 - /// - private static readonly string[] Animal = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; - /// - /// 农历月份 - /// - private static readonly string[] MonthNames = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; - /// - /// 农历日期头 - /// - private static readonly string[] DayNames = { "初", "十", "廿", "三" }; - private static readonly string[] SolarTerm = { - "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", - "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", - "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" + // 地支 + private static readonly string[] DiZhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; + + // 生肖 + private static readonly string[] ShengXiao = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; + + // 农历月份 + private static readonly string[] LunarMonths = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊" }; + + // 农历日期 + private static readonly string[] LunarDays = { + "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", + "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", + "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" }; /// - /// 支持查询的最小农历年份 + /// 公历转农历 /// - private const int MinYear = 1900; - - private static readonly int[] LunarMonthDays = + /// 公历日期 + /// 农历信息 + public static LunarDate SolarToLunar(DateTime date) { - 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, // 1901-1910 - 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, // 1911-1920 - 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, // 1921-1930 - 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, // 1931-1940 - 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, // 1941-1950 - 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, // 1951-1960 - 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, // 1961-1970 - 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, // 1971-1980 - 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, // 1981-1990 - 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, // 1991-2000 - 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, // 2001-2010 - 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, // 2011-2020 - 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, // 2021-2030 - 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, // 2031-2040 - 0x06d20, 0x0ada0, 0x14b63, 0x09370, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, // 2041-2050 - 0x0aae0, 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, // 2051-2060 - 0x055d4, 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, // 2061-2070 - 0x052b0, 0x0b273, 0x0d950, 0x05b57, 0x056d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, // 2071-2080 - 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x0ada0, 0x095d0, // 2081-2090 - 0x04bd5, 0x04ad0, 0x0a4d0, 0x1d0b2, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, // 2091-2100 - 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, 0x14b63, 0x09370, 0x04970, // 2101-2110 - 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, // 2111-2120 - }; + if (date.Year < 1900 || date.Year > 2100) + throw new ArgumentOutOfRangeException(nameof(date), "日期范围必须在 1900-2100 年之间"); - #endregion + // 计算与 1900 年 1 月 31 日(农历 1900 年正月初一)的天数差 + var baseDate = new DateTime(1900, 1, 31); + var offset = (int)(date - baseDate).TotalDays; + if (offset < 0) + throw new ArgumentOutOfRangeException(nameof(date), "日期必须在 1900 年 1 月 31 日之后"); + // 计算农历年 + int year = 1900; + int daysOfYear; + while (year < 2100 && offset > 0) + { + daysOfYear = GetLunarYearDays(year); + if (offset < daysOfYear) + break; - /// - /// 获取指定公历日期对应的农历日期 - /// - /// 公历日期 - /// 农历日期 如:庚子鼠年正月初一 - public static string GetLunarDate(DateTime dateTime) - { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarYear(lunarDate[0])}年{GetLunarMonth(lunarDate[1])}月{GetLunarDay(lunarDate[2])}"; + offset -= daysOfYear; + year++; + } + + // 计算农历月和日 + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); // 闰月 + var isLeap = false; + int month = 1; + int daysOfMonth; + + while (month <= 12 && offset > 0) + { + daysOfMonth = GetLunarMonthDays(year, month, false); + if (offset < daysOfMonth) + break; + + offset -= daysOfMonth; + + // 检查是否有闰月 + if (leapMonth == month && !isLeap) + { + isLeap = true; + daysOfMonth = GetLunarMonthDays(year, month, true); + if (offset < daysOfMonth) + break; + + offset -= daysOfMonth; + isLeap = false; + } + + month++; + } + + return new LunarDate + { + Year = year, + Month = month, + Day = offset + 1, + IsLeapMonth = isLeap, + YearString = GetYearString(year), + MonthString = GetMonthString(month, isLeap), + DayString = LunarDays[offset], + GanZhiYear = GetGanZhiYear(year), + GanZhiMonth = GetGanZhiMonth(year, month), + GanZhiDay = GetGanZhiDay(date), + ShengXiao = GetShengXiao(year) + }; } /// - /// 获取农历年份 + /// 农历转公历 /// - /// 公历日期 - /// 农历年份(字符串)如:庚子鼠年 - public static string GetLunarYear(DateTime dateTime) + /// 农历年 + /// 农历月 + /// 农历日 + /// 是否闰月 + /// 公历日期 + public static DateTime LunarToSolar(int year, int month, int day, bool isLeapMonth = false) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarYear(lunarDate[0])}年"; + if (year < 1900 || year > 2100) + throw new ArgumentOutOfRangeException(nameof(year), "年份必须在 1900-2100 年之间"); + + var baseDate = new DateTime(1900, 1, 31); + var offset = 0; + + // 计算年份偏移 + for (int y = 1900; y < year; y++) + { + offset += GetLunarYearDays(y); + } + + // 计算月份偏移 + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); + + for (int m = 1; m < month; m++) + { + offset += GetLunarMonthDays(year, m, false); + + // 如果是闰月之前的月份,还要加上闰月的天数 + if (m == leapMonth && !isLeapMonth) + { + offset += GetLunarMonthDays(year, m, true); + } + } + + // 如果是闰月,加上正常月的天数 + if (isLeapMonth) + { + offset += GetLunarMonthDays(year, month, false); + } + + // 加上日期偏移 + offset += day - 1; + + return baseDate.AddDays(offset); } /// - /// 获取天干 + /// 获取农历年份的天数 /// - /// 公历日期 - /// 农历天干(字符串)如:庚 - public static string GetTianGan(DateTime dateTime) + public static int GetLunarYearDays(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Gan[(lunarDate[0] - 4) % 10]}"; + var yearInfo = LunarInfo[year - 1900]; + var leapMonth = (int)(yearInfo & 0xf); + var leapDays = leapMonth > 0 ? GetLunarMonthDays(year, leapMonth, true) : 0; + + var days = 0; + for (int i = 0x8000; i > 0x8; i >>= 1) + { + days += (yearInfo & i) != 0 ? 30 : 29; + } + + return days + leapDays; } /// - /// 获取地支 + /// 获取农历月份的天数 /// - /// 公历日期 - /// 农历地支(字符串)如 子 - public static string GetDiZhi(DateTime dateTime) + public static int GetLunarMonthDays(int year, int month, bool isLeap) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Zhi[(lunarDate[0] - 4) % 12]}"; + var yearInfo = LunarInfo[year - 1900]; + + if (isLeap) + { + return (yearInfo & 0x10000) != 0 ? 30 : 29; + } + + var bit = 0x8000 >> (month - 1); + return (yearInfo & bit) != 0 ? 30 : 29; } /// - /// 获取生肖 + /// 获取闰月(0 表示没有闰月) /// - /// 公历日期 - /// 农历生肖(字符串)如 鼠 - public static string GetChineseZodiac(DateTime dateTime) + public static int GetLeapMonth(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{Animal[(lunarDate[0] - 4) % 12]}"; + var yearInfo = LunarInfo[year - 1900]; + return (int)(yearInfo & 0xf); } /// - /// 获取农历月份 + /// 获取干支年 /// - /// 公历日期 - /// 农历月份(字符串)如:正月 - public static string GetLunarMonth(DateTime dateTime) + public static string GetGanZhiYear(int year) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarMonth(lunarDate[1])}月"; + var ganIndex = (year - 4) % 10; + var zhiIndex = (year - 4) % 12; + + if (ganIndex < 0) ganIndex += 10; + if (zhiIndex < 0) zhiIndex += 12; + + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取农历日期 + /// 获取干支月 /// - /// 公历日期 - /// 农历日期(字符串)如:廿三 - public static string GetLunarDay(DateTime dateTime) + public static string GetGanZhiMonth(int year, int month) { - int[] lunarDate = GetLunarDate(dateTime.Year, dateTime.Month, dateTime.Day); - return $"{GetLunarDay(lunarDate[2])}"; + var ganIndex = (year * 12 + month + 13) % 10; + var zhiIndex = (month + 1) % 12; + + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取指定公历日期对应的农历日期 + /// 获取干支日 /// - /// 公历年份 - /// 公历月份 - /// 公历日期 - /// 农历日期 - private static int[] GetLunarDate(int year, int month, int day) + public static string GetGanZhiDay(DateTime date) { - int leapMonth = GetLunarLeapMonth(year); - int offset = (new DateTime(year, month, day) - new DateTime(1900, 1, 31)).Days; - - int iYear = 0, iMonth = 0, iDay = 0; - bool leap = false; - - for (iYear = 1900; iYear <= 2100 && offset > 0; iYear++) - { - offset -= GetLunarYearDays(iYear); - } - - if (offset < 0) - { - offset += GetLunarYearDays(--iYear); - } - - int yearDays = GetLunarYearDays(iYear); - int leapMonthIndex = GetLunarLeapMonth(iYear); + var baseDate = new DateTime(1900, 1, 31); + var offset = (int)(date - baseDate).TotalDays; - for (iMonth = 1; iMonth <= 12 && offset > 0; iMonth++) - { - if (leapMonthIndex > 0 && iMonth == leapMonthIndex + 1 && !leap) - { - iMonth--; - leap = true; - yearDays = GetLunarLeapMonthDays(iYear); - } - else - { - yearDays = GetLunarMonthDays(iYear, iMonth); - } - - if (leap && iMonth == leapMonthIndex + 1) - { - leap = false; - } - - offset -= yearDays; - } + var ganIndex = (offset + 10) % 10; + var zhiIndex = (offset + 12) % 12; - if (offset == 0 && leapMonthIndex > 0 && iMonth == leapMonthIndex + 1) - { - if (leap) - { - leap = false; - } - else - { - leap = true; - iMonth--; - } - } - - if (offset < 0) - { - offset += yearDays; - iMonth--; - } - - iDay = offset + 1; - return new[] { iYear, iMonth, iDay, leapMonth, leap ? 1 : 0 }; + return TianGan[ganIndex] + DiZhi[zhiIndex]; } /// - /// 获取农历年份 + /// 获取生肖 /// - /// 农历年份(数字) - /// 农历年份(字符串)如:庚子鼠年 - private static string GetLunarYear(int year) + public static string GetShengXiao(int year) { - return $"{Gan[(year - 4) % 10]}{Zhi[(year - 4) % 12]}{Animal[(year - 4) % 12]}年"; + var index = (year - 4) % 12; + if (index < 0) index += 12; + return ShengXiao[index]; } /// - /// 获取农历月份 + /// 获取生肖(GetShengXiao 的别名) /// - /// 农历月份(数字) - /// 农历月份(字符串)如:正月 - private static string GetLunarMonth(int month) + /// 日期 + /// 生肖 + public static string GetChineseZodiac(DateTime date) { - return MonthNames[month - 1] + "月"; + return GetShengXiao(date.Year); } /// - /// 获取指定年份的农历年份的天数 + /// 获取年份字符串 /// - /// 指定年份 - /// 农历年份的天数 - private static int GetLunarYearDays(int year) + private static string GetYearString(int year) { - int sum = 348; - for (int i = 0x8000; i > 0x8; i >>= 1) + var digits = new[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; + var result = ""; + while (year > 0) { - if ((LunarMonthDays[year - MinYear] & i) != 0) - { - sum += 1; - } + result = digits[year % 10] + result; + year /= 10; } - return sum + GetLunarLeapMonthDays(year); + return result; } + /// + /// 获取月份字符串 + /// + private static string GetMonthString(int month, bool isLeap) + { + return (isLeap ? "闰" : "") + LunarMonths[month - 1] + "月"; + } /// - /// 获取农历闰月月份 + /// 获取中国传统节日 /// - /// 农历年份 - /// 闰月月份(若当年没有闰月,返回0) - private static int GetLunarLeapMonth(int year) + public static List GetLunarFestivals(int year) { - int leapMonth = LunarMonthDays[year - MinYear] >> 16; - if (leapMonth > 0 && leapMonth < 13 && GetBit(LunarMonthDays[year - MinYear], 16 - leapMonth) == 0) - { - return leapMonth; - } - else + return new List { - return 0; - } + new LunarFestival { Name = "春节", Month = 1, Day = 1, Description = "农历新年" }, + new LunarFestival { Name = "元宵节", Month = 1, Day = 15, Description = "正月十五" }, + new LunarFestival { Name = "端午节", Month = 5, Day = 5, Description = "五月初五" }, + new LunarFestival { Name = "七夕节", Month = 7, Day = 7, Description = "七月初七" }, + new LunarFestival { Name = "中元节", Month = 7, Day = 15, Description = "七月十五" }, + new LunarFestival { Name = "中秋节", Month = 8, Day = 15, Description = "八月十五" }, + new LunarFestival { Name = "重阳节", Month = 9, Day = 9, Description = "九月初九" }, + new LunarFestival { Name = "腊八节", Month = 12, Day = 8, Description = "腊月初八" }, + new LunarFestival { Name = "除夕", Month = 12, Day = 30, Description = "腊月最后一天" } + }; } /// - /// 获取农历日期 + /// 判断是否是节日 /// - /// 农历日期(数字) - /// 农历日期(字符串)如:廿三 - private static string GetLunarDay(int day) + public static string? GetFestivalName(int lunarMonth, int lunarDay) { - int d1 = day / 10; - int d2 = day % 10; - if (d1 == 0) - { - d1 = 3; - } - if (d2 == 0) + return (lunarMonth, lunarDay) switch { - d2 = 10; - } - if (d2 == 20) - { - d2 = 0; - d1++; - } - return $"{DayNames[d1 - 1]}{ChineseNumbers[d2]}"; + (1, 1) => "春节", + (1, 15) => "元宵节", + (5, 5) => "端午节", + (7, 7) => "七夕节", + (7, 15) => "中元节", + (8, 15) => "中秋节", + (9, 9) => "重阳节", + (12, 8) => "腊八节", + (12, 30) => "除夕", + _ => null + }; } + } + /// + /// 农历日期 + /// + public class LunarDate + { + /// + /// 年 + /// + public int Year { get; set; } + /// + /// 月 + /// + public int Month { get; set; } /// - /// 获取指定年份和月份的农历月份的天数 + /// 日 /// - /// 指定年份 - /// 指定月份 - /// 农历月份的天数 29或30 - private static int GetLunarMonthDays(int year, int month) - { - return (LunarMonthDays[year - MinYear] & (0x10000 >> month)) == 0 ? 29 : 30; - } + public int Day { get; set; } /// - /// 获取指定年份的农历闰月的天数 + /// 是否闰月 /// - /// 指定年份 - /// 农历闰月的天数(29或30,若当年没有闰月,返回0) - private static int GetLunarLeapMonthDays(int year) - { - if (GetLunarLeapMonth(year) > 0) - { - return (LunarMonthDays[year - MinYear] & 0x10000) == 0 ? 29 : 30; - } - else - { - return 0; - } - } + public bool IsLeapMonth { get; set; } + + /// + /// 年份字符串(中文) + /// + public string YearString { get; set; } = string.Empty; + + /// + /// 月份字符串(中文) + /// + public string MonthString { get; set; } = string.Empty; + + /// + /// 日期字符串(中文) + /// + public string DayString { get; set; } = string.Empty; + + /// + /// 干支年 + /// + public string GanZhiYear { get; set; } = string.Empty; + + /// + /// 干支月 + /// + public string GanZhiMonth { get; set; } = string.Empty; + + /// + /// 干支日 + /// + public string GanZhiDay { get; set; } = string.Empty; + + /// + /// 生肖 + /// + public string ShengXiao { get; set; } = string.Empty; + + /// + /// 完整日期字符串 + /// + public string FullString => $"{YearString}年{MonthString}{DayString}"; /// - /// 获取指定整数的指定位的值 + /// 干支日期字符串 /// - /// 指定整数 - /// 指定位(从右往左数,最右边的位为第1位) - /// 指定位的值(0或1) - private static int GetBit(int num, int bit) + public string GanZhiString => $"{GanZhiYear}年{GanZhiMonth}月{GanZhiDay}日"; + + public override string ToString() { - return (num >> (bit - 1)) & 1; + return $"{FullString}({GanZhiString}){ShengXiao}年"; } + } + /// + /// 农历节日 + /// + public class LunarFestival + { + /// + /// 节日名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 农历月 + /// + public int Month { get; set; } + + /// + /// 农历日 + /// + public int Day { get; set; } + + /// + /// 描述 + /// + public string Description { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs b/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs new file mode 100644 index 0000000..c19bf0f --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/SolarTermUtil.cs @@ -0,0 +1,348 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 二十四节气工具类 + /// 计算二十四节气日期 + /// + public static class SolarTermUtil + { + // 二十四节气名称 + private static readonly string[] SolarTerms = { + "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", + "清明", "谷雨", "立夏", "小满", "芒种", "夏至", + "小暑", "大暑", "立秋", "处暑", "白露", "秋分", + "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" + }; + + // 节气对应的公历日期(基准数据,1900年) + // 实际计算时会根据年份进行调整 + private static readonly (int Month, int Day, int Hour)[] SolarTermBase = { + (1, 6, 0), // 小寒 + (1, 20, 0), // 大寒 + (2, 4, 0), // 立春 + (2, 19, 0), // 雨水 + (3, 6, 0), // 惊蛰 + (3, 21, 0), // 春分 + (4, 5, 0), // 清明 + (4, 20, 0), // 谷雨 + (5, 6, 0), // 立夏 + (5, 21, 0), // 小满 + (6, 6, 0), // 芒种 + (6, 21, 0), // 夏至 + (7, 7, 0), // 小暑 + (7, 23, 0), // 大暑 + (8, 8, 0), // 立秋 + (8, 23, 0), // 处暑 + (9, 8, 0), // 白露 + (9, 23, 0), // 秋分 + (10, 8, 0), // 寒露 + (10, 24, 0), // 霜降 + (11, 8, 0), // 立冬 + (11, 22, 0), // 小雪 + (12, 7, 0), // 大雪 + (12, 22, 0) // 冬至 + }; + + // 节气计算系数(简化算法) + // 基于1900年小寒为1月6日2时5分的基准 + private static readonly double[] TermCoefficients = { + 0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, + 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, + 18.5, 19.5, 20.5, 21.5, 22.5, 23.5 + }; + + /// + /// 获取指定年份的所有节气 + /// + /// 年份 + /// 节气列表 + public static List GetSolarTerms(int year) + { + var result = new List(); + + for (int i = 0; i < 24; i++) + { + var date = CalculateSolarTerm(year, i); + result.Add(new SolarTermInfo + { + Index = i, + Name = SolarTerms[i], + Date = date, + Month = date.Month, + Day = date.Day, + Type = i % 2 == 0 ? SolarTermType.Jie : SolarTermType.Qi + }); + } + + return result; + } + + /// + /// 获取指定日期所在的节气 + /// + /// 日期 + /// 节气信息,如果不是节气日则返回 null + public static SolarTermInfo? GetSolarTerm(DateTime date) + { + var terms = GetSolarTerms(date.Year); + + // 检查是否是前一年的最后一个节气 + if (date.Month == 1 && date.Day < 6) + { + var lastYearTerms = GetSolarTerms(date.Year - 1); + var lastTerm = lastYearTerms[23]; // 冬至 + if (lastTerm.Date.Date == date.Date) + return lastTerm; + } + + foreach (var term in terms) + { + if (term.Date.Date == date.Date) + return term; + } + + return null; + } + + /// + /// 获取下一个节气 + /// + /// 基准日期 + /// 下一个节气 + public static SolarTermInfo GetNextSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + foreach (var term in terms) + { + if (term.Date > date) + return term; + } + + // 如果当前年份没有了,返回下一年的第一个节气 + return GetSolarTerms(year + 1)[0]; + } + + /// + /// 获取上一个节气 + /// + /// 基准日期 + /// 上一个节气 + public static SolarTermInfo GetPreviousSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + for (int i = terms.Count - 1; i >= 0; i--) + { + if (terms[i].Date < date) + return terms[i]; + } + + // 如果当前年份没有了,返回上一年的最后一个节气 + return GetSolarTerms(year - 1)[23]; + } + + /// + /// 获取当前节气(今天或之前最近的节气) + /// + /// 日期 + /// 当前节气 + public static SolarTermInfo GetCurrentSolarTerm(DateTime date) + { + var year = date.Year; + var terms = GetSolarTerms(year); + + SolarTermInfo? current = null; + foreach (var term in terms) + { + if (term.Date <= date) + current = term; + else + break; + } + + if (current != null) + return current; + + // 返回上一年的最后一个节气 + return GetSolarTerms(year - 1)[23]; + } + + /// + /// 计算节气日期 + /// + private static DateTime CalculateSolarTerm(int year, int termIndex) + { + // 使用简化的节气计算算法 + // 基于黄经计算(每个节气相差15度) + + var baseDate = new DateTime(year, 1, 6, 2, 5, 0); // 1900年小寒基准 + var baseYear = 1900; + + // 计算从1900年到目标年份的累积偏移 + var totalDays = 0.0; + + // 简化计算:使用回归年长度 365.2422 天 + var tropicalYear = 365.2422; + var yearOffset = (year - baseYear) * tropicalYear; + + // 每个节气平均间隔约 15.2184 天 + var termOffset = termIndex * 15.2184; + + // 计算总偏移 + totalDays = yearOffset + termOffset; + + // 从基准日期计算 + var result = baseDate.AddDays(totalDays - (year - baseYear) * tropicalYear); + + // 调整到正确的年份 + result = new DateTime(year, result.Month, result.Day, result.Hour, result.Minute, 0); + + // 使用更精确的表格数据进行微调 + var (month, day, hour) = SolarTermBase[termIndex]; + + // 年份修正(每4年大约有1天的偏差) + var correction = (year - 1900) * 0.2422; + var correctedDay = day + (int)Math.Round(correction); + + // 处理月份边界 + if (correctedDay < 1) + { + month--; + if (month == 0) month = 12; + correctedDay += DateTime.DaysInMonth(year, month); + } + else if (correctedDay > DateTime.DaysInMonth(year, month)) + { + correctedDay -= DateTime.DaysInMonth(year, month); + month++; + if (month > 12) month = 1; + } + + return new DateTime(year, month, correctedDay, hour, 0, 0); + } + + /// + /// 获取节气名称 + /// + /// 节气索引(0-23) + /// 节气名称 + public static string GetSolarTermName(int index) + { + if (index < 0 || index >= 24) + throw new ArgumentOutOfRangeException(nameof(index), "节气索引必须在 0-23 之间"); + + return SolarTerms[index]; + } + + /// + /// 获取季节 + /// + /// 节气索引 + /// 季节 + public static Season GetSeason(int termIndex) + { + return termIndex switch + { + >= 0 and < 6 => Season.Spring, + >= 6 and < 12 => Season.Summer, + >= 12 and < 18 => Season.Autumn, + _ => Season.Winter + }; + } + + /// + /// 判断是否是"节"(奇数索引) + /// + public static bool IsJie(int termIndex) + { + return termIndex % 2 == 0; + } + + /// + /// 判断是否是"气"(偶数索引) + /// + public static bool IsQi(int termIndex) + { + return termIndex % 2 == 1; + } + } + + /// + /// 节气信息 + /// + public class SolarTermInfo + { + /// + /// 节气索引(0-23) + /// + public int Index { get; set; } + + /// + /// 节气名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 公历日期 + /// + public DateTime Date { get; set; } + + /// + /// 月份 + /// + public int Month { get; set; } + + /// + /// 日期 + /// + public int Day { get; set; } + + /// + /// 节气类型 + /// + public SolarTermType Type { get; set; } + + /// + /// 所属季节 + /// + public Season Season => SolarTermUtil.GetSeason(Index); + + public override string ToString() + { + return $"{Name} ({Date:yyyy-MM-dd})"; + } + } + + /// + /// 节气类型 + /// + public enum SolarTermType + { + /// + /// 节(每月的第一个节气) + /// + Jie, + + /// + /// 气(每月的第二个节气) + /// + Qi + } + + /// + /// 季节 + /// + public enum Season + { + Spring, + Summer, + Autumn, + Winter + } +} diff --git a/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs b/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs new file mode 100644 index 0000000..986da31 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/StopwatchUtil.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 计时器工具类 + /// 提供便捷的计时功能 + /// + public static class StopwatchUtil + { + /// + /// 创建并启动计时器 + /// + /// 计时器 + public static Stopwatch StartNew() + { + return Stopwatch.StartNew(); + } + + /// + /// 测量操作执行时间 + /// + /// 操作 + /// 执行时间 + public static TimeSpan Measure(Action action) + { + var stopwatch = StartNew(); + action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 测量操作执行时间(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 执行时间和结果 + public static (TimeSpan Elapsed, T Result) Measure(Func func) + { + var stopwatch = StartNew(); + var result = func(); + stopwatch.Stop(); + return (stopwatch.Elapsed, result); + } + + /// + /// 异步测量操作执行时间 + /// + /// 操作 + /// 执行时间 + public static async Task MeasureAsync(Func action) + { + var stopwatch = StartNew(); + await action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 异步测量操作执行时间(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 执行时间和结果 + public static async Task<(TimeSpan Elapsed, T Result)> MeasureAsync(Func> func) + { + var stopwatch = StartNew(); + var result = await func(); + stopwatch.Stop(); + return (stopwatch.Elapsed, result); + } + + /// + /// 使用计时器执行操作 + /// + /// 操作 + /// 计时回调 + public static void WithTimer(Action action, Action callback) + { + var elapsed = Measure(action); + callback(elapsed); + } + + /// + /// 使用计时器执行操作 + /// + /// 返回值类型 + /// 操作 + /// 计时回调 + /// 操作结果 + public static T WithTimer(Func func, Action callback) + { + var (elapsed, result) = Measure(func); + callback(elapsed); + return result; + } + + /// + /// 异步使用计时器执行操作 + /// + /// 操作 + /// 计时回调 + public static async Task WithTimerAsync(Func action, Action callback) + { + var elapsed = await MeasureAsync(action); + callback(elapsed); + } + + /// + /// 异步使用计时器执行操作 + /// + /// 返回值类型 + /// 操作 + /// 计时回调 + /// 操作结果 + public static async Task WithTimerAsync(Func> func, Action callback) + { + var (elapsed, result) = await MeasureAsync(func); + callback(elapsed); + return result; + } + + /// + /// 等待指定时间 + /// + /// 等待时间 + public static void Wait(TimeSpan duration) + { + Thread.Sleep(duration); + } + + /// + /// 异步等待指定时间 + /// + /// 等待时间 + /// 取消令牌 + public static Task WaitAsync(TimeSpan duration, CancellationToken cancellationToken = default) + { + return Task.Delay(duration, cancellationToken); + } + + /// + /// 执行带超时的操作 + /// + /// 操作 + /// 超时时间 + /// 是否在超时前完成 + public static bool TryExecute(Action action, TimeSpan timeout) + { + var task = Task.Run(action); + return task.Wait(timeout); + } + + /// + /// 异步执行带超时的操作 + /// + /// 操作 + /// 超时时间 + /// 取消令牌 + /// 是否在超时前完成 + public static async Task TryExecuteAsync(Func action, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + await action(); + return true; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + return false; + } + } + + /// + /// 执行带超时的操作(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 超时时间 + /// 结果 + /// 是否在超时前完成 + public static bool TryExecute(Func func, TimeSpan timeout, out T? result) + { + result = default; + var task = Task.Run(func); + + if (task.Wait(timeout)) + { + result = task.Result; + return true; + } + + return false; + } + + /// + /// 异步执行带超时的操作(带返回值) + /// + /// 返回值类型 + /// 操作 + /// 超时时间 + /// 取消令牌 + /// 结果或默认值 + public static async Task<(bool Success, T? Result)> TryExecuteAsync(Func> func, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + var result = await func(); + return (true, result); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + return (false, default); + } + } + + /// + /// 格式化时间输出 + /// + /// 时间 + /// 格式化字符串 + public static string FormatTime(TimeSpan time) + { + if (time.TotalSeconds >= 1) + return $"{time.TotalSeconds:F2}s"; + if (time.TotalMilliseconds >= 1) + return $"{time.TotalMilliseconds:F2}ms"; +#if NET7_0_OR_GREATER + if (time.TotalMicroseconds >= 1) + return $"{time.TotalMicroseconds:F2}μs"; + return $"{time.TotalNanoseconds:F2}ns"; +#else + // For older frameworks, use ticks for sub-millisecond precision + var ticks = time.Ticks; + if (ticks >= 10) // >= 1 microsecond (10 ticks = 1 μs) + return $"{ticks / 10.0:F2}μs"; + return $"{ticks * 100.0:F2}ns"; +#endif + } + + /// + /// 格式化时间为详细字符串 + /// + /// 时间 + /// 格式化字符串 + public static string FormatTimeDetailed(TimeSpan time) + { + var parts = new List(); + + if (time.Days > 0) + parts.Add($"{time.Days}天"); + if (time.Hours > 0) + parts.Add($"{time.Hours}小时"); + if (time.Minutes > 0) + parts.Add($"{time.Minutes}分钟"); + if (time.Seconds > 0) + parts.Add($"{time.Seconds}秒"); + if (time.Milliseconds > 0) + parts.Add($"{time.Milliseconds}毫秒"); + + return parts.Count > 0 ? string.Join(" ", parts) : "0毫秒"; + } + } +} diff --git a/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs b/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs new file mode 100644 index 0000000..d3b005a --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/TimeZoneUtil.cs @@ -0,0 +1,453 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 时区转换工具类 + /// 提供时区转换和时区信息查询功能 + /// + public static class TimeZoneUtil + { + #region 常用时区 + + /// + /// UTC时区 + /// + public static TimeZoneInfo UtcTimeZone => TimeZoneInfo.Utc; + + /// + /// 本地时区 + /// + public static TimeZoneInfo LocalTimeZone => TimeZoneInfo.Local; + + /// + /// 中国标准时间时区(UTC+8) + /// + public static TimeZoneInfo ChinaStandardTime => FindTimeZoneById("China Standard Time", "Asia/Shanghai", 8); + + /// + /// 美国东部时区 + /// + public static TimeZoneInfo USEasternTime => FindTimeZoneById("Eastern Standard Time", "America/New_York", -5); + + /// + /// 美国太平洋时区 + /// + public static TimeZoneInfo USPacificTime => FindTimeZoneById("Pacific Standard Time", "America/Los_Angeles", -8); + + /// + /// 欧洲伦敦时区 + /// + public static TimeZoneInfo LondonTime => FindTimeZoneById("GMT Standard Time", "Europe/London", 0); + + /// + /// 日本标准时间时区 + /// + public static TimeZoneInfo JapanStandardTime => FindTimeZoneById("Tokyo Standard Time", "Asia/Tokyo", 9); + + /// + /// 韩国标准时间时区 + /// + public static TimeZoneInfo KoreaStandardTime => FindTimeZoneById("Korea Standard Time", "Asia/Seoul", 9); + + /// + /// 新加坡时区 + /// + public static TimeZoneInfo SingaporeTime => FindTimeZoneById("Singapore Standard Time", "Asia/Singapore", 8); + + /// + /// 澳大利亚悉尼时区 + /// + public static TimeZoneInfo SydneyTime => FindTimeZoneById("AUS Eastern Standard Time", "Australia/Sydney", 10); + + /// + /// 印度标准时间时区 + /// + public static TimeZoneInfo IndiaStandardTime => FindTimeZoneById("India Standard Time", "Asia/Kolkata", 5.5); + + /// + /// 德国柏林时区 + /// + public static TimeZoneInfo BerlinTime => FindTimeZoneById("W. Europe Standard Time", "Europe/Berlin", 1); + + private static TimeZoneInfo FindTimeZoneById(string windowsId, string ianaId, double offsetHours) + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(windowsId); + } + catch + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(ianaId); + } + catch + { + // 创建自定义时区 + return CreateCustomTimeZone(windowsId, offsetHours); + } + } + } + + private static TimeZoneInfo CreateCustomTimeZone(string id, double offsetHours) + { + var offset = TimeSpan.FromHours(offsetHours); + return TimeZoneInfo.CreateCustomTimeZone(id, offset, id, id); + } + + #endregion + + #region 时区转换 + + /// + /// 将时间从一个时区转换到另一个时区 + /// + /// 要转换的时间 + /// 源时区 + /// 目标时区 + /// 转换后的时间 + public static DateTime ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone) + { + return TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, destinationTimeZone); + } + + /// + /// 将时间转换为UTC时间 + /// + /// 本地时间 + /// UTC时间 + public static DateTime ToUtc(DateTime dateTime) + { + return dateTime.Kind switch + { + DateTimeKind.Utc => dateTime, + DateTimeKind.Local => dateTime.ToUniversalTime(), + _ => DateTime.SpecifyKind(dateTime, DateTimeKind.Local).ToUniversalTime() + }; + } + + /// + /// 将UTC时间转换为本地时间 + /// + /// UTC时间 + /// 本地时间 + public static DateTime FromUtc(DateTime utcDateTime) + { + return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.Local); + } + + /// + /// 将时间转换为中国标准时间 + /// + /// 源时间 + /// 源时区(默认本地时区) + /// 中国标准时间 + public static DateTime ToChinaTime(DateTime dateTime, TimeZoneInfo? sourceTimeZone = null) + { + sourceTimeZone ??= TimeZoneInfo.Local; + return ConvertTime(dateTime, sourceTimeZone, ChinaStandardTime); + } + + /// + /// 将时间转换为美国东部时间 + /// + /// 源时间 + /// 源时区(默认本地时区) + /// 美国东部时间 + public static DateTime ToUSEasternTime(DateTime dateTime, TimeZoneInfo? sourceTimeZone = null) + { + sourceTimeZone ??= TimeZoneInfo.Local; + return ConvertTime(dateTime, sourceTimeZone, USEasternTime); + } + + /// + /// 将时间转换为指定偏移量时区的时间 + /// + /// 源时间 + /// 源时区偏移量(小时) + /// 目标时区偏移量(小时) + /// 目标时区时间 + public static DateTime ConvertByOffset(DateTime dateTime, double sourceOffset, double targetOffset) + { + // 先转为UTC + var utc = dateTime.AddHours(-sourceOffset); + // 再转为目标时区 + return utc.AddHours(targetOffset); + } + + /// + /// 获取指定时区当前时间 + /// + /// 时区 + /// 当前时间 + public static DateTime GetNow(TimeZoneInfo timeZone) + { + return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone); + } + + /// + /// 获取中国当前时间 + /// + /// 中国当前时间 + public static DateTime GetChinaNow() + { + return GetNow(ChinaStandardTime); + } + + #endregion + + #region 时区信息 + + /// + /// 获取所有时区 + /// + /// 时区列表 + public static IReadOnlyCollection GetAllTimeZones() + { + return TimeZoneInfo.GetSystemTimeZones(); + } + + /// + /// 根据ID获取时区 + /// + /// 时区ID + /// 时区信息 + public static TimeZoneInfo? GetTimeZoneById(string id) + { + try + { + return TimeZoneInfo.FindSystemTimeZoneById(id); + } + catch + { + return null; + } + } + + /// + /// 根据偏移量查找时区 + /// + /// 偏移量(小时) + /// 匹配的时区列表 + public static List GetTimeZonesByOffset(double offsetHours) + { + var offset = TimeSpan.FromHours(offsetHours); + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.BaseUtcOffset == offset) + .ToList(); + } + + /// + /// 获取时区偏移量 + /// + /// 时区 + /// 偏移量(小时) + public static double GetOffsetHours(TimeZoneInfo timeZone) + { + return timeZone.BaseUtcOffset.TotalHours; + } + + /// + /// 获取时区偏移量字符串 + /// + /// 时区 + /// 偏移量字符串(如+08:00) + public static string GetOffsetString(TimeZoneInfo timeZone) + { + var offset = timeZone.BaseUtcOffset; + return $"{(offset >= TimeSpan.Zero ? "+" : "")}{offset:hh\\:mm}"; + } + + /// + /// 判断时区是否支持夏令时 + /// + /// 时区 + /// 是否支持夏令时 + public static bool SupportsDaylightSavingTime(TimeZoneInfo timeZone) + { + return timeZone.SupportsDaylightSavingTime; + } + + /// + /// 判断指定时间是否处于夏令时 + /// + /// 时间 + /// 时区 + /// 是否处于夏令时 + public static bool IsDaylightSavingTime(DateTime dateTime, TimeZoneInfo timeZone) + { + return timeZone.IsDaylightSavingTime(dateTime); + } + + /// + /// 获取时区当前偏移量(考虑夏令时) + /// + /// 时区 + /// 当前偏移量 + public static TimeSpan GetCurrentOffset(TimeZoneInfo timeZone) + { + var now = DateTime.UtcNow; + var offset = timeZone.GetUtcOffset(now); + return offset; + } + + #endregion + + #region DateTimeOffset + + /// + /// 创建DateTimeOffset + /// + /// 本地时间 + /// 时区 + /// DateTimeOffset + public static DateTimeOffset CreateDateTimeOffset(DateTime dateTime, TimeZoneInfo timeZone) + { + var utcTime = ConvertTime(dateTime, timeZone, TimeZoneInfo.Utc); + return new DateTimeOffset(utcTime, TimeSpan.Zero).ToOffset(timeZone.GetUtcOffset(dateTime)); + } + + /// + /// 将DateTimeOffset转换到指定时区 + /// + /// DateTimeOffset + /// 目标时区 + /// 转换后的DateTimeOffset + public static DateTimeOffset ConvertTime(DateTimeOffset dateTimeOffset, TimeZoneInfo timeZone) + { + return TimeZoneInfo.ConvertTime(dateTimeOffset, timeZone); + } + + #endregion + + #region 时区差异计算 + + /// + /// 计算两个时区之间的时间差 + /// + /// 时区1 + /// 时区2 + /// 时间差 + public static TimeSpan GetTimeDifference(TimeZoneInfo timeZone1, TimeZoneInfo timeZone2) + { + return timeZone1.BaseUtcOffset - timeZone2.BaseUtcOffset; + } + + /// + /// 计算两个时区之间的小时差 + /// + /// 时区1 + /// 时区2 + /// 小时差 + public static double GetHoursDifference(TimeZoneInfo timeZone1, TimeZoneInfo timeZone2) + { + return GetTimeDifference(timeZone1, timeZone2).TotalHours; + } + + #endregion + + #region 时区查找 + + /// + /// 根据名称模糊查找时区 + /// + /// 时区名称 + /// 匹配的时区列表 + public static List FindTimeZonesByName(string name) + { + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.DisplayName.Contains(name, StringComparison.OrdinalIgnoreCase) || + tz.Id.Contains(name, StringComparison.OrdinalIgnoreCase) || + tz.StandardName.Contains(name, StringComparison.OrdinalIgnoreCase)) + .ToList(); + } + + /// + /// 获取UTC+N时区列表 + /// + /// UTC偏移量(如8表示UTC+8) + /// 时区列表 + public static List GetUtcPlusTimeZones(int offset) + { + return TimeZoneInfo.GetSystemTimeZones() + .Where(tz => tz.BaseUtcOffset == TimeSpan.FromHours(offset)) + .ToList(); + } + + #endregion + + #region 格式化 + + /// + /// 格式化时区信息 + /// + /// 时区 + /// 格式化字符串 + public static string FormatTimeZone(TimeZoneInfo timeZone) + { + return $"{timeZone.Id} ({GetOffsetString(timeZone)}) {timeZone.DisplayName}"; + } + + /// + /// 格式化时间为带时区的字符串 + /// + /// 时间 + /// 时区 + /// 时间格式 + /// 格式化字符串 + public static string FormatDateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, string format = "yyyy-MM-dd HH:mm:ss") + { + var offset = GetOffsetString(timeZone); + return $"{dateTime.ToString(format)} (UTC{offset})"; + } + + #endregion + + #region 常用城市时区 + + /// + /// 获取常用城市时区映射 + /// + public static Dictionary GetCommonCityTimeZones() + { + return new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "北京", ChinaStandardTime }, + { "上海", ChinaStandardTime }, + { "香港", FindTimeZoneById("China Standard Time", "Asia/Hong_Kong", 8) }, + { "台北", FindTimeZoneById("Taipei Standard Time", "Asia/Taipei", 8) }, + { "东京", JapanStandardTime }, + { "首尔", KoreaStandardTime }, + { "新加坡", SingaporeTime }, + { "悉尼", SydneyTime }, + { "伦敦", LondonTime }, + { "巴黎", FindTimeZoneById("Romance Standard Time", "Europe/Paris", 1) }, + { "柏林", BerlinTime }, + { "纽约", USEasternTime }, + { "洛杉矶", USPacificTime }, + { "芝加哥", FindTimeZoneById("Central Standard Time", "America/Chicago", -6) }, + { "多伦多", FindTimeZoneById("Eastern Standard Time", "America/Toronto", -5) }, + { "温哥华", FindTimeZoneById("Pacific Standard Time", "America/Vancouver", -8) }, + { "迪拜", FindTimeZoneById("Arabian Standard Time", "Asia/Dubai", 4) }, + { "孟买", IndiaStandardTime }, + { "莫斯科", FindTimeZoneById("Russian Standard Time", "Europe/Moscow", 3) } + }; + } + + /// + /// 根据城市名获取时区 + /// + /// 城市名 + /// 时区信息 + public static TimeZoneInfo? GetTimeZoneByCity(string cityName) + { + var cityTimeZones = GetCommonCityTimeZones(); + return cityTimeZones.TryGetValue(cityName, out var timeZone) ? timeZone : null; + } + + #endregion + } +} diff --git a/EasyTool.Core/DateTimeCategory/TimerUtil.cs b/EasyTool.Core/DateTimeCategory/TimerUtil.cs index 68f56e4..2ea5253 100644 --- a/EasyTool.Core/DateTimeCategory/TimerUtil.cs +++ b/EasyTool.Core/DateTimeCategory/TimerUtil.cs @@ -1,147 +1,443 @@ -using System; -using System.Diagnostics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; -namespace EasyTool +namespace EasyTool.DateTimeCategory { /// - /// 计时器工具类,提供各种计时和时间间隔计算的方法。 + /// 定时器工具类 + /// 提供增强的定时器功能 /// - public class TimerUtil + public static class TimerUtil { /// - /// 记录程序启动时间。 + /// 创建一次性定时器 /// - private static readonly DateTime _startTime = DateTime.Now; + /// 延迟时间 + /// 回调 + /// 定时器 + public static Timer Once(TimeSpan delay, Action callback) + { + return new Timer(_ => callback(), null, delay, Timeout.InfiniteTimeSpan); + } + + /// + /// 创建一次性定时器(异步) + /// + /// 延迟时间 + /// 回调 + /// 定时器 + public static Timer OnceAsync(TimeSpan delay, Func callback) + { + Timer? timer = null; + timer = new Timer(async _ => + { + timer?.Dispose(); + await callback(); + }, null, delay, Timeout.InfiniteTimeSpan); + return timer; + } + + /// + /// 创建周期性定时器 + /// + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer Interval(TimeSpan interval, Action callback) + { + return new Timer(_ => callback(), null, interval, interval); + } + + /// + /// 创建周期性定时器(异步) + /// + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer IntervalAsync(TimeSpan interval, Func callback) + { + Timer? timer = null; + timer = new Timer(async _ => + { + await callback(); + }, null, interval, interval); + return timer; + } + + /// + /// 创建带延迟的周期性定时器 + /// + /// 首次执行延迟 + /// 间隔时间 + /// 回调 + /// 定时器 + public static Timer DelayedInterval(TimeSpan dueTime, TimeSpan period, Action callback) + { + return new Timer(_ => callback(), null, dueTime, period); + } + + /// + /// 等待指定时间后执行 + /// + /// 延迟时间 + /// 回调 + /// 可取消令牌 + public static CancellationTokenSource RunAfter(TimeSpan delay, Action callback) + { + var cts = new CancellationTokenSource(); + + Task.Run(async () => + { + try + { + await Task.Delay(delay, cts.Token); + if (!cts.Token.IsCancellationRequested) + { + callback(); + } + } + catch (OperationCanceledException) + { + // 取消时不执行回调 + } + }, cts.Token); + + return cts; + } + + /// + /// 异步等待指定时间后执行 + /// + /// 延迟时间 + /// 回调 + /// 可取消令牌 + public static CancellationTokenSource RunAfterAsync(TimeSpan delay, Func callback) + { + var cts = new CancellationTokenSource(); + + Task.Run(async () => + { + try + { + await Task.Delay(delay, cts.Token); + if (!cts.Token.IsCancellationRequested) + { + await callback(); + } + } + catch (OperationCanceledException) + { + // 取消时不执行回调 + } + }, cts.Token); + + return cts; + } + + /// + /// 重复执行直到条件满足 + /// + /// 间隔时间 + /// 执行操作,返回是否继续 + /// 最大执行次数(0表示无限) + /// 可取消令牌 + public static CancellationTokenSource RepeatUntil(TimeSpan interval, Func action, int maxCount = 0) + { + var cts = new CancellationTokenSource(); + var count = 0; + + Task.Run(async () => + { + while (!cts.Token.IsCancellationRequested) + { + if (maxCount > 0 && count >= maxCount) + break; + + if (!action()) + break; + + count++; + + try + { + await Task.Delay(interval, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + } + }, cts.Token); + + return cts; + } /// - /// 获取当前时间戳,即 Unix 时间戳,精确到毫秒。 + /// 异步重复执行直到条件满足 /// - /// 当前时间戳。 - public static long GetCurrentTimestamp() + /// 间隔时间 + /// 执行操作,返回是否继续 + /// 最大执行次数(0表示无限) + /// 可取消令牌 + public static CancellationTokenSource RepeatUntilAsync(TimeSpan interval, Func> action, int maxCount = 0) { - return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var cts = new CancellationTokenSource(); + var count = 0; + + Task.Run(async () => + { + while (!cts.Token.IsCancellationRequested) + { + if (maxCount > 0 && count >= maxCount) + break; + + if (!await action()) + break; + + count++; + + try + { + await Task.Delay(interval, cts.Token); + } + catch (OperationCanceledException) + { + break; + } + } + }, cts.Token); + + return cts; + } + } + + /// + /// 定时任务调度器 + /// + public class ScheduledTask + { + private Timer? _timer; + private readonly Action _callback; + private readonly TimeSpan _interval; + private readonly DateTime _startTime; + private readonly int _maxRuns; + private int _runCount; + + /// + /// 任务名称 + /// + public string Name { get; } + + /// + /// 是否正在运行 + /// + public bool IsRunning { get; private set; } + + /// + /// 已运行次数 + /// + public int RunCount => _runCount; + + /// + /// 创建定时任务 + /// + /// 任务名称 + /// 回调 + /// 间隔时间 + /// 开始时间 + /// 最大运行次数(0表示无限) + public ScheduledTask(string name, Action callback, TimeSpan interval, DateTime? startTime = null, int maxRuns = 0) + { + Name = name; + _callback = callback; + _interval = interval; + _startTime = startTime ?? DateTime.MinValue; + _maxRuns = maxRuns; + _runCount = 0; } /// - /// 获取程序启动时间。 + /// 启动任务 /// - /// 程序启动时间。 - public static DateTime GetStartTime() + public void Start() + { + if (IsRunning) + return; + + IsRunning = true; + + var dueTime = _startTime > DateTime.MinValue + ? _startTime - DateTime.Now + : TimeSpan.Zero; + + if (dueTime < TimeSpan.Zero) + dueTime = TimeSpan.Zero; + + _timer = new Timer(Execute, null, dueTime, _interval); + } + + private void Execute(object? state) { - return _startTime; + if (_maxRuns > 0 && _runCount >= _maxRuns) + { + Stop(); + return; + } + + try + { + _callback(); + } + catch + { + // 忽略异常,继续执行 + } + + Interlocked.Increment(ref _runCount); } /// - /// 获取当前时间距离程序启动时间的时间间隔。 + /// 停止任务 /// - /// 当前时间距离程序启动时间的时间间隔。 - public static TimeSpan GetElapsedTime() + public void Stop() { - return DateTime.Now - _startTime; + if (!IsRunning) + return; + + IsRunning = false; + _timer?.Dispose(); + _timer = null; } /// - /// 创建一个新的 Stopwatch 并启动计时。 + /// 重置运行计数 /// - /// 一个新的 Stopwatch。 - public static Stopwatch StartNew() + public void Reset() { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - return stopwatch; + _runCount = 0; } + } + + /// + /// 定时任务管理器 + /// + public class ScheduleManager + { + private readonly Dictionary _tasks = new(); /// - /// 计算指定操作的执行时间。 + /// 添加定时任务 /// - /// 要执行的操作。 - /// 操作执行的时间。 - public static TimeSpan Measure(Action action) + /// 任务名称 + /// 回调 + /// 间隔时间 + /// 开始时间 + /// 最大运行次数 + /// 定时任务 + public ScheduledTask AddTask(string name, Action callback, TimeSpan interval, DateTime? startTime = null, int maxRuns = 0) { - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - action.Invoke(); - stopwatch.Stop(); - return stopwatch.Elapsed; + var task = new ScheduledTask(name, callback, interval, startTime, maxRuns); + _tasks[name] = task; + return task; } /// - /// 计算指定操作的执行时间,并输出执行结果。 + /// 获取任务 /// - /// 要执行的操作。 - /// 执行结果的描述。 - public static void MeasureAndPrint(Action action, string description) + /// 任务名称 + /// 定时任务 + public ScheduledTask? GetTask(string name) { - TimeSpan elapsedTime = Measure(action); - Console.WriteLine($"{description}: {elapsedTime.TotalMilliseconds}ms"); + return _tasks.TryGetValue(name, out var task) ? task : null; } /// - /// 计算指定操作的执行时间,并输出执行结果到指定文件。 + /// 启动任务 /// - /// 要执行的操作。 - /// 输出结果的文件名。 - public static void MeasureAndSave(Action action, string fileName) + /// 任务名称 + /// 是否成功 + public bool StartTask(string name) { - TimeSpan elapsedTime = Measure(action); - System.IO.File.WriteAllText(fileName, elapsedTime.TotalMilliseconds.ToString()); + var task = GetTask(name); + if (task == null) + return false; + + task.Start(); + return true; } /// - /// 计算指定操作的执行时间,并将执行结果添加到指定日志文件的末尾。 + /// 停止任务 /// - /// 要执行的操作。 - /// 日志文件名。 - public static void MeasureAndLog(Action action, string fileName) + /// 任务名称 + /// 是否成功 + public bool StopTask(string name) { - TimeSpan elapsedTime = Measure(action); - System.IO.File.AppendAllText(fileName, $"{DateTime.Now}: {elapsedTime.TotalMilliseconds}ms{Environment.NewLine}"); + var task = GetTask(name); + if (task == null) + return false; + + task.Stop(); + return true; } /// - /// 等待指定的时间 + /// 移除任务 /// - /// 要等待的毫秒数。 - public static void Wait(int milliseconds) + /// 任务名称 + /// 是否成功 + public bool RemoveTask(string name) { - System.Threading.Thread.Sleep(milliseconds); + var task = GetTask(name); + if (task == null) + return false; + + task.Stop(); + _tasks.Remove(name); + return true; } /// - /// 计算两个时间的时间间隔。 + /// 启动所有任务 /// - /// 第一个时间。 - /// 第二个时间。 - /// 两个时间的时间间隔。 - public static TimeSpan GetTimeSpan(DateTime time1, DateTime time2) + public void StartAll() { - return time1 - time2; + foreach (var task in _tasks.Values) + { + task.Start(); + } } /// - /// 计算两个时间戳的时间间隔。 + /// 停止所有任务 /// - /// 第一个时间戳。 - /// 第二个时间戳。 - /// 两个时间戳的时间间隔。 - public static TimeSpan GetTimeSpan(long timestamp1, long timestamp2) + public void StopAll() { - DateTime time1 = DateTimeOffset.FromUnixTimeMilliseconds(timestamp1).LocalDateTime; - DateTime time2 = DateTimeOffset.FromUnixTimeMilliseconds(timestamp2).LocalDateTime; - return GetTimeSpan(time1, time2); + foreach (var task in _tasks.Values) + { + task.Stop(); + } } /// - /// 将时间间隔格式化为友好的字符串,例如 1h 20m 30s。 + /// 获取所有任务名称 /// - /// 要格式化的时间间隔。 - /// 格式化后的字符串。 - public static string FormatTimeSpan(TimeSpan timeSpan) + /// 任务名称列表 + public string[] GetTaskNames() { - int hours = timeSpan.Days * 24 + timeSpan.Hours; - string formattedTimeSpan = $"{hours}h {timeSpan.Minutes}m {timeSpan.Seconds}s"; - return formattedTimeSpan; + return _tasks.Keys.ToArray(); } + /// + /// 获取正在运行的任务数量 + /// + /// 数量 + public int GetRunningCount() + { + return _tasks.Values.Count(t => t.IsRunning); + } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs b/EasyTool.Core/DateTimeCategory/TimestampUtil.cs deleted file mode 100644 index 8e712c2..0000000 --- a/EasyTool.Core/DateTimeCategory/TimestampUtil.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; - -namespace EasyTool -{ - /// - /// 时间戳处理工具类 - /// - public static class TimestampUtil - { - /// - /// 获取当前时间戳(毫秒级) - /// - /// 当前时间戳(毫秒级) - public static long GetCurrentTimestamp() - { - DateTime dt = DateTime.UtcNow; - TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return (long)ts.TotalMilliseconds; - } - - /// - /// 将时间戳(毫秒级)转换为 DateTime 类型 - /// - /// 时间戳(毫秒级) - /// 转换后的 DateTime 类型 - public static DateTime ConvertToDateTime(long timestamp) - { - DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return dt.AddMilliseconds(timestamp); - } - - /// - /// 将 DateTime 类型转换为时间戳(毫秒级) - /// - /// DateTime 类型 - /// 转换后的时间戳(毫秒级) - public static long ConvertToTimestamp(DateTime dateTime) - { - TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - return (long)ts.TotalMilliseconds; - } - - /// - /// 获取当前时间戳(秒级) - /// - /// 当前时间戳(秒级) - public static long GetCurrentTimestampSeconds() - { - return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; - } - - /// - /// 将时间戳(秒级)转换为 DateTime 类型 - /// - /// 时间戳(秒级) - /// 转换后的 DateTime 类型 - public static DateTime ConvertToDateTimeSeconds(long timestamp) - { - return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp); - } - - /// - /// 将 DateTime 类型转换为时间戳(秒级) - /// - /// DateTime 类型 - /// 转换后的时间戳(秒级) - public static long ConvertToTimestampSeconds(DateTime dateTime) - { - return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; - } - } -} diff --git a/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs b/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs new file mode 100644 index 0000000..2768462 --- /dev/null +++ b/EasyTool.Core/DateTimeCategory/WorkdayUtil.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.DateTimeCategory +{ + /// + /// 工作日计算工具类 + /// 提供工作日相关的计算功能,支持自定义节假日和调休 + /// + public static class WorkdayUtil + { + private static readonly HashSet _defaultHolidays = new(); + private static readonly HashSet _defaultWorkdays = new(); + + static WorkdayUtil() + { + // 可以在这里初始化默认的节假日和调休工作日 + } + + /// + /// 添加节假日 + /// + /// 节假日日期 + public static void AddHoliday(DateTime date) + { + _defaultHolidays.Add(date.Date); + } + + /// + /// 批量添加节假日 + /// + /// 节假日日期集合 + public static void AddHolidays(IEnumerable dates) + { + foreach (var date in dates) + { + _defaultHolidays.Add(date.Date); + } + } + + /// + /// 移除节假日 + /// + /// 节假日日期 + public static void RemoveHoliday(DateTime date) + { + _defaultHolidays.Remove(date.Date); + } + + /// + /// 添加调休工作日(周末调休上班) + /// + /// 调休工作日日期 + public static void AddWorkday(DateTime date) + { + _defaultWorkdays.Add(date.Date); + } + + /// + /// 批量添加调休工作日 + /// + /// 调休工作日日期集合 + public static void AddWorkdays(IEnumerable dates) + { + foreach (var date in dates) + { + _defaultWorkdays.Add(date.Date); + } + } + + /// + /// 移除调休工作日 + /// + /// 调休工作日日期 + public static void RemoveWorkday(DateTime date) + { + _defaultWorkdays.Remove(date.Date); + } + + /// + /// 清空所有节假日和调休工作日配置 + /// + public static void ClearAll() + { + _defaultHolidays.Clear(); + _defaultWorkdays.Clear(); + } + + /// + /// 判断是否为工作日 + /// + /// 日期 + /// 是否为工作日 + public static bool IsWorkday(DateTime date) + { + return IsWorkday(date, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 判断是否为工作日 + /// + /// 日期 + /// 节假日集合 + /// 调休工作日集合 + /// 是否为工作日 + public static bool IsWorkday(DateTime date, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + var dateOnly = date.Date; + var holidaySet = holidays?.Select(d => d.Date).ToHashSet() ?? new HashSet(); + var workdaySet = adjustedWorkdays?.Select(d => d.Date).ToHashSet() ?? new HashSet(); + + // 如果是调休工作日,返回true + if (workdaySet.Contains(dateOnly)) + return true; + + // 如果是节假日,返回false + if (holidaySet.Contains(dateOnly)) + return false; + + // 判断是否为周末 + var dayOfWeek = date.DayOfWeek; + return dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday; + } + + /// + /// 判断是否为周末 + /// + /// 日期 + /// 是否为周末 + public static bool IsWeekend(DateTime date) + { + var dayOfWeek = date.DayOfWeek; + return dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 判断是否为节假日 + /// + /// 日期 + /// 是否为节假日 + public static bool IsHoliday(DateTime date) + { + return _defaultHolidays.Contains(date.Date); + } + + /// + /// 计算两个日期之间的工作日数量 + /// + /// 开始日期 + /// 结束日期 + /// 工作日数量 + public static int GetWorkdayCount(DateTime startDate, DateTime endDate) + { + return GetWorkdayCount(startDate, endDate, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 计算两个日期之间的工作日数量 + /// + /// 开始日期 + /// 结束日期 + /// 节假日集合 + /// 调休工作日集合 + /// 工作日数量 + public static int GetWorkdayCount(DateTime startDate, DateTime endDate, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + if (startDate > endDate) + { + var temp = startDate; + startDate = endDate; + endDate = temp; + } + + int count = 0; + var current = startDate.Date; + var endDateOnly = endDate.Date; + + while (current <= endDateOnly) + { + if (IsWorkday(current, holidays, adjustedWorkdays)) + count++; + current = current.AddDays(1); + } + + return count; + } + + /// + /// 计算指定工作日数后的日期 + /// + /// 开始日期 + /// 工作日数(正数表示往后,负数表示往前) + /// 目标日期 + public static DateTime AddWorkdays(DateTime startDate, int workdays) + { + return AddWorkdays(startDate, workdays, _defaultHolidays, _defaultWorkdays); + } + + /// + /// 计算指定工作日数后的日期 + /// + /// 开始日期 + /// 工作日数(正数表示往后,负数表示往前) + /// 节假日集合 + /// 调休工作日集合 + /// 目标日期 + public static DateTime AddWorkdays(DateTime startDate, int workdays, IEnumerable? holidays = null, IEnumerable? adjustedWorkdays = null) + { + if (workdays == 0) + return startDate.Date; + + var current = startDate.Date; + var increment = workdays > 0 ? 1 : -1; + var remaining = Math.Abs(workdays); + + while (remaining > 0) + { + current = current.AddDays(increment); + + if (IsWorkday(current, holidays, adjustedWorkdays)) + remaining--; + } + + return current; + } + + /// + /// 获取下一个工作日 + /// + /// 起始日期 + /// 下一个工作日 + public static DateTime GetNextWorkday(DateTime date) + { + return AddWorkdays(date, 1); + } + + /// + /// 获取上一个工作日 + /// + /// 起始日期 + /// 上一个工作日 + public static DateTime GetPreviousWorkday(DateTime date) + { + return AddWorkdays(date, -1); + } + + /// + /// 获取指定日期所在周的工作日列表 + /// + /// 日期 + /// 周起始日 + /// 工作日列表 + public static List GetWorkdaysOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + var result = new List(); + var startOfWeek = GetStartOfWeek(date, weekStartsOn); + + for (int i = 0; i < 7; i++) + { + var current = startOfWeek.AddDays(i); + if (IsWorkday(current)) + result.Add(current); + } + + return result; + } + + /// + /// 获取指定日期所在月的工作日列表 + /// + /// 日期 + /// 工作日列表 + public static List GetWorkdaysOfMonth(DateTime date) + { + var result = new List(); + var firstDay = new DateTime(date.Year, date.Month, 1); + var lastDay = firstDay.AddMonths(1).AddDays(-1); + + var current = firstDay; + while (current <= lastDay) + { + if (IsWorkday(current)) + result.Add(current); + current = current.AddDays(1); + } + + return result; + } + + /// + /// 获取指定日期所在月的工作日数量 + /// + /// 年份 + /// 月份 + /// 工作日数量 + public static int GetWorkdaysInMonth(int year, int month) + { + var firstDay = new DateTime(year, month, 1); + var lastDay = firstDay.AddMonths(1).AddDays(-1); + return GetWorkdayCount(firstDay, lastDay); + } + + /// + /// 获取指定日期所在年的工作日数量 + /// + /// 年份 + /// 工作日数量 + public static int GetWorkdaysInYear(int year) + { + var firstDay = new DateTime(year, 1, 1); + var lastDay = new DateTime(year, 12, 31); + return GetWorkdayCount(firstDay, lastDay); + } + + /// + /// 获取指定日期所在周的第一天 + /// + /// 日期 + /// 周起始日 + /// 周的第一天 + public static DateTime GetStartOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + var diff = (7 + (date.DayOfWeek - weekStartsOn)) % 7; + return date.Date.AddDays(-diff); + } + + /// + /// 获取指定日期所在周的最后一天 + /// + /// 日期 + /// 周起始日 + /// 周的最后一天 + public static DateTime GetEndOfWeek(DateTime date, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + return GetStartOfWeek(date, weekStartsOn).AddDays(6); + } + + /// + /// 计算工作日区间(返回所有工作日) + /// + /// 开始日期 + /// 结束日期 + /// 工作日列表 + public static List GetWorkdaysBetween(DateTime startDate, DateTime endDate) + { + var result = new List(); + + if (startDate > endDate) + { + var temp = startDate; + startDate = endDate; + endDate = temp; + } + + var current = startDate.Date; + while (current <= endDate.Date) + { + if (IsWorkday(current)) + result.Add(current); + current = current.AddDays(1); + } + + return result; + } + + /// + /// 判断是否为同一天 + /// + /// 日期1 + /// 日期2 + /// 是否为同一天 + public static bool IsSameDay(DateTime date1, DateTime date2) + { + return date1.Date == date2.Date; + } + + /// + /// 判断两个日期是否为同一周 + /// + /// 日期1 + /// 日期2 + /// 周起始日 + /// 是否为同一周 + public static bool IsSameWeek(DateTime date1, DateTime date2, DayOfWeek weekStartsOn = DayOfWeek.Monday) + { + return GetStartOfWeek(date1, weekStartsOn) == GetStartOfWeek(date2, weekStartsOn); + } + + /// + /// 判断两个日期是否为同一月 + /// + /// 日期1 + /// 日期2 + /// 是否为同一月 + public static bool IsSameMonth(DateTime date1, DateTime date2) + { + return date1.Year == date2.Year && date1.Month == date2.Month; + } + + /// + /// 判断两个日期是否为同一年 + /// + /// 日期1 + /// 日期2 + /// 是否为同一年 + public static bool IsSameYear(DateTime date1, DateTime date2) + { + return date1.Year == date2.Year; + } + + /// + /// 获取第n个工作日 + /// + /// 年份 + /// 月份 + /// 第n个工作日(从1开始) + /// 工作日日期 + public static DateTime GetNthWorkdayOfMonth(int year, int month, int n) + { + if (n < 1) + throw new ArgumentException("n必须大于0", nameof(n)); + + var workdays = GetWorkdaysOfMonth(new DateTime(year, month, 1)); + + if (n > workdays.Count) + throw new ArgumentException($"该月只有{workdays.Count}个工作日", nameof(n)); + + return workdays[n - 1]; + } + + /// + /// 获取日期在当月中的第几个工作日 + /// + /// 日期 + /// 第几个工作日(从1开始),如果不是工作日返回-1 + public static int GetWorkdayIndexInMonth(DateTime date) + { + if (!IsWorkday(date)) + return -1; + + var workdays = GetWorkdaysOfMonth(date); + return workdays.FindIndex(d => d.Date == date.Date) + 1; + } + } +} diff --git a/EasyTool.Core/EasyTool.Core.csproj b/EasyTool.Core/EasyTool.Core.csproj index 821383f..cb0e391 100644 --- a/EasyTool.Core/EasyTool.Core.csproj +++ b/EasyTool.Core/EasyTool.Core.csproj @@ -1,13 +1,16 @@ - + - netstandard2.1;.net6.0 - 11 - enable + netstandard2.1 + latest + true + $(NoWarn); $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) + annotations + Joce.EasyTool.Core 一个大西瓜,TimChen - 2023.0908.1 + 2026.0108.1 A open source C# tool to make .NET easy @@ -36,9 +39,24 @@ - - - + + + + + + + + + + + + + + + + + + diff --git a/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs b/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs deleted file mode 100644 index 84cafe5..0000000 --- a/EasyTool.Core/IEnumerableCategory/IEnumerableExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool.IEnumerableCategory -{ - /// - /// 通用拓展 - /// - public static class IEnumerableExtensions - { - - - - #region IEnumerable拓展 - /// - /// 对List 等集合Foreach的时候不用在上层判空,直接加上这个就好 - /// - /// - /// - /// - public static IEnumerable CheckNull(this IEnumerable values) - { - return values is null ? new List(0) : values; - } - - #region 集合运算 - /// - /// 求集合的笛卡尔积 - /// - public static IEnumerable> Cartesian(this IEnumerable> sequences) - { - IEnumerable> tempProduct = new[] { Enumerable.Empty() }; - return sequences.Aggregate(tempProduct, - (accumulator, sequence) => - from accseq in accumulator - from item in sequence - select accseq.Concat(new[] { item - })); - } - - #endregion - #endregion - } -} diff --git a/EasyTool.Core/IOCategory/ArchiveUtil.cs b/EasyTool.Core/IOCategory/ArchiveUtil.cs new file mode 100644 index 0000000..13d5be9 --- /dev/null +++ b/EasyTool.Core/IOCategory/ArchiveUtil.cs @@ -0,0 +1,449 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 压缩包工具类 + /// 支持 ZIP、TAR、GZip 等格式 + /// + public static class ArchiveUtil + { + #region ZIP 操作 + + /// + /// 创建 ZIP 压缩包 + /// + /// 源文件或目录路径 + /// ZIP 文件路径 + /// 压缩级别 + /// 是否包含根目录 + public static void CreateZip(string sourcePath, string zipPath, CompressionLevel compressionLevel = CompressionLevel.Optimal, bool includeBaseDirectory = false) + { + if (File.Exists(sourcePath)) + { + // 压缩单个文件 + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create); + archive.CreateEntryFromFile(sourcePath, Path.GetFileName(sourcePath), compressionLevel); + } + else if (Directory.Exists(sourcePath)) + { + // 压缩目录 + ZipFile.CreateFromDirectory(sourcePath, zipPath, compressionLevel, includeBaseDirectory); + } + else + { + throw new FileNotFoundException("源路径不存在", sourcePath); + } + } + + /// + /// 解压 ZIP 文件 + /// + /// ZIP 文件路径 + /// 解压目录 + /// 是否覆盖已存在的文件 + public static void ExtractZip(string zipPath, string extractPath, bool overwrite = false) + { + ZipFile.ExtractToDirectory(zipPath, extractPath, overwrite); + } + + /// + /// 列出 ZIP 文件内容 + /// + /// ZIP 文件路径 + /// 文件条目列表 + public static List ListZip(string zipPath) + { + using var archive = ZipFile.OpenRead(zipPath); + return archive.Entries.Select(e => new ArchiveEntry + { + Name = e.Name, + FullName = e.FullName, + Length = e.Length, + CompressedLength = e.CompressedLength, + LastWriteTime = e.LastWriteTime.DateTime, + IsDirectory = string.IsNullOrEmpty(e.Name) + }).ToList(); + } + + /// + /// 从 ZIP 中提取单个文件 + /// + /// ZIP 文件路径 + /// 条目名称 + /// 目标路径 + public static void ExtractFileFromZip(string zipPath, string entryName, string destinationPath) + { + using var archive = ZipFile.OpenRead(zipPath); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"ZIP 中找不到条目: {entryName}"); + + entry.ExtractToFile(destinationPath, true); + } + + /// + /// 向 ZIP 添加文件 + /// + /// ZIP 文件路径 + /// 要添加的文件路径 + /// ZIP 中的条目名称 + public static void AddFileToZip(string zipPath, string filePath, string? entryName = null) + { + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Update); + archive.CreateEntryFromFile(filePath, entryName ?? Path.GetFileName(filePath)); + } + + /// + /// 从 ZIP 删除文件 + /// + /// ZIP 文件路径 + /// 要删除的条目名称 + public static void RemoveFileFromZip(string zipPath, string entryName) + { + using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Update); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"ZIP 中找不到条目: {entryName}"); + entry.Delete(); + } + + #endregion + + #region GZip 操作 + + /// + /// 使用 GZip 压缩文件 + /// + /// 源文件路径 + /// 目标文件路径(可选,默认添加 .gz 后缀) + public static void CompressGZip(string sourcePath, string? destinationPath = null) + { + destinationPath ??= sourcePath + ".gz"; + + using var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read); + using var destinationStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write); + using var gzipStream = new GZipStream(destinationStream, CompressionMode.Compress); + + sourceStream.CopyTo(gzipStream); + } + + /// + /// 解压 GZip 文件 + /// + /// GZip 文件路径 + /// 目标文件路径(可选,默认移除 .gz 后缀) + public static void DecompressGZip(string sourcePath, string? destinationPath = null) + { + if (destinationPath == null) + { + destinationPath = sourcePath.EndsWith(".gz", StringComparison.OrdinalIgnoreCase) + ? sourcePath.Substring(0, sourcePath.Length - 3) + : sourcePath + ".out"; + } + + using var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read); + using var destinationStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write); + using var gzipStream = new GZipStream(sourceStream, CompressionMode.Decompress); + + gzipStream.CopyTo(destinationStream); + } + + /// + /// 压缩字节数组 + /// + /// 原始数据 + /// 压缩后的数据 + public static byte[] CompressGZip(byte[] data) + { + using var output = new MemoryStream(); + using (var gzip = new GZipStream(output, CompressionMode.Compress)) + { + gzip.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// 解压字节数组 + /// + /// 压缩数据 + /// 解压后的数据 + public static byte[] DecompressGZip(byte[] compressedData) + { + using var input = new MemoryStream(compressedData); + using var gzip = new GZipStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + gzip.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region Tar 操作 + + /// + /// 创建 TAR 归档 + /// + /// 源目录路径 + /// TAR 文件路径 + public static void CreateTar(string sourcePath, string tarPath) + { + using var output = new FileStream(tarPath, FileMode.Create, FileAccess.Write); + using var tar = new TarWriter(output); + + if (Directory.Exists(sourcePath)) + { + var dir = new DirectoryInfo(sourcePath); + foreach (var file in dir.GetFiles("*", SearchOption.AllDirectories)) + { + var relativePath = GetRelativePath(dir.FullName, file.FullName); + tar.Write(file.FullName, relativePath); + } + } + else if (File.Exists(sourcePath)) + { + tar.Write(sourcePath, Path.GetFileName(sourcePath)); + } + } + + /// + /// 创建 TAR.GZ 归档 + /// + /// 源目录路径 + /// TAR.GZ 文件路径 + public static void CreateTarGz(string sourcePath, string tarGzPath) + { + using var output = new FileStream(tarGzPath, FileMode.Create, FileAccess.Write); + using var gzip = new GZipStream(output, CompressionMode.Compress); + using var tar = new TarWriter(gzip); + + if (Directory.Exists(sourcePath)) + { + var dir = new DirectoryInfo(sourcePath); + foreach (var file in dir.GetFiles("*", SearchOption.AllDirectories)) + { + var relativePath = GetRelativePath(dir.FullName, file.FullName); + tar.Write(file.FullName, relativePath); + } + } + else if (File.Exists(sourcePath)) + { + tar.Write(sourcePath, Path.GetFileName(sourcePath)); + } + } + + private static string GetRelativePath(string basePath, string fullPath) + { + if (fullPath.StartsWith(basePath)) + { + var relative = fullPath.Substring(basePath.Length); + return relative.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } + return fullPath; + } + + #endregion + + #region 内存压缩 + + /// + /// 将多个文件压缩到内存 + /// + /// 文件字典(文件名 -> 文件内容) + /// 压缩后的数据 + public static byte[] CreateZipInMemory(Dictionary files) + { + using var output = new MemoryStream(); + using (var archive = new ZipArchive(output, ZipArchiveMode.Create, true)) + { + foreach (var kvp in files) + { + var entry = archive.CreateEntry(kvp.Key); + using var entryStream = entry.Open(); + entryStream.Write(kvp.Value, 0, kvp.Value.Length); + } + } + return output.ToArray(); + } + + /// + /// 从内存中解压文件 + /// + /// ZIP 数据 + /// 文件字典 + public static Dictionary ExtractZipFromMemory(byte[] zipData) + { + var result = new Dictionary(); + + using var input = new MemoryStream(zipData); + using var archive = new ZipArchive(input, ZipArchiveMode.Read); + + foreach (var entry in archive.Entries) + { + if (!string.IsNullOrEmpty(entry.Name)) + { + using var entryStream = entry.Open(); + using var output = new MemoryStream(); + entryStream.CopyTo(output); + result[entry.FullName] = output.ToArray(); + } + } + + return result; + } + + #endregion + + #region 流式压缩 + + /// + /// 创建压缩流 + /// + /// 输出流 + /// 压缩流 + public static Stream CreateCompressStream(Stream outputStream) + { + return new GZipStream(outputStream, CompressionMode.Compress); + } + + /// + /// 创建解压流 + /// + /// 输入流 + /// 解压流 + public static Stream CreateDecompressStream(Stream inputStream) + { + return new GZipStream(inputStream, CompressionMode.Decompress); + } + + #endregion + } + + /// + /// 压缩包条目 + /// + public class ArchiveEntry + { + /// + /// 文件名 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 完整路径 + /// + public string FullName { get; set; } = string.Empty; + + /// + /// 原始大小 + /// + public long Length { get; set; } + + /// + /// 压缩后大小 + /// + public long CompressedLength { get; set; } + + /// + /// 最后修改时间 + /// + public DateTime LastWriteTime { get; set; } + + /// + /// 是否为目录 + /// + public bool IsDirectory { get; set; } + + /// + /// 压缩率 + /// + public double CompressionRatio => Length > 0 ? (1 - (double)CompressedLength / Length) * 100 : 0; + } + + #region TarWriter 简单实现 + + internal class TarWriter : IDisposable + { + private readonly Stream _stream; + private static readonly byte[] EmptyBlock = new byte[512]; + + public TarWriter(Stream stream) + { + _stream = stream; + } + + public void Write(string filePath, string entryName) + { + var fileInfo = new FileInfo(filePath); + var header = CreateHeader(entryName, fileInfo.Length, fileInfo.LastWriteTime); + _stream.Write(header, 0, header.Length); + + using var fileStream = fileInfo.OpenRead(); + fileStream.CopyTo(_stream); + + // 填充到 512 字节边界 + var remainder = fileInfo.Length % 512; + if (remainder > 0) + { + _stream.Write(EmptyBlock, 0, (int)(512 - remainder)); + } + } + + private byte[] CreateHeader(string name, long size, DateTime mtime) + { + var header = new byte[512]; + var nameBytes = System.Text.Encoding.UTF8.GetBytes(name); + + // 名称 + Array.Copy(nameBytes, header, Math.Min(nameBytes.Length, 100)); + + // 文件模式 + var mode = "0000644\0"u8.ToArray(); + Array.Copy(mode, 0, header, 100, mode.Length); + + // UID/GID + var uid = "0000000\0"u8.ToArray(); + Array.Copy(uid, 0, header, 108, uid.Length); + Array.Copy(uid, 0, header, 116, uid.Length); + + // 大小(八进制) + var sizeStr = Convert.ToString(size, 8).PadLeft(11, '0') + "\0"; + var sizeBytes = System.Text.Encoding.ASCII.GetBytes(sizeStr); + Array.Copy(sizeBytes, 0, header, 124, sizeBytes.Length); + + // 修改时间 + var unixTime = new DateTimeOffset(mtime).ToUnixTimeSeconds(); + var mtimeStr = Convert.ToString(unixTime, 8).PadLeft(11, '0') + "\0"; + var mtimeBytes = System.Text.Encoding.ASCII.GetBytes(mtimeStr); + Array.Copy(mtimeBytes, 0, header, 136, mtimeBytes.Length); + + // 类型标志 + header[156] = (byte)'0'; // 普通文件 + + // 校验和(先填空格) + for (int i = 148; i < 156; i++) header[i] = (byte)' '; + + // 计算校验和 + int checksum = 0; + foreach (var b in header) checksum += b; + + var checksumStr = Convert.ToString(checksum, 8).PadLeft(6, '0') + "\0 "; + var checksumBytes = System.Text.Encoding.ASCII.GetBytes(checksumStr); + Array.Copy(checksumBytes, 0, header, 148, checksumBytes.Length); + + return header; + } + + public void Dispose() + { + // 写入两个空块作为文件结束 + _stream.Write(EmptyBlock, 0, EmptyBlock.Length); + _stream.Write(EmptyBlock, 0, EmptyBlock.Length); + } + } + + #endregion +} diff --git a/EasyTool.Core/IOCategory/BomUtil.cs b/EasyTool.Core/IOCategory/BomUtil.cs new file mode 100644 index 0000000..67842d1 --- /dev/null +++ b/EasyTool.Core/IOCategory/BomUtil.cs @@ -0,0 +1,236 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// BOM(字节顺序标记)工具类 + /// 处理不同编码的 BOM + /// + public static class BomUtil + { + /// + /// BOM 定义 + /// + public static readonly Dictionary BomDefinitions = new() + { + {"UTF-8", new byte[] {0xEF, 0xBB, 0xBF}}, + {"UTF-16BE", new byte[] {0xFE, 0xFF}}, + {"UTF-16LE", new byte[] {0xFF, 0xFE}}, + {"UTF-32BE", new byte[] {0x00, 0x00, 0xFE, 0xFF}}, + {"UTF-32LE", new byte[] {0xFF, 0xFE, 0x00, 0x00}}, + {"UTF-7", new byte[] {0x2B, 0x2F, 0x76}}, + {"UTF-1", new byte[] {0xF7, 0x64, 0x4C}}, + {"UTF-EBCDIC", new byte[] {0xDD, 0x73, 0x66, 0x73}}, + {"SCSU", new byte[] {0x0E, 0xFE, 0xFF}}, + {"BOCU-1", new byte[] {0xFB, 0xEE, 0x28}}, + {"GB-18030", new byte[] {0x84, 0x31, 0x95, 0x33}}, + }; + + /// + /// 检测文件的 BOM + /// + public static BomInfo Detect(string filePath) + { + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + return Detect(stream); + } + + /// + /// 检测流的 BOM + /// + public static BomInfo Detect(Stream stream) + { + byte[] buffer = new byte[4]; + int bytesRead = stream.Read(buffer, 0, 4); + + // 重置流位置 + if (stream.CanSeek) + stream.Position = 0; + + return Detect(buffer, bytesRead); + } + + /// + /// 检测字节数组的 BOM + /// + public static BomInfo Detect(byte[] bytes) + { + return Detect(bytes, bytes.Length); + } + + private static BomInfo Detect(byte[] bytes, int length) + { + if (length < 2) + return new BomInfo { HasBom = false, Encoding = null, BomLength = 0 }; + + // UTF-8 + if (length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.UTF8, BomLength = 3 }; + } + + // UTF-32BE + if (length >= 4 && bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.GetEncoding("utf-32BE"), BomLength = 4 }; + } + + // UTF-32LE + if (length >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0x00 && bytes[3] == 0x00) + { + return new BomInfo { HasBom = true, Encoding = Encoding.GetEncoding("utf-32LE"), BomLength = 4 }; + } + + // UTF-16BE + if (length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) + { + return new BomInfo { HasBom = true, Encoding = Encoding.BigEndianUnicode, BomLength = 2 }; + } + + // UTF-16LE + if (length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) + { + return new BomInfo { HasBom = true, Encoding = Encoding.Unicode, BomLength = 2 }; + } + + // UTF-7 (可能有变体) + if (length >= 3 && bytes[0] == 0x2B && bytes[1] == 0x2F && bytes[2] == 0x76) + { + return new BomInfo { HasBom = true, Encoding = Encoding.UTF7, BomLength = 3 }; + } + + return new BomInfo { HasBom = false, Encoding = null, BomLength = 0 }; + } + + /// + /// 移除文件的 BOM + /// + public static void Remove(string filePath) + { + var bom = Detect(filePath); + if (!bom.HasBom) return; + + byte[] content = File.ReadAllBytes(filePath); + byte[] newContent = new byte[content.Length - bom.BomLength]; + Array.Copy(content, bom.BomLength, newContent, 0, newContent.Length); + File.WriteAllBytes(filePath, newContent); + } + + /// + /// 为文件添加 BOM + /// + public static void Add(string filePath, Encoding encoding) + { + byte[] bom = GetBom(encoding); + if (bom == null) return; + + var bomInfo = Detect(filePath); + if (bomInfo.HasBom) return; + + byte[] content = File.ReadAllBytes(filePath); + byte[] newContent = new byte[bom.Length + content.Length]; + Array.Copy(bom, 0, newContent, 0, bom.Length); + Array.Copy(content, 0, newContent, bom.Length, content.Length); + File.WriteAllBytes(filePath, newContent); + } + + /// + /// 获取指定编码的 BOM + /// + public static byte[] GetBom(Encoding encoding) + { + if (encoding == null) + return null; + + // 使用内置方法获取 BOM + if (encoding.Equals(Encoding.UTF8)) + return Encoding.UTF8.GetPreamble(); + if (encoding.Equals(Encoding.Unicode)) + return Encoding.Unicode.GetPreamble(); + if (encoding.Equals(Encoding.BigEndianUnicode)) + return Encoding.BigEndianUnicode.GetPreamble(); + if (encoding.Equals(Encoding.UTF32)) + return Encoding.UTF32.GetPreamble(); + + return null; + } + + /// + /// 读取文件内容(自动处理 BOM) + /// + public static string ReadAllText(string filePath) + { + var bom = Detect(filePath); + Encoding encoding = bom.Encoding ?? Encoding.UTF8; + + byte[] bytes = File.ReadAllBytes(filePath); + int offset = bom.HasBom ? bom.BomLength : 0; + int length = bytes.Length - offset; + + return encoding.GetString(bytes, offset, length); + } + + /// + /// 写入文件内容(可选是否添加 BOM) + /// + public static void WriteAllText(string filePath, string content, Encoding encoding, bool includeBom = true) + { + if (includeBom) + { + byte[] bom = GetBom(encoding); + byte[] contentBytes = encoding.GetBytes(content); + + if (bom != null && bom.Length > 0) + { + byte[] allBytes = new byte[bom.Length + contentBytes.Length]; + Array.Copy(bom, 0, allBytes, 0, bom.Length); + Array.Copy(contentBytes, 0, allBytes, bom.Length, contentBytes.Length); + File.WriteAllBytes(filePath, allBytes); + return; + } + } + + File.WriteAllText(filePath, content, encoding); + } + + /// + /// 转换文件编码(处理 BOM) + /// + public static void Convert(string filePath, Encoding targetEncoding, bool includeBom = true) + { + string content = ReadAllText(filePath); + WriteAllText(filePath, content, targetEncoding, includeBom); + } + } + + /// + /// BOM 信息 + /// + public class BomInfo + { + /// + /// 是否有 BOM + /// + public bool HasBom { get; set; } + + /// + /// 编码 + /// + public Encoding Encoding { get; set; } + + /// + /// BOM 长度(字节) + /// + public int BomLength { get; set; } + + public override string ToString() + { + return HasBom + ? $"Has BOM: {Encoding?.WebName ?? "Unknown"}, Length: {BomLength}" + : "No BOM detected"; + } + } +} diff --git a/EasyTool.Core/IOCategory/CompressionUtil.cs b/EasyTool.Core/IOCategory/CompressionUtil.cs new file mode 100644 index 0000000..a529aa7 --- /dev/null +++ b/EasyTool.Core/IOCategory/CompressionUtil.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 压缩工具类 + /// + public static class CompressionUtil + { + #region GZip + + /// + /// GZip压缩 + /// + public static byte[] GZipCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var gzip = new GZipStream(output, CompressionMode.Compress)) + { + gzip.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// GZip解压 + /// + public static byte[] GZipDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var gzip = new GZipStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + gzip.CopyTo(output); + return output.ToArray(); + } + + /// + /// GZip压缩字符串 + /// + public static string GZipCompressString(string text, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + var data = encoding.GetBytes(text); + var compressed = GZipCompress(data); + return Convert.ToBase64String(compressed); + } + + /// + /// GZip解压字符串 + /// + public static string GZipDecompressString(string compressedText, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + var data = Convert.FromBase64String(compressedText); + var decompressed = GZipDecompress(data); + return encoding.GetString(decompressed); + } + + #endregion + + #region Deflate + + /// + /// Deflate压缩 + /// + public static byte[] DeflateCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var deflate = new DeflateStream(output, CompressionMode.Compress)) + { + deflate.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// Deflate解压 + /// + public static byte[] DeflateDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var deflate = new DeflateStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + deflate.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region Zip + + /// + /// 压缩文件到Zip + /// + public static void ZipFile(string sourceFilePath, string zipFilePath) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + System.IO.Compression.ZipFile.CreateFromDirectory( + Path.GetDirectoryName(sourceFilePath) ?? "", + zipFilePath); + } + + /// + /// 压缩目录到Zip + /// + public static void ZipDirectory(string sourceDirectory, string zipFilePath, bool includeBaseDirectory = true) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + System.IO.Compression.ZipFile.CreateFromDirectory(sourceDirectory, zipFilePath, + CompressionLevel.Optimal, includeBaseDirectory); + } + + /// + /// 解压Zip文件 + /// + public static void Unzip(string zipFilePath, string destinationDirectory) + { + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + + /// + /// 解压Zip文件(覆盖已存在的文件) + /// + public static void Unzip(string zipFilePath, string destinationDirectory, bool overwrite) + { + if (overwrite) + { + // 先删除目标目录中的文件 + if (Directory.Exists(destinationDirectory)) + { + Directory.Delete(destinationDirectory, true); + } + Directory.CreateDirectory(destinationDirectory); + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + else + { + System.IO.Compression.ZipFile.ExtractToDirectory(zipFilePath, destinationDirectory); + } + } + + /// + /// 压缩文件列表到Zip + /// + public static void ZipFiles(IEnumerable filePaths, string zipFilePath, string? basePath = null) + { + var directory = Path.GetDirectoryName(zipFilePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Create); + foreach (var filePath in filePaths) + { + var entryName = basePath != null + ? filePath.Substring(basePath.Length).TrimStart(Path.DirectorySeparatorChar) + : Path.GetFileName(filePath); + archive.CreateEntryFromFile(filePath, entryName); + } + } + + /// + /// 获取Zip文件中的文件列表 + /// + public static List GetZipEntries(string zipFilePath) + { + using var archive = System.IO.Compression.ZipFile.OpenRead(zipFilePath); + return archive.Entries.Select(e => e.FullName).ToList(); + } + + /// + /// 从Zip中提取单个文件 + /// + public static void ExtractFile(string zipFilePath, string entryName, string destinationPath) + { + using var archive = System.IO.Compression.ZipFile.OpenRead(zipFilePath); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"Zip中未找到文件: {entryName}"); + entry.ExtractToFile(destinationPath, true); + } + + /// + /// 向Zip添加文件 + /// + public static void AddFileToZip(string zipFilePath, string filePath, string? entryName = null) + { + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Update); + archive.CreateEntryFromFile(filePath, entryName ?? Path.GetFileName(filePath)); + } + + /// + /// 从Zip删除文件 + /// + public static void RemoveFileFromZip(string zipFilePath, string entryName) + { + using var archive = System.IO.Compression.ZipFile.Open(zipFilePath, ZipArchiveMode.Update); + var entry = archive.GetEntry(entryName) + ?? throw new FileNotFoundException($"Zip中未找到文件: {entryName}"); + entry.Delete(); + } + + #endregion + + #region Brotli + + /// + /// Brotli压缩 + /// + public static byte[] BrotliCompress(byte[] data) + { + using var output = new MemoryStream(); + using (var brotli = new BrotliStream(output, CompressionMode.Compress)) + { + brotli.Write(data, 0, data.Length); + } + return output.ToArray(); + } + + /// + /// Brotli解压 + /// + public static byte[] BrotliDecompress(byte[] data) + { + using var input = new MemoryStream(data); + using var brotli = new BrotliStream(input, CompressionMode.Decompress); + using var output = new MemoryStream(); + brotli.CopyTo(output); + return output.ToArray(); + } + + #endregion + + #region 压缩率计算 + + /// + /// 计算压缩率 + /// + public static double CalculateCompressionRatio(long originalSize, long compressedSize) + { + if (originalSize == 0) + return 0; + return (double)(originalSize - compressedSize) / originalSize * 100; + } + + /// + /// 获取最佳压缩级别 + /// + public static CompressionLevel GetOptimalCompressionLevel(double targetRatio) + { + return targetRatio switch + { + > 80 => CompressionLevel.Optimal, + > 50 => CompressionLevel.Optimal, + > 20 => CompressionLevel.Fastest, + _ => CompressionLevel.NoCompression + }; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/ConfigUtil.cs b/EasyTool.Core/IOCategory/ConfigUtil.cs new file mode 100644 index 0000000..04de5fd --- /dev/null +++ b/EasyTool.Core/IOCategory/ConfigUtil.cs @@ -0,0 +1,252 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 配置文件工具类 + /// 支持INI格式配置文件 + /// + public static class ConfigUtil + { + /// + /// 读取INI配置值 + /// + public static string? GetIniValue(string filePath, string section, string key) + { + if (!File.Exists(filePath)) + return null; + + var lines = File.ReadAllLines(filePath); + var currentSection = ""; + var sectionHeader = $"[{section}]"; + + foreach (var line in lines) + { + var trimmed = line.Trim(); + + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed; + continue; + } + + if (currentSection == sectionHeader) + { + if (trimmed.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + trimmed.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + var valueStart = trimmed.IndexOf('=') + 1; + return trimmed.Substring(valueStart).Trim(); + } + } + } + + return null; + } + + /// + /// 设置INI配置值 + /// + public static void SetIniValue(string filePath, string section, string key, string value) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = File.Exists(filePath) ? File.ReadAllLines(filePath).ToList() : new List(); + var sectionHeader = $"[{section}]"; + var sectionIndex = -1; + var keyIndex = -1; + + // 查找section + for (int i = 0; i < lines.Count; i++) + { + if (lines[i].Trim() == sectionHeader) + { + sectionIndex = i; + break; + } + } + + // 如果section不存在,添加它 + if (sectionIndex < 0) + { + if (lines.Count > 0 && !string.IsNullOrWhiteSpace(lines[^1])) + lines.Add(""); + lines.Add(sectionHeader); + lines.Add($"{key}={value}"); + } + else + { + // 查找key + for (int i = sectionIndex + 1; i < lines.Count; i++) + { + var line = lines[i].Trim(); + if (line.StartsWith("[") && line.EndsWith("]")) + break; // 进入下一个section + + if (line.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + line.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + keyIndex = i; + break; + } + } + + if (keyIndex >= 0) + { + lines[keyIndex] = $"{key}={value}"; + } + else + { + lines.Insert(sectionIndex + 1, $"{key}={value}"); + } + } + + File.WriteAllLines(filePath, lines); + } + + /// + /// 读取INI配置的所有键值对 + /// + public static Dictionary GetIniSection(string filePath, string section) + { + var result = new Dictionary(); + + if (!File.Exists(filePath)) + return result; + + var lines = File.ReadAllLines(filePath); + var currentSection = ""; + var sectionHeader = $"[{section}]"; + + foreach (var line in lines) + { + var trimmed = line.Trim(); + + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed; + continue; + } + + if (currentSection == sectionHeader && trimmed.Contains("=")) + { + var eqIndex = trimmed.IndexOf('='); + var key = trimmed.Substring(0, eqIndex).Trim(); + var value = trimmed.Substring(eqIndex + 1).Trim(); + result[key] = value; + } + } + + return result; + } + + /// + /// 获取INI文件所有节名 + /// + public static List GetIniSections(string filePath) + { + var sections = new List(); + + if (!File.Exists(filePath)) + return sections; + + var lines = File.ReadAllLines(filePath); + + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + var sectionName = trimmed.Substring(1, trimmed.Length - 2); + sections.Add(sectionName); + } + } + + return sections; + } + + /// + /// 删除INI键 + /// + public static void RemoveIniKey(string filePath, string section, string key) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath).ToList(); + var sectionHeader = $"[{section}]"; + var inSection = false; + var keyIndex = -1; + + for (int i = 0; i < lines.Count; i++) + { + var line = lines[i].Trim(); + + if (line == sectionHeader) + { + inSection = true; + continue; + } + + if (inSection) + { + if (line.StartsWith("[") && line.EndsWith("]")) + break; + + if (line.StartsWith($"{key}=", StringComparison.OrdinalIgnoreCase) || + line.StartsWith($"{key} =", StringComparison.OrdinalIgnoreCase)) + { + keyIndex = i; + break; + } + } + } + + if (keyIndex >= 0) + { + lines.RemoveAt(keyIndex); + File.WriteAllLines(filePath, lines); + } + } + + /// + /// 删除INI节 + /// + public static void RemoveIniSection(string filePath, string section) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath).ToList(); + var sectionHeader = $"[{section}]"; + var startIndex = -1; + var endIndex = lines.Count; + + for (int i = 0; i < lines.Count; i++) + { + var line = lines[i].Trim(); + + if (line == sectionHeader) + { + startIndex = i; + } + else if (startIndex >= 0 && line.StartsWith("[") && line.EndsWith("]")) + { + endIndex = i; + break; + } + } + + if (startIndex >= 0) + { + lines.RemoveRange(startIndex, endIndex - startIndex); + File.WriteAllLines(filePath, lines); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/CsvStreamingReader.cs b/EasyTool.Core/IOCategory/CsvStreamingReader.cs new file mode 100644 index 0000000..a7856b1 --- /dev/null +++ b/EasyTool.Core/IOCategory/CsvStreamingReader.cs @@ -0,0 +1,483 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// CSV 流式读取器选项 + /// + public class CsvReaderOptions + { + /// + /// 分隔符(默认逗号) + /// + public char Delimiter { get; set; } = ','; + + /// + /// 引号字符(默认双引号) + /// + public char QuoteChar { get; set; } = '"'; + + /// + /// 是否有标题行 + /// + public bool HasHeader { get; set; } = true; + + /// + /// 编码(默认 UTF-8) + /// + public Encoding Encoding { get; set; } = Encoding.UTF8; + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } = 4096; + + /// + /// 是否跳过空行 + /// + public bool SkipEmptyLines { get; set; } = true; + + /// + /// 是否去除字段首尾空白 + /// + public bool TrimFields { get; set; } = false; + } + + /// + /// CSV 流式读取器 + /// 支持大文件逐行读取 + /// + public class CsvStreamingReader : IDisposable + { + private readonly TextReader _reader; + private readonly CsvReaderOptions _options; + private string[]? _headers; + private int _lineNumber; + private bool _disposed; + + /// + /// 获取标题行 + /// + public string[]? Headers => _headers; + + /// + /// 获取当前行号 + /// + public int LineNumber => _lineNumber; + + /// + /// 创建 CSV 流式读取器 + /// + /// 文本读取器 + /// 选项 + public CsvStreamingReader(TextReader reader, CsvReaderOptions? options = null) + { + _reader = reader ?? throw new ArgumentNullException(nameof(reader)); + _options = options ?? new CsvReaderOptions(); + _lineNumber = 0; + + if (_options.HasHeader) + { + ReadHeaders(); + } + } + + /// + /// 从文件创建 CSV 流式读取器 + /// + /// 文件路径 + /// 选项 + /// CSV 流式读取器 + public static CsvStreamingReader FromFile(string filePath, CsvReaderOptions? options = null) + { + options ??= new CsvReaderOptions(); + var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, options.BufferSize); + var reader = new StreamReader(stream, options.Encoding); + return new CsvStreamingReader(reader, options); + } + + /// + /// 从字符串创建 CSV 流式读取器 + /// + /// CSV 内容 + /// 选项 + /// CSV 流式读取器 + public static CsvStreamingReader FromString(string content, CsvReaderOptions? options = null) + { + var reader = new StringReader(content); + return new CsvStreamingReader(reader, options); + } + + /// + /// 读取标题行 + /// + private void ReadHeaders() + { + var line = _reader.ReadLine(); + _lineNumber++; + + if (line != null) + { + _headers = ParseLine(line); + } + } + + /// + /// 读取下一行 + /// + /// 字段数组,如果到达文件末尾则返回 null + public string[]? ReadLine() + { + while (true) + { + var line = _reader.ReadLine(); + _lineNumber++; + + if (line == null) + return null; + + if (_options.SkipEmptyLines && string.IsNullOrWhiteSpace(line)) + continue; + + return ParseLine(line); + } + } + + /// + /// 异步读取下一行 + /// + /// 取消令牌 + /// 字段数组,如果到达文件末尾则返回 null + public async Task ReadLineAsync(CancellationToken cancellationToken = default) + { + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + var line = await _reader.ReadLineAsync(); + + if (line == null) + return null; + + _lineNumber++; + + if (_options.SkipEmptyLines && string.IsNullOrWhiteSpace(line)) + continue; + + return ParseLine(line); + } + } + + /// + /// 读取所有行 + /// + /// 所有行的枚举 + public IEnumerable ReadAll() + { + string[]? line; + + while ((line = ReadLine()) != null) + { + yield return line; + } + } + + /// + /// 异步读取所有行 + /// + /// 取消令牌 + /// 所有行的异步枚举 + public async IAsyncEnumerable ReadAllAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + string[]? line; + + while ((line = await ReadLineAsync(cancellationToken)) != null) + { + yield return line; + } + } + + /// + /// 读取为字典(需要标题行) + /// + /// 字典行的枚举 + public IEnumerable> ReadAsDict() + { + if (_headers == null) + throw new InvalidOperationException("需要标题行才能读取为字典"); + + string[]? line; + + while ((line = ReadLine()) != null) + { + yield return LineToDict(line); + } + } + + /// + /// 异步读取为字典(需要标题行) + /// + /// 取消令牌 + /// 字典行的异步枚举 + public async IAsyncEnumerable> ReadAsDictAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + if (_headers == null) + throw new InvalidOperationException("需要标题行才能读取为字典"); + + string[]? line; + + while ((line = await ReadLineAsync(cancellationToken)) != null) + { + yield return LineToDict(line); + } + } + + /// + /// 解析 CSV 行 + /// + private string[] ParseLine(string line) + { + var fields = new List(); + var currentField = new StringBuilder(); + var inQuotes = false; + + for (int i = 0; i < line.Length; i++) + { + var c = line[i]; + + if (inQuotes) + { + if (c == _options.QuoteChar) + { + // 检查是否是转义引号 + if (i + 1 < line.Length && line[i + 1] == _options.QuoteChar) + { + currentField.Append(_options.QuoteChar); + i++; + } + else + { + inQuotes = false; + } + } + else + { + currentField.Append(c); + } + } + else + { + if (c == _options.QuoteChar) + { + inQuotes = true; + } + else if (c == _options.Delimiter) + { + fields.Add(FinalizeField(currentField)); + currentField.Clear(); + } + else + { + currentField.Append(c); + } + } + } + + fields.Add(FinalizeField(currentField)); + + return fields.ToArray(); + } + + private string FinalizeField(StringBuilder field) + { + var result = field.ToString(); + + if (_options.TrimFields) + { + result = result.Trim(); + } + + return result; + } + + private Dictionary LineToDict(string[] fields) + { + var dict = new Dictionary(); + + for (int i = 0; i < _headers!.Length && i < fields.Length; i++) + { + dict[_headers[i]] = fields[i]; + } + + return dict; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _reader.Dispose(); + _disposed = true; + } + } + } + + /// + /// CSV 流式写入器 + /// + public class CsvStreamingWriter : IDisposable + { + private readonly TextWriter _writer; + private readonly CsvReaderOptions _options; + private bool _disposed; + private bool _headerWritten; + + /// + /// 创建 CSV 流式写入器 + /// + /// 文本写入器 + /// 选项 + public CsvStreamingWriter(TextWriter writer, CsvReaderOptions? options = null) + { + _writer = writer ?? throw new ArgumentNullException(nameof(writer)); + _options = options ?? new CsvReaderOptions(); + } + + /// + /// 创建文件 CSV 写入器 + /// + /// 文件路径 + /// 选项 + /// 是否追加 + /// CSV 写入器 + public static CsvStreamingWriter ToFile(string filePath, CsvReaderOptions? options = null, bool append = false) + { + options ??= new CsvReaderOptions(); + var stream = new FileStream(filePath, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, options.BufferSize); + var writer = new StreamWriter(stream, options.Encoding); + return new CsvStreamingWriter(writer, options); + } + + /// + /// 写入标题行 + /// + /// 标题 + public void WriteHeaders(params string[] headers) + { + if (_headerWritten) + throw new InvalidOperationException("标题行已写入"); + + WriteLine(headers); + _headerWritten = true; + } + + /// + /// 写入一行 + /// + /// 字段 + public void WriteLine(params string[] fields) + { + var line = FormatLine(fields); + _writer.WriteLine(line); + } + + /// + /// 异步写入一行 + /// + /// 字段 + public async Task WriteLineAsync(params string[] fields) + { + var line = FormatLine(fields); + await _writer.WriteLineAsync(line); + } + + /// + /// 写入字典行 + /// + /// 字典 + /// 列顺序 + public void WriteDict(Dictionary dict, string[]? columnOrder = null) + { + var columns = columnOrder ?? dict.Keys.ToArray(); + var fields = columns.Select(c => dict.TryGetValue(c, out var v) ? v : "").ToArray(); + WriteLine(fields); + } + + /// + /// 异步写入字典行 + /// + /// 字典 + /// 列顺序 + public async Task WriteDictAsync(Dictionary dict, string[]? columnOrder = null) + { + var columns = columnOrder ?? dict.Keys.ToArray(); + var fields = columns.Select(c => dict.TryGetValue(c, out var v) ? v : "").ToArray(); + await WriteLineAsync(fields); + } + + /// + /// 刷新缓冲区 + /// + public void Flush() + { + _writer.Flush(); + } + + /// + /// 异步刷新缓冲区 + /// + public async Task FlushAsync() + { + await _writer.FlushAsync(); + } + + private string FormatLine(string[] fields) + { + var formattedFields = fields.Select(f => FormatField(f)); + return string.Join(_options.Delimiter, formattedFields); + } + + private string FormatField(string field) + { + if (string.IsNullOrEmpty(field)) + return ""; + + bool needsQuoting = field.Contains(_options.Delimiter) || + + field.Contains(_options.QuoteChar) || + + field.Contains('\n') || + + field.Contains('\r'); + + if (needsQuoting) + { + var escaped = field.Replace(_options.QuoteChar.ToString(), _options.QuoteChar.ToString() + _options.QuoteChar.ToString()); + return $"{_options.QuoteChar}{escaped}{_options.QuoteChar}"; + } + + return field; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _writer.Dispose(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/IOCategory/CsvUtil.cs b/EasyTool.Core/IOCategory/CsvUtil.cs new file mode 100644 index 0000000..94fba42 --- /dev/null +++ b/EasyTool.Core/IOCategory/CsvUtil.cs @@ -0,0 +1,622 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// CSV工具类 + /// 提供CSV文件的读写功能 + /// + public static class CsvUtil + { + #region 读取CSV + + /// + /// 读取CSV文件为字符串二维数组 + /// + /// 文件路径 + /// 编码 + /// 是否有标题行 + /// 分隔符 + /// 数据数组 + public static string[][] Read(string filePath, Encoding? encoding = null, bool hasHeader = false, char delimiter = ',') + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + var result = new List(); + + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) + { + var row = ParseLine(lines[i], delimiter); + result.Add(row); + } + + return result.ToArray(); + } + + /// + /// 异步读取CSV文件 + /// + public static async Task ReadAsync(string filePath, Encoding? encoding = null, bool hasHeader = false, char delimiter = ',') + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = await File.ReadAllLinesAsync(filePath, encoding); + var result = new List(); + + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) + { + var row = ParseLine(lines[i], delimiter); + result.Add(row); + } + + return result.ToArray(); + } + + /// + /// 读取CSV文件为对象列表 + /// + /// 对象类型 + /// 文件路径 + /// 编码 + /// 分隔符 + /// 对象列表 + public static List Read(string filePath, Encoding? encoding = null, char delimiter = ',') where T : class, new() + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(headers[j], StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + + result.Add(obj); + } + + return result; + } + + /// + /// 异步读取CSV文件为对象列表 + /// + public static async Task> ReadAsync(string filePath, Encoding? encoding = null, char delimiter = ',') where T : class, new() + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = await File.ReadAllLinesAsync(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(headers[j], StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + + result.Add(obj); + } + + return result; + } + + /// + /// 读取CSV文件(带标题映射) + /// + public static List Read(string filePath, Dictionary columnMapping, Encoding? encoding = null, char delimiter = ',') where T : class, new() + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("CSV文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + + if (lines.Length == 0) + return new List(); + + var headers = ParseLine(lines[0], delimiter); + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToList(); + + var result = new List(); + + for (int i = 1; i < lines.Length; i++) + { + var values = ParseLine(lines[i], delimiter); + var obj = new T(); + + for (int j = 0; j < headers.Length && j < values.Length; j++) + { + var csvColumn = headers[j]; + string? propertyName; + + if (columnMapping.TryGetValue(csvColumn, out propertyName) || + columnMapping.TryGetValue(csvColumn.ToLower(), out propertyName)) + { + var property = properties.FirstOrDefault(p => + p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); + + if (property != null) + { + var value = ConvertValue(values[j], property.PropertyType); + property.SetValue(obj, value); + } + } + } + + result.Add(obj); + } + + return result; + } + + #endregion + + #region 写入CSV + + /// + /// 写入CSV文件 + /// + /// 文件路径 + /// 数据 + /// 标题行 + /// 编码 + /// 分隔符 + public static void Write(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = new List(); + + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + File.WriteAllLines(filePath, lines, encoding); + } + + /// + /// 异步写入CSV文件 + /// + public static async Task WriteAsync(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var lines = new List(); + + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + await File.WriteAllLinesAsync(filePath, lines, encoding); + } + + /// + /// 写入对象列表到CSV文件 + /// + /// 对象类型 + /// 文件路径 + /// 数据列表 + /// 自定义标题(可选) + /// 编码 + /// 分隔符 + public static void Write(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); + + var lines = new List(); + + // 标题行 + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + lines.Add(FormatLine(propertyNames, delimiter)); + } + + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + lines.Add(FormatLine(values, delimiter)); + } + + File.WriteAllLines(filePath, lines, encoding); + } + + /// + /// 异步写入对象列表到CSV文件 + /// + public static async Task WriteAsync(string filePath, IEnumerable data, string[]? headers = null, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); + + var lines = new List(); + + // 标题行 + if (headers != null && headers.Length > 0) + { + lines.Add(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + lines.Add(FormatLine(propertyNames, delimiter)); + } + + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + lines.Add(FormatLine(values, delimiter)); + } + + await File.WriteAllLinesAsync(filePath, lines, encoding); + } + + /// + /// 追加数据到CSV文件 + /// + public static void Append(string filePath, IEnumerable data, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + + var lines = new List(); + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + File.AppendAllLines(filePath, lines, encoding); + } + + /// + /// 异步追加数据到CSV文件 + /// + public static async Task AppendAsync(string filePath, IEnumerable data, Encoding? encoding = null, char delimiter = ',') + { + encoding ??= Encoding.UTF8; + + var lines = new List(); + foreach (var row in data) + { + lines.Add(FormatLine(row, delimiter)); + } + + await File.AppendAllLinesAsync(filePath, lines, encoding); + } + + #endregion + + #region 解析与格式化 + + /// + /// 解析CSV行 + /// + private static string[] ParseLine(string line, char delimiter) + { + var result = new List(); + var current = new StringBuilder(); + bool inQuotes = false; + + for (int i = 0; i < line.Length; i++) + { + var c = line[i]; + + if (c == '"') + { + if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') + { + current.Append('"'); + i++; + } + else + { + inQuotes = !inQuotes; + } + } + else if (c == delimiter && !inQuotes) + { + result.Add(current.ToString()); + current.Clear(); + } + else + { + current.Append(c); + } + } + + result.Add(current.ToString()); + return result.ToArray(); + } + + /// + /// 格式化CSV行 + /// + private static string FormatLine(string[] values, char delimiter) + { + return string.Join(delimiter, values.Select(v => EscapeValue(v))); + } + + /// + /// 转义CSV值 + /// + private static string EscapeValue(string? value) + { + if (string.IsNullOrEmpty(value)) + return ""; + + if (value.Contains('"') || value.Contains(',') || value.Contains('\n') || value.Contains('\r')) + { + return $"\"{value.Replace("\"", "\"\"")}\""; + } + + return value; + } + + /// + /// 格式化值 + /// + private static string FormatValue(object? value) + { + if (value == null) + return ""; + + return value switch + { + DateTime dt => dt.ToString("yyyy-MM-dd HH:mm:ss"), + decimal dec => dec.ToString(CultureInfo.InvariantCulture), + double d => d.ToString(CultureInfo.InvariantCulture), + float f => f.ToString(CultureInfo.InvariantCulture), + bool b => b.ToString().ToLower(), + _ => value.ToString() ?? "" + }; + } + + /// + /// 转换值类型 + /// + private static object? ConvertValue(string value, Type targetType) + { + if (string.IsNullOrWhiteSpace(value)) + { + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + } + + try + { + if (targetType == typeof(string)) + return value; + + if (targetType == typeof(int) || targetType == typeof(int?)) + return int.Parse(value); + + if (targetType == typeof(long) || targetType == typeof(long?)) + return long.Parse(value); + + if (targetType == typeof(double) || targetType == typeof(double?)) + return double.Parse(value, CultureInfo.InvariantCulture); + + if (targetType == typeof(decimal) || targetType == typeof(decimal?)) + return decimal.Parse(value, CultureInfo.InvariantCulture); + + if (targetType == typeof(bool) || targetType == typeof(bool?)) + return bool.Parse(value); + + if (targetType == typeof(DateTime) || targetType == typeof(DateTime?)) + return DateTime.TryParse(value, out var dt) ? dt : DateTime.MinValue; + + if (targetType == typeof(Guid) || targetType == typeof(Guid?)) + return Guid.Parse(value); + + return Convert.ChangeType(value, targetType); + } + catch + { + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; + } + } + + #endregion + + #region 辅助方法 + + /// + /// 从字符串读取CSV数据 + /// + public static List Parse(string csvContent, bool hasHeader = false, char delimiter = ',') + { + var lines = csvContent.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); + var result = new List(); + + int startRow = hasHeader ? 1 : 0; + for (int i = startRow; i < lines.Length; i++) + { + var row = ParseLine(lines[i], delimiter); + result.Add(row); + } + + return result; + } + + /// + /// 将数据转换为CSV字符串 + /// + public static string ToCsvString(IEnumerable data, string[]? headers = null, char delimiter = ',') + { + var sb = new StringBuilder(); + + if (headers != null && headers.Length > 0) + { + sb.AppendLine(FormatLine(headers, delimiter)); + } + + foreach (var row in data) + { + sb.AppendLine(FormatLine(row, delimiter)); + } + + return sb.ToString(); + } + + /// + /// 将对象列表转换为CSV字符串 + /// + public static string ToCsvString(IEnumerable data, string[]? headers = null, char delimiter = ',') + { + var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead) + .ToList(); + + var sb = new StringBuilder(); + + // 标题行 + if (headers != null && headers.Length > 0) + { + sb.AppendLine(FormatLine(headers, delimiter)); + } + else + { + var propertyNames = properties.Select(p => p.Name).ToArray(); + sb.AppendLine(FormatLine(propertyNames, delimiter)); + } + + // 数据行 + foreach (var item in data) + { + var values = properties.Select(p => FormatValue(p.GetValue(item))).ToArray(); + sb.AppendLine(FormatLine(values, delimiter)); + } + + return sb.ToString(); + } + + /// + /// 获取CSV文件的列数 + /// + public static int GetColumnCount(string filePath, Encoding? encoding = null, char delimiter = ',') + { + if (!File.Exists(filePath)) + return 0; + + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(filePath, encoding); + var firstLine = reader.ReadLine(); + + if (string.IsNullOrEmpty(firstLine)) + return 0; + + return ParseLine(firstLine, delimiter).Length; + } + + /// + /// 获取CSV文件的行数 + /// + public static int GetRowCount(string filePath, bool hasHeader = true, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + return 0; + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + return hasHeader ? Math.Max(0, lines.Length - 1) : lines.Length; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/ExcelUtil.cs b/EasyTool.Core/IOCategory/ExcelUtil.cs new file mode 100644 index 0000000..3e51a6a --- /dev/null +++ b/EasyTool.Core/IOCategory/ExcelUtil.cs @@ -0,0 +1,400 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Security; +using System.Text; +using System.Xml; +using System.Xml.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// Excel工具类(轻量级实现,不依赖第三方库) + /// 支持读取和写入xlsx格式文件 + /// + public static class ExcelUtil + { + private const string NS_SS = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; + private const string NS_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; + private static readonly string[] ColumnNames = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BE,BF,BG,BH,BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ".Split(','); + + #region 读取Excel + + /// + /// 读取Excel文件为DataTable + /// + public static DataTable Read(string filePath, int sheetIndex = 0, bool hasHeader = true) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + return Read(stream, sheetIndex, hasHeader); + } + + /// + /// 从流读取Excel为DataTable + /// + public static DataTable Read(Stream stream, int sheetIndex = 0, bool hasHeader = true) + { + using var archive = new ZipArchive(stream, ZipArchiveMode.Read); + var sharedStrings = LoadSharedStrings(archive); + var sheetEntry = GetSheetEntry(archive, sheetIndex); + + if (sheetEntry == null) + throw new ArgumentException($"工作表索引 {sheetIndex} 不存在"); + + return ParseWorksheet(sheetEntry, sharedStrings, hasHeader); + } + + /// + /// 获取所有工作表名称 + /// + public static List GetSheetNames(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + return GetSheetNames(stream); + } + + /// + /// 从流获取所有工作表名称 + /// + public static List GetSheetNames(Stream stream) + { + var names = new List(); + using var archive = new ZipArchive(stream, ZipArchiveMode.Read); + + var workbookEntry = archive.GetEntry("xl/workbook.xml"); + if (workbookEntry == null) return names; + + using var reader = new StreamReader(workbookEntry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var sheets = doc.Root?.Element(ns + "sheets")?.Elements(ns + "sheet"); + if (sheets != null) + { + foreach (var sheet in sheets) + { + var name = sheet.Attribute("name")?.Value; + if (!string.IsNullOrEmpty(name)) + names.Add(name); + } + } + + return names; + } + + private static Dictionary LoadSharedStrings(ZipArchive archive) + { + var strings = new Dictionary(); + var entry = archive.GetEntry("xl/sharedStrings.xml"); + if (entry == null) return strings; + + using var reader = new StreamReader(entry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var siElements = doc.Root?.Elements(ns + "si"); + if (siElements == null) return strings; + + int index = 0; + foreach (var si in siElements) + { + var text = si.Element(ns + "t")?.Value ?? ""; + strings[index++] = text; + } + + return strings; + } + + private static ZipArchiveEntry? GetSheetEntry(ZipArchive archive, int sheetIndex) + { + var entries = new List(); + foreach (var entry in archive.Entries) + { + if (entry.FullName.StartsWith("xl/worksheets/sheet") && entry.FullName.EndsWith(".xml")) + entries.Add(entry); + } + + entries.Sort((a, b) => string.Compare(a.FullName, b.FullName, StringComparison.Ordinal)); + return sheetIndex < entries.Count ? entries[sheetIndex] : null; + } + + private static DataTable ParseWorksheet(ZipArchiveEntry entry, Dictionary sharedStrings, bool hasHeader) + { + var table = new DataTable(); + + using var reader = new StreamReader(entry.Open()); + var doc = XDocument.Load(reader); + XNamespace ns = NS_SS; + + var sheetData = doc.Root?.Element(ns + "sheetData"); + if (sheetData == null) return table; + + var rows = sheetData.Elements(ns + "row").ToList(); + if (rows.Count == 0) return table; + + // 解析所有行数据 + var allData = new List>(); + int maxCols = 0; + + foreach (var row in rows) + { + var rowData = new List(); + var cells = row.Elements(ns + "c"); + + foreach (var cell in cells) + { + var refAttr = cell.Attribute("r")?.Value ?? ""; + var type = cell.Attribute("t")?.Value; + var value = cell.Element(ns + "v")?.Value ?? ""; + + if (type == "s" && int.TryParse(value, out int sharedIndex)) + { + value = sharedStrings.TryGetValue(sharedIndex, out var s) ? s : ""; + } + + rowData.Add(value); + } + + if (rowData.Count > maxCols) + maxCols = rowData.Count; + + allData.Add(rowData); + } + + // 创建列 + if (hasHeader && allData.Count > 0) + { + var headers = allData[0]; + for (int i = 0; i < maxCols; i++) + { + var colName = i < headers.Count && !string.IsNullOrEmpty(headers[i]) + ? headers[i] + : $"Column{i + 1}"; + table.Columns.Add(colName, typeof(string)); + } + allData.RemoveAt(0); + } + else + { + for (int i = 0; i < maxCols; i++) + table.Columns.Add($"Column{i + 1}", typeof(string)); + } + + // 添加数据行 + foreach (var rowData in allData) + { + var row = table.NewRow(); + for (int i = 0; i < Math.Min(rowData.Count, maxCols); i++) + { + row[i] = rowData[i]; + } + table.Rows.Add(row); + } + + return table; + } + + #endregion + + #region 写入Excel + + /// + /// 将DataTable写入Excel文件 + /// + public static void Write(string filePath, DataTable dataTable, string sheetName = "Sheet1") + { + using var stream = File.Create(filePath); + Write(stream, dataTable, sheetName); + } + + /// + /// 将DataTable写入Excel流 + /// + public static void Write(Stream stream, DataTable dataTable, string sheetName = "Sheet1") + { + using var archive = new ZipArchive(stream, ZipArchiveMode.Create); + + // 创建必要的文件结构 + CreateContentType(archive); + CreateRels(archive); + CreateWorkbook(archive, sheetName); + CreateWorkbookRels(archive); + CreateWorksheet(archive, dataTable); + CreateStyles(archive); + } + + private static void CreateContentType(ZipArchive archive) + { + var entry = archive.CreateEntry("[Content_Types].xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + + + + +"); + } + + private static void CreateRels(ZipArchive archive) + { + var entry = archive.CreateEntry("_rels/.rels"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + +"); + } + + private static void CreateWorkbook(ZipArchive archive, string sheetName) + { + var entry = archive.CreateEntry("xl/workbook.xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write($@" + + + + +"); + } + + private static void CreateWorkbookRels(ZipArchive archive) + { + var entry = archive.CreateEntry("xl/_rels/workbook.xml.rels"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + +"); + } + + private static void CreateWorksheet(ZipArchive archive, DataTable dataTable) + { + var entry = archive.CreateEntry("xl/worksheets/sheet1.xml"); + using var writer = new StreamWriter(entry.Open()); + + writer.Write($@" + +"); + + for (int r = 0; r < dataTable.Rows.Count; r++) + { + writer.Write($""); + + for (int c = 0; c < dataTable.Columns.Count; c++) + { + var cellRef = GetColumnName(c) + (r + 1); + var value = dataTable.Rows[r][c]?.ToString() ?? ""; + + // 尝试解析为数字 + if (double.TryParse(value, out double numValue)) + { + writer.Write($"{numValue}"); + } + else + { + writer.Write($"{SecurityElement.Escape(value)}"); + } + } + + writer.Write(""); + } + + writer.Write(""); + } + + private static void CreateStyles(ZipArchive archive) + { + var entry = archive.CreateEntry("xl/styles.xml"); + using var writer = new StreamWriter(entry.Open()); + writer.Write(@" + + + + + + +"); + } + + private static string GetColumnName(int index) + { + if (index < ColumnNames.Length) + return ColumnNames[index]; + + var name = new StringBuilder(); + index++; + while (index > 0) + { + index--; + name.Insert(0, (char)('A' + index % 26)); + index /= 26; + } + return name.ToString(); + } + + #endregion + + #region 辅助方法 + + /// + /// 将List转换为DataTable + /// + public static DataTable ToDataTable(IEnumerable list) + { + var table = new DataTable(); + var properties = typeof(T).GetProperties(); + + foreach (var prop in properties) + table.Columns.Add(prop.Name, typeof(object)); + + foreach (var item in list) + { + var row = table.NewRow(); + foreach (var prop in properties) + row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; + table.Rows.Add(row); + } + + return table; + } + + /// + /// 将DataTable转换为List + /// + public static List ToList(DataTable table) where T : new() + { + var list = new List(); + var properties = typeof(T).GetProperties(); + + foreach (DataRow row in table.Rows) + { + var item = new T(); + foreach (var prop in properties) + { + if (table.Columns.Contains(prop.Name) && row[prop.Name] != DBNull.Value) + { + var value = Convert.ChangeType(row[prop.Name], prop.PropertyType); + prop.SetValue(item, value); + } + } + list.Add(item); + } + + return list; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/FileChunkUtil.cs b/EasyTool.Core/IOCategory/FileChunkUtil.cs new file mode 100644 index 0000000..4f10c97 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileChunkUtil.cs @@ -0,0 +1,395 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件分片工具类 + /// 支持大文件分片上传、下载和合并 + /// + public static class FileChunkUtil + { + /// + /// 分片文件 + /// + /// 源文件路径 + /// 输出目录 + /// 分片大小(字节) + /// 进度回调 + /// 分片信息 + public static ChunkInfo Split(string filePath, string outputDir, long chunkSize = 5 * 1024 * 1024, Action? progress = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + Directory.CreateDirectory(outputDir); + + var fileInfo = new FileInfo(filePath); + var fileName = Path.GetFileNameWithoutExtension(filePath); + var extension = Path.GetExtension(filePath); + var totalChunks = (int)Math.Ceiling((double)fileInfo.Length / chunkSize); + var fileId = Guid.NewGuid().ToString("N"); + + var chunkInfo = new ChunkInfo + { + FileId = fileId, + FileName = fileInfo.Name, + FileSize = fileInfo.Length, + ChunkSize = chunkSize, + TotalChunks = totalChunks, + FileHash = ComputeFileHash(filePath), + Chunks = new List() + }; + + using var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + var buffer = new byte[Math.Min(chunkSize, int.MaxValue)]; + + for (int i = 0; i < totalChunks; i++) + { + var chunkFileName = $"{fileName}_{i + 1:D5}{extension}.chunk"; + var chunkFilePath = Path.Combine(outputDir, chunkFileName); + + var bytesRead = sourceStream.Read(buffer, 0, buffer.Length); + + using var chunkStream = new FileStream(chunkFilePath, FileMode.Create, FileAccess.Write); + chunkStream.Write(buffer, 0, bytesRead); + + var chunkHash = ComputeHash(buffer, 0, bytesRead); + + chunkInfo.Chunks.Add(new ChunkDetail + { + Index = i + 1, + ChunkFile = chunkFileName, + Size = bytesRead, + Hash = chunkHash + }); + + progress?.Invoke((double)(i + 1) / totalChunks * 100); + } + + // 保存分片信息文件 + var infoPath = Path.Combine(outputDir, $"{fileName}.chunkinfo"); + SaveChunkInfo(chunkInfo, infoPath); + + return chunkInfo; + } + + /// + /// 异步分片文件 + /// + public static async Task SplitAsync(string filePath, string outputDir, long chunkSize = 5 * 1024 * 1024, Action? progress = null) + { + return await Task.Run(() => Split(filePath, outputDir, chunkSize, progress)); + } + + /// + /// 合并分片文件 + /// + /// 分片信息 + /// 分片文件目录 + /// 输出文件路径 + /// 进度回调 + public static void Merge(ChunkInfo chunkInfo, string chunkDir, string outputPath, Action? progress = null) + { + var dir = new DirectoryInfo(chunkDir); + + using var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write); + + foreach (var chunk in chunkInfo.Chunks.OrderBy(c => c.Index)) + { + var chunkFilePath = Path.Combine(chunkDir, chunk.ChunkFile); + + if (!File.Exists(chunkFilePath)) + throw new FileNotFoundException($"分片文件不存在: {chunk.ChunkFile}"); + + using var chunkStream = new FileStream(chunkFilePath, FileMode.Open, FileAccess.Read); + var buffer = new byte[chunk.Size]; + chunkStream.Read(buffer, 0, buffer.Length); + + // 验证分片哈希 + var hash = ComputeHash(buffer, 0, buffer.Length); + if (hash != chunk.Hash) + throw new InvalidDataException($"分片 {chunk.Index} 哈希验证失败"); + + outputStream.Write(buffer, 0, buffer.Length); + + progress?.Invoke((double)chunk.Index / chunkInfo.TotalChunks * 100); + } + + // 验证最终文件哈希 + var finalHash = ComputeFileHash(outputPath); + if (finalHash != chunkInfo.FileHash) + throw new InvalidDataException("合并后文件哈希验证失败"); + } + + /// + /// 异步合并分片文件 + /// + public static async Task MergeAsync(ChunkInfo chunkInfo, string chunkDir, string outputPath, Action? progress = null) + { + await Task.Run(() => Merge(chunkInfo, chunkDir, outputPath, progress)); + } + + /// + /// 从信息文件加载分片信息 + /// + /// 信息文件路径 + /// 分片信息 + public static ChunkInfo LoadChunkInfo(string infoFilePath) + { + var json = File.ReadAllText(infoFilePath); + return System.Text.Json.JsonSerializer.Deserialize(json) + ?? throw new InvalidDataException("无效的分片信息文件"); + } + + /// + /// 保存分片信息到文件 + /// + private static void SaveChunkInfo(ChunkInfo chunkInfo, string infoFilePath) + { + var json = System.Text.Json.JsonSerializer.Serialize(chunkInfo, new System.Text.Json.JsonSerializerOptions + { + WriteIndented = true + }); + File.WriteAllText(infoFilePath, json); + } + + /// + /// 验证分片完整性 + /// + /// 分片信息 + /// 分片目录 + /// 验证结果 + public static ChunkValidationResult Validate(ChunkInfo chunkInfo, string chunkDir) + { + var result = new ChunkValidationResult + { + IsValid = true, + MissingChunks = new List(), + CorruptedChunks = new List() + }; + + foreach (var chunk in chunkInfo.Chunks) + { + var chunkFilePath = Path.Combine(chunkDir, chunk.ChunkFile); + + if (!File.Exists(chunkFilePath)) + { + result.IsValid = false; + result.MissingChunks.Add(chunk.Index); + continue; + } + + var fileInfo = new FileInfo(chunkFilePath); + if (fileInfo.Length != chunk.Size) + { + result.IsValid = false; + result.CorruptedChunks.Add(chunk.Index); + continue; + } + + var hash = ComputeFileHash(chunkFilePath); + if (hash != chunk.Hash) + { + result.IsValid = false; + result.CorruptedChunks.Add(chunk.Index); + } + } + + return result; + } + + /// + /// 获取上传进度 + /// + /// 分片信息 + /// 已上传的分片索引 + /// 上传进度(百分比) + public static double GetUploadProgress(ChunkInfo chunkInfo, HashSet uploadedChunks) + { + if (chunkInfo.TotalChunks == 0) + return 0; + + return (double)uploadedChunks.Count / chunkInfo.TotalChunks * 100; + } + + /// + /// 计算文件哈希 + /// + private static string ComputeFileHash(string filePath) + { + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 计算数据哈希 + /// + private static string ComputeHash(byte[] buffer, int offset, int count) + { + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(buffer, offset, count); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + #region 流式分片 + + /// + /// 流式读取分片 + /// + /// 输入流 + /// 分片大小 + /// 分片数据枚举 + public static IEnumerable ReadChunks(Stream stream, long chunkSize = 5 * 1024 * 1024) + { + var buffer = new byte[Math.Min(chunkSize, int.MaxValue)]; + int index = 1; + + while (true) + { + var bytesRead = stream.Read(buffer, 0, buffer.Length); + if (bytesRead == 0) + break; + + var chunk = new byte[bytesRead]; + Buffer.BlockCopy(buffer, 0, chunk, 0, bytesRead); + + yield return new ChunkData + { + Index = index++, + Data = chunk + }; + } + } + + /// + /// 流式写入分片 + /// + /// 输出流 + /// 分片数据 + public static void WriteChunks(Stream stream, IEnumerable chunks) + { + foreach (var chunk in chunks.OrderBy(c => c.Index)) + { + stream.Write(chunk.Data, 0, chunk.Data.Length); + } + } + + #endregion + } + + /// + /// 分片信息 + /// + public class ChunkInfo + { + /// + /// 文件唯一标识 + /// + public string FileId { get; set; } = string.Empty; + + /// + /// 原始文件名 + /// + public string FileName { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long FileSize { get; set; } + + /// + /// 分片大小 + /// + public long ChunkSize { get; set; } + + /// + /// 总分片数 + /// + public int TotalChunks { get; set; } + + /// + /// 文件哈希 + /// + public string FileHash { get; set; } = string.Empty; + + /// + /// 分片详情列表 + /// + public List Chunks { get; set; } = new(); + } + + /// + /// 分片详情 + /// + public class ChunkDetail + { + /// + /// 分片索引(从1开始) + /// + public int Index { get; set; } + + /// + /// 分片文件名 + /// + public string ChunkFile { get; set; } = string.Empty; + + /// + /// 分片大小 + /// + public int Size { get; set; } + + /// + /// 分片哈希 + /// + public string Hash { get; set; } = string.Empty; + } + + /// + /// 分片数据 + /// + public class ChunkData + { + /// + /// 分片索引 + /// + public int Index { get; set; } + + /// + /// 分片数据 + /// + public byte[] Data { get; set; } = Array.Empty(); + + /// + /// 大小 + /// + public int Size => Data.Length; + } + + /// + /// 分片验证结果 + /// + public class ChunkValidationResult + { + /// + /// 是否完整有效 + /// + public bool IsValid { get; set; } + + /// + /// 缺失的分片索引 + /// + public List MissingChunks { get; set; } = new(); + + /// + /// 损坏的分片索引 + /// + public List CorruptedChunks { get; set; } = new(); + } +} diff --git a/EasyTool.Core/IOCategory/FileCompareUtil.cs b/EasyTool.Core/IOCategory/FileCompareUtil.cs new file mode 100644 index 0000000..8bf698d --- /dev/null +++ b/EasyTool.Core/IOCategory/FileCompareUtil.cs @@ -0,0 +1,258 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; + +namespace EasyTool.IOCategory +{ + /// + /// 文件比较工具类 + /// + public static class FileCompareUtil + { + /// + /// 比较两个文件内容是否相同 + /// + public static bool AreContentsEqual(string filePath1, string filePath2) + { + if (!File.Exists(filePath1) || !File.Exists(filePath2)) + return false; + + var fileInfo1 = new FileInfo(filePath1); + var fileInfo2 = new FileInfo(filePath2); + + // 大小不同,内容肯定不同 + if (fileInfo1.Length != fileInfo2.Length) + return false; + + // 逐字节比较 + using var stream1 = File.OpenRead(filePath1); + using var stream2 = File.OpenRead(filePath2); + + var buffer1 = new byte[4096]; + var buffer2 = new byte[4096]; + + while (true) + { + var count1 = stream1.Read(buffer1, 0, buffer1.Length); + var count2 = stream2.Read(buffer2, 0, buffer2.Length); + + if (count1 != count2) + return false; + + if (count1 == 0) + return true; + + for (int i = 0; i < count1; i++) + { + if (buffer1[i] != buffer2[i]) + return false; + } + } + } + + /// + /// 比较两个文件内容是否相同(使用哈希) + /// + public static bool AreContentsEqualByHash(string filePath1, string filePath2) + { + if (!File.Exists(filePath1) || !File.Exists(filePath2)) + return false; + + var hash1 = ComputeFileHash(filePath1); + var hash2 = ComputeFileHash(filePath2); + + return hash1 == hash2; + } + + /// + /// 计算文件哈希值 + /// + public static string ComputeFileHash(string filePath, string algorithm = "MD5") + { + using var stream = File.OpenRead(filePath); + using HashAlgorithm hasher = algorithm.ToUpper() switch + { + "MD5" => MD5.Create(), + "SHA1" => SHA1.Create(), + "SHA256" => SHA256.Create(), + "SHA512" => SHA512.Create(), + _ => MD5.Create() + }; + + var hash = hasher.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLower(); + } + + /// + /// 比较两个目录 + /// + public static DirectoryCompareResult CompareDirectories(string directory1, string directory2, string searchPattern = "*") + { + var result = new DirectoryCompareResult(); + + var files1 = Directory.GetFiles(directory1, searchPattern, SearchOption.AllDirectories); + var files2 = Directory.GetFiles(directory2, searchPattern, SearchOption.AllDirectories); + + var relativePath1 = new Dictionary(); + var relativePath2 = new Dictionary(); + + foreach (var file in files1) + { + var relative = file.Substring(directory1.Length).TrimStart(Path.DirectorySeparatorChar); + relativePath1[relative] = file; + } + + foreach (var file in files2) + { + var relative = file.Substring(directory2.Length).TrimStart(Path.DirectorySeparatorChar); + relativePath2[relative] = file; + } + + // 只在目录1中的文件 + foreach (var kvp in relativePath1) + { + if (!relativePath2.ContainsKey(kvp.Key)) + { + result.OnlyInDirectory1.Add(kvp.Value); + } + } + + // 只在目录2中的文件 + foreach (var kvp in relativePath2) + { + if (!relativePath1.ContainsKey(kvp.Key)) + { + result.OnlyInDirectory2.Add(kvp.Value); + } + } + + // 两边都有的文件 + foreach (var kvp in relativePath1) + { + if (relativePath2.TryGetValue(kvp.Key, out var file2)) + { + if (AreContentsEqual(kvp.Value, file2)) + { + result.IdenticalFiles.Add(kvp.Value); + } + else + { + result.DifferentFiles.Add(new FileDifference + { + File1 = kvp.Value, + File2 = file2 + }); + } + } + } + + return result; + } + + /// + /// 查找重复文件 + /// + public static List> FindDuplicateFiles(string directory, string searchPattern = "*") + { + var files = Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories); + var hashGroups = new Dictionary>(); + + foreach (var file in files) + { + try + { + var hash = ComputeFileHash(file); + if (!hashGroups.ContainsKey(hash)) + hashGroups[hash] = new List(); + hashGroups[hash].Add(file); + } + catch + { + // 忽略无法读取的文件 + } + } + + return hashGroups.Values.Where(g => g.Count > 1).ToList(); + } + + /// + /// 查找相似文件(大小相同) + /// + public static List> FindSimilarSizedFiles(string directory, string searchPattern = "*") + { + var files = Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories); + var sizeGroups = new Dictionary>(); + + foreach (var file in files) + { + try + { + var size = new FileInfo(file).Length; + if (!sizeGroups.ContainsKey(size)) + sizeGroups[size] = new List(); + sizeGroups[size].Add(file); + } + catch + { + // 忽略无法读取的文件 + } + } + + return sizeGroups.Values.Where(g => g.Count > 1).ToList(); + } + } + + /// + /// 目录比较结果 + /// + public class DirectoryCompareResult + { + /// + /// 只在目录1中的文件 + /// + public List OnlyInDirectory1 { get; } = new(); + + /// + /// 只在目录2中的文件 + /// + public List OnlyInDirectory2 { get; } = new(); + + /// + /// 相同的文件 + /// + public List IdenticalFiles { get; } = new(); + + /// + /// 不同的文件 + /// + public List DifferentFiles { get; } = new(); + + /// + /// 是否完全相同 + /// + public bool AreIdentical => OnlyInDirectory1.Count == 0 && OnlyInDirectory2.Count == 0 && DifferentFiles.Count == 0; + } + + /// + /// 文件差异 + /// + public class FileDifference + { + /// + /// 文件1路径 + /// + public string File1 { get; set; } = string.Empty; + + /// + /// 文件2路径 + /// + public string File2 { get; set; } = string.Empty; + + public override string ToString() + { + return $"{File1} <-> {File2}"; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/FileLockUtil.cs b/EasyTool.Core/IOCategory/FileLockUtil.cs new file mode 100644 index 0000000..0912081 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileLockUtil.cs @@ -0,0 +1,331 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件锁选项 + /// + public class FileLockOptions + { + /// + /// 锁超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 重试间隔 + /// + public TimeSpan RetryInterval { get; set; } = TimeSpan.FromMilliseconds(100); + + /// + /// 锁文件目录 + /// + public string LockDirectory { get; set; } = Path.GetTempPath(); + } + + /// + /// 文件锁工具类 + /// 提供跨进程的文件锁定机制 + /// + public static class FileLockUtil + { + private static readonly FileLockOptions _defaultOptions = new(); + + /// + /// 获取文件锁 + /// + /// 要锁定的文件路径 + /// 锁选项 + /// 文件锁 + public static FileLock Acquire(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + + var lockFilePath = GetLockFilePath(filePath, options); + var startTime = DateTime.UtcNow; + + while (true) + { + try + { + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + // 写入锁信息 + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + fileStream.Write(bytes, 0, bytes.Length); + fileStream.Flush(); + + return new FileLock(lockFilePath, fileStream); + } + catch (IOException) + { + // 检查是否超时 + if (DateTime.UtcNow - startTime >= options.Timeout) + { + throw new TimeoutException($"获取文件锁超时: {filePath}"); + } + + Thread.Sleep(options.RetryInterval); + } + } + } + + /// + /// 异步获取文件锁 + /// + /// 要锁定的文件路径 + /// 锁选项 + /// 取消令牌 + /// 文件锁 + public static async Task AcquireAsync(string filePath, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + options ??= _defaultOptions; + + var lockFilePath = GetLockFilePath(filePath, options); + var startTime = DateTime.UtcNow; + + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + await fileStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + await fileStream.FlushAsync(cancellationToken); + + return new FileLock(lockFilePath, fileStream); + } + catch (IOException) + { + if (DateTime.UtcNow - startTime >= options.Timeout) + { + throw new TimeoutException($"获取文件锁超时: {filePath}"); + } + + await Task.Delay(options.RetryInterval, cancellationToken); + } + } + } + + /// + /// 尝试获取文件锁 + /// + /// 要锁定的文件路径 + /// 文件锁 + /// 锁选项 + /// 是否成功获取 + public static bool TryAcquire(string filePath, out FileLock? fileLock, FileLockOptions? options = null) + { + options ??= _defaultOptions; + + try + { + var lockFilePath = GetLockFilePath(filePath, options); + var fileStream = new FileStream( + lockFilePath, + FileMode.CreateNew, + FileAccess.Write, + FileShare.None, + 1, + FileOptions.DeleteOnClose); + + var lockInfo = $"{Environment.MachineName}|{Process.GetCurrentProcess().Id}|{DateTime.UtcNow:O}"; + var bytes = System.Text.Encoding.UTF8.GetBytes(lockInfo); + fileStream.Write(bytes, 0, bytes.Length); + fileStream.Flush(); + + fileLock = new FileLock(lockFilePath, fileStream); + return true; + } + catch + { + fileLock = null; + return false; + } + } + + /// + /// 检查文件是否被锁定 + /// + /// 文件路径 + /// 锁选项 + /// 是否被锁定 + public static bool IsLocked(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + var lockFilePath = GetLockFilePath(filePath, options); + + if (!File.Exists(lockFilePath)) + return false; + + // 尝试打开锁文件 + try + { + using var stream = new FileStream( + lockFilePath, + FileMode.Open, + FileAccess.Read, + FileShare.None); + + return false; + } + catch + { + return true; + } + } + + /// + /// 强制释放文件锁 + /// + /// 文件路径 + /// 锁选项 + /// 是否成功释放 + public static bool ForceRelease(string filePath, FileLockOptions? options = null) + { + options ??= _defaultOptions; + var lockFilePath = GetLockFilePath(filePath, options); + + try + { + if (File.Exists(lockFilePath)) + { + File.Delete(lockFilePath); + } + return true; + } + catch + { + return false; + } + } + + /// + /// 使用文件锁执行操作 + /// + /// 文件路径 + /// 操作 + /// 锁选项 + public static void WithLock(string filePath, Action action, FileLockOptions? options = null) + { + using var fileLock = Acquire(filePath, options); + action(); + } + + /// + /// 使用文件锁执行操作并返回结果 + /// + /// 返回类型 + /// 文件路径 + /// 操作 + /// 锁选项 + /// 操作结果 + public static T WithLock(string filePath, Func func, FileLockOptions? options = null) + { + using var fileLock = Acquire(filePath, options); + return func(); + } + + /// + /// 异步使用文件锁执行操作 + /// + /// 文件路径 + /// 操作 + /// 锁选项 + /// 取消令牌 + public static async Task WithLockAsync(string filePath, Func action, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + using var fileLock = await AcquireAsync(filePath, options, cancellationToken); + await action(); + } + + /// + /// 异步使用文件锁执行操作并返回结果 + /// + /// 返回类型 + /// 文件路径 + /// 操作 + /// 锁选项 + /// 取消令牌 + /// 操作结果 + public static async Task WithLockAsync(string filePath, Func> func, FileLockOptions? options = null, CancellationToken cancellationToken = default) + { + using var fileLock = await AcquireAsync(filePath, options, cancellationToken); + return await func(); + } + + private static string GetLockFilePath(string filePath, FileLockOptions options) + { + var fileName = Path.GetFileName(filePath); + var directory = Path.GetDirectoryName(Path.GetFullPath(filePath)); + + // 使用文件路径的哈希作为锁文件名的一部分 + var hash = Math.Abs(directory?.GetHashCode() ?? 0); + var lockFileName = $"{fileName}.{hash}.lock"; + + return Path.Combine(options.LockDirectory, lockFileName); + } + } + + /// + /// 文件锁 + /// + public class FileLock : IDisposable + { + private readonly string _lockFilePath; + private readonly FileStream _fileStream; + private bool _disposed; + + internal FileLock(string lockFilePath, FileStream fileStream) + { + _lockFilePath = lockFilePath; + _fileStream = fileStream; + } + + /// + /// 锁文件路径 + /// + public string LockFilePath => _lockFilePath; + + /// + /// 释放锁 + /// + public void Dispose() + { + if (!_disposed) + { + _fileStream?.Dispose(); + _disposed = true; + } + } + + /// + /// 释放锁 + /// + public void Release() + { + Dispose(); + } + } +} diff --git a/EasyTool.Core/IOCategory/FileSearch.cs b/EasyTool.Core/IOCategory/FileSearch.cs new file mode 100644 index 0000000..70fa22f --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSearch.cs @@ -0,0 +1,286 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 文件搜索工具类 + /// + public static class FileSearch + { + /// + /// 搜索文件 + /// + /// 搜索目录 + /// 搜索模式 + /// 是否搜索子目录 + /// 文件路径列表 + public static List SearchFiles(string directory, string pattern = "*", bool searchSubdirectories = true) + { + var option = searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + + try + { + return Directory.GetFiles(directory, pattern, option).ToList(); + } + catch (UnauthorizedAccessException) + { + return new List(); + } + } + + /// + /// 搜索文件(多个模式) + /// + public static List SearchFiles(string directory, string[] patterns, bool searchSubdirectories = true) + { + var results = new List(); + + foreach (var pattern in patterns) + { + results.AddRange(SearchFiles(directory, pattern, searchSubdirectories)); + } + + return results.Distinct().ToList(); + } + + /// + /// 搜索目录 + /// + public static List SearchDirectories(string directory, string pattern = "*", bool searchSubdirectories = true) + { + var option = searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + + try + { + return Directory.GetDirectories(directory, pattern, option).ToList(); + } + catch (UnauthorizedAccessException) + { + return new List(); + } + } + + /// + /// 按大小搜索文件 + /// + public static List SearchBySize(string directory, long minSize = 0, long maxSize = long.MaxValue, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var results = new List(); + + foreach (var file in files) + { + try + { + var info = new FileInfo(file); + if (info.Length >= minSize && info.Length <= maxSize) + { + results.Add(file); + } + } + catch + { + // 忽略无法访问的文件 + } + } + + return results; + } + + /// + /// 按修改时间搜索文件 + /// + public static List SearchByDate(string directory, DateTime? startTime = null, DateTime? endTime = null, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var results = new List(); + + foreach (var file in files) + { + try + { + var info = new FileInfo(file); + var writeTime = info.LastWriteTime; + + var afterStart = !startTime.HasValue || writeTime >= startTime.Value; + var beforeEnd = !endTime.HasValue || writeTime <= endTime.Value; + + if (afterStart && beforeEnd) + { + results.Add(file); + } + } + catch + { + // 忽略无法访问的文件 + } + } + + return results; + } + + /// + /// 按内容搜索文件 + /// + public static async Task> SearchByContent(string directory, string searchText, bool searchSubdirectories = true, bool ignoreCase = true) + { + var files = SearchFiles(directory, "*.txt", searchSubdirectories); + files.AddRange(SearchFiles(directory, "*.log", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.json", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.xml", searchSubdirectories)); + files.AddRange(SearchFiles(directory, "*.cs", searchSubdirectories)); + + var results = new List(); + var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + foreach (var file in files.Distinct()) + { + try + { + var content = await File.ReadAllTextAsync(file); + if (content.Contains(searchText, comparison)) + { + results.Add(file); + } + } + catch + { + // 忽略无法读取的文件 + } + } + + return results; + } + + /// + /// 查找重复文件 + /// + public static List> FindDuplicates(string directory, bool searchSubdirectories = true) + { + var files = SearchFiles(directory, "*", searchSubdirectories); + var sizeGroups = files.GroupBy(f => + { + try + { + return new FileInfo(f).Length; + } + catch + { + return -1L; + } + }).Where(g => g.Key > 0 && g.Count() > 1); + + var duplicates = new List>(); + + foreach (var group in sizeGroups) + { + var sameSizeFiles = group.ToList(); + var hashGroups = sameSizeFiles.GroupBy(f => + { + try + { + using var stream = File.OpenRead(f); + using var md5 = System.Security.Cryptography.MD5.Create(); + var hash = md5.ComputeHash(stream); + return Convert.ToBase64String(hash); + } + catch + { + return string.Empty; + } + }).Where(g => !string.IsNullOrEmpty(g.Key) && g.Count() > 1); + + foreach (var hashGroup in hashGroups) + { + duplicates.Add(hashGroup.ToList()); + } + } + + return duplicates; + } + + /// + /// 查找空目录 + /// + public static List FindEmptyDirectories(string directory) + { + var emptyDirs = new List(); + + try + { + var subDirs = Directory.GetDirectories(directory, "*", SearchOption.AllDirectories); + + foreach (var dir in subDirs) + { + try + { + if (!Directory.EnumerateFileSystemEntries(dir).Any()) + { + emptyDirs.Add(dir); + } + } + catch + { + // 忽略无法访问的目录 + } + } + } + catch + { + // 忽略无法访问的目录 + } + + return emptyDirs; + } + + /// + /// 获取目录大小 + /// + public static long GetDirectorySize(string directory) + { + var files = SearchFiles(directory, "*", true); + long size = 0; + + foreach (var file in files) + { + try + { + size += new FileInfo(file).Length; + } + catch + { + // 忽略无法访问的文件 + } + } + + return size; + } + + /// + /// 获取文件统计信息 + /// + public static Dictionary GetFileStatistics(string directory) + { + var files = SearchFiles(directory, "*", true); + var stats = new Dictionary(StringComparer.OrdinalIgnoreCase); + + foreach (var file in files) + { + var ext = Path.GetExtension(file); + if (string.IsNullOrEmpty(ext)) + ext = "(无扩展名)"; + + if (stats.ContainsKey(ext)) + stats[ext]++; + else + stats[ext] = 1; + } + + return stats; + } + } +} diff --git a/EasyTool.Core/IOCategory/FileSignatureUtil.cs b/EasyTool.Core/IOCategory/FileSignatureUtil.cs new file mode 100644 index 0000000..be72d91 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSignatureUtil.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 文件签名(魔数)检测工具类 + /// 通过文件头字节判断真实文件类型 + /// + public static class FileSignatureUtil + { + /// + /// 常见文件签名 + /// + private static readonly List Signatures = new() + { + // 图片 + new("jpg", "JPEG Image", new[] { "FF D8 FF" }, new[] { ".jpg", ".jpeg" }), + new("png", "PNG Image", new[] { "89 50 4E 47 0D 0A 1A 0A" }, new[] { ".png" }), + new("gif", "GIF Image", new[] { "47 49 46 38 37 61", "47 49 46 38 39 61" }, new[] { ".gif" }), + new("bmp", "BMP Image", new[] { "42 4D" }, new[] { ".bmp" }), + new("webp", "WebP Image", new[] { "52 49 46 46 ?? ?? ?? ?? 57 45 42 50" }, new[] { ".webp" }), + new("ico", "ICO Image", new[] { "00 00 01 00" }, new[] { ".ico" }), + new("svg", "SVG Image", new[] { "3C 3F 78 6D 6C", "3C 73 76 67" }, new[] { ".svg" }), + new("tiff", "TIFF Image", new[] { "49 49 2A 00", "4D 4D 00 2A" }, new[] { ".tiff", ".tif" }), + + // 文档 + new("pdf", "PDF Document", new[] { "25 50 44 46" }, new[] { ".pdf" }), + new("doc", "Word Document (old)", new[] { "D0 CF 11 E0 A1 B1 1A E1" }, new[] { ".doc", ".xls", ".ppt" }), + new("docx", "Word Document", new[] { "50 4B 03 04 14 00 06 00" }, new[] { ".docx", ".xlsx", ".pptx" }), + new("rtf", "RTF Document", new[] { "7B 5C 72 74 66 31" }, new[] { ".rtf" }), + + // 压缩 + new("zip", "ZIP Archive", new[] { "50 4B 03 04", "50 4B 05 06", "50 4B 07 08" }, new[] { ".zip" }), + new("rar", "RAR Archive", new[] { "52 61 72 21 1A 07" }, new[] { ".rar" }), + new("7z", "7-Zip Archive", new[] { "37 7A BC AF 27 1C" }, new[] { ".7z" }), + new("tar", "TAR Archive", new[] { "75 73 74 61 72" }, new[] { ".tar" }, 257), + new("gz", "GZIP Archive", new[] { "1F 8B" }, new[] { ".gz", ".gzip" }), + new("bz2", "BZIP2 Archive", new[] { "42 5A 68" }, new[] { ".bz2" }), + + // 音频 + new("mp3", "MP3 Audio", new[] { "FF FB", "FF FA", "FF F3", "FF F2", "49 44 33" }, new[] { ".mp3" }), + new("wav", "WAV Audio", new[] { "52 49 46 46 ?? ?? ?? ?? 57 41 56 45" }, new[] { ".wav" }), + new("flac", "FLAC Audio", new[] { "66 4C 61 43" }, new[] { ".flac" }), + new("m4a", "M4A Audio", new[] { "66 74 79 70 4D 34 41" }, new[] { ".m4a" }), + new("ogg", "OGG Audio", new[] { "4F 67 67 53" }, new[] { ".ogg" }), + + // 视频 + new("mp4", "MP4 Video", new[] { "66 74 79 70 69 73 6F 6D", "66 74 79 70 6D 70 34 32" }, new[] { ".mp4" }), + new("avi", "AVI Video", new[] { "52 49 46 46 ?? ?? ?? ?? 41 56 49 20" }, new[] { ".avi" }), + new("mkv", "MKV Video", new[] { "1A 45 DF A3" }, new[] { ".mkv", ".webm" }), + new("mov", "MOV Video", new[] { "66 74 79 70 71 74 20 20" }, new[] { ".mov" }), + new("flv", "FLV Video", new[] { "46 4C 56" }, new[] { ".flv" }), + new("wmv", "WMV Video", new[] { "30 26 B2 75 8E 66 CF 11" }, new[] { ".wmv", ".asf" }), + + // 可执行 + new("exe", "Windows Executable", new[] { "4D 5A" }, new[] { ".exe", ".dll" }), + new("elf", "Linux Executable", new[] { "7F 45 4C 46" }, new[] { "" }), + new("class", "Java Class", new[] { "CA FE BA BE" }, new[] { ".class" }), + new("dex", "Android DEX", new[] { "64 65 78 0A 30 33 35" }, new[] { ".dex" }), + new("apk", "Android APK", new[] { "50 4B 03 04" }, new[] { ".apk" }), + + // 其他 + new("sqlite", "SQLite Database", new[] { "53 51 4C 69 74 65 21" }, new[] { ".sqlite", ".db" }), + new("psd", "Photoshop Document", new[] { "38 42 50 53" }, new[] { ".psd" }), + new("ai", "Adobe Illustrator", new[] { "25 50 44 46" }, new[] { ".ai" }), + new("swf", "Flash SWF", new[] { "46 57 53", "43 57 53" }, new[] { ".swf" }), + new("torrent", "Torrent File", new[] { "64 38 3A 61 6E 6E 6F 75 6E 63 65" }, new[] { ".torrent" }), + }; + + /// + /// 检测文件类型 + /// + /// 文件路径 + /// 文件类型信息 + public static FileTypeInfo? Detect(string filePath) + { + if (!File.Exists(filePath)) + return null; + + using var stream = File.OpenRead(filePath); + return Detect(stream); + } + + /// + /// 检测文件类型 + /// + /// 文件流 + /// 文件类型信息 + public static FileTypeInfo? Detect(Stream stream) + { + if (stream == null || stream.Length == 0) + return null; + + var header = new byte[Math.Min(32, stream.Length)]; + var originalPosition = stream.Position; + stream.Position = 0; + stream.Read(header, 0, header.Length); + stream.Position = originalPosition; + + return DetectFromHeader(header); + } + + /// + /// 从字节数组检测文件类型 + /// + /// 文件字节数组 + /// 文件类型信息 + public static FileTypeInfo? DetectFromBytes(byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + return null; + + var header = new byte[Math.Min(32, bytes.Length)]; + Array.Copy(bytes, header, header.Length); + + return DetectFromHeader(header); + } + + /// + /// 从文件头检测文件类型 + /// + /// 文件头字节 + /// 文件类型信息 + public static FileTypeInfo? DetectFromHeader(byte[] header) + { + if (header == null || header.Length == 0) + return null; + + foreach (var signature in Signatures) + { + foreach (var pattern in signature.Patterns) + { + if (MatchesPattern(header, pattern, signature.Offset)) + { + return new FileTypeInfo + { + TypeId = signature.TypeId, + Description = signature.Description, + Extensions = signature.Extensions + }; + } + } + } + + return null; + } + + /// + /// 验证文件扩展名是否与实际内容匹配 + /// + /// 文件路径 + /// 是否匹配 + public static bool ValidateExtension(string filePath) + { + var detected = Detect(filePath); + if (detected == null) + return true; // 无法检测时默认通过 + + var extension = Path.GetExtension(filePath).ToLowerInvariant(); + return detected.Extensions.Contains(extension); + } + + /// + /// 获取文件的真实扩展名 + /// + /// 文件路径 + /// 扩展名(包含点号) + public static string? GetRealExtension(string filePath) + { + var detected = Detect(filePath); + return detected?.Extensions.FirstOrDefault(); + } + + /// + /// 检查文件是否为图片 + /// + /// 文件路径 + /// 是否为图片 + public static bool IsImage(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "jpg" or "png" or "gif" or "bmp" or "webp" or "ico" or "svg" or "tiff"; + } + + /// + /// 检查文件是否为视频 + /// + /// 文件路径 + /// 是否为视频 + public static bool IsVideo(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "mp4" or "avi" or "mkv" or "mov" or "flv" or "wmv"; + } + + /// + /// 检查文件是否为音频 + /// + /// 文件路径 + /// 是否为音频 + public static bool IsAudio(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "mp3" or "wav" or "flac" or "m4a" or "ogg"; + } + + /// + /// 检查文件是否为文档 + /// + /// 文件路径 + /// 是否为文档 + public static bool IsDocument(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "pdf" or "doc" or "docx" or "rtf"; + } + + /// + /// 检查文件是否为压缩包 + /// + /// 文件路径 + /// 是否为压缩包 + public static bool IsArchive(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "zip" or "rar" or "7z" or "tar" or "gz" or "bz2"; + } + + /// + /// 检查文件是否为可执行文件 + /// + /// 文件路径 + /// 是否为可执行文件 + public static bool IsExecutable(string filePath) + { + var detected = Detect(filePath); + return detected?.TypeId is "exe" or "elf" or "class" or "dex"; + } + + private static bool MatchesPattern(byte[] header, string pattern, int offset = 0) + { + var patternBytes = pattern.Split(' '); + var requiredLength = offset + patternBytes.Length; + + if (header.Length < requiredLength) + return false; + + for (int i = 0; i < patternBytes.Length; i++) + { + var patternByte = patternBytes[i]; + var headerByte = header[offset + i]; + + if (patternByte == "??") + continue; + + if (!byte.TryParse(patternByte, System.Globalization.NumberStyles.HexNumber, null, out var expectedByte)) + continue; + + if (headerByte != expectedByte) + return false; + } + + return true; + } + + #region 内部类 + + private class FileSignature + { + public string TypeId { get; } + public string Description { get; } + public string[] Patterns { get; } + public string[] Extensions { get; } + public int Offset { get; } + + public FileSignature(string typeId, string description, string[] patterns, string[] extensions, int offset = 0) + { + TypeId = typeId; + Description = description; + Patterns = patterns; + Extensions = extensions; + Offset = offset; + } + } + + #endregion + } + + /// + /// 文件类型信息 + /// + public class FileTypeInfo + { + /// + /// 类型标识 + /// + public string TypeId { get; set; } = string.Empty; + + /// + /// 类型描述 + /// + public string Description { get; set; } = string.Empty; + + /// + /// 可能的文件扩展名 + /// + public string[] Extensions { get; set; } = Array.Empty(); + + public override string ToString() => $"{TypeId} ({Description})"; + } +} diff --git a/EasyTool.Core/IOCategory/FileSystemExtension.cs b/EasyTool.Core/IOCategory/FileSystemExtension.cs new file mode 100644 index 0000000..7d214fa --- /dev/null +++ b/EasyTool.Core/IOCategory/FileSystemExtension.cs @@ -0,0 +1,464 @@ +using System; +using System.IO; +using System.Linq; +using EasyTool.MathCategory; + +namespace EasyTool.IOCategory +{ + /// + /// 文件系统扩展方法 + /// + public static class FileSystemExtension + { + #region FileInfo 扩展 + + /// + /// 获取文件大小(格式化字符串) + /// + public static string GetSizeFormatted(this FileInfo file) + { + if (file == null || !file.Exists) + return "0 B"; + + return file.Length.ToFileSize(); + } + + /// + /// 获取相对路径 + /// + /// 源文件 + /// 参考路径 + public static string GetRelativePath(this FileInfo file, string relativeTo) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + return GetRelativePath(file.FullName, relativeTo); + } + + /// + /// 获取文件的 MIME 类型 + /// + public static string GetMimeType(this FileInfo file) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + return file.Extension.GetMimeType(); + } + + /// + /// 判断文件是否为图片 + /// + public static bool IsImage(this FileInfo file) + { + if (file == null) + return false; + + string[] imageExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".ico", ".svg" }; + return imageExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为文档 + /// + public static bool IsDocument(this FileInfo file) + { + if (file == null) + return false; + + string[] docExtensions = { ".doc", ".docx", ".pdf", ".txt", ".rtf", ".odt", ".xls", ".xlsx", ".ppt", ".pptx" }; + return docExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为视频 + /// + public static bool IsVideo(this FileInfo file) + { + if (file == null) + return false; + + string[] videoExtensions = { ".mp4", ".avi", ".mov", ".wmv", ".flv", ".mkv", ".webm", ".m4v" }; + return videoExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否为音频 + /// + public static bool IsAudio(this FileInfo file) + { + if (file == null) + return false; + + string[] audioExtensions = { ".mp3", ".wav", ".flac", ".aac", ".ogg", ".wma", ".m4a" }; + return audioExtensions.Contains(file.Extension.ToLowerInvariant()); + } + + /// + /// 判断文件是否被锁定(正在使用) + /// + public static bool IsLocked(this FileInfo file) + { + if (file == null || !file.Exists) + return false; + + try + { + using (var stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + { + return false; + } + } + catch (IOException) + { + return true; + } + catch (UnauthorizedAccessException) + { + return true; + } + } + + /// + /// 安全删除文件(如果存在) + /// + public static bool DeleteIfExists(this FileInfo file) + { + if (file == null || !file.Exists) + return false; + + try + { + file.Delete(); + return true; + } + catch + { + return false; + } + } + + /// + /// 移动文件到指定目录 + /// + public static FileInfo MoveToDirectory(this FileInfo file, string targetDirectory) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + if (!Directory.Exists(targetDirectory)) + Directory.CreateDirectory(targetDirectory); + + string targetPath = Path.Combine(targetDirectory, file.Name); + file.MoveTo(targetPath); + return new FileInfo(targetPath); + } + + /// + /// 复制文件到指定目录 + /// + public static FileInfo CopyToDirectory(this FileInfo file, string targetDirectory, bool overwrite = false) + { + if (file == null) + throw new ArgumentNullException(nameof(file)); + + if (!Directory.Exists(targetDirectory)) + Directory.CreateDirectory(targetDirectory); + + string targetPath = Path.Combine(targetDirectory, file.Name); + file.CopyTo(targetPath, overwrite); + return new FileInfo(targetPath); + } + + + #endregion + + #region DirectoryInfo 扩展 + + /// + /// 获取目录的总大小(包含所有子目录) + /// + public static long GetTotalSize(this DirectoryInfo directory) + { + if (directory == null || !directory.Exists) + return 0; + + long size = 0; + + try + { + size += directory.GetFiles().Sum(f => f.Length); + size += directory.GetDirectories().Sum(d => GetTotalSize(d)); + } + catch (UnauthorizedAccessException) + { + // 忽略无权限访问的目录 + } + + return size; + } + + /// + /// 获取目录的总大小(格式化字符串) + /// + public static string GetTotalSizeFormatted(this DirectoryInfo directory) + { + return directory.GetTotalSize().ToFileSize(); + } + + /// + /// 获取目录中的所有文件(包含子目录) + /// + public static FileInfo[] GetAllFiles(this DirectoryInfo directory, string searchPattern = "*.*") + { + if (directory == null || !directory.Exists) + return Array.Empty(); + + try + { + var files = directory.GetFiles(searchPattern, SearchOption.AllDirectories); + return files; + } + catch (UnauthorizedAccessException) + { + return Array.Empty(); + } + } + + /// + /// 清空目录(删除所有文件和子目录) + /// + public static void Clear(this DirectoryInfo directory) + { + if (directory == null || !directory.Exists) + return; + + foreach (var file in directory.GetFiles()) + { + file.Delete(); + } + + foreach (var subDir in directory.GetDirectories()) + { + subDir.Delete(true); + } + } + + /// + /// 安全删除目录(如果存在) + /// + public static bool DeleteIfExists(this DirectoryInfo directory, bool recursive = false) + { + if (directory == null || !directory.Exists) + return false; + + try + { + directory.Delete(recursive); + return true; + } + catch + { + return false; + } + } + + /// + /// 确保目录存在,不存在则创建 + /// + public static DirectoryInfo EnsureExists(this DirectoryInfo directory) + { + if (directory == null) + throw new ArgumentNullException(nameof(directory)); + + if (!directory.Exists) + directory.Create(); + + return directory; + } + + /// + /// 复制目录到指定位置 + /// + public static DirectoryInfo CopyTo(this DirectoryInfo sourceDir, string targetPath) + { + if (sourceDir == null) + throw new ArgumentNullException(nameof(sourceDir)); + + var targetDir = Directory.CreateDirectory(targetPath); + + // 复制文件 + foreach (var file in sourceDir.GetFiles()) + { + string targetFilePath = Path.Combine(targetPath, file.Name); + file.CopyTo(targetFilePath, true); + } + + // 递归复制子目录 + foreach (var subDir in sourceDir.GetDirectories()) + { + string targetSubDirPath = Path.Combine(targetPath, subDir.Name); + subDir.CopyTo(targetSubDirPath); + } + + return targetDir; + } + + #endregion + + #region 路径扩展 + + /// + /// 获取相对路径 + /// + /// 绝对路径 + /// 参考路径 + public static string GetRelativePath(string absolutePath, string relativeTo) + { + if (string.IsNullOrEmpty(absolutePath)) + throw new ArgumentNullException(nameof(absolutePath)); + + if (string.IsNullOrEmpty(relativeTo)) + throw new ArgumentNullException(nameof(relativeTo)); + + absolutePath = Path.GetFullPath(absolutePath); + relativeTo = Path.GetFullPath(relativeTo); + + // 从 .NET Core 2.0 / .NET Standard 2.1 开始,可以使用 Path.GetRelativePath + // 这里提供一个兼容的实现 + var absolutePathParts = absolutePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var relativeToParts = relativeTo.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + int length = Math.Min(absolutePathParts.Length, relativeToParts.Length); + int lastCommonRoot = -1; + + for (int i = 0; i < length; i++) + { + if (string.Equals(absolutePathParts[i], relativeToParts[i], StringComparison.OrdinalIgnoreCase)) + { + lastCommonRoot = i; + } + else + { + break; + } + } + + if (lastCommonRoot == -1) + return absolutePath; + + var relativePath = new System.Text.StringBuilder(); + + // 添加 .. + for (int i = lastCommonRoot + 1; i < relativeToParts.Length; i++) + { + if (relativePath.Length > 0) + relativePath.Append(Path.DirectorySeparatorChar); + + relativePath.Append(".."); + } + + // 添加目标路径的剩余部分 + for (int i = lastCommonRoot + 1; i < absolutePathParts.Length; i++) + { + if (relativePath.Length > 0) + relativePath.Append(Path.DirectorySeparatorChar); + + relativePath.Append(absolutePathParts[i]); + } + + return relativePath.ToString(); + } + + /// + /// 确保路径以目录分隔符结尾 + /// + public static string EnsureEndsWithSeparator(this string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + char lastChar = path[path.Length - 1]; + if (lastChar != Path.DirectorySeparatorChar && lastChar != Path.AltDirectorySeparatorChar) + { + return path + Path.DirectorySeparatorChar; + } + + return path; + } + + #endregion + + #region 文件扩展名扩展 + + /// + /// 获取文件扩展名对应的 MIME 类型 + /// + public static string GetMimeType(this string extension) + { + if (string.IsNullOrEmpty(extension)) + return "application/octet-stream"; + + // 确保扩展名以 . 开头 + if (!extension.StartsWith(".")) + extension = "." + extension; + + return extension.ToLowerInvariant() switch + { + // 文本 + ".html" => "text/html", + ".htm" => "text/html", + ".css" => "text/css", + ".js" => "application/javascript", + ".json" => "application/json", + ".xml" => "application/xml", + ".txt" => "text/plain", + + // 图片 + ".jpg" => "image/jpeg", + ".jpeg" => "image/jpeg", + ".png" => "image/png", + ".gif" => "image/gif", + ".bmp" => "image/bmp", + ".webp" => "image/webp", + ".ico" => "image/x-icon", + ".svg" => "image/svg+xml", + + // 视频 + ".mp4" => "video/mp4", + ".avi" => "video/x-msvideo", + ".mov" => "video/quicktime", + ".wmv" => "video/x-ms-wmv", + ".flv" => "video/x-flv", + ".mkv" => "video/x-matroska", + ".webm" => "video/webm", + + // 音频 + ".mp3" => "audio/mpeg", + ".wav" => "audio/wav", + ".flac" => "audio/flac", + ".aac" => "audio/aac", + ".ogg" => "audio/ogg", + ".wma" => "audio/x-ms-wma", + ".m4a" => "audio/mp4", + + // 文档 + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".ppt" => "application/vnd.ms-powerpoint", + ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".zip" => "application/zip", + ".rar" => "application/x-rar-compressed", + ".7z" => "application/x-7z-compressed", + + _ => "application/octet-stream" + }; + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/FileTypeExtension.cs b/EasyTool.Core/IOCategory/FileTypeExtension.cs index 3216dda..4220f7a 100644 --- a/EasyTool.Core/IOCategory/FileTypeExtension.cs +++ b/EasyTool.Core/IOCategory/FileTypeExtension.cs @@ -3,13 +3,16 @@ using System.IO; using System.Text; -namespace EasyTool.Extension +namespace EasyTool.IOCategory { + /// + /// 文件类型扩展方法 + /// public static class FileTypeExtension { /// /// 文件流头部信息获得文件类型 - /// + /// /// 说明: /// 1、无法识别类型默认按照扩展名识别 /// 2、xls、doc、msi、ppt、vsd头信息无法区分,按照扩展名区分 @@ -17,6 +20,63 @@ public static class FileTypeExtension /// /// 文件 /// 类型,文件的扩展名,未找到为null - public static string GetType(this FileInfo file) => FileTypeUtil.GetType(file); + public static string GetFileType(this FileInfo file) + { + byte[] buffer = new byte[256]; + using (FileStream fs = file.OpenRead()) + { + int readLength = fs.Read(buffer, 0, buffer.Length); + if (readLength < buffer.Length) + { + // 处理读取不足的情况,虽然对于头部检测通常前几个字节就够了,但为了严谨性 + } + } + + string header = ""; + for (int i = 0; i < buffer.Length; i++) + { + header += buffer[i].ToString(); + } + + string? type = null; + switch (header) + { + case "255216": // jpg + type = ".jpg"; + break; + case "13780": // png + type = ".png"; + break; + case "7173": // gif + type = ".gif"; + break; + case "6677": // bmp + type = ".bmp"; + break; + case "7790": // exe dll + type = ".exe"; + break; + case "6063": // xml + type = ".xml"; + break; + case "6033": // htm html + type = ".html"; + break; + case "4742": // js + type = ".js"; + break; + case "5144": // txt + type = ".txt"; + break; + default: + case "8297": // rar + case "8075": // zip + case "D0CF11E0": // doc xls ppt vsd + type = file.Extension; + break; + } + + return type; + } } } diff --git a/EasyTool.Core/IOCategory/FileTypeUtil.cs b/EasyTool.Core/IOCategory/FileTypeUtil.cs deleted file mode 100644 index d2fb426..0000000 --- a/EasyTool.Core/IOCategory/FileTypeUtil.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; - -namespace EasyTool -{ - /// - /// 文件类型判断工具类 - /// - public class FileTypeUtil - { - /// - /// 文件流头部信息获得文件类型 - /// - /// 说明: - /// 1、无法识别类型默认按照扩展名识别 - /// 2、xls、doc、msi、ppt、vsd头信息无法区分,按照扩展名区分 - /// 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分 - /// - /// 文件 - /// 类型,文件的扩展名,未找到为null - public static string GetType(FileInfo file) - { - byte[] buffer = new byte[256]; - using (FileStream fs = file.OpenRead()) - { - if (fs.Length >= 256) - fs.Read(buffer, 0, 256); - else - fs.Read(buffer, 0, (int)fs.Length); - } - - string header = ""; - for (int i = 0; i < buffer.Length; i++) - { - header += buffer[i].ToString(); - } - - string type = null; - switch (header) - { - case "255216": // jpg - type = ".jpg"; - break; - case "13780": // png - type = ".png"; - break; - case "7173": // gif - type = ".gif"; - break; - case "6677": // bmp - type = ".bmp"; - break; - case "7790": // exe dll - type = ".exe"; - break; - case "6063": // xml - type = ".xml"; - break; - case "6033": // htm html - type = ".html"; - break; - case "4742": // js - type = ".js"; - break; - case "5144": // txt - type = ".txt"; - break; - default: - case "8297": // rar - case "8075": // zip - case "D0CF11E0": // doc xls ppt vsd - type = file.Extension; - break; - } - - return type; - } - } -} diff --git a/EasyTool.Core/IOCategory/FileUtil.cs b/EasyTool.Core/IOCategory/FileUtil.cs index c46db33..62e271c 100644 --- a/EasyTool.Core/IOCategory/FileUtil.cs +++ b/EasyTool.Core/IOCategory/FileUtil.cs @@ -1,57 +1,24 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Text; using System.Web; +using EasyTool.IOCategory; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件操作类 /// - public class FileUtil + public static class FileUtil { /// /// 判断当前操作系统是否为 Windows /// - public static bool IsWindows() - { - // 判断当前操作系统的 PlatformID 是否为 Win32S、Win32Windows、Win32NT 或 WinCE - return Environment.OSVersion.Platform == PlatformID.Win32S - || Environment.OSVersion.Platform == PlatformID.Win32Windows - || Environment.OSVersion.Platform == PlatformID.Win32NT - || Environment.OSVersion.Platform == PlatformID.WinCE; - } - - /// - /// 判断当前操作系统是否为 Unix - /// - public static bool IsUnix() - { - // 判断当前操作系统的 PlatformID 是否为 Unix - return Environment.OSVersion.Platform == PlatformID.Unix; - } - - /// - /// 判断当前操作系统是否为 Xbox - /// - public static bool IsXbox() - { - // 判断当前操作系统的 PlatformID 是否为 Xbox - return Environment.OSVersion.Platform == PlatformID.Xbox; - } - - /// - /// 判断当前操作系统是否为 macOS - /// - public static bool IsMacOSX() - { - // 判断当前操作系统的 PlatformID 是否为 macOSX - return Environment.OSVersion.Platform == PlatformID.MacOSX; - } /// /// 判断文件或目录是否为空 @@ -61,19 +28,24 @@ public static bool IsMacOSX() /// /// 文件或目录的路径 /// 是否为空 + /// 路径不存在时抛出异常 public static bool IsEmpty(string path) { // 判断是否为目录 if (Directory.Exists(path)) { - // 如果是目录,遍历目录下的所有文件,判断是否有文件 - return Directory.GetFiles(path).Length>0; + // 如果是目录,判断目录下是否有文件(没有文件则为空) + return Directory.GetFiles(path).Length == 0; } - else + else if (File.Exists(path)) { // 如果是文件,判断文件大小是否为 0 return new FileInfo(path).Length == 0; } + else + { + throw new FileNotFoundException($"路径 {path} 不存在"); + } } @@ -301,22 +273,6 @@ public static FileInfo Touch(string path) } - /// - /// 拷贝文件 - /// - /// 源文件路径 - /// 目标文件路径 - public static void Cp(string src, string dest) - { - try - { - File.Copy(src, dest); - } - catch (Exception ex) - { - throw new Exception($"拷贝文件 {src} 到 {dest} 失败:{ex.Message}", ex); - } - } /// /// 复制文件或目录 @@ -395,22 +351,6 @@ public static bool Copy(string src, string dest, bool isOverride) } } - /// - /// 移动文件或重命名文件 - /// - /// 源文件路径 - /// 目标文件路径 - public static void Mv(string src, string dest) - { - try - { - File.Move(src, dest); - } - catch (Exception ex) - { - throw new Exception($"移动/重命名文件 {src} 到 {dest} 失败:{ex.Message}", ex); - } - } /// /// 移动文件或者目录 @@ -534,20 +474,6 @@ public static FileInfo Rename(FileInfo file, string newName, bool isRetainExt, b } } - /// - /// 获取绝对路径 - /// - /// 相对路径 - /// 绝对路径 - public static string GetAbsolutePath(string path) - { - if (!Path.IsPathRooted(path)) - { - path = Path.Combine(Directory.GetCurrentDirectory(), path); - } - - return Path.GetFullPath(path); - } /// /// 判断给定路径是否是绝对路径 @@ -763,68 +689,7 @@ public static string SubPath(string dirPath, string filePath) return filePath.Substring(startIndex); } - /// - /// 删除文件 - /// - /// 文件路径 - public static void Rm(string path) - { - try - { - File.Delete(path); - } - catch (Exception ex) - { - throw new Exception($"删除文件 {path} 失败:{ex.Message}"); - } - } - - /// - /// 创建目录 - /// - /// 目录路径 - public static void Mkdir(string path) - { - try - { - Directory.CreateDirectory(path); - } - catch (Exception ex) - { - throw new Exception($"创建目录 {path} 失败:{ex.Message}", ex); - } - } - /// - /// 删除目录 - /// - /// 目录路径 - public static void Rmdir(string path) - { - try - { - Directory.Delete(path); - Console.WriteLine($"目录 {path} 已成功删除"); - } - catch (Exception ex) - { - throw new Exception($"删除目录 {path} 失败:{ex.Message}",ex); - } - } - - /// - /// 获取文件名 - /// - /// 文件 - /// 文件名 - public static string GetFileName(FileInfo file) - { - if (file == null) - { - return null; - } - return file.Name; - } /// /// 获取文件名 @@ -884,7 +749,7 @@ public static string GetFileSuffix(string filePath) /// /// 文件 /// 文件名 - public static string GetFilePrefix(FileInfo file) + public static string? GetFilePrefix(FileInfo file) { if (file == null) { @@ -921,9 +786,10 @@ public static string GetFilePrefix(string filePath) /// /// 文件 /// 类型,文件的扩展名,未找到为null - public static string GetType(FileInfo file) + public static string? GetType(FileInfo file) { - return FileTypeUtil.GetType(file); + // 通过文件头部获取类型 + return file.GetFileType(); } /// @@ -954,7 +820,7 @@ public static Stream GetInputStream(string path) /// 文件 /// 编码格式,默认为UTF-8 /// BOM输入流 - public static StreamReader GetBOMInputStream(FileInfo file, Encoding encoding = null) + public static StreamReader GetBOMInputStream(FileInfo file, Encoding? encoding = null) { if (encoding == null) { @@ -998,7 +864,7 @@ public static StreamReader GetBOMInputStream(FileInfo file, Encoding encoding = /// 文件 /// 编码格式,默认为UTF-8 /// 文件读取流 - public static StreamReader GetReader(FileInfo file, Encoding encoding = null) + public static StreamReader GetReader(FileInfo file, Encoding? encoding = null) { if (encoding == null) { @@ -1078,7 +944,7 @@ public static byte[] ReadBytes(string path) /// 文件 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(FileInfo file, Encoding encoding = null) + public static string ReadString(FileInfo file, Encoding? encoding = null) { if (encoding == null) @@ -1101,7 +967,7 @@ public static string ReadString(FileInfo file, Encoding encoding = null) /// 文件路径 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(string path, Encoding encoding = null) + public static string ReadString(string path, Encoding? encoding = null) { return ReadString(new FileInfo(path), encoding); // 直接调用另一个重载方法 } @@ -1113,7 +979,7 @@ public static string ReadString(string path, Encoding encoding = null) /// 网络文件地址 /// 编码格式,默认为UTF-8 /// 内容 - public static string ReadString(Uri url, Encoding encoding = null) + public static string ReadString(Uri url, Encoding? encoding = null) { // 如果未指定编码格式,则默认为UTF-8 if (encoding == null) @@ -1124,11 +990,12 @@ public static string ReadString(Uri url, Encoding encoding = null) string result; try { - // 创建WebClient对象 - using (WebClient client = new WebClient()) + // 创建HttpClient对象 + using (HttpClient client = new HttpClient()) { // 下载指定地址的文件,并转换为字节数组 - byte[] data = client.DownloadData(url); + // 注意:为了保持同步方法签名,这里使用了同步等待,这在某些上下文中可能会导致死锁 + byte[] data = client.GetByteArrayAsync(url).GetAwaiter().GetResult(); // 将字节数组转换为字符串,并使用指定编码格式解码 result = encoding.GetString(data); } @@ -1142,25 +1009,6 @@ public static string ReadString(Uri url, Encoding encoding = null) return result; } - /// - /// 从文件中读取每一行数据 - /// - /// 文件路径 - /// 编码格式,默认为UTF-8 - /// - public static string[] ReadAllLines(string path, Encoding encoding = null) - { - // 如果未指定编码格式,则默认为 UTF-8 - if (encoding == null) - { - encoding = Encoding.UTF8; - } - - // 读取文件所有行数据 - string[] lines = File.ReadAllLines(path, encoding); - - return lines; - } /// @@ -1190,14 +1038,6 @@ public static Stream GetOutputStream(string path) } - /// - /// 获取当前系统的换行分隔符 - /// - /// 换行分隔符 - public static string GetLineSeparator() - { - return Environment.NewLine; - } /// /// 将string写入文件,覆盖模式 @@ -1206,7 +1046,7 @@ public static string GetLineSeparator() /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo WriteString(string content, string path, Encoding encoding = null) + public static FileInfo WriteString(string content, string path, Encoding? encoding = null) { if (encoding == null) { @@ -1229,7 +1069,7 @@ public static FileInfo WriteString(string content, string path, Encoding encodin /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo AppendString(string content, string path, Encoding encoding = null) + public static FileInfo AppendString(string content, string path, Encoding? encoding = null) { if (encoding == null) { @@ -1249,7 +1089,7 @@ public static FileInfo AppendString(string content, string path, Encoding encodi /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo WriteLines(List list, string path, Encoding encoding = null) + public static FileInfo WriteLines(List list, string path, Encoding? encoding = null) { if (encoding == null) { @@ -1274,7 +1114,7 @@ public static FileInfo WriteLines(List list, string path, Encoding encod /// 文件路径 /// 编码格式,默认为UTF-8 /// 文件信息 - public static FileInfo AppendLines(List list, string path, Encoding encoding = null) + public static FileInfo AppendLines(List list, string path, Encoding? encoding = null) { if (encoding == null) { diff --git a/EasyTool.Core/IOCategory/FileWatcher.cs b/EasyTool.Core/IOCategory/FileWatcher.cs new file mode 100644 index 0000000..0cbe5b0 --- /dev/null +++ b/EasyTool.Core/IOCategory/FileWatcher.cs @@ -0,0 +1,386 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// 文件监视器 + /// 提供文件变更监视功能 + /// + public class FileWatcher : IDisposable + { + private readonly FileSystemWatcher _watcher; + private readonly Dictionary _lastWriteTimes = new(); + private readonly object _lock = new(); + private int _debounceMilliseconds = 100; + + /// + /// 文件变更事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 错误事件 + /// + public event EventHandler? Error; + + /// + /// 监视路径 + /// + public string Path { get; } + + /// + /// 监视筛选器 + /// + public string Filter + { + get => _watcher.Filter; + set => _watcher.Filter = value; + } + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories + { + get => _watcher.IncludeSubdirectories; + set => _watcher.IncludeSubdirectories = value; + } + + /// + /// 防抖时间(毫秒) + /// + public int DebounceMilliseconds + { + get => _debounceMilliseconds; + set => _debounceMilliseconds = Math.Max(0, value); + } + + /// + /// 是否正在监视 + /// + public bool IsWatching => _watcher.EnableRaisingEvents; + + /// + /// 创建文件监视器 + /// + /// 监视路径 + public FileWatcher(string path) + { + if (!Directory.Exists(path)) + throw new DirectoryNotFoundException($"目录不存在: {path}"); + + Path = path; + _watcher = new FileSystemWatcher(path) + { + NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | + NotifyFilters.LastWrite | NotifyFilters.Size + }; + + _watcher.Changed += OnChanged; + _watcher.Created += OnCreated; + _watcher.Deleted += OnDeleted; + _watcher.Renamed += OnRenamed; + _watcher.Error += OnError; + } + + /// + /// 创建文件监视器 + /// + /// 监视路径 + /// 文件筛选器 + public FileWatcher(string path, string filter) : this(path) + { + Filter = filter; + } + + private void OnChanged(object sender, FileSystemEventArgs e) + { + // 防抖处理 + lock (_lock) + { + var now = DateTime.UtcNow; + if (_lastWriteTimes.TryGetValue(e.FullPath, out var lastWrite)) + { + if ((now - lastWrite).TotalMilliseconds < _debounceMilliseconds) + return; + } + _lastWriteTimes[e.FullPath] = now; + } + + FileChanged?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Changed)); + } + + private void OnCreated(object sender, FileSystemEventArgs e) + { + FileCreated?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Created)); + } + + private void OnDeleted(object sender, FileSystemEventArgs e) + { + lock (_lock) + { + _lastWriteTimes.Remove(e.FullPath); + } + FileDeleted?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name!, ChangeType.Deleted)); + } + + private void OnRenamed(object sender, RenamedEventArgs e) + { + lock (_lock) + { + _lastWriteTimes.Remove(e.OldFullPath); + } + FileRenamed?.Invoke(this, new FileRenamedEventArgs(e.FullPath, e.Name!, e.OldFullPath, e.OldName!)); + } + + private void OnError(object sender, ErrorEventArgs e) + { + Error?.Invoke(this, e); + } + + /// + /// 开始监视 + /// + public void Start() + { + _watcher.EnableRaisingEvents = true; + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.EnableRaisingEvents = false; + } + + /// + /// 释放资源 + /// + public void Dispose() + { + _watcher.EnableRaisingEvents = false; + _watcher.Changed -= OnChanged; + _watcher.Created -= OnCreated; + _watcher.Deleted -= OnDeleted; + _watcher.Renamed -= OnRenamed; + _watcher.Error -= OnError; + _watcher.Dispose(); + } + } + + /// + /// 文件变更类型 + /// + public enum ChangeType + { + /// + /// 已更改 + /// + Changed, + + /// + /// 已创建 + /// + Created, + + /// + /// 已删除 + /// + Deleted, + + /// + /// 已重命名 + /// + Renamed + } + + /// + /// 文件变更事件参数 + /// + public class FileChangedEventArgs : EventArgs + { + /// + /// 完整路径 + /// + public string FullPath { get; } + + /// + /// 文件名 + /// + public string Name { get; } + + /// + /// 变更类型 + /// + public ChangeType ChangeType { get; } + + /// + /// 创建事件参数 + /// + public FileChangedEventArgs(string fullPath, string name, ChangeType changeType) + { + FullPath = fullPath; + Name = name; + ChangeType = changeType; + } + } + + /// + /// 文件重命名事件参数 + /// + public class FileRenamedEventArgs : EventArgs + { + /// + /// 新完整路径 + /// + public string FullPath { get; } + + /// + /// 新文件名 + /// + public string Name { get; } + + /// + /// 旧完整路径 + /// + public string OldFullPath { get; } + + /// + /// 旧文件名 + /// + public string OldName { get; } + + /// + /// 创建事件参数 + /// + public FileRenamedEventArgs(string fullPath, string name, string oldFullPath, string oldName) + { + FullPath = fullPath; + Name = name; + OldFullPath = oldFullPath; + OldName = oldName; + } + } + + /// + /// 目录监视器 + /// + public class DirectoryWatcher : IDisposable + { + private readonly List _watchers = new(); + + /// + /// 文件变更事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 创建目录监视器 + /// + /// 监视路径列表 + public DirectoryWatcher(params string[] paths) + { + foreach (var path in paths) + { + AddPath(path); + } + } + + /// + /// 添加监视路径 + /// + /// 路径 + public void AddPath(string path) + { + var watcher = new FileWatcher(path); + watcher.FileChanged += (s, e) => FileChanged?.Invoke(s, e); + watcher.FileCreated += (s, e) => FileCreated?.Invoke(s, e); + watcher.FileDeleted += (s, e) => FileDeleted?.Invoke(s, e); + watcher.FileRenamed += (s, e) => FileRenamed?.Invoke(s, e); + _watchers.Add(watcher); + } + + /// + /// 添加监视路径 + /// + /// 路径 + /// 筛选器 + public void AddPath(string path, string filter) + { + var watcher = new FileWatcher(path, filter); + watcher.FileChanged += (s, e) => FileChanged?.Invoke(s, e); + watcher.FileCreated += (s, e) => FileCreated?.Invoke(s, e); + watcher.FileDeleted += (s, e) => FileDeleted?.Invoke(s, e); + watcher.FileRenamed += (s, e) => FileRenamed?.Invoke(s, e); + _watchers.Add(watcher); + } + + /// + /// 开始监视所有路径 + /// + public void StartAll() + { + foreach (var watcher in _watchers) + { + watcher.Start(); + } + } + + /// + /// 停止监视所有路径 + /// + public void StopAll() + { + foreach (var watcher in _watchers) + { + watcher.Stop(); + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + foreach (var watcher in _watchers) + { + watcher.Dispose(); + } + _watchers.Clear(); + } + } +} diff --git a/EasyTool.Core/IOCategory/FileWatcherEx.cs b/EasyTool.Core/IOCategory/FileWatcherEx.cs new file mode 100644 index 0000000..b4760eb --- /dev/null +++ b/EasyTool.Core/IOCategory/FileWatcherEx.cs @@ -0,0 +1,298 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 文件监听器增强版 + /// + public class FileWatcherEx : IDisposable + { + private readonly FileSystemWatcher _watcher; + private readonly Dictionary _lastEvents = new(); + private readonly TimeSpan _debounceInterval; + private readonly object _lock = new(); + + /// + /// 文件创建事件 + /// + public event EventHandler? FileCreated; + + /// + /// 文件删除事件 + /// + public event EventHandler? FileDeleted; + + /// + /// 文件修改事件 + /// + public event EventHandler? FileChanged; + + /// + /// 文件重命名事件 + /// + public event EventHandler? FileRenamed; + + /// + /// 错误事件 + /// + public event EventHandler? Error; + + /// + /// 监视的目录路径 + /// + public string Path => _watcher.Path; + + /// + /// 监视的文件过滤器 + /// + public string Filter => _watcher.Filter; + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories => _watcher.IncludeSubdirectories; + + /// + /// 创建文件监听器 + /// + /// 监视目录 + /// 文件过滤器 + /// 包含子目录 + /// 防抖间隔 + public FileWatcherEx(string path, string filter = "*.*", bool includeSubdirectories = true, TimeSpan? debounceInterval = null) + { + _debounceInterval = debounceInterval ?? TimeSpan.FromMilliseconds(100); + _watcher = new FileSystemWatcher + { + Path = path, + Filter = filter, + IncludeSubdirectories = includeSubdirectories, + NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | + NotifyFilters.LastWrite | NotifyFilters.Size + }; + + _watcher.Created += OnCreated; + _watcher.Deleted += OnDeleted; + _watcher.Changed += OnChanged; + _watcher.Renamed += OnRenamed; + _watcher.Error += OnError; + } + + /// + /// 开始监视 + /// + public void Start() + { + _watcher.EnableRaisingEvents = true; + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.EnableRaisingEvents = false; + } + + private void OnCreated(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Created")) + { + FileCreated?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Created)); + } + } + + private void OnDeleted(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Deleted")) + { + FileDeleted?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Deleted)); + } + } + + private void OnChanged(object sender, FileSystemEventArgs e) + { + if (ShouldProcess(e.FullPath, "Changed")) + { + FileChanged?.Invoke(this, new FileChangedEventArgs(e.FullPath, e.Name ?? string.Empty, ChangeType.Changed)); + } + } + + private void OnRenamed(object sender, RenamedEventArgs e) + { + if (ShouldProcess(e.FullPath, "Renamed")) + { + FileRenamed?.Invoke(this, new FileRenamedEventArgs(e.FullPath, e.Name ?? string.Empty, e.OldFullPath, e.OldName ?? string.Empty)); + } + } + + private void OnError(object sender, ErrorEventArgs e) + { + Error?.Invoke(this, e); + } + + private bool ShouldProcess(string path, string eventType) + { + lock (_lock) + { + var key = $"{path}|{eventType}"; + var now = DateTime.UtcNow; + + if (_lastEvents.TryGetValue(key, out var lastTime)) + { + if (now - lastTime < _debounceInterval) + { + return false; + } + } + + _lastEvents[key] = now; + return true; + } + } + + public void Dispose() + { + _watcher?.Dispose(); + } + } + + /// + /// 目录监视器 + /// + public class DirectoryMonitor : IDisposable + { + private readonly string _path; + private readonly FileWatcherEx _watcher; + private readonly Dictionary _files = new(); + private readonly object _lock = new(); + + /// + /// 文件添加事件 + /// + public event EventHandler? FileAdded; + + /// + /// 文件移除事件 + /// + public event EventHandler? FileRemoved; + + /// + /// 文件修改事件 + /// + public event EventHandler? FileModified; + + /// + /// 当前文件列表 + /// + public IReadOnlyList CurrentFiles + { + get + { + lock (_lock) + { + return _files.Values.ToList().AsReadOnly(); + } + } + } + + /// + /// 创建目录监视器 + /// + public DirectoryMonitor(string path, string filter = "*.*", bool includeSubdirectories = true) + { + _path = path; + _watcher = new FileWatcherEx(path, filter, includeSubdirectories); + _watcher.FileCreated += OnFileCreated; + _watcher.FileDeleted += OnFileDeleted; + _watcher.FileChanged += OnFileChanged; + } + + /// + /// 开始监视 + /// + public void Start() + { + // 初始化现有文件 + InitializeFiles(); + _watcher.Start(); + } + + /// + /// 停止监视 + /// + public void Stop() + { + _watcher.Stop(); + } + + private void InitializeFiles() + { + lock (_lock) + { + _files.Clear(); + + if (Directory.Exists(_path)) + { + foreach (var file in Directory.GetFiles(_path, "*", SearchOption.AllDirectories)) + { + var info = new FileInfo(file); + _files[file] = info; + } + } + } + } + + private void OnFileCreated(object? sender, FileChangedEventArgs e) + { + if (File.Exists(e.FullPath)) + { + var info = new FileInfo(e.FullPath); + lock (_lock) + { + _files[e.FullPath] = info; + } + FileAdded?.Invoke(this, info); + } + } + + private void OnFileDeleted(object? sender, FileChangedEventArgs e) + { + FileInfo? info; + lock (_lock) + { + if (_files.TryGetValue(e.FullPath, out info)) + { + _files.Remove(e.FullPath); + } + } + + if (info != null) + { + FileRemoved?.Invoke(this, info); + } + } + + private void OnFileChanged(object? sender, FileChangedEventArgs e) + { + if (File.Exists(e.FullPath)) + { + var info = new FileInfo(e.FullPath); + lock (_lock) + { + _files[e.FullPath] = info; + } + FileModified?.Invoke(this, info); + } + } + + public void Dispose() + { + _watcher?.Dispose(); + } + } +} diff --git a/EasyTool.Core/IOCategory/ImageMetadataUtil.cs b/EasyTool.Core/IOCategory/ImageMetadataUtil.cs new file mode 100644 index 0000000..bfa2471 --- /dev/null +++ b/EasyTool.Core/IOCategory/ImageMetadataUtil.cs @@ -0,0 +1,346 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.IOCategory +{ + /// + /// 图片元数据工具类 + /// + public static class ImageMetadataUtil + { + /// + /// 读取图片EXIF信息 + /// + public static ExifData ReadExif(string imagePath) + { + var exif = new ExifData(); + + try + { + using var image = System.Drawing.Image.FromFile(imagePath); + var propertyItems = image.PropertyItems; + + foreach (var item in propertyItems) + { + var value = ParsePropertyItemValue(item); + var tagName = GetPropertyName(item.Id); + + switch (item.Id) + { + case 0x010F: // 制造商 + exif.Make = value; + break; + case 0x0110: // 型号 + exif.Model = value; + break; + case 0x0112: // 方向 + exif.Orientation = ParseOrientation(value); + break; + case 0x011A: // X分辨率 + case 0x011B: // Y分辨率 + break; + case 0x0128: // 分辨率单位 + break; + case 0x0131: // 软件 + exif.Software = value; + break; + case 0x0132: // 日期时间 + exif.DateTime = ParseDateTime(value); + break; + case 0x8769: // Exif IFD + break; + case 0x8827: // ISO速度 + exif.ISO = ParseInt(value); + break; + case 0x9003: // 原始日期时间 + exif.DateTimeOriginal = ParseDateTime(value); + break; + case 0x9004: // 数字化日期时间 + exif.DateTimeDigitized = ParseDateTime(value); + break; + case 0x920A: // 焦距 + exif.FocalLength = ParseRational(value); + break; + case 0x9207: // 光圈值 + break; + case 0x829A: // 曝光时间 + exif.ExposureTime = ParseRational(value); + break; + case 0x829D: // F值 + exif.FNumber = ParseRational(value); + break; + case 0x8825: // GPS信息 + break; + case 0xA002: // 图像宽度 + exif.ExifImageWidth = ParseInt(value); + break; + case 0xA003: // 图像高度 + exif.ExifImageHeight = ParseInt(value); + break; + case 0xA402: // 曝光模式 + break; + case 0xA403: // 白平衡 + break; + case 0xA406: // 场景拍摄类型 + break; + case 0xA420: // 图像唯一ID + exif.ImageUniqueID = value; + break; + } + + exif.AllProperties[tagName] = value; + } + } + catch + { + } + + return exif; + } + + /// + /// 移除EXIF信息 + /// + public static bool RemoveExif(string sourcePath, string destinationPath) + { + try + { + using var image = System.Drawing.Image.FromFile(sourcePath); + + // 创建没有EXIF的新图像 + using var newImage = new System.Drawing.Bitmap(image); + newImage.Save(destinationPath); + return true; + } + catch + { + return false; + } + } + + private static string ParsePropertyItemValue(System.Drawing.Imaging.PropertyItem item) + { + try + { + switch (item.Type) + { + case 1: // Byte + return BitConverter.ToString(item.Value).Replace("-", " "); + case 2: // ASCII + return System.Text.Encoding.ASCII.GetString(item.Value).TrimEnd('\0'); + case 3: // Short + return BitConverter.ToUInt16(item.Value, 0).ToString(); + case 4: // Long + return BitConverter.ToUInt32(item.Value, 0).ToString(); + case 5: // Rational + return ParseRational(item.Value).ToString(); + case 7: // Undefined + return BitConverter.ToString(item.Value).Replace("-", " "); + case 9: // SLong + return BitConverter.ToInt32(item.Value, 0).ToString(); + case 10: // SRational + return ParseRational(item.Value).ToString(); + default: + return BitConverter.ToString(item.Value); + } + } + catch + { + return ""; + } + } + + private static double ParseRational(byte[] value) + { + if (value.Length < 8) return 0; + var numerator = BitConverter.ToUInt32(value, 0); + var denominator = BitConverter.ToUInt32(value, 4); + return denominator != 0 ? (double)numerator / denominator : 0; + } + + private static double ParseRational(string value) + { + if (string.IsNullOrEmpty(value)) return 0; + if (double.TryParse(value, out var result)) return result; + return 0; + } + + private static int ParseInt(string value) + { + return int.TryParse(value, out var result) ? result : 0; + } + + private static DateTime? ParseDateTime(string value) + { + if (string.IsNullOrEmpty(value)) return null; + // EXIF日期格式: "yyyy:MM:dd HH:mm:ss" + if (DateTime.TryParseExact(value, "yyyy:MM:dd HH:mm:ss", + System.Globalization.CultureInfo.InvariantCulture, + System.Globalization.DateTimeStyles.None, out var result)) + { + return result; + } + return null; + } + + private static int ParseOrientation(string value) + { + return int.TryParse(value, out var result) ? result : 1; + } + + private static string GetPropertyName(int id) + { + return id switch + { + 0x0100 => "ImageWidth", + 0x0101 => "ImageLength", + 0x0102 => "BitsPerSample", + 0x0103 => "Compression", + 0x0106 => "PhotometricInterpretation", + 0x010E => "ImageDescription", + 0x010F => "Make", + 0x0110 => "Model", + 0x0111 => "StripOffsets", + 0x0112 => "Orientation", + 0x0115 => "SamplesPerPixel", + 0x0116 => "RowsPerStrip", + 0x0117 => "StripByteCounts", + 0x011A => "XResolution", + 0x011B => "YResolution", + 0x0128 => "ResolutionUnit", + 0x0131 => "Software", + 0x0132 => "DateTime", + 0x8769 => "ExifIFDPointer", + 0x8827 => "ISOSpeedRatings", + 0x9003 => "DateTimeOriginal", + 0x9004 => "DateTimeDigitized", + 0x920A => "FocalLength", + 0x829A => "ExposureTime", + 0x829D => "FNumber", + 0xA002 => "ExifImageWidth", + 0xA003 => "ExifImageHeight", + _ => $"0x{id:X4}" + }; + } + } + + /// + /// EXIF数据 + /// + public class ExifData + { + /// + /// 制造商 + /// + public string Make { get; set; } = ""; + + /// + /// 型号 + /// + public string Model { get; set; } = ""; + + /// + /// 软件 + /// + public string Software { get; set; } = ""; + + /// + /// 方向(1-8) + /// + public int Orientation { get; set; } = 1; + + /// + /// 日期时间 + /// + public DateTime? DateTime { get; set; } + + /// + /// 原始日期时间 + /// + public DateTime? DateTimeOriginal { get; set; } + + /// + /// 数字化日期时间 + /// + public DateTime? DateTimeDigitized { get; set; } + + /// + /// ISO感光度 + /// + public int ISO { get; set; } + + /// + /// 焦距 + /// + public double FocalLength { get; set; } + + /// + /// 曝光时间 + /// + public double ExposureTime { get; set; } + + /// + /// 光圈值 + /// + public double FNumber { get; set; } + + /// + /// EXIF图像宽度 + /// + public int ExifImageWidth { get; set; } + + /// + /// EXIF图像高度 + /// + public int ExifImageHeight { get; set; } + + /// + /// 图像唯一ID + /// + public string ImageUniqueID { get; set; } = ""; + + /// + /// 所有属性 + /// + public Dictionary AllProperties { get; } = new(); + + /// + /// 获取方向描述 + /// + public string OrientationDescription => Orientation switch + { + 1 => "正常", + 2 => "水平翻转", + 3 => "旋转180度", + 4 => "垂直翻转", + 5 => "逆时针90度+水平翻转", + 6 => "顺时针90度", + 7 => "顺时针90度+水平翻转", + 8 => "逆时针90度", + _ => "未知" + }; + + /// + /// 曝光时间显示 + /// + public string ExposureTimeDisplay + { + get + { + if (ExposureTime >= 1) + return $"{ExposureTime:F1}s"; + return $"1/{(int)(1 / ExposureTime)}s"; + } + } + + /// + /// 光圈显示 + /// + public string FNumberDisplay => FNumber > 0 ? $"f/{FNumber:F1}" : ""; + + /// + /// 焦距显示 + /// + public string FocalLengthDisplay => FocalLength > 0 ? $"{FocalLength:F1}mm" : ""; + } +} diff --git a/EasyTool.Core/IOCategory/IniUtil.cs b/EasyTool.Core/IOCategory/IniUtil.cs new file mode 100644 index 0000000..9142dba --- /dev/null +++ b/EasyTool.Core/IOCategory/IniUtil.cs @@ -0,0 +1,328 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// INI 文件工具类 + /// 提供 INI 配置文件的读写功能 + /// + public static class IniUtil + { + /// + /// 读取 INI 文件 + /// + public static IniFile Read(string filePath) + { + var ini = new IniFile(); + ini.Load(filePath); + return ini; + } + + /// + /// 读取 INI 文件中的值 + /// + public static string GetValue(string filePath, string section, string key, string defaultValue = "") + { + var ini = Read(filePath); + return ini.GetValue(section, key, defaultValue); + } + + /// + /// 写入值到 INI 文件 + /// + public static void SetValue(string filePath, string section, string key, string value) + { + var ini = Read(filePath); + ini.SetValue(section, key, value); + ini.Save(filePath); + } + + /// + /// 创建空的 INI 文件对象 + /// + public static IniFile Create() + { + return new IniFile(); + } + } + + /// + /// INI 文件对象 + /// + public class IniFile + { + private readonly Dictionary> _sections; + private readonly List _sectionOrder; + private string _commentPrefix = ";"; + + /// + /// 注释前缀 + /// + public string CommentPrefix + { + get => _commentPrefix; + set => _commentPrefix = value ?? ";"; + } + + /// + /// 节名称列表 + /// + public IEnumerable Sections => _sectionOrder; + + /// + /// 创建 INI 文件对象 + /// + public IniFile() + { + _sections = new Dictionary>(StringComparer.OrdinalIgnoreCase); + _sectionOrder = new List(); + } + + /// + /// 从文件加载 + /// + public void Load(string filePath) + { + if (!File.Exists(filePath)) + return; + + var lines = File.ReadAllLines(filePath, Encoding.UTF8); + string currentSection = ""; + + foreach (var line in lines) + { + string trimmed = line.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith(_commentPrefix) || trimmed.StartsWith("#")) + continue; + + // 节 + if (trimmed.StartsWith("[") && trimmed.EndsWith("]")) + { + currentSection = trimmed.Substring(1, trimmed.Length - 2).Trim(); + if (!_sections.ContainsKey(currentSection)) + { + _sections[currentSection] = new Dictionary(StringComparer.OrdinalIgnoreCase); + _sectionOrder.Add(currentSection); + } + continue; + } + + // 键值对 + int equalIndex = trimmed.IndexOf('='); + if (equalIndex > 0) + { + string key = trimmed.Substring(0, equalIndex).Trim(); + string value = trimmed.Substring(equalIndex + 1).Trim(); + + // 移除行内注释 + int commentIndex = value.IndexOf(_commentPrefix); + if (commentIndex >= 0) + { + value = value.Substring(0, commentIndex).Trim(); + } + + // 处理引号包裹的值 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + value = value.Substring(1, value.Length - 2); + } + + if (!_sections.ContainsKey(currentSection)) + { + _sections[currentSection] = new Dictionary(StringComparer.OrdinalIgnoreCase); + if (!string.IsNullOrEmpty(currentSection)) + _sectionOrder.Add(currentSection); + } + + _sections[currentSection][key] = value; + } + } + } + + /// + /// 保存到文件 + /// + public void Save(string filePath) + { + var sb = new StringBuilder(); + + // 先写入空节的值 + if (_sections.TryGetValue("", out var globalSection)) + { + foreach (var kvp in globalSection) + { + sb.AppendLine($"{kvp.Key}={FormatValue(kvp.Value)}"); + } + sb.AppendLine(); + } + + // 写入各节 + foreach (var section in _sectionOrder) + { + if (string.IsNullOrEmpty(section)) continue; + + sb.AppendLine($"[{section}]"); + if (_sections.TryGetValue(section, out var sectionData)) + { + foreach (var kvp in sectionData) + { + sb.AppendLine($"{kvp.Key}={FormatValue(kvp.Value)}"); + } + } + sb.AppendLine(); + } + + File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); + } + + private static string FormatValue(string value) + { + if (string.IsNullOrEmpty(value)) return ""; + if (value.Contains(";") || value.Contains("#") || value.Contains(" ")) + return $"\"{value}\""; + return value; + } + + /// + /// 获取值 + /// + public string GetValue(string section, string key, string defaultValue = "") + { + if (_sections.TryGetValue(section, out var sectionData)) + { + if (sectionData.TryGetValue(key, out var value)) + return value; + } + return defaultValue; + } + + /// + /// 获取值并转换为指定类型 + /// + public T GetValue(string section, string key, T defaultValue = default) + { + string value = GetValue(section, key); + if (string.IsNullOrEmpty(value)) + return defaultValue; + + try + { + var type = typeof(T); + if (type == typeof(string)) + return (T)(object)value; + if (type == typeof(int)) + return (T)(object)int.Parse(value); + if (type == typeof(long)) + return (T)(object)long.Parse(value); + if (type == typeof(double)) + return (T)(object)double.Parse(value); + if (type == typeof(bool)) + return (T)(object)ParseBool(value); + if (type == typeof(DateTime)) + return (T)(object)DateTime.Parse(value); + + return (T)Convert.ChangeType(value, type); + } + catch + { + return defaultValue; + } + } + + private static bool ParseBool(string value) + { + return value.Equals("true", StringComparison.OrdinalIgnoreCase) || + value.Equals("1", StringComparison.OrdinalIgnoreCase) || + value.Equals("yes", StringComparison.OrdinalIgnoreCase) || + value.Equals("on", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 设置值 + /// + public void SetValue(string section, string key, string value) + { + if (!_sections.TryGetValue(section, out var sectionData)) + { + sectionData = new Dictionary(StringComparer.OrdinalIgnoreCase); + _sections[section] = sectionData; + if (!string.IsNullOrEmpty(section) && !_sectionOrder.Contains(section)) + _sectionOrder.Add(section); + } + + sectionData[key] = value; + } + + /// + /// 设置值 + /// + public void SetValue(string section, string key, T value) + { + SetValue(section, key, value?.ToString()); + } + + /// + /// 删除键 + /// + public bool DeleteKey(string section, string key) + { + if (_sections.TryGetValue(section, out var sectionData)) + { + return sectionData.Remove(key); + } + return false; + } + + /// + /// 删除节 + /// + public bool DeleteSection(string section) + { + _sectionOrder.Remove(section); + return _sections.Remove(section); + } + + /// + /// 获取节中的所有键值对 + /// + public Dictionary GetSection(string section) + { + if (_sections.TryGetValue(section, out var sectionData)) + { + return new Dictionary(sectionData); + } + return new Dictionary(); + } + + /// + /// 节是否存在 + /// + public bool HasSection(string section) + { + return _sections.ContainsKey(section); + } + + /// + /// 键是否存在 + /// + public bool HasKey(string section, string key) + { + return _sections.TryGetValue(section, out var sectionData) && sectionData.ContainsKey(key); + } + + /// + /// 清空所有内容 + /// + public void Clear() + { + _sections.Clear(); + _sectionOrder.Clear(); + } + } +} diff --git a/EasyTool.Core/IOCategory/IoUtil.cs b/EasyTool.Core/IOCategory/IoUtil.cs deleted file mode 100644 index 96cc1b9..0000000 --- a/EasyTool.Core/IOCategory/IoUtil.cs +++ /dev/null @@ -1,162 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Text; - -namespace EasyTool -{ - /// - /// Io流处理工具类 - /// - public static class IoUtil - { - /// - /// 读取文件的所有行到一个字符串数组中 - /// - /// 文件路径 - /// 字符串数组,其中包含文件的所有行。 - public static string[] ReadAllLines(string path) - { - return File.ReadAllLines(path); - } - - /// - /// 将字符串数组写入文件,覆盖原有内容 - /// - /// 文件路径 - /// 待写入的字符串数组 - public static void WriteAllLines(string path, string[] lines) - { - File.WriteAllLines(path, lines); - } - - /// - /// 读取整个文件到一个字符串中 - /// - /// 文件路径 - /// 文件的所有内容 - public static string ReadAllText(string path) - { - return File.ReadAllText(path); - } - - /// - /// 将字符串写入文件,覆盖原有内容 - /// - /// 文件路径 - /// 待写入的字符串 - public static void WriteAllText(string path, string text) - { - File.WriteAllText(path, text); - } - - /// - /// 读取二进制数据到一个字节数组中 - /// - /// 文件路径 - /// - public static byte[] ReadAllBytes(string path) - { - return File.ReadAllBytes(path); - } - - /// - /// 将字节数组写入二进制文件,覆盖原有内容 - /// - /// 文件路径 - /// 待写入的字节数组 - public static void WriteAllBytes(string path, byte[] bytes) - { - File.WriteAllBytes(path, bytes); - } - - /// - /// 读取指定 URL 的文本内容 - /// - /// URL 地址 - /// URL 返回的文本内容 - public static string ReadUrl(string url) - { - WebClient client = new WebClient(); - return client.DownloadString(url); - } - - /// - /// 将字符串写入指定 URL - /// - /// URL 地址 - /// 待写入的字符串 - public static void WriteUrl(string url, string text) - { - WebClient client = new WebClient(); - client.UploadString(url, text); - } - - /// - /// 读取网络流到一个字符串中 - /// - /// 网络流 - /// 网络流的所有内容 - public static string ReadStream(Stream stream) - { - using (StreamReader reader = new StreamReader(stream)) - { - return reader.ReadToEnd(); - } - } - - /// - /// 将字符串写入网络流 - /// - /// 网络流 - /// 待写入的字符串 - public static void WriteStream(Stream stream, string text) - { - using (StreamWriter writer = new StreamWriter(stream)) - { - writer.Write(text); - } - } - - /// - /// 读取二进制数据到一个内存流中 - /// - /// 二进制数据 - /// 内存流,其中包含输入的二进制数据 - public static MemoryStream ReadMemoryStream(byte[] bytes) - { - return new MemoryStream(bytes); - } - - /// - /// 将二进制数据写入一个内存流中 - /// - /// 内存流 - /// 待写入的字节数组 - public static void WriteMemoryStream(MemoryStream stream, byte[] bytes) - { - stream.Write(bytes, 0, bytes.Length); - } - - /// - /// 将一个字符串转换为字节数组 - /// - /// 待转换的字符串 - /// 字节数组,其中包含输入字符串的编码数据 - public static byte[] StringToBytes(string text) - { - return Encoding.UTF8.GetBytes(text); - } - - /// - /// 将一个字节数组转换为字符串 - /// - /// 待转换的字节数组 - /// 字符串,其中包含输入字节数组的编码数据 - public static string BytesToString(byte[] bytes) - { - return Encoding.UTF8.GetString(bytes); - } - } -} diff --git a/EasyTool.Core/IOCategory/JsonSerializer.cs b/EasyTool.Core/IOCategory/JsonSerializer.cs new file mode 100644 index 0000000..871aa5f --- /dev/null +++ b/EasyTool.Core/IOCategory/JsonSerializer.cs @@ -0,0 +1,314 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace EasyTool.IOCategory +{ + /// + /// JSON序列化工具增强版 + /// + public static class JsonSerializer + { + private static JsonSerializerOptions _defaultOptions; + private static JsonSerializerOptions _indentedOptions; + private static JsonSerializerOptions _camelCaseOptions; + + static JsonSerializer() + { + _defaultOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = null, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + + _indentedOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = null, + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + + _camelCaseOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + Converters = { new JsonStringEnumConverter() } + }; + } + + /// + /// 序列化对象为JSON字符串 + /// + public static string Serialize(T value, bool indented = false) + { + return System.Text.Json.JsonSerializer.Serialize(value, indented ? _indentedOptions : _defaultOptions); + } + + /// + /// 序列化对象为JSON字符串(驼峰命名) + /// + public static string SerializeCamelCase(T value) + { + return System.Text.Json.JsonSerializer.Serialize(value, _camelCaseOptions); + } + + /// + /// 反序列化JSON字符串为对象 + /// + public static T? Deserialize(string json) + { + return System.Text.Json.JsonSerializer.Deserialize(json, _defaultOptions); + } + + /// + /// 反序列化JSON字符串为对象(驼峰命名) + /// + public static T? DeserializeCamelCase(string json) + { + return System.Text.Json.JsonSerializer.Deserialize(json, _camelCaseOptions); + } + + /// + /// 序列化到文件 + /// + public static void SerializeToFile(T value, string filePath, bool indented = true) + { + var directory = System.IO.Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !System.IO.Directory.Exists(directory)) + System.IO.Directory.CreateDirectory(directory); + + var json = Serialize(value, indented); + System.IO.File.WriteAllText(filePath, json); + } + + /// + /// 从文件反序列化 + /// + public static T? DeserializeFromFile(string filePath) + { + if (!System.IO.File.Exists(filePath)) + throw new System.IO.FileNotFoundException("文件不存在", filePath); + + var json = System.IO.File.ReadAllText(filePath); + return Deserialize(json); + } + + /// + /// 异步序列化到文件 + /// + public static async System.Threading.Tasks.Task SerializeToFileAsync(T value, string filePath, bool indented = true) + { + var directory = System.IO.Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !System.IO.Directory.Exists(directory)) + System.IO.Directory.CreateDirectory(directory); + + var json = Serialize(value, indented); + await System.IO.File.WriteAllTextAsync(filePath, json); + } + + /// + /// 异步从文件反序列化 + /// + public static async System.Threading.Tasks.Task DeserializeFromFileAsync(string filePath) + { + if (!System.IO.File.Exists(filePath)) + throw new System.IO.FileNotFoundException("文件不存在", filePath); + + var json = await System.IO.File.ReadAllTextAsync(filePath); + return Deserialize(json); + } + + /// + /// 尝试反序列化 + /// + public static bool TryDeserialize(string json, out T? result) + { + try + { + result = Deserialize(json); + return true; + } + catch + { + result = default; + return false; + } + } + + /// + /// 验证JSON格式 + /// + public static bool IsValidJson(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + using var doc = JsonDocument.Parse(json); + return true; + } + catch + { + return false; + } + } + + /// + /// 格式化JSON + /// + public static string Format(string json) + { + using var doc = JsonDocument.Parse(json); + return System.Text.Json.JsonSerializer.Serialize(doc.RootElement, _indentedOptions); + } + + /// + /// 压缩JSON + /// + public static string Minify(string json) + { + using var doc = JsonDocument.Parse(json); + return System.Text.Json.JsonSerializer.Serialize(doc.RootElement, _defaultOptions); + } + + /// + /// 合并两个JSON对象 + /// + public static string Merge(string json1, string json2) + { + var dict1 = Deserialize>(json1); + var dict2 = Deserialize>(json2); + + if (dict1 == null) return json2; + if (dict2 == null) return json1; + + foreach (var kvp in dict2) + { + dict1[kvp.Key] = kvp.Value; + } + + return Serialize(dict1); + } + + /// + /// 获取JSON值(通过路径) + /// + public static string? GetValue(string json, string path) + { + try + { + using var doc = JsonDocument.Parse(json); + var current = doc.RootElement; + + var parts = path.Split('.'); + foreach (var part in parts) + { + if (current.TryGetProperty(part, out var property)) + { + current = property; + } + else + { + return null; + } + } + + return current.ValueKind == JsonValueKind.String + ? current.GetString() + : current.ToString(); + } + catch + { + return null; + } + } + + /// + /// 设置JSON值(通过路径) + /// + public static string SetValue(string json, string path, object value) + { + var dict = Deserialize>(json) ?? new Dictionary(); + + var parts = path.Split('.'); + var current = dict; + + for (int i = 0; i < parts.Length - 1; i++) + { + var part = parts[i]; + if (!current.ContainsKey(part)) + { + current[part] = new Dictionary(); + } + + current = (Dictionary)current[part]; + } + + current[parts[^1]] = value; + + return Serialize(dict); + } + + /// + /// 获取JSON的所有键 + /// + public static List GetKeys(string json) + { + var keys = new List(); + + try + { + using var doc = JsonDocument.Parse(json); + if (doc.RootElement.ValueKind == JsonValueKind.Object) + { + foreach (var property in doc.RootElement.EnumerateObject()) + { + keys.Add(property.Name); + } + } + } + catch + { + } + + return keys; + } + + /// + /// 深拷贝对象 + /// + public static T? DeepClone(T obj) + { + var json = Serialize(obj); + return Deserialize(json); + } + + /// + /// 转换类型 + /// + public static TTo? Convert(TFrom from) + { + var json = Serialize(from); + return Deserialize(json); + } + + /// + /// 获取自定义选项 + /// + public static JsonSerializerOptions GetOptions(bool indented = false, bool camelCase = false) + { + if (camelCase) + return new JsonSerializerOptions(_camelCaseOptions) { WriteIndented = indented }; + return new JsonSerializerOptions(_defaultOptions) { WriteIndented = indented }; + } + } +} diff --git a/EasyTool.Core/IOCategory/JsonUtil.cs b/EasyTool.Core/IOCategory/JsonUtil.cs new file mode 100644 index 0000000..91d1fb5 --- /dev/null +++ b/EasyTool.Core/IOCategory/JsonUtil.cs @@ -0,0 +1,497 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace EasyTool.IOCategory +{ + /// + /// JSON 工具类 + /// 提供 JSON 序列化/反序列化的增强功能 + /// + public static class JsonUtil + { + #region 默认选项 + + /// + /// 默认序列化选项(驼峰命名、缩进、忽略null) + /// + public static JsonSerializerOptions DefaultOptions + { + get + { + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString + }; + options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); + return options; + } + } + + /// + /// 紧凑序列化选项(无缩进、忽略null、驼峰命名) + /// + public static JsonSerializerOptions CompactOptions + { + get + { + return new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = false, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + NumberHandling = JsonNumberHandling.AllowReadingFromString + }; + } + } + + /// + /// 宽松反序列化选项(允许不带引号的数字、允许注释、允许尾随逗号) + /// + public static JsonSerializerOptions LenientOptions + { + get + { + return new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true + }; + } + } + + #endregion + + #region 序列化 + + /// + /// 将对象序列化为 JSON 字符串 + /// + public static string Serialize(T obj, JsonSerializerOptions? options = null) + { + if (obj == null) + return "null"; + + // 使用泛型方法 + return JsonSerializer.Serialize(obj); + } + + /// + /// 将对象序列化为 JSON 字符串(紧凑格式) + /// + public static string SerializeCompact(T obj) + { + return JsonSerializer.Serialize(obj); + } + + /// + /// 将对象序列化为 JSON 字节数组 + /// + public static byte[] SerializeToUtf8Bytes(T obj, JsonSerializerOptions? options = null) + { + var json = JsonSerializer.Serialize(obj); + return Encoding.UTF8.GetBytes(json); + } + + #endregion + + #region 反序列化 + + /// + /// 将 JSON 字符串反序列化为对象 + /// + public static T? Deserialize(string json, JsonSerializerOptions? options = null) + { + if (string.IsNullOrWhiteSpace(json)) + return default; + + return JsonSerializer.Deserialize(json); + } + + /// + /// 将 JSON 字节数组反序列化为对象 + /// + public static T? Deserialize(byte[] utf8Json, JsonSerializerOptions? options = null) + { + if (utf8Json == null || utf8Json.Length == 0) + return default; + + var json = Encoding.UTF8.GetString(utf8Json); + return JsonSerializer.Deserialize(json); + } + + /// + /// 尝试将 JSON 字符串反序列化为对象 + /// + public static bool TryDeserialize(string json, out T? result, JsonSerializerOptions? options = null) + { + result = default; + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + result = Deserialize(json, options); + return true; + } + catch + { + return false; + } + } + + /// + /// 将 JSON 字符串反序列化为动态对象 + /// + public static JsonNode? Parse(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return null; + + return JsonNode.Parse(json); + } + + #endregion + + #region 格式化与验证 + + /// + /// 格式化 JSON 字符串(美化输出) + /// + public static string Prettify(string json, string indent = " ") + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + var options = new JsonSerializerOptions { WriteIndented = true }; + return node?.ToJsonString(options) ?? json; + } + catch + { + return json; + } + } + + /// + /// 压缩 JSON 字符串(移除空白) + /// + public static string Minify(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + return node?.ToJsonString(new JsonSerializerOptions { WriteIndented = false }) ?? json; + } + catch + { + return json; + } + } + + /// + /// 验证是否为有效的 JSON + /// + public static bool IsValid(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return false; + + try + { + JsonNode.Parse(json); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 路径操作 + + /// + /// 从 JSON 字符串中获取指定路径的值 + /// + public static object? GetValue(string json, string path) + { + if (string.IsNullOrWhiteSpace(json) || string.IsNullOrWhiteSpace(path)) + return null; + + try + { + var node = JsonNode.Parse(json); + return GetValueByPath(node, path); + } + catch + { + return null; + } + } + + /// + /// 从 JSON 字符串中获取指定路径的值并转换为指定类型 + /// + public static T? GetValue(string json, string path) + { + var value = GetValue(json, path); + if (value == null) + return default; + + if (value is JsonValue jsonValue) + { + return jsonValue.GetValue(); + } + + if (value is JsonNode jsonNode) + { + return Deserialize(jsonNode.ToJsonString()); + } + + return (T?)Convert.ChangeType(value, typeof(T)); + } + + /// + /// 设置 JSON 字符串中指定路径的值 + /// + public static string SetValue(string json, string path, object? value) + { + if (string.IsNullOrWhiteSpace(json)) + return json; + + try + { + var node = JsonNode.Parse(json); + SetValueByPath(node, path, value); + return node?.ToJsonString(DefaultOptions) ?? json; + } + catch + { + return json; + } + } + + private static object? GetValueByPath(JsonNode? node, string path) + { + if (node == null) + return null; + + var parts = path.Split('.'); + JsonNode? current = node; + + foreach (var part in parts) + { + if (current == null) + return null; + + if (part.Contains('[') && part.EndsWith(']')) + { + var name = part.Substring(0, part.IndexOf('[')); + var indexStr = part.Substring(part.IndexOf('[') + 1, part.Length - part.IndexOf('[') - 2); + + if (!string.IsNullOrEmpty(name)) + current = current[name]; + + if (int.TryParse(indexStr, out int index) && current is JsonArray array) + { + current = index < array.Count ? array[index] : null; + } + } + else + { + current = current[part]; + } + } + + return current; + } + + private static void SetValueByPath(JsonNode? node, string path, object? value) + { + if (node == null) + return; + + var parts = path.Split('.'); + JsonNode current = node; + + for (int i = 0; i < parts.Length - 1; i++) + { + var part = parts[i]; + + if (part.Contains('[') && part.EndsWith(']')) + { + var name = part.Substring(0, part.IndexOf('[')); + var indexStr = part.Substring(part.IndexOf('[') + 1, part.Length - part.IndexOf('[') - 2); + + if (!string.IsNullOrEmpty(name)) + { + if (current[name] == null) + current[name] = new JsonObject(); + current = current[name]!; + } + + if (int.TryParse(indexStr, out int index)) + { + if (current is JsonArray array) + { + while (array.Count <= index) + array.Add(null); + current = array[index] ??= new JsonObject(); + } + } + } + else + { + if (current[part] == null) + current[part] = new JsonObject(); + current = current[part]!; + } + } + + var lastPart = parts[^1]; + if (lastPart.Contains('[') && lastPart.EndsWith(']')) + { + var name = lastPart.Substring(0, lastPart.IndexOf('[')); + var indexStr = lastPart.Substring(lastPart.IndexOf('[') + 1, lastPart.Length - lastPart.IndexOf('[') - 2); + + JsonNode? target = current; + if (!string.IsNullOrEmpty(name)) + { + if (current[name] == null) + current[name] = new JsonArray(); + target = current[name]; + } + + if (int.TryParse(indexStr, out int index) && target is JsonArray array) + { + while (array.Count <= index) + array.Add(null); + array[index] = JsonValue.Create(value); + } + } + else + { + current[lastPart] = JsonValue.Create(value); + } + } + + #endregion + + #region 转换操作 + + /// + /// 将字典转换为 JSON 对象 + /// + public static string FromDictionary(Dictionary dictionary) + { + if (dictionary == null) + return "{}"; + + return JsonSerializer.Serialize(dictionary); + } + + /// + /// 将 JSON 对象转换为字典 + /// + public static Dictionary? ToDictionary(string json) + { + if (string.IsNullOrWhiteSpace(json)) + return null; + + return JsonSerializer.Deserialize>(json); + } + + /// + /// 将匿名对象转换为 JSON 字符串 + /// + public static string FromAnonymous(object obj) + { + return JsonSerializer.Serialize(obj); + } + + /// + /// 深拷贝对象(通过 JSON 序列化/反序列化) + /// + public static T? DeepClone(T obj) + { + if (obj == null) + return default; + + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize(json); + } + + #endregion + + #region 合并操作 + + /// + /// 合并两个 JSON 对象 + /// + public static string Merge(string json1, string json2) + { + if (string.IsNullOrWhiteSpace(json1)) + return json2; + if (string.IsNullOrWhiteSpace(json2)) + return json1; + + try + { + var node1 = JsonNode.Parse(json1) as JsonObject; + var node2 = JsonNode.Parse(json2) as JsonObject; + + if (node1 == null) + return json2; + if (node2 == null) + return json1; + + MergeObjects(node1, node2); + return node1.ToJsonString(DefaultOptions); + } + catch + { + return json1; + } + } + + private static void MergeObjects(JsonObject target, JsonObject source) + { + foreach (var property in source) + { + if (target.ContainsKey(property.Key)) + { + if (target[property.Key] is JsonObject targetObj && + property.Value is JsonObject sourceObj) + { + MergeObjects(targetObj, sourceObj); + } + else + { + target[property.Key] = property.Value?.DeepClone(); + } + } + else + { + target[property.Key] = property.Value?.DeepClone(); + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/MimeTypeUtil.cs b/EasyTool.Core/IOCategory/MimeTypeUtil.cs new file mode 100644 index 0000000..67ea4ea --- /dev/null +++ b/EasyTool.Core/IOCategory/MimeTypeUtil.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace EasyTool.IOCategory +{ + /// + /// MIME 类型工具类 + /// 提供根据文件扩展名和文件内容检测 MIME 类型的功能 + /// + public static class MimeTypeUtil + { + private static readonly Dictionary ExtensionToMimeType = new(StringComparer.OrdinalIgnoreCase) + { + // 文本 + {".txt", "text/plain"}, + {".html", "text/html"}, + {".htm", "text/html"}, + {".css", "text/css"}, + {".js", "application/javascript"}, + {".json", "application/json"}, + {".xml", "application/xml"}, + {".csv", "text/csv"}, + {".md", "text/markdown"}, + {".yaml", "text/yaml"}, + {".yml", "text/yaml"}, + + // 图片 + {".jpg", "image/jpeg"}, + {".jpeg", "image/jpeg"}, + {".png", "image/png"}, + {".gif", "image/gif"}, + {".bmp", "image/bmp"}, + {".ico", "image/x-icon"}, + {".svg", "image/svg+xml"}, + {".webp", "image/webp"}, + {".tiff", "image/tiff"}, + {".tif", "image/tiff"}, + + // 音频 + {".mp3", "audio/mpeg"}, + {".wav", "audio/wav"}, + {".ogg", "audio/ogg"}, + {".flac", "audio/flac"}, + {".aac", "audio/aac"}, + {".wma", "audio/x-ms-wma"}, + {".m4a", "audio/mp4"}, + + // 视频 + {".mp4", "video/mp4"}, + {".avi", "video/x-msvideo"}, + {".mkv", "video/x-matroska"}, + {".mov", "video/quicktime"}, + {".wmv", "video/x-ms-wmv"}, + {".flv", "video/x-flv"}, + {".webm", "video/webm"}, + {".m4v", "video/mp4"}, + + // 文档 + {".pdf", "application/pdf"}, + {".doc", "application/msword"}, + {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {".xls", "application/vnd.ms-excel"}, + {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {".ppt", "application/vnd.ms-powerpoint"}, + {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + + // 压缩 + {".zip", "application/zip"}, + {".rar", "application/x-rar-compressed"}, + {".7z", "application/x-7z-compressed"}, + {".tar", "application/x-tar"}, + {".gz", "application/gzip"}, + {".bz2", "application/x-bzip2"}, + + // 可执行 + {".exe", "application/x-msdownload"}, + {".msi", "application/x-msi"}, + {".dll", "application/x-msdownload"}, + {".so", "application/x-sharedlib"}, + {".dylib", "application/x-sharedlib"}, + {".jar", "application/java-archive"}, + {".apk", "application/vnd.android.package-archive"}, + + // 代码 + {".cs", "text/x-csharp"}, + {".java", "text/x-java-source"}, + {".py", "text/x-python"}, + {".rb", "text/x-ruby"}, + {".php", "text/x-php"}, + {".cpp", "text/x-c++src"}, + {".c", "text/x-csrc"}, + {".h", "text/x-chdr"}, + {".go", "text/x-go"}, + {".rs", "text/x-rust"}, + {".swift", "text/x-swift"}, + {".kt", "text/x-kotlin"}, + {".ts", "text/typescript"}, + {".tsx", "text/typescript-jsx"}, + + // 字体 + {".ttf", "font/ttf"}, + {".otf", "font/otf"}, + {".woff", "font/woff"}, + {".woff2", "font/woff2"}, + {".eot", "application/vnd.ms-fontobject"}, + + // 其他 + {".bin", "application/octet-stream"}, + {".dat", "application/octet-stream"}, + }; + + private static readonly Dictionary FileSignatures = new() + { + {"image/jpeg", new byte[] {0xFF, 0xD8, 0xFF}}, + {"image/png", new byte[] {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}}, + {"image/gif", new byte[] {0x47, 0x49, 0x46, 0x38}}, + {"image/bmp", new byte[] {0x42, 0x4D}}, + {"application/pdf", new byte[] {0x25, 0x50, 0x44, 0x46}}, + {"application/zip", new byte[] {0x50, 0x4B, 0x03, 0x04}}, + {"application/x-rar-compressed", new byte[] {0x52, 0x61, 0x72, 0x21}}, + {"application/x-7z-compressed", new byte[] {0x37, 0x7A, 0xBC, 0xAF}}, + {"video/mp4", new byte[] {0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70}}, + {"audio/mpeg", new byte[] {0xFF, 0xFB}}, + {"application/java-archive", new byte[] {0x50, 0x4B, 0x03, 0x04}}, + }; + + /// + /// 根据文件扩展名获取 MIME 类型 + /// + public static string GetByExtension(string extension) + { + if (string.IsNullOrEmpty(extension)) + return "application/octet-stream"; + + if (!extension.StartsWith(".")) + extension = "." + extension; + + return ExtensionToMimeType.TryGetValue(extension, out string mime) + ? mime + : "application/octet-stream"; + } + + /// + /// 根据文件路径获取 MIME 类型 + /// + public static string GetByPath(string filePath) + { + return GetByExtension(Path.GetExtension(filePath)); + } + + /// + /// 根据文件内容检测 MIME 类型 + /// + public static string DetectByContent(string filePath) + { + if (!File.Exists(filePath)) + return "application/octet-stream"; + + using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + return DetectByContent(stream); + } + + /// + /// 根据流内容检测 MIME 类型 + /// + public static string DetectByContent(Stream stream) + { + byte[] header = new byte[16]; + int bytesRead = stream.Read(header, 0, header.Length); + + if (bytesRead == 0) + return "application/octet-stream"; + + foreach (var signature in FileSignatures) + { + if (header.Length >= signature.Value.Length) + { + bool match = true; + for (int i = 0; i < signature.Value.Length; i++) + { + if (header[i] != signature.Value[i]) + { + match = false; + break; + } + } + if (match) + return signature.Key; + } + } + + // 检查是否为文本 + if (IsTextContent(header, bytesRead)) + return "text/plain"; + + return "application/octet-stream"; + } + + private static bool IsTextContent(byte[] data, int length) + { + for (int i = 0; i < length; i++) + { + byte b = data[i]; + // 允许的控制字符:换行、回车、制表符 + if (b < 32 && b != 9 && b != 10 && b != 13) + return false; + } + return true; + } + + /// + /// 组合检测(先检测内容,再根据扩展名补充) + /// + public static string Detect(string filePath) + { + string byContent = DetectByContent(filePath); + if (byContent != "application/octet-stream") + return byContent; + + return GetByPath(filePath); + } + + /// + /// 根据 MIME 类型获取文件扩展名 + /// + public static string GetExtension(string mimeType) + { + if (string.IsNullOrEmpty(mimeType)) + return ".bin"; + + var entry = ExtensionToMimeType.FirstOrDefault(x => + x.Value.Equals(mimeType, StringComparison.OrdinalIgnoreCase)); + + return entry.Key ?? ".bin"; + } + + /// + /// 判断是否为图片 + /// + public static bool IsImage(string mimeType) + { + return mimeType?.StartsWith("image/") == true; + } + + /// + /// 判断是否为音频 + /// + public static bool IsAudio(string mimeType) + { + return mimeType?.StartsWith("audio/") == true; + } + + /// + /// 判断是否为视频 + /// + public static bool IsVideo(string mimeType) + { + return mimeType?.StartsWith("video/") == true; + } + + /// + /// 判断是否为文本 + /// + public static bool IsText(string mimeType) + { + return mimeType?.StartsWith("text/") == true || + mimeType == "application/json" || + mimeType == "application/xml" || + mimeType == "application/javascript"; + } + + /// + /// 注册自定义 MIME 类型 + /// + public static void Register(string extension, string mimeType) + { + if (!extension.StartsWith(".")) + extension = "." + extension; + + ExtensionToMimeType[extension] = mimeType; + } + } +} diff --git a/EasyTool.Core/IOCategory/PathUtil.cs b/EasyTool.Core/IOCategory/PathUtil.cs new file mode 100644 index 0000000..a65039a --- /dev/null +++ b/EasyTool.Core/IOCategory/PathUtil.cs @@ -0,0 +1,353 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 路径工具类 + /// + public static class PathUtil + { + /// + /// 合并路径 + /// + public static string Combine(params string[] paths) + { + return Path.Combine(paths); + } + + /// + /// 获取绝对路径 + /// + public static string GetFullPath(string path, string? basePath = null) + { + if (string.IsNullOrEmpty(path)) + return path; + + if (Path.IsPathRooted(path)) + return Path.GetFullPath(path); + + basePath ??= Directory.GetCurrentDirectory(); + return Path.GetFullPath(Path.Combine(basePath, path)); + } + + /// + /// 获取相对路径 + /// + public static string GetRelativePath(string relativeTo, string path) + { + return Path.GetRelativePath(relativeTo, path); + } + + /// + /// 获取文件名(包含扩展名) + /// + public static string GetFileName(string path) + { + return Path.GetFileName(path); + } + + /// + /// 获取文件名(不含扩展名) + /// + public static string GetFileNameWithoutExtension(string path) + { + return Path.GetFileNameWithoutExtension(path); + } + + /// + /// 获取扩展名 + /// + public static string GetExtension(string path) + { + return Path.GetExtension(path); + } + + /// + /// 获取目录路径 + /// + public static string? GetDirectoryName(string path) + { + return Path.GetDirectoryName(path); + } + + /// + /// 更改扩展名 + /// + public static string ChangeExtension(string path, string extension) + { + return Path.ChangeExtension(path, extension); + } + + /// + /// 移除扩展名 + /// + public static string RemoveExtension(string path) + { + return Path.ChangeExtension(path, null) ?? path; + } + + /// + /// 检查是否是绝对路径 + /// + public static bool IsAbsolute(string path) + { + return Path.IsPathRooted(path); + } + + /// + /// 检查是否是相对路径 + /// + public static bool IsRelative(string path) + { + return !Path.IsPathRooted(path); + } + + /// + /// 规范化路径(统一分隔符) + /// + public static string Normalize(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + return path.Replace('/', Path.DirectorySeparatorChar) + .Replace('\\', Path.DirectorySeparatorChar) + .TrimEnd(Path.DirectorySeparatorChar); + } + + /// + /// 确保以分隔符结尾 + /// + public static string EnsureTrailingSeparator(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + if (!path.EndsWith(Path.DirectorySeparatorChar.ToString())) + return path + Path.DirectorySeparatorChar; + + return path; + } + + /// + /// 移除尾部分隔符 + /// + public static string TrimTrailingSeparator(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + return path.TrimEnd(Path.DirectorySeparatorChar, '/'); + } + + /// + /// 获取父目录 + /// + public static string? GetParent(string path) + { + if (string.IsNullOrEmpty(path)) + return null; + + var dir = Path.GetDirectoryName(path); + if (string.IsNullOrEmpty(dir)) + return null; + + return dir; + } + + /// + /// 获取所有父目录 + /// + public static IEnumerable GetParents(string path) + { + var current = path; + while (!string.IsNullOrEmpty(current)) + { + var parent = Path.GetDirectoryName(current); + if (string.IsNullOrEmpty(parent)) + yield break; + + yield return parent; + current = parent; + } + } + + /// + /// 获取目录深度 + /// + public static int GetDepth(string path) + { + if (string.IsNullOrEmpty(path)) + return 0; + + path = Normalize(path); + return path.Split(Path.DirectorySeparatorChar).Length - 1; + } + + /// + /// 检查路径是否在指定目录下 + /// + public static bool IsInDirectory(string path, string directory) + { + if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(directory)) + return false; + + var fullPath = GetFullPath(path); + var fullDirectory = GetFullPath(directory); + + return fullPath.StartsWith(EnsureTrailingSeparator(fullDirectory), StringComparison.OrdinalIgnoreCase); + } + + /// + /// 获取唯一文件名(避免冲突) + /// + public static string GetUniqueFileName(string directory, string fileName) + { + var fullPath = Path.Combine(directory, fileName); + + if (!File.Exists(fullPath)) + return fileName; + + var name = Path.GetFileNameWithoutExtension(fileName); + var ext = Path.GetExtension(fileName); + var counter = 1; + + while (true) + { + var newName = $"{name} ({counter}){ext}"; + fullPath = Path.Combine(directory, newName); + + if (!File.Exists(fullPath)) + return newName; + + counter++; + } + } + + /// + /// 获取临时文件路径 + /// + public static string GetTempFilePath(string? extension = null) + { + var path = Path.GetTempFileName(); + + if (!string.IsNullOrEmpty(extension)) + { + var newPath = Path.ChangeExtension(path, extension); + File.Move(path, newPath); + return newPath; + } + + return path; + } + + /// + /// 获取临时目录路径 + /// + public static string GetTempDirectoryPath() + { + var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + return path; + } + + /// + /// 分割路径为各部分 + /// + public static string[] Split(string path) + { + if (string.IsNullOrEmpty(path)) + return Array.Empty(); + + path = Normalize(path); + + // 处理根目录 + var root = Path.GetPathRoot(path); + if (!string.IsNullOrEmpty(root)) + { + path = path.Substring(root.Length); + var parts = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + var result = new string[parts.Length + 1]; + result[0] = root.TrimEnd(Path.DirectorySeparatorChar); + Array.Copy(parts, 0, result, 1, parts.Length); + return result; + } + + return path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + } + + /// + /// 构建路径 + /// + public static string Build(params string[] parts) + { + return Path.Combine(parts.Where(p => !string.IsNullOrEmpty(p)).ToArray()); + } + + /// + /// 验证路径是否有效 + /// + public static bool IsValid(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + var invalidChars = Path.GetInvalidPathChars(); + return path.IndexOfAny(invalidChars) < 0; + } + + /// + /// 验证文件名是否有效 + /// + public static bool IsValidFileName(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + return false; + + var invalidChars = Path.GetInvalidFileNameChars(); + return fileName.IndexOfAny(invalidChars) < 0; + } + + /// + /// 清理文件名(移除无效字符) + /// + public static string SanitizeFileName(string fileName, char replacement = '_') + { + if (string.IsNullOrEmpty(fileName)) + return fileName; + + var invalidChars = Path.GetInvalidFileNameChars(); + var result = new StringBuilder(fileName); + + foreach (var c in invalidChars) + { + result.Replace(c, replacement); + } + + return result.ToString(); + } + + /// + /// 获取路径大小(文件或目录) + /// + public static long GetSize(string path) + { + if (File.Exists(path)) + { + return new FileInfo(path).Length; + } + + if (Directory.Exists(path)) + { + var dirInfo = new DirectoryInfo(path); + return dirInfo.EnumerateFiles("*", SearchOption.AllDirectories).Sum(f => f.Length); + } + + return 0; + } + } +} diff --git a/EasyTool.Core/IOCategory/PropertiesUtil.cs b/EasyTool.Core/IOCategory/PropertiesUtil.cs new file mode 100644 index 0000000..0f83432 --- /dev/null +++ b/EasyTool.Core/IOCategory/PropertiesUtil.cs @@ -0,0 +1,630 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// Properties 配置文件工具类 + /// 用于读写 Java 风格的 .properties 配置文件 + /// + public static class PropertiesUtil + { + #region 读取方法 + + /// + /// 从文件加载 Properties + /// + /// 文件路径 + /// 编码方式(默认UTF-8) + /// Properties 字典 + public static Dictionary Load(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("Properties 文件不存在", filePath); + + encoding ??= Encoding.UTF8; + var lines = File.ReadAllLines(filePath, encoding); + return ParseLines(lines); + } + + /// + /// 从文件异步加载 Properties + /// + /// 文件路径 + /// 编码方式 + /// Properties 字典 + public static async System.Threading.Tasks.Task> LoadAsync(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("Properties 文件不存在", filePath); + + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(filePath, encoding); + var content = await reader.ReadToEndAsync(); + var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + return ParseLines(lines); + } + + /// + /// 从字符串加载 Properties + /// + /// Properties 内容 + /// Properties 字典 + public static Dictionary Parse(string content) + { + if (string.IsNullOrEmpty(content)) + return new Dictionary(); + + var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); + return ParseLines(lines); + } + + /// + /// 从流加载 Properties + /// + /// 输入流 + /// 编码方式 + /// Properties 字典 + public static Dictionary LoadFromStream(Stream stream, Encoding? encoding = null) + { + encoding ??= Encoding.UTF8; + using var reader = new StreamReader(stream, encoding); + var content = reader.ReadToEnd(); + return Parse(content); + } + + private static Dictionary ParseLines(string[] lines) + { + var properties = new Dictionary(); + int lineNumber = 0; + + foreach (var originalLine in lines) + { + lineNumber++; + string line = originalLine.Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#") || line.StartsWith("!")) + continue; + + // 查找分隔符 + int separatorIndex = FindSeparator(line); + if (separatorIndex < 0) + continue; + + string key = UnescapeKey(line.Substring(0, separatorIndex).Trim()); + string value = separatorIndex < line.Length - 1 + ? UnescapeValue(line.Substring(separatorIndex + 1).TrimStart()) + : string.Empty; + + properties[key] = value; + } + + return properties; + } + + private static int FindSeparator(string line) + { + for (int i = 0; i < line.Length; i++) + { + char c = line[i]; + if (c == '=' || c == ':' || char.IsWhiteSpace(c)) + { + // 检查是否被转义 + if (i > 0 && line[i - 1] == '\\') + continue; + return i; + } + } + return -1; + } + + #endregion + + #region 保存方法 + + /// + /// 保存 Properties 到文件 + /// + /// 文件路径 + /// Properties 字典 + /// 编码方式 + /// 注释(可选) + public static void Save(string filePath, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + File.WriteAllText(filePath, content, encoding); + } + + /// + /// 异步保存 Properties 到文件 + /// + /// 文件路径 + /// Properties 字典 + /// 编码方式 + /// 注释 + public static async System.Threading.Tasks.Task SaveAsync(string filePath, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + using var writer = new StreamWriter(filePath, false, encoding); + await writer.WriteAsync(content); + } + + /// + /// 保存 Properties 到流 + /// + /// 输出流 + /// Properties 字典 + /// 编码方式 + /// 注释 + public static void SaveToStream(Stream stream, Dictionary properties, Encoding? encoding = null, string? comment = null) + { + encoding ??= Encoding.UTF8; + var content = BuildContent(properties, comment); + using var writer = new StreamWriter(stream, encoding); + writer.Write(content); + } + + /// + /// 将 Properties 转换为字符串 + /// + /// Properties 字典 + /// 注释 + /// Properties 格式字符串 + public static string ToString(Dictionary properties, string? comment = null) + { + return BuildContent(properties, comment); + } + + private static string BuildContent(Dictionary properties, string? comment) + { + var sb = new StringBuilder(); + + // 添加注释 + if (!string.IsNullOrEmpty(comment)) + { + sb.AppendLine("# " + comment.Replace("\n", "\n# ")); + sb.AppendLine(); + } + + // 添加时间戳 + sb.AppendLine($"# {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); + sb.AppendLine(); + + foreach (var kvp in properties) + { + sb.AppendLine($"{EscapeKey(kvp.Key)}={EscapeValue(kvp.Value)}"); + } + + return sb.ToString(); + } + + #endregion + + #region 单值操作 + + /// + /// 获取属性值 + /// + /// 文件路径 + /// 键 + /// 默认值 + /// 编码方式 + /// 属性值 + public static string Get(string filePath, string key, string defaultValue = "", Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + return properties.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 设置属性值 + /// + /// 文件路径 + /// 键 + /// 值 + /// 编码方式 + public static void Set(string filePath, string key, string value, Encoding? encoding = null) + { + var properties = File.Exists(filePath) ? Load(filePath, encoding) : new Dictionary(); + properties[key] = value; + Save(filePath, properties, encoding); + } + + /// + /// 删除属性 + /// + /// 文件路径 + /// 键 + /// 编码方式 + /// 是否删除成功 + public static bool Remove(string filePath, string key, Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + if (properties.Remove(key)) + { + Save(filePath, properties, encoding); + return true; + } + return false; + } + + /// + /// 检查属性是否存在 + /// + /// 文件路径 + /// 键 + /// 编码方式 + /// 是否存在 + public static bool ContainsKey(string filePath, string key, Encoding? encoding = null) + { + var properties = Load(filePath, encoding); + return properties.ContainsKey(key); + } + + #endregion + + #region 类型转换获取 + + /// + /// 获取整数值 + /// + public static int GetInt(string filePath, string key, int defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !int.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取长整数值 + /// + public static long GetLong(string filePath, string key, long defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !long.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取双精度浮点值 + /// + public static double GetDouble(string filePath, string key, double defaultValue = 0, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !double.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取布尔值 + /// + public static bool GetBool(string filePath, string key, bool defaultValue = false, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null) + return defaultValue; + + return value.ToLower() switch + { + "true" or "yes" or "1" or "on" => true, + "false" or "no" or "0" or "off" => false, + _ => defaultValue + }; + } + + /// + /// 获取日期时间值 + /// + public static DateTime GetDateTime(string filePath, string key, DateTime defaultValue = default, Encoding? encoding = null) + { + var value = Get(filePath, key, null, encoding); + if (value == null || !DateTime.TryParse(value, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取枚举值 + /// + public static T GetEnum(string filePath, string key, T defaultValue = default, Encoding? encoding = null) where T : struct, Enum + { + var value = Get(filePath, key, null, encoding); + if (value == null || !Enum.TryParse(value, true, out var result)) + return defaultValue; + return result; + } + + /// + /// 获取字符串列表(逗号分隔) + /// + public static List GetList(string filePath, string key, Encoding? encoding = null) + { + var value = Get(filePath, key, "", encoding); + if (string.IsNullOrEmpty(value)) + return new List(); + + return value.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Where(s => !string.IsNullOrEmpty(s)) + .ToList(); + } + + #endregion + + #region 转义处理 + + private static string EscapeKey(string key) + { + var sb = new StringBuilder(); + foreach (char c in key) + { + switch (c) + { + case '=': sb.Append("\\="); break; + case ':': sb.Append("\\:"); break; + case ' ': sb.Append("\\ "); break; + case '\t': sb.Append("\\t"); break; + case '\n': sb.Append("\\n"); break; + case '\r': sb.Append("\\r"); break; + case '\\': sb.Append("\\\\"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + private static string EscapeValue(string value) + { + var sb = new StringBuilder(); + foreach (char c in value) + { + switch (c) + { + case '\n': sb.Append("\\n"); break; + case '\r': sb.Append("\\r"); break; + case '\t': sb.Append("\\t"); break; + case '\\': sb.Append("\\\\"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + private static string UnescapeKey(string key) + { + return Unescape(key); + } + + private static string UnescapeValue(string value) + { + return Unescape(value); + } + + private static string Unescape(string s) + { + var sb = new StringBuilder(); + bool escape = false; + + foreach (char c in s) + { + if (escape) + { + switch (c) + { + case 'n': sb.Append('\n'); break; + case 'r': sb.Append('\r'); break; + case 't': sb.Append('\t'); break; + case '\\': sb.Append('\\'); break; + case '=': sb.Append('='); break; + case ':': sb.Append(':'); break; + case ' ': sb.Append(' '); break; + default: sb.Append(c); break; + } + escape = false; + } + else if (c == '\\') + { + escape = true; + } + else + { + sb.Append(c); + } + } + + // 处理结尾的转义符 + if (escape) + sb.Append('\\'); + + return sb.ToString(); + } + + #endregion + + #region PropertiesDocument 类 + + /// + /// 创建可操作的 Properties 文档对象 + /// + /// 文件路径(可选) + /// PropertiesDocument 对象 + public static PropertiesDocument CreateDocument(string? filePath = null) + { + if (filePath != null && File.Exists(filePath)) + { + var properties = Load(filePath); + return new PropertiesDocument(filePath, properties); + } + return new PropertiesDocument(filePath, new Dictionary()); + } + + #endregion + } + + /// + /// 可操作的 Properties 文档对象 + /// + public class PropertiesDocument + { + private readonly string? _filePath; + private readonly Dictionary _properties; + private readonly List _comments; + private bool _modified; + + /// + /// 属性数量 + /// + public int Count => _properties.Count; + + /// + /// 是否已修改 + /// + public bool IsModified => _modified; + + /// + /// 所有键 + /// + public IEnumerable Keys => _properties.Keys; + + /// + /// 所有值 + /// + public IEnumerable Values => _properties.Values; + + /// + /// 获取或设置属性值 + /// + /// 键 + /// + public string this[string key] + { + get => _properties.TryGetValue(key, out var value) ? value : string.Empty; + set + { + _properties[key] = value; + _modified = true; + } + } + + internal PropertiesDocument(string? filePath, Dictionary properties) + { + _filePath = filePath; + _properties = properties; + _comments = new List(); + _modified = false; + } + + /// + /// 获取属性值 + /// + public string Get(string key, string defaultValue = "") + { + return _properties.TryGetValue(key, out var value) ? value : defaultValue; + } + + /// + /// 设置属性值 + /// + public void Set(string key, string value) + { + _properties[key] = value; + _modified = true; + } + + /// + /// 移除属性 + /// + public bool Remove(string key) + { + if (_properties.Remove(key)) + { + _modified = true; + return true; + } + return false; + } + + /// + /// 检查是否包含键 + /// + public bool ContainsKey(string key) + { + return _properties.ContainsKey(key); + } + + /// + /// 添加注释 + /// + public void AddComment(string comment) + { + _comments.Add(comment); + } + + /// + /// 保存到原文件 + /// + public void Save() + { + if (_filePath == null) + throw new InvalidOperationException("未指定文件路径"); + + PropertiesUtil.Save(_filePath, _properties, null, string.Join("\n", _comments)); + _modified = false; + } + + /// + /// 保存到指定文件 + /// + public void Save(string filePath) + { + PropertiesUtil.Save(filePath, _properties, null, string.Join("\n", _comments)); + _modified = false; + } + + /// + /// 重新加载文件 + /// + public void Reload() + { + if (_filePath == null || !File.Exists(_filePath)) + return; + + var newProperties = PropertiesUtil.Load(_filePath); + _properties.Clear(); + foreach (var kvp in newProperties) + { + _properties[kvp.Key] = kvp.Value; + } + _modified = false; + } + + /// + /// 转换为字典 + /// + public Dictionary ToDictionary() + { + return new Dictionary(_properties); + } + + /// + /// 批量设置属性 + /// + public void SetRange(Dictionary properties) + { + foreach (var kvp in properties) + { + _properties[kvp.Key] = kvp.Value; + } + _modified = true; + } + } +} diff --git a/EasyTool.Core/IOCategory/QrCodeUtil.cs b/EasyTool.Core/IOCategory/QrCodeUtil.cs new file mode 100644 index 0000000..2686cd0 --- /dev/null +++ b/EasyTool.Core/IOCategory/QrCodeUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 二维码配置 + /// + public class QrCodeOptions + { + /// + /// 宽度(像素) + /// + public int Width { get; set; } = 200; + + /// + /// 高度(像素) + /// + public int Height { get; set; } = 200; + + /// + /// 纠错级别 + /// + public QrCodeErrorCorrection ErrorCorrection { get; set; } = QrCodeErrorCorrection.Medium; + + /// + /// 前景色 + /// + public Color ForeColor { get; set; } = Color.Black; + + /// + /// 背景色 + /// + public Color BackColor { get; set; } = Color.White; + + /// + /// 边距(模块数) + /// + public int Margin { get; set; } = 4; + } + + /// + /// 二维码纠错级别 + /// + public enum QrCodeErrorCorrection + { + /// + /// 低(7%可纠错) + /// + Low = 0, + + /// + /// 中(15%可纠错) + /// + Medium = 1, + + /// + /// 高(25%可纠错) + /// + Quartile = 2, + + /// + /// 最高(30%可纠错) + /// + High = 3 + } + + /// + /// 二维码工具类 + /// 提供二维码生成功能 + /// + public static class QrCodeUtil + { + #region 生成二维码 + + /// + /// 生成二维码图像 + /// + /// 内容 + /// 配置 + /// 二维码图像 + public static Bitmap Generate(string content, QrCodeOptions? options = null) + { + options ??= new QrCodeOptions(); + + // 编码内容 + var bytes = Encoding.UTF8.GetBytes(content); + + // 生成QR码矩阵 + var matrix = GenerateQrMatrix(bytes, options.ErrorCorrection); + + // 创建图像 + var bitmap = new Bitmap(options.Width, options.Height, PixelFormat.Format24bppRgb); + + using (var g = Graphics.FromImage(bitmap)) + { + g.Clear(options.BackColor); + + var moduleWidth = (double)options.Width / (matrix.GetLength(0) + 2 * options.Margin); + var moduleHeight = (double)options.Height / (matrix.GetLength(1) + 2 * options.Margin); + var moduleSize = Math.Min(moduleWidth, moduleHeight); + + var offsetX = (options.Width - matrix.GetLength(0) * moduleSize) / 2; + var offsetY = (options.Height - matrix.GetLength(1) * moduleSize) / 2; + + using var brush = new SolidBrush(options.ForeColor); + + for (int y = 0; y < matrix.GetLength(1); y++) + { + for (int x = 0; x < matrix.GetLength(0); x++) + { + if (matrix[x, y]) + { + var rect = new RectangleF( + (float)(offsetX + x * moduleSize), + (float)(offsetY + y * moduleSize), + (float)moduleSize, + (float)moduleSize); + g.FillRectangle(brush, rect); + } + } + } + } + + return bitmap; + } + + /// + /// 生成二维码并保存到文件 + /// + /// 内容 + /// 文件路径 + /// 配置 + public static void GenerateToFile(string content, string filePath, QrCodeOptions? options = null) + { + using var bitmap = Generate(content, options); + var format = GetImageFormat(filePath); + bitmap.Save(filePath, format); + } + + /// + /// 生成二维码并返回Base64字符串 + /// + /// 内容 + /// 配置 + /// 图像格式 + /// Base64字符串 + public static string GenerateToBase64(string content, QrCodeOptions? options = null, ImageFormat? format = null) + { + using var bitmap = Generate(content, options); + using var ms = new MemoryStream(); + bitmap.Save(ms, format ?? ImageFormat.Png); + return Convert.ToBase64String(ms.ToArray()); + } + + /// + /// 生成二维码并返回Data URI + /// + /// 内容 + /// 配置 + /// 图像格式 + /// Data URI字符串 + public static string GenerateToDataUri(string content, QrCodeOptions? options = null, ImageFormat? format = null) + { + format ??= ImageFormat.Png; + var base64 = GenerateToBase64(content, options, format); + var mimeType = GetMimeType(format); + return $"data:{mimeType};base64,{base64}"; + } + + /// + /// 生成带Logo的二维码 + /// + /// 内容 + /// Logo路径 + /// 配置 + /// Logo占二维码比例(0.1-0.3) + /// 二维码图像 + public static Bitmap GenerateWithLogo(string content, string logoPath, QrCodeOptions? options = null, double logoRatio = 0.2) + { + using var logo = Image.FromFile(logoPath); + return GenerateWithLogo(content, logo, options, logoRatio); + } + + /// + /// 生成带Logo的二维码 + /// + /// 内容 + /// Logo图像 + /// 配置 + /// Logo占二维码比例 + /// 二维码图像 + public static Bitmap GenerateWithLogo(string content, Image logo, QrCodeOptions? options = null, double logoRatio = 0.2) + { + var bitmap = Generate(content, options); + options ??= new QrCodeOptions(); + + using (var g = Graphics.FromImage(bitmap)) + { + var logoSize = (int)(Math.Min(options.Width, options.Height) * logoRatio); + var logoX = (options.Width - logoSize) / 2; + var logoY = (options.Height - logoSize) / 2; + + // 绘制白色背景 + g.FillRectangle(Brushes.White, logoX - 2, logoY - 2, logoSize + 4, logoSize + 4); + + // 绘制Logo + g.DrawImage(logo, logoX, logoY, logoSize, logoSize); + } + + return bitmap; + } + + #endregion + + #region QR码矩阵生成 + + private static bool[,] GenerateQrMatrix(byte[] data, QrCodeErrorCorrection errorCorrection) + { + // 简化实现:生成基础QR码矩阵 + // 实际应用中建议使用专门的QR码库如 QRCoder 或 ZXing + + // 确定版本(基于数据长度) + int version = DetermineVersion(data.Length, errorCorrection); + + // 计算模块数(版本1为21,每增加1版本增加4个模块) + int size = 21 + (version - 1) * 4; + + // 创建矩阵 + var matrix = new bool[size, size]; + + // 添加定位图案 + AddFinderPatterns(matrix, size); + + // 添加对齐图案(版本2及以上) + if (version >= 2) + { + AddAlignmentPatterns(matrix, size, version); + } + + // 添加时序图案 + AddTimingPatterns(matrix, size); + + // 添加格式信息区域 + AddFormatInfoAreas(matrix, size); + + // 填充数据(简化实现) + FillData(matrix, size, data); + + return matrix; + } + + private static int DetermineVersion(int dataLength, QrCodeErrorCorrection errorCorrection) + { + // 简化版本确定 + var capacities = new int[] { 17, 32, 53, 78, 106, 134, 154, 192, 230, 271 }; + var reduction = errorCorrection switch + { + QrCodeErrorCorrection.Low => 0, + QrCodeErrorCorrection.Medium => 1, + QrCodeErrorCorrection.Quartile => 2, + QrCodeErrorCorrection.High => 3, + _ => 1 + }; + + for (int v = 0; v < capacities.Length; v++) + { + var capacity = capacities[v] - reduction * (v + 1) * 5; + if (capacity >= dataLength) + return v + 1; + } + + return 10; // 最大版本 + } + + private static void AddFinderPatterns(bool[,] matrix, int size) + { + int patternSize = 7; + + // 左上角 + DrawFinderPattern(matrix, 0, 0); + // 右上角 + DrawFinderPattern(matrix, size - patternSize, 0); + // 左下角 + DrawFinderPattern(matrix, 0, size - patternSize); + } + + private static void DrawFinderPattern(bool[,] matrix, int startX, int startY) + { + // 外框(7x7黑) + for (int i = 0; i < 7; i++) + { + for (int j = 0; j < 7; j++) + { + if (i == 0 || i == 6 || j == 0 || j == 6 || + (i >= 2 && i <= 4 && j >= 2 && j <= 4)) + { + matrix[startX + i, startY + j] = true; + } + } + } + } + + private static void AddAlignmentPatterns(bool[,] matrix, int size, int version) + { + // 简化:仅在右下角添加一个对齐图案 + if (version >= 2) + { + var positions = GetAlignmentPositions(version); + foreach (var pos in positions) + { + if (pos.X > 7 && pos.Y > 7) // 避免与定位图案重叠 + { + DrawAlignmentPattern(matrix, pos.X - 2, pos.Y - 2); + } + } + } + } + + private static List<(int X, int Y)> GetAlignmentPositions(int version) + { + var positions = new List<(int, int)>(); + int size = 21 + (version - 1) * 4; + + if (version >= 2) + { + positions.Add((size - 7, size - 7)); + } + + return positions; + } + + private static void DrawAlignmentPattern(bool[,] matrix, int startX, int startY) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + if (i == 0 || i == 4 || j == 0 || j == 4 || (i == 2 && j == 2)) + { + matrix[startX + i, startY + j] = true; + } + } + } + } + + private static void AddTimingPatterns(bool[,] matrix, int size) + { + // 水平时序图案 + for (int i = 8; i < size - 8; i++) + { + matrix[i, 6] = i % 2 == 0; + } + + // 垂直时序图案 + for (int i = 8; i < size - 8; i++) + { + matrix[6, i] = i % 2 == 0; + } + } + + private static void AddFormatInfoAreas(bool[,] matrix, int size) + { + // 格式信息区域标记(简化) + for (int i = 0; i < 9; i++) + { + if (i != 6) // 避开时序图案 + { + matrix[8, i] = false; + matrix[i, 8] = false; + } + } + } + + private static void FillData(bool[,] matrix, int size, byte[] data) + { + // 简化数据填充 + int dataIndex = 0; + bool upward = true; + + for (int col = size - 1; col >= 0; col -= 2) + { + if (col == 6) col--; // 跳过时序图案列 + + for (int i = 0; i < size; i++) + { + int row = upward ? size - 1 - i : i; + + for (int c = 0; c < 2; c++) + { + int currentCol = col - c; + + if (!IsReserved(currentCol, row, size)) + { + if (dataIndex < data.Length * 8) + { + int byteIndex = dataIndex / 8; + int bitIndex = 7 - (dataIndex % 8); + matrix[currentCol, row] = ((data[byteIndex] >> bitIndex) & 1) == 1; + dataIndex++; + } + else + { + matrix[currentCol, row] = false; + } + } + } + } + + upward = !upward; + } + } + + private static bool IsReserved(int x, int y, int size) + { + // 检查定位图案区域 + if ((x < 9 && y < 9) || (x < 9 && y >= size - 8) || (x >= size - 8 && y < 9)) + return true; + + // 检查时序图案 + if (x == 6 || y == 6) + return true; + + return false; + } + + #endregion + + #region 辅助方法 + + private static ImageFormat GetImageFormat(string filePath) + { + var ext = Path.GetExtension(filePath).ToLower(); + return ext switch + { + ".jpg" or ".jpeg" => ImageFormat.Jpeg, + ".gif" => ImageFormat.Gif, + ".bmp" => ImageFormat.Bmp, + ".tiff" => ImageFormat.Tiff, + _ => ImageFormat.Png + }; + } + + private static string GetMimeType(ImageFormat format) + { + if (format.Equals(ImageFormat.Jpeg)) + return "image/jpeg"; + if (format.Equals(ImageFormat.Gif)) + return "image/gif"; + if (format.Equals(ImageFormat.Bmp)) + return "image/bmp"; + if (format.Equals(ImageFormat.Tiff)) + return "image/tiff"; + return "image/png"; + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/ResourceUtil.cs b/EasyTool.Core/IOCategory/ResourceUtil.cs new file mode 100644 index 0000000..c62802a --- /dev/null +++ b/EasyTool.Core/IOCategory/ResourceUtil.cs @@ -0,0 +1,415 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// 嵌入资源工具类 + /// 提供程序集嵌入资源的读取和管理功能 + /// + public static class ResourceUtil + { + #region 读取嵌入资源 + + /// + /// 读取嵌入资源为字符串 + /// + /// 资源名称 + /// 程序集(默认为调用程序集) + /// 资源内容 + public static string ReadAsString(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + return reader.ReadToEnd(); + } + + /// + /// 读取嵌入资源为字节数组 + /// + /// 资源名称 + /// 程序集 + /// 资源数据 + public static byte[] ReadAsBytes(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var memoryStream = new MemoryStream(); + stream.CopyTo(memoryStream); + return memoryStream.ToArray(); + } + + /// + /// 获取嵌入资源流 + /// + /// 资源名称 + /// 程序集 + /// 资源流 + public static Stream? GetStream(string resourceName, Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + + // 尝试精确匹配 + var stream = assembly.GetManifestResourceStream(resourceName); + if (stream != null) + return stream; + + // 尝试模糊匹配 + var names = assembly.GetManifestResourceNames(); + var matchedName = names.FirstOrDefault(n => + n.Equals(resourceName, StringComparison.OrdinalIgnoreCase) || + n.EndsWith("." + resourceName, StringComparison.OrdinalIgnoreCase)); + + if (matchedName != null) + return assembly.GetManifestResourceStream(matchedName); + + return null; + } + + /// + /// 异步读取嵌入资源为字符串 + /// + /// 资源名称 + /// 程序集 + /// 资源内容 + public static async Task ReadAsStringAsync(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + return await reader.ReadToEndAsync(); + } + + /// + /// 异步读取嵌入资源为字节数组 + /// + /// 资源名称 + /// 程序集 + /// 资源数据 + public static async Task ReadAsBytesAsync(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var memoryStream = new MemoryStream(); + await stream.CopyToAsync(memoryStream); + return memoryStream.ToArray(); + } + + #endregion + + #region 读取行 + + /// + /// 读取嵌入资源的所有行 + /// + /// 资源名称 + /// 程序集 + /// 行列表 + public static List ReadAllLines(string resourceName, Assembly? assembly = null) + { + var content = ReadAsString(resourceName, assembly); + return content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList(); + } + + /// + /// 逐行读取嵌入资源 + /// + /// 资源名称 + /// 程序集 + /// 行枚举 + public static IEnumerable ReadLines(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + using var reader = new StreamReader(stream, Encoding.UTF8); + while (!reader.EndOfStream) + { + yield return reader.ReadLine() ?? string.Empty; + } + } + + #endregion + + #region 资源信息 + + /// + /// 获取程序集中所有嵌入资源名称 + /// + /// 程序集 + /// 资源名称列表 + public static string[] GetResourceNames(Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + return assembly.GetManifestResourceNames(); + } + + /// + /// 检查嵌入资源是否存在 + /// + /// 资源名称 + /// 程序集 + /// 是否存在 + public static bool Exists(string resourceName, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + return stream != null; + } + + /// + /// 获取嵌入资源信息 + /// + /// 资源名称 + /// 程序集 + /// 资源信息 + public static ResourceInfo? GetResourceInfo(string resourceName, Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + + var names = assembly.GetManifestResourceNames(); + var matchedName = names.FirstOrDefault(n => + n.Equals(resourceName, StringComparison.OrdinalIgnoreCase) || + n.EndsWith("." + resourceName, StringComparison.OrdinalIgnoreCase)); + + if (matchedName == null) + return null; + + using var stream = assembly.GetManifestResourceStream(matchedName); + if (stream == null) + return null; + + return new ResourceInfo + { + FullName = matchedName, + ShortName = GetShortName(matchedName), + Size = stream.Length, + Assembly = assembly + }; + } + + private static string GetShortName(string fullName) + { + var parts = fullName.Split('.'); + if (parts.Length >= 2) + { + return parts[parts.Length - 1]; + } + return fullName; + } + + #endregion + + #region 提取资源 + + /// + /// 将嵌入资源提取到文件 + /// + /// 资源名称 + /// 输出文件路径 + /// 程序集 + public static void ExtractToFile(string resourceName, string outputPath, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + var directory = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var fileStream = File.Create(outputPath); + stream.CopyTo(fileStream); + } + + /// + /// 异步将嵌入资源提取到文件 + /// + /// 资源名称 + /// 输出文件路径 + /// 程序集 + public static async Task ExtractToFileAsync(string resourceName, string outputPath, Assembly? assembly = null) + { + using var stream = GetStream(resourceName, assembly); + if (stream == null) + throw new FileNotFoundException($"嵌入资源未找到: {resourceName}"); + + var directory = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var fileStream = File.Create(outputPath); + await stream.CopyToAsync(fileStream); + } + + /// + /// 将所有嵌入资源提取到目录 + /// + /// 输出目录 + /// 程序集 + /// 资源名称过滤器 + /// 提取的文件数量 + public static int ExtractAllToDirectory(string outputDirectory, Assembly? assembly = null, Func? filter = null) + { + assembly ??= Assembly.GetCallingAssembly(); + var names = assembly.GetManifestResourceNames(); + int count = 0; + + if (!Directory.Exists(outputDirectory)) + { + Directory.CreateDirectory(outputDirectory); + } + + foreach (var name in names) + { + if (filter != null && !filter(name)) + continue; + + var shortName = GetShortName(name); + var outputPath = Path.Combine(outputDirectory, shortName); + + try + { + ExtractToFile(name, outputPath, assembly); + count++; + } + catch + { + // 忽略提取失败的资源 + } + } + + return count; + } + + #endregion + + #region 类型化资源 + + /// + /// 读取嵌入资源并反序列化为对象 + /// + /// 对象类型 + /// 资源名称 + /// 程序集 + /// 反序列化的对象 + public static T? ReadAsJson(string resourceName, Assembly? assembly = null) + { + var json = ReadAsString(resourceName, assembly); + return System.Text.Json.JsonSerializer.Deserialize(json); + } + + /// + /// 异步读取嵌入资源并反序列化为对象 + /// + /// 对象类型 + /// 资源名称 + /// 程序集 + /// 反序列化的对象 + public static async Task ReadAsJsonAsync(string resourceName, Assembly? assembly = null) + { + var json = await ReadAsStringAsync(resourceName, assembly); + return System.Text.Json.JsonSerializer.Deserialize(json); + } + + #endregion + + #region 快捷方法 + + /// + /// 从当前程序集读取嵌入资源 + /// + /// 资源名称 + /// 资源内容 + public static string Read(string resourceName) + { + return ReadAsString(resourceName, Assembly.GetCallingAssembly()); + } + + /// + /// 从指定类型所在程序集读取嵌入资源 + /// + /// 类型 + /// 资源名称 + /// 资源内容 + public static string ReadFromAssemblyOf(string resourceName) + { + return ReadAsString(resourceName, typeof(T).Assembly); + } + + /// + /// 从类型所在程序集读取嵌入资源(资源名基于类型命名空间) + /// + /// 类型 + /// 相对资源名称 + /// 资源内容 + public static string ReadRelativeToType(Type type, string relativeName) + { + var ns = type.Namespace ?? string.Empty; + var resourceName = string.IsNullOrEmpty(ns) ? relativeName : $"{ns}.{relativeName}"; + return ReadAsString(resourceName, type.Assembly); + } + + /// + /// 从类型所在程序集读取嵌入资源 + /// + /// 类型 + /// 相对资源名称 + /// 资源内容 + public static string ReadRelativeToType(string relativeName) + { + return ReadRelativeToType(typeof(T), relativeName); + } + + #endregion + } + + /// + /// 资源信息 + /// + public class ResourceInfo + { + /// + /// 资源完整名称 + /// + public string? FullName { get; set; } + + /// + /// 资源短名称 + /// + public string? ShortName { get; set; } + + /// + /// 资源大小(字节) + /// + public long Size { get; set; } + + /// + /// 所在程序集 + /// + public Assembly? Assembly { get; set; } + + public override string ToString() + { + return $"{ShortName} ({Size} bytes)"; + } + } +} diff --git a/EasyTool.Core/IOCategory/SerializeUtil.cs b/EasyTool.Core/IOCategory/SerializeUtil.cs new file mode 100644 index 0000000..1336374 --- /dev/null +++ b/EasyTool.Core/IOCategory/SerializeUtil.cs @@ -0,0 +1,202 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.IOCategory +{ + /// + /// 序列化工具类 + /// + public static class SerializeUtil + { + #region 二进制序列化 + + /// + /// 二进制序列化 + /// + public static byte[] Serialize(T obj) + { + using var stream = new MemoryStream(); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + formatter.Serialize(stream, obj!); + return stream.ToArray(); + } + + /// + /// 二进制反序列化 + /// + public static T? Deserialize(byte[] data) + { + using var stream = new MemoryStream(data); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + return (T?)formatter.Deserialize(stream); + } + + /// + /// 序列化到文件 + /// + public static void SerializeToFile(T obj, string filePath) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + using var stream = File.Create(filePath); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + formatter.Serialize(stream, obj!); + } + + /// + /// 从文件反序列化 + /// + public static T? DeserializeFromFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + using var stream = File.OpenRead(filePath); + var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + return (T?)formatter.Deserialize(stream); + } + + #endregion + + #region Base64 + + /// + /// 对象转Base64字符串 + /// + public static string ToBase64(T obj) + { + var data = Serialize(obj); + return Convert.ToBase64String(data); + } + + /// + /// Base64字符串转对象 + /// + public static T? FromBase64(string base64) + { + var data = Convert.FromBase64String(base64); + return Deserialize(data); + } + + #endregion + + #region JSON + + /// + /// JSON序列化 + /// + public static string ToJson(T obj, bool indented = false) + { + var options = new System.Text.Json.JsonSerializerOptions + { + WriteIndented = indented, + PropertyNamingPolicy = null + }; + return System.Text.Json.JsonSerializer.Serialize(obj, options); + } + + /// + /// JSON反序列化 + /// + public static T? FromJson(string json) + { + var options = new System.Text.Json.JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + return System.Text.Json.JsonSerializer.Deserialize(json, options); + } + + /// + /// JSON序列化到文件 + /// + public static void ToJsonFile(T obj, string filePath, bool indented = false) + { + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + var json = ToJson(obj, indented); + File.WriteAllText(filePath, json, Encoding.UTF8); + } + + /// + /// 从文件反序列化JSON + /// + public static T? FromJsonFile(string filePath) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException("文件不存在", filePath); + + var json = File.ReadAllText(filePath, Encoding.UTF8); + return FromJson(json); + } + + #endregion + + #region 深拷贝 + + /// + /// 深拷贝对象 + /// + public static T DeepClone(T obj) + { + if (obj == null) + return default!; + + // 使用JSON序列化实现深拷贝 + var json = ToJson(obj); + return FromJson(json)!; + } + + /// + /// 尝试深拷贝 + /// + public static bool TryDeepClone(T obj, out T? clone) + { + try + { + clone = DeepClone(obj); + return true; + } + catch + { + clone = default; + return false; + } + } + + #endregion + + #region 对象比较 + + /// + /// 比较两个对象是否相等(通过序列化比较) + /// + public static bool Equals(T obj1, T obj2) + { + if (obj1 == null && obj2 == null) + return true; + if (obj1 == null || obj2 == null) + return false; + + return ToJson(obj1) == ToJson(obj2); + } + + /// + /// 获取对象的哈希值 + /// + public static int GetHashCode(T obj) + { + if (obj == null) + return 0; + + return ToJson(obj).GetHashCode(); + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/StreamExtension.cs b/EasyTool.Core/IOCategory/StreamExtension.cs new file mode 100644 index 0000000..bad7a17 --- /dev/null +++ b/EasyTool.Core/IOCategory/StreamExtension.cs @@ -0,0 +1,295 @@ +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.IOCategory +{ + /// + /// Stream 扩展方法 + /// + public static class StreamExtension + { + #region 读取操作 + + /// + /// 读取流中的所有字节 + /// + public static byte[] ReadAllBytes(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + using var ms = new MemoryStream(); + stream.CopyTo(ms); + return ms.ToArray(); + } + + /// + /// 异步读取流中的所有字节 + /// + public static async Task ReadAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + using var ms = new MemoryStream(); + await stream.CopyToAsync(ms, cancellationToken); + return ms.ToArray(); + } + + /// + /// 读取流中的所有文本(UTF-8 编码) + /// + public static string ReadAllText(this Stream stream) + { + return stream.ReadAllText(Encoding.UTF8); + } + + /// + /// 读取流中的所有文本 + /// + public static string ReadAllText(this Stream stream, Encoding encoding) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + return reader.ReadToEnd(); + } + + /// + /// 异步读取流中的所有文本(UTF-8 编码) + /// + public static Task ReadAllTextAsync(this Stream stream, CancellationToken cancellationToken = default) + { + return stream.ReadAllTextAsync(Encoding.UTF8, cancellationToken); + } + + /// + /// 异步读取流中的所有文本 + /// + public static async Task ReadAllTextAsync(this Stream stream, Encoding encoding, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + return await reader.ReadToEndAsync(); + } + + /// + /// 读取流中的所有行 + /// + public static string[] ReadAllLines(this Stream stream, Encoding? encoding = null) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + encoding ??= Encoding.UTF8; + + using var reader = new StreamReader(stream, encoding, true); + var lines = new System.Collections.Generic.List(); + string? line; + while ((line = reader.ReadLine()) != null) + { + lines.Add(line); + } + return lines.ToArray(); + } + + #endregion + + #region 写入操作 + + /// + /// 将字节写入流 + /// + public static void WriteBytes(this Stream stream, byte[] bytes) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (bytes == null || bytes.Length == 0) + return; + + stream.Write(bytes, 0, bytes.Length); + } + + /// + /// 异步将字节写入流 + /// + public static async Task WriteBytesAsync(this Stream stream, byte[] bytes, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (bytes == null || bytes.Length == 0) + return; + + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + } + + /// + /// 将文本写入流(UTF-8 编码) + /// + public static void WriteText(this Stream stream, string text) + { + stream.WriteText(text, Encoding.UTF8); + } + + /// + /// 将文本写入流 + /// + public static void WriteText(this Stream stream, string text, Encoding encoding) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (string.IsNullOrEmpty(text)) + return; + + encoding ??= Encoding.UTF8; + + var bytes = encoding.GetBytes(text); + stream.Write(bytes, 0, bytes.Length); + } + + /// + /// 异步将文本写入流(UTF-8 编码) + /// + public static Task WriteTextAsync(this Stream stream, string text, CancellationToken cancellationToken = default) + { + return stream.WriteTextAsync(text, Encoding.UTF8, cancellationToken); + } + + /// + /// 异步将文本写入流 + /// + public static async Task WriteTextAsync(this Stream stream, string text, Encoding encoding, CancellationToken cancellationToken = default) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (string.IsNullOrEmpty(text)) + return; + + encoding ??= Encoding.UTF8; + + var bytes = encoding.GetBytes(text); + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); + } + + #endregion + + #region 复制操作 + + + /// + /// 将流复制到字节数组 + /// + public static byte[] CopyToByteArray(this Stream stream) + { + return stream.ReadAllBytes(); + } + + /// + /// 将流复制到内存流 + /// + public static MemoryStream CopyToMemoryStream(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var ms = new MemoryStream(); + stream.CopyTo(ms); + return ms; + } + + #endregion + + #region 位置操作 + + + #endregion + + #region 转换操作 + + /// + /// 将流转为 Base64 字符串 + /// + public static string ToBase64(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var bytes = stream.ReadAllBytes(); + return Convert.ToBase64String(bytes); + } + + /// + /// 将流转为十六进制字符串 + /// + public static string ToHex(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + var bytes = stream.ReadAllBytes(); + return BitConverter.ToString(bytes).Replace("-", "").ToLowerInvariant(); + } + + #endregion + + #region 检查操作 + + /// + /// 判断流是否为空 + /// + public static bool IsEmpty(this Stream? stream) + { + if (stream == null) + return true; + + if (stream.CanSeek) + { + return stream.Length == 0; + } + + return stream.ReadByte() == -1; + } + + #endregion + + #region 缓冲操作 + + /// + /// 使用缓冲读取器包装流 + /// + public static BufferedStream Buffer(this Stream stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + return new BufferedStream(stream); + } + + /// + /// 使用缓冲读取器包装流(指定缓冲区大小) + /// + public static BufferedStream Buffer(this Stream stream, int bufferSize) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + return new BufferedStream(stream, bufferSize); + } + + #endregion + } +} diff --git a/EasyTool.Core/IOCategory/Tailer.cs b/EasyTool.Core/IOCategory/Tailer.cs index b8df260..2fd0380 100644 --- a/EasyTool.Core/IOCategory/Tailer.cs +++ b/EasyTool.Core/IOCategory/Tailer.cs @@ -1,10 +1,10 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件跟随工具类 @@ -16,7 +16,7 @@ public class Tailer : IDisposable private readonly Timer timer; // 定时器,用于定期检查文件是否有新内容 // 定义事件,用于通知外部监听器 - public event EventHandler NewLine; + public event EventHandler? NewLine; // 构造函数,初始化文件路径、StreamReader 和定时器 public Tailer(string filePath) @@ -48,7 +48,7 @@ private void OnTimerCallback(object state) return; // 逐行读取文件内容 - string line; + string? line; while ((line = reader.ReadLine()) != null) { // 如果有新行,触发 NewLine 事件 diff --git a/EasyTool.Core/IOCategory/TempFileUtil.cs b/EasyTool.Core/IOCategory/TempFileUtil.cs new file mode 100644 index 0000000..62a6e39 --- /dev/null +++ b/EasyTool.Core/IOCategory/TempFileUtil.cs @@ -0,0 +1,269 @@ +using System; +using System.IO; + +namespace EasyTool.IOCategory +{ + /// + /// 临时文件工具类 + /// + public static class TempFileUtil + { + private static readonly object _lock = new(); + private static readonly string _tempDirectory = Path.Combine(Path.GetTempPath(), "EasyTool_Temp"); + + /// + /// 临时目录 + /// + public static string TempDirectory + { + get + { + if (!Directory.Exists(_tempDirectory)) + Directory.CreateDirectory(_tempDirectory); + return _tempDirectory; + } + } + + /// + /// 创建临时文件 + /// + public static string CreateTempFile(string? extension = null, string? prefix = null) + { + var fileName = $"{prefix ?? "temp"}_{Guid.NewGuid():N}{extension ?? ".tmp"}"; + var filePath = Path.Combine(TempDirectory, fileName); + File.Create(filePath).Dispose(); + return filePath; + } + + /// + /// 创建临时目录 + /// + public static string CreateTempDirectory(string? prefix = null) + { + var dirName = $"{prefix ?? "temp"}_{Guid.NewGuid():N}"; + var dirPath = Path.Combine(TempDirectory, dirName); + Directory.CreateDirectory(dirPath); + return dirPath; + } + + /// + /// 创建临时文件并写入内容 + /// + public static string CreateTempFileWithContent(string content, string? extension = null, string? prefix = null) + { + var filePath = CreateTempFile(extension, prefix); + File.WriteAllText(filePath, content); + return filePath; + } + + /// + /// 创建临时文件并写入二进制内容 + /// + public static string CreateTempFileWithBytes(byte[] bytes, string? extension = null, string? prefix = null) + { + var filePath = CreateTempFile(extension, prefix); + File.WriteAllBytes(filePath, bytes); + return filePath; + } + + /// + /// 删除临时文件 + /// + public static bool DeleteTempFile(string filePath) + { + try + { + if (File.Exists(filePath)) + { + File.Delete(filePath); + return true; + } + return false; + } + catch + { + return false; + } + } + + /// + /// 删除临时目录 + /// + public static bool DeleteTempDirectory(string dirPath) + { + try + { + if (Directory.Exists(dirPath)) + { + Directory.Delete(dirPath, true); + return true; + } + return false; + } + catch + { + return false; + } + } + + /// + /// 清理所有临时文件 + /// + public static void CleanupAll() + { + lock (_lock) + { + if (Directory.Exists(_tempDirectory)) + { + try + { + Directory.Delete(_tempDirectory, true); + } + catch + { + // 忽略清理错误 + } + } + } + } + + /// + /// 清理过期的临时文件 + /// + public static void CleanupExpired(TimeSpan expiration) + { + if (!Directory.Exists(_tempDirectory)) + return; + + var cutoff = DateTime.Now - expiration; + + foreach (var file in Directory.GetFiles(_tempDirectory)) + { + try + { + if (File.GetCreationTime(file) < cutoff) + File.Delete(file); + } + catch + { + // 忽略单个文件删除错误 + } + } + + foreach (var dir in Directory.GetDirectories(_tempDirectory)) + { + try + { + if (Directory.GetCreationTime(dir) < cutoff) + Directory.Delete(dir, true); + } + catch + { + // 忽略单个目录删除错误 + } + } + } + + /// + /// 获取临时文件大小 + /// + public static long GetTempDirectorySize() + { + if (!Directory.Exists(_tempDirectory)) + return 0; + + long size = 0; + foreach (var file in Directory.GetFiles(_tempDirectory, "*", SearchOption.AllDirectories)) + { + try + { + size += new FileInfo(file).Length; + } + catch + { + // 忽略单个文件错误 + } + } + return size; + } + + /// + /// 获取临时文件数量 + /// + public static int GetTempFileCount() + { + if (!Directory.Exists(_tempDirectory)) + return 0; + + return Directory.GetFiles(_tempDirectory, "*", SearchOption.AllDirectories).Length; + } + } + + /// + /// 临时文件自动清理器 + /// + public class TempFileScope : IDisposable + { + private string? _filePath; + private string? _directoryPath; + private bool _disposed; +#pragma warning disable CS0414 // 字段保留供扩展使用 + private readonly bool _isDirectory; +#pragma warning restore CS0414 + + /// + /// 创建临时文件作用域 + /// + public TempFileScope(string? extension = null, string? prefix = null) + { + _filePath = TempFileUtil.CreateTempFile(extension, prefix); + _isDirectory = false; + } + + private TempFileScope(bool isDirectory, string? prefix) + { + if (isDirectory) + { + _directoryPath = TempFileUtil.CreateTempDirectory(prefix); + _isDirectory = true; + } + else + { + _filePath = TempFileUtil.CreateTempFile(null, prefix); + _isDirectory = false; + } + } + + /// + /// 创建临时目录作用域 + /// + public static TempFileScope CreateDirectoryScope(string? prefix = null) + { + return new TempFileScope(true, prefix); + } + + /// + /// 临时文件路径 + /// + public string FilePath => _filePath ?? throw new InvalidOperationException("这不是文件作用域"); + + /// + /// 临时目录路径 + /// + public string DirectoryPath => _directoryPath ?? throw new InvalidOperationException("这不是目录作用域"); + + public void Dispose() + { + if (_disposed) + return; + + if (_filePath != null) + TempFileUtil.DeleteTempFile(_filePath); + + if (_directoryPath != null) + TempFileUtil.DeleteTempDirectory(_directoryPath); + + _disposed = true; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/IOCategory/TomlUtil.cs b/EasyTool.Core/IOCategory/TomlUtil.cs new file mode 100644 index 0000000..7c12ba2 --- /dev/null +++ b/EasyTool.Core/IOCategory/TomlUtil.cs @@ -0,0 +1,438 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.IOCategory +{ + /// + /// TOML 工具类 + /// 提供 TOML 配置文件的读写功能 + /// + public static class TomlUtil + { + /// + /// 将对象序列化为 TOML 字符串 + /// + public static string Serialize(object obj) + { + var serializer = new TomlSerializer(); + return serializer.Serialize(obj); + } + + /// + /// 将 TOML 字符串反序列化为字典 + /// + public static Dictionary Deserialize(string toml) + { + var deserializer = new TomlDeserializer(); + return deserializer.Deserialize(toml); + } + + /// + /// 从文件读取 TOML + /// + public static Dictionary ReadFile(string filePath) + { + var content = File.ReadAllText(filePath, Encoding.UTF8); + return Deserialize(content); + } + + /// + /// 将对象写入 TOML 文件 + /// + public static void WriteFile(string filePath, object obj) + { + var toml = Serialize(obj); + File.WriteAllText(filePath, toml, Encoding.UTF8); + } + } + + /// + /// TOML 序列化器 + /// + public class TomlSerializer + { + private readonly StringBuilder _sb; + + /// + /// 创建 TOML 序列化器 + /// + public TomlSerializer() + { + _sb = new StringBuilder(); + } + + /// + /// 序列化对象 + /// + public string Serialize(object obj) + { + _sb.Clear(); + SerializeValue(obj, ""); + return _sb.ToString(); + } + + private void SerializeValue(object value, string prefix) + { + if (value == null) + return; + + var type = value.GetType(); + + if (value is IDictionary dict) + { + SerializeDictionary(new Dictionary(dict), prefix); + } + else if (value is IList list) + { + SerializeArray(new List(list), prefix); + } + else if (type.IsPrimitive || value is string || value is decimal || value is DateTime) + { + // 简单值不单独序列化 + } + else + { + // 复杂对象,反射属性 + var props = type.GetProperties(); + var objDict = new Dictionary(); + foreach (var prop in props) + { + if (prop.CanRead) + { + objDict[prop.Name] = prop.GetValue(value); + } + } + SerializeDictionary(objDict, prefix); + } + } + + private void SerializeDictionary(Dictionary dict, string prefix) + { + var simpleValues = new List>(); + var complexValues = new List>(); + + foreach (var kvp in dict) + { + if (IsSimpleValue(kvp.Value)) + simpleValues.Add(kvp); + else + complexValues.Add(kvp); + } + + // 先输出简单值 + if (!string.IsNullOrEmpty(prefix) && simpleValues.Count > 0) + { + _sb.AppendLine($"[{prefix}]"); + } + + foreach (var kvp in simpleValues) + { + _sb.AppendLine($"{kvp.Key} = {FormatValue(kvp.Value)}"); + } + + if (simpleValues.Count > 0 && complexValues.Count > 0) + _sb.AppendLine(); + + // 处理复杂值 + foreach (var kvp in complexValues) + { + string newPrefix = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}.{kvp.Key}"; + SerializeValue(kvp.Value, newPrefix); + } + } + + private void SerializeArray(List list, string prefix) + { + if (!string.IsNullOrEmpty(prefix)) + { + _sb.AppendLine($"[[{prefix}]]"); + } + + foreach (var item in list) + { + if (IsSimpleValue(item)) + { + _sb.AppendLine(FormatValue(item)); + } + else if (item is Dictionary dict) + { + foreach (var kvp in dict) + { + _sb.AppendLine($"{kvp.Key} = {FormatValue(kvp.Value)}"); + } + _sb.AppendLine(); + } + } + } + + private static bool IsSimpleValue(object value) + { + if (value == null) return true; + var type = value.GetType(); + return type.IsPrimitive || value is string || value is decimal || value is DateTime; + } + + private static string FormatValue(object value) + { + if (value == null) return "\"\""; + + if (value is string str) + { + if (str.Contains("\n")) + return $"\"\"\"\n{str}\n\"\"\""; + if (str.Contains("\"") || str.Contains("'") || str.Contains("\\")) + return $"\"{str.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""; + return $"\"{str}\""; + } + + if (value is bool b) return b ? "true" : "false"; + if (value is DateTime dt) return dt.ToString("yyyy-MM-ddTHH:mm:ssZ"); + if (value is double d) return d.ToString(System.Globalization.CultureInfo.InvariantCulture); + if (value is float f) return f.ToString(System.Globalization.CultureInfo.InvariantCulture); + + return value.ToString(); + } + } + + /// + /// TOML 反序列化器 + /// + public class TomlDeserializer + { + /// + /// 反序列化 TOML 字符串 + /// + public Dictionary Deserialize(string toml) + { + var result = new Dictionary(); + var lines = toml.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + + string currentSection = ""; + var currentDict = result; + + for (int i = 0; i < lines.Length; i++) + { + string line = lines[i].Trim(); + + // 跳过空行和注释 + if (string.IsNullOrEmpty(line) || line.StartsWith("#")) + continue; + + // 表头 + if (line.StartsWith("[") && line.EndsWith("]")) + { + string sectionName = line.Substring(1, line.Length - 2).Trim(); + if (sectionName.StartsWith("[[") && sectionName.EndsWith("]]")) + { + // 数组表 + sectionName = sectionName.Substring(2, sectionName.Length - 4).Trim(); + currentSection = sectionName; + var list = GetOrCreateArray(result, sectionName); + currentDict = new Dictionary(); + list.Add(currentDict); + } + else + { + currentSection = sectionName; + currentDict = GetOrCreateDictionary(result, sectionName); + } + continue; + } + + // 键值对 + int equalIndex = line.IndexOf('='); + if (equalIndex > 0) + { + string key = line.Substring(0, equalIndex).Trim(); + string valueStr = line.Substring(equalIndex + 1).Trim(); + + // 处理行内注释 + int commentIndex = valueStr.IndexOf(" #"); + if (commentIndex > 0) + { + valueStr = valueStr.Substring(0, commentIndex).Trim(); + } + + object value = ParseValue(valueStr, lines, ref i); + currentDict[key] = value; + } + } + + return result; + } + + private static List> GetOrCreateArray(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + for (int i = 0; i < parts.Length - 1; i++) + { + if (!current.TryGetValue(parts[i], out var obj) || !(obj is Dictionary dict)) + { + dict = new Dictionary(); + current[parts[i]] = dict; + } + current = dict; + } + + string lastKey = parts[parts.Length - 1]; + if (!current.TryGetValue(lastKey, out var listObj) || !(listObj is List> list)) + { + list = new List>(); + current[lastKey] = list; + } + + return list; + } + + private static Dictionary GetOrCreateDictionary(Dictionary root, string path) + { + var parts = path.Split('.'); + var current = root; + + foreach (var part in parts) + { + if (!current.TryGetValue(part, out var obj) || !(obj is Dictionary dict)) + { + dict = new Dictionary(); + current[part] = dict; + } + current = dict; + } + + return current; + } + + private static object ParseValue(string valueStr, string[] lines, ref int lineIndex) + { + // 布尔值 + if (valueStr == "true") return true; + if (valueStr == "false") return false; + + // 数字 + if (int.TryParse(valueStr, out int intVal)) return intVal; + if (long.TryParse(valueStr, out long longVal)) return longVal; + if (double.TryParse(valueStr, out double doubleVal)) return doubleVal; + + // 字符串 + if (valueStr.StartsWith("\"\"\"")) + { + // 多行字符串 + var sb = new StringBuilder(); + lineIndex++; + while (lineIndex < lines.Length && !lines[lineIndex].Trim().EndsWith("\"\"\"")) + { + sb.AppendLine(lines[lineIndex]); + lineIndex++; + } + if (lineIndex < lines.Length) + { + string lastLine = lines[lineIndex].Trim(); + sb.Append(lastLine.Substring(0, lastLine.Length - 3)); + } + return sb.ToString(); + } + + if (valueStr.StartsWith("\"") && valueStr.EndsWith("\"")) + { + return valueStr.Substring(1, valueStr.Length - 2) + .Replace("\\\"", "\"") + .Replace("\\\\", "\\") + .Replace("\\n", "\n") + .Replace("\\t", "\t"); + } + + if (valueStr.StartsWith("'") && valueStr.EndsWith("'")) + { + return valueStr.Substring(1, valueStr.Length - 2); + } + + // 日期时间 + if (DateTime.TryParse(valueStr, out DateTime dt)) return dt; + + // 数组 + if (valueStr.StartsWith("[") && valueStr.EndsWith("]")) + { + return ParseArray(valueStr); + } + + // 内联表 + if (valueStr.StartsWith("{") && valueStr.EndsWith("}")) + { + return ParseInlineTable(valueStr); + } + + return valueStr; + } + + private static List ParseArray(string valueStr) + { + var result = new List(); + string inner = valueStr.Substring(1, valueStr.Length - 2).Trim(); + + if (string.IsNullOrEmpty(inner)) + return result; + + // 简单分割(不支持嵌套) + var parts = inner.Split(','); + foreach (var part in parts) + { + string item = part.Trim(); + if (!string.IsNullOrEmpty(item)) + { + if (item.StartsWith("\"") && item.EndsWith("\"")) + result.Add(item.Substring(1, item.Length - 2)); + else if (int.TryParse(item, out int intVal)) + result.Add(intVal); + else if (double.TryParse(item, out double doubleVal)) + result.Add(doubleVal); + else if (item == "true") + result.Add(true); + else if (item == "false") + result.Add(false); + else + result.Add(item); + } + } + + return result; + } + + private static Dictionary ParseInlineTable(string valueStr) + { + var result = new Dictionary(); + string inner = valueStr.Substring(1, valueStr.Length - 2).Trim(); + + if (string.IsNullOrEmpty(inner)) + return result; + + var parts = inner.Split(','); + foreach (var part in parts) + { + int equalIndex = part.IndexOf('='); + if (equalIndex > 0) + { + string key = part.Substring(0, equalIndex).Trim(); + string value = part.Substring(equalIndex + 1).Trim(); + + if (value.StartsWith("\"") && value.EndsWith("\"")) + result[key] = value.Substring(1, value.Length - 2); + else if (int.TryParse(value, out int intVal)) + result[key] = intVal; + else if (value == "true") + result[key] = true; + else if (value == "false") + result[key] = false; + else + result[key] = value; + } + } + + return result; + } + } +} diff --git a/EasyTool.Core/IOCategory/WatchMonitor.cs b/EasyTool.Core/IOCategory/WatchMonitor.cs index 0db8a47..6be74b7 100644 --- a/EasyTool.Core/IOCategory/WatchMonitor.cs +++ b/EasyTool.Core/IOCategory/WatchMonitor.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 文件监听工具类 @@ -13,11 +13,11 @@ public class WatchMonitor private readonly FileSystemWatcher watcher; // 定义事件,用于通知外部监听器 - public event EventHandler FileChanged; - public event EventHandler FileCreated; - public event EventHandler FileDeleted; - public event EventHandler FileMissing; - public event EventHandler FileError; + public event EventHandler? FileChanged; + public event EventHandler? FileCreated; + public event EventHandler? FileDeleted; + public event EventHandler? FileMissing; + public event EventHandler? FileError; /// /// 构造函数,初始化 FileSystemWatcher 实例 @@ -109,7 +109,7 @@ private void OnFileError(object sender, ErrorEventArgs e) { if (FileError != null) { - FileError(this, new FileEventArgs(e.GetException())); + FileError(this, new FileEventArgs(e.GetException()!)); } } @@ -132,8 +132,8 @@ public void Dispose() /// public class FileEventArgs : EventArgs { - public string FilePath { get; } - public Exception Exception { get; } + public string? FilePath { get; } + public Exception? Exception { get; } public FileEventArgs(string path) { diff --git a/EasyTool.Core/IOCategory/YamlUtil.cs b/EasyTool.Core/IOCategory/YamlUtil.cs new file mode 100644 index 0000000..a00dc46 --- /dev/null +++ b/EasyTool.Core/IOCategory/YamlUtil.cs @@ -0,0 +1,369 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.IOCategory +{ + /// + /// YAML 工具类 + /// 提供简单的 YAML 序列化和反序列化功能 + /// + public static class YamlUtil + { + /// + /// 将对象序列化为 YAML 字符串 + /// + public static string Serialize(object obj, int indentSize = 2) + { + var serializer = new YamlSerializer(indentSize); + return serializer.Serialize(obj); + } + + /// + /// 将 YAML 字符串反序列化为字典 + /// + public static Dictionary Deserialize(string yaml) + { + var deserializer = new YamlDeserializer(); + return deserializer.Deserialize(yaml); + } + + /// + /// 从文件读取 YAML + /// + public static Dictionary ReadFile(string filePath) + { + var content = File.ReadAllText(filePath, Encoding.UTF8); + return Deserialize(content); + } + + /// + /// 将对象写入 YAML 文件 + /// + public static void WriteFile(string filePath, object obj, int indentSize = 2) + { + var yaml = Serialize(obj, indentSize); + File.WriteAllText(filePath, yaml, Encoding.UTF8); + } + + /// + /// 将 YAML 字符串反序列化为指定类型 + /// + public static T Deserialize(string yaml) where T : new() + { + var dict = Deserialize(yaml); + return MapToObject(dict); + } + + private static T MapToObject(Dictionary dict) where T : new() + { + var obj = new T(); + var type = typeof(T); + + foreach (var kvp in dict) + { + var property = type.GetProperty(kvp.Key); + if (property != null && property.CanWrite) + { + var value = ConvertValue(kvp.Value, property.PropertyType); + if (value != null) + property.SetValue(obj, value); + } + } + + return obj; + } + + private static object ConvertValue(object value, Type targetType) + { + if (value == null) return null; + + if (targetType == typeof(string)) + return value.ToString(); + + if (targetType == typeof(int)) + return Convert.ToInt32(value); + + if (targetType == typeof(long)) + return Convert.ToInt64(value); + + if (targetType == typeof(double)) + return Convert.ToDouble(value); + + if (targetType == typeof(bool)) + return Convert.ToBoolean(value); + + if (targetType == typeof(DateTime)) + return Convert.ToDateTime(value); + + return value; + } + } + + /// + /// YAML 序列化器 + /// + public class YamlSerializer + { + private readonly int _indentSize; + private readonly StringBuilder _sb; + + /// + /// 创建 YAML 序列化器 + /// + public YamlSerializer(int indentSize = 2) + { + _indentSize = indentSize; + _sb = new StringBuilder(); + } + + /// + /// 序列化对象 + /// + public string Serialize(object obj) + { + _sb.Clear(); + SerializeValue(obj, 0, ""); + return _sb.ToString(); + } + + private void SerializeValue(object value, int indent, string key) + { + string indentStr = new string(' ', indent); + + if (value == null) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}: null"); + return; + } + + var type = value.GetType(); + + if (value is IDictionary dict) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + else if (indent > 0) + _sb.AppendLine($"{indentStr}:"); + + foreach (var kvp in dict) + { + SerializeValue(kvp.Value, indent + _indentSize, kvp.Key); + } + } + else if (value is IList list) + { + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + else if (indent > 0) + _sb.AppendLine($"{indentStr}:"); + + foreach (var item in list) + { + SerializeValue(item, indent + _indentSize, "-"); + } + } + else if (type.IsPrimitive || value is string || value is DateTime || value is decimal) + { + string valueStr = FormatScalar(value); + if (!string.IsNullOrEmpty(key)) + { + if (key == "-") + _sb.AppendLine($"{indentStr}- {valueStr}"); + else + _sb.AppendLine($"{indentStr}{key}: {valueStr}"); + } + } + else + { + // 复杂对象,反射属性 + if (!string.IsNullOrEmpty(key)) + _sb.AppendLine($"{indentStr}{key}:"); + + var properties = type.GetProperties(); + foreach (var prop in properties) + { + if (prop.CanRead) + { + var propValue = prop.GetValue(value); + SerializeValue(propValue, indent + _indentSize, prop.Name); + } + } + } + } + + private static string FormatScalar(object value) + { + if (value == null) return "null"; + if (value is string str) + { + if (string.IsNullOrEmpty(str)) return "\"\""; + if (str.Contains(":") || str.Contains("#") || str.Contains("\n") || str.StartsWith(" ") || str.EndsWith(" ")) + return $"\"{str.Replace("\"", "\\\"")}\""; + return str; + } + if (value is bool b) return b ? "true" : "false"; + if (value is DateTime dt) return dt.ToString("yyyy-MM-dd HH:mm:ss"); + + return value.ToString(); + } + } + + /// + /// YAML 反序列化器 + /// + public class YamlDeserializer + { + /// + /// 反序列化 YAML 字符串 + /// + public Dictionary Deserialize(string yaml) + { + var lines = yaml.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + var result = new Dictionary(); + var context = new ParseContext { Lines = lines, Index = 0 }; + int currentIndent = 0; + + ParseBlock(context, result, currentIndent); + + return result; + } + + private class ParseContext + { + public string[] Lines { get; set; } + public int Index { get; set; } + public int LineCount => Lines.Length; + public string CurrentLine => Index < LineCount ? Lines[Index] : null; + } + + private void ParseBlock(ParseContext context, Dictionary result, int baseIndent) + { + while (context.Index < context.LineCount) + { + string line = context.CurrentLine; + + // 跳过空行和注释 + if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) + { + context.Index++; + continue; + } + + int indent = GetIndent(line); + if (indent < baseIndent) break; + + string trimmed = line.TrimStart(); + + // 列表项 + if (trimmed.StartsWith("- ")) + { + var list = new List(); + while (context.Index < context.LineCount) + { + string itemLine = context.CurrentLine; + if (string.IsNullOrWhiteSpace(itemLine) || itemLine.TrimStart().StartsWith("#")) + { + context.Index++; + continue; + } + + int itemIndent = GetIndent(itemLine); + if (itemIndent < indent) break; + if (itemIndent > indent) + { + // 嵌套块 + context.Index--; + break; + } + + string itemTrimmed = itemLine.TrimStart(); + if (!itemTrimmed.StartsWith("- ")) break; + + string itemContent = itemTrimmed.Substring(2).Trim(); + if (itemContent.Contains(":")) + { + // 列表项是字典 + var itemDict = new Dictionary(); + context.Index++; + ParseBlock(context, itemDict, context.Index < context.LineCount ? GetIndent(context.CurrentLine) : indent + 2); + list.Add(itemDict); + } + else + { + list.Add(ParseScalar(itemContent)); + context.Index++; + } + } + + result["__list__"] = list; + continue; + } + + // 键值对 + int colonIndex = trimmed.IndexOf(':'); + if (colonIndex > 0) + { + string key = trimmed.Substring(0, colonIndex).Trim(); + string valueStr = trimmed.Substring(colonIndex + 1).Trim(); + + if (string.IsNullOrEmpty(valueStr)) + { + // 嵌套块 + context.Index++; + var nested = new Dictionary(); + ParseBlock(context, nested, indent + 2); + result[key] = nested; + } + else + { + result[key] = ParseScalar(valueStr); + context.Index++; + } + } + else + { + context.Index++; + } + } + } + + private static int GetIndent(string line) + { + int indent = 0; + foreach (char c in line) + { + if (c == ' ') indent++; + else if (c == '\t') indent += 2; + else break; + } + return indent; + } + + private static object ParseScalar(string value) + { + if (string.IsNullOrEmpty(value)) return null; + if (value == "null" || value == "~") return null; + if (value == "true") return true; + if (value == "false") return false; + + // 移除引号 + if ((value.StartsWith("\"") && value.EndsWith("\"")) || + (value.StartsWith("'") && value.EndsWith("'"))) + { + return value.Substring(1, value.Length - 2); + } + + // 尝试解析数字 + if (int.TryParse(value, out int intVal)) return intVal; + if (long.TryParse(value, out long longVal)) return longVal; + if (double.TryParse(value, out double doubleVal)) return doubleVal; + if (DateTime.TryParse(value, out DateTime dateVal)) return dateVal; + + return value; + } + } +} diff --git a/EasyTool.Core/ToolCategory/ZipUtil.cs b/EasyTool.Core/IOCategory/ZipUtil.cs similarity index 98% rename from EasyTool.Core/ToolCategory/ZipUtil.cs rename to EasyTool.Core/IOCategory/ZipUtil.cs index 4d3de35..ceb894d 100644 --- a/EasyTool.Core/ToolCategory/ZipUtil.cs +++ b/EasyTool.Core/IOCategory/ZipUtil.cs @@ -4,12 +4,12 @@ using System.IO; using System.Text; -namespace EasyTool +namespace EasyTool.IOCategory { /// /// 压缩工具 /// - public class ZipUtil + public static class ZipUtil { /// /// 压缩文件或目录 diff --git a/EasyTool.Core/ToolCategory/IdUtil.cs b/EasyTool.Core/IdentifierCategory/IdUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/IdUtil.cs rename to EasyTool.Core/IdentifierCategory/IdUtil.cs index 64edf2a..393e887 100644 --- a/EasyTool.Core/ToolCategory/IdUtil.cs +++ b/EasyTool.Core/IdentifierCategory/IdUtil.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; -namespace EasyTool +namespace EasyTool.IdentifierCategory { /// /// uuid生成风格 @@ -27,7 +27,7 @@ public enum UUIDStyle /// /// 唯一ID工具 /// - public class IdUtil + public static class IdUtil { private static readonly DateTime epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static int objectIdCounter = 0; diff --git a/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs b/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs new file mode 100644 index 0000000..13fa42a --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ObjectIdUtil.cs @@ -0,0 +1,539 @@ +using System; +using System.Net.NetworkInformation; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// MongoDB ObjectId 生成器 + /// ObjectId 是一个 12 字节的唯一标识符,由时间戳、机器标识、进程 ID 和计数器组成 + /// + public static class ObjectIdUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly byte[] _machineId; + private static readonly byte[] _processId; + private static int _counter; + private static readonly object _counterLock = new(); + + static ObjectIdUtil() + { + _machineId = GetMachineId(); + _processId = GetProcessId(); + _counter = GetRandomCounter(); + } + + private const int ObjectIdLength = 12; + private const int TimestampLength = 4; + private const int MachineIdLength = 3; + private const int ProcessIdLength = 2; + private const int CounterLength = 3; + + /// + /// 生成新的 ObjectId + /// + /// ObjectId 字节数组 + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 ObjectId + /// + /// 时间戳 + /// ObjectId 字节数组 + public static byte[] Generate(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + // 写入时间戳(4字节,大端序) + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + + // 写入机器标识(3字节) + Buffer.BlockCopy(_machineId, 0, objectId, 4, MachineIdLength); + + // 写入进程 ID(2字节) + Buffer.BlockCopy(_processId, 0, objectId, 7, ProcessIdLength); + + // 写入计数器(3字节,大端序) + int counter; + lock (_counterLock) + { + counter = _counter++; + if (_counter > 0xFFFFFF) + { + _counter = GetRandomCounter(); + } + } + + objectId[9] = (byte)(counter >> 16); + objectId[10] = (byte)(counter >> 8); + objectId[11] = (byte)counter; + + return objectId; + } + + /// + /// 生成新的 ObjectId 字符串 + /// + /// ObjectId 字符串(24字符十六进制) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 生成指定时间的 ObjectId 字符串 + /// + /// 时间戳 + /// ObjectId 字符串(24字符十六进制) + public static string GenerateString(DateTimeOffset timestamp) + { + return Encode(Generate(timestamp)); + } + + /// + /// 将 ObjectId 字节数组编码为十六进制字符串 + /// + /// ObjectId 字节数组 + /// ObjectId 十六进制字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(bytes)); + } + + var hex = new StringBuilder(24); + foreach (var b in bytes) + { + hex.AppendFormat("{0:x2}", b); + } + return hex.ToString(); + } + + /// + /// 将十六进制字符串解码为 ObjectId 字节数组 + /// + /// ObjectId 十六进制字符串 + /// ObjectId 字节数组 + public static byte[] Decode(string objectId) + { + if (string.IsNullOrEmpty(objectId) || objectId.Length != 24) + { + throw new ArgumentException("ObjectId 字符串长度必须为 24", nameof(objectId)); + } + + var bytes = new byte[ObjectIdLength]; + for (int i = 0; i < ObjectIdLength; i++) + { + var hex = objectId.Substring(i * 2, 2); + bytes[i] = Convert.ToByte(hex, 16); + } + return bytes; + } + + /// + /// 从 ObjectId 提取时间戳 + /// + /// ObjectId 字节数组 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + var timestampSec = (objectId[0] << 24) | + (objectId[1] << 16) | + (objectId[2] << 8) | + objectId[3]; + + return DateTimeOffset.FromUnixTimeSeconds(timestampSec); + } + + /// + /// 从 ObjectId 字符串提取时间戳 + /// + /// ObjectId 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string objectId) + { + return ExtractTimestamp(Decode(objectId)); + } + + /// + /// 从 ObjectId 提取机器标识 + /// + /// ObjectId 字节数组 + /// 机器标识(十六进制字符串) + public static string ExtractMachineId(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return $"{objectId[4]:x2}{objectId[5]:x2}{objectId[6]:x2}"; + } + + /// + /// 从 ObjectId 提取进程 ID + /// + /// ObjectId 字节数组 + /// 进程 ID + public static int ExtractProcessId(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return (objectId[7] << 8) | objectId[8]; + } + + /// + /// 从 ObjectId 提取计数器 + /// + /// ObjectId 字节数组 + /// 计数器值 + public static int ExtractCounter(byte[] objectId) + { + if (objectId == null || objectId.Length != ObjectIdLength) + { + throw new ArgumentException($"ObjectId 字节数组长度必须为 {ObjectIdLength}", nameof(objectId)); + } + + return (objectId[9] << 16) | (objectId[10] << 8) | objectId[11]; + } + + /// + /// 验证 ObjectId 字符串是否有效 + /// + /// ObjectId 字符串 + /// 是否有效 + public static bool IsValid(string objectId) + { + if (string.IsNullOrEmpty(objectId) || objectId.Length != 24) + { + return false; + } + + foreach (var c in objectId) + { + if (!Uri.IsHexDigit(c)) + { + return false; + } + } + + return true; + } + + /// + /// 比较两个 ObjectId 的大小 + /// + /// 第一个 ObjectId + /// 第二个 ObjectId + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + /// + /// 获取 ObjectId 的信息 + /// + /// ObjectId 字符串 + /// ObjectId 信息 + public static ObjectIdInfo GetInfo(string objectId) + { + var bytes = Decode(objectId); + return new ObjectIdInfo + { + Timestamp = ExtractTimestamp(bytes), + MachineId = ExtractMachineId(bytes), + ProcessId = ExtractProcessId(bytes), + Counter = ExtractCounter(bytes) + }; + } + + /// + /// 生成最小 ObjectId(指定时间) + /// + /// 时间戳 + /// 最小 ObjectId 字符串 + public static string Min(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + // 其余部分为 0 + + return Encode(objectId); + } + + /// + /// 生成最大 ObjectId(指定时间) + /// + /// 时间戳 + /// 最大 ObjectId 字符串 + public static string Max(DateTimeOffset timestamp) + { + var objectId = new byte[ObjectIdLength]; + var timestampSec = (int)timestamp.ToUnixTimeSeconds(); + + objectId[0] = (byte)(timestampSec >> 24); + objectId[1] = (byte)(timestampSec >> 16); + objectId[2] = (byte)(timestampSec >> 8); + objectId[3] = (byte)timestampSec; + // 其余部分为 0xFF + for (int i = 4; i < ObjectIdLength; i++) + { + objectId[i] = 0xFF; + } + + return Encode(objectId); + } + + #region 私有方法 + + private static byte[] GetMachineId() + { + var machineId = new byte[MachineIdLength]; + + try + { + // 尝试使用网络接口的 MAC 地址 + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + foreach (var ni in interfaces) + { + if (ni.OperationalStatus == OperationalStatus.Up && + ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) + { + var mac = ni.GetPhysicalAddress().GetAddressBytes(); + if (mac.Length >= MachineIdLength) + { + Buffer.BlockCopy(mac, 0, machineId, 0, MachineIdLength); + return machineId; + } + } + } + } + catch + { + // 忽略异常,使用随机值 + } + + // 使用机器名哈希 + var machineName = Environment.MachineName; + using var md5 = MD5.Create(); + var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(machineName)); + Buffer.BlockCopy(hash, 0, machineId, 0, MachineIdLength); + + return machineId; + } + + private static byte[] GetProcessId() + { + var processId = new byte[ProcessIdLength]; +#if NETSTANDARD2_1 + var pid = System.Diagnostics.Process.GetCurrentProcess().Id; +#else + var pid = System.Diagnostics.Process.GetCurrentProcess().Id; +#endif + + processId[0] = (byte)(pid >> 8); + processId[1] = (byte)pid; + + return processId; + } + + private static int GetRandomCounter() + { + var counterBytes = new byte[CounterLength]; + _rng.GetBytes(counterBytes); + return (counterBytes[0] << 16) | (counterBytes[1] << 8) | counterBytes[2]; + } + + #endregion + } + + /// + /// ObjectId 结构体 + /// + public readonly struct ObjectId : IComparable, IEquatable + { + private readonly byte[] _bytes; + + /// + /// 创建 ObjectId + /// + /// 字节数组 + public ObjectId(byte[] bytes) + { + if (bytes == null || bytes.Length != 12) + { + throw new ArgumentException("ObjectId 字节数组长度必须为 12", nameof(bytes)); + } + _bytes = bytes; + } + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => ObjectIdUtil.ExtractTimestamp(_bytes); + + /// + /// 字节数组 + /// + public byte[] ToByteArray() => (byte[])_bytes.Clone(); + + /// + /// 转换为字符串 + /// + public override string ToString() => ObjectIdUtil.Encode(_bytes); + + /// + /// 比较大小 + /// + public int CompareTo(ObjectId other) + { + for (int i = 0; i < 12; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return _bytes[i].CompareTo(other._bytes[i]); + } + } + return 0; + } + + /// + /// 判断相等 + /// + public bool Equals(ObjectId other) + { + for (int i = 0; i < 12; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return false; + } + } + return true; + } + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) + { + return obj is ObjectId other && Equals(other); + } + + /// + /// 获取哈希码 + /// + public override int GetHashCode() + { + var hash = 0; + for (int i = 0; i < 12; i++) + { + hash = (hash << 2) ^ _bytes[i]; + } + return hash; + } + + /// + /// 等于运算符 + /// + public static bool operator ==(ObjectId left, ObjectId right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(ObjectId left, ObjectId right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(ObjectId left, ObjectId right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(ObjectId left, ObjectId right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(ObjectId left, ObjectId right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(ObjectId left, ObjectId right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 ObjectId + /// + public static ObjectId NewObjectId() => new ObjectId(ObjectIdUtil.Generate()); + + /// + /// 解析字符串 + /// + public static ObjectId Parse(string objectId) => new ObjectId(ObjectIdUtil.Decode(objectId)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string objectId, out ObjectId result) + { + if (ObjectIdUtil.IsValid(objectId)) + { + result = Parse(objectId); + return true; + } + result = default; + return false; + } + } + + /// + /// ObjectId 信息 + /// + public class ObjectIdInfo + { + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp { get; set; } + + /// + /// 机器标识 + /// + public string MachineId { get; set; } = string.Empty; + + /// + /// 进程 ID + /// + public int ProcessId { get; set; } + + /// + /// 计数器 + /// + public int Counter { get; set; } + } +} diff --git a/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs b/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs new file mode 100644 index 0000000..7a4d2c5 --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ShortIdUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// 短 ID 生成器,生成简洁的唯一标识符 + /// + public static class ShortIdUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + + // 去除易混淆字符(0OIl1)的字符集 + private static readonly char[] DefaultChars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789".ToCharArray(); + private static readonly char[] AlphanumericChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); + private static readonly char[] LowercaseChars = "abcdefghjkmnpqrstuvwxyz23456789".ToCharArray(); + private static readonly char[] UppercaseChars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".ToCharArray(); + private static readonly char[] NumericChars = "0123456789".ToCharArray(); + + /// + /// 生成默认短 ID(8字符) + /// + /// 短 ID + public static string Generate() + { + return Generate(8); + } + + /// + /// 生成指定长度的短 ID + /// + /// 长度(建议 6-16) + /// 短 ID + public static string Generate(int length) + { + return Generate(length, ShortIdOptions.Default); + } + + /// + /// 使用指定选项生成短 ID + /// + /// 长度 + /// 选项 + /// 短 ID + public static string Generate(int length, ShortIdOptions options) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于 0"); + } + + var chars = GetChars(options); + var result = new char[length]; + var bytes = new byte[length]; + + _rng.GetBytes(bytes); + + for (int i = 0; i < length; i++) + { + result[i] = chars[bytes[i] % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成带前缀的短 ID + /// + /// 前缀 + /// ID 部分长度 + /// 带前缀的短 ID + public static string GenerateWithPrefix(string prefix, int length = 8) + { + return $"{prefix}{Generate(length)}"; + } + + /// + /// 生成小写短 ID + /// + /// 长度 + /// 小写短 ID + public static string GenerateLowercase(int length = 8) + { + return Generate(length, ShortIdOptions.Lowercase); + } + + /// + /// 生成大写短 ID + /// + /// 长度 + /// 大写短 ID + public static string GenerateUppercase(int length = 8) + { + return Generate(length, ShortIdOptions.Uppercase); + } + + /// + /// 生成纯数字短 ID + /// + /// 长度 + /// 纯数字短 ID + public static string GenerateNumeric(int length = 8) + { + return Generate(length, ShortIdOptions.Numeric); + } + + /// + /// 生成基于时间的短 ID(可排序) + /// + /// 随机部分长度 + /// 基于时间的短 ID + public static string GenerateTimeBased(int randomLength = 4) + { + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var timestampBase36 = ToBase36(timestamp); + var randomPart = Generate(randomLength, ShortIdOptions.Lowercase); + return $"{timestampBase36}{randomPart}"; + } + + /// + /// 生成邀请码风格短 ID(易于阅读和朗读) + /// + /// 分段数 + /// 每段长度 + /// 邀请码风格短 ID + public static string GenerateInviteCode(int segments = 3, int segmentLength = 4) + { + var parts = new string[segments]; + for (int i = 0; i < segments; i++) + { + parts[i] = Generate(segmentLength, ShortIdOptions.Uppercase); + } + return string.Join("-", parts); + } + + /// + /// 生成优化的 URL 短链接 ID + /// + /// 长度(建议 6-8) + /// URL 友好的短 ID + public static string GenerateUrlFriendly(int length = 6) + { + return Generate(length, ShortIdOptions.Lowercase); + } + + /// + /// 生成订单号风格短 ID + /// + /// 前缀(如 ORD) + /// 订单号风格短 ID + public static string GenerateOrderNumber(string prefix = "ORD") + { + var date = DateTime.UtcNow.ToString("yyyyMMdd"); + var random = GenerateNumeric(6); + return $"{prefix}{date}{random}"; + } + + /// + /// 生成优惠券码风格短 ID + /// + /// 长度 + /// 优惠券码 + public static string GenerateCouponCode(int length = 12) + { + return Generate(length, ShortIdOptions.Uppercase); + } + + /// + /// 从整数生成短 ID + /// + /// 数字 + /// 短 ID + public static string FromNumber(long number) + { + return ToBase62(number); + } + + /// + /// 将短 ID 转换为整数 + /// + /// 短 ID + /// 数字 + public static long ToNumber(string shortId) + { + return FromBase62(shortId); + } + + /// + /// 生成唯一短 ID(带校验) + /// + /// 长度(不含校验位) + /// 带校验位的短 ID + public static string GenerateWithChecksum(int length = 7) + { + var id = Generate(length); + var checksum = ComputeChecksum(id); + return $"{id}{checksum}"; + } + + /// + /// 验证带校验位的短 ID + /// + /// 带校验位的短 ID + /// 是否有效 + public static bool ValidateChecksum(string shortId) + { + if (string.IsNullOrEmpty(shortId) || shortId.Length < 2) + { + return false; + } + + var id = shortId[..^1]; + var checksum = shortId[^1]; + return checksum == ComputeChecksum(id); + } + + #region 私有方法 + + private static char[] GetChars(ShortIdOptions options) + { + return options switch + { + ShortIdOptions.Lowercase => LowercaseChars, + ShortIdOptions.Uppercase => UppercaseChars, + ShortIdOptions.Numeric => NumericChars, + ShortIdOptions.Alphanumeric => AlphanumericChars, + _ => DefaultChars + }; + } + + private static string ToBase36(long number) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyz"; + if (number < 0) + { + throw new ArgumentException("数字必须为非负数", nameof(number)); + } + + if (number == 0) + { + return "0"; + } + + var result = new StringBuilder(); + while (number > 0) + { + result.Insert(0, chars[(int)(number % 36)]); + number /= 36; + } + return result.ToString(); + } + + private static string ToBase62(long number) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (number < 0) + { + throw new ArgumentException("数字必须为非负数", nameof(number)); + } + + if (number == 0) + { + return "0"; + } + + var result = new StringBuilder(); + while (number > 0) + { + result.Insert(0, chars[(int)(number % 62)]); + number /= 62; + } + return result.ToString(); + } + + private static long FromBase62(string str) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + long result = 0; + + foreach (var c in str) + { + result = result * 62 + chars.IndexOf(c); + } + + return result; + } + + private static char ComputeChecksum(string id) + { + var chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; + int sum = 0; + foreach (var c in id) + { + sum += chars.IndexOf(char.ToUpper(c)); + } + return chars[sum % chars.Length]; + } + + #endregion + } + + /// + /// 短 ID 生成选项 + /// + public enum ShortIdOptions + { + /// + /// 默认(去除易混淆字符) + /// + Default, + + /// + /// 小写字母和数字 + /// + Lowercase, + + /// + /// 大写字母和数字 + /// + Uppercase, + + /// + /// 纯数字 + /// + Numeric, + + /// + /// 完整字母数字(包含易混淆字符) + /// + Alphanumeric + } + + /// + /// 短 ID 生成器配置 + /// + public class ShortIdGenerator + { + private readonly int _length; + private readonly ShortIdOptions _options; + private readonly string? _prefix; + private readonly string? _suffix; + + /// + /// 创建短 ID 生成器 + /// + /// 长度 + /// 选项 + /// 前缀 + /// 后缀 + public ShortIdGenerator(int length = 8, ShortIdOptions options = ShortIdOptions.Default, string? prefix = null, string? suffix = null) + { + _length = length; + _options = options; + _prefix = prefix; + _suffix = suffix; + } + + /// + /// 生成短 ID + /// + /// 短 ID + public string Generate() + { + var id = ShortIdUtil.Generate(_length, _options); + return $"{_prefix}{id}{_suffix}"; + } + + /// + /// 批量生成短 ID + /// + /// 数量 + /// 短 ID 列表 + public string[] GenerateMany(int count) + { + var result = new string[count]; + for (int i = 0; i < count; i++) + { + result[i] = Generate(); + } + return result; + } + + /// + /// 创建默认生成器 + /// + public static ShortIdGenerator Default => new(); + + /// + /// 创建 URL 友好生成器 + /// + public static ShortIdGenerator UrlFriendly => new(6, ShortIdOptions.Lowercase); + + /// + /// 创建邀请码生成器 + /// + public static ShortIdGenerator InviteCode => new(12, ShortIdOptions.Uppercase); + + /// + /// 创建订单号生成器 + /// + public static ShortIdGenerator OrderNumber => new(10, ShortIdOptions.Numeric, "ORD"); + } +} diff --git a/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs b/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs new file mode 100644 index 0000000..e1018c7 --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/SnowflakeIdUtil.cs @@ -0,0 +1,237 @@ +using System; + +namespace EasyTool.IdentifierCategory +{ + /// + /// 雪花算法ID生成器 + /// + public class SnowflakeIdGenerator + { + private long _workerId; + private long _datacenterId; + private readonly object _lock = new(); + private long _sequence; + private long _lastTimestamp; + + /// + /// 机器ID位数 + /// + public const int WorkerIdBits = 5; + + /// + /// 数据中心ID位数 + /// + public const int DatacenterIdBits = 5; + + /// + /// 序列号位数 + /// + public const int SequenceBits = 12; + + /// + /// 时间戳位数 + /// + public const int TimestampBits = 41; + + /// + /// 机器ID最大值 + /// + public const long MaxWorkerId = (1L << WorkerIdBits) - 1; + + /// + /// 数据中心ID最大值 + /// + public const long MaxDatacenterId = (1L << DatacenterIdBits) - 1; + + /// + /// 序列号最大值 + /// + public const long MaxSequence = (1L << SequenceBits) - 1; + + /// + /// 时间戳最大值 + /// + public const long MaxTimestamp = (1L << TimestampBits) - 1; + + /// + /// 起始时间戳(2020-01-01 00:00:00) + /// + public const long Twepoch = 1577808000000L; + + /// + /// 时间戳左移位数 + /// + public const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits; + + /// + /// 数据中心ID左移位数 + /// + public const int DatacenterIdShift = SequenceBits + WorkerIdBits; + + /// + /// 工作机器ID左移位数 + /// + public const int WorkerIdShift = SequenceBits; + + /// + /// 自定义时间戳生成函数 + /// + public Func? CustomTimestampFunc { get; set; } + + /// + /// 获取或设置工作机器ID + /// + public long WorkerId + { + get => _workerId; + set + { + if (value < 0 || value > MaxWorkerId) + throw new ArgumentException($"工作机器ID必须在 0 到 {MaxWorkerId} 之间"); + + _workerId = value; + } + } + + /// + /// 获取或设置数据中心ID + /// + public long DatacenterId + { + get => _datacenterId; + set + { + if (value < 0 || value > MaxDatacenterId) + throw new ArgumentException($"数据中心ID必须在 0 到 {MaxDatacenterId} 之间"); + + _datacenterId = value; + } + } + + /// + /// 创建雪花算法ID生成器 + /// + /// 工作机器ID(0-31) + /// 数据中心ID(0-31) + /// 初始序列号 + public SnowflakeIdGenerator(long workerId, long datacenterId, long sequence = 0L) + { + if (workerId > MaxWorkerId || workerId < 0) + throw new ArgumentException($"工作机器ID必须在 0 到 {MaxWorkerId} 之间"); + + if (datacenterId > MaxDatacenterId || datacenterId < 0) + throw new ArgumentException($"数据中心ID必须在 0 到 {MaxDatacenterId} 之间"); + + _workerId = workerId; + _datacenterId = datacenterId; + _sequence = sequence; + _lastTimestamp = -1L; + } + + /// + /// 创建雪花算法ID生成器(使用默认配置) + /// + public SnowflakeIdGenerator() : this(1, 1, 0) { } + + /// + /// 生成下一个唯一ID + /// + /// 唯一ID + public virtual long NextId() + { + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + // 时钟回拨检测 + if (_lastTimestamp == timestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + // 序列号溢出,等待下一毫秒 + timestamp = GetCurrentTimestamp(); + _sequence = 0; + } + } + else + { + _sequence = 0; + } + + _lastTimestamp = timestamp; + + return ((timestamp - Twepoch) << TimestampLeftShift) + | (_datacenterId << DatacenterIdShift) + | (_workerId << WorkerIdShift) + | _sequence; + } + } + + /// + /// 获取当前时间戳(毫秒) + /// + private long GetCurrentTimestamp() + { + if (CustomTimestampFunc != null) + { + return CustomTimestampFunc(); + } + return (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond); + } + + /// + /// 解析ID + /// + /// 雪花ID + /// 解析结果 + public static SnowflakeIdInfo Parse(long id) + { + var timestamp = (id >> TimestampLeftShift) + Twepoch; + var datacenterId = (id >> DatacenterIdShift) & MaxDatacenterId; + var workerId = (id >> WorkerIdShift) & MaxWorkerId; + var sequence = id & MaxSequence; + + return new SnowflakeIdInfo + { + Timestamp = timestamp, + DataCenterId = datacenterId, + WorkerId = workerId, + Sequence = sequence + }; + } + } + + /// + /// 雪花ID信息 + /// + public class SnowflakeIdInfo + { + /// + /// 时间戳 + /// + public long Timestamp { get; set; } + + /// + /// 数据中心ID + /// + public long DataCenterId { get; set; } + + /// + /// 工作机器ID + /// + public long WorkerId { get; set; } + + /// + /// 序列号 + /// + public long Sequence { get; set; } + + /// + /// 创建时间 + /// + public DateTime DateTime => DateTime.FromBinary(Timestamp); + + } + +} diff --git a/EasyTool.Core/IdentifierCategory/TSIDUtil.cs b/EasyTool.Core/IdentifierCategory/TSIDUtil.cs new file mode 100644 index 0000000..b45f515 --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/TSIDUtil.cs @@ -0,0 +1,503 @@ +using System; +using System.Security.Cryptography; +using System.Threading; + +namespace EasyTool.IdentifierCategory +{ + /// + /// TSID(Time-Sorted Identifier)生成器 + /// 生成可按时间排序的唯一标识符,支持分布式环境 + /// + public static class TsidUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] EncodingChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); + private static readonly byte[] DecodingMap = BuildDecodingMap(); + + private const int TimestampBits = 42; + private const int NodeIdBits = 8; + private const int SequenceBits = 14; + + private const long MaxTimestamp = (1L << TimestampBits) - 1; + private const int MaxNodeId = (1 << NodeIdBits) - 1; + private const int MaxSequence = (1 << SequenceBits) - 1; + + private static readonly long CustomEpoch = new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds(); + private static int _nodeId; + private static int _sequence; + private static long _lastTimestamp = -1; + private static readonly object _lock = new(); + + static TsidUtil() + { + _nodeId = GenerateNodeId(); + _sequence = GetRandomSequence(); + } + + /// + /// 生成新的 TSID + /// + /// TSID 长整型 + public static long Generate() + { + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + // 序列号溢出,等待下一毫秒 + timestamp = WaitNextMillis(timestamp); + } + } + else + { + _sequence = GetRandomSequence(); + } + + _lastTimestamp = timestamp; + + return ((timestamp & MaxTimestamp) << (NodeIdBits + SequenceBits)) + | ((long)(uint)_nodeId << SequenceBits) + | (long)(uint)_sequence; + } + } + + /// + /// 生成新的 TSID 字符串 + /// + /// TSID 字符串(13字符) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 使用指定节点 ID 生成 TSID + /// + /// 节点 ID(0-255) + /// TSID 长整型 + public static long Generate(int nodeId) + { + if (nodeId < 0 || nodeId > MaxNodeId) + { + throw new ArgumentOutOfRangeException(nameof(nodeId), $"节点 ID 必须在 0 到 {MaxNodeId} 之间"); + } + + lock (_lock) + { + var timestamp = GetCurrentTimestamp(); + + if (timestamp == _lastTimestamp) + { + _sequence = (_sequence + 1) & MaxSequence; + if (_sequence == 0) + { + timestamp = WaitNextMillis(timestamp); + } + } + else + { + _sequence = GetRandomSequence(); + } + + _lastTimestamp = timestamp; + + return ((timestamp & MaxTimestamp) << (NodeIdBits + SequenceBits)) + | ((long)(uint)nodeId << SequenceBits) + | (long)(uint)_sequence; + } + } + + /// + /// 使用指定节点 ID 生成 TSID 字符串 + /// + /// 节点 ID + /// TSID 字符串 + public static string GenerateString(int nodeId) + { + return Encode(Generate(nodeId)); + } + + /// + /// 将 TSID 长整型编码为字符串 + /// + /// TSID 值 + /// TSID 字符串 + public static string Encode(long tsid) + { + var chars = new char[13]; + + for (int i = 12; i >= 0; i--) + { + chars[i] = EncodingChars[(int)(tsid & 0x1F)]; + tsid >>= 5; + } + + return new string(chars); + } + + /// + /// 将 TSID 字符串解码为长整型 + /// + /// TSID 字符串 + /// TSID 长整型 + public static long Decode(string tsid) + { + if (string.IsNullOrEmpty(tsid) || tsid.Length != 13) + { + throw new ArgumentException("TSID 字符串长度必须为 13", nameof(tsid)); + } + + long result = 0; + + foreach (var c in tsid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + throw new ArgumentException($"无效的 TSID 字符: {c}", nameof(tsid)); + } + + result = (result << 5) | DecodingMap[c]; + } + + return result; + } + + /// + /// 从 TSID 提取时间戳 + /// + /// TSID 值 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(long tsid) + { + var timestamp = tsid >> (NodeIdBits + SequenceBits); + var milliseconds = timestamp + CustomEpoch; + return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); + } + + /// + /// 从 TSID 字符串提取时间戳 + /// + /// TSID 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string tsid) + { + return ExtractTimestamp(Decode(tsid)); + } + + /// + /// 从 TSID 提取节点 ID + /// + /// TSID 值 + /// 节点 ID + public static int ExtractNodeId(long tsid) + { + return (int)((tsid >> SequenceBits) & MaxNodeId); + } + + /// + /// 从 TSID 字符串提取节点 ID + /// + /// TSID 字符串 + /// 节点 ID + public static int ExtractNodeId(string tsid) + { + return ExtractNodeId(Decode(tsid)); + } + + /// + /// 从 TSID 提取序列号 + /// + /// TSID 值 + /// 序列号 + public static int ExtractSequence(long tsid) + { + return (int)(tsid & MaxSequence); + } + + /// + /// 从 TSID 字符串提取序列号 + /// + /// TSID 字符串 + /// 序列号 + public static int ExtractSequence(string tsid) + { + return ExtractSequence(Decode(tsid)); + } + + /// + /// 验证 TSID 字符串是否有效 + /// + /// TSID 字符串 + /// 是否有效 + public static bool IsValid(string tsid) + { + if (string.IsNullOrEmpty(tsid) || tsid.Length != 13) + { + return false; + } + + foreach (var c in tsid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + return false; + } + } + + return true; + } + + /// + /// 设置节点 ID + /// + /// 节点 ID(0-255) + public static void SetNodeId(int nodeId) + { + if (nodeId < 0 || nodeId > MaxNodeId) + { + throw new ArgumentOutOfRangeException(nameof(nodeId), $"节点 ID 必须在 0 到 {MaxNodeId} 之间"); + } + _nodeId = nodeId; + } + + /// + /// 获取当前节点 ID + /// + /// 节点 ID + public static int GetNodeId() + { + return _nodeId; + } + + /// + /// 比较两个 TSID 的大小 + /// + /// 第一个 TSID + /// 第二个 TSID + /// 比较结果 + public static int Compare(long a, long b) + { + return a.CompareTo(b); + } + + /// + /// 比较两个 TSID 字符串的大小 + /// + /// 第一个 TSID 字符串 + /// 第二个 TSID 字符串 + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + #region 私有方法 + + private static long GetCurrentTimestamp() + { + var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var timestamp = currentTime - CustomEpoch; + + if (timestamp < 0) + { + throw new InvalidOperationException("当前时间早于自定义纪元时间"); + } + + if (timestamp > MaxTimestamp) + { + throw new OverflowException("时间戳溢出"); + } + + return timestamp; + } + + private static long WaitNextMillis(long currentTimestamp) + { + var timestamp = GetCurrentTimestamp(); + while (timestamp <= currentTimestamp) + { + Thread.SpinWait(10); + timestamp = GetCurrentTimestamp(); + } + return timestamp; + } + + private static int GenerateNodeId() + { + var bytes = new byte[1]; + _rng.GetBytes(bytes); + return bytes[0]; + } + + private static int GetRandomSequence() + { + var bytes = new byte[2]; + _rng.GetBytes(bytes); + return ((bytes[0] << 6) | (bytes[1] >> 2)) & MaxSequence; + } + + private static byte[] BuildDecodingMap() + { + var map = new byte[256]; + Array.Fill(map, (byte)0xFF); + + for (int i = 0; i < EncodingChars.Length; i++) + { + map[EncodingChars[i]] = (byte)i; + } + + // 支持小写字母 + map['a'] = map['A']; + map['b'] = map['B']; + map['c'] = map['C']; + map['d'] = map['D']; + map['e'] = map['E']; + map['f'] = map['F']; + map['g'] = map['G']; + map['h'] = map['H']; + map['i'] = map['I']; + map['j'] = map['J']; + map['k'] = map['K']; + map['l'] = map['L']; + map['m'] = map['M']; + map['n'] = map['N']; + map['o'] = map['O']; + map['p'] = map['P']; + map['q'] = map['Q']; + map['r'] = map['R']; + map['s'] = map['S']; + map['t'] = map['T']; + map['u'] = map['U']; + map['v'] = map['V']; + map['w'] = map['W']; + map['x'] = map['X']; + map['y'] = map['Y']; + map['z'] = map['Z']; + + return map; + } + + #endregion + } + + /// + /// TSID 结构体 + /// + public readonly struct Tsid : IComparable, IEquatable + { + private readonly long _value; + + /// + /// 创建 TSID + /// + /// TSID 值 + public Tsid(long value) + { + _value = value; + } + + /// + /// TSID 值 + /// + public long Value => _value; + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => TsidUtil.ExtractTimestamp(_value); + + /// + /// 节点 ID + /// + public int NodeId => TsidUtil.ExtractNodeId(_value); + + /// + /// 序列号 + /// + public int Sequence => TsidUtil.ExtractSequence(_value); + + /// + /// 转换为字符串 + /// + public override string ToString() => TsidUtil.Encode(_value); + + /// + /// 比较大小 + /// + public int CompareTo(Tsid other) => _value.CompareTo(other._value); + + /// + /// 判断相等 + /// + public bool Equals(Tsid other) => _value == other._value; + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) => obj is Tsid other && Equals(other); + + /// + /// 获取哈希码 + /// + public override int GetHashCode() => _value.GetHashCode(); + + /// + /// 等于运算符 + /// + public static bool operator ==(Tsid left, Tsid right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(Tsid left, Tsid right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(Tsid left, Tsid right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(Tsid left, Tsid right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(Tsid left, Tsid right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(Tsid left, Tsid right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 TSID + /// + public static Tsid NewTsid() => new Tsid(TsidUtil.Generate()); + + /// + /// 解析字符串 + /// + public static Tsid Parse(string tsid) => new Tsid(TsidUtil.Decode(tsid)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string tsid, out Tsid result) + { + if (TsidUtil.IsValid(tsid)) + { + result = Parse(tsid); + return true; + } + result = default; + return false; + } + } +} diff --git a/EasyTool.Core/IdentifierCategory/ULIDUtil.cs b/EasyTool.Core/IdentifierCategory/ULIDUtil.cs new file mode 100644 index 0000000..b431bdc --- /dev/null +++ b/EasyTool.Core/IdentifierCategory/ULIDUtil.cs @@ -0,0 +1,460 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.IdentifierCategory +{ + /// + /// ULID(Universally Unique Lexicographically Sortable Identifier)生成器 + /// ULID 是一种可排序的唯一标识符,由 48 位时间戳和 80 位随机数组成 + /// + public static class UlidUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] EncodingChars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".ToCharArray(); + private static readonly byte[] DecodingMap = BuildDecodingMap(); + + private const int TimestampLength = 6; + private const int RandomnessLength = 10; + private const int UlidLength = 16; + private const int StringLength = 26; + + /// + /// 生成新的 ULID + /// + /// ULID 字节数组 + public static byte[] Generate() + { + return Generate(DateTimeOffset.UtcNow); + } + + /// + /// 生成指定时间的 ULID + /// + /// 时间戳 + /// ULID 字节数组 + public static byte[] Generate(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + // 写入时间戳(6字节,大端序) + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + + // 写入随机数(10字节) + _rng.GetBytes(ulid, TimestampLength, RandomnessLength); + + return ulid; + } + + /// + /// 生成新的 ULID 字符串 + /// + /// ULID 字符串(26字符) + public static string GenerateString() + { + return Encode(Generate()); + } + + /// + /// 生成指定时间的 ULID 字符串 + /// + /// 时间戳 + /// ULID 字符串(26字符) + public static string GenerateString(DateTimeOffset timestamp) + { + return Encode(Generate(timestamp)); + } + + /// + /// 生成 ULID 结构体 + /// + /// ULID 结构体 + public static Ulid GenerateUlid() + { + return new Ulid(Generate()); + } + + /// + /// 生成指定时间的 ULID 结构体 + /// + /// 时间戳 + /// ULID 结构体 + public static Ulid GenerateUlid(DateTimeOffset timestamp) + { + return new Ulid(Generate(timestamp)); + } + + /// + /// 将 ULID 字节数组编码为字符串 + /// + /// ULID 字节数组 + /// ULID 字符串 + public static string Encode(byte[] bytes) + { + if (bytes == null || bytes.Length != UlidLength) + { + throw new ArgumentException($"ULID 字节数组长度必须为 {UlidLength}", nameof(bytes)); + } + + var result = new char[StringLength]; + var buffer = 0; + var bufferBits = 0; + var index = StringLength - 1; + + for (int i = UlidLength - 1; i >= 0; i--) + { + buffer = (buffer << 8) | bytes[i]; + bufferBits += 8; + + while (bufferBits >= 5) + { + result[index--] = EncodingChars[(buffer >> (bufferBits - 5)) & 0x1F]; + bufferBits -= 5; + } + } + + if (bufferBits > 0) + { + result[index] = EncodingChars[buffer & 0x1F]; + } + + return new string(result); + } + + /// + /// 将 ULID 字符串解码为字节数组 + /// + /// ULID 字符串 + /// ULID 字节数组 + public static byte[] Decode(string ulid) + { + if (string.IsNullOrEmpty(ulid) || ulid.Length != StringLength) + { + throw new ArgumentException($"ULID 字符串长度必须为 {StringLength}", nameof(ulid)); + } + + var result = new byte[UlidLength]; + var buffer = 0; + var bufferBits = 0; + var index = UlidLength - 1; + + for (int i = StringLength - 1; i >= 0; i--) + { + var c = ulid[i]; + var value = DecodingMap[c]; + + if (value == 0xFF) + { + throw new ArgumentException($"无效的 ULID 字符: {c}", nameof(ulid)); + } + + buffer = (buffer << 5) | value; + bufferBits += 5; + + while (bufferBits >= 8) + { + result[index--] = (byte)((buffer >> (bufferBits - 8)) & 0xFF); + bufferBits -= 8; + } + } + + return result; + } + + /// + /// 从 ULID 提取时间戳 + /// + /// ULID 字节数组 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(byte[] ulid) + { + if (ulid == null || ulid.Length != UlidLength) + { + throw new ArgumentException($"ULID 字节数组长度必须为 {UlidLength}", nameof(ulid)); + } + + var timestampMs = ((long)ulid[0] << 40) | + ((long)ulid[1] << 32) | + ((long)ulid[2] << 24) | + ((long)ulid[3] << 16) | + ((long)ulid[4] << 8) | + ulid[5]; + + return DateTimeOffset.FromUnixTimeMilliseconds(timestampMs); + } + + /// + /// 从 ULID 字符串提取时间戳 + /// + /// ULID 字符串 + /// 时间戳 + public static DateTimeOffset ExtractTimestamp(string ulid) + { + return ExtractTimestamp(Decode(ulid)); + } + + /// + /// 验证 ULID 字符串是否有效 + /// + /// ULID 字符串 + /// 是否有效 + public static bool IsValid(string ulid) + { + if (string.IsNullOrEmpty(ulid) || ulid.Length != StringLength) + { + return false; + } + + foreach (var c in ulid) + { + if (c >= DecodingMap.Length || DecodingMap[c] == 0xFF) + { + return false; + } + } + + return true; + } + + /// + /// 比较两个 ULID 的大小 + /// + /// 第一个 ULID + /// 第二个 ULID + /// 比较结果 + public static int Compare(string a, string b) + { + return string.CompareOrdinal(a, b); + } + + /// + /// 获取最小 ULID(指定时间) + /// + /// 时间戳 + /// 最小 ULID 字符串 + public static string Min(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + // 随机部分全部为 0 + + return Encode(ulid); + } + + /// + /// 获取最大 ULID(指定时间) + /// + /// 时间戳 + /// 最大 ULID 字符串 + public static string Max(DateTimeOffset timestamp) + { + var ulid = new byte[UlidLength]; + var timestampMs = timestamp.ToUnixTimeMilliseconds(); + + ulid[0] = (byte)(timestampMs >> 40); + ulid[1] = (byte)(timestampMs >> 32); + ulid[2] = (byte)(timestampMs >> 24); + ulid[3] = (byte)(timestampMs >> 16); + ulid[4] = (byte)(timestampMs >> 8); + ulid[5] = (byte)timestampMs; + // 随机部分全部为 0xFF + for (int i = TimestampLength; i < UlidLength; i++) + { + ulid[i] = 0xFF; + } + + return Encode(ulid); + } + + private static byte[] BuildDecodingMap() + { + var map = new byte[256]; + Array.Fill(map, (byte)0xFF); + + for (int i = 0; i < EncodingChars.Length; i++) + { + map[EncodingChars[i]] = (byte)i; + } + + // 支持小写字母 + map['a'] = map['A']; + map['b'] = map['B']; + map['c'] = map['C']; + map['d'] = map['D']; + map['e'] = map['E']; + map['f'] = map['F']; + map['g'] = map['G']; + map['h'] = map['H']; + map['j'] = map['J']; + map['k'] = map['K']; + map['m'] = map['M']; + map['n'] = map['N']; + map['p'] = map['P']; + map['q'] = map['Q']; + map['r'] = map['R']; + map['s'] = map['S']; + map['t'] = map['T']; + map['v'] = map['V']; + map['w'] = map['W']; + map['x'] = map['X']; + map['y'] = map['Y']; + map['z'] = map['Z']; + + return map; + } + } + + /// + /// ULID 结构体 + /// + public readonly struct Ulid : IComparable, IEquatable + { + private readonly byte[] _bytes; + + /// + /// 创建 ULID + /// + /// 字节数组 + public Ulid(byte[] bytes) + { + if (bytes == null || bytes.Length != 16) + { + throw new ArgumentException("ULID 字节数组长度必须为 16", nameof(bytes)); + } + _bytes = bytes; + } + + /// + /// 时间戳 + /// + public DateTimeOffset Timestamp => UlidUtil.ExtractTimestamp(_bytes); + + /// + /// 字节数组 + /// + public byte[] ToByteArray() => (byte[])_bytes.Clone(); + + /// + /// 转换为字符串 + /// + public override string ToString() => UlidUtil.Encode(_bytes); + + /// + /// 比较大小 + /// + public int CompareTo(Ulid other) + { + for (int i = 0; i < 16; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return _bytes[i].CompareTo(other._bytes[i]); + } + } + return 0; + } + + /// + /// 判断相等 + /// + public bool Equals(Ulid other) + { + for (int i = 0; i < 16; i++) + { + if (_bytes[i] != other._bytes[i]) + { + return false; + } + } + return true; + } + + /// + /// 判断相等 + /// + public override bool Equals(object? obj) + { + return obj is Ulid other && Equals(other); + } + + /// + /// 获取哈希码 + /// + public override int GetHashCode() + { + var hash = 0; + for (int i = 0; i < 16; i++) + { + hash = (hash << 2) ^ _bytes[i]; + } + return hash; + } + + /// + /// 等于运算符 + /// + public static bool operator ==(Ulid left, Ulid right) => left.Equals(right); + + /// + /// 不等于运算符 + /// + public static bool operator !=(Ulid left, Ulid right) => !left.Equals(right); + + /// + /// 小于运算符 + /// + public static bool operator <(Ulid left, Ulid right) => left.CompareTo(right) < 0; + + /// + /// 大于运算符 + /// + public static bool operator >(Ulid left, Ulid right) => left.CompareTo(right) > 0; + + /// + /// 小于等于运算符 + /// + public static bool operator <=(Ulid left, Ulid right) => left.CompareTo(right) <= 0; + + /// + /// 大于等于运算符 + /// + public static bool operator >=(Ulid left, Ulid right) => left.CompareTo(right) >= 0; + + /// + /// 生成新 ULID + /// + public static Ulid NewUlid() => UlidUtil.GenerateUlid(); + + /// + /// 解析字符串 + /// + public static Ulid Parse(string ulid) => new Ulid(UlidUtil.Decode(ulid)); + + /// + /// 尝试解析字符串 + /// + public static bool TryParse(string ulid, out Ulid result) + { + if (UlidUtil.IsValid(ulid)) + { + result = Parse(ulid); + return true; + } + result = default; + return false; + } + } +} diff --git a/EasyTool.Core/LanguageCategory/BCDUtil.cs b/EasyTool.Core/LanguageCategory/BCDUtil.cs deleted file mode 100644 index 8e07e35..0000000 --- a/EasyTool.Core/LanguageCategory/BCDUtil.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// BCD工具 - /// - public class BCDUtil - { - /// - /// 将一个十进制数转换成对应的二进制码数组 - /// - /// 需要转换的十进制数 - /// 二进制码数组 - public static int[] DecToBinaryArray(int dec) - { - if (dec < 0) - { - throw new ArgumentException("dec必须是非负整数。"); - } - - if (dec == 0) - { - return new int[] { 0 }; - } - - int[] binaryArray = new int[32]; - int index = 0; - - while (dec > 0) - { - binaryArray[index++] = dec % 2; - dec /= 2; - } - - Array.Resize(ref binaryArray, index); - Array.Reverse(binaryArray); - - return binaryArray; - } - - /// - /// 将一个二进制码数组转换成对应的十进制数 - /// - /// 需要转换的二进制码数组 - /// 对应的十进制数 - public static int BinaryArrayToDec(int[] binaryArray) - { - if (binaryArray == null) - { - throw new ArgumentNullException("binaryArray不能为null。"); - } - - int dec = 0; - int power = 1; - - for (int i = binaryArray.Length - 1; i >= 0; i--) - { - dec += binaryArray[i] * power; - power *= 2; - } - - return dec; - } - - /// - /// 将一个十进制数转换成对应的BCD码数组 - /// - /// 需要转换的十进制数 - /// BCD码数组 - public static int[] DecToBCDArray(int dec) - { - if (dec < 0) - { - throw new ArgumentException("dec必须是非负整数。"); - } - - if (dec == 0) - { - return new int[] { 0 }; - } - - int[] bcdArray = new int[10]; - int index = 0; - - while (dec > 0) - { - int remainder = dec % 10; - int[] binaryArray = DecToBinaryArray(remainder); - int paddingCount = 4 - binaryArray.Length; - - for (int i = 0; i < paddingCount; i++) - { - bcdArray[index++] = 0; - } - - for (int i = 0; i < binaryArray.Length; i++) - { - bcdArray[index++] = binaryArray[i]; - } - - dec /= 10; - } - - Array.Resize(ref bcdArray, index); - Array.Reverse(bcdArray); - - return bcdArray; - } - - /// - /// 将一个BCD码数组转换成对应的十进制数 - /// - /// 需要转换的BCD码数组 - /// 对应的十进制数 - public static int BCDArrayToDec(int[] bcdArray) - { - if (bcdArray == null) - { - throw new ArgumentNullException("bcdArray不能为null。"); - } - - int dec = 0; - int power = 1; - - for (int i = bcdArray.Length - 1; i >= 0; i -= 4) - { - int binary = 0; - - for (int j = 0; j < 4; j++) - { - int index = i - j; - - if (index < 0) - { - break; - } - - binary += bcdArray[index] * (int)Math.Pow(2, 3 - j); - } - - dec += binary * power; - power *= 10; - } - - return dec; - } - - /// - /// 将给定的十进制数转换为 BCD 码字符串。 - /// - /// 要转换的十进制数 - /// 转换后的 BCD 码字符串 - public static string Encode(int dec) - { - if (dec == 0) - { - return "0"; - } - - string str = dec.ToString(); - int len = str.Length; - char[] bcdChars = new char[len * 2]; - for (int i = 0; i < len; i++) - { - int bcd = ((int)Char.GetNumericValue(str[i])) & 0x0F; - bcdChars[i * 2] = (char)(bcd + ((bcd > 9) ? 0x37 : 0x30)); - - bcd = (((int)Char.GetNumericValue(str[i])) >> 4) & 0x0F; - bcdChars[i * 2 + 1] = (char)(bcd + ((bcd > 9) ? 0x37 : 0x30)); - } - return new string(bcdChars); - } - - /// - /// 将给定的 BCD 码字符串转换为十进制数。 - /// - /// 要转换的 BCD 码字符串 - /// 转换后的十进制数 - public static int Decode(string bcd) - { - if (string.IsNullOrEmpty(bcd)) - { - return 0; - } - - int len = bcd.Length; - int dec = 0; - for (int i = 0; i < len; i += 2) - { - int a = ((int)bcd[i]) & 0x0F; - int b = ((int)bcd[i + 1]) & 0x0F; - dec = dec * 100 + a + b * 10; - } - return dec; - } - } -} diff --git a/EasyTool.Core/LanguageCategory/SingletonUtil.cs b/EasyTool.Core/LanguageCategory/SingletonUtil.cs deleted file mode 100644 index f49d677..0000000 --- a/EasyTool.Core/LanguageCategory/SingletonUtil.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// 单例工具类 - /// - public class SingletonUtil where T : class, new() - { - private static readonly Lazy lazyInstance = new Lazy(() => new T()); - - /// - /// 返回单例对象的唯一实例(懒汉模式) - /// - public static T LazyInstance => lazyInstance.Value; - - - private static T instance; - private static readonly object lockObject = new object(); - - /// - /// 返回单例对象的唯一实例(饿汉模式) - /// - [Obsolete] - public static T Instance - { - get - { - if (instance == null) - { - lock (lockObject) - { - if (instance == null) - { - instance = new T(); - } - } - } - - return instance; - } - } - } -} diff --git a/EasyTool.Core/LanguageCategory/TreeUtil.cs b/EasyTool.Core/LanguageCategory/TreeUtil.cs deleted file mode 100644 index 852a4ce..0000000 --- a/EasyTool.Core/LanguageCategory/TreeUtil.cs +++ /dev/null @@ -1,301 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - /// - /// 树工具类,用于构建树结构 - /// - public class TreeUtil - { - private List> _nodes; - - /// - /// 构造函数 - /// - /// 树节点列表 - public TreeUtil(List> nodes) - { - _nodes = nodes; - } - - /// - /// 构建树结构 - /// - /// 根节点 - public TreeNode BuildTree() - { - // 获取根节点 - var root = _nodes.FirstOrDefault(n => n.ParentId.Equals(default(T))); - - if (root == null) - { - return null; - } - - // 构建树结构 - BuildTree(root); - - return root; - } - - private void BuildTree(TreeNode node) - { - node.Children = _nodes.Where(n => n.ParentId.Equals(node.Id)).ToList(); - - if (node.Children.Count > 0) - { - foreach (var child in node.Children) - { - BuildTree(child); - } - } - } - - /// - /// 获取某个节点的所有父节点 - /// - /// 节点 - /// 父节点列表,从根节点到该节点的顺序 - public List> GetParents(TreeNode node) - { - var parents = new List>(); - var parent = GetParent(node.Id); - while (parent != null) - { - parents.Insert(0, parent); - parent = GetParent(parent.Id); - } - return parents; - } - - /// - /// 获取某个节点的深度 - /// - /// 节点 - /// 节点深度,根节点的深度为0 - public int GetDepth(TreeNode node) - { - return GetParents(node).Count; - } - - /// - /// 获取某个节点的所有子孙节点 - /// - /// 节点 - /// 子孙节点列表 - public List> GetDescendants(TreeNode node) - { - var descendants = new List>(); - GetDescendants(node, descendants); - return descendants; - } - - private void GetDescendants(TreeNode node, List> descendants) - { - descendants.Add(node); - if (node.Children.Count > 0) - { - foreach (var child in node.Children) - { - GetDescendants(child, descendants); - } - } - } - - private TreeNode GetParent(T id) - { - return _nodes.FirstOrDefault(n => n.Id.Equals(id)); - } - - /// - /// 获取某个节点的所有兄弟节点 - /// - /// 节点 - /// 兄弟节点列表 - public List> GetSiblings(TreeNode node) - { - var parent = GetParent(node.Id); - if (parent == null) - { - return new List>(); - } - return parent.Children.Where(n => !n.Id.Equals(node.Id)).ToList(); - } - - /// - /// 获取某个节点的所有兄弟节点数量 - /// - /// 节点 - /// 兄弟节点数量 - public int GetSiblingCount(TreeNode node) - { - return GetSiblings(node).Count; - } - - /// - /// 判断某个节点是否是叶子节点 - /// - /// 节点 - /// 是否是叶子节点 - public bool IsLeaf(TreeNode node) - { - return node.Children.Count == 0; - } - - /// - /// 获取树的最大深度 - /// - /// 树的最大深度 - public int GetMaxDepth() - { - return _nodes.Max(n => GetDepth(n)); - } - - /// - /// 获取树的最小深度 - /// - /// 树的最小深度 - public int GetMinDepth() - { - return _nodes.Min(n => GetDepth(n)); - } - - /// - /// 获取某个节点的下一个兄弟节点 - /// - /// 节点 - /// 下一个兄弟节点 - public TreeNode GetNextSibling(TreeNode node) - { - var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id.Equals(node.Id)); - return index + 1 < siblings.Count ? siblings[index + 1] : null; - } - - /// - /// 获取某个节点的上一个兄弟节点 - /// - /// 节点 - /// 上一个兄弟节点 - public TreeNode GetPreviousSibling(TreeNode node) - { - var siblings = GetSiblings(node); - var index = siblings.FindIndex(n => n.Id.Equals(node.Id)); - return index - 1 >= 0 ? siblings[index - 1] : null; - } - - /// - /// 获取某个节点的首个子节点 - /// - /// 节点 - /// 首个子节点 - public TreeNode GetFirstChild(TreeNode node) - { - return node.Children.Count > 0 ? node.Children[0] : null; - } - - /// - /// 获取某个节点的最后一个子节点 - /// - /// 节点 - /// 最后一个子节点 - public TreeNode GetLastChild(TreeNode node) - { - return node.Children.Count > 0 ? node.Children[node.Children.Count - 1] : null; - } - - /// - /// 获取树的所有节点数量 - /// - /// 树的所有节点数量 - public int GetNodeCount() - { - return _nodes.Count; - } - - /// - /// 获取树的所有叶子节点数量 - /// - /// 树的所有叶子节点数量 - public int GetLeafCount() - { - return _nodes.Count(IsLeaf); - } - - /// - /// 获取树的所有节点的权重和 - /// - /// 树的所有节点的权重和 - public int GetTotalWeight() - { - return _nodes.Sum(n => n.Weight); - } - - /// - /// 获取树的所有叶子节点的权重和 - /// - /// 树的所有叶子节点的权重和 - public int GetLeafWeightTotal() - { - return _nodes.Where(IsLeaf).Sum(n => n.Weight); - } - - /// - /// 获取树的平均深度 - /// - /// 树的平均深度 - public int GetAverageDepth() - { - return (int)_nodes.Average(n => GetDepth(n)); - } - - /// - /// 获取树的平均节点权重 - /// - /// 树的平均节点权重 - public int GetAverageWeight() - { - return (int)_nodes.Average(n => n.Weight); - } - - /// - /// 获取树的最大节点权重 - /// - /// 树的最大节点权重 - public int GetMaxWeight() - { - return _nodes.Max(n => n.Weight); - } - - /// - /// 获取树的最小节点权重 - /// - /// 树的最小节点权重 - public int GetMinWeight() - { - return _nodes.Min(n => n.Weight); - } - } - - public class TreeNode - { - public T Id { get; set; } - public T ParentId { get; set; } - public string Name { get; set; } - public int Weight { get; set; } - public D Data { get; set; } - public List> Children { get; set; } - - public TreeNode(T id, T parentId, string name, int weight, D data) - { - this.Id = id; - this.ParentId = parentId; - this.Name = name; - this.Weight = weight; - this.Data = data; - } - } -} diff --git a/EasyTool.Core/MathCategory/AngleUtil.cs b/EasyTool.Core/MathCategory/AngleUtil.cs new file mode 100644 index 0000000..3a1893d --- /dev/null +++ b/EasyTool.Core/MathCategory/AngleUtil.cs @@ -0,0 +1,312 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 角度运算工具类 + /// + public static class AngleUtil + { + /// + /// 弧度转角度 + /// + public static double RadiansToDegrees(double radians) + { + return radians * (180.0 / Math.PI); + } + + /// + /// 角度转弧度 + /// + public static double DegreesToRadians(double degrees) + { + return degrees * (Math.PI / 180.0); + } + + /// + /// 角度转弧度 + /// + public static Angle Radians(double radians) + { + return Angle.FromRadians(radians); + } + + /// + /// 角度转弧度 + /// + public static Angle Degrees(double degrees) + { + return Angle.FromDegrees(degrees); + } + + /// + /// 规范化角度到 [0, 360) 范围 + /// + public static double NormalizeDegrees(double degrees) + { + degrees %= 360; + return degrees < 0 ? degrees + 360 : degrees; + } + + /// + /// 规范化弧度到 [0, 2π) 范围 + /// + public static double NormalizeRadians(double radians) + { + radians %= (2 * Math.PI); + return radians < 0 ? radians + (2 * Math.PI) : radians; + } + + /// + /// 角度加法 + /// + public static double AddDegrees(double a, double b) + { + return NormalizeDegrees(a + b); + } + + /// + /// 角度减法 + /// + public static double SubtractDegrees(double a, double b) + { + return NormalizeDegrees(a - b); + } + + /// + /// 计算两个角度的最小差值 + /// + public static double MinimumAngleDifference(double a, double b) + { + var diff = NormalizeDegrees(a - b); + return diff > 180 ? 360 - diff : diff; + } + + /// + /// 角度线性插值 + /// + public static double LerpDegrees(double from, double to, double t) + { + var diff = to - from; + if (diff > 180) diff -= 360; + else if (diff < -180) diff += 360; + return NormalizeDegrees(from + diff * t); + } + + /// + /// 度分秒转十进制度 + /// + public static double DmsToDecimal(int degrees, int minutes, double seconds) + { + var sign = degrees < 0 ? -1 : 1; + return sign * (Math.Abs(degrees) + minutes / 60.0 + seconds / 3600.0); + } + + /// + /// 十进制度转度分秒 + /// + public static (int Degrees, int Minutes, double Seconds) DecimalToDms(double decimalDegrees) + { + var sign = decimalDegrees < 0 ? -1 : 1; + decimalDegrees = Math.Abs(decimalDegrees); + + var degrees = (int)decimalDegrees; + var minutes = (int)((decimalDegrees - degrees) * 60); + var seconds = ((decimalDegrees - degrees) * 60 - minutes) * 60; + + return (sign * degrees, minutes, seconds); + } + + /// + /// 格式化度分秒 + /// + public static string FormatDms(double decimalDegrees) + { + var (degrees, minutes, seconds) = DecimalToDms(decimalDegrees); + return $"{degrees}°{minutes}'{seconds:F2}″"; + } + + /// + /// 解析度分秒字符串 + /// + public static double ParseDms(string dms) + { + var parts = dms.Split(new[] { '°', '\'', '″', '"' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 0) + throw new ArgumentException("无效的度分秒格式"); + + var degrees = double.Parse(parts[0].Trim()); + var minutes = parts.Length > 1 ? double.Parse(parts[1].Trim()) : 0; + var seconds = parts.Length > 2 ? double.Parse(parts[2].Trim()) : 0; + + return DmsToDecimal((int)degrees, (int)minutes, seconds); + } + + #region 三角函数(角度版本) + + /// + /// 正弦(角度) + /// + public static double Sin(double degrees) + { + return Math.Sin(DegreesToRadians(degrees)); + } + + /// + /// 余弦(角度) + /// + public static double Cos(double degrees) + { + return Math.Cos(DegreesToRadians(degrees)); + } + + /// + /// 正切(角度) + /// + public static double Tan(double degrees) + { + return Math.Tan(DegreesToRadians(degrees)); + } + + /// + /// 反正弦(返回角度) + /// + public static double Asin(double value) + { + return RadiansToDegrees(Math.Asin(value)); + } + + /// + /// 反余弦(返回角度) + /// + public static double Acos(double value) + { + return RadiansToDegrees(Math.Acos(value)); + } + + /// + /// 反正切(返回角度) + /// + public static double Atan(double value) + { + return RadiansToDegrees(Math.Atan(value)); + } + + /// + /// 反正切2(返回角度) + /// + public static double Atan2(double y, double x) + { + return RadiansToDegrees(Math.Atan2(y, x)); + } + + #endregion + } + + /// + /// 角度结构 + /// + public readonly struct Angle : IEquatable, IComparable + { + private readonly double _degrees; + + private Angle(double degrees) + { + _degrees = AngleUtil.NormalizeDegrees(degrees); + } + + /// + /// 角度值 + /// + public double Degrees => _degrees; + + /// + /// 弧度值 + /// + public double Radians => AngleUtil.DegreesToRadians(_degrees); + + /// + /// 从度创建角度 + /// + public static Angle FromDegrees(double degrees) => new Angle(degrees); + + /// + /// 从弧度创建角度 + /// + public static Angle FromRadians(double radians) => new Angle(AngleUtil.RadiansToDegrees(radians)); + + /// + /// 从度分秒创建角度 + /// + public static Angle FromDms(int degrees, int minutes, double seconds) + => new Angle(AngleUtil.DmsToDecimal(degrees, minutes, seconds)); + + /// + /// 零度 + /// + public static Angle Zero => new Angle(0); + + /// + /// 直角 (90°) + /// + public static Angle Right => new Angle(90); + + /// + /// 平角 (180°) + /// + public static Angle Straight => new Angle(180); + + /// + /// 周角 (360°) + /// + public static Angle Full => new Angle(360); + + #region 运算符 + + public static Angle operator +(Angle a, Angle b) => new Angle(a._degrees + b._degrees); + public static Angle operator -(Angle a, Angle b) => new Angle(a._degrees - b._degrees); + public static Angle operator *(Angle a, double scalar) => new Angle(a._degrees * scalar); + public static Angle operator *(double scalar, Angle a) => new Angle(a._degrees * scalar); + public static Angle operator /(Angle a, double scalar) => new Angle(a._degrees / scalar); + public static Angle operator -(Angle a) => new Angle(-a._degrees); + public static bool operator ==(Angle a, Angle b) => a.Equals(b); + public static bool operator !=(Angle a, Angle b) => !a.Equals(b); + public static bool operator <(Angle a, Angle b) => a._degrees < b._degrees; + public static bool operator >(Angle a, Angle b) => a._degrees > b._degrees; + public static bool operator <=(Angle a, Angle b) => a._degrees <= b._degrees; + public static bool operator >=(Angle a, Angle b) => a._degrees >= b._degrees; + + public static implicit operator double(Angle angle) => angle._degrees; + + #endregion + + #region 三角函数 + + public double Sin() => AngleUtil.Sin(_degrees); + public double Cos() => AngleUtil.Cos(_degrees); + public double Tan() => AngleUtil.Tan(_degrees); + + #endregion + + #region 接口实现 + + public bool Equals(Angle other) => Math.Abs(_degrees - other._degrees) < double.Epsilon; + public override bool Equals(object? obj) => obj is Angle other && Equals(other); + public override int GetHashCode() => _degrees.GetHashCode(); + public int CompareTo(Angle other) => _degrees.CompareTo(other._degrees); + + public override string ToString() => $"{_degrees:F2}°"; + + public string ToString(string format) + { + if (format == "DMS") + { + var (degrees, minutes, seconds) = AngleUtil.DecimalToDms(_degrees); + return $"{degrees}°{minutes}'{seconds:F2}″"; + } + return $"{_degrees.ToString(format)}°"; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/CombinatoricsUtil.cs b/EasyTool.Core/MathCategory/CombinatoricsUtil.cs new file mode 100644 index 0000000..de61a79 --- /dev/null +++ b/EasyTool.Core/MathCategory/CombinatoricsUtil.cs @@ -0,0 +1,576 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 组合数学工具类 + /// 提供排列、组合等计算功能 + /// + public static class CombinatoricsUtil + { + #region 阶乘 + + /// + /// 计算阶乘 + /// + /// 非负整数 + /// n! + public static long Factorial(int n) + { + if (n < 0) + throw new ArgumentException("阶乘只能计算非负整数"); + + if (n <= 1) + return 1; + + long result = 1; + + for (int i = 2; i <= n; i++) + { + result *= i; + } + + return result; + } + + /// + /// 计算大数阶乘 + /// + /// 非负整数 + /// n! 的字符串表示 + public static string FactorialBig(int n) + { + if (n < 0) + throw new ArgumentException("阶乘只能计算非负整数"); + + if (n <= 1) + return "1"; + + var result = new List { 1 }; + + for (int i = 2; i <= n; i++) + { + int carry = 0; + + for (int j = 0; j < result.Count; j++) + { + int product = result[j] * i + carry; + result[j] = product % 10; + carry = product / 10; + } + + while (carry > 0) + { + result.Add(carry % 10); + carry /= 10; + } + } + + result.Reverse(); + return string.Join("", result); + } + + #endregion + + #region 排列组合 + + /// + /// 计算排列数 P(n, r) = n! / (n-r)! + /// + /// 总数 + /// 选取数 + /// 排列数 + public static long Permutation(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0) + return 1; + + long result = 1; + + for (int i = n; i > n - r; i--) + { + result *= i; + } + + return result; + } + + /// + /// 计算组合数 C(n, r) = n! / (r! * (n-r)!) + /// + /// 总数 + /// 选取数 + /// 组合数 + public static long Combination(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0 || r == n) + return 1; + + // 使用较小的 r 计算 + r = Math.Min(r, n - r); + + long result = 1; + + for (int i = 0; i < r; i++) + { + result = result * (n - i) / (i + 1); + } + + return result; + } + + /// + /// 计算组合数(大数) + /// + /// 总数 + /// 选取数 + /// 组合数的字符串表示 + public static string CombinationBig(int n, int r) + { + if (n < 0 || r < 0 || r > n) + throw new ArgumentException("参数无效"); + + if (r == 0 || r == n) + return "1"; + + r = Math.Min(r, n - r); + + var numerator = new List(); + var denominator = new List(); + + for (int i = 0; i < r; i++) + { + numerator.Add(n - i); + denominator.Add(i + 1); + } + + // 约分 + for (int i = 0; i < denominator.Count; i++) + { + for (int j = 0; j < numerator.Count; j++) + { + var gcd = PrimeUtil.Gcd(numerator[j], denominator[i]); + + if (gcd > 1) + { + numerator[j] /= (int)gcd; + denominator[i] /= (int)gcd; + + if (denominator[i] == 1) + break; + } + } + } + + // 计算乘积 + var result = new List { 1 }; + + foreach (var num in numerator) + { + int carry = 0; + + for (int j = 0; j < result.Count; j++) + { + int product = result[j] * num + carry; + result[j] = product % 10; + carry = product / 10; + } + + while (carry > 0) + { + result.Add(carry % 10); + carry /= 10; + } + } + + result.Reverse(); + return string.Join("", result); + } + + #endregion + + #region 排列生成 + + /// + /// 生成所有排列 + /// + /// 元素类型 + /// 元素集合 + /// 所有排列 + public static List> GetAllPermutations(IEnumerable elements) + { + var list = elements.ToList(); + var result = new List>(); + + Permute(list, 0, result); + + return result; + } + + /// + /// 生成指定长度的排列 + /// + /// 元素类型 + /// 元素集合 + /// 排列长度 + /// 所有排列 + public static List> GetPermutations(IEnumerable elements, int length) + { + var list = elements.ToList(); + var result = new List>(); + + if (length > list.Count) + throw new ArgumentException("排列长度不能超过元素数量"); + + GeneratePermutations(list, length, new List(), new bool[list.Count], result); + + return result; + } + + /// + /// 生成下一个排列(字典序) + /// + /// 元素类型 + /// 当前排列(会被修改) + /// 是否存在下一个排列 + public static bool NextPermutation(List elements) where T : IComparable + { + int i = elements.Count - 2; + + while (i >= 0 && elements[i].CompareTo(elements[i + 1]) >= 0) + { + i--; + } + + if (i < 0) + return false; + + int j = elements.Count - 1; + + while (elements[j].CompareTo(elements[i]) <= 0) + { + j--; + } + + // 交换 + var temp = elements[i]; + elements[i] = elements[j]; + elements[j] = temp; + + // 反转 + Reverse(elements, i + 1, elements.Count - 1); + + return true; + } + + #endregion + + #region 组合生成 + + /// + /// 生成所有组合 + /// + /// 元素类型 + /// 元素集合 + /// 所有组合(包括空集) + public static List> GetAllCombinations(IEnumerable elements) + { + var list = elements.ToList(); + var result = new List>(); + + for (int i = 0; i <= list.Count; i++) + { + result.AddRange(GetCombinations(list, i)); + } + + return result; + } + + /// + /// 生成指定长度的组合 + /// + /// 元素类型 + /// 元素集合 + /// 组合长度 + /// 所有组合 + public static List> GetCombinations(IEnumerable elements, int length) + { + var list = elements.ToList(); + var result = new List>(); + + if (length > list.Count) + return result; + + GenerateCombinations(list, length, 0, new List(), result); + + return result; + } + + #endregion + + #region 其他组合数学 + + /// + /// 计算卡特兰数 C_n = C(2n, n) / (n+1) + /// + /// 索引 + /// 第 n 个卡特兰数 + public static long Catalan(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + return Combination(2 * n, n) / (n + 1); + } + + /// + /// 计算第 n 行的杨辉三角 + /// + /// 行号(从0开始) + /// 杨辉三角第 n 行 + public static List PascalRow(int n) + { + var row = new List { 1 }; + + for (int i = 1; i <= n; i++) + { + row.Add(row[i - 1] * (n - i + 1) / i); + } + + return row; + } + + /// + /// 生成杨辉三角 + /// + /// 行数 + /// 杨辉三角 + public static List> PascalTriangle(int rows) + { + var triangle = new List>(); + + for (int i = 0; i < rows; i++) + { + triangle.Add(PascalRow(i)); + } + + return triangle; + } + + /// + /// 计算贝尔数(集合划分数) + /// + /// 索引 + /// 第 n 个贝尔数 + public static long Bell(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + var bell = new long[n + 1, n + 1]; + bell[0, 0] = 1; + + for (int i = 1; i <= n; i++) + { + bell[i, 0] = bell[i - 1, i - 1]; + + for (int j = 1; j <= i; j++) + { + bell[i, j] = bell[i - 1, j - 1] + bell[i, j - 1]; + } + } + + return bell[n, 0]; + } + + /// + /// 计算斯特林数(第二类) + /// + /// 元素数 + /// 集合数 + /// 斯特林数 + public static long StirlingSecond(int n, int k) + { + if (n < 0 || k < 0 || k > n) + throw new ArgumentException("参数无效"); + + if (k == 0) + return n == 0 ? 1 : 0; + + if (k == 1) + return 1; + + if (k == n) + return 1; + + var stirling = new long[n + 1, k + 1]; + stirling[0, 0] = 1; + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= Math.Min(i, k); j++) + { + stirling[i, j] = j * stirling[i - 1, j] + stirling[i - 1, j - 1]; + } + } + + return stirling[n, k]; + } + + /// + /// 计算错排数 D_n + /// + /// 元素数 + /// 错排数 + public static long Derangement(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + if (n == 0) + return 1; + + if (n == 1) + return 0; + + long prev2 = 1, prev1 = 0; + + for (int i = 2; i <= n; i++) + { + long current = (i - 1) * (prev1 + prev2); + prev2 = prev1; + prev1 = current; + } + + return prev1; + } + + /// + /// 计算斐波那契数 + /// + /// 索引 + /// 第 n 个斐波那契数 + public static long Fibonacci(int n) + { + if (n < 0) + throw new ArgumentException("n 必须为非负整数"); + + if (n <= 1) + return n; + + long prev2 = 0, prev1 = 1; + + for (int i = 2; i <= n; i++) + { + long current = prev1 + prev2; + prev2 = prev1; + prev1 = current; + } + + return prev1; + } + + /// + /// 生成斐波那契数列 + /// + /// 数量 + /// 斐波那契数列 + public static List FibonacciSequence(int count) + { + var sequence = new List(); + + for (int i = 0; i < count; i++) + { + sequence.Add(Fibonacci(i)); + } + + return sequence; + } + + #endregion + + #region 私有方法 + + private static void Permute(List list, int start, List> result) + { + if (start == list.Count - 1) + { + result.Add(new List(list)); + return; + } + + for (int i = start; i < list.Count; i++) + { + Swap(list, start, i); + Permute(list, start + 1, result); + Swap(list, start, i); + } + } + + private static void GeneratePermutations(List list, int length, List current, bool[] used, List> result) + { + if (current.Count == length) + { + result.Add(new List(current)); + return; + } + + for (int i = 0; i < list.Count; i++) + { + if (used[i]) + continue; + + used[i] = true; + current.Add(list[i]); + + GeneratePermutations(list, length, current, used, result); + + current.RemoveAt(current.Count - 1); + used[i] = false; + } + } + + private static void GenerateCombinations(List list, int length, int start, List current, List> result) + { + if (current.Count == length) + { + result.Add(new List(current)); + return; + } + + for (int i = start; i < list.Count; i++) + { + current.Add(list[i]); + GenerateCombinations(list, length, i + 1, current, result); + current.RemoveAt(current.Count - 1); + } + } + + private static void Swap(List list, int i, int j) + { + var temp = list[i]; + list[i] = list[j]; + list[j] = temp; + } + + private static void Reverse(List list, int start, int end) + { + while (start < end) + { + Swap(list, start, end); + start++; + end--; + } + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/ComplexUtil.cs b/EasyTool.Core/MathCategory/ComplexUtil.cs new file mode 100644 index 0000000..cfe08b4 --- /dev/null +++ b/EasyTool.Core/MathCategory/ComplexUtil.cs @@ -0,0 +1,330 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 复数结构 + /// + public struct ComplexNumber : IEquatable, IFormattable + { + /// + /// 实部 + /// + public double Real { get; } + + /// + /// 虚部 + /// + public double Imaginary { get; } + + /// + /// 模(绝对值) + /// + public double Magnitude => Math.Sqrt(Real * Real + Imaginary * Imaginary); + + /// + /// 相位角(弧度) + /// + public double Phase => Math.Atan2(Imaginary, Real); + + /// + /// 共轭复数 + /// + public ComplexNumber Conjugate => new ComplexNumber(Real, -Imaginary); + + /// + /// 创建复数 + /// + public ComplexNumber(double real, double imaginary) + { + Real = real; + Imaginary = imaginary; + } + + #region 静态属性 + + /// + /// 零 + /// + public static ComplexNumber Zero => new ComplexNumber(0, 0); + + /// + /// 一 + /// + public static ComplexNumber One => new ComplexNumber(1, 0); + + /// + /// 虚数单位 i + /// + public static ComplexNumber ImaginaryOne => new ComplexNumber(0, 1); + + #endregion + + #region 静态方法 + + /// + /// 从极坐标创建复数 + /// + public static ComplexNumber FromPolarCoordinates(double magnitude, double phase) + { + return new ComplexNumber(magnitude * Math.Cos(phase), magnitude * Math.Sin(phase)); + } + + /// + /// 解析字符串为复数(支持格式: "a+bi", "a-bi", "a", "bi") + /// + public static ComplexNumber Parse(string s) + { + if (string.IsNullOrWhiteSpace(s)) + throw new ArgumentException("字符串不能为空"); + + s = s.Trim().Replace(" ", ""); + + // 尝试解析纯实数 + if (double.TryParse(s, out var real)) + return new ComplexNumber(real, 0); + + // 解析复数 + int iIndex = s.LastIndexOf('i'); + if (iIndex < 0) + throw new FormatException("无效的复数格式"); + + int signIndex = s.LastIndexOfAny(new[] { '+', '-' }, iIndex - 1, iIndex); + + if (signIndex < 0) + { + // 只有虚部 + var imaginaryStr = s.Substring(0, iIndex); + if (string.IsNullOrEmpty(imaginaryStr) || imaginaryStr == "+") + return new ComplexNumber(0, 1); + if (imaginaryStr == "-") + return new ComplexNumber(0, -1); + return new ComplexNumber(0, double.Parse(imaginaryStr)); + } + + var realStr = s.Substring(0, signIndex); + var imagPartStr = s.Substring(signIndex, iIndex - signIndex); + + var realPart = string.IsNullOrEmpty(realStr) ? 0 : double.Parse(realStr); + var imagPart = imagPartStr == "+" || imagPartStr == "" ? 1 : (imagPartStr == "-" ? -1 : double.Parse(imagPartStr)); + + return new ComplexNumber(realPart, imagPart); + } + + /// + /// 尝试解析字符串为复数 + /// + public static bool TryParse(string s, out ComplexNumber result) + { + try + { + result = Parse(s); + return true; + } + catch + { + result = Zero; + return false; + } + } + + #endregion + + #region 运算符重载 + + public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real + b.Real, a.Imaginary + b.Imaginary); + + public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real - b.Real, a.Imaginary - b.Imaginary); + + public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b) + => new ComplexNumber(a.Real * b.Real - a.Imaginary * b.Imaginary, a.Real * b.Imaginary + a.Imaginary * b.Real); + + public static ComplexNumber operator /(ComplexNumber a, ComplexNumber b) + { + var denom = b.Real * b.Real + b.Imaginary * b.Imaginary; + if (denom == 0) throw new DivideByZeroException(); + return new ComplexNumber( + (a.Real * b.Real + a.Imaginary * b.Imaginary) / denom, + (a.Imaginary * b.Real - a.Real * b.Imaginary) / denom); + } + + public static ComplexNumber operator -(ComplexNumber a) + => new ComplexNumber(-a.Real, -a.Imaginary); + + public static bool operator ==(ComplexNumber a, ComplexNumber b) + => a.Equals(b); + + public static bool operator !=(ComplexNumber a, ComplexNumber b) + => !a.Equals(b); + + public static implicit operator ComplexNumber(double value) + => new ComplexNumber(value, 0); + + #endregion + + #region 数学运算 + + /// + /// 平方根 + /// + public ComplexNumber Sqrt() + { + var m = Magnitude; + var r = Math.Sqrt((m + Real) / 2); + var i = Math.Sign(Imaginary) * Math.Sqrt((m - Real) / 2); + return new ComplexNumber(r, i); + } + + /// + /// 幂运算 + /// + public ComplexNumber Pow(double exponent) + { + var m = Math.Pow(Magnitude, exponent); + var p = Phase * exponent; + return FromPolarCoordinates(m, p); + } + + /// + /// 幂运算 + /// + public ComplexNumber Pow(ComplexNumber exponent) + { + return (exponent * Log()).Exp(); + } + + /// + /// 自然对数 + /// + public ComplexNumber Log() + { + return new ComplexNumber(Math.Log(Magnitude), Phase); + } + + /// + /// 指数函数 + /// + public ComplexNumber Exp() + { + return FromPolarCoordinates(Math.Exp(Real), Imaginary); + } + + /// + /// 正弦 + /// + public ComplexNumber Sin() + { + return new ComplexNumber( + Math.Sin(Real) * Math.Cosh(Imaginary), + Math.Cos(Real) * Math.Sinh(Imaginary)); + } + + /// + /// 余弦 + /// + public ComplexNumber Cos() + { + return new ComplexNumber( + Math.Cos(Real) * Math.Cosh(Imaginary), + -Math.Sin(Real) * Math.Sinh(Imaginary)); + } + + /// + /// 正切 + /// + public ComplexNumber Tan() + { + return Sin() / Cos(); + } + + #endregion + + #region 接口实现 + + public bool Equals(ComplexNumber other) + => Real.Equals(other.Real) && Imaginary.Equals(other.Imaginary); + + public override bool Equals(object? obj) + => obj is ComplexNumber other && Equals(other); + + public override int GetHashCode() + => HashCode.Combine(Real, Imaginary); + + public string ToString(string? format, IFormatProvider? formatProvider) + => $"({Real.ToString(format, formatProvider)}, {Imaginary.ToString(format, formatProvider)}i)"; + + public override string ToString() + => Imaginary >= 0 ? $"{Real}+{Imaginary}i" : $"{Real}{Imaginary}i"; + + #endregion + } + + /// + /// 复数运算工具类 + /// + public static class ComplexUtil + { + /// + /// 创建复数 + /// + public static ComplexNumber Create(double real, double imaginary) + => new ComplexNumber(real, imaginary); + + /// + /// 从极坐标创建复数 + /// + public static ComplexNumber FromPolar(double magnitude, double phase) + => ComplexNumber.FromPolarCoordinates(magnitude, phase); + + /// + /// 求和 + /// + public static ComplexNumber Sum(params ComplexNumber[] numbers) + { + var sum = ComplexNumber.Zero; + foreach (var n in numbers) + sum += n; + return sum; + } + + /// + /// 求积 + /// + public static ComplexNumber Product(params ComplexNumber[] numbers) + { + var product = ComplexNumber.One; + foreach (var n in numbers) + product *= n; + return product; + } + + /// + /// 平均值 + /// + public static ComplexNumber Average(params ComplexNumber[] numbers) + { + if (numbers.Length == 0) return ComplexNumber.Zero; + return Sum(numbers) / numbers.Length; + } + + /// + /// 欧拉公式 e^(ix) = cos(x) + i*sin(x) + /// + public static ComplexNumber Euler(double x) + => ComplexNumber.FromPolarCoordinates(1, x); + + /// + /// 解析字符串 + /// + public static ComplexNumber Parse(string s) + => ComplexNumber.Parse(s); + + /// + /// 尝试解析 + /// + public static bool TryParse(string s, out ComplexNumber result) + => ComplexNumber.TryParse(s, out result); + } +} diff --git a/EasyTool.Core/MathCategory/DistanceUtil.cs b/EasyTool.Core/MathCategory/DistanceUtil.cs new file mode 100644 index 0000000..f59e99a --- /dev/null +++ b/EasyTool.Core/MathCategory/DistanceUtil.cs @@ -0,0 +1,369 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 距离计算工具类 + /// 提供基于经纬度的距离计算、地理编码等功能 + /// + public static class DistanceUtil + { + /// + /// 地球半径(千米) + /// + public const double EarthRadiusKm = 6371.0; + + /// + /// 地球半径(米) + /// + public const double EarthRadiusM = 6371000.0; + + /// + /// 地球半径(英里) + /// + public const double EarthRadiusMile = 3958.8; + + #region Haversine 距离计算 + + /// + /// 使用 Haversine 公式计算两个坐标之间的球面距离 + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 距离(千米) + public static double Haversine(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadiusKm * c; + } + + /// + /// 计算两个坐标之间的距离(米) + /// + public static double DistanceInMeters(double lat1, double lon1, double lat2, double lon2) + { + return Haversine(lat1, lon1, lat2, lon2) * 1000; + } + + /// + /// 计算两个坐标之间的距离(英里) + /// + public static double DistanceInMiles(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadiusMile * c; + } + + #endregion + + #region 方位角计算 + + /// + /// 计算从起点到终点的方位角(初始方位角) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 方位角(度数,0-360,正北为0) + public static double Bearing(double lat1, double lon1, double lat2, double lon2) + { + var dLon = ToRadians(lon2 - lon1); + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + + var y = Math.Sin(dLon) * Math.Cos(lat2Rad); + var x = Math.Cos(lat1Rad) * Math.Sin(lat2Rad) - + Math.Sin(lat1Rad) * Math.Cos(lat2Rad) * Math.Cos(dLon); + + var bearing = Math.Atan2(y, x); + bearing = ToDegrees(bearing); + bearing = (bearing + 360) % 360; + + return bearing; + } + + /// + /// 根据方位角获取方向描述 + /// + /// 方位角 + /// 方向描述 + public static string GetDirectionName(double bearing) + { + bearing = ((bearing % 360) + 360) % 360; + + return bearing switch + { + >= 337.5 or < 22.5 => "正北", + >= 22.5 and < 67.5 => "东北", + >= 67.5 and < 112.5 => "正东", + >= 112.5 and < 157.5 => "东南", + >= 157.5 and < 202.5 => "正南", + >= 202.5 and < 247.5 => "西南", + >= 247.5 and < 292.5 => "正西", + >= 292.5 and < 337.5 => "西北", + _ => "未知" + }; + } + + #endregion + + #region 目标点计算 + + /// + /// 根据起点、方位角和距离计算终点坐标 + /// + /// 起点纬度 + /// 起点经度 + /// 方位角(度) + /// 距离(千米) + /// 终点坐标(纬度,经度) + public static (double Latitude, double Longitude) DestinationPoint( + double lat, double lon, double bearing, double distanceKm) + { + var bearingRad = ToRadians(bearing); + var lat1 = ToRadians(lat); + var lon1 = ToRadians(lon); + var d = distanceKm / EarthRadiusKm; + + var lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(d) + + Math.Cos(lat1) * Math.Sin(d) * Math.Cos(bearingRad)); + + var lon2 = lon1 + Math.Atan2( + Math.Sin(bearingRad) * Math.Sin(d) * Math.Cos(lat1), + Math.Cos(d) - Math.Sin(lat1) * Math.Sin(lat2)); + + return (ToDegrees(lat2), ToDegrees(lon2)); + } + + /// + /// 计算指定距离处的边界框(用于数据库查询) + /// + /// 中心点纬度 + /// 中心点经度 + /// 距离(千米) + /// 边界框(最小纬度,最小经度,最大纬度,最大经度) + public static (double MinLat, double MinLon, double MaxLat, double MaxLon) BoundingBox( + double lat, double lon, double distanceKm) + { + var latRad = ToRadians(lat); + var d = distanceKm / EarthRadiusKm; + + // 纬度变化 + var dLat = d; + var dLon = Math.Asin(Math.Sin(d) / Math.Cos(latRad)); + + var minLat = lat - ToDegrees(dLat); + var maxLat = lat + ToDegrees(dLat); + var minLon = lon - ToDegrees(dLon); + var maxLon = lon + ToDegrees(dLon); + + return (minLat, minLon, maxLat, maxLon); + } + + #endregion + + #region 中点计算 + + /// + /// 计算两个坐标之间的中点 + /// + public static (double Latitude, double Longitude) Midpoint( + double lat1, double lon1, double lat2, double lon2) + { + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + var lon1Rad = ToRadians(lon1); + var dLon = ToRadians(lon2 - lon1); + + var bx = Math.Cos(lat2Rad) * Math.Cos(dLon); + var by = Math.Cos(lat2Rad) * Math.Sin(dLon); + + var lat3 = Math.Atan2( + Math.Sin(lat1Rad) + Math.Sin(lat2Rad), + Math.Sqrt((Math.Cos(lat1Rad) + bx) * (Math.Cos(lat1Rad) + bx) + by * by)); + + var lon3 = lon1Rad + Math.Atan2(by, Math.Cos(lat1Rad) + bx); + + return (ToDegrees(lat3), ToDegrees(lon3)); + } + + #endregion + + #region 直线距离估算 + + /// + /// 使用勾股定理近似计算短距离(适用于小范围) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 距离(米) + public static double EuclideanDistance(double lat1, double lon1, double lat2, double lon2) + { + var avgLat = ToRadians((lat1 + lat2) / 2); + var latDist = ToRadians(lat2 - lat1) * EarthRadiusM; + var lonDist = ToRadians(lon2 - lon1) * EarthRadiusM * Math.Cos(avgLat); + + return Math.Sqrt(latDist * latDist + lonDist * lonDist); + } + + #endregion + + #region 驾驶距离估算 + + /// + /// 估算驾驶距离(直线距离乘以系数) + /// + /// 起点纬度 + /// 起点经度 + /// 终点纬度 + /// 终点经度 + /// 系数(默认1.4,城市间约为1.2-1.3,城市内约为1.4-1.6) + /// 估算驾驶距离(千米) + public static double EstimatedDrivingDistance( + double lat1, double lon1, double lat2, double lon2, double factor = 1.4) + { + return Haversine(lat1, lon1, lat2, lon2) * factor; + } + + #endregion + + #region 坐标转换 + + /// + /// 度转弧度 + /// + public static double ToRadians(double degrees) + { + return degrees * Math.PI / 180.0; + } + + /// + /// 弧度转度 + /// + public static double ToDegrees(double radians) + { + return radians * 180.0 / Math.PI; + } + + /// + /// 度分秒转十进制度 + /// + /// 度 + /// 分 + /// 秒 + /// 十进制度 + public static double DmsToDecimal(int degrees, int minutes, double seconds) + { + return degrees + minutes / 60.0 + seconds / 3600.0; + } + + /// + /// 十进制度转度分秒 + /// + /// 十进制度 + /// 度分秒元组 + public static (int Degrees, int Minutes, double Seconds) DecimalToDms(double decimalDegrees) + { + var degrees = (int)decimalDegrees; + var remainder = (decimalDegrees - degrees) * 60; + var minutes = (int)remainder; + var seconds = (remainder - minutes) * 60; + + return (degrees, minutes, seconds); + } + + #endregion + + #region 坐标验证 + + /// + /// 验证经度是否有效 + /// + public static bool IsValidLongitude(double longitude) + { + return longitude >= -180 && longitude <= 180; + } + + /// + /// 验证纬度是否有效 + /// + public static bool IsValidLatitude(double latitude) + { + return latitude >= -90 && latitude <= 90; + } + + /// + /// 验证坐标是否有效 + /// + public static bool IsValidCoordinate(double latitude, double longitude) + { + return IsValidLatitude(latitude) && IsValidLongitude(longitude); + } + + /// + /// 标准化经度到 -180 到 180 范围 + /// + public static double NormalizeLongitude(double longitude) + { + while (longitude > 180) longitude -= 360; + while (longitude < -180) longitude += 360; + return longitude; + } + + #endregion + + #region 格式化 + + /// + /// 格式化坐标为字符串 + /// + /// 纬度 + /// 经度 + /// 小数位数 + /// 格式化后的字符串 + public static string Format(double latitude, double longitude, int decimalPlaces = 6) + { + var latDir = latitude >= 0 ? "N" : "S"; + var lonDir = longitude >= 0 ? "E" : "W"; + + return $"{Math.Abs(latitude).ToString("F" + decimalPlaces)}°{latDir}, {Math.Abs(longitude).ToString("F" + decimalPlaces)}°{lonDir}"; + } + + /// + /// 格式化为度分秒 + /// + public static string FormatDms(double latitude, double longitude) + { + var (latDeg, latMin, latSec) = DecimalToDms(Math.Abs(latitude)); + var (lonDeg, lonMin, lonSec) = DecimalToDms(Math.Abs(longitude)); + + var latDir = latitude >= 0 ? "N" : "S"; + var lonDir = longitude >= 0 ? "E" : "W"; + + return $"{latDeg}°{latMin}'{latSec:F2}\"{latDir}, {lonDeg}°{lonMin}'{lonSec:F2}\"{lonDir}"; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/FractionUtil.cs b/EasyTool.Core/MathCategory/FractionUtil.cs new file mode 100644 index 0000000..78b0a63 --- /dev/null +++ b/EasyTool.Core/MathCategory/FractionUtil.cs @@ -0,0 +1,405 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 分数工具类 + /// 提供精确的有理数运算 + /// + public static class FractionUtil + { + /// + /// 创建分数 + /// + public static Fraction Create(long numerator, long denominator = 1) + { + return new Fraction(numerator, denominator); + } + + /// + /// 从小数创建分数 + /// + public static Fraction FromDouble(double value, long maxDenominator = 1000000) + { + return Fraction.FromDouble(value, maxDenominator); + } + + /// + /// 解析分数字符串(如 "3/4") + /// + public static Fraction Parse(string s) + { + return Fraction.Parse(s); + } + + /// + /// 尝试解析分数字符串 + /// + public static bool TryParse(string s, out Fraction result) + { + return Fraction.TryParse(s, out result); + } + + /// + /// 获取最小公倍数 + /// + public static long LCM(long a, long b) + { + return Math.Abs(a * b) / GCD(a, b); + } + + /// + /// 获取最大公约数 + /// + public static long GCD(long a, long b) + { + a = Math.Abs(a); + b = Math.Abs(b); + while (b != 0) + { + long temp = b; + b = a % b; + a = temp; + } + return a; + } + } + + /// + /// 分数(有理数) + /// + public readonly struct Fraction : IComparable, IEquatable + { + /// + /// 分子 + /// + public long Numerator { get; } + + /// + /// 分母 + /// + public long Denominator { get; } + + /// + /// 零 + /// + public static Fraction Zero => new(0, 1); + + /// + /// 一 + /// + public static Fraction One => new(1, 1); + + /// + /// 二分之一 + /// + public static Fraction Half => new(1, 2); + + /// + /// 创建分数 + /// + public Fraction(long numerator, long denominator = 1) + { + if (denominator == 0) + throw new DivideByZeroException("Denominator cannot be zero"); + + // 约分 + long gcd = FractionUtil.GCD(numerator, denominator); + numerator /= gcd; + denominator /= gcd; + + // 确保分母为正 + if (denominator < 0) + { + numerator = -numerator; + denominator = -denominator; + } + + Numerator = numerator; + Denominator = denominator; + } + + /// + /// 从小数创建分数 + /// + public static Fraction FromDouble(double value, long maxDenominator = 1000000) + { + if (double.IsNaN(value) || double.IsInfinity(value)) + throw new ArgumentException("Cannot convert NaN or infinity to fraction"); + + long sign = value < 0 ? -1 : 1; + value = Math.Abs(value); + + long wholePart = (long)value; + double fractionalPart = value - wholePart; + + if (fractionalPart < 1e-15) + { + return new Fraction(sign * wholePart, 1); + } + + // 使用连分数算法 + long numerator = 1; + long denominator = (long)(1 / fractionalPart); + double remainder = 1 / fractionalPart - denominator; + + while (Math.Abs(fractionalPart - (double)numerator / denominator) > 1e-15 && denominator < maxDenominator) + { + long newNumerator = denominator; + long newDenominator = (long)(1 / remainder); + remainder = 1 / remainder - newDenominator; + + if (newDenominator == 0 || denominator + newDenominator > maxDenominator) + break; + + numerator = newNumerator; + denominator = denominator + newDenominator; + } + + // 简化计算 + long bestDen = 1; + double bestError = Math.Abs(fractionalPart); + + for (long d = 1; d <= Math.Min(maxDenominator, 10000); d++) + { + long n = (long)Math.Round(fractionalPart * d); + double error = Math.Abs(fractionalPart - (double)n / d); + if (error < bestError) + { + bestError = error; + bestDen = d; + } + } + + long finalNumerator = (long)Math.Round(fractionalPart * bestDen); + return new Fraction(sign * (wholePart * bestDen + finalNumerator), bestDen); + } + + /// + /// 解析分数字符串 + /// + public static Fraction Parse(string s) + { + if (!TryParse(s, out var result)) + throw new FormatException($"Cannot parse '{s}' as fraction"); + return result; + } + + /// + /// 尝试解析分数字符串 + /// + public static bool TryParse(string s, out Fraction result) + { + result = Zero; + + if (string.IsNullOrWhiteSpace(s)) + return false; + + s = s.Trim(); + + // 处理负号 + int sign = 1; + if (s.StartsWith("-")) + { + sign = -1; + s = s.Substring(1); + } + + // 尝试解析为纯数字 + if (long.TryParse(s, out long whole)) + { + result = new Fraction(sign * whole, 1); + return true; + } + + // 尝试解析为分数 + if (s.Contains("/")) + { + var parts = s.Split('/'); + if (parts.Length == 2 && + long.TryParse(parts[0], out long num) && + long.TryParse(parts[1], out long den)) + { + result = new Fraction(sign * num, den); + return true; + } + } + + // 尝试解析为带分数(如 "1 1/2") + if (s.Contains(" ")) + { + var parts = s.Split(' '); + if (parts.Length == 2 && + long.TryParse(parts[0], out long whole2) && + parts[1].Contains("/")) + { + var fracParts = parts[1].Split('/'); + if (fracParts.Length == 2 && + long.TryParse(fracParts[0], out long num) && + long.TryParse(fracParts[1], out long den)) + { + result = new Fraction(sign * (whole2 * den + num), den); + return true; + } + } + } + + return false; + } + + /// + /// 转换为小数 + /// + public double ToDouble() => (double)Numerator / Denominator; + + /// + /// 转换为小数(decimal) + /// + public decimal ToDecimal() => (decimal)Numerator / Denominator; + + /// + /// 获取倒数 + /// + public Fraction Reciprocal => new(Denominator, Numerator); + + /// + /// 获取绝对值 + /// + public Fraction Abs => new(Math.Abs(Numerator), Denominator); + + /// + /// 取反 + /// + public Fraction Negate => new(-Numerator, Denominator); + + /// + /// 约分 + /// + public Fraction Simplify() + { + if (Numerator == 0) return Zero; + + long gcd = FractionUtil.GCD(Numerator, Denominator); + return new Fraction(Numerator / gcd, Denominator / gcd); + } + + /// + /// 转换为带分数 + /// + public (long Whole, Fraction Fractional) ToMixedNumber() + { + long whole = Numerator / Denominator; + long remainder = Numerator % Denominator; + return (whole, new Fraction(remainder, Denominator)); + } + + #region 运算符 + + public static Fraction operator +(Fraction a, Fraction b) + { + long den = FractionUtil.LCM(a.Denominator, b.Denominator); + long num = a.Numerator * (den / a.Denominator) + b.Numerator * (den / b.Denominator); + return new Fraction(num, den); + } + + public static Fraction operator -(Fraction a, Fraction b) + { + long den = FractionUtil.LCM(a.Denominator, b.Denominator); + long num = a.Numerator * (den / a.Denominator) - b.Numerator * (den / b.Denominator); + return new Fraction(num, den); + } + + public static Fraction operator *(Fraction a, Fraction b) + { + return new Fraction(a.Numerator * b.Numerator, a.Denominator * b.Denominator); + } + + public static Fraction operator /(Fraction a, Fraction b) + { + if (b.Numerator == 0) + throw new DivideByZeroException(); + return new Fraction(a.Numerator * b.Denominator, a.Denominator * b.Numerator); + } + + public static Fraction operator %(Fraction a, Fraction b) + { + return a - (a / b).Floor * b; + } + + public static Fraction operator +(Fraction a) => a; + public static Fraction operator -(Fraction a) => a.Negate; + + public static bool operator ==(Fraction a, Fraction b) => a.Equals(b); + public static bool operator !=(Fraction a, Fraction b) => !a.Equals(b); + public static bool operator <(Fraction a, Fraction b) => a.CompareTo(b) < 0; + public static bool operator >(Fraction a, Fraction b) => a.CompareTo(b) > 0; + public static bool operator <=(Fraction a, Fraction b) => a.CompareTo(b) <= 0; + public static bool operator >=(Fraction a, Fraction b) => a.CompareTo(b) >= 0; + + public static implicit operator Fraction(long value) => new(value, 1); + public static implicit operator Fraction(int value) => new(value, 1); + public static explicit operator double(Fraction f) => f.ToDouble(); + public static explicit operator decimal(Fraction f) => f.ToDecimal(); + + #endregion + + /// + /// 向下取整 + /// + public Fraction Floor => new(Numerator / Denominator, 1); + + /// + /// 向上取整 + /// + public Fraction Ceiling => new((Numerator + Denominator - 1) / Denominator, 1); + + /// + /// 四舍五入 + /// + public Fraction Round() + { + var mixed = ToMixedNumber(); + if (mixed.Fractional >= Half) + return new Fraction(mixed.Whole + 1, 1); + return new Fraction(mixed.Whole, 1); + } + + public int CompareTo(Fraction other) + { + return (Numerator * other.Denominator).CompareTo(other.Numerator * Denominator); + } + + public bool Equals(Fraction other) + { + return Numerator == other.Numerator && Denominator == other.Denominator; + } + + public override bool Equals(object obj) + { + return obj is Fraction other && Equals(other); + } + + public override int GetHashCode() + { + return HashCode.Combine(Numerator, Denominator); + } + + public override string ToString() + { + if (Denominator == 1) + return Numerator.ToString(); + return $"{Numerator}/{Denominator}"; + } + + /// + /// 转换为带分数字符串 + /// + public string ToMixedString() + { + var (whole, frac) = ToMixedNumber(); + if (whole == 0) return frac.ToString(); + if (frac.Numerator == 0) return whole.ToString(); + return $"{whole} {frac}"; + } + } +} diff --git a/EasyTool.Core/MathCategory/GeoUtil.cs b/EasyTool.Core/MathCategory/GeoUtil.cs new file mode 100644 index 0000000..7b49fa9 --- /dev/null +++ b/EasyTool.Core/MathCategory/GeoUtil.cs @@ -0,0 +1,283 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 地理坐标工具类 + /// 提供距离计算、坐标转换等功能 + /// + public static class GeoUtil + { + /// + /// 地球半径(米) + /// + public const double EarthRadius = 6371000; + + /// + /// 计算两点之间的距离(Haversine公式) + /// + /// 纬度1 + /// 经度1 + /// 纬度2 + /// 经度2 + /// 距离(米) + public static double Distance(double lat1, double lon1, double lat2, double lon2) + { + var dLat = ToRadians(lat2 - lat1); + var dLon = ToRadians(lon2 - lon1); + + var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + + Math.Cos(ToRadians(lat1)) * Math.Cos(ToRadians(lat2)) * + Math.Sin(dLon / 2) * Math.Sin(dLon / 2); + + var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); + + return EarthRadius * c; + } + + /// + /// 计算两点之间的方位角(从北向顺时针) + /// + public static double Bearing(double lat1, double lon1, double lat2, double lon2) + { + var dLon = ToRadians(lon2 - lon1); + var lat1Rad = ToRadians(lat1); + var lat2Rad = ToRadians(lat2); + + var y = Math.Sin(dLon) * Math.Cos(lat2Rad); + var x = Math.Cos(lat1Rad) * Math.Sin(lat2Rad) - + Math.Sin(lat1Rad) * Math.Cos(lat2Rad) * Math.Cos(dLon); + + var bearing = Math.Atan2(y, x); + return (ToDegrees(bearing) + 360) % 360; + } + + /// + /// 根据起点、方位角和距离计算终点 + /// + public static (double Latitude, double Longitude) Destination( + double startLat, double startLon, double bearing, double distance) + { + var bearingRad = ToRadians(bearing); + var lat1 = ToRadians(startLat); + var lon1 = ToRadians(startLon); + + var angularDistance = distance / EarthRadius; + + var lat2 = Math.Asin( + Math.Sin(lat1) * Math.Cos(angularDistance) + + Math.Cos(lat1) * Math.Sin(angularDistance) * Math.Cos(bearingRad)); + + var lon2 = lon1 + Math.Atan2( + Math.Sin(bearingRad) * Math.Sin(angularDistance) * Math.Cos(lat1), + Math.Cos(angularDistance) - Math.Sin(lat1) * Math.Sin(lat2)); + + return (ToDegrees(lat2), ToDegrees(lon2)); + } + + /// + /// 计算矩形边界(用于数据库范围查询) + /// + public static (double MinLat, double MinLon, double MaxLat, double MaxLon) GetBoundingBox( + double centerLat, double centerLon, double radiusInMeters) + { + var latChange = radiusInMeters / EarthRadius * (180 / Math.PI); + var lonChange = radiusInMeters / (EarthRadius * Math.Cos(ToRadians(centerLat))) * (180 / Math.PI); + + return ( + centerLat - latChange, + centerLon - lonChange, + centerLat + latChange, + centerLon + lonChange + ); + } + + /// + /// 判断点是否在矩形范围内 + /// + public static bool IsInBoundingBox( + double lat, double lon, + double minLat, double minLon, double maxLat, double maxLon) + { + return lat >= minLat && lat <= maxLat && lon >= minLon && lon <= maxLon; + } + + /// + /// 判断点是否在圆形范围内 + /// + public static bool IsInCircle( + double lat, double lon, + double centerLat, double centerLon, double radiusInMeters) + { + return Distance(lat, lon, centerLat, centerLon) <= radiusInMeters; + } + + /// + /// 判断点是否在多边形内 + /// + public static bool IsInPolygon(double lat, double lon, params (double Lat, double Lon)[] polygon) + { + if (polygon == null || polygon.Length < 3) + return false; + + var inside = false; + var j = polygon.Length - 1; + + for (int i = 0; i < polygon.Length; j = i++) + { + var xi = polygon[i].Lon; + var yi = polygon[i].Lat; + var xj = polygon[j].Lon; + var yj = polygon[j].Lat; + + var intersect = ((yi > lat) != (yj > lat)) && + (lon < (xj - xi) * (lat - yi) / (yj - yi) + xi); + + if (intersect) + inside = !inside; + } + + return inside; + } + + /// + /// 计算多边形面积(平方米) + /// + public static double PolygonArea(params (double Lat, double Lon)[] polygon) + { + if (polygon == null || polygon.Length < 3) + return 0; + + var area = 0.0; + var j = polygon.Length - 1; + + for (int i = 0; i < polygon.Length; j = i++) + { + var xi = ToRadians(polygon[i].Lon); + var yi = ToRadians(polygon[i].Lat); + var xj = ToRadians(polygon[j].Lon); + var yj = ToRadians(polygon[j].Lat); + + area += (xj - xi) * (2 + Math.Sin(yi) + Math.Sin(yj)); + } + + return Math.Abs(area * EarthRadius * EarthRadius / 2); + } + + #region 坐标转换 + + /// + /// WGS84转GCJ02(火星坐标) + /// + public static (double Lat, double Lon) Wgs84ToGcj02(double wgsLat, double wgsLon) + { + var dLat = TransformLat(wgsLon - 105.0, wgsLat - 35.0); + var dLon = TransformLon(wgsLon - 105.0, wgsLat - 35.0); + + var radLat = wgsLat / 180.0 * Math.PI; + var magic = Math.Sin(radLat); + magic = 1 - 0.00669342162296594323 * magic * magic; + var sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((EarthRadius / 1000) * (1 - 0.00669342162296594323) * sqrtMagic * Math.PI); + dLon = (dLon * 180.0) / ((EarthRadius / 1000) * sqrtMagic * Math.Cos(radLat) * Math.PI); + + return (wgsLat + dLat, wgsLon + dLon); + } + + /// + /// GCJ02转WGS84 + /// + public static (double Lat, double Lon) Gcj02ToWgs84(double gcjLat, double gcjLon) + { + var dLat = TransformLat(gcjLon - 105.0, gcjLat - 35.0); + var dLon = TransformLon(gcjLon - 105.0, gcjLat - 35.0); + + var radLat = gcjLat / 180.0 * Math.PI; + var magic = Math.Sin(radLat); + magic = 1 - 0.00669342162296594323 * magic * magic; + var sqrtMagic = Math.Sqrt(magic); + + dLat = (dLat * 180.0) / ((EarthRadius / 1000) * (1 - 0.00669342162296594323) * sqrtMagic * Math.PI); + dLon = (dLon * 180.0) / ((EarthRadius / 1000) * sqrtMagic * Math.Cos(radLat) * Math.PI); + + return (gcjLat - dLat, gcjLon - dLon); + } + + /// + /// BD09转GCJ02 + /// + public static (double Lat, double Lon) Bd09ToGcj02(double bdLat, double bdLon) + { + var x = bdLon - 0.0065; + var y = bdLat - 0.006; + var z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Math.PI * 3000.0 / 180.0); + var theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Math.PI * 3000.0 / 180.0); + + return (z * Math.Sin(theta), z * Math.Cos(theta)); + } + + /// + /// GCJ02转BD09 + /// + public static (double Lat, double Lon) Gcj02ToBd09(double gcjLat, double gcjLon) + { + var z = Math.Sqrt(gcjLon * gcjLon + gcjLat * gcjLat) + 0.00002 * Math.Sin(gcjLat * Math.PI * 3000.0 / 180.0); + var theta = Math.Atan2(gcjLat, gcjLon) + 0.000003 * Math.Cos(gcjLon * Math.PI * 3000.0 / 180.0); + + return (z * Math.Sin(theta) + 0.006, z * Math.Cos(theta) + 0.0065); + } + + /// + /// BD09转WGS84 + /// + public static (double Lat, double Lon) Bd09ToWgs84(double bdLat, double bdLon) + { + var gcj = Bd09ToGcj02(bdLat, bdLon); + return Gcj02ToWgs84(gcj.Lat, gcj.Lon); + } + + /// + /// WGS84转BD09 + /// + public static (double Lat, double Lon) Wgs84ToBd09(double wgsLat, double wgsLon) + { + var gcj = Wgs84ToGcj02(wgsLat, wgsLon); + return Gcj02ToBd09(gcj.Lat, gcj.Lon); + } + + private static double TransformLat(double x, double y) + { + var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(y * Math.PI) + 40.0 * Math.Sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; + ret += (160.0 * Math.Sin(y / 12.0 * Math.PI) + 320 * Math.Sin(y * Math.PI / 30.0)) * 2.0 / 3.0; + return ret; + } + + private static double TransformLon(double x, double y) + { + var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x)); + ret += (20.0 * Math.Sin(6.0 * x * Math.PI) + 20.0 * Math.Sin(2.0 * x * Math.PI)) * 2.0 / 3.0; + ret += (20.0 * Math.Sin(x * Math.PI) + 40.0 * Math.Sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; + ret += (150.0 * Math.Sin(x / 12.0 * Math.PI) + 300.0 * Math.Sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; + return ret; + } + + #endregion + + #region 辅助方法 + + private static double ToRadians(double degrees) + { + return degrees * Math.PI / 180.0; + } + + private static double ToDegrees(double radians) + { + return radians * 180.0 / Math.PI; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/GeometryUtil.cs b/EasyTool.Core/MathCategory/GeometryUtil.cs new file mode 100644 index 0000000..df01c4f --- /dev/null +++ b/EasyTool.Core/MathCategory/GeometryUtil.cs @@ -0,0 +1,593 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 几何工具类 + /// 提供点、线、面、多边形等几何计算功能 + /// + public static class GeometryUtil + { + #region 点 + + /// + /// 计算两点之间的距离 + /// + public static double Distance(Point2D p1, Point2D p2) + { + return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2)); + } + + /// + /// 计算两点之间的距离(3D) + /// + public static double Distance(Point3D p1, Point3D p2) + { + return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2) + Math.Pow(p2.Z - p1.Z, 2)); + } + + /// + /// 获取两点之间的中点 + /// + public static Point2D Midpoint(Point2D p1, Point2D p2) + { + return new Point2D((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2); + } + + /// + /// 点是否在线段上 + /// + public static bool IsPointOnLine(Point2D point, Line2D line, double tolerance = 1e-10) + { + // 使用叉积判断 + double cross = (point.Y - line.Start.Y) * (line.End.X - line.Start.X) - + (point.X - line.Start.X) * (line.End.Y - line.Start.Y); + + if (Math.Abs(cross) > tolerance) return false; + + // 检查是否在线段范围内 + return point.X >= Math.Min(line.Start.X, line.End.X) - tolerance && + point.X <= Math.Max(line.Start.X, line.End.X) + tolerance && + point.Y >= Math.Min(line.Start.Y, line.End.Y) - tolerance && + point.Y <= Math.Max(line.Start.Y, line.End.Y) + tolerance; + } + + /// + /// 点到直线的距离 + /// + public static double PointToLineDistance(Point2D point, Line2D line) + { + double A = line.End.Y - line.Start.Y; + double B = line.Start.X - line.End.X; + double C = line.End.X * line.Start.Y - line.Start.X * line.End.Y; + + return Math.Abs(A * point.X + B * point.Y + C) / Math.Sqrt(A * A + B * B); + } + + /// + /// 点到线段的最近点 + /// + public static Point2D ClosestPointOnSegment(Point2D point, Line2D line) + { + double dx = line.End.X - line.Start.X; + double dy = line.End.Y - line.Start.Y; + + if (Math.Abs(dx) < 1e-10 && Math.Abs(dy) < 1e-10) + return line.Start; + + double t = ((point.X - line.Start.X) * dx + (point.Y - line.Start.Y) * dy) / (dx * dx + dy * dy); + t = Math.Max(0, Math.Min(1, t)); + + return new Point2D(line.Start.X + t * dx, line.Start.Y + t * dy); + } + + #endregion + + #region 线 + + /// + /// 计算线段长度 + /// + public static double Length(Line2D line) + { + return Distance(line.Start, line.End); + } + + /// + /// 两条线段是否相交 + /// + public static bool Intersects(Line2D line1, Line2D line2) + { + return GetIntersection(line1, line2) != null; + } + + /// + /// 获取两条线段的交点 + /// + public static Point2D? GetIntersection(Line2D line1, Line2D line2) + { + double x1 = line1.Start.X, y1 = line1.Start.Y; + double x2 = line1.End.X, y2 = line1.End.Y; + double x3 = line2.Start.X, y3 = line2.Start.Y; + double x4 = line2.End.X, y4 = line2.End.Y; + + double denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); + + if (Math.Abs(denom) < 1e-10) return null; // 平行 + + double t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom; + double u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom; + + if (t >= 0 && t <= 1 && u >= 0 && u <= 1) + { + return new Point2D(x1 + t * (x2 - x1), y1 + t * (y2 - y1)); + } + + return null; + } + + /// + /// 计算两直线的夹角(弧度) + /// + public static double AngleBetween(Line2D line1, Line2D line2) + { + double dx1 = line1.End.X - line1.Start.X; + double dy1 = line1.End.Y - line1.Start.Y; + double dx2 = line2.End.X - line2.Start.X; + double dy2 = line2.End.Y - line2.Start.Y; + + double dot = dx1 * dx2 + dy1 * dy2; + double len1 = Math.Sqrt(dx1 * dx1 + dy1 * dy1); + double len2 = Math.Sqrt(dx2 * dx2 + dy2 * dy2); + + if (len1 < 1e-10 || len2 < 1e-10) return 0; + + double cos = dot / (len1 * len2); + cos = Math.Max(-1, Math.Min(1, cos)); + + return Math.Acos(cos); + } + + #endregion + + #region 多边形 + + /// + /// 计算多边形周长 + /// + public static double Perimeter(Polygon polygon) + { + double perimeter = 0; + var points = polygon.Points; + for (int i = 0; i < points.Count; i++) + { + int next = (i + 1) % points.Count; + perimeter += Distance(points[i], points[next]); + } + return perimeter; + } + + /// + /// 计算多边形面积(使用鞋带公式) + /// + public static double Area(Polygon polygon) + { + double area = 0; + var points = polygon.Points; + + for (int i = 0; i < points.Count; i++) + { + int next = (i + 1) % points.Count; + area += points[i].X * points[next].Y; + area -= points[next].X * points[i].Y; + } + + return Math.Abs(area) / 2; + } + + /// + /// 判断多边形是否为凸多边形 + /// + public static bool IsConvex(Polygon polygon) + { + var points = polygon.Points; + if (points.Count < 3) return false; + + bool? sign = null; + for (int i = 0; i < points.Count; i++) + { + int prev = (i - 1 + points.Count) % points.Count; + int next = (i + 1) % points.Count; + + double cross = CrossProduct( + points[prev], points[i], points[next]); + + if (cross != 0) + { + if (sign == null) + sign = cross > 0; + else if (sign != cross > 0) + return false; + } + } + + return true; + } + + private static double CrossProduct(Point2D o, Point2D a, Point2D b) + { + return (a.X - o.X) * (b.Y - o.Y) - (a.Y - o.Y) * (b.X - o.X); + } + + /// + /// 判断点是否在多边形内(射线法) + /// + public static bool IsPointInPolygon(Point2D point, Polygon polygon) + { + var points = polygon.Points; + int n = points.Count; + bool inside = false; + + for (int i = 0, j = n - 1; i < n; j = i++) + { + if (((points[i].Y > point.Y) != (points[j].Y > point.Y)) && + (point.X < (points[j].X - points[i].X) * (point.Y - points[i].Y) / (points[j].Y - points[i].Y) + points[i].X)) + { + inside = !inside; + } + } + + return inside; + } + + /// + /// 计算多边形质心 + /// + public static Point2D Centroid(Polygon polygon) + { + var points = polygon.Points; + double cx = 0, cy = 0; + + foreach (var p in points) + { + cx += p.X; + cy += p.Y; + } + + return new Point2D(cx / points.Count, cy / points.Count); + } + + /// + /// 计算凸包(Graham 扫描算法) + /// + public static Polygon ConvexHull(List points) + { + if (points.Count < 3) return new Polygon(points); + + // 找到最下方的点(y最小,y相同取x最小) + var start = points.OrderBy(p => p.Y).ThenBy(p => p.X).First(); + var sorted = points.Where(p => p != start).ToList(); + + // 按极角排序 + sorted.Sort((a, b) => + { + double angleA = Math.Atan2(a.Y - start.Y, a.X - start.X); + double angleB = Math.Atan2(b.Y - start.Y, b.X - start.X); + if (Math.Abs(angleA - angleB) < 1e-10) + { + return Distance(start, a).CompareTo(Distance(start, b)); + } + return angleA.CompareTo(angleB); + }); + + var hull = new List { start }; + + foreach (var point in sorted) + { + while (hull.Count > 1 && CrossProduct(hull[hull.Count - 2], hull[hull.Count - 1], point) <= 0) + { + hull.RemoveAt(hull.Count - 1); + } + hull.Add(point); + } + + return new Polygon(hull); + } + + /// + /// 多边形简化(Douglas-Peucker 算法) + /// + public static Polygon Simplify(Polygon polygon, double tolerance) + { + var points = polygon.Points; + if (points.Count < 3) return polygon; + + var result = DouglasPeucker(points, tolerance); + return new Polygon(result); + } + + private static List DouglasPeucker(List points, double tolerance) + { + if (points.Count <= 2) return points; + + double maxDist = 0; + int maxIndex = 0; + var line = new Line2D(points[0], points[points.Count - 1]); + + for (int i = 1; i < points.Count - 1; i++) + { + double dist = PointToLineDistance(points[i], line); + if (dist > maxDist) + { + maxDist = dist; + maxIndex = i; + } + } + + if (maxDist > tolerance) + { + var left = DouglasPeucker(points.GetRange(0, maxIndex + 1), tolerance); + var right = DouglasPeucker(points.GetRange(maxIndex, points.Count - maxIndex), tolerance); + + var result = new List(left); + result.AddRange(right.Skip(1)); + return result; + } + + return new List { points[0], points[points.Count - 1] }; + } + + #endregion + + #region 圆 + + /// + /// 计算圆的周长 + /// + public static double Circumference(Circle circle) + { + return 2 * Math.PI * circle.Radius; + } + + /// + /// 计算圆的面积 + /// + public static double Area(Circle circle) + { + return Math.PI * circle.Radius * circle.Radius; + } + + /// + /// 判断点是否在圆内 + /// + public static bool IsPointInCircle(Point2D point, Circle circle) + { + return Distance(point, circle.Center) <= circle.Radius; + } + + /// + /// 获取圆与直线的交点 + /// + public static List GetCircleLineIntersections(Circle circle, Line2D line) + { + var result = new List(); + + double dx = line.End.X - line.Start.X; + double dy = line.End.Y - line.Start.Y; + + double fx = line.Start.X - circle.Center.X; + double fy = line.Start.Y - circle.Center.Y; + + double a = dx * dx + dy * dy; + double b = 2 * (fx * dx + fy * dy); + double c = fx * fx + fy * fy - circle.Radius * circle.Radius; + + double discriminant = b * b - 4 * a * c; + + if (discriminant < 0) return result; + + discriminant = Math.Sqrt(discriminant); + + double t1 = (-b - discriminant) / (2 * a); + double t2 = (-b + discriminant) / (2 * a); + + if (t1 >= 0 && t1 <= 1) + result.Add(new Point2D(line.Start.X + t1 * dx, line.Start.Y + t1 * dy)); + + if (t2 >= 0 && t2 <= 1 && Math.Abs(t1 - t2) > 1e-10) + result.Add(new Point2D(line.Start.X + t2 * dx, line.Start.Y + t2 * dy)); + + return result; + } + + #endregion + + #region 三角形 + + /// + /// 计算三角形面积(海伦公式) + /// + public static double TriangleArea(Point2D a, Point2D b, Point2D c) + { + double ab = Distance(a, b); + double bc = Distance(b, c); + double ca = Distance(c, a); + double s = (ab + bc + ca) / 2; + + return Math.Sqrt(s * (s - ab) * (s - bc) * (s - ca)); + } + + /// + /// 判断点是否在三角形内 + /// + public static bool IsPointInTriangle(Point2D p, Point2D a, Point2D b, Point2D c) + { + double area = TriangleArea(a, b, c); + double area1 = TriangleArea(p, b, c); + double area2 = TriangleArea(a, p, c); + double area3 = TriangleArea(a, b, p); + + return Math.Abs(area - (area1 + area2 + area3)) < 1e-10; + } + + #endregion + } + + #region 几何类型定义 + + /// + /// 二维点 + /// + public struct Point2D : IEquatable + { + /// X坐标 + public double X { get; set; } + /// Y坐标 + public double Y { get; set; } + + public Point2D(double x, double y) { X = x; Y = y; } + + public static Point2D operator +(Point2D a, Point2D b) => new(a.X + b.X, a.Y + b.Y); + public static Point2D operator -(Point2D a, Point2D b) => new(a.X - b.X, a.Y - b.Y); + public static Point2D operator *(Point2D p, double scalar) => new(p.X * scalar, p.Y * scalar); + public static bool operator ==(Point2D left, Point2D right) => left.Equals(right); + public static bool operator !=(Point2D left, Point2D right) => !left.Equals(right); + + public double Length => Math.Sqrt(X * X + Y * Y); + public Point2D Normalize => this * (1 / Length); + + public bool Equals(Point2D other) => Math.Abs(X - other.X) < 1e-10 && Math.Abs(Y - other.Y) < 1e-10; + public override bool Equals(object? obj) => obj is Point2D other && Equals(other); + public override int GetHashCode() => HashCode.Combine(X, Y); + public override string ToString() => $"({X:F2}, {Y:F2})"; + } + + /// + /// 三维点 + /// + public struct Point3D + { + /// X坐标 + public double X { get; set; } + /// Y坐标 + public double Y { get; set; } + /// Z坐标 + public double Z { get; set; } + + public Point3D(double x, double y, double z) { X = x; Y = y; Z = z; } + + public static Point3D operator +(Point3D a, Point3D b) => new(a.X + b.X, a.Y + b.Y, a.Z + b.Z); + public static Point3D operator -(Point3D a, Point3D b) => new(a.X - b.X, a.Y - b.Y, a.Z - b.Z); + + public double Length => Math.Sqrt(X * X + Y * Y + Z * Z); + + public override string ToString() => $"({X:F2}, {Y:F2}, {Z:F2})"; + } + + /// + /// 二维线段 + /// + public struct Line2D + { + /// 起点 + public Point2D Start { get; set; } + /// 终点 + public Point2D End { get; set; } + + public Line2D(Point2D start, Point2D end) { Start = start; End = end; } + public Line2D(double x1, double y1, double x2, double y2) + : this(new Point2D(x1, y1), new Point2D(x2, y2)) { } + + public double Length => GeometryUtil.Distance(Start, End); + + public override string ToString() => $"[{Start} -> {End}]"; + } + + /// + /// 多边形 + /// + public class Polygon + { + /// 顶点列表 + public List Points { get; } + + public Polygon(IEnumerable points) + { + Points = new List(points); + } + + public int VertexCount => Points.Count; + public double Perimeter => GeometryUtil.Perimeter(this); + public double Area => GeometryUtil.Area(this); + public bool IsConvex => GeometryUtil.IsConvex(this); + public Point2D Centroid => GeometryUtil.Centroid(this); + + public override string ToString() => $"Polygon[{VertexCount} vertices, Area={Area:F2}]"; + } + + /// + /// 圆 + /// + public struct Circle + { + /// 圆心 + public Point2D Center { get; set; } + /// 半径 + public double Radius { get; set; } + + public Circle(Point2D center, double radius) { Center = center; Radius = radius; } + public Circle(double x, double y, double radius) + : this(new Point2D(x, y), radius) { } + + public double Circumference => GeometryUtil.Circumference(this); + public double Area => GeometryUtil.Area(this); + + public override string ToString() => $"Circle[Center={Center}, R={Radius:F2}]"; + } + + /// + /// 矩形 + /// + public struct Rectangle2D + { + /// 左上角X + public double X { get; set; } + /// 左上角Y + public double Y { get; set; } + /// 宽度 + public double Width { get; set; } + /// 高度 + public double Height { get; set; } + + public Rectangle2D(double x, double y, double width, double height) + { + X = x; Y = y; Width = width; Height = height; + } + + public double Left => X; + public double Top => Y; + public double Right => X + Width; + public double Bottom => Y + Height; + + public Point2D TopLeft => new(X, Y); + public Point2D TopRight => new(Right, Y); + public Point2D BottomLeft => new(X, Bottom); + public Point2D BottomRight => new(Right, Bottom); + public Point2D Center => new(X + Width / 2, Y + Height / 2); + + public double Perimeter => 2 * (Width + Height); + public double Area => Width * Height; + + public bool Contains(Point2D point) => + point.X >= X && point.X <= Right && point.Y >= Y && point.Y <= Bottom; + + public bool Intersects(Rectangle2D other) => + X < other.Right && Right > other.X && Y < other.Bottom && Bottom > other.Y; + + public override string ToString() => $"Rect[X={X}, Y={Y}, W={Width}, H={Height}]"; + } + + #endregion +} diff --git a/EasyTool.Core/MathCategory/InterpolationUtil.cs b/EasyTool.Core/MathCategory/InterpolationUtil.cs new file mode 100644 index 0000000..3f5fc81 --- /dev/null +++ b/EasyTool.Core/MathCategory/InterpolationUtil.cs @@ -0,0 +1,284 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.MathCategory +{ + /// + /// 插值工具类 + /// 提供各种插值算法 + /// + public static class InterpolationUtil + { + /// + /// 线性插值 + /// + public static double Linear(double x0, double y0, double x1, double y1, double x) + { + if (Math.Abs(x1 - x0) < double.Epsilon) + return y0; + + return y0 + (y1 - y0) * (x - x0) / (x1 - x0); + } + + /// + /// 双线性插值 + /// + public static double Bilinear(double x, double y, + double x1, double y1, double v11, + double x2, double y2, double v12, + double x3, double y3, double v21, + double x4, double y4, double v22) + { + double r1 = Linear(x1, v11, x2, v12, x); + double r2 = Linear(x3, v21, x4, v22, x); + return Linear(y1, r1, y3, r2, y); + } + + /// + /// 拉格朗日插值 + /// + public static double Lagrange(double[] xValues, double[] yValues, double x) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length == 0) + throw new ArgumentException("Arrays cannot be empty"); + + int n = xValues.Length; + double result = 0; + + for (int i = 0; i < n; i++) + { + double term = yValues[i]; + for (int j = 0; j < n; j++) + { + if (i != j) + { + term *= (x - xValues[j]) / (xValues[i] - xValues[j]); + } + } + result += term; + } + + return result; + } + + /// + /// 牛顿插值 + /// + public static double Newton(double[] xValues, double[] yValues, double x) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length == 0) + throw new ArgumentException("Arrays cannot be empty"); + + int n = xValues.Length; + + // 计算差商表 + double[,] dividedDiff = new double[n, n]; + for (int i = 0; i < n; i++) + { + dividedDiff[i, 0] = yValues[i]; + } + + for (int j = 1; j < n; j++) + { + for (int i = 0; i < n - j; i++) + { + dividedDiff[i, j] = (dividedDiff[i + 1, j - 1] - dividedDiff[i, j - 1]) / + (xValues[i + j] - xValues[i]); + } + } + + // 计算插值 + double result = dividedDiff[0, 0]; + double term = 1; + + for (int i = 1; i < n; i++) + { + term *= (x - xValues[i - 1]); + result += term * dividedDiff[0, i]; + } + + return result; + } + + /// + /// 创建三次样条插值器 + /// + public static CubicSpline CreateCubicSpline(double[] xValues, double[] yValues) + { + return new CubicSpline(xValues, yValues); + } + + /// + /// 创建线性插值器 + /// + public static LinearInterpolator CreateLinearInterpolator(double[] xValues, double[] yValues) + { + return new LinearInterpolator(xValues, yValues); + } + } + + /// + /// 三次样条插值 + /// + public class CubicSpline + { + private readonly double[] _x; + private readonly double[] _y; + private readonly double[] _m; // 二阶导数 + + /// + /// 数据点数量 + /// + public int Count => _x.Length; + + /// + /// 创建三次样条插值 + /// + public CubicSpline(double[] xValues, double[] yValues) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length < 2) + throw new ArgumentException("At least 2 points required"); + + _x = (double[])xValues.Clone(); + _y = (double[])yValues.Clone(); + _m = ComputeSecondDerivatives(); + } + + private double[] ComputeSecondDerivatives() + { + int n = _x.Length; + double[] m = new double[n]; + double[] u = new double[n - 1]; + double[] y = new double[n - 1]; + + // 自然边界条件 + m[0] = 0; + m[n - 1] = 0; + + // 追赶法求解三对角方程组 + for (int i = 1; i < n - 1; i++) + { + double hi = _x[i] - _x[i - 1]; + double hi1 = _x[i + 1] - _x[i]; + double alpha = hi / (hi + hi1); + double beta = (3 * (1 - alpha) * (_y[i] - _y[i - 1]) / hi + + 3 * alpha * (_y[i + 1] - _y[i]) / hi1) / (hi + hi1); + + double p = alpha * m[i - 1] + 2; + m[i] = (alpha - 1) / p; + u[i] = (beta - alpha * u[i - 1]) / p; + } + + for (int i = n - 2; i > 0; i--) + { + m[i] = m[i] * m[i + 1] + u[i]; + } + + return m; + } + + /// + /// 插值计算 + /// + public double Interpolate(double x) + { + int n = _x.Length; + + // 二分查找区间 + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) i = 1; + if (i >= n) i = n - 1; + + double h = _x[i] - _x[i - 1]; + double t = (x - _x[i - 1]) / h; + + // 三次样条公式 + double a = _y[i - 1]; + double b = (_y[i] - _y[i - 1]) / h - h * (_m[i] + 2 * _m[i - 1]) / 6; + double c = _m[i - 1] / 2; + double d = (_m[i] - _m[i - 1]) / (6 * h); + + return a + b * t * h + c * t * t * h * h + d * t * t * t * h * h * h; + } + + /// + /// 计算导数 + /// + public double Derivative(double x) + { + int n = _x.Length; + + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) i = 1; + if (i >= n) i = n - 1; + + double h = _x[i] - _x[i - 1]; + double t = (x - _x[i - 1]) / h; + + double b = (_y[i] - _y[i - 1]) / h - h * (_m[i] + 2 * _m[i - 1]) / 6; + double c = _m[i - 1]; + double d = (_m[i] - _m[i - 1]) / (2 * h); + + return b + c * t * h + d * t * t * h * h; + } + } + + /// + /// 线性插值器 + /// + public class LinearInterpolator + { + private readonly double[] _x; + private readonly double[] _y; + + /// + /// 数据点数量 + /// + public int Count => _x.Length; + + /// + /// 创建线性插值器 + /// + public LinearInterpolator(double[] xValues, double[] yValues) + { + if (xValues == null || yValues == null) + throw new ArgumentNullException(); + if (xValues.Length != yValues.Length) + throw new ArgumentException("Arrays must have the same length"); + if (xValues.Length < 2) + throw new ArgumentException("At least 2 points required"); + + _x = (double[])xValues.Clone(); + _y = (double[])yValues.Clone(); + } + + /// + /// 插值计算 + /// + public double Interpolate(double x) + { + int n = _x.Length; + + int i = Array.BinarySearch(_x, x); + if (i < 0) i = ~i; + if (i == 0) return _y[0]; + if (i >= n) return _y[n - 1]; + + return InterpolationUtil.Linear(_x[i - 1], _y[i - 1], _x[i], _y[i], x); + } + } +} diff --git a/EasyTool.Core/MathCategory/MathUtil.cs b/EasyTool.Core/MathCategory/MathUtil.cs index df37079..af442c1 100644 --- a/EasyTool.Core/MathCategory/MathUtil.cs +++ b/EasyTool.Core/MathCategory/MathUtil.cs @@ -1,297 +1,331 @@ -using System; +using System; using System.Collections.Generic; -using System.Text; +using System.Linq; -namespace EasyTool +namespace EasyTool.MathCategory { + /// + /// 数学计算工具类 + /// public static class MathUtil { /// - /// 计算两个整数的最大公约数 + /// 计算平均值 /// - /// 第一个整数 - /// 第二个整数 - /// 最大公约数 - public static int Gcd(int a, int b) + public static double Average(IEnumerable values) { - if (b == 0) - { - return a; - } - else - { - return Gcd(b, a % b); - } + var list = values.ToList(); + return list.Count == 0 ? 0 : list.Sum() / list.Count; } /// - /// 计算两个整数的最小公倍数 + /// 计算标准差 /// - /// 第一个整数 - /// 第二个整数 - /// 最小公倍数 - public static int Lcm(int a, int b) + public static double StandardDeviation(IEnumerable values) { - return a * b / Gcd(a, b); + var list = values.ToList(); + if (list.Count == 0) return 0; + + var avg = Average(list); + var sumOfSquares = list.Sum(v => Math.Pow(v - avg, 2)); + return Math.Sqrt(sumOfSquares / list.Count); } /// - /// 判断一个整数是否为质数 + /// 计算方差 /// - /// 要判断的整数 - /// 如果是质数,则返回 true;否则返回 false - public static bool IsPrime(int n) + public static double Variance(IEnumerable values) { - if (n <= 1) - { - return false; - } + var list = values.ToList(); + if (list.Count == 0) return 0; - for (int i = 2; i <= Math.Sqrt(n); i++) - { - if (n % i == 0) - { - return false; - } - } - - return true; + var avg = Average(list); + return list.Sum(v => Math.Pow(v - avg, 2)) / list.Count; } /// - /// 计算两个浮点数的差的绝对值是否小于指定的精度 + /// 计算中位数 /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 指定的精度 - /// 如果两个浮点数的差的绝对值小于指定的精度,则返回 true;否则返回 false - public static bool ApproxEqual(double a, double b, double eps) + public static double Median(IEnumerable values) { - return Math.Abs(a - b) < eps; + var sorted = values.OrderBy(v => v).ToList(); + if (sorted.Count == 0) return 0; + + var mid = sorted.Count / 2; + return sorted.Count % 2 == 0 + ? (sorted[mid - 1] + sorted[mid]) / 2 + : sorted[mid]; } /// - /// 求一个整数的阶乘 + /// 计算众数 /// - /// 要求阶乘的整数 - /// 阶乘结果 - public static int Factorial(int n) + public static List Mode(IEnumerable values) { - if (n <= 1) - { - return 1; - } - else - { - return n * Factorial(n - 1); - } + var groups = values.GroupBy(v => v) + .OrderByDescending(g => g.Count()) + .ToList(); + + if (groups.Count == 0) return new List(); + + var maxCount = groups[0].Count(); + return groups.Where(g => g.Count() == maxCount) + .Select(g => g.Key) + .ToList(); } /// - /// 求一个整数的斐波那契数列的值 + /// 计算百分位数 /// - /// 要求斐波那契数列的整数 - /// 斐波那契数列的值 - public static int Fibonacci(int n) + public static double Percentile(IEnumerable values, double percentile) { - if (n <= 1) - { - return n; - } - else - { - return Fibonacci(n - 1) + Fibonacci(n - 2); - } + var sorted = values.OrderBy(v => v).ToList(); + if (sorted.Count == 0) return 0; + + var index = (percentile / 100) * (sorted.Count - 1); + var lower = (int)Math.Floor(index); + var upper = (int)Math.Ceiling(index); + + if (lower == upper) return sorted[lower]; + + return sorted[lower] + (index - lower) * (sorted[upper] - sorted[lower]); } /// - /// 求一个整数的二进制表示中 1 的个数 + /// 限制值在指定范围内 /// - /// 要求二进制表示中 1 的个数的整数 - /// 二进制表示中 1 的个数 - public static int CountBits(int n) + public static double Clamp(double value, double min, double max) { - int count = 0; - - while (n != 0) - { - count++; - n &= n - 1; - } + return Math.Max(min, Math.Min(max, value)); + } - return count; + /// + /// 线性插值 + /// + public static double Lerp(double a, double b, double t) + { + return a + (b - a) * Clamp(t, 0, 1); } /// - /// 求两个浮点数的平均值 + /// 反向线性插值 /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 两个浮点数的平均值 - public static double Average(double a, double b) + public static double InverseLerp(double a, double b, double value) { - return (a + b) / 2; + if (a == b) return 0; + return Clamp((value - a) / (b - a), 0, 1); } /// - /// 求两个浮点数的中位数 + /// 映射值从一个范围到另一个范围 /// - /// 第一个浮点数 - /// 第二个浮点数 - /// 两个浮点数的中位数 - public static double Median(double a, double b) + public static double Remap(double value, double fromMin, double fromMax, double toMin, double toMax) { - return (a + b) / 2; + var t = InverseLerp(fromMin, fromMax, value); + return Lerp(toMin, toMax, t); } /// - /// 计算 n 的 k 次方 + /// 计算最大公约数 /// - /// 底数 - /// 指数 - /// n 的 k 次方 - public static int Pow(int n, int k) + public static long GCD(long a, long b) { - if (k == 0) - { - return 1; - } - else if (k % 2 == 0) - { - int half = Pow(n, k / 2); - return half * half; - } - else + a = Math.Abs(a); + b = Math.Abs(b); + + while (b != 0) { - int half = Pow(n, k / 2); - return half * half * n; + var temp = b; + b = a % b; + a = temp; } + + return a; } /// - /// 判断一个整数是否为完全平方数 + /// 计算最大公约数(别名) + /// + public static long Gcd(long a, long b) => GCD(a, b); + + /// + /// 计算最小公倍数 /// - /// 要判断的整数 - /// 如果是完全平方数,则返回 true;否则返回 false - public static bool IsPerfectSquare(int n) + public static long LCM(long a, long b) { - int sqrt = (int)Math.Sqrt(n); - return sqrt * sqrt == n; + if (a == 0 || b == 0) return 0; + return Math.Abs(a * b) / GCD(a, b); } /// - /// 计算一个整数的各个数位上数字的平方和,如果结果为 1,则返回 true;否则进行下一次计算,直到结果为 1 或者进入死循环为止 + /// 计算最小公倍数(别名) + /// + public static long Lcm(long a, long b) => LCM(a, b); + + /// + /// 判断是否为素数 /// - /// 要计算的整数 - /// 如果结果为 1,则返回 true;否则返回 false - public static bool IsHappyNumber(int n) + public static bool IsPrime(long n) { - int sum = n; + if (n < 2) return false; + if (n == 2) return true; + if (n % 2 == 0) return false; - while (true) + var sqrt = (long)Math.Sqrt(n); + for (long i = 3; i <= sqrt; i += 2) { - int digitsSum = 0; - while (sum > 0) - { - int digit = sum % 10; - digitsSum += digit * digit; - sum /= 10; - } + if (n % i == 0) return false; + } - if (digitsSum == 1) - { - return true; - } - else if (digitsSum == 4) + return true; + } + + /// + /// 获取所有素数因子 + /// + public static List GetPrimeFactors(long n) + { + var factors = new List(); + n = Math.Abs(n); + + while (n % 2 == 0) + { + factors.Add(2); + n /= 2; + } + + for (long i = 3; i * i <= n; i += 2) + { + while (n % i == 0) { - return false; + factors.Add(i); + n /= i; } - - sum = digitsSum; } + + if (n > 2) factors.Add(n); + + return factors; } /// - /// 计算两个整数的二进制表示中有多少位不同 + /// 计算阶乘 /// - /// 第一个整数 - /// 第二个整数 - /// 两个整数的二进制表示中有多少位不同 - public static int HammingDistance(int a, int b) + public static long Factorial(int n) { - int count = 0; - int xor = a ^ b; + if (n < 0) throw new ArgumentException("阶乘不支持负数"); + if (n <= 1) return 1; - while (xor != 0) + long result = 1; + for (int i = 2; i <= n; i++) { - count++; - xor &= xor - 1; + result *= i; } - return count; + return result; } /// - /// 求一个整数的所有因子 + /// 计算排列数 A(n, m) /// - /// 要求因子的整数 - /// 所有因子 - public static int[] GetAllFactors(int n) + public static long Permutation(int n, int m) { - int count = 0; + if (m > n) return 0; + if (m == 0) return 1; - for (int i = 1; i <= Math.Sqrt(n); i++) + long result = 1; + for (int i = 0; i < m; i++) { - if (n % i == 0) - { - count++; - if (i != n / i) - { - count++; - } - } + result *= (n - i); } - int[] factors = new int[count]; - int index = 0; + return result; + } + + /// + /// 计算组合数 C(n, m) + /// + public static long Combination(int n, int m) + { + if (m > n) return 0; + if (m == 0 || m == n) return 1; + + m = Math.Min(m, n - m); - for (int i = 1; i <= Math.Sqrt(n); i++) + long result = 1; + for (int i = 0; i < m; i++) { - if (n % i == 0) - { - factors[index++] = i; - if (i != n / i) - { - factors[index++] = n / i; - } - } + result = result * (n - i) / (i + 1); } - return factors; + return result; } /// - /// 计算两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue + /// 计算斐波那契数 /// - /// 第一个整数 - /// 第二个整数 - /// 两个整数的和,如果结果溢出了 int 类型的取值范围,则返回 int.MaxValue - public static int Add(int a, int b) + public static long Fibonacci(int n) { - int sum = a + b; + if (n < 0) throw new ArgumentException("斐波那契数不支持负数"); + if (n <= 1) return n; - if (a > 0 && b > 0 && sum < 0) + long a = 0, b = 1; + for (int i = 2; i <= n; i++) { - return int.MaxValue; - } - else if (a < 0 && b < 0 && sum > 0) - { - return int.MinValue; - } - else - { - return sum; + var temp = a + b; + a = b; + b = temp; } + + return b; + } + + /// + /// 判断是否在范围内 + /// + public static bool InRange(double value, double min, double max) + { + return value >= min && value <= max; + } + + /// + /// 判断两个浮点数是否近似相等 + /// + public static bool Approximately(double a, double b, double epsilon = 1e-10) + { + return Math.Abs(a - b) < epsilon; + } + + /// + /// 计算两点之间的距离 + /// + public static double Distance(double x1, double y1, double x2, double y2) + { + return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); + } + + /// + /// 计算两点之间的角度(弧度) + /// + public static double Angle(double x1, double y1, double x2, double y2) + { + return Math.Atan2(y2 - y1, x2 - x1); + } + + /// + /// 弧度转角度 + /// + public static double ToDegrees(double radians) + { + return radians * 180 / Math.PI; + } + + /// + /// 角度转弧度 + /// + public static double ToRadians(double degrees) + { + return degrees * Math.PI / 180; } } -} +} \ No newline at end of file diff --git a/EasyTool.Core/MathCategory/MatrixUtil.cs b/EasyTool.Core/MathCategory/MatrixUtil.cs new file mode 100644 index 0000000..7967045 --- /dev/null +++ b/EasyTool.Core/MathCategory/MatrixUtil.cs @@ -0,0 +1,560 @@ +using System; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 矩阵工具类 + /// 提供矩阵的基本运算 + /// + public static class MatrixUtil + { + #region 创建 + + /// + /// 创建矩阵 + /// + /// 行数 + /// 列数 + /// 初始值 + /// 矩阵 + public static Matrix Create(int rows, int cols, double value = 0) + { + return new Matrix(rows, cols, value); + } + + /// + /// 从二维数组创建矩阵 + /// + /// 二维数组 + /// 矩阵 + public static Matrix FromArray(double[,] array) + { + var rows = array.GetLength(0); + var cols = array.GetLength(1); + var matrix = new Matrix(rows, cols); + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix[i, j] = array[i, j]; + } + } + + return matrix; + } + + /// + /// 创建单位矩阵 + /// + /// 大小 + /// 单位矩阵 + public static Matrix Identity(int size) + { + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + { + matrix[i, i] = 1; + } + return matrix; + } + + /// + /// 创建零矩阵 + /// + /// 行数 + /// 列数 + /// 零矩阵 + public static Matrix Zeros(int rows, int cols) + { + return new Matrix(rows, cols); + } + + /// + /// 创建全1矩阵 + /// + /// 行数 + /// 列数 + /// 全1矩阵 + public static Matrix Ones(int rows, int cols) + { + return new Matrix(rows, cols, 1); + } + + /// + /// 创建对角矩阵 + /// + /// 对角元素 + /// 对角矩阵 + public static Matrix Diagonal(params double[] diagonal) + { + var size = diagonal.Length; + var matrix = new Matrix(size, size); + for (int i = 0; i < size; i++) + { + matrix[i, i] = diagonal[i]; + } + return matrix; + } + + /// + /// 创建随机矩阵 + /// + /// 行数 + /// 列数 + /// 最小值 + /// 最大值 + /// 随机矩阵 + public static Matrix Random(int rows, int cols, double min = 0, double max = 1) + { + var random = new Random(); + var matrix = new Matrix(rows, cols); + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix[i, j] = random.NextDouble() * (max - min) + min; + } + } + + return matrix; + } + + #endregion + + #region 运算 + + /// + /// 矩阵加法 + /// + public static Matrix Add(Matrix a, Matrix b) + { + if (a.Rows != b.Rows || a.Cols != b.Cols) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j] + b[i, j]; + } + } + return result; + } + + /// + /// 矩阵减法 + /// + public static Matrix Subtract(Matrix a, Matrix b) + { + if (a.Rows != b.Rows || a.Cols != b.Cols) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j] - b[i, j]; + } + } + return result; + } + + /// + /// 矩阵乘法 + /// + public static Matrix Multiply(Matrix a, Matrix b) + { + if (a.Cols != b.Rows) + throw new ArgumentException("矩阵维度不匹配"); + + var result = new Matrix(a.Rows, b.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < b.Cols; j++) + { + double sum = 0; + for (int k = 0; k < a.Cols; k++) + { + sum += a[i, k] * b[k, j]; + } + result[i, j] = sum; + } + } + return result; + } + + /// + /// 标量乘法 + /// + public static Matrix Scale(Matrix matrix, double scalar) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[i, j] * scalar; + } + } + return result; + } + + /// + /// 矩阵转置 + /// + public static Matrix Transpose(Matrix matrix) + { + var result = new Matrix(matrix.Cols, matrix.Rows); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[j, i] = matrix[i, j]; + } + } + return result; + } + + /// + /// 行列式 + /// + public static double Determinant(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + return DeterminantInternal(matrix); + } + + private static double DeterminantInternal(Matrix matrix) + { + int n = matrix.Rows; + + if (n == 1) + return matrix[0, 0]; + + if (n == 2) + return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]; + + double det = 0; + for (int j = 0; j < n; j++) + { + det += matrix[0, j] * Cofactor(matrix, 0, j); + } + return det; + } + + private static double Cofactor(Matrix matrix, int row, int col) + { + var minor = GetMinor(matrix, row, col); + return Math.Pow(-1, row + col) * DeterminantInternal(minor); + } + + private static Matrix GetMinor(Matrix matrix, int excludeRow, int excludeCol) + { + var minor = new Matrix(matrix.Rows - 1, matrix.Cols - 1); + int mi = 0, mj = 0; + + for (int i = 0; i < matrix.Rows; i++) + { + if (i == excludeRow) continue; + + mj = 0; + for (int j = 0; j < matrix.Cols; j++) + { + if (j == excludeCol) continue; + minor[mi, mj] = matrix[i, j]; + mj++; + } + mi++; + } + + return minor; + } + + /// + /// 逆矩阵 + /// + public static Matrix? Inverse(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + var det = Determinant(matrix); + if (Math.Abs(det) < double.Epsilon) + return null; + + int n = matrix.Rows; + var result = new Matrix(n, n); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + result[i, j] = Cofactor(matrix, i, j) / det; + } + } + + // 转置得到逆矩阵 + return Transpose(result); + } + + /// + /// 迹(对角元素之和) + /// + public static double Trace(Matrix matrix) + { + if (!matrix.IsSquare) + throw new ArgumentException("矩阵必须是方阵"); + + double trace = 0; + for (int i = 0; i < matrix.Rows; i++) + { + trace += matrix[i, i]; + } + return trace; + } + + /// + /// Frobenius 范数 + /// + public static double FrobeniusNorm(Matrix matrix) + { + double sum = 0; + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + sum += matrix[i, j] * matrix[i, j]; + } + } + return Math.Sqrt(sum); + } + + #endregion + + #region 变换 + + /// + /// 水平翻转 + /// + public static Matrix FlipHorizontal(Matrix matrix) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[i, matrix.Cols - 1 - j]; + } + } + return result; + } + + /// + /// 垂直翻转 + /// + public static Matrix FlipVertical(Matrix matrix) + { + var result = new Matrix(matrix.Rows, matrix.Cols); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[i, j] = matrix[matrix.Rows - 1 - i, j]; + } + } + return result; + } + + /// + /// 顺时针旋转 90 度 + /// + public static Matrix Rotate90(Matrix matrix) + { + var result = new Matrix(matrix.Cols, matrix.Rows); + for (int i = 0; i < matrix.Rows; i++) + { + for (int j = 0; j < matrix.Cols; j++) + { + result[j, matrix.Rows - 1 - i] = matrix[i, j]; + } + } + return result; + } + + /// + /// 水平拼接 + /// + public static Matrix HorizontalConcat(Matrix a, Matrix b) + { + if (a.Rows != b.Rows) + throw new ArgumentException("矩阵行数不匹配"); + + var result = new Matrix(a.Rows, a.Cols + b.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j]; + } + for (int j = 0; j < b.Cols; j++) + { + result[i, a.Cols + j] = b[i, j]; + } + } + return result; + } + + /// + /// 垂直拼接 + /// + public static Matrix VerticalConcat(Matrix a, Matrix b) + { + if (a.Cols != b.Cols) + throw new ArgumentException("矩阵列数不匹配"); + + var result = new Matrix(a.Rows + b.Rows, a.Cols); + for (int i = 0; i < a.Rows; i++) + { + for (int j = 0; j < a.Cols; j++) + { + result[i, j] = a[i, j]; + } + } + for (int i = 0; i < b.Rows; i++) + { + for (int j = 0; j < b.Cols; j++) + { + result[a.Rows + i, j] = b[i, j]; + } + } + return result; + } + + #endregion + } + + /// + /// 矩阵类 + /// + public class Matrix + { + private readonly double[,] _data; + + /// + /// 行数 + /// + public int Rows { get; } + + /// + /// 列数 + /// + public int Cols { get; } + + /// + /// 是否为方阵 + /// + public bool IsSquare => Rows == Cols; + + /// + /// 访问元素 + /// + public double this[int row, int col] + { + get => _data[row, col]; + set => _data[row, col] = value; + } + + /// + /// 创建矩阵 + /// + public Matrix(int rows, int cols, double value = 0) + { + Rows = rows; + Cols = cols; + _data = new double[rows, cols]; + + if (value != 0) + { + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + _data[i, j] = value; + } + } + } + } + + /// + /// 获取行 + /// + public double[] GetRow(int row) + { + var result = new double[Cols]; + for (int j = 0; j < Cols; j++) + { + result[j] = _data[row, j]; + } + return result; + } + + /// + /// 获取列 + /// + public double[] GetColumn(int col) + { + var result = new double[Rows]; + for (int i = 0; i < Rows; i++) + { + result[i] = _data[i, col]; + } + return result; + } + + /// + /// 转换为二维数组 + /// + public double[,] ToArray() + { + var result = new double[Rows, Cols]; + Array.Copy(_data, result, _data.Length); + return result; + } + + /// + /// 转换为字符串 + /// + public override string ToString() + { + var sb = new System.Text.StringBuilder(); + for (int i = 0; i < Rows; i++) + { + sb.Append("["); + for (int j = 0; j < Cols; j++) + { + sb.Append(_data[i, j].ToString("F4").PadLeft(10)); + if (j < Cols - 1) sb.Append(", "); + } + sb.AppendLine("]"); + } + return sb.ToString(); + } + + #region 运算符重载 + + public static Matrix operator +(Matrix a, Matrix b) => MatrixUtil.Add(a, b); + public static Matrix operator -(Matrix a, Matrix b) => MatrixUtil.Subtract(a, b); + public static Matrix operator *(Matrix a, Matrix b) => MatrixUtil.Multiply(a, b); + public static Matrix operator *(Matrix a, double scalar) => MatrixUtil.Scale(a, scalar); + public static Matrix operator *(double scalar, Matrix a) => MatrixUtil.Scale(a, scalar); + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/MoneyUtil.cs b/EasyTool.Core/MathCategory/MoneyUtil.cs new file mode 100644 index 0000000..1a291bf --- /dev/null +++ b/EasyTool.Core/MathCategory/MoneyUtil.cs @@ -0,0 +1,470 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.MathCategory +{ + /// + /// 金额工具类 + /// 提供精确的金额计算、格式化、大写转换等功能 + /// + public static class MoneyUtil + { + #region 金额计算 + + /// + /// 精确加法运算 + /// + /// 金额1 + /// 金额2 + /// 结果 + public static decimal Add(decimal amount1, decimal amount2) + { + return decimal.Add(amount1, amount2); + } + + /// + /// 精确减法运算 + /// + /// 金额1 + /// 金额2 + /// 结果 + public static decimal Subtract(decimal amount1, decimal amount2) + { + return decimal.Subtract(amount1, amount2); + } + + /// + /// 精确乘法运算 + /// + /// 金额 + /// 乘数 + /// 结果 + public static decimal Multiply(decimal amount, decimal multiplier) + { + return decimal.Multiply(amount, multiplier); + } + + /// + /// 精确除法运算 + /// + /// 金额 + /// 除数 + /// 保留小数位数(默认2) + /// 结果 + public static decimal Divide(decimal amount, decimal divisor, int decimals = 2) + { + if (divisor == 0) + throw new DivideByZeroException("除数不能为0"); + + return Math.Round(amount / divisor, decimals, MidpointRounding.AwayFromZero); + } + + /// + /// 四舍五入 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Round(decimal amount, int decimals = 2) + { + return Math.Round(amount, decimals, MidpointRounding.AwayFromZero); + } + + /// + /// 向上取整 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Ceiling(decimal amount, int decimals = 0) + { + var factor = (decimal)Math.Pow(10, decimals); + return Math.Ceiling(amount * factor) / factor; + } + + /// + /// 向下取整 + /// + /// 金额 + /// 保留小数位数 + /// 结果 + public static decimal Floor(decimal amount, int decimals = 0) + { + var factor = (decimal)Math.Pow(10, decimals); + return Math.Floor(amount * factor) / factor; + } + + /// + /// 计算百分比 + /// + /// 金额 + /// 百分比(如25表示25%) + /// 保留小数位数 + /// 结果 + public static decimal Percentage(decimal amount, decimal percentage, int decimals = 2) + { + return Round(amount * percentage / 100, decimals); + } + + /// + /// 计算折扣金额 + /// + /// 原价 + /// 折扣(如8表示8折) + /// 保留小数位数 + /// 折后价 + public static decimal Discount(decimal originalPrice, decimal discount, int decimals = 2) + { + return Round(originalPrice * discount / 10, decimals); + } + + /// + /// 计算利息 + /// + /// 本金 + /// 年利率(如5.5表示5.5%) + /// 天数 + /// 保留小数位数 + /// 利息 + public static decimal Interest(decimal principal, decimal rate, int days, int decimals = 2) + { + return Round(principal * rate / 100 * days / 365, decimals); + } + + #endregion + + #region 格式化 + + /// + /// 格式化金额(默认2位小数,千分位) + /// + /// 金额 + /// 小数位数 + /// 货币符号 + /// 格式化后的字符串 + public static string Format(decimal amount, int decimals = 2, string symbol = "¥") + { + return $"{symbol}{amount.ToString("N" + decimals)}"; + } + + /// + /// 格式化为人民币格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatCNY(decimal amount) + { + return Format(amount, 2, "¥"); + } + + /// + /// 格式化为美元格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatUSD(decimal amount) + { + return Format(amount, 2, "$"); + } + + /// + /// 格式化为欧元格式 + /// + /// 金额 + /// 格式化后的字符串 + public static string FormatEUR(decimal amount) + { + return Format(amount, 2, "€"); + } + + #endregion + + #region 金额大写 + + private static readonly string[] ChineseDigits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + private static readonly string[] ChineseUnits = { "", "拾", "佰", "仟" }; + private static readonly string[] ChineseBigUnits = { "", "万", "亿", "万亿" }; + + /// + /// 转换为人民币大写金额 + /// + /// 金额 + /// 大写金额 + public static string ToChineseUpper(decimal amount) + { + if (amount < 0) + return "负" + ToChineseUpper(-amount); + + if (amount == 0) + return "零元整"; + + // 处理超出范围的金额 + if (amount >= 10000000000000000m) + throw new ArgumentOutOfRangeException(nameof(amount), "金额超出转换范围"); + + var result = new StringBuilder(); + var amountStr = amount.ToString("F2"); + var parts = amountStr.Split('.'); + + // 整数部分 + var integerPart = long.Parse(parts[0]); + if (integerPart > 0) + { + result.Append(ConvertIntegerToChinese(integerPart)); + result.Append("元"); + } + + // 小数部分 + if (parts.Length > 1) + { + var decimalPart = parts[1]; + var jiao = int.Parse(decimalPart[0].ToString()); + var fen = int.Parse(decimalPart[1].ToString()); + + if (jiao > 0) + { + result.Append(ChineseDigits[jiao]); + result.Append("角"); + } + + if (fen > 0) + { + if (jiao == 0 && integerPart > 0) + result.Append("零"); + result.Append(ChineseDigits[fen]); + result.Append("分"); + } + } + + // 只有整数部分 + if (result.ToString().EndsWith("元")) + { + result.Append("整"); + } + + return result.ToString(); + } + + private static string ConvertIntegerToChinese(long number) + { + if (number == 0) + return ChineseDigits[0]; + + var result = new StringBuilder(); + var unitIndex = 0; + var zeroFlag = false; + + while (number > 0) + { + var section = (int)(number % 10000); + var sectionStr = ConvertSectionToChinese(section, zeroFlag); + + if (section > 0) + { + result.Insert(0, ChineseBigUnits[unitIndex]); + result.Insert(0, sectionStr); + zeroFlag = false; + } + else + { + zeroFlag = true; + } + + number /= 10000; + unitIndex++; + } + + return result.ToString(); + } + + private static string ConvertSectionToChinese(int section, bool zeroFlag) + { + var result = new StringBuilder(); + var unitIndex = 0; + var hasZero = zeroFlag; + + while (section > 0) + { + var digit = section % 10; + + if (digit > 0) + { + result.Insert(0, ChineseUnits[unitIndex]); + result.Insert(0, ChineseDigits[digit]); + hasZero = false; + } + else if (!hasZero && unitIndex > 0) + { + result.Insert(0, ChineseDigits[0]); + hasZero = true; + } + + section /= 10; + unitIndex++; + } + + return result.ToString(); + } + + /// + /// 人民币大写金额转数字 + /// + /// 大写金额 + /// 数字金额 + public static decimal FromChineseUpper(string chineseAmount) + { + if (string.IsNullOrWhiteSpace(chineseAmount)) + return 0; + + // 移除"人民币"、"整"等 + chineseAmount = chineseAmount.Replace("人民币", "").Replace("整", "").Trim(); + + if (chineseAmount == "零元") + return 0; + + var digitMap = new Dictionary + { + {'零', 0}, {'壹', 1}, {'贰', 2}, {'叁', 3}, {'肆', 4}, + {'伍', 5}, {'陆', 6}, {'柒', 7}, {'捌', 8}, {'玖', 9} + }; + + var unitMap = new Dictionary + { + {'拾', 10}, {'佰', 100}, {'仟', 1000}, + {'万', 10000}, {'亿', 100000000} + }; + + decimal result = 0; + decimal temp = 0; + decimal section = 0; + + foreach (var c in chineseAmount) + { + if (c == '元') + { + result += temp + section; + temp = 0; + section = 0; + } + else if (c == '角') + { + result += temp / 10m; + temp = 0; + } + else if (c == '分') + { + result += temp / 100m; + temp = 0; + } + else if (digitMap.ContainsKey(c)) + { + temp = digitMap[c]; + } + else if (c == '拾' || c == '佰' || c == '仟') + { + section += temp * unitMap[c]; + temp = 0; + } + else if (c == '万' || c == '亿') + { + section = (section + temp) * unitMap[c]; + temp = 0; + } + } + + return result + section + temp; + } + + #endregion + + #region 汇率转换(简化版) + + /// + /// 常用货币汇率(相对于人民币,仅供参考) + /// + private static readonly Dictionary ExchangeRates = new() + { + { "CNY", 1.0m }, + { "USD", 7.2m }, + { "EUR", 7.8m }, + { "GBP", 9.1m }, + { "JPY", 0.048m }, + { "KRW", 0.0054m }, + { "HKD", 0.92m }, + { "TWD", 0.22m } + }; + + /// + /// 货币转换 + /// + /// 金额 + /// 源货币代码 + /// 目标货币代码 + /// 保留小数位数 + /// 转换后的金额 + public static decimal Convert(decimal amount, string fromCurrency, string toCurrency, int decimals = 2) + { + fromCurrency = fromCurrency.ToUpperInvariant(); + toCurrency = toCurrency.ToUpperInvariant(); + + if (!ExchangeRates.ContainsKey(fromCurrency)) + throw new ArgumentException($"不支持的货币: {fromCurrency}"); + + if (!ExchangeRates.ContainsKey(toCurrency)) + throw new ArgumentException($"不支持的货币: {toCurrency}"); + + // 先转为人民币,再转为目标货币 + var cny = amount * ExchangeRates[fromCurrency]; + var result = cny / ExchangeRates[toCurrency]; + + return Round(result, decimals); + } + + /// + /// 获取支持的货币列表 + /// + /// 货币代码列表 + public static IEnumerable GetSupportedCurrencies() + { + return ExchangeRates.Keys; + } + + /// + /// 更新汇率 + /// + /// 货币代码 + /// 对人民币汇率 + public static void UpdateExchangeRate(string currency, decimal rateToCNY) + { + ExchangeRates[currency.ToUpperInvariant()] = rateToCNY; + } + + #endregion + + #region 分转元 + + /// + /// 分转元 + /// + /// 分 + /// + public static decimal FenToYuan(long fen) + { + return fen / 100m; + } + + /// + /// 元转分 + /// + /// 元 + /// + public static long YuanToFen(decimal yuan) + { + return (long)Math.Round(yuan * 100, MidpointRounding.AwayFromZero); + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/NumberExtension.cs b/EasyTool.Core/MathCategory/NumberExtension.cs new file mode 100644 index 0000000..07fe0e4 --- /dev/null +++ b/EasyTool.Core/MathCategory/NumberExtension.cs @@ -0,0 +1,418 @@ +using System; + +namespace EasyTool.MathCategory +{ + /// + /// 数字类型扩展方法 + /// + public static class NumberExtension + { + #region 整数扩展 + + /// + /// 判断整数是否为偶数 + /// + public static bool IsEven(this int value) + { + return value % 2 == 0; + } + + /// + /// 判断整数是否为奇数 + /// + public static bool IsOdd(this int value) + { + return value % 2 != 0; + } + + /// + /// 判断长整数是否为偶数 + /// + public static bool IsEven(this long value) + { + return value % 2 == 0; + } + + /// + /// 判断长整数是否为奇数 + /// + public static bool IsOdd(this long value) + { + return value % 2 != 0; + } + + /// + /// 判断短整数是否为偶数 + /// + public static bool IsEven(this short value) + { + return value % 2 == 0; + } + + /// + /// 判断短整数是否为奇数 + /// + public static bool IsOdd(this short value) + { + return value % 2 != 0; + } + + #endregion + + #region 浮点数扩展 + + /// + /// 判断浮点数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this float value, float min, float max) + { + return value >= min && value <= max; + } + + /// + /// 判断双精度浮点数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this double value, double min, double max) + { + return value >= min && value <= max; + } + + /// + /// 判断小数是否在指定范围内(包含边界) + /// + public static bool IsBetween(this decimal value, decimal min, decimal max) + { + return value >= min && value <= max; + } + + /// + /// 限制浮点数在指定范围内 + /// + public static float Clamp(this float value, float min, float max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制双精度浮点数在指定范围内 + /// + public static double Clamp(this double value, double min, double max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制小数在指定范围内 + /// + public static decimal Clamp(this decimal value, decimal min, decimal max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制整数在指定范围内 + /// + public static int Clamp(this int value, int min, int max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + /// + /// 限制长整数在指定范围内 + /// + public static long Clamp(this long value, long min, long max) + { + if (value < min) return min; + if (value > max) return max; + return value; + } + + #endregion + + #region 百分比转换 + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this double value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this float value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 将小数转换为百分比字符串 + /// + /// 数值 + /// 小数位数,默认2位 + public static string ToPercentage(this decimal value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + #endregion + + #region 文件大小转换 + + /// + /// 将字节数转换为人类可读的文件大小格式 + /// + /// 字节数 + public static string ToFileSize(this long bytes) + { + string[] sizes = { "B", "KB", "MB", "GB", "TB" }; + double len = bytes; + int order = 0; + + while (len >= 1024 && order < sizes.Length - 1) + { + order++; + len /= 1024; + } + + return $"{len:0.##} {sizes[order]}"; + } + + /// + /// 将字节数转换为人类可读的文件大小格式 + /// + /// 字节数 + public static string ToFileSize(this int bytes) + { + return ((long)bytes).ToFileSize(); + } + + #endregion + + #region 时间转换 + + /// + /// 将秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpan(this double seconds) + { + return TimeSpan.FromSeconds(seconds); + } + + /// + /// 将秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpan(this int seconds) + { + return TimeSpan.FromSeconds(seconds); + } + + /// + /// 将毫秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpanFromMilliseconds(this long milliseconds) + { + return TimeSpan.FromMilliseconds(milliseconds); + } + + /// + /// 将毫秒数转换为时间跨度 + /// + public static TimeSpan ToTimeSpanFromMilliseconds(this int milliseconds) + { + return TimeSpan.FromMilliseconds(milliseconds); + } + + #endregion + + #region 数学运算 + + /// + /// 计算数值的平方 + /// + public static int Square(this int value) + { + return value * value; + } + + /// + /// 计算数值的平方 + /// + public static long Square(this long value) + { + return value * value; + } + + /// + /// 计算数值的平方 + /// + public static double Square(this double value) + { + return value * value; + } + + /// + /// 计算数值的立方 + /// + public static int Cube(this int value) + { + return value * value * value; + } + + /// + /// 计算数值的立方 + /// + public static long Cube(this long value) + { + return value * value * value; + } + + /// + /// 计算数值的立方 + /// + public static double Cube(this double value) + { + return value * value * value; + } + + + #endregion + + #region 数值判断 + + + #endregion + + #region 数值格式化 + + /// + /// 将数字格式化为带千分位的字符串 + /// + public static string ToThousandsSeparator(this int value) + { + return value.ToString("#,##0"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + public static string ToThousandsSeparator(this long value) + { + return value.ToString("#,##0"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this double value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this float value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字格式化为带千分位的字符串 + /// + /// 小数位数 + public static string ToThousandsSeparator(this decimal value, int decimals = 2) + { + return value.ToString($"#,##0.{new string('0', decimals)}"); + } + + /// + /// 将数字转换为中文大写金额 + /// + public static string ToChineseMoney(this decimal value) + { + if (value == 0) + return "零元整"; + + string[] digits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + string[] units = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" }; + + string result = string.Empty; + bool hasYuan = false; + bool hasJiao = false; + bool hasFen = false; + + // 处理整数部分 + long integerPart = (long)value; + if (integerPart > 0) + { + string integerStr = integerPart.ToString(); + int length = integerStr.Length; + + for (int i = 0; i < length; i++) + { + int digit = integerStr[i] - '0'; + int pos = length - i - 1; + + if (digit != 0) + { + result += digits[digit] + units[pos]; + hasYuan = true; + } + else if (result.Length > 0 && result[result.Length - 1] != '零') + { + result += '零'; + } + } + + if (hasYuan) + result += '元'; + } + + // 处理小数部分 + decimal decimalPart = value - integerPart; + int jiao = (int)(decimalPart * 10); + int fen = (int)(decimalPart * 100) % 10; + + if (jiao > 0) + { + result += digits[jiao] + "角"; + hasJiao = true; + } + + if (fen > 0) + { + result += digits[fen] + "分"; + hasFen = true; + } + + if (!hasJiao && !hasFen && hasYuan) + result += "整"; + + // 清理多余的零 + result = result.Replace("零零", "零"); + if (result.EndsWith("零元")) + result = result.Substring(0, result.Length - 2) + "元"; + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/NumberFormatUtil.cs b/EasyTool.Core/MathCategory/NumberFormatUtil.cs new file mode 100644 index 0000000..2bee23c --- /dev/null +++ b/EasyTool.Core/MathCategory/NumberFormatUtil.cs @@ -0,0 +1,505 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace EasyTool.MathCategory +{ + /// + /// 数字格式化工具类 + /// 提供数字转换为大写金额、中文数字等功能 + /// + public static class NumberFormatUtil + { + #region 中文大写金额 + + private static readonly string[] ChineseDigits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; + private static readonly string[] ChineseUnits = { "", "拾", "佰", "仟" }; + private static readonly string[] ChineseBigUnits = { "", "万", "亿", "兆" }; + + /// + /// 数字转中文大写金额 + /// + /// 金额 + /// 中文大写金额 + public static string ToChineseAmount(decimal amount) + { + if (amount == 0) + return "零元整"; + + var result = new StringBuilder(); + var isNegative = amount < 0; + + if (isNegative) + { + result.Append("负"); + amount = -amount; + } + + // 四舍五入到分 + amount = Math.Round(amount, 2); + + var intPart = (long)amount; + var decPart = (int)((amount - intPart) * 100); + + // 处理整数部分 + if (intPart > 0) + { + result.Append(ConvertToChineseAmount(intPart)); + result.Append("元"); + } + + // 处理小数部分 + if (decPart > 0) + { + var jiao = decPart / 10; + var fen = decPart % 10; + + if (jiao > 0) + { + result.Append(ChineseDigits[jiao]); + result.Append("角"); + } + + if (fen > 0) + { + result.Append(ChineseDigits[fen]); + result.Append("分"); + } + } + else + { + result.Append("整"); + } + + return result.ToString(); + } + + private static string ConvertToChineseAmount(long number) + { + var result = new StringBuilder(); + var parts = new List(); + int unitIndex = 0; + + while (number > 0) + { + var part = (int)(number % 10000); + var partStr = ConvertPartToChinese(part); + + if (!string.IsNullOrEmpty(partStr)) + { + if (unitIndex > 0) + partStr += ChineseBigUnits[unitIndex]; + parts.Insert(0, partStr); + } + else if (parts.Count > 0) + { + parts.Insert(0, "零"); + } + + number /= 10000; + unitIndex++; + } + + result.Append(string.Join("", parts)); + + // 处理连续的零 + var final = result.ToString(); + while (final.Contains("零零")) + final = final.Replace("零零", "零"); + + // 去掉末尾的零 + final = final.TrimEnd('零'); + + return final; + } + + private static string ConvertPartToChinese(int number) + { + if (number == 0) + return ""; + + var result = new StringBuilder(); + var needZero = false; + + for (int i = 3; i >= 0; i--) + { + var digit = (int)(number / Math.Pow(10, i)) % 10; + + if (digit == 0) + { + needZero = true; + } + else + { + if (needZero) + { + result.Append("零"); + needZero = false; + } + result.Append(ChineseDigits[digit]); + if (i > 0) + result.Append(ChineseUnits[i]); + } + } + + return result.ToString(); + } + + #endregion + + #region 中文数字 + + private static readonly string[] SimpleChineseDigits = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; + + /// + /// 数字转中文数字 + /// + public static string ToChineseNumber(long number) + { + if (number == 0) + return "零"; + + var result = new StringBuilder(); + var isNegative = number < 0; + + if (isNegative) + { + result.Append("负"); + number = -number; + } + + var parts = new List(); + int unitIndex = 0; + + while (number > 0) + { + var part = (int)(number % 10000); + var partStr = ConvertPartToSimpleChinese(part); + + if (!string.IsNullOrEmpty(partStr)) + { + if (unitIndex > 0) + partStr += ChineseBigUnits[unitIndex]; + parts.Insert(0, partStr); + } + else if (parts.Count > 0) + { + parts.Insert(0, "零"); + } + + number /= 10000; + unitIndex++; + } + + result.Append(string.Join("", parts)); + + var final = result.ToString(); + while (final.Contains("零零")) + final = final.Replace("零零", "零"); + + final = final.TrimEnd('零'); + + // 处理"一十"开头的特殊情况 + if (final.StartsWith("一十")) + final = final.Substring(1); + + return final; + } + + private static string ConvertPartToSimpleChinese(int number) + { + if (number == 0) + return ""; + + var result = new StringBuilder(); + var needZero = false; + + for (int i = 3; i >= 0; i--) + { + var digit = (int)(number / Math.Pow(10, i)) % 10; + + if (digit == 0) + { + needZero = true; + } + else + { + if (needZero) + { + result.Append("零"); + needZero = false; + } + result.Append(SimpleChineseDigits[digit]); + if (i > 0) + result.Append(ChineseUnits[i]); + } + } + + return result.ToString(); + } + + #endregion + + #region 英文数字 + + private static readonly string[] Ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; + private static readonly string[] Tens = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; + private static readonly string[] Thousands = { "", "thousand", "million", "billion", "trillion" }; + + /// + /// 数字转英文单词 + /// + public static string ToEnglishWords(long number) + { + if (number == 0) + return "zero"; + + var result = new StringBuilder(); + var isNegative = number < 0; + + if (isNegative) + { + result.Append("negative "); + number = -number; + } + + var groups = new List(); + while (number > 0) + { + groups.Add((int)(number % 1000)); + number /= 1000; + } + + for (int i = groups.Count - 1; i >= 0; i--) + { + if (groups[i] > 0) + { + result.Append(ConvertGroupToEnglish(groups[i])); + if (i > 0) + result.Append(" " + Thousands[i] + " "); + } + } + + return result.ToString().Trim(); + } + + private static string ConvertGroupToEnglish(int number) + { + var result = new StringBuilder(); + + if (number >= 100) + { + result.Append(Ones[number / 100] + " hundred"); + number %= 100; + if (number > 0) + result.Append(" "); + } + + if (number >= 20) + { + result.Append(Tens[number / 10]); + number %= 10; + if (number > 0) + result.Append("-" + Ones[number]); + } + else if (number > 0) + { + result.Append(Ones[number]); + } + + return result.ToString(); + } + + /// + /// 数字转英文金额 + /// + public static string ToEnglishAmount(decimal amount) + { + if (amount == 0) + return "zero dollars"; + + var result = new StringBuilder(); + var isNegative = amount < 0; + + if (isNegative) + { + result.Append("negative "); + amount = -amount; + } + + amount = Math.Round(amount, 2); + var intPart = (long)amount; + var decPart = (int)((amount - intPart) * 100); + + if (intPart > 0) + { + result.Append(ToEnglishWords(intPart)); + result.Append(intPart == 1 ? " dollar" : " dollars"); + } + + if (decPart > 0) + { + if (intPart > 0) + result.Append(" and "); + result.Append(ToEnglishWords(decPart)); + result.Append(decPart == 1 ? " cent" : " cents"); + } + + return result.ToString(); + } + + #endregion + + #region 罗马数字 + + private static readonly (int Value, string Symbol)[] RomanSymbols = + { + (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), + (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), + (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I") + }; + + /// + /// 数字转罗马数字 + /// + public static string ToRoman(int number) + { + if (number < 1 || number > 3999) + throw new ArgumentOutOfRangeException(nameof(number), "罗马数字范围是1-3999"); + + var result = new StringBuilder(); + + foreach (var (value, symbol) in RomanSymbols) + { + while (number >= value) + { + result.Append(symbol); + number -= value; + } + } + + return result.ToString(); + } + + /// + /// 罗马数字转数字 + /// + public static int FromRoman(string roman) + { + if (string.IsNullOrWhiteSpace(roman)) + throw new ArgumentException("罗马数字不能为空"); + + var values = new Dictionary + { + {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, + {'C', 100}, {'D', 500}, {'M', 1000} + }; + + roman = roman.ToUpper(); + int result = 0; + int prevValue = 0; + + for (int i = roman.Length - 1; i >= 0; i--) + { + if (!values.TryGetValue(roman[i], out var value)) + throw new ArgumentException($"无效的罗马数字字符: {roman[i]}"); + + if (value < prevValue) + result -= value; + else + result += value; + + prevValue = value; + } + + return result; + } + + #endregion + + #region 格式化 + + /// + /// 格式化为百分比 + /// + public static string ToPercent(double value, int decimals = 2) + { + return (value * 100).ToString($"F{decimals}") + "%"; + } + + /// + /// 格式化为货币 + /// + public static string ToCurrency(decimal amount, string currencySymbol = "¥") + { + return currencySymbol + amount.ToString("N2"); + } + + /// + /// 格式化为科学计数法 + /// + public static string ToScientific(double value, int decimals = 2) + { + return value.ToString($"E{decimals}"); + } + + /// + /// 格式化为千分位 + /// + public static string ToThousands(long number, string separator = ",") + { + return number.ToString("N0").Replace(",", separator); + } + + /// + /// 格式化文件大小 + /// + public static string ToFileSize(long bytes, int decimals = 2) + { + string[] units = { "B", "KB", "MB", "GB", "TB", "PB" }; + double size = bytes; + int unitIndex = 0; + + while (size >= 1024 && unitIndex < units.Length - 1) + { + size /= 1024; + unitIndex++; + } + + return $"{Math.Round(size, decimals)} {units[unitIndex]}"; + } + + /// + /// 格式化序数词(1st, 2nd, 3rd, 4th...) + /// + public static string ToOrdinal(int number) + { + if (number <= 0) + return number.ToString(); + + string suffix; + int mod100 = number % 100; + + if (mod100 == 11 || mod100 == 12 || mod100 == 13) + { + suffix = "th"; + } + else + { + int mod10 = number % 10; + suffix = mod10 switch + { + 1 => "st", + 2 => "nd", + 3 => "rd", + _ => "th" + }; + } + + return number + suffix; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/NumberUtil.cs b/EasyTool.Core/MathCategory/NumberUtil.cs deleted file mode 100644 index a55a70f..0000000 --- a/EasyTool.Core/MathCategory/NumberUtil.cs +++ /dev/null @@ -1,350 +0,0 @@ -using System; - -namespace EasyTool -{ - /// - /// 数字工具类,提供了多种对数字的操作方法 - /// - public class NumberUtil - { - /// - /// 针对数字类型做加法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的和 - public static decimal Add(float a, float b) - { - return (decimal)a + (decimal)b; - } - - /// - /// 针对数字类型做加法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的和 - public static decimal Add(double a, double b) - { - return (decimal)a + (decimal)b; - } - - /// - /// 针对数字类型做减法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的差 - public static decimal Sub(float a, float b) - { - return (decimal)a - (decimal)b; - } - - /// - /// 针对数字类型做减法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的差 - public static decimal Sub(double a, double b) - { - return (decimal)a - (decimal)b; - } - - /// - /// 针对数字类型做乘法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的积 - public static decimal Mul(float a, float b) - { - return (decimal)a * (decimal)b; - } - - /// - /// 针对数字类型做乘法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的积 - public static decimal Mul(double a, double b) - { - return (decimal)a * (decimal)b; - } - - /// - /// 针对数字类型做除法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的商 - public static decimal Div(float a, float b) - { - return (decimal)a / (decimal)b; - } - - /// - /// 针对数字类型做除法 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的商 - public static decimal Div(double a, double b) - { - return (decimal)a / (decimal)b; - } - - /// - /// 针对数字类型做除法,并限制返回的小数位数 - /// - /// 第一个数字 - /// 第二个数字 - /// 限制返回的小数位数 - /// 两个数字的商 - public static decimal Div(float a, float b, int decimalPlaces) - { - decimal result = Div(a, b); - return decimal.Round(result, decimalPlaces); - } - - /// - /// 针对数字类型做除法,并限制返回的小数位数 - /// - /// 第一个数字 - /// 第二个数字 - /// 限制返回的小数位数 - /// 两个数字的商 - public static decimal Div(double a, double b, int decimalPlaces) - { - decimal result = Div(a, b); - return decimal.Round(result, decimalPlaces); - } - - /// - /// 格式化一个 decimal 数字 - /// - /// 待格式化的数字 - /// 格式化字符串 - /// 格式化后的字符串 - public static string DecimalFormat(decimal number, string format) - { - return number.ToString(format); - } - - /// - /// 保留一个 decimal 数字的小数点后指定位数 - /// - /// 待格式化的数字 - /// 小数点后保留的位数 - /// 格式化后的字符串 - public static string DecimalFormat(decimal number, int decimalPlaces) - { - string format = "0."; - for (int i = 0; i < decimalPlaces; i++) - { - format += "0"; - } - return DecimalFormat(number, format); - } - - /// - /// 格式化一个 decimal 数字,并加上千位分隔符 - /// - /// 待格式化的数字 - /// 格式化后的字符串 - public static string DecimalFormatWithCommas(decimal number) - { - return DecimalFormat(number, "0,0.00"); - } - - - - /// - /// 判断一个数字是否是质数 - /// - /// 待判断的数字 - /// 如果是质数,则返回 true;否则返回 false - public static bool IsPrime(int number) - { - if (number <= 1) - { - return false; - } - - for (int i = 2; i <= Math.Sqrt(number); i++) - { - if (number % i == 0) - { - return false; - } - } - - return true; - } - - /// - /// 求一个数字的阶乘 - /// - /// 待求阶乘的数字 - /// 该数字的阶乘 - public static int Factorial(int number) - { - if (number < 0) - { - throw new ArgumentException("阶乘只能求非负整数"); - } - - int result = 1; - for (int i = 1; i <= number; i++) - { - result *= i; - } - - return result; - } - - /// - /// 求两个数字的最大公约数 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的最大公约数 - public static int GCD(int a, int b) - { - if (a < 0 || b < 0) - { - throw new ArgumentException("求最大公约数只能接受非负整数"); - } - - while (b != 0) - { - int temp = b; - b = a % b; - a = temp; - } - - return a; - } - - /// - /// 求两个数字的最小公倍数 - /// - /// 第一个数字 - /// 第二个数字 - /// 两个数字的最小公倍数 - public static int LCM(int a, int b) - { - if (a < 0 || b < 0) - { - throw new ArgumentException("求最小公倍数只能接受非负整数"); - } - - return a * b / GCD(a, b); - } - - /// - /// 把一个数字转换为二进制字符串 - /// - /// 待转换的数字 - /// 该数字的二进制字符串 - public static string ToBinaryString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - result = (number % 2).ToString() + result; - number /= 2; - } - - return result; - } - - /// - /// 把一个数字转换为八进制字符串 - /// - /// 待转换的数字 - /// 该数字的八进制字符串 - public static string ToOctalString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - result = (number % 8).ToString() + result; - number /= 8; - } - - return result; - } - - /// - /// 把一个数字转换为十六进制字符串 - /// - /// 待转换的数字 - /// 该数字的十六进制字符串 - public static string ToHexString(int number) - { - if (number == 0) - { - return "0"; - } - - string result = string.Empty; - while (number > 0) - { - int remainder = number % 16; - if (remainder < 10) - { - result = remainder.ToString() + result; - } - else - { - result = (char)('A' + remainder - 10) + result; - } - number /= 16; - } - - return result; - } - - /// - /// 求一个数字的绝对值 - /// - /// 待求绝对值的数字 - /// 该数字的绝对值 - public static int Abs(int number) - { - return number < 0 ? -number : number; - } - - /// - /// 求一个数字的平方 - /// - /// 待求平方的数字 - /// 该数字的平方 - public static int Square(int number) - { - return number * number; - } - - /// - /// 求一个数字的立方 - /// - /// 待求立方的数字 - /// 该数字的立方 - public static int Cube(int number) - { - return number * number * number; - } - } -} diff --git a/EasyTool.Core/MathCategory/PredictUtil.cs b/EasyTool.Core/MathCategory/PredictUtil.cs index ebff051..564a42f 100644 --- a/EasyTool.Core/MathCategory/PredictUtil.cs +++ b/EasyTool.Core/MathCategory/PredictUtil.cs @@ -3,12 +3,12 @@ using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.MathCategory { /// /// 预测数据类 /// - public class PredictUtil + public static class PredictUtil { /// /// 线性回归预测 diff --git a/EasyTool.Core/MathCategory/PrimeUtil.cs b/EasyTool.Core/MathCategory/PrimeUtil.cs new file mode 100644 index 0000000..8091bcf --- /dev/null +++ b/EasyTool.Core/MathCategory/PrimeUtil.cs @@ -0,0 +1,483 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 质数工具类 + /// 提供质数相关的计算功能 + /// + public static class PrimeUtil + { + /// + /// 检查是否为质数 + /// + /// 数字 + /// 是否为质数 + public static bool IsPrime(long n) + { + if (n < 2) + return false; + + if (n == 2) + return true; + + if (n % 2 == 0) + return false; + + var sqrt = (long)Math.Sqrt(n); + + for (long i = 3; i <= sqrt; i += 2) + { + if (n % i == 0) + return false; + } + + return true; + } + + /// + /// 使用 Miller-Rabin 算法检查大数是否为质数 + /// + /// 数字 + /// 迭代次数(精度) + /// 是否为质数 + public static bool IsPrimeMillerRabin(long n, int iterations = 5) + { + if (n < 2) + return false; + + if (n == 2 || n == 3) + return true; + + if (n % 2 == 0) + return false; + + // 将 n-1 分解为 2^r * d + long d = n - 1; + int r = 0; + + while (d % 2 == 0) + { + d /= 2; + r++; + } + + var random = new Random(); + + for (int i = 0; i < iterations; i++) + { + long a = 2 + (long)(random.NextDouble() * (n - 4)); + long x = ModPow(a, d, n); + + if (x == 1 || x == n - 1) + continue; + + bool composite = true; + + for (int j = 0; j < r - 1; j++) + { + x = ModPow(x, 2, n); + + if (x == n - 1) + { + composite = false; + break; + } + } + + if (composite) + return false; + } + + return true; + } + + /// + /// 获取下一个质数 + /// + /// 起始数字 + /// 下一个质数 + public static long NextPrime(long n) + { + if (n < 2) + return 2; + + long candidate = n + 1; + + if (candidate % 2 == 0) + candidate++; + + while (!IsPrime(candidate)) + { + candidate += 2; + } + + return candidate; + } + + /// + /// 获取前一个质数 + /// + /// 起始数字 + /// 前一个质数,如果不存在返回 -1 + public static long PreviousPrime(long n) + { + if (n <= 2) + return -1; + + if (n == 3) + return 2; + + long candidate = n - 1; + + if (candidate % 2 == 0) + candidate--; + + while (candidate >= 2 && !IsPrime(candidate)) + { + candidate -= 2; + } + + return candidate >= 2 ? candidate : -1; + } + + /// + /// 获取范围内的所有质数 + /// + /// 起始数字 + /// 结束数字 + /// 质数列表 + public static List GetPrimesInRange(long start, long end) + { + var primes = new List(); + + for (long i = start; i <= end; i++) + { + if (IsPrime(i)) + primes.Add(i); + } + + return primes; + } + + /// + /// 使用埃拉托斯特尼筛法获取指定范围内的所有质数 + /// + /// 上限 + /// 质数列表 + public static List SieveOfEratosthenes(long limit) + { + if (limit < 2) + return new List(); + + var isPrime = new bool[limit + 1]; + Array.Fill(isPrime, true); + + isPrime[0] = false; + isPrime[1] = false; + + var sqrt = (long)Math.Sqrt(limit); + + for (long i = 2; i <= sqrt; i++) + { + if (isPrime[i]) + { + for (long j = i * i; j <= limit; j += i) + { + isPrime[j] = false; + } + } + } + + var primes = new List(); + + for (long i = 2; i <= limit; i++) + { + if (isPrime[i]) + primes.Add(i); + } + + return primes; + } + + /// + /// 获取质因数分解 + /// + /// 数字 + /// 质因数及其幂次的字典 + public static Dictionary PrimeFactorization(long n) + { + var factors = new Dictionary(); + + if (n < 2) + return factors; + + // 处理因子 2 + while (n % 2 == 0) + { + if (factors.ContainsKey(2)) + factors[2]++; + else + factors[2] = 1; + + n /= 2; + } + + // 处理奇数因子 + for (long i = 3; i * i <= n; i += 2) + { + while (n % i == 0) + { + if (factors.ContainsKey(i)) + factors[i]++; + else + factors[i] = 1; + + n /= i; + } + } + + // 如果剩下的 n 大于 1,则它本身是质数 + if (n > 1) + { + factors[n] = 1; + } + + return factors; + } + + /// + /// 获取所有因数 + /// + /// 数字 + /// 因数列表 + public static List GetDivisors(long n) + { + var divisors = new List(); + + if (n < 1) + return divisors; + + var sqrt = (long)Math.Sqrt(n); + + for (long i = 1; i <= sqrt; i++) + { + if (n % i == 0) + { + divisors.Add(i); + + if (i != n / i) + { + divisors.Add(n / i); + } + } + } + + divisors.Sort(); + return divisors; + } + + /// + /// 计算因数个数 + /// + /// 数字 + /// 因数个数 + public static long CountDivisors(long n) + { + if (n < 1) + return 0; + + var factors = PrimeFactorization(n); + long count = 1; + + foreach (var power in factors.Values) + { + count *= (power + 1); + } + + return count; + } + + /// + /// 计算最大公约数 + /// + /// 数字1 + /// 数字2 + /// 最大公约数 + public static long Gcd(long a, long b) + { + a = Math.Abs(a); + b = Math.Abs(b); + + while (b != 0) + { + var temp = b; + b = a % b; + a = temp; + } + + return a; + } + + /// + /// 计算最小公倍数 + /// + /// 数字1 + /// 数字2 + /// 最小公倍数 + public static long Lcm(long a, long b) + { + if (a == 0 || b == 0) + return 0; + + return Math.Abs(a * b) / Gcd(a, b); + } + + /// + /// 计算多个数的最大公约数 + /// + /// 数字数组 + /// 最大公约数 + public static long Gcd(params long[] numbers) + { + if (numbers == null || numbers.Length == 0) + return 0; + + long result = numbers[0]; + + for (int i = 1; i < numbers.Length; i++) + { + result = Gcd(result, numbers[i]); + } + + return result; + } + + /// + /// 计算多个数的最小公倍数 + /// + /// 数字数组 + /// 最小公倍数 + public static long Lcm(params long[] numbers) + { + if (numbers == null || numbers.Length == 0) + return 0; + + long result = numbers[0]; + + for (int i = 1; i < numbers.Length; i++) + { + result = Lcm(result, numbers[i]); + } + + return result; + } + + /// + /// 计算欧拉函数 φ(n) + /// + /// 数字 + /// 欧拉函数值 + public static long EulerTotient(long n) + { + if (n < 1) + return 0; + + var factors = PrimeFactorization(n); + long result = n; + + foreach (var p in factors.Keys) + { + result = result / p * (p - 1); + } + + return result; + } + + /// + /// 判断是否为互质数 + /// + /// 数字1 + /// 数字2 + /// 是否互质 + public static bool AreCoprime(long a, long b) + { + return Gcd(a, b) == 1; + } + + /// + /// 获取第 n 个质数(从1开始) + /// + /// 序号 + /// 第 n 个质数 + public static long GetNthPrime(int n) + { + if (n < 1) + throw new ArgumentException("n must be positive"); + + if (n == 1) + return 2; + + int count = 1; + long candidate = 1; + + while (count < n) + { + candidate += 2; + + if (IsPrime(candidate)) + count++; + } + + return candidate; + } + + /// + /// 判断是否为梅森数 + /// + /// 数字 + /// 是否为梅森数 + public static bool IsMersennePrime(long n) + { + // 梅森数形式为 2^p - 1,其中 p 是质数 + n = n + 1; + + if (n <= 2 || (n & (n - 1)) != 0) + return false; + + int p = 0; + while (n > 1) + { + n >>= 1; + p++; + } + + return IsPrime(p); + } + + #region 私有方法 + + private static long ModPow(long baseVal, long exponent, long modulus) + { + long result = 1; + baseVal %= modulus; + + while (exponent > 0) + { + if (exponent % 2 == 1) + { + result = (result * baseVal) % modulus; + } + + exponent >>= 1; + baseVal = (baseVal * baseVal) % modulus; + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/RandomUtil.cs b/EasyTool.Core/MathCategory/RandomUtil.cs new file mode 100644 index 0000000..7ce180e --- /dev/null +++ b/EasyTool.Core/MathCategory/RandomUtil.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.MathCategory +{ + /// + /// 随机数工具类 + /// 提供各种随机数生成功能,包括安全随机数 + /// + public static class RandomUtil + { + private static readonly Random _random = new(); + private static readonly object _lock = new(); + private static readonly char[] _alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); + private static readonly char[] _numericChars = "0123456789".ToCharArray(); + private static readonly char[] _alphanumericChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray(); + private static readonly char[] _hexChars = "0123456789ABCDEF".ToCharArray(); + + /// + /// 生成指定范围内的随机整数 + /// + /// 最小值(包含) + /// 最大值(不包含) + /// 随机整数 + public static int Next(int min, int max) + { + lock (_lock) + { + return _random.Next(min, max); + } + } + + /// + /// 生成非负随机整数 + /// + /// 随机整数 + public static int Next() + { + lock (_lock) + { + return _random.Next(); + } + } + + /// + /// 生成指定范围内的随机浮点数 + /// + /// 最小值 + /// 最大值 + /// 随机浮点数 + public static double NextDouble(double min, double max) + { + lock (_lock) + { + return _random.NextDouble() * (max - min) + min; + } + } + + /// + /// 生成随机布尔值 + /// + /// 随机布尔值 + public static bool NextBool() + { + lock (_lock) + { + return _random.Next(2) == 1; + } + } + + /// + /// 生成随机字节数组 + /// + /// 长度 + /// 字节数组 + public static byte[] NextBytes(int length) + { + var bytes = new byte[length]; + lock (_lock) + { + _random.NextBytes(bytes); + } + return bytes; + } + + /// + /// 生成随机字母字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextAlphaString(int length) + { + return NextString(length, _alphaChars); + } + + /// + /// 生成随机数字字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextNumericString(int length) + { + return NextString(length, _numericChars); + } + + /// + /// 生成随机字母数字字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextAlphanumericString(int length) + { + return NextString(length, _alphanumericChars); + } + + /// + /// 生成随机十六进制字符串 + /// + /// 长度 + /// 随机字符串 + public static string NextHexString(int length) + { + return NextString(length, _hexChars); + } + + /// + /// 使用指定字符生成随机字符串 + /// + /// 长度 + /// 字符集 + /// 随机字符串 + public static string NextString(int length, char[] chars) + { + var result = new StringBuilder(length); + lock (_lock) + { + for (int i = 0; i < length; i++) + { + result.Append(chars[_random.Next(chars.Length)]); + } + } + return result.ToString(); + } + + /// + /// 使用指定字符生成随机字符串 + /// + /// 长度 + /// 字符集 + /// 随机字符串 + public static string NextString(int length, string chars) + { + return NextString(length, chars.ToCharArray()); + } + + /// + /// 从数组中随机选择一个元素 + /// + /// 元素类型 + /// 数组 + /// 随机元素 + public static T? NextItem(T[] array) + { + if (array == null || array.Length == 0) + return default; + + lock (_lock) + { + return array[_random.Next(array.Length)]; + } + } + + /// + /// 随机打乱数组 + /// + /// 元素类型 + /// 数组 + /// 打乱后的数组 + public static T[] Shuffle(T[] array) + { + if (array == null || array.Length <= 1) + return array; + + var result = (T[])array.Clone(); + lock (_lock) + { + for (int i = result.Length - 1; i > 0; i--) + { + int j = _random.Next(i + 1); + (result[i], result[j]) = (result[j], result[i]); + } + } + return result; + } + + /// + /// 生成安全随机整数 + /// + /// 最小值 + /// 最大值(不包含) + /// 安全随机整数 + public static int NextSecure(int min, int max) + { + if (min >= max) + throw new ArgumentException("max must be greater than min"); + + var range = (long)max - min; + var bytes = new byte[4]; + + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + var randomValue = BitConverter.ToUInt32(bytes, 0); + + return (int)(min + (randomValue % range)); + } + + /// + /// 生成安全随机字节数组 + /// + /// 长度 + /// 安全随机字节数组 + public static byte[] NextSecureBytes(int length) + { + var bytes = new byte[length]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + return bytes; + } + + /// + /// 生成安全随机字符串 + /// + /// 长度 + /// 字符集 + /// 安全随机字符串 + public static string NextSecureString(int length, string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") + { + var result = new StringBuilder(length); + var charArray = chars.ToCharArray(); + + using var rng = RandomNumberGenerator.Create(); + var bytes = new byte[4]; + + for (int i = 0; i < length; i++) + { + rng.GetBytes(bytes); + var randomIndex = BitConverter.ToUInt32(bytes, 0) % (uint)charArray.Length; + result.Append(charArray[randomIndex]); + } + + return result.ToString(); + } + + /// + /// 生成随机 GUID(不带连字符) + /// + /// 随机 GUID 字符串 + public static string NextGuid() + { + return Guid.NewGuid().ToString("N"); + } + + /// + /// 生成随机 UUID + /// + /// UUID 字符串 + public static string NextUuid() + { + return Guid.NewGuid().ToString(); + } + + /// + /// 随机选择多个不重复的元素 + /// + /// 元素类型 + /// 数组 + /// 选择数量 + /// 随机选择的元素数组 + public static T[] NextItems(T[] array, int count) + { + if (array == null || array.Length == 0) + return Array.Empty(); + + if (count >= array.Length) + return Shuffle(array); + + var shuffled = Shuffle(array); + var result = new T[count]; + Array.Copy(shuffled, result, count); + return result; + } + + /// + /// 根据权重随机选择 + /// + /// 元素类型 + /// 元素数组 + /// 权重数组 + /// 随机选择的元素 + public static T? NextWeighted(T[] items, int[] weights) + { + if (items == null || items.Length == 0) + return default; + + if (weights == null || weights.Length != items.Length) + throw new ArgumentException("Weights array must have the same length as items array"); + + var totalWeight = 0; + foreach (var w in weights) + { + if (w < 0) + throw new ArgumentException("Weights must be non-negative"); + totalWeight += w; + } + + if (totalWeight == 0) + return default; + + int randomValue; + lock (_lock) + { + randomValue = _random.Next(totalWeight); + } + + var currentSum = 0; + for (int i = 0; i < items.Length; i++) + { + currentSum += weights[i]; + if (randomValue < currentSum) + return items[i]; + } + + return items[^1]; + } + + #region 向后兼容方法别名 + + /// + /// 生成随机整数(Next 的别名) + /// + public static int RandomInt(int min, int max) => Next(min, max); + + /// + /// 生成随机整数(Next 的别名) + /// + public static int RandomInt() => Next(); + + /// + /// 从数组中随机选择一个元素(NextItem 的别名) + /// + public static T? GetRandomElement(T[] array) => NextItem(array); + + /// + /// 从列表中随机选择一个元素 + /// + public static T? GetRandomElement(IList list) + { + if (list == null || list.Count == 0) + return default; + + lock (_lock) + { + return list[_random.Next(list.Count)]; + } + } + + /// + /// 从集合中随机选择一个元素 + /// + public static T? GetRandomElement(IEnumerable collection) + { + if (collection == null) + return default; + + var list = collection.ToList(); + if (list.Count == 0) + return default; + + return GetRandomElement(list); + } + + /// + /// 生成随机数字字符串(NextNumericString 的别名) + /// + public static string RandomDigitString(int length) => NextNumericString(length); + + /// + /// 生成随机字符串(NextAlphanumericString 的别名) + /// + public static string RandomString(int length) => NextAlphanumericString(length); + + /// + /// 生成随机字母字符串(NextAlphaString 的别名) + /// + public static string RandomAlphaString(int length) => NextAlphaString(length); + + /// + /// 生成随机布尔值(NextBool 的别名) + /// + public static bool RandomBool() => NextBool(); + + /// + /// 生成随机日期时间 + /// + /// 最小日期 + /// 最大日期 + /// 随机日期时间 + public static DateTime GetRandomDateTime(DateTime minDate, DateTime maxDate) + { + if (minDate >= maxDate) + throw new ArgumentException("minDate must be less than maxDate"); + + var range = (maxDate - minDate).Ticks; + lock (_lock) + { + var ticks = (long)(_random.NextDouble() * range); + return minDate.AddTicks(ticks); + } + } + + /// + /// 生成随机日期时间(默认1970年至今) + /// + /// 随机日期时间 + public static DateTime GetRandomDateTime() + { + return GetRandomDateTime(new DateTime(1970, 1, 1), DateTime.Now); + } + + /// + /// 生成随机日期(不含时间) + /// + /// 最小日期 + /// 最大日期 + /// 随机日期 + public static DateTime GetRandomDate(DateTime minDate, DateTime maxDate) + { + return GetRandomDateTime(minDate, maxDate).Date; + } + + #endregion + } +} diff --git a/EasyTool.Core/MathCategory/RomanNumeralUtil.cs b/EasyTool.Core/MathCategory/RomanNumeralUtil.cs new file mode 100644 index 0000000..79d6f7a --- /dev/null +++ b/EasyTool.Core/MathCategory/RomanNumeralUtil.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.MathCategory +{ + /// + /// 罗马数字工具类 + /// 提供阿拉伯数字与罗马数字之间的转换 + /// + public static class RomanNumeralUtil + { + private static readonly Dictionary RomanMap = new() + { + { 1000, "M" }, + { 900, "CM" }, + { 500, "D" }, + { 400, "CD" }, + { 100, "C" }, + { 90, "XC" }, + { 50, "L" }, + { 40, "XL" }, + { 10, "X" }, + { 9, "IX" }, + { 5, "V" }, + { 4, "IV" }, + { 1, "I" } + }; + + private static readonly Dictionary RomanValues = new() + { + { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } + }; + + /// + /// 将整数转换为罗马数字 + /// + public static string ToRoman(int number) + { + if (number < 1 || number > 3999) + throw new ArgumentOutOfRangeException(nameof(number), "Number must be between 1 and 3999"); + + var result = new StringBuilder(); + + foreach (var kvp in RomanMap) + { + while (number >= kvp.Key) + { + result.Append(kvp.Value); + number -= kvp.Key; + } + } + + return result.ToString(); + } + + /// + /// 将罗马数字转换为整数 + /// + public static int FromRoman(string roman) + { + if (string.IsNullOrWhiteSpace(roman)) + throw new ArgumentException("Roman numeral cannot be empty"); + + roman = roman.ToUpperInvariant().Trim(); + int result = 0; + int prevValue = 0; + + for (int i = roman.Length - 1; i >= 0; i--) + { + if (!RomanValues.TryGetValue(roman[i], out int value)) + throw new ArgumentException($"Invalid Roman numeral character: {roman[i]}"); + + if (value < prevValue) + result -= value; + else + result += value; + + prevValue = value; + } + + // 验证结果是否有效 + if (ToRoman(result) != roman) + throw new ArgumentException($"Invalid Roman numeral: {roman}"); + + return result; + } + + /// + /// 尝试将罗马数字转换为整数 + /// + public static bool TryParse(string roman, out int result) + { + result = 0; + try + { + result = FromRoman(roman); + return true; + } + catch + { + return false; + } + } + + /// + /// 验证罗马数字是否有效 + /// + public static bool IsValid(string roman) + { + return TryParse(roman, out _); + } + } +} diff --git a/EasyTool.Core/MathCategory/StatisticsUtil.cs b/EasyTool.Core/MathCategory/StatisticsUtil.cs new file mode 100644 index 0000000..ab721fe --- /dev/null +++ b/EasyTool.Core/MathCategory/StatisticsUtil.cs @@ -0,0 +1,633 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.MathCategory +{ + /// + /// 统计分析工具类 + /// 提供常用的统计分析功能 + /// + public static class StatisticsUtil + { + #region 基础统计 + + /// + /// 计算总和 + /// + public static double Sum(IEnumerable values) + { + return values.Sum(); + } + + /// + /// 计算平均值 + /// + public static double Mean(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + return list.Sum() / list.Count; + } + + /// + /// 计算中位数 + /// + public static double Median(IEnumerable values) + { + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + if (count % 2 == 0) + { + return (sorted[count / 2 - 1] + sorted[count / 2]) / 2.0; + } + + return sorted[count / 2]; + } + + /// + /// 计算众数(出现频率最高的值) + /// + public static List Mode(IEnumerable values) + { + var groups = values.GroupBy(v => v) + .OrderByDescending(g => g.Count()) + .ToList(); + + if (groups.Count == 0) return new List(); + + var maxCount = groups[0].Count(); + return groups.Where(g => g.Count() == maxCount) + .Select(g => g.Key) + .ToList(); + } + + /// + /// 计算极差(最大值-最小值) + /// + public static double Range(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + return list.Max() - list.Min(); + } + + /// + /// 计算最小值 + /// + public static double Min(IEnumerable values) + { + return values.Min(); + } + + /// + /// 计算最大值 + /// + public static double Max(IEnumerable values) + { + return values.Max(); + } + + /// + /// 计算计数 + /// + public static int Count(IEnumerable values) + { + return values.Count(); + } + + #endregion + + #region 离散程度 + + /// + /// 计算方差(总体方差) + /// + public static double Variance(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + + var mean = Mean(list); + var sumSquaredDiff = list.Sum(v => Math.Pow(v - mean, 2)); + return sumSquaredDiff / list.Count; + } + + /// + /// 计算样本方差 + /// + public static double SampleVariance(IEnumerable values) + { + var list = values.ToList(); + if (list.Count <= 1) return 0; + + var mean = Mean(list); + var sumSquaredDiff = list.Sum(v => Math.Pow(v - mean, 2)); + return sumSquaredDiff / (list.Count - 1); + } + + /// + /// 计算标准差(总体标准差) + /// + public static double StandardDeviation(IEnumerable values) + { + return Math.Sqrt(Variance(values)); + } + + /// + /// 计算样本标准差 + /// + public static double SampleStandardDeviation(IEnumerable values) + { + return Math.Sqrt(SampleVariance(values)); + } + + /// + /// 计算变异系数(标准差/平均值) + /// + public static double CoefficientOfVariation(IEnumerable values) + { + var list = values.ToList(); + var mean = Mean(list); + if (mean == 0) return 0; + return StandardDeviation(list) / mean; + } + + /// + /// 计算平均绝对偏差 + /// + public static double MeanAbsoluteDeviation(IEnumerable values) + { + var list = values.ToList(); + if (list.Count == 0) return 0; + + var mean = Mean(list); + return list.Average(v => Math.Abs(v - mean)); + } + + /// + /// 计算四分位数 + /// + /// 数据集 + /// 四分位数类型(1=Q1, 2=Q2/中位数, 3=Q3) + /// 四分位数值 + public static double Quartile(IEnumerable values, int q) + { + if (q < 1 || q > 3) + throw new ArgumentException("四分位数参数q必须为1、2或3", nameof(q)); + + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + if (q == 2) return Median(sorted); + + double position; + if (q == 1) + position = (count + 1) / 4.0; + else + position = 3 * (count + 1) / 4.0; + + var lowerIndex = (int)Math.Floor(position) - 1; + var upperIndex = (int)Math.Ceiling(position) - 1; + var fraction = position - Math.Floor(position); + + if (lowerIndex == upperIndex || fraction == 0) + return sorted[Math.Max(0, Math.Min(count - 1, lowerIndex))]; + + lowerIndex = Math.Max(0, Math.Min(count - 1, lowerIndex)); + upperIndex = Math.Max(0, Math.Min(count - 1, upperIndex)); + + return sorted[lowerIndex] * (1 - fraction) + sorted[upperIndex] * fraction; + } + + /// + /// 计算四分位距(IQR = Q3 - Q1) + /// + public static double InterquartileRange(IEnumerable values) + { + return Quartile(values, 3) - Quartile(values, 1); + } + + #endregion + + #region 百分位数 + + /// + /// 计算百分位数 + /// + /// 数据集 + /// 百分位(0-100) + /// 百分位数值 + public static double Percentile(IEnumerable values, double percentile) + { + if (percentile < 0 || percentile > 100) + throw new ArgumentException("百分位必须在0-100之间", nameof(percentile)); + + var sorted = values.OrderBy(v => v).ToList(); + var count = sorted.Count; + + if (count == 0) return 0; + + var position = (percentile / 100.0) * (count - 1); + var lowerIndex = (int)Math.Floor(position); + var upperIndex = (int)Math.Ceiling(position); + var fraction = position - lowerIndex; + + if (lowerIndex == upperIndex) + return sorted[lowerIndex]; + + return sorted[lowerIndex] * (1 - fraction) + sorted[upperIndex] * fraction; + } + + /// + /// 计算百分等级(某个值在数据集中的百分位) + /// + public static double PercentileRank(IEnumerable values, double value) + { + var list = values.ToList(); + var lessCount = list.Count(v => v < value); + var equalCount = list.Count(v => v == value); + var totalCount = list.Count; + + if (totalCount == 0) return 0; + + // 使用线性插值法 + return (lessCount + 0.5 * equalCount) / totalCount * 100; + } + + #endregion + + #region 分布形状 + + /// + /// 计算偏度(Skewness) + /// 正偏度表示右偏,负偏度表示左偏 + /// + public static double Skewness(IEnumerable values) + { + var list = values.ToList(); + if (list.Count < 3) return 0; + + var mean = Mean(list); + var stdDev = StandardDeviation(list); + if (stdDev == 0) return 0; + + var n = list.Count; + var sumCubedDiff = list.Sum(v => Math.Pow((v - mean) / stdDev, 3)); + + return (n / ((n - 1) * (n - 2))) * sumCubedDiff; + } + + /// + /// 计算峰度(Kurtosis) + /// 正态分布峰度为0,大于0表示尖峰,小于0表示平峰 + /// + public static double Kurtosis(IEnumerable values) + { + var list = values.ToList(); + if (list.Count < 4) return 0; + + var mean = Mean(list); + var stdDev = StandardDeviation(list); + if (stdDev == 0) return 0; + + var n = list.Count; + var sumFourthPower = list.Sum(v => Math.Pow((v - mean) / stdDev, 4)); + + return (n * (n + 1) / ((n - 1) * (n - 2) * (n - 3))) * sumFourthPower + - (3 * Math.Pow(n - 1, 2)) / ((n - 2) * (n - 3)); + } + + #endregion + + #region 协方差和相关系数 + + /// + /// 计算协方差 + /// + public static double Covariance(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + if (xList.Count == 0) return 0; + + var meanX = Mean(xList); + var meanY = Mean(yList); + var n = xList.Count; + + return xList.Zip(yList, (xi, yi) => (xi - meanX) * (yi - meanY)).Sum() / n; + } + + /// + /// 计算皮尔逊相关系数 + /// + public static double PearsonCorrelation(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + if (xList.Count == 0) return 0; + + var stdDevX = StandardDeviation(xList); + var stdDevY = StandardDeviation(yList); + + if (stdDevX == 0 || stdDevY == 0) return 0; + + return Covariance(xList, yList) / (stdDevX * stdDevY); + } + + /// + /// 计算斯皮尔曼等级相关系数 + /// + public static double SpearmanCorrelation(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + // 转换为秩 + var xRanks = GetRanks(xList); + var yRanks = GetRanks(yList); + + return PearsonCorrelation(xRanks, yRanks); + } + + private static List GetRanks(List values) + { + var sorted = values.Select((v, i) => new { Value = v, Index = i }) + .OrderBy(x => x.Value) + .ToList(); + + var ranks = new double[values.Count]; + for (int i = 0; i < sorted.Count; i++) + { + // 处理相同值的平均秩 + var sameValues = sorted.Where(s => s.Value == sorted[i].Value).ToList(); + var avgRank = sameValues.Select(s => s.Index).Average(); + ranks[sorted[i].Index] = avgRank + 1; + } + + return ranks.ToList(); + } + + #endregion + + #region 回归分析 + + /// + /// 简单线性回归 + /// + /// 斜率和截距 + public static (double Slope, double Intercept) LinearRegression(IEnumerable x, IEnumerable y) + { + var xList = x.ToList(); + var yList = y.ToList(); + + if (xList.Count != yList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + var n = xList.Count; + if (n == 0) return (0, 0); + + var meanX = Mean(xList); + var meanY = Mean(yList); + var stdDevX = StandardDeviation(xList); + var stdDevY = StandardDeviation(yList); + + if (stdDevX == 0) return (0, meanY); + + var correlation = PearsonCorrelation(xList, yList); + var slope = correlation * stdDevY / stdDevX; + var intercept = meanY - slope * meanX; + + return (slope, intercept); + } + + /// + /// 使用回归模型预测 + /// + public static double Predict(double x, double slope, double intercept) + { + return slope * x + intercept; + } + + /// + /// 计算R平方(决定系数) + /// + public static double RSquared(IEnumerable actual, IEnumerable predicted) + { + var actualList = actual.ToList(); + var predictedList = predicted.ToList(); + + if (actualList.Count != predictedList.Count) + throw new ArgumentException("两个数据集的长度必须相同"); + + var mean = Mean(actualList); + var ssTotal = actualList.Sum(a => Math.Pow(a - mean, 2)); + var ssResidual = actualList.Zip(predictedList, (a, p) => Math.Pow(a - p, 2)).Sum(); + + if (ssTotal == 0) return 1; + + return 1 - (ssResidual / ssTotal); + } + + #endregion + + #region 描述性统计 + + /// + /// 获取完整统计摘要 + /// + public static StatisticsSummary GetSummary(IEnumerable values) + { + var list = values.ToList(); + + return new StatisticsSummary + { + Count = list.Count, + Sum = Sum(list), + Mean = Mean(list), + Median = Median(list), + Mode = Mode(list), + Min = Min(list), + Max = Max(list), + Range = Range(list), + Variance = Variance(list), + StandardDeviation = StandardDeviation(list), + SampleVariance = SampleVariance(list), + SampleStandardDeviation = SampleStandardDeviation(list), + Q1 = Quartile(list, 1), + Q3 = Quartile(list, 3), + IQR = InterquartileRange(list), + Skewness = Skewness(list), + Kurtosis = Kurtosis(list), + CoefficientOfVariation = CoefficientOfVariation(list) + }; + } + + #endregion + + #region 异常值检测 + + /// + /// 使用IQR方法检测异常值 + /// + public static List DetectOutliersIQR(IEnumerable values, double multiplier = 1.5) + { + var list = values.ToList(); + var q1 = Quartile(list, 1); + var q3 = Quartile(list, 3); + var iqr = q3 - q1; + + var lowerBound = q1 - multiplier * iqr; + var upperBound = q3 + multiplier * iqr; + + return list.Where(v => v < lowerBound || v > upperBound).ToList(); + } + + /// + /// 使用Z-Score方法检测异常值 + /// + public static List DetectOutliersZScore(IEnumerable values, double threshold = 3.0) + { + var list = values.ToList(); + var mean = Mean(list); + var stdDev = StandardDeviation(list); + + if (stdDev == 0) return new List(); + + return list.Where(v => Math.Abs((v - mean) / stdDev) > threshold).ToList(); + } + + /// + /// 计算Z-Score + /// + public static List ZScore(IEnumerable values) + { + var list = values.ToList(); + var mean = Mean(list); + var stdDev = StandardDeviation(list); + + if (stdDev == 0) return list.Select(_ => 0.0).ToList(); + + return list.Select(v => (v - mean) / stdDev).ToList(); + } + + #endregion + } + + /// + /// 统计摘要 + /// + public class StatisticsSummary + { + /// + /// 计数 + /// + public int Count { get; set; } + + /// + /// 总和 + /// + public double Sum { get; set; } + + /// + /// 平均值 + /// + public double Mean { get; set; } + + /// + /// 中位数 + /// + public double Median { get; set; } + + /// + /// 众数 + /// + public List Mode { get; set; } = new(); + + /// + /// 最小值 + /// + public double Min { get; set; } + + /// + /// 最大值 + /// + public double Max { get; set; } + + /// + /// 极差 + /// + public double Range { get; set; } + + /// + /// 总体方差 + /// + public double Variance { get; set; } + + /// + /// 总体标准差 + /// + public double StandardDeviation { get; set; } + + /// + /// 样本方差 + /// + public double SampleVariance { get; set; } + + /// + /// 样本标准差 + /// + public double SampleStandardDeviation { get; set; } + + /// + /// 第一四分位数 + /// + public double Q1 { get; set; } + + /// + /// 第三四分位数 + /// + public double Q3 { get; set; } + + /// + /// 四分位距 + /// + public double IQR { get; set; } + + /// + /// 偏度 + /// + public double Skewness { get; set; } + + /// + /// 峰度 + /// + public double Kurtosis { get; set; } + + /// + /// 变异系数 + /// + public double CoefficientOfVariation { get; set; } + + public override string ToString() + { + return $"统计摘要: N={Count}, 均值={Mean:F4}, 标准差={StandardDeviation:F4}, 中位数={Median:F4}, 范围=[{Min:F4}, {Max:F4}]"; + } + } +} diff --git a/EasyTool.Core/MathCategory/WeightedRandomUtil.cs b/EasyTool.Core/MathCategory/WeightedRandomUtil.cs new file mode 100644 index 0000000..5a3b6cc --- /dev/null +++ b/EasyTool.Core/MathCategory/WeightedRandomUtil.cs @@ -0,0 +1,319 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace EasyTool.MathCategory +{ + /// + /// 加权随机选择工具类 + /// 根据权重随机选择元素 + /// + public static class WeightedRandomUtil + { +#if NET6_0_OR_GREATER + private static Random SharedRandom => Random.Shared; +#else + private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); + private static Random SharedRandom => ThreadLocalRandom.Value!; +#endif + + /// + /// 创建加权随机选择器 + /// + /// 元素类型 + /// 加权随机选择器构建器 + public static WeightedRandomBuilder CreateBuilder() + { + return new WeightedRandomBuilder(); + } + + /// + /// 从字典中按权重随机选择 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 随机选中的元素 + public static T Select(IDictionary items) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + var totalWeight = items.Values.Sum(); + var random = SharedRandom.NextDouble() * totalWeight; + + double cumulative = 0; + foreach (var kvp in items) + { + cumulative += kvp.Value; + if (random < cumulative) + return kvp.Key; + } + + return items.Last().Key; + } + + /// + /// 从列表中按权重随机选择 + /// + /// 元素类型 + /// 元素列表 + /// 权重选择器 + /// 随机选中的元素 + public static T Select(IEnumerable items, Func weightSelector) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + var itemList = items.ToList(); + if (itemList.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + var totalWeight = itemList.Sum(weightSelector); + var random = SharedRandom.NextDouble() * totalWeight; + + double cumulative = 0; + foreach (var item in itemList) + { + cumulative += weightSelector(item); + if (random < cumulative) + return item; + } + + return itemList.Last(); + } + + /// + /// 按权重随机选择多个元素(可重复) + /// + /// 元素类型 + /// 元素和权重的字典 + /// 选择数量 + /// 随机选中的元素列表 + public static List SelectMany(IDictionary items, int count) + { + var result = new List(); + for (int i = 0; i < count; i++) + { + result.Add(Select(items)); + } + return result; + } + + /// + /// 按权重随机选择多个不重复元素 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 选择数量 + /// 随机选中的元素列表 + public static List SelectDistinct(IDictionary items, int count) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + count = Math.Min(count, items.Count); + + var remaining = new Dictionary(items); + var result = new List(); + + for (int i = 0; i < count; i++) + { + var selected = Select(remaining); + result.Add(selected); + remaining.Remove(selected); + } + + return result; + } + + /// + /// 使用别名方法进行O(1)时间复杂度的加权随机选择 + /// 适用于元素数量多、需要频繁选择的场景 + /// + /// 元素类型 + /// 元素和权重的字典 + /// 别名方法选择器 + public static AliasMethodSelector CreateAliasSelector(IDictionary items) + { + return new AliasMethodSelector(items); + } + } + + /// + /// 加权随机选择器构建器 + /// + /// 元素类型 + public class WeightedRandomBuilder + { + private readonly Dictionary _items = new(); + + /// + /// 添加元素 + /// + /// 元素 + /// 权重 + /// 构建器 + public WeightedRandomBuilder Add(T item, double weight) + { + if (weight < 0) + throw new ArgumentOutOfRangeException(nameof(weight), "权重不能为负数"); + + _items[item] = weight; + return this; + } + + /// + /// 添加多个元素 + /// + /// 元素和权重 + /// 构建器 + public WeightedRandomBuilder AddRange(IDictionary items) + { + foreach (var kvp in items) + { + _items[kvp.Key] = kvp.Value; + } + return this; + } + + /// + /// 构建选择器 + /// + /// 选择器 + public Func Build() + { + if (_items.Count == 0) + throw new InvalidOperationException("没有添加任何元素"); + + var items = new Dictionary(_items); + return () => WeightedRandomUtil.Select(items); + } + + /// + /// 构建别名方法选择器(高性能) + /// + /// 别名方法选择器 + public AliasMethodSelector BuildAliasSelector() + { + if (_items.Count == 0) + throw new InvalidOperationException("没有添加任何元素"); + + return new AliasMethodSelector(_items); + } + } + + /// + /// 别名方法选择器(O(1)时间复杂度) + /// + /// 元素类型 + public class AliasMethodSelector + { +#if NET6_0_OR_GREATER + private static Random SharedRandom => Random.Shared; +#else + private static readonly ThreadLocal ThreadLocalRandom = new(() => new Random(Guid.NewGuid().GetHashCode())); + private static Random GetSharedRandom() => ThreadLocalRandom.Value!; +#endif + + private readonly T[] _items; + private readonly double[] _probabilities; + private readonly int[] _alias; + private readonly int _count; + + public AliasMethodSelector(IDictionary items) + { + if (items == null || items.Count == 0) + throw new ArgumentException("元素集合不能为空"); + + _count = items.Count; + _items = items.Keys.ToArray(); + _probabilities = new double[_count]; + _alias = new int[_count]; + + Initialize(items.Values.ToArray()); + } + + private void Initialize(double[] weights) + { + var totalWeight = weights.Sum(); + var scale = _count / totalWeight; + + // 标准化权重 + var scaledWeights = weights.Select(w => w * scale).ToArray(); + + var small = new Queue(); + var large = new Queue(); + + for (int i = 0; i < _count; i++) + { + if (scaledWeights[i] < 1.0) + small.Enqueue(i); + else + large.Enqueue(i); + } + + while (small.Count > 0 && large.Count > 0) + { + var smallIndex = small.Dequeue(); + var largeIndex = large.Dequeue(); + + _probabilities[smallIndex] = scaledWeights[smallIndex]; + _alias[smallIndex] = largeIndex; + + scaledWeights[largeIndex] = scaledWeights[largeIndex] + scaledWeights[smallIndex] - 1.0; + + if (scaledWeights[largeIndex] < 1.0) + small.Enqueue(largeIndex); + else + large.Enqueue(largeIndex); + } + + while (large.Count > 0) + { + _probabilities[large.Dequeue()] = 1.0; + } + + while (small.Count > 0) + { + _probabilities[small.Dequeue()] = 1.0; + } + } + + /// + /// 随机选择一个元素 + /// + /// 选中的元素 + public T Select() + { +#if NET6_0_OR_GREATER + var index = SharedRandom.Next(_count); + var r = SharedRandom.NextDouble(); +#else + var random = GetSharedRandom(); + var index = random.Next(_count); + var r = random.NextDouble(); +#endif + + if (r < _probabilities[index]) + return _items[index]; + else + return _items[_alias[index]]; + } + + /// + /// 选择多个元素 + /// + /// 数量 + /// 选中的元素列表 + public List SelectMany(int count) + { + var result = new List(count); + for (int i = 0; i < count; i++) + { + result.Add(Select()); + } + return result; + } + } +} diff --git a/EasyTool.Core/MediaCategory/AudioUtil.cs b/EasyTool.Core/MediaCategory/AudioUtil.cs new file mode 100644 index 0000000..e23a486 --- /dev/null +++ b/EasyTool.Core/MediaCategory/AudioUtil.cs @@ -0,0 +1,229 @@ +using System; +using System.IO; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace EasyTool.MediaCategory +{ + /// + /// 音频工具类 + /// 提供音频转换、提取、处理等功能 + /// 需要安装 FFmpeg + /// + public static class AudioUtil + { + /// + /// FFmpeg 可执行文件路径 + /// + public static string? FFmpegPath { get; set; } + + /// + /// 转换音频格式 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 输出格式(mp3, wav, aac, flac 等) + /// 比特率(如 "128k", "256k") + /// 采样率(如 44100, 48000) + /// 是否成功 + public static bool Convert(string inputPath, string outputPath, string format, string? bitrate = null, int? sampleRate = null) + { + var args = $"-i \"{inputPath}\""; + + if (!string.IsNullOrEmpty(bitrate)) + args += $" -b:a {bitrate}"; + + if (sampleRate.HasValue) + args += $" -ar {sampleRate.Value}"; + + args += $" -f {format} \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 异步转换音频格式 + /// + public static async Task ConvertAsync(string inputPath, string outputPath, string format, string? bitrate = null, int? sampleRate = null) + { + return await Task.Run(() => Convert(inputPath, outputPath, format, bitrate, sampleRate)); + } + + /// + /// 从视频中提取音频 + /// + /// 视频文件路径 + /// 输出音频路径 + /// 输出格式 + /// 比特率 + /// 是否成功 + public static bool ExtractFromVideo(string videoPath, string outputPath, string format = "mp3", string? bitrate = "192k") + { + var args = $"-i \"{videoPath}\" -vn"; + + if (!string.IsNullOrEmpty(bitrate)) + args += $" -b:a {bitrate}"; + + args += $" -f {format} \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 裁剪音频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 开始时间 + /// 持续时间 + /// 是否成功 + public static bool Trim(string inputPath, string outputPath, TimeSpan startTime, TimeSpan duration) + { + var args = $"-i \"{inputPath}\" -ss {startTime:hh\\:mm\\:ss\\.fff} -t {duration:hh\\:mm\\:ss\\.fff} -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 合并音频文件 + /// + /// 输入文件路径列表 + /// 输出文件路径 + /// 是否成功 + public static bool Merge(string[] inputPaths, string outputPath) + { + // 创建临时文件列表 + var tempListPath = Path.Combine(Path.GetTempPath(), $"ffmpeg_list_{Guid.NewGuid():N}.txt"); + using (var writer = new StreamWriter(tempListPath)) + { + foreach (var path in inputPaths) + { + writer.WriteLine($"file '{path}'"); + } + } + + try + { + var args = $"-f concat -safe 0 -i \"{tempListPath}\" -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + finally + { + File.Delete(tempListPath); + } + } + + /// + /// 调整音量 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 音量因子(1.0 = 原音量,2.0 = 两倍,0.5 = 一半) + /// 是否成功 + public static bool AdjustVolume(string inputPath, string outputPath, double volumeFactor) + { + var args = $"-i \"{inputPath}\" -af \"volume={volumeFactor}\" \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 获取音频信息 + /// + /// 音频文件路径 + /// 音频信息 + public static AudioInfo? GetInfo(string filePath) + { + var args = $"-i \"{filePath}\" -hide_banner -show_format -show_streams -of json"; + var result = ExecuteFFmpegProbe(args); + + if (string.IsNullOrEmpty(result)) + return null; + + try + { + var json = System.Text.Json.JsonDocument.Parse(result); + var format = json.RootElement.GetProperty("format"); + + return new AudioInfo + { + Duration = TimeSpan.FromSeconds(double.Parse(format.GetProperty("duration").GetString() ?? "0")), + BitRate = long.Parse(format.GetProperty("bit_rate").GetString() ?? "0"), + Format = format.GetProperty("format_name").GetString() ?? "", + Size = long.Parse(format.GetProperty("size").GetString() ?? "0") + }; + } + catch + { + return null; + } + } + + private static bool ExecuteFFmpeg(string arguments) + { + var ffmpeg = FFmpegPath ?? "ffmpeg"; + var psi = new ProcessStartInfo + { + FileName = ffmpeg, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return false; + + process.WaitForExit(); + return process.ExitCode == 0; + } + + private static string? ExecuteFFmpegProbe(string arguments) + { + var ffprobe = FFmpegPath ?? "ffprobe"; + var probePath = ffprobe.Replace("ffmpeg", "ffprobe"); + + var psi = new ProcessStartInfo + { + FileName = probePath, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return null; + + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return output; + } + } + + /// + /// 音频信息 + /// + public class AudioInfo + { + /// + /// 时长 + /// + public TimeSpan Duration { get; set; } + + /// + /// 比特率 + /// + public long BitRate { get; set; } + + /// + /// 格式 + /// + public string Format { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long Size { get; set; } + } +} diff --git a/EasyTool.Core/MediaCategory/VideoUtil.cs b/EasyTool.Core/MediaCategory/VideoUtil.cs new file mode 100644 index 0000000..04729f1 --- /dev/null +++ b/EasyTool.Core/MediaCategory/VideoUtil.cs @@ -0,0 +1,363 @@ +using System; +using System.IO; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace EasyTool.MediaCategory +{ + /// + /// 视频工具类 + /// 提供视频转换、剪辑、处理等功能 + /// 需要安装 FFmpeg + /// + public static class VideoUtil + { + /// + /// FFmpeg 可执行文件路径 + /// + public static string? FFmpegPath { get; set; } + + /// + /// 转换视频格式 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 视频编码器(libx264, libx265, vp9 等) + /// 音频编码器(aac, mp3, opus 等) + /// 视频质量(0-51,越小质量越高,默认23) + /// 是否成功 + public static bool Convert(string inputPath, string outputPath, string? videoCodec = null, string? audioCodec = null, int? crf = null) + { + var args = $"-i \"{inputPath}\""; + + if (!string.IsNullOrEmpty(videoCodec)) + args += $" -c:v {videoCodec}"; + else + args += " -c:v libx264"; + + if (!string.IsNullOrEmpty(audioCodec)) + args += $" -c:a {audioCodec}"; + else + args += " -c:a aac"; + + if (crf.HasValue) + args += $" -crf {crf.Value}"; + else + args += " -crf 23"; + + args += $" \"{outputPath}\" -y"; + + return ExecuteFFmpeg(args); + } + + /// + /// 异步转换视频格式 + /// + public static async Task ConvertAsync(string inputPath, string outputPath, string? videoCodec = null, string? audioCodec = null, int? crf = null) + { + return await Task.Run(() => Convert(inputPath, outputPath, videoCodec, audioCodec, crf)); + } + + /// + /// 压缩视频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 质量(1-100,越小压缩率越高) + /// 是否成功 + public static bool Compress(string inputPath, string outputPath, int quality = 50) + { + var crf = 51 - (quality * 51 / 100); + var args = $"-i \"{inputPath}\" -c:v libx264 -crf {crf} -c:a aac -b:a 128k \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 裁剪视频 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 开始时间 + /// 持续时间 + /// 是否成功 + public static bool Trim(string inputPath, string outputPath, TimeSpan startTime, TimeSpan duration) + { + var args = $"-i \"{inputPath}\" -ss {startTime:hh\\:mm\\:ss\\.fff} -t {duration:hh\\:mm\\:ss\\.fff} -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 合并视频文件 + /// + /// 输入文件路径列表 + /// 输出文件路径 + /// 是否成功 + public static bool Merge(string[] inputPaths, string outputPath) + { + var tempListPath = Path.Combine(Path.GetTempPath(), $"ffmpeg_list_{Guid.NewGuid():N}.txt"); + using (var writer = new StreamWriter(tempListPath)) + { + foreach (var path in inputPaths) + { + writer.WriteLine($"file '{path}'"); + } + } + + try + { + var args = $"-f concat -safe 0 -i \"{tempListPath}\" -c copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + finally + { + File.Delete(tempListPath); + } + } + + /// + /// 提取视频帧为图片 + /// + /// 视频文件路径 + /// 输出目录 + /// 每秒帧数(默认1,即每秒1帧) + /// 图片格式(jpg, png) + /// 是否成功 + public static bool ExtractFrames(string videoPath, string outputDirectory, int fps = 1, string imageFormat = "jpg") + { + Directory.CreateDirectory(outputDirectory); + var args = $"-i \"{videoPath}\" -vf fps={fps} \"{outputDirectory}/frame_%04d.{imageFormat}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 从图片创建视频 + /// + /// 图片目录 + /// 输出视频路径 + /// 帧率 + /// 图片文件模式(如 "frame_%04d.jpg") + /// 是否成功 + public static bool CreateFromImages(string imageDirectory, string outputPath, int fps = 30, string imagePattern = "frame_%04d.jpg") + { + var args = $"-framerate {fps} -i \"{imageDirectory}/{imagePattern}\" -c:v libx264 -pix_fmt yuv420p \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 添加水印 + /// + /// 视频文件路径 + /// 水印图片路径 + /// 输出文件路径 + /// 水印位置 + /// 透明度(0-1) + /// 是否成功 + public static bool AddWatermark(string videoPath, string watermarkPath, string outputPath, WatermarkPosition position = WatermarkPosition.BottomRight, double opacity = 1.0) + { + var overlay = position switch + { + WatermarkPosition.TopLeft => "0:0", + WatermarkPosition.TopRight => "main_w-overlay_w-10:10", + WatermarkPosition.BottomLeft => "10:main_h-overlay_h-10", + WatermarkPosition.BottomRight => "main_w-overlay_w-10:main_h-overlay_h-10", + WatermarkPosition.Center => "(main_w-overlay_w)/2:(main_h-overlay_h)/2", + _ => "main_w-overlay_w-10:main_h-overlay_h-10" + }; + + var args = $"-i \"{videoPath}\" -i \"{watermarkPath}\" -filter_complex \"[1:v]format=rgba,colorchannelmixer=aa={opacity}[logo];[0:v][logo]overlay={overlay}\" -c:a copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 调整视频分辨率 + /// + /// 输入文件路径 + /// 输出文件路径 + /// 目标宽度 + /// 目标高度 + /// 是否成功 + public static bool Resize(string inputPath, string outputPath, int width, int height) + { + var args = $"-i \"{inputPath}\" -vf scale={width}:{height} -c:a copy \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + /// + /// 获取视频信息 + /// + /// 视频文件路径 + /// 视频信息 + public static VideoInfo? GetInfo(string filePath) + { + var args = $"-i \"{filePath}\" -hide_banner -show_format -show_streams -of json"; + var result = ExecuteFFmpegProbe(args); + + if (string.IsNullOrEmpty(result)) + return null; + + try + { + var json = System.Text.Json.JsonDocument.Parse(result); + var format = json.RootElement.GetProperty("format"); + + var info = new VideoInfo + { + Duration = TimeSpan.FromSeconds(double.Parse(format.GetProperty("duration").GetString() ?? "0")), + BitRate = long.Parse(format.GetProperty("bit_rate").GetString() ?? "0"), + Format = format.GetProperty("format_name").GetString() ?? "", + Size = long.Parse(format.GetProperty("size").GetString() ?? "0") + }; + + // 获取视频流信息 + var streams = json.RootElement.GetProperty("streams"); + foreach (var stream in streams.EnumerateArray()) + { + if (stream.GetProperty("codec_type").GetString() == "video") + { + info.Width = stream.GetProperty("width").GetInt32(); + info.Height = stream.GetProperty("height").GetInt32(); + info.VideoCodec = stream.GetProperty("codec_name").GetString() ?? ""; + if (stream.TryGetProperty("r_frame_rate", out var frameRate)) + { + var fpsStr = frameRate.GetString() ?? "0/1"; + var parts = fpsStr.Split('/'); + if (parts.Length == 2 && int.TryParse(parts[1], out var denom) && denom > 0) + { + info.FrameRate = double.Parse(parts[0]) / denom; + } + } + break; + } + } + + return info; + } + catch + { + return null; + } + } + + /// + /// 生成 GIF + /// + /// 视频文件路径 + /// 输出 GIF 路径 + /// 开始时间 + /// 持续时间 + /// 宽度(默认320) + /// 帧率(默认10) + /// 是否成功 + public static bool CreateGif(string videoPath, string outputPath, TimeSpan startTime, TimeSpan duration, int width = 320, int fps = 10) + { + var args = $"-i \"{videoPath}\" -ss {startTime:hh\\:mm\\:ss} -t {duration:hh\\:mm\\:ss} -vf \"fps={fps},scale={width}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\" \"{outputPath}\" -y"; + return ExecuteFFmpeg(args); + } + + private static bool ExecuteFFmpeg(string arguments) + { + var ffmpeg = FFmpegPath ?? "ffmpeg"; + var psi = new ProcessStartInfo + { + FileName = ffmpeg, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return false; + + process.WaitForExit(); + return process.ExitCode == 0; + } + + private static string? ExecuteFFmpegProbe(string arguments) + { + var ffprobe = FFmpegPath ?? "ffprobe"; + var probePath = ffprobe.Replace("ffmpeg", "ffprobe"); + + var psi = new ProcessStartInfo + { + FileName = probePath, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process == null) return null; + + var output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return output; + } + } + + /// + /// 视频信息 + /// + public class VideoInfo + { + /// + /// 时长 + /// + public TimeSpan Duration { get; set; } + + /// + /// 比特率 + /// + public long BitRate { get; set; } + + /// + /// 格式 + /// + public string Format { get; set; } = string.Empty; + + /// + /// 文件大小 + /// + public long Size { get; set; } + + /// + /// 视频宽度 + /// + public int Width { get; set; } + + /// + /// 视频高度 + /// + public int Height { get; set; } + + /// + /// 视频编码 + /// + public string VideoCodec { get; set; } = string.Empty; + + /// + /// 帧率 + /// + public double FrameRate { get; set; } + + /// + /// 分辨率字符串 + /// + public string Resolution => $"{Width}x{Height}"; + } + + /// + /// 水印位置 + /// + public enum WatermarkPosition + { + TopLeft, + TopRight, + BottomLeft, + BottomRight, + Center + } +} diff --git a/EasyTool.Core/NetCategory/DnsServerUtil.cs b/EasyTool.Core/NetCategory/DnsServerUtil.cs new file mode 100644 index 0000000..5b1706e --- /dev/null +++ b/EasyTool.Core/NetCategory/DnsServerUtil.cs @@ -0,0 +1,511 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// DNS 记录类型 + /// + public enum DnsRecordType + { + A = 1, + NS = 2, + CNAME = 5, + SOA = 6, + PTR = 12, + MX = 15, + TXT = 16, + AAAA = 28 + } + + /// + /// DNS 记录 + /// + public class DnsRecord + { + /// + /// 记录名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 记录类型 + /// + public DnsRecordType Type { get; set; } + + /// + /// TTL(秒) + /// + public int Ttl { get; set; } + + /// + /// 记录值 + /// + public string Value { get; set; } = string.Empty; + + /// + /// MX 优先级(仅 MX 记录) + /// + public int? Priority { get; set; } + + public override string ToString() + { + var priority = Priority.HasValue ? $" {Priority}" : ""; + return $"{Name} {Ttl} IN {Type} {priority}{Value}"; + } + } + + /// + /// DNS 查询选项 + /// + public class DnsQueryOptions + { + /// + /// DNS 服务器地址 + /// + public IPAddress DnsServer { get; set; } = IPAddress.Parse("8.8.8.8"); + + /// + /// DNS 服务器端口 + /// + public int Port { get; set; } = 53; + + /// + /// 查询超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 是否使用 TCP + /// + public bool UseTcp { get; set; } + + /// + /// 是否递归查询 + /// + public bool RecursionDesired { get; set; } = true; + } + + /// + /// DNS 工具类 + /// 提供 DNS 查询和解析功能 + /// + public static class DnsServerUtil + { + private static readonly Random _random = new(); + + /// + /// 查询 A 记录 + /// + /// 域名 + /// 查询选项 + /// IP 地址列表 + public static async Task> QueryAAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.A, options); + + return records + .Select(r => IPAddress.TryParse(r.Value, out var ip) ? ip : null) + .Where(ip => ip != null) + .Cast() + .ToList(); + } + + /// + /// 查询 AAAA 记录 + /// + /// 域名 + /// 查询选项 + /// IPv6 地址列表 + public static async Task> QueryAaaaAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.AAAA, options); + + return records + .Select(r => IPAddress.TryParse(r.Value, out var ip) ? ip : null) + .Where(ip => ip != null) + .Cast() + .ToList(); + } + + /// + /// 查询 MX 记录 + /// + /// 域名 + /// 查询选项 + /// MX 记录列表 + public static async Task> QueryMxAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.MX, options); + + return records + .Where(r => r.Priority.HasValue) + .Select(r => (Priority: r.Priority!.Value, MailServer: r.Value)) + .OrderBy(r => r.Priority) + .ToList(); + } + + /// + /// 查询 TXT 记录 + /// + /// 域名 + /// 查询选项 + /// TXT 记录列表 + public static async Task> QueryTxtAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.TXT, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 查询 CNAME 记录 + /// + /// 域名 + /// 查询选项 + /// CNAME 目标 + public static async Task QueryCnameAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.CNAME, options); + return records.FirstOrDefault()?.Value; + } + + /// + /// 查询 NS 记录 + /// + /// 域名 + /// 查询选项 + /// NS 服务器列表 + public static async Task> QueryNsAsync(string domain, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + var records = await QueryAsync(domain, DnsRecordType.NS, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 反向查询(IP 到域名) + /// + /// IP 地址 + /// 查询选项 + /// 域名列表 + public static async Task> ReverseQueryAsync(IPAddress ipAddress, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + + // 构建反向查询域名 + var bytes = ipAddress.GetAddressBytes(); + Array.Reverse(bytes); + var ptrDomain = $"{string.Join(".", bytes)}.in-addr.arpa"; + + var records = await QueryAsync(ptrDomain, DnsRecordType.PTR, options); + return records.Select(r => r.Value).ToList(); + } + + /// + /// 通用 DNS 查询 + /// + /// 域名 + /// 记录类型 + /// 查询选项 + /// DNS 记录列表 + public static async Task> QueryAsync(string domain, DnsRecordType recordType, DnsQueryOptions? options = null) + { + options ??= new DnsQueryOptions(); + + // 构建查询包 + var queryPacket = BuildQueryPacket(domain, recordType, options.RecursionDesired); + + // 发送查询 + byte[] responseBytes; + + if (options.UseTcp) + { + responseBytes = await QueryOverTcpAsync(queryPacket, options); + } + else + { + responseBytes = await QueryOverUdpAsync(queryPacket, options); + } + + // 解析响应 + return ParseResponse(responseBytes); + } + + /// + /// 批量查询 + /// + /// 域名列表 + /// 记录类型 + /// 查询选项 + /// 域名到记录列表的映射 + public static async Task>> QueryManyAsync( + IEnumerable domains, + DnsRecordType recordType, + DnsQueryOptions? options = null) + { + var result = new Dictionary>(); + + foreach (var domain in domains) + { + result[domain] = await QueryAsync(domain, recordType, options); + } + + return result; + } + + /// + /// 获取本机 DNS 服务器 + /// + /// DNS 服务器列表 + public static List GetLocalDnsServers() + { + var servers = new List(); + + try + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var iface in interfaces) + { + if (iface.OperationalStatus != OperationalStatus.Up) + continue; + + var ipProps = iface.GetIPProperties(); + var dnsAddresses = ipProps.DnsAddresses; + foreach (var dns in dnsAddresses) + { + if (!servers.Contains(dns)) + { + servers.Add(dns); + } + } + } + } + catch + { + // 返回默认 DNS 服务器 + servers.Add(IPAddress.Parse("8.8.8.8")); + servers.Add(IPAddress.Parse("8.8.4.4")); + } + + return servers; + } + + #region 私有方法 + + private static byte[] BuildQueryPacket(string domain, DnsRecordType recordType, bool recursionDesired) + { + using var stream = new System.IO.MemoryStream(); + using var writer = new BinaryWriter(stream); + + // Transaction ID + writer.Write((ushort)_random.Next(0, 65536)); + + // Flags + var flags = (ushort)0x0100; // Standard query + if (recursionDesired) + flags |= 0x0100; + writer.Write(flags); + + // Questions count + writer.Write((ushort)1); + + // Answer, Authority, Additional counts + writer.Write((ushort)0); + writer.Write((ushort)0); + writer.Write((ushort)0); + + // Question section + WriteDomainName(writer, domain); + writer.Write((ushort)recordType); + writer.Write((ushort)1); // Class IN + + return stream.ToArray(); + } + + private static void WriteDomainName(BinaryWriter writer, string domain) + { + var parts = domain.Split('.'); + foreach (var part in parts) + { + var bytes = Encoding.ASCII.GetBytes(part); + writer.Write((byte)bytes.Length); + writer.Write(bytes); + } + writer.Write((byte)0); + } + + private static async Task QueryOverUdpAsync(byte[] query, DnsQueryOptions options) + { + using var client = new UdpClient(); + client.Client.ReceiveTimeout = (int)options.Timeout.TotalMilliseconds; + + await client.SendAsync(query, query.Length, options.DnsServer.ToString(), options.Port); + + var result = await client.ReceiveAsync(); + return result.Buffer; + } + + private static async Task QueryOverTcpAsync(byte[] query, DnsQueryOptions options) + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(options.DnsServer, options.Port); + + if (await Task.WhenAny(connectTask, Task.Delay(options.Timeout)) != connectTask) + { + throw new TimeoutException("DNS 查询超时"); + } + + await connectTask; + + var stream = client.GetStream(); + stream.ReadTimeout = (int)options.Timeout.TotalMilliseconds; + stream.WriteTimeout = (int)options.Timeout.TotalMilliseconds; + + // TCP DNS 需要在前面加 2 字节长度 + var lengthBytes = BitConverter.GetBytes((ushort)query.Length); + Array.Reverse(lengthBytes); // Big-endian + + await stream.WriteAsync(lengthBytes, 0, 2); + await stream.WriteAsync(query, 0, query.Length); + + // 读取响应 + var responseLengthBytes = new byte[2]; + await stream.ReadAsync(responseLengthBytes, 0, 2); + Array.Reverse(responseLengthBytes); + var responseLength = BitConverter.ToUInt16(responseLengthBytes, 0); + + var response = new byte[responseLength]; + await stream.ReadAsync(response, 0, responseLength); + + return response; + } + + private static List ParseResponse(byte[] response) + { + var records = new List(); + + using var stream = new System.IO.MemoryStream(response); + using var reader = new BinaryReader(stream); + + // 跳过 Header (12 bytes) + reader.ReadBytes(12); + + // Question count + var questionCount = reader.ReadUInt16(); + for (int i = 0; i < questionCount; i++) + { + ReadDomainName(reader); + reader.ReadUInt16(); // Type + reader.ReadUInt16(); // Class + } + + // Answer count + var answerCount = reader.ReadUInt16(); + for (int i = 0; i < answerCount; i++) + { + var name = ReadDomainName(reader); + var type = (DnsRecordType)reader.ReadUInt16(); + reader.ReadUInt16(); // Class + var ttl = (int)reader.ReadUInt32(); + var dataLength = reader.ReadUInt16(); + var dataPosition = stream.Position; + + var record = new DnsRecord + { + Name = name, + Type = type, + Ttl = ttl + }; + + switch (type) + { + case DnsRecordType.A: + var aBytes = reader.ReadBytes(4); + record.Value = new IPAddress(aBytes).ToString(); + break; + + case DnsRecordType.AAAA: + var aaaaBytes = reader.ReadBytes(16); + record.Value = new IPAddress(aaaaBytes).ToString(); + break; + + case DnsRecordType.CNAME: + case DnsRecordType.NS: + case DnsRecordType.PTR: + record.Value = ReadDomainName(reader); + break; + + case DnsRecordType.MX: + record.Priority = reader.ReadUInt16(); + record.Value = ReadDomainName(reader); + break; + + case DnsRecordType.TXT: + var txtLength = reader.ReadByte(); + record.Value = Encoding.ASCII.GetString(reader.ReadBytes(txtLength)); + break; + + default: + stream.Position = dataPosition + dataLength; + break; + } + + records.Add(record); + } + + return records; + } + + private static string ReadDomainName(BinaryReader reader) + { + var labels = new List(); + var visited = new HashSet(); + + while (true) + { + var length = reader.ReadByte(); + + if (length == 0) + break; + + // 指针压缩 + if ((length & 0xC0) == 0xC0) + { + var offset = ((length & 0x3F) << 8) | reader.ReadByte(); + if (visited.Contains(offset)) + break; + + visited.Add(offset); + var currentPos = reader.BaseStream.Position; + reader.BaseStream.Position = offset; + + var pointerLabel = ReadDomainName(reader); + labels.Add(pointerLabel); + + reader.BaseStream.Position = currentPos; + break; + } + + labels.Add(Encoding.ASCII.GetString(reader.ReadBytes(length))); + } + + return string.Join(".", labels); + } + + #endregion + } +} diff --git a/EasyTool.Core/NetCategory/DnsUtil.cs b/EasyTool.Core/NetCategory/DnsUtil.cs new file mode 100644 index 0000000..0fc375c --- /dev/null +++ b/EasyTool.Core/NetCategory/DnsUtil.cs @@ -0,0 +1,461 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// DNS 解析工具类 + /// 提供域名解析、反向解析、DNS 查询等功能 + /// + public static class DnsUtil + { + #region 正向解析 + + /// + /// 解析域名获取 IP 地址 + /// + /// 主机名或域名 + /// IP 地址列表 + public static string[] GetIPAddresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 解析域名获取 IPv4 地址 + /// + /// 主机名或域名 + /// IPv4 地址列表 + public static string[] GetIPv4Addresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 解析域名获取 IPv6 地址 + /// + /// 主机名或域名 + /// IPv6 地址列表 + public static string[] GetIPv6Addresses(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + /// + /// 获取第一个 IP 地址 + /// + /// 主机名或域名 + /// IP 地址,解析失败返回 null + public static string? GetFirstIPAddress(string hostName) + { + var ips = GetIPAddresses(hostName); + return ips.Length > 0 ? ips[0] : null; + } + + /// + /// 异步解析域名获取 IP 地址 + /// + /// 主机名或域名 + /// IP 地址列表 + public static async Task GetIPAddressesAsync(string hostName) + { + try + { + var entry = await Dns.GetHostEntryAsync(hostName); + return entry.AddressList + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork || ip.AddressFamily == AddressFamily.InterNetworkV6) + .Select(ip => ip.ToString()) + .ToArray(); + } + catch + { + return Array.Empty(); + } + } + + #endregion + + #region 反向解析 + + /// + /// 反向解析 IP 地址获取主机名 + /// + /// IP 地址 + /// 主机名 + public static string? GetHostName(string ipAddress) + { + try + { + var entry = Dns.GetHostEntry(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + /// + /// 反向解析 IP 地址获取主机名 + /// + /// IP 地址对象 + /// 主机名 + public static string? GetHostName(IPAddress ipAddress) + { + try + { + var entry = Dns.GetHostEntry(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + /// + /// 异步反向解析 IP 地址获取主机名 + /// + /// IP 地址 + /// 主机名 + public static async Task GetHostNameAsync(string ipAddress) + { + try + { + var entry = await Dns.GetHostEntryAsync(ipAddress); + return entry.HostName; + } + catch + { + return null; + } + } + + #endregion + + #region 本机信息 + + /// + /// 获取本机主机名 + /// + /// 主机名 + public static string GetLocalHostName() + { + return Dns.GetHostName(); + } + + /// + /// 获取本机 IP 地址 + /// + /// IP 地址列表 + public static string[] GetLocalIPAddresses() + { + return GetIPAddresses(Dns.GetHostName()); + } + + /// + /// 获取本机 IPv4 地址 + /// + /// IPv4 地址列表 + public static string[] GetLocalIPv4Addresses() + { + return GetIPv4Addresses(Dns.GetHostName()); + } + + /// + /// 获取本机主要 IP 地址(优先返回内网地址) + /// + /// IP 地址 + public static string? GetLocalMainIPAddress() + { + var hostName = Dns.GetHostName(); + var entry = Dns.GetHostEntry(hostName); + + // 优先返回非回环、非链路本地地址 + var ip = entry.AddressList + .Where(a => a.AddressFamily == AddressFamily.InterNetwork) + .FirstOrDefault(a => !IPAddress.IsLoopback(a) && !IsLinkLocal(a)); + + return ip?.ToString(); + } + + private static bool IsLinkLocal(IPAddress ip) + { + if (ip.AddressFamily != AddressFamily.InterNetwork) + return false; + + var bytes = ip.GetAddressBytes(); + return bytes[0] == 169 && bytes[1] == 254; // 169.254.x.x + } + + #endregion + + #region DNS 记录查询 + + /// + /// 获取 MX 记录(邮件交换记录) + /// 注意:需要使用外部库或自定义实现,这里返回空 + /// + /// 域名 + /// MX 记录列表 + public static List GetMxRecords(string domain) + { + // 简化实现,实际需要使用 DnsClient 等库 + return new List(); + } + + /// + /// 获取 TXT 记录 + /// + /// 域名 + /// TXT 记录列表 + public static List GetTxtRecords(string domain) + { + // 简化实现 + return new List(); + } + + /// + /// 获取 CNAME 记录 + /// + /// 域名 + /// CNAME 记录 + public static string? GetCnameRecord(string domain) + { + // 简化实现 + return null; + } + + #endregion + + #region 验证方法 + + /// + /// 验证域名是否可以解析 + /// + /// 主机名或域名 + /// 是否可以解析 + public static bool CanResolve(string hostName) + { + try + { + var entry = Dns.GetHostEntry(hostName); + return entry.AddressList.Length > 0; + } + catch + { + return false; + } + } + + /// + /// 异步验证域名是否可以解析 + /// + /// 主机名或域名 + /// 是否可以解析 + public static async Task CanResolveAsync(string hostName) + { + try + { + var entry = await Dns.GetHostEntryAsync(hostName); + return entry.AddressList.Length > 0; + } + catch + { + return false; + } + } + + /// + /// 验证 IP 地址格式是否有效 + /// + /// IP 地址字符串 + /// 是否有效 + public static bool IsValidIPAddress(string ipString) + { + return IPAddress.TryParse(ipString, out _); + } + + /// + /// 验证是否为 IPv4 地址 + /// + /// IP 地址字符串 + /// 是否为 IPv4 + public static bool IsIPv4(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return ip.AddressFamily == AddressFamily.InterNetwork; + } + return false; + } + + /// + /// 验证是否为 IPv6 地址 + /// + /// IP 地址字符串 + /// 是否为 IPv6 + public static bool IsIPv6(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return ip.AddressFamily == AddressFamily.InterNetworkV6; + } + return false; + } + + /// + /// 验证是否为内网 IP 地址 + /// + /// IP 地址字符串 + /// 是否为内网 IP + public static bool IsPrivateIP(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return false; + + if (ip.AddressFamily != AddressFamily.InterNetwork) + return false; + + var bytes = ip.GetAddressBytes(); + + // 10.0.0.0 - 10.255.255.255 + if (bytes[0] == 10) + return true; + + // 172.16.0.0 - 172.31.255.255 + if (bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31) + return true; + + // 192.168.0.0 - 192.168.255.255 + if (bytes[0] == 192 && bytes[1] == 168) + return true; + + return false; + } + + /// + /// 验证是否为回环地址 + /// + /// IP 地址字符串 + /// 是否为回环地址 + public static bool IsLoopback(string ipString) + { + if (IPAddress.TryParse(ipString, out var ip)) + { + return IPAddress.IsLoopback(ip); + } + return false; + } + + #endregion + + #region 工具方法 + + /// + /// 将 IP 地址转换为长整型 + /// + /// IP 地址字符串 + /// 长整型值 + public static long IPToLong(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return 0; + + var bytes = ip.GetAddressBytes(); + if (bytes.Length != 4) + return 0; + + return ((long)bytes[0] << 24) | ((long)bytes[1] << 16) | ((long)bytes[2] << 8) | bytes[3]; + } + + /// + /// 将长整型转换为 IP 地址 + /// + /// 长整型值 + /// IP 地址字符串 + public static string LongToIP(long ipLong) + { + return $"{(ipLong >> 24) & 0xFF}.{(ipLong >> 16) & 0xFF}.{(ipLong >> 8) & 0xFF}.{ipLong & 0xFF}"; + } + + /// + /// 获取 IP 地址类型描述 + /// + /// IP 地址字符串 + /// 类型描述 + public static string GetIPType(string ipString) + { + if (!IPAddress.TryParse(ipString, out var ip)) + return "无效IP"; + + if (IPAddress.IsLoopback(ip)) + return "回环地址"; + + if (IsPrivateIP(ipString)) + return "内网地址"; + + return "公网地址"; + } + + #endregion + } + + /// + /// MX 记录 + /// + public class MxRecord + { + /// + /// 优先级 + /// + public int Priority { get; set; } + + /// + /// 邮件服务器地址 + /// + public string? Exchange { get; set; } + + public override string ToString() + { + return $"[{Priority}] {Exchange}"; + } + } +} diff --git a/EasyTool.Core/NetCategory/FtpUtil.cs b/EasyTool.Core/NetCategory/FtpUtil.cs new file mode 100644 index 0000000..4b60db5 --- /dev/null +++ b/EasyTool.Core/NetCategory/FtpUtil.cs @@ -0,0 +1,600 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// FTP 文件传输工具类 + /// 提供 FTP 文件上传、下载、删除、列表等功能 + /// + public static class FtpUtil + { + #region 上传方法 + + /// + /// 上传文件到 FTP 服务器 + /// + /// FTP 配置 + /// 本地文件路径 + /// 远程文件路径 + /// 是否成功 + public static bool Upload(FtpConfig config, string localFilePath, string remoteFilePath) + { + if (!File.Exists(localFilePath)) + throw new FileNotFoundException("本地文件不存在", localFilePath); + + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + + using var fileStream = File.OpenRead(localFilePath); + using var requestStream = request.GetRequestStream(); + fileStream.CopyTo(requestStream); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步上传文件到 FTP 服务器 + /// + /// FTP 配置 + /// 本地文件路径 + /// 远程文件路径 + /// 是否成功 + public static async Task UploadAsync(FtpConfig config, string localFilePath, string remoteFilePath) + { + if (!File.Exists(localFilePath)) + throw new FileNotFoundException("本地文件不存在", localFilePath); + + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + + using var fileStream = File.OpenRead(localFilePath); + using var requestStream = await request.GetRequestStreamAsync(); + await fileStream.CopyToAsync(requestStream); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 上传数据到 FTP 服务器 + /// + /// FTP 配置 + /// 数据 + /// 远程文件路径 + /// 是否成功 + public static bool UploadData(FtpConfig config, byte[] data, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + request.ContentLength = data.Length; + + using var requestStream = request.GetRequestStream(); + requestStream.Write(data, 0, data.Length); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步上传数据到 FTP 服务器 + /// + /// FTP 配置 + /// 数据 + /// 远程文件路径 + /// 是否成功 + public static async Task UploadDataAsync(FtpConfig config, byte[] data, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.UploadFile); + request.ContentLength = data.Length; + + using var requestStream = await request.GetRequestStreamAsync(); + await requestStream.WriteAsync(data, 0, data.Length); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + return response.StatusCode == FtpStatusCode.ClosingData; + } + + #endregion + + #region 下载方法 + + /// + /// 从 FTP 服务器下载文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 本地文件路径 + /// 是否成功 + public static bool Download(FtpConfig config, string remoteFilePath, string localFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var fileStream = File.Create(localFilePath); + responseStream?.CopyTo(fileStream); + + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 异步从 FTP 服务器下载文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 本地文件路径 + /// 是否成功 + public static async Task DownloadAsync(FtpConfig config, string remoteFilePath, string localFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var fileStream = File.Create(localFilePath); + + if (responseStream != null) + { + await responseStream.CopyToAsync(fileStream); + } + + return response.StatusCode == FtpStatusCode.ClosingData; + } + + /// + /// 从 FTP 服务器下载数据 + /// + /// FTP 配置 + /// 远程文件路径 + /// 下载数据 + public static byte[] DownloadData(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var memoryStream = new MemoryStream(); + responseStream?.CopyTo(memoryStream); + return memoryStream.ToArray(); + } + + /// + /// 异步从 FTP 服务器下载数据 + /// + /// FTP 配置 + /// 远程文件路径 + /// 下载数据 + public static async Task DownloadDataAsync(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var memoryStream = new MemoryStream(); + + if (responseStream != null) + { + await responseStream.CopyToAsync(memoryStream); + } + + return memoryStream.ToArray(); + } + + /// + /// 下载文件内容为字符串 + /// + /// FTP 配置 + /// 远程文件路径 + /// 编码方式 + /// 文件内容 + public static string DownloadString(FtpConfig config, string remoteFilePath, Encoding? encoding = null) + { + var data = DownloadData(config, remoteFilePath); + encoding ??= Encoding.UTF8; + return encoding.GetString(data); + } + + /// + /// 异步下载文件内容为字符串 + /// + /// FTP 配置 + /// 远程文件路径 + /// 编码方式 + /// 文件内容 + public static async Task DownloadStringAsync(FtpConfig config, string remoteFilePath, Encoding? encoding = null) + { + var data = await DownloadDataAsync(config, remoteFilePath); + encoding ??= Encoding.UTF8; + return encoding.GetString(data); + } + + #endregion + + #region 目录操作 + + /// + /// 列出目录内容 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件列表 + public static List ListDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectoryDetails); + var items = new List(); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = reader.ReadLine()) != null) + { + var item = ParseFtpLine(line); + if (item != null) + { + items.Add(item); + } + } + + return items; + } + + /// + /// 异步列出目录内容 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件列表 + public static async Task> ListDirectoryAsync(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectoryDetails); + var items = new List(); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = await reader.ReadLineAsync()) != null) + { + var item = ParseFtpLine(line); + if (item != null) + { + items.Add(item); + } + } + + return items; + } + + /// + /// 列出目录中的文件名 + /// + /// FTP 配置 + /// 远程目录路径 + /// 文件名列表 + public static List ListFileNames(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.ListDirectory); + var names = new List(); + + using var response = (FtpWebResponse)request.GetResponse(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream ?? Stream.Null, Encoding.UTF8); + + string? line; + while ((line = reader.ReadLine()) != null) + { + if (!string.IsNullOrWhiteSpace(line)) + { + names.Add(line.Trim()); + } + } + + return names; + } + + /// + /// 创建目录 + /// + /// FTP 配置 + /// 远程目录路径 + /// 是否成功 + public static bool CreateDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.MakeDirectory); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.PathnameCreated; + } + + /// + /// 删除目录 + /// + /// FTP 配置 + /// 远程目录路径 + /// 是否成功 + public static bool DeleteDirectory(FtpConfig config, string remotePath) + { + var request = CreateRequest(config, remotePath, WebRequestMethods.Ftp.RemoveDirectory); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + #endregion + + #region 文件操作 + + /// + /// 删除文件 + /// + /// FTP 配置 + /// 远程文件路径 + /// 是否成功 + public static bool DeleteFile(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.DeleteFile); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + /// + /// 重命名文件或目录 + /// + /// FTP 配置 + /// 原路径 + /// 新路径 + /// 是否成功 + public static bool Rename(FtpConfig config, string oldPath, string newPath) + { + var request = CreateRequest(config, oldPath, WebRequestMethods.Ftp.Rename); + request.RenameTo = newPath; + + using var response = (FtpWebResponse)request.GetResponse(); + return response.StatusCode == FtpStatusCode.FileActionOK; + } + + /// + /// 检查文件是否存在 + /// + /// FTP 配置 + /// 远程文件路径 + /// 是否存在 + public static bool FileExists(FtpConfig config, string remoteFilePath) + { + try + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetFileSize); + using var response = (FtpWebResponse)request.GetResponse(); + return true; + } + catch (WebException ex) when (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) + { + return false; + } + } + + /// + /// 获取文件大小 + /// + /// FTP 配置 + /// 远程文件路径 + /// 文件大小(字节) + public static long GetFileSize(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetFileSize); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.ContentLength; + } + + /// + /// 获取文件修改时间 + /// + /// FTP 配置 + /// 远程文件路径 + /// 修改时间 + public static DateTime GetLastModified(FtpConfig config, string remoteFilePath) + { + var request = CreateRequest(config, remoteFilePath, WebRequestMethods.Ftp.GetDateTimestamp); + + using var response = (FtpWebResponse)request.GetResponse(); + return response.LastModified; + } + + #endregion + + #region 私有方法 + + private static FtpWebRequest CreateRequest(FtpConfig config, string remotePath, string method) + { + string url = config.Host; + if (!url.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase)) + { + url = "ftp://" + url; + } + if (!url.EndsWith("/") && !remotePath.StartsWith("/")) + { + url += "/"; + } + url += remotePath.TrimStart('/'); + + var request = (FtpWebRequest)WebRequest.Create(url); + request.Method = method; + request.Credentials = new NetworkCredential(config.UserName, config.Password); + request.UseBinary = config.UseBinary; + request.UsePassive = config.UsePassive; + request.EnableSsl = config.EnableSsl; + request.KeepAlive = config.KeepAlive; + request.Timeout = config.Timeout; + + return request; + } + + private static FtpItem? ParseFtpLine(string line) + { + if (string.IsNullOrWhiteSpace(line)) + return null; + + // UNIX 风格: drwxr-xr-x 2 owner group 4096 Jan 1 12:00 name + // Windows 风格: 01-01-24 12:00PM name + // 01-01-24 12:00PM 12345 name + + var item = new FtpItem(); + string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length < 4) + return null; + + // 检查是否为 Windows 风格 + if (parts[0].Contains("-") && parts[1].Contains(":")) + { + // Windows 风格 + if (parts[2] == "") + { + item.IsDirectory = true; + item.Name = string.Join(" ", parts.Skip(3)); + } + else + { + item.IsDirectory = false; + if (long.TryParse(parts[2], out long size)) + { + item.Size = size; + } + item.Name = string.Join(" ", parts.Skip(3)); + } + return item; + } + + // UNIX 风格 + if (parts[0].StartsWith("d")) + { + item.IsDirectory = true; + } + else if (parts[0].StartsWith("-")) + { + item.IsDirectory = false; + // 尝试解析大小 + if (parts.Length > 4 && long.TryParse(parts[4], out long size)) + { + item.Size = size; + } + } + + // 获取文件名(最后一个部分) + item.Name = parts[parts.Length - 1]; + + return item; + } + + #endregion + } + + #region 配置和结果类 + + /// + /// FTP 配置 + /// + public class FtpConfig + { + /// + /// FTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 用户名 + /// + public string UserName { get; set; } = string.Empty; + + /// + /// 密码 + /// + public string Password { get; set; } = string.Empty; + + /// + /// 是否使用二进制模式(默认true) + /// + public bool UseBinary { get; set; } = true; + + /// + /// 是否使用被动模式(默认true) + /// + public bool UsePassive { get; set; } = true; + + /// + /// 是否启用 SSL(默认false) + /// + public bool EnableSsl { get; set; } + + /// + /// 是否保持连接(默认true) + /// + public bool KeepAlive { get; set; } = true; + + /// + /// 超时时间(毫秒,默认30000) + /// + public int Timeout { get; set; } = 30000; + + /// + /// 创建匿名 FTP 配置 + /// + /// FTP 服务器地址 + /// FTP 配置 + public static FtpConfig Anonymous(string host) + { + return new FtpConfig + { + Host = host, + UserName = "anonymous", + Password = "anonymous@anonymous.com" + }; + } + } + + /// + /// FTP 文件项 + /// + public class FtpItem + { + /// + /// 文件名 + /// + public string? Name { get; set; } + + /// + /// 是否为目录 + /// + public bool IsDirectory { get; set; } + + /// + /// 文件大小(字节) + /// + public long Size { get; set; } + + /// + /// 修改时间(如果可用) + /// + public DateTime LastModified { get; set; } + + /// + /// 权限(如果可用) + /// + public string? Permissions { get; set; } + + public override string ToString() + { + return IsDirectory ? $"[{Name}]" : $"{Name} ({Size} bytes)"; + } + } + + #endregion +} diff --git a/EasyTool.Core/NetCategory/GrpcUtil.cs b/EasyTool.Core/NetCategory/GrpcUtil.cs new file mode 100644 index 0000000..15b5ca0 --- /dev/null +++ b/EasyTool.Core/NetCategory/GrpcUtil.cs @@ -0,0 +1,424 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// gRPC 配置选项 + /// + public class GrpcOptions + { + /// + /// 服务地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } = true; + + /// + /// 是否忽略 SSL 证书错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 最大接收消息大小(字节) + /// + public int? MaxReceiveMessageSize { get; set; } + + /// + /// 最大发送消息大小(字节) + /// + public int? MaxSendMessageSize { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromSeconds(1); + } + + /// + /// gRPC 工具类 + /// 注意:此类提供 gRPC 调用的抽象接口,实际使用需要引入 Grpc.Net.Client 包 + /// + public static class GrpcUtil + { + /// + /// 创建 gRPC 通道配置 + /// + /// gRPC 配置 + /// 配置对象 + public static GrpcChannelConfiguration CreateChannelConfiguration(GrpcOptions options) + { + return new GrpcChannelConfiguration + { + Address = options.Address, + UseSsl = options.UseSsl, + IgnoreSslErrors = options.IgnoreSslErrors, + MaxReceiveMessageSize = options.MaxReceiveMessageSize, + MaxSendMessageSize = options.MaxSendMessageSize, + Timeout = options.Timeout, + Headers = options.Headers, + EnableCompression = options.EnableCompression + }; + } + + /// + /// 构建 gRPC 服务 URL + /// + /// 主机地址 + /// 端口 + /// 是否使用 SSL + /// 服务 URL + public static string BuildServiceUrl(string host, int port, bool useSsl = true) + { + var scheme = useSsl ? "https" : "http"; + return $"{scheme}://{host}:{port}"; + } + + /// + /// 创建 gRPC 元数据 + /// + /// 请求头 + /// 元数据 + public static GrpcMetadata CreateMetadata(Dictionary headers) + { + return new GrpcMetadata + { + Headers = headers + }; + } + + /// + /// 创建带认证的元数据 + /// + /// Bearer Token + /// 额外请求头 + /// 元数据 + public static GrpcMetadata CreateAuthenticatedMetadata(string token, Dictionary? additionalHeaders = null) + { + var headers = additionalHeaders ?? new Dictionary(); + headers["Authorization"] = $"Bearer {token}"; + return new GrpcMetadata { Headers = headers }; + } + + /// + /// 创建 API Key 认证元数据 + /// + /// API Key + /// 请求头名称 + /// 元数据 + public static GrpcMetadata CreateApiKeyMetadata(string apiKey, string headerName = "x-api-key") + { + return new GrpcMetadata + { + Headers = new Dictionary + { + [headerName] = apiKey + } + }; + } + + /// + /// 执行带重试的 gRPC 调用 + /// + /// 返回类型 + /// gRPC 调用 + /// 重试次数 + /// 重试延迟 + /// 取消令牌 + /// 调用结果 + public static async Task ExecuteWithRetryAsync( + Func> call, + int retryCount = 3, + TimeSpan? retryDelay = null, + CancellationToken cancellationToken = default) + { + var delay = retryDelay ?? TimeSpan.FromSeconds(1); + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await call(); + } + catch (Exception ex) when (IsRetryableError(ex)) + { + lastException = ex; + + if (i < retryCount) + { + await Task.Delay(delay, cancellationToken); + } + } + } + + throw lastException ?? new Exception("gRPC 调用失败"); + } + + /// + /// 执行带超时的 gRPC 调用 + /// + /// 返回类型 + /// gRPC 调用 + /// 超时时间 + /// 取消令牌 + /// 调用结果 + public static async Task ExecuteWithTimeoutAsync( + Func> call, + TimeSpan timeout, + CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + try + { + return await call(cts.Token); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + throw new TimeoutException($"gRPC 调用超时: {timeout}"); + } + } + + private static bool IsRetryableError(Exception ex) + { + // 判断是否为可重试的错误 + var message = ex.Message.ToLowerInvariant(); + return message.Contains("unavailable") || + message.Contains("deadline exceeded") || + message.Contains("resource exhausted") || + message.Contains("internal") || + message.Contains("unknown"); + } + } + + /// + /// gRPC 通道配置 + /// + public class GrpcChannelConfiguration + { + /// + /// 服务地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 是否使用 SSL + /// + public bool UseSsl { get; set; } = true; + + /// + /// 是否忽略 SSL 证书错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 最大接收消息大小 + /// + public int? MaxReceiveMessageSize { get; set; } + + /// + /// 最大发送消息大小 + /// + public int? MaxSendMessageSize { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否启用压缩 + /// + public bool EnableCompression { get; set; } + } + + /// + /// gRPC 元数据 + /// + public class GrpcMetadata + { + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 添加请求头 + /// + /// 键 + /// 值 + public void Add(string key, string value) + { + Headers[key] = value; + } + + /// + /// 获取请求头 + /// + /// 键 + /// + public string? Get(string key) + { + return Headers.TryGetValue(key, out var value) ? value : null; + } + } + + /// + /// gRPC 响应状态 + /// + public class GrpcResponseStatus + { + /// + /// 状态码 + /// + public GrpcStatusCode StatusCode { get; set; } + + /// + /// 错误详情 + /// + public string? Detail { get; set; } + + /// + /// 是否成功 + /// + public bool IsSuccess => StatusCode == GrpcStatusCode.OK; + + /// + /// 创建成功状态 + /// + public static GrpcResponseStatus Success => new() { StatusCode = GrpcStatusCode.OK }; + + /// + /// 创建错误状态 + /// + public static GrpcResponseStatus Error(GrpcStatusCode code, string detail) => new() + { + StatusCode = code, + Detail = detail + }; + } + + /// + /// gRPC 状态码 + /// + public enum GrpcStatusCode + { + /// + /// 成功 + /// + OK = 0, + + /// + /// 取消 + /// + Cancelled = 1, + + /// + /// 未知错误 + /// + Unknown = 2, + + /// + /// 参数无效 + /// + InvalidArgument = 3, + + /// + /// 超时 + /// + DeadlineExceeded = 4, + + /// + /// 未找到 + /// + NotFound = 5, + + /// + /// 已存在 + /// + AlreadyExists = 6, + + /// + /// 权限不足 + /// + PermissionDenied = 7, + + /// + /// 资源耗尽 + /// + ResourceExhausted = 8, + + /// + /// 前置条件失败 + /// + FailedPrecondition = 9, + + /// + /// 请求中止 + /// + Aborted = 10, + + /// + /// 超出范围 + /// + OutOfRange = 11, + + /// + /// 未实现 + /// + Unimplemented = 12, + + /// + /// 内部错误 + /// + Internal = 13, + + /// + /// 不可用 + /// + Unavailable = 14, + + /// + /// 数据丢失 + /// + DataLoss = 15, + + /// + /// 未认证 + /// + Unauthenticated = 16 + } +} diff --git a/EasyTool.Core/NetCategory/HttpClientBuilder.cs b/EasyTool.Core/NetCategory/HttpClientBuilder.cs new file mode 100644 index 0000000..fa45ebe --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpClientBuilder.cs @@ -0,0 +1,685 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HttpClient 构建器 + /// 提供流畅的 HttpClient 配置接口 + /// + public class HttpClientBuilder + { + private readonly HttpClientHandler _handler; + private readonly List _handlers; + private TimeSpan _timeout = TimeSpan.FromSeconds(100); + private long _maxResponseContentBufferSize = int.MaxValue; + private Dictionary _defaultHeaders = new(); + private Dictionary _defaultRequestHeaders = new(); + private AuthenticationHeaderValue? _authorizationHeader; + private string? _baseAddress; +#pragma warning disable CS0169 // 字段保留供扩展使用 + private TimeSpan? _pipeliningPolicy; +#pragma warning restore CS0169 + private bool _allowAutoRedirect = true; + private int _maxAutomaticRedirections = 50; + private DecompressionMethods _automaticDecompression = DecompressionMethods.None; + private ICredentials? _credentials; + private IWebProxy? _proxy; +#pragma warning disable CS0414 // 字段保留供扩展使用 + private bool _useDefaultCredentials; +#pragma warning restore CS0414 + private TimeSpan? _connectionTimeout; + private int _maxConnectionsPerServer = int.MaxValue; + private int _maxResponseHeadersLength = 64; + + /// + /// 创建 HttpClient 构建器 + /// + public HttpClientBuilder() + { + _handler = new HttpClientHandler(); + _handlers = new List(); + } + + #region 基础配置 + + /// + /// 设置基础地址 + /// + /// 基础 URL + /// HttpClientBuilder + public HttpClientBuilder WithBaseAddress(string baseAddress) + { + _baseAddress = baseAddress; + return this; + } + + /// + /// 设置超时时间 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder WithTimeout(TimeSpan timeout) + { + _timeout = timeout; + return this; + } + + /// + /// 设置最大响应内容缓冲区大小 + /// + /// 大小(字节) + /// HttpClientBuilder + public HttpClientBuilder WithMaxResponseContentBufferSize(long size) + { + _maxResponseContentBufferSize = size; + return this; + } + + #endregion + + #region 请求头 + + /// + /// 添加默认请求头 + /// + /// 头名称 + /// 头值 + /// HttpClientBuilder + public HttpClientBuilder WithDefaultHeader(string name, string value) + { + _defaultHeaders[name] = value; + return this; + } + + /// + /// 批量添加默认请求头 + /// + /// 请求头字典 + /// HttpClientBuilder + public HttpClientBuilder WithDefaultHeaders(Dictionary headers) + { + foreach (var header in headers) + { + _defaultHeaders[header.Key] = header.Value; + } + return this; + } + + /// + /// 设置 Accept 头 + /// + /// 媒体类型 + /// HttpClientBuilder + public HttpClientBuilder WithAccept(string mediaType) + { + _defaultRequestHeaders["Accept"] = mediaType; + return this; + } + + /// + /// 设置 Content-Type 头 + /// + /// 媒体类型 + /// HttpClientBuilder + public HttpClientBuilder WithContentType(string mediaType) + { + _defaultRequestHeaders["Content-Type"] = mediaType; + return this; + } + + /// + /// 设置 User-Agent 头 + /// + /// User-Agent 字符串 + /// HttpClientBuilder + public HttpClientBuilder WithUserAgent(string userAgent) + { + _defaultRequestHeaders["User-Agent"] = userAgent; + return this; + } + + /// + /// 设置 Bearer Token 认证 + /// + /// Token + /// HttpClientBuilder + public HttpClientBuilder WithBearerToken(string token) + { + _authorizationHeader = new AuthenticationHeaderValue("Bearer", token); + return this; + } + + /// + /// 设置 Basic 认证 + /// + /// 用户名 + /// 密码 + /// HttpClientBuilder + public HttpClientBuilder WithBasicAuth(string username, string password) + { + var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")); + _authorizationHeader = new AuthenticationHeaderValue("Basic", credentials); + return this; + } + + /// + /// 设置自定义认证头 + /// + /// 认证方案 + /// 参数 + /// HttpClientBuilder + public HttpClientBuilder WithAuthorization(string scheme, string parameter) + { + _authorizationHeader = new AuthenticationHeaderValue(scheme, parameter); + return this; + } + + #endregion + + #region 代理和安全 + + /// + /// 设置代理 + /// + /// 代理 URL + /// HttpClientBuilder + public HttpClientBuilder WithProxy(string proxyUrl) + { + _proxy = new WebProxy(proxyUrl); + _handler.Proxy = _proxy; + _handler.UseProxy = true; + return this; + } + + /// + /// 设置代理 + /// + /// 代理对象 + /// HttpClientBuilder + public HttpClientBuilder WithProxy(IWebProxy proxy) + { + _proxy = proxy; + _handler.Proxy = proxy; + _handler.UseProxy = true; + return this; + } + + /// + /// 设置代理凭据 + /// + /// 用户名 + /// 密码 + /// HttpClientBuilder + public HttpClientBuilder WithProxyCredentials(string username, string password) + { + if (_proxy != null) + { + _proxy.Credentials = new NetworkCredential(username, password); + } + return this; + } + + /// + /// 忽略 SSL 证书错误 + /// + /// HttpClientBuilder + public HttpClientBuilder IgnoreSslErrors() + { + _handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; + return this; + } + + /// + /// 设置客户端证书 + /// + /// 证书集合 + /// HttpClientBuilder + public HttpClientBuilder WithClientCertificates(System.Security.Cryptography.X509Certificates.X509CertificateCollection certificates) + { + _handler.ClientCertificates.AddRange(certificates); + return this; + } + + #endregion + + #region 重定向和压缩 + + /// + /// 设置是否允许自动重定向 + /// + /// 是否允许 + /// HttpClientBuilder + public HttpClientBuilder WithAutoRedirect(bool allow) + { + _allowAutoRedirect = allow; + _handler.AllowAutoRedirect = allow; + return this; + } + + /// + /// 设置最大自动重定向次数 + /// + /// 次数 + /// HttpClientBuilder + public HttpClientBuilder WithMaxAutomaticRedirections(int count) + { + _maxAutomaticRedirections = count; + _handler.MaxAutomaticRedirections = count; + return this; + } + + /// + /// 启用自动解压缩 + /// + /// 解压缩方法 + /// HttpClientBuilder + public HttpClientBuilder WithAutomaticDecompression(DecompressionMethods methods) + { + _automaticDecompression = methods; + _handler.AutomaticDecompression = methods; + return this; + } + + /// + /// 启用 Gzip 解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithGzipDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.GZip); + } + + /// + /// 启用 Deflate 解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithDeflateDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.Deflate); + } + + /// + /// 启用所有解压缩 + /// + /// HttpClientBuilder + public HttpClientBuilder WithAllDecompression() + { + return WithAutomaticDecompression(DecompressionMethods.GZip | DecompressionMethods.Deflate); + } + + #endregion + + #region 连接配置 + + /// + /// 设置连接超时 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder WithConnectionTimeout(TimeSpan timeout) + { + _connectionTimeout = timeout; + return this; + } + + /// + /// 设置每服务器最大连接数 + /// + /// 连接数 + /// HttpClientBuilder + public HttpClientBuilder WithMaxConnectionsPerServer(int count) + { + _maxConnectionsPerServer = count; + _handler.MaxConnectionsPerServer = count; + return this; + } + + /// + /// 设置最大响应头长度 + /// + /// 长度(KB) + /// HttpClientBuilder + public HttpClientBuilder WithMaxResponseHeadersLength(int length) + { + _maxResponseHeadersLength = length; + _handler.MaxResponseHeadersLength = length; + return this; + } + + /// + /// 使用默认凭据 + /// + /// HttpClientBuilder + public HttpClientBuilder WithDefaultCredentials() + { + _useDefaultCredentials = true; + _handler.UseDefaultCredentials = true; + return this; + } + + /// + /// 设置凭据 + /// + /// 凭据 + /// HttpClientBuilder + public HttpClientBuilder WithCredentials(ICredentials credentials) + { + _credentials = credentials; + _handler.Credentials = credentials; + return this; + } + + #endregion + + #region 中间件 + + /// + /// 添加委托处理器 + /// + /// 处理器 + /// HttpClientBuilder + public HttpClientBuilder AddHandler(DelegatingHandler handler) + { + _handlers.Add(handler); + return this; + } + + /// + /// 添加重试中间件 + /// + /// 重试次数 + /// 重试延迟 + /// HttpClientBuilder + public HttpClientBuilder AddRetry(int retryCount, TimeSpan? retryDelay = null) + { + _handlers.Add(new RetryHandler(retryCount, retryDelay ?? TimeSpan.FromSeconds(1))); + return this; + } + + /// + /// 添加超时中间件 + /// + /// 超时时间 + /// HttpClientBuilder + public HttpClientBuilder AddTimeout(TimeSpan timeout) + { + _handlers.Add(new TimeoutHandler(timeout)); + return this; + } + + /// + /// 添加日志中间件 + /// + /// 日志记录器 + /// HttpClientBuilder + public HttpClientBuilder AddLogging(Action logger) + { + _handlers.Add(new LoggingHandler(logger)); + return this; + } + + #endregion + + #region 构建 + + /// + /// 构建 HttpClient + /// + /// HttpClient 实例 + public HttpClient Build() + { + HttpMessageHandler handler = _handler; + + // 反向添加处理器以形成正确的链 + for (int i = _handlers.Count - 1; i >= 0; i--) + { + _handlers[i].InnerHandler = handler; + handler = _handlers[i]; + } + + var client = new HttpClient(handler); + + // 应用配置 + if (!string.IsNullOrEmpty(_baseAddress)) + { + client.BaseAddress = new Uri(_baseAddress); + } + + client.Timeout = _timeout; + client.MaxResponseContentBufferSize = _maxResponseContentBufferSize; + + // 添加默认头 + foreach (var header in _defaultHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + + foreach (var header in _defaultRequestHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + + // 设置认证头 + if (_authorizationHeader != null) + { + client.DefaultRequestHeaders.Authorization = _authorizationHeader; + } + + return client; + } + + /// + /// 构建并返回一次性使用的 HttpClient(自动释放 Handler) + /// + /// HttpClient 实例 + public HttpClient BuildDisposable() + { + return Build(); + } + + #endregion + } + + #region 中间件处理器 + + /// + /// 重试处理器 + /// + internal class RetryHandler : DelegatingHandler + { + private readonly int _retryCount; + private readonly TimeSpan _retryDelay; + + public RetryHandler(int retryCount, TimeSpan retryDelay) + { + _retryCount = retryCount; + _retryDelay = retryDelay; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + HttpResponseMessage? response = null; + Exception? lastException = null; + + for (int i = 0; i <= _retryCount; i++) + { + try + { + response = await base.SendAsync(request, cancellationToken); + + if (response.IsSuccessStatusCode) + { + return response; + } + + // 服务器错误时重试 + if ((int)response.StatusCode >= 500) + { + lastException = new HttpRequestException($"服务器返回错误: {response.StatusCode}"); + response.Dispose(); + } + else + { + return response; + } + } + catch (Exception ex) + { + lastException = ex; + } + + if (i < _retryCount) + { + await Task.Delay(_retryDelay, cancellationToken); + } + } + + throw lastException ?? new HttpRequestException("重试次数已用尽"); + } + } + + /// + /// 超时处理器 + /// + internal class TimeoutHandler : DelegatingHandler + { + private readonly TimeSpan _timeout; + + public TimeoutHandler(TimeSpan timeout) + { + _timeout = timeout; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(_timeout); + + try + { + return await base.SendAsync(request, cts.Token); + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + throw new TimeoutException($"请求超时: {_timeout}"); + } + } + } + + /// + /// 日志处理器 + /// + internal class LoggingHandler : DelegatingHandler + { + private readonly Action _logger; + + public LoggingHandler(Action logger) + { + _logger = logger; + InnerHandler = new HttpClientHandler(); + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri}"); + + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + + try + { + var response = await base.SendAsync(request, cancellationToken); + stopwatch.Stop(); + + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri} -> {(int)response.StatusCode} ({stopwatch.ElapsedMilliseconds}ms)"); + + return response; + } + catch (Exception ex) + { + stopwatch.Stop(); + _logger($"[{DateTime.UtcNow:HH:mm:ss.fff}] HTTP {request.Method} {request.RequestUri} -> ERROR: {ex.Message} ({stopwatch.ElapsedMilliseconds}ms)"); + throw; + } + } + } + + #endregion + + /// + /// HttpClient 构建工具类 + /// + public static class HttpClientBuilderUtil + { + /// + /// 创建 HttpClient 构建器 + /// + /// HttpClientBuilder + public static HttpClientBuilder Create() + { + return new HttpClientBuilder(); + } + + /// + /// 创建默认 HttpClient + /// + /// HttpClient + public static HttpClient CreateDefault() + { + return new HttpClientBuilder() + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .Build(); + } + + /// + /// 创建 JSON API HttpClient + /// + /// 基础地址 + /// HttpClient + public static HttpClient CreateForJsonApi(string baseAddress) + { + return new HttpClientBuilder() + .WithBaseAddress(baseAddress) + .WithAccept("application/json") + .WithContentType("application/json") + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .Build(); + } + + /// + /// 创建带重试的 HttpClient + /// + /// 重试次数 + /// HttpClient + public static HttpClient CreateWithRetry(int retryCount = 3) + { + return new HttpClientBuilder() + .WithAllDecompression() + .WithTimeout(TimeSpan.FromSeconds(30)) + .AddRetry(retryCount) + .Build(); + } + + /// + /// 创建忽略 SSL 的 HttpClient + /// + /// HttpClient + public static HttpClient CreateIgnoringSsl() + { + return new HttpClientBuilder() + .IgnoreSslErrors() + .WithAllDecompression() + .Build(); + } + } +} diff --git a/EasyTool.Core/NetCategory/HttpClientExtension.cs b/EasyTool.Core/NetCategory/HttpClientExtension.cs index 452804b..59ca88b 100644 --- a/EasyTool.Core/NetCategory/HttpClientExtension.cs +++ b/EasyTool.Core/NetCategory/HttpClientExtension.cs @@ -5,7 +5,7 @@ using System.Threading; using System.Threading.Tasks; -namespace EasyTool.Extension +namespace EasyTool.NetCategory { /// /// 扩展HttpClient中一些缺少的请求方式 @@ -13,7 +13,7 @@ namespace EasyTool.Extension public static class HttpClientExtension { private const string NetErrorMessage = "网络异常"; - + #region 标准的Http请求扩展 /// diff --git a/EasyTool.Core/NetCategory/HttpClientPool.cs b/EasyTool.Core/NetCategory/HttpClientPool.cs new file mode 100644 index 0000000..b9fa77b --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpClientPool.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HttpClient 连接池管理器 + /// 正确管理 HttpClient 的生命周期,避免 socket 耗尽问题 + /// + public sealed class HttpClientPool : IDisposable + { + private static readonly Lazy _default = new(() => new HttpClientPool()); + private readonly ConcurrentDictionary _clients = new(); + private readonly ConcurrentDictionary _handlers = new(); + private readonly object _lock = new(); + private bool _disposed; + + /// + /// 默认 HttpClient 池实例 + /// + public static HttpClientPool Default => _default.Value; + + /// + /// 获取或创建 HttpClient + /// + /// 客户端名称 + /// 配置操作 + /// HttpClient 实例 + public HttpClient GetClient(string name = "default", Action? configure = null) + { + ThrowIfDisposed(); + + return _clients.GetOrAdd(name, key => + { + var options = new HttpClientOptions(); + configure?.Invoke(options); + + var handler = CreateHandler(options); + _handlers[key] = handler; + + var client = new HttpClient(handler, disposeHandler: false); + ConfigureClient(client, options); + + return client; + }); + } + + /// + /// 获取或创建 HttpClient(异步) + /// + /// 客户端名称 + /// 配置操作 + /// HttpClient 实例 + public Task GetClientAsync(string name = "default", Action? configure = null) + { + return Task.FromResult(GetClient(name, configure)); + } + + /// + /// 移除并释放指定的 HttpClient + /// + /// 客户端名称 + public void RemoveClient(string name) + { + ThrowIfDisposed(); + + if (_clients.TryRemove(name, out var client)) + { + client.CancelPendingRequests(); + client.Dispose(); + } + + if (_handlers.TryRemove(name, out var handler)) + { + handler.Dispose(); + } + } + + /// + /// 获取所有客户端名称 + /// + /// 客户端名称集合 + public string[] GetClientNames() + { + return _clients.Keys.ToArray(); + } + + /// + /// 获取客户端数量 + /// + public int ClientCount => _clients.Count; + + /// + /// 设置默认请求头 + /// + /// 客户端名称 + /// 请求头 + public void SetDefaultHeaders(string name, params (string name, string value)[] headers) + { + var client = GetClient(name); + foreach (var (headerName, headerValue) in headers) + { + client.DefaultRequestHeaders.Remove(headerName); + client.DefaultRequestHeaders.TryAddWithoutValidation(headerName, headerValue); + } + } + + /// + /// 清除所有客户端 + /// + public void Clear() + { + ThrowIfDisposed(); + + foreach (var client in _clients.Values) + { + client.CancelPendingRequests(); + client.Dispose(); + } + _clients.Clear(); + + foreach (var handler in _handlers.Values) + { + handler.Dispose(); + } + _handlers.Clear(); + } + + /// + /// 为所有客户端设置代理 + /// + /// 代理地址 + public void SetProxyForAll(string proxyAddress) + { + ThrowIfDisposed(); + + foreach (var kvp in _handlers) + { +#if NET5_0_OR_GREATER + if (kvp.Value is SocketsHttpHandler socketsHandler) + { + if (!string.IsNullOrEmpty(proxyAddress)) + { + socketsHandler.Proxy = new WebProxy(proxyAddress); + socketsHandler.UseProxy = true; + } + else + { + socketsHandler.UseProxy = false; + } + } +#else + if (kvp.Value is HttpClientHandler httpHandler) + { + if (!string.IsNullOrEmpty(proxyAddress)) + { + httpHandler.Proxy = new WebProxy(proxyAddress); + httpHandler.UseProxy = true; + } + else + { + httpHandler.UseProxy = false; + } + } +#endif + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (_disposed) + return; + + lock (_lock) + { + if (_disposed) + return; + + Clear(); + _disposed = true; + } + } + + private HttpMessageHandler CreateHandler(HttpClientOptions options) + { +#if NET5_0_OR_GREATER + // 在 .NET 5+ 中使用 SocketsHttpHandler 以支持连接池设置 + var handler = new SocketsHttpHandler + { + AllowAutoRedirect = options.AllowAutoRedirect, + MaxAutomaticRedirections = options.MaxAutomaticRedirections, + AutomaticDecompression = options.AutomaticDecompression, + UseCookies = options.UseCookies, + UseProxy = options.UseProxy, + MaxConnectionsPerServer = options.MaxConnectionsPerServer, + PooledConnectionLifetime = options.PooledConnectionLifetime, + PooledConnectionIdleTimeout = options.PooledConnectionIdleTimeout + }; + + if (options.Proxy != null) + { + handler.Proxy = options.Proxy; + } + + // SocketsHttpHandler 使用不同的证书验证方式 + if (options.ServerCertificateCustomValidationCallback != null) + { +#if NET10_0_OR_GREATER + // .NET 10 中 RemoteCertificateValidationCallback 需要不同的委托签名 + var callback = options.ServerCertificateCustomValidationCallback; + handler.SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => + { + return callback(null, certificate as X509Certificate2, chain, sslPolicyErrors); + } + }; +#else + handler.SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = options.ServerCertificateCustomValidationCallback + }; +#endif + } + + // SocketsHttpHandler 不直接支持 ClientCertificates,需要通过 SslOptions 配置 +#else + // 在 netstandard2.1 中使用 HttpClientHandler(不支持连接池设置) + var handler = new HttpClientHandler + { + AllowAutoRedirect = options.AllowAutoRedirect, + MaxAutomaticRedirections = options.MaxAutomaticRedirections, + AutomaticDecompression = options.AutomaticDecompression, + UseCookies = options.UseCookies, + UseProxy = options.UseProxy, + MaxConnectionsPerServer = options.MaxConnectionsPerServer + }; + + if (options.Proxy != null) + { + handler.Proxy = options.Proxy; + } + + if (options.ServerCertificateCustomValidationCallback != null) + { + handler.ServerCertificateCustomValidationCallback = options.ServerCertificateCustomValidationCallback; + } + + if (options.ClientCertificates?.Count > 0) + { + handler.ClientCertificates.AddRange(options.ClientCertificates); + } +#endif + + return handler; + } + + private void ConfigureClient(HttpClient client, HttpClientOptions options) + { + client.Timeout = options.Timeout; + client.BaseAddress = options.BaseAddress; + + if (options.DefaultRequestHeaders != null) + { + foreach (var header in options.DefaultRequestHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value); + } + } + + if (!string.IsNullOrEmpty(options.UserAgent)) + { + client.DefaultRequestHeaders.UserAgent.TryParseAdd(options.UserAgent); + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(HttpClientPool)); + } + } + } + + /// + /// HttpClient 配置选项 + /// + public class HttpClientOptions + { + /// + /// 基础地址 + /// + public Uri? BaseAddress { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(100); + + /// + /// 是否允许自动重定向 + /// + public bool AllowAutoRedirect { get; set; } = true; + + /// + /// 最大自动重定向次数 + /// + public int MaxAutomaticRedirections { get; set; } = 50; + + /// + /// 自动解压缩方式 + /// + public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip | DecompressionMethods.Deflate; + + /// + /// 是否使用 Cookie + /// + public bool UseCookies { get; set; } = false; + + /// + /// 是否使用代理 + /// + public bool UseProxy { get; set; } = false; + + /// + /// 代理设置 + /// + public IWebProxy? Proxy { get; set; } + + /// + /// 每个服务器最大连接数 + /// + public int MaxConnectionsPerServer { get; set; } = int.MaxValue; + + /// + /// 连接池连接生存期 + /// + public TimeSpan PooledConnectionLifetime { get; set; } = TimeSpan.FromMinutes(15); + + /// + /// 连接池空闲超时 + /// + public TimeSpan PooledConnectionIdleTimeout { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 默认请求头 + /// + public Dictionary? DefaultRequestHeaders { get; set; } + + /// + /// User-Agent + /// + public string? UserAgent { get; set; } + + /// + /// 服务器证书验证回调 + /// + public Func? + ServerCertificateCustomValidationCallback { get; set; } + + /// + /// 客户端证书集合 + /// + public X509CertificateCollection? ClientCertificates { get; set; } + } + + /// + /// HttpClient 扩展方法 + /// + public static class HttpClientPoolExtensions + { + /// + /// 创建配置好的 HttpClient + /// + public static HttpClient CreateClient(Action? configure = null) + { + return HttpClientPool.Default.GetClient(Guid.NewGuid().ToString(), configure); + } + + /// + /// 创建用于 JSON API 的 HttpClient + /// + public static HttpClient CreateJsonClient(string? baseUrl = null) + { + return HttpClientPool.Default.GetClient("json_" + (baseUrl ?? "default"), options => + { + if (!string.IsNullOrEmpty(baseUrl)) + { + options.BaseAddress = new Uri(baseUrl); + } + options.DefaultRequestHeaders = new Dictionary + { + ["Accept"] = "application/json", + ["Content-Type"] = "application/json" + }; + }); + } + + /// + /// 创建用于下载大文件的 HttpClient + /// + public static HttpClient CreateDownloadClient() + { + return HttpClientPool.Default.GetClient("download", options => + { + options.Timeout = TimeSpan.FromMinutes(30); + options.AutomaticDecompression = DecompressionMethods.None; + }); + } + + /// + /// 创建允许无效证书的 HttpClient(仅用于开发环境) + /// + public static HttpClient CreateInsecureClient() + { + return HttpClientPool.Default.GetClient("insecure_" + Guid.NewGuid(), options => + { + options.ServerCertificateCustomValidationCallback = (_, _, _, _) => true; + }); + } + + /// + /// 创建带代理的 HttpClient + /// + public static HttpClient CreateProxyClient(string proxyAddress) + { + return HttpClientPool.Default.GetClient("proxy_" + proxyAddress.GetHashCode(), options => + { + options.UseProxy = true; + options.Proxy = new WebProxy(proxyAddress); + }); + } + } +} diff --git a/EasyTool.Core/NetCategory/HttpUtil.cs b/EasyTool.Core/NetCategory/HttpUtil.cs new file mode 100644 index 0000000..641edbb --- /dev/null +++ b/EasyTool.Core/NetCategory/HttpUtil.cs @@ -0,0 +1,381 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// HTTP工具类 + /// 提供HTTP请求的便捷操作 + /// + public static class HttpUtil + { + private static readonly HttpClient _sharedClient = new(); + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + /// + /// 获取共享HttpClient + /// + public static HttpClient SharedClient => _sharedClient; + + /// + /// 创建HttpClient + /// + /// 基础地址 + /// 超时时间 + /// HttpClient实例 + public static HttpClient CreateClient(string? baseAddress = null, TimeSpan? timeout = null) + { + var client = new HttpClient(); + + if (!string.IsNullOrEmpty(baseAddress)) + { + client.BaseAddress = new Uri(baseAddress); + } + + if (timeout.HasValue) + { + client.Timeout = timeout.Value; + } + + return client; + } + + #region GET请求 + + /// + /// GET请求 + /// + public static async Task GetStringAsync(string url, CancellationToken cancellationToken = default) + { +#if NET5_0_OR_GREATER + return await _sharedClient.GetStringAsync(url, cancellationToken); +#else + return await _sharedClient.GetStringAsync(url); +#endif + } + + /// + /// GET请求 + /// + public static async Task GetStringAsync(string url, Dictionary? headers, CancellationToken cancellationToken = default) + { + using var request = new HttpRequestMessage(HttpMethod.Get, url); + + if (headers != null) + { + foreach (var header in headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + } + + using var response = await _sharedClient.SendAsync(request, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + /// + /// GET请求(返回字节数组) + /// + public static async Task GetBytesAsync(string url, CancellationToken cancellationToken = default) + { +#if NET5_0_OR_GREATER + return await _sharedClient.GetByteArrayAsync(url, cancellationToken); +#else + return await _sharedClient.GetByteArrayAsync(url); +#endif + } + + /// + /// GET请求(返回流) + /// + public static async Task GetStreamAsync(string url, CancellationToken cancellationToken = default) + { +#if NET5_0_OR_GREATER + return await _sharedClient.GetStreamAsync(url, cancellationToken); +#else + return await _sharedClient.GetStreamAsync(url); +#endif + } + + /// + /// GET请求(反序列化为对象) + /// + public static async Task GetJsonAsync(string url, CancellationToken cancellationToken = default) + { + var json = await GetStringAsync(url, cancellationToken); + return JsonSerializer.Deserialize(json, _jsonOptions); + } + + #endregion + + #region POST请求 + + /// + /// POST请求(字符串内容) + /// + public static async Task PostStringAsync(string url, string content, string? contentType = null, CancellationToken cancellationToken = default) + { + using var httpContent = new StringContent(content, Encoding.UTF8, contentType ?? "text/plain"); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + /// + /// POST请求(JSON内容) + /// + public static async Task PostJsonAsync(string url, T data, CancellationToken cancellationToken = default) + { + var json = JsonSerializer.Serialize(data, _jsonOptions); + using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + /// + /// POST请求(JSON内容,返回反序列化对象) + /// + public static async Task PostJsonAsync(string url, TRequest data, CancellationToken cancellationToken = default) + { + var json = await PostJsonAsync(url, data, cancellationToken); + return JsonSerializer.Deserialize(json, _jsonOptions); + } + + /// + /// POST请求(表单数据) + /// + public static async Task PostFormAsync(string url, Dictionary formData, CancellationToken cancellationToken = default) + { + using var httpContent = new FormUrlEncodedContent(formData); + using var response = await _sharedClient.PostAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + #endregion + + #region PUT请求 + + /// + /// PUT请求 + /// + public static async Task PutStringAsync(string url, string content, string? contentType = null, CancellationToken cancellationToken = default) + { + using var httpContent = new StringContent(content, Encoding.UTF8, contentType ?? "text/plain"); + using var response = await _sharedClient.PutAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + /// + /// PUT请求(JSON内容) + /// + public static async Task PutJsonAsync(string url, T data, CancellationToken cancellationToken = default) + { + var json = JsonSerializer.Serialize(data, _jsonOptions); + using var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); + using var response = await _sharedClient.PutAsync(url, httpContent, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + #endregion + + #region DELETE请求 + + /// + /// DELETE请求 + /// + public static async Task DeleteAsync(string url, CancellationToken cancellationToken = default) + { + using var response = await _sharedClient.DeleteAsync(url, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + #endregion + + #region 通用请求 + + /// + /// 发送请求 + /// + public static async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) + { + return await _sharedClient.SendAsync(request, cancellationToken); + } + + /// + /// 发送请求并返回字符串 + /// + public static async Task SendAsStringAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) + { + using var response = await _sharedClient.SendAsync(request, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + /// + /// 下载文件 + /// + public static async Task DownloadFileAsync(string url, string filePath, CancellationToken cancellationToken = default) + { + using var response = await _sharedClient.GetAsync(url, cancellationToken); + response.EnsureSuccessStatusCode(); + + var directory = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + using var fileStream = File.Create(filePath); +#if NET5_0_OR_GREATER + await response.Content.CopyToAsync(fileStream, cancellationToken); +#else + await response.Content.CopyToAsync(fileStream); +#endif + } + + /// + /// 上传文件 + /// + public static async Task UploadFileAsync(string url, string filePath, string? formFieldName = null, CancellationToken cancellationToken = default) + { + using var fileStream = File.OpenRead(filePath); + using var streamContent = new StreamContent(fileStream); + using var formData = new MultipartFormDataContent(); + + var fieldName = formFieldName ?? "file"; + var fileName = Path.GetFileName(filePath); + + formData.Add(streamContent, fieldName, fileName); + + using var response = await _sharedClient.PostAsync(url, formData, cancellationToken); + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + return await response.Content.ReadAsStringAsync(cancellationToken); +#else + return await response.Content.ReadAsStringAsync(); +#endif + } + + #endregion + + #region 辅助方法 + + /// + /// 构建查询字符串 + /// + public static string BuildQueryString(Dictionary parameters) + { + if (parameters == null || parameters.Count == 0) + return string.Empty; + + var sb = new StringBuilder(); + foreach (var kvp in parameters) + { + if (kvp.Value != null) + { + if (sb.Length > 0) + sb.Append('&'); + sb.Append(Uri.EscapeDataString(kvp.Key)); + sb.Append('='); + sb.Append(Uri.EscapeDataString(kvp.Value)); + } + } + + return sb.ToString(); + } + + /// + /// 解析查询字符串 + /// + public static Dictionary ParseQueryString(string query) + { + var result = new Dictionary(); + + if (string.IsNullOrEmpty(query)) + return result; + + if (query.StartsWith("?")) + query = query.Substring(1); + + foreach (var pair in query.Split('&')) + { + var index = pair.IndexOf('='); + if (index > 0) + { + var key = Uri.UnescapeDataString(pair.Substring(0, index)); + var value = Uri.UnescapeDataString(pair.Substring(index + 1)); + result[key] = value; + } + else if (pair.Length > 0) + { + result[Uri.UnescapeDataString(pair)] = string.Empty; + } + } + + return result; + } + + /// + /// 组合URL和查询参数 + /// + public static string CombineUrl(string baseUrl, Dictionary parameters) + { + var queryString = BuildQueryString(parameters); + if (string.IsNullOrEmpty(queryString)) + return baseUrl; + + var separator = baseUrl.Contains('?') ? "&" : "?"; + return baseUrl + separator + queryString; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/IpUtil.cs b/EasyTool.Core/NetCategory/IpUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/IpUtil.cs rename to EasyTool.Core/NetCategory/IpUtil.cs index 577185e..34adbe8 100644 --- a/EasyTool.Core/ToolCategory/IpUtil.cs +++ b/EasyTool.Core/NetCategory/IpUtil.cs @@ -2,12 +2,12 @@ using System.Net; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.NetCategory { /// /// IP地址工具类 /// - public class IpUtil + public static class IpUtil { /// /// 判断是否是ipv4格式 diff --git a/EasyTool.Core/NetCategory/MailUtil.cs b/EasyTool.Core/NetCategory/MailUtil.cs new file mode 100644 index 0000000..40c6800 --- /dev/null +++ b/EasyTool.Core/NetCategory/MailUtil.cs @@ -0,0 +1,593 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 邮件发送工具类 + /// 支持 SMTP 协议发送邮件,包括附件、HTML 正文、抄送等功能 + /// + public static class MailUtil + { + #region 快捷发送方法 + + /// + /// 发送简单文本邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + public static void Send(SmtpConfig config, string to, string subject, string body) + { + Send(config, new[] { to }, subject, body); + } + + /// + /// 发送简单文本邮件 + /// + /// SMTP 配置 + /// 收件人列表 + /// 主题 + /// 正文 + public static void Send(SmtpConfig config, IEnumerable to, string subject, string body) + { + Send(config, new MailMessageOptions + { + To = to.ToList(), + Subject = subject, + Body = body, + IsBodyHtml = false + }); + } + + /// + /// 发送 HTML 邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// HTML 正文 + public static void SendHtml(SmtpConfig config, string to, string subject, string htmlBody) + { + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = htmlBody, + IsBodyHtml = true + }); + } + + /// + /// 发送带附件的邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + /// 附件文件路径列表 + public static void SendWithAttachments(SmtpConfig config, string to, string subject, string body, params string[] attachments) + { + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = false, + Attachments = attachments.ToList() + }); + } + + #endregion + + #region 完整发送方法 + + /// + /// 发送邮件(完整选项) + /// + /// SMTP 配置 + /// 邮件选项 + public static void Send(SmtpConfig config, MailMessageOptions options) + { + using var message = CreateMessage(config, options); + using var client = CreateClient(config); + client.Send(message); + } + + /// + /// 异步发送邮件 + /// + /// SMTP 配置 + /// 邮件选项 + /// Task + public static async Task SendAsync(SmtpConfig config, MailMessageOptions options) + { + using var message = CreateMessage(config, options); + using var client = CreateClient(config); + await client.SendMailAsync(message); + } + + /// + /// 批量发送邮件 + /// + /// SMTP 配置 + /// 邮件选项列表 + /// 是否并行发送 + /// 发送结果列表 + public static List SendBatch(SmtpConfig config, List messages, bool parallel = false) + { + var results = new List(); + + if (parallel) + { + var tasks = messages.Select(msg => Task.Run(() => + { + try + { + Send(config, msg); + return new SendResult { Success = true, Recipients = msg.To }; + } + catch (Exception ex) + { + return new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }; + } + })).ToArray(); + + Task.WaitAll(tasks); + results = tasks.Select(t => t.Result).ToList(); + } + else + { + foreach (var msg in messages) + { + try + { + Send(config, msg); + results.Add(new SendResult { Success = true, Recipients = msg.To }); + } + catch (Exception ex) + { + results.Add(new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }); + } + } + } + + return results; + } + + /// + /// 批量异步发送邮件 + /// + /// SMTP 配置 + /// 邮件选项列表 + /// 最大并行度 + /// 发送结果列表 + public static async Task> SendBatchAsync(SmtpConfig config, List messages, int maxDegreeOfParallelism = 5) + { + var results = new List(); + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + + var tasks = messages.Select(async msg => + { + await semaphore.WaitAsync(); + try + { + await SendAsync(config, msg); + return new SendResult { Success = true, Recipients = msg.To }; + } + catch (Exception ex) + { + return new SendResult { Success = false, Recipients = msg.To, Error = ex.Message }; + } + finally + { + semaphore.Release(); + } + }); + + results = (await Task.WhenAll(tasks)).ToList(); + return results; + } + + #endregion + + #region 模板发送 + + /// + /// 使用模板发送邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板内容(使用 {key} 占位符) + /// 参数字典 + /// 是否为 HTML 格式 + public static void SendTemplate(SmtpConfig config, string to, string subject, string template, Dictionary parameters, bool isHtml = true) + { + string body = RenderTemplate(template, parameters); + Send(config, new MailMessageOptions + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml + }); + } + + /// + /// 使用模板文件发送邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板文件路径 + /// 参数字典 + public static void SendTemplateFile(SmtpConfig config, string to, string subject, string templatePath, Dictionary parameters) + { + if (!File.Exists(templatePath)) + throw new FileNotFoundException("模板文件不存在", templatePath); + + string template = File.ReadAllText(templatePath, Encoding.UTF8); + bool isHtml = templatePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase) || + templatePath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase); + + SendTemplate(config, to, subject, template, parameters, isHtml); + } + + #endregion + + #region 私有方法 + + private static SmtpClient CreateClient(SmtpConfig config) + { + var client = new SmtpClient(config.Host, config.Port) + { + EnableSsl = config.EnableSsl, + Timeout = config.Timeout ?? 30000 + }; + + if (!string.IsNullOrEmpty(config.UserName) && !string.IsNullOrEmpty(config.Password)) + { + client.Credentials = new NetworkCredential(config.UserName, config.Password); + } + + return client; + } + + private static MailMessage CreateMessage(SmtpConfig config, MailMessageOptions options) + { + var message = new MailMessage + { + From = new MailAddress(options.From ?? config.DefaultFrom), + Subject = options.Subject, + Body = options.Body, + IsBodyHtml = options.IsBodyHtml, + BodyEncoding = Encoding.UTF8, + SubjectEncoding = Encoding.UTF8, + Priority = options.Priority + }; + + // 添加收件人 + if (options.To != null) + { + foreach (var to in options.To) + { + if (!string.IsNullOrEmpty(to)) + message.To.Add(to); + } + } + + // 添加抄送 + if (options.Cc != null) + { + foreach (var cc in options.Cc) + { + if (!string.IsNullOrEmpty(cc)) + message.CC.Add(cc); + } + } + + // 添加密送 + if (options.Bcc != null) + { + foreach (var bcc in options.Bcc) + { + if (!string.IsNullOrEmpty(bcc)) + message.Bcc.Add(bcc); + } + } + + // 添加回复地址 + if (!string.IsNullOrEmpty(options.ReplyTo)) + { + message.ReplyToList.Add(new MailAddress(options.ReplyTo)); + } + + // 添加附件 + if (options.Attachments != null) + { + foreach (var filePath in options.Attachments) + { + if (File.Exists(filePath)) + { + var attachment = new Attachment(filePath); + attachment.ContentDisposition!.CreationDate = File.GetCreationTime(filePath); + attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(filePath); + attachment.ContentDisposition.ReadDate = File.GetLastAccessTime(filePath); + message.Attachments.Add(attachment); + } + } + } + + // 添加内嵌资源(用于 HTML 邮件中的图片) + if (options.EmbeddedResources != null) + { + foreach (var resource in options.EmbeddedResources) + { + if (File.Exists(resource.Value)) + { + var attachment = new LinkedResource(resource.Value) + { + ContentId = resource.Key + }; + var htmlView = AlternateView.CreateAlternateViewFromString(options.Body, Encoding.UTF8, MediaTypeNames.Text.Html); + htmlView.LinkedResources.Add(attachment); + message.AlternateViews.Add(htmlView); + } + } + } + + // 添加自定义头部 + if (options.Headers != null) + { + foreach (var header in options.Headers) + { + message.Headers.Add(header.Key, header.Value); + } + } + + return message; + } + + private static string RenderTemplate(string template, Dictionary parameters) + { + if (string.IsNullOrEmpty(template) || parameters == null) + return template ?? string.Empty; + + string result = template; + foreach (var kvp in parameters) + { + string placeholder = "{" + kvp.Key + "}"; + string value = kvp.Value?.ToString() ?? string.Empty; + result = result.Replace(placeholder, value); + } + + return result; + } + + #endregion + } + + #region 配置和选项类 + + /// + /// SMTP 配置 + /// + public class SmtpConfig + { + /// + /// SMTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// SMTP 服务器端口(默认25) + /// + public int Port { get; set; } = 25; + + /// + /// 是否启用 SSL + /// + public bool EnableSsl { get; set; } + + /// + /// 用户名 + /// + public string? UserName { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 默认发件人地址 + /// + public string? DefaultFrom { get; set; } + + /// + /// 超时时间(毫秒) + /// + public int? Timeout { get; set; } + + /// + /// 创建 QQ 邮箱配置 + /// + /// QQ 邮箱 + /// 授权码 + /// SMTP 配置 + public static SmtpConfig ForQQ(string userName, string authCode) + { + return new SmtpConfig + { + Host = "smtp.qq.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = authCode, + DefaultFrom = userName + }; + } + + /// + /// 创建 163 邮箱配置 + /// + /// 163 邮箱 + /// 授权码 + /// SMTP 配置 + public static SmtpConfig For163(string userName, string authCode) + { + return new SmtpConfig + { + Host = "smtp.163.com", + Port = 465, + EnableSsl = true, + UserName = userName, + Password = authCode, + DefaultFrom = userName + }; + } + + /// + /// 创建 Gmail 配置 + /// + /// Gmail 地址 + /// 应用专用密码 + /// SMTP 配置 + public static SmtpConfig ForGmail(string userName, string appPassword) + { + return new SmtpConfig + { + Host = "smtp.gmail.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = appPassword, + DefaultFrom = userName + }; + } + + /// + /// 创建 Outlook 配置 + /// + /// Outlook 地址 + /// 密码 + /// SMTP 配置 + public static SmtpConfig ForOutlook(string userName, string password) + { + return new SmtpConfig + { + Host = "smtp-mail.outlook.com", + Port = 587, + EnableSsl = true, + UserName = userName, + Password = password, + DefaultFrom = userName + }; + } + } + + /// + /// 邮件消息选项 + /// + public class MailMessageOptions + { + /// + /// 发件人(可选,使用配置中的默认值) + /// + public string? From { get; set; } + + /// + /// 收件人列表 + /// + public List? To { get; set; } + + /// + /// 抄送列表 + /// + public List? Cc { get; set; } + + /// + /// 密送列表 + /// + public List? Bcc { get; set; } + + /// + /// 回复地址 + /// + public string? ReplyTo { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 正文 + /// + public string Body { get; set; } = string.Empty; + + /// + /// 是否为 HTML 正文 + /// + public bool IsBodyHtml { get; set; } + + /// + /// 附件文件路径列表 + /// + public List? Attachments { get; set; } + + /// + /// 内嵌资源(ContentId -> 文件路径) + /// + public Dictionary? EmbeddedResources { get; set; } + + /// + /// 自定义邮件头 + /// + public Dictionary? Headers { get; set; } + + /// + /// 邮件优先级 + /// + public MailPriority Priority { get; set; } = MailPriority.Normal; + } + + /// + /// 发送结果 + /// + public class SendResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 收件人列表 + /// + public List? Recipients { get; set; } + + /// + /// 错误信息 + /// + public string? Error { get; set; } + + public override string ToString() + { + return Success + ? $"成功发送至: {string.Join(", ", Recipients ?? new List())}" + : $"发送失败: {Error}"; + } + } + + #endregion +} diff --git a/EasyTool.Core/NetCategory/NetUtil.cs b/EasyTool.Core/NetCategory/NetUtil.cs deleted file mode 100644 index 3f41d78..0000000 --- a/EasyTool.Core/NetCategory/NetUtil.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.NetworkInformation; -using System.Net.Sockets; -using System.Net; -using System.Text; - -namespace EasyTool -{ - /// - /// 网络工具 - /// - public class NetUtil - { - // Ping a host and return true if the ping was successful - // 对指定主机进行Ping测试,返回是否成功 - public static bool Ping(string host) - { - try - { - Ping pingSender = new Ping(); - PingReply reply = pingSender.Send(host); - - if (reply.Status == IPStatus.Success) - { - return true; - } - else - { - return false; - } - } - catch - { - return false; - } - } - - // Resolve the IP address of a host - // 获取指定主机的IP地址 - public static IPAddress GetIpAddress(string host) - { - try - { - IPHostEntry hostEntry = Dns.GetHostEntry(host); - - foreach (IPAddress address in hostEntry.AddressList) - { - // 返回IPv4地址 - if (address.AddressFamily == AddressFamily.InterNetwork) - { - return address; - } - } - - return null; - } - catch - { - return null; - } - } - - // Check if a port is open on a given IP address - // 检查给定IP地址上的端口是否开放 - public static bool IsPortOpen(string host, int port) - { - try - { - // 获取IP地址 - IPAddress ipAddress = GetIpAddress(host); - - if (ipAddress == null) - { - return false; - } - - // 创建套接字,连接端口 - IPEndPoint endpoint = new IPEndPoint(ipAddress, port); - using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) - { - socket.Connect(endpoint); - return true; - } - } - catch - { - return false; - } - } - - // Send an HTTP GET request and return the response - // 发送HTTP GET请求并返回响应 - [Obsolete("建议使用HttpClient替代此方法")] - public static string HttpGet(string url) - { - try - { - WebClient client = new WebClient(); - return client.DownloadString(url); - } - catch - { - return null; - } - } - - // Send an HTTP POST request and return the response - // 发送HTTP POST请求并返回响应 - [Obsolete("建议使用HttpClient替代此方法")] - public static string HttpPost(string url, string data) - { - try - { - WebClient client = new WebClient(); - // 设置请求头的内容类型 - client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; - return client.UploadString(url, data); - } - catch - { - return null; - } - } - } -} diff --git a/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs b/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs new file mode 100644 index 0000000..5090574 --- /dev/null +++ b/EasyTool.Core/NetCategory/NetworkInterfaceUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; + +namespace EasyTool.NetCategory +{ + /// + /// 网络接口工具类 + /// + public static class NetworkInterfaceUtil + { + /// + /// 获取所有网络接口 + /// + public static List GetAllInterfaces() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + var info = new NetworkInterfaceInfo + { + Id = ni.Id, + Name = ni.Name, + Description = ni.Description, + InterfaceType = ni.NetworkInterfaceType.ToString(), + OperationalStatus = ni.OperationalStatus.ToString(), + Speed = ni.Speed, + IsReceiveOnly = ni.IsReceiveOnly, + SupportsMulticast = ni.SupportsMulticast + }; + + // 获取MAC地址 + var mac = ni.GetPhysicalAddress(); + info.MacAddress = mac.ToString(); + + // 获取IP地址 + var ipProps = ni.GetIPProperties(); + foreach (var addr in ipProps.UnicastAddresses) + { + if (addr.Address.AddressFamily == AddressFamily.InterNetwork) + { + info.IPv4Addresses.Add(new IPAddressInfo + { + Address = addr.Address.ToString(), + SubnetMask = addr.IPv4Mask?.ToString() ?? "" + }); + } + else if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6) + { + info.IPv6Addresses.Add(addr.Address.ToString()); + } + } + + // 获取网关 + foreach (var gateway in ipProps.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + info.Gateway = gateway.Address.ToString(); + break; + } + } + + // 获取DNS服务器 + foreach (var dns in ipProps.DnsAddresses) + { + info.DnsServers.Add(dns.ToString()); + } + + // 获取DHCP服务器 + foreach (var dhcp in ipProps.DhcpServerAddresses) + { + info.DhcpServers.Add(dhcp.ToString()); + } + + // 获取统计信息 + try + { + var stats = ni.GetIPv4Statistics(); + info.BytesReceived = stats.BytesReceived; + info.BytesSent = stats.BytesSent; + info.UnicastPacketsReceived = stats.UnicastPacketsReceived; + info.UnicastPacketsSent = stats.UnicastPacketsSent; + info.NonUnicastPacketsReceived = stats.NonUnicastPacketsReceived; + info.NonUnicastPacketsSent = stats.NonUnicastPacketsSent; + } + catch + { + } + + result.Add(info); + } + + return result; + } + + /// + /// 获取活动网络接口 + /// + public static List GetActiveInterfaces() + { + return GetAllInterfaces() + .Where(i => i.OperationalStatus == "Up") + .ToList(); + } + + /// + /// 获取本机IP地址 + /// + public static string GetLocalIPAddress() + { + using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0); + socket.Connect("8.8.8.8", 65530); + var endPoint = socket.LocalEndPoint as IPEndPoint; + return endPoint?.Address.ToString() ?? ""; + } + + /// + /// 获取本机所有IPv4地址 + /// + public static List GetAllLocalIPv4Addresses() + { + var result = new List(); + var host = Dns.GetHostEntry(Dns.GetHostName()); + + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == AddressFamily.InterNetwork) + { + result.Add(ip.ToString()); + } + } + + return result; + } + + /// + /// 获取本机MAC地址 + /// + public static string GetMacAddress() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up) + .OrderByDescending(ni => ni.Speed); + + foreach (var ni in interfaces) + { + var mac = ni.GetPhysicalAddress(); + if (!string.IsNullOrEmpty(mac.ToString())) + { + return mac.ToString(); + } + } + + return string.Empty; + } + + /// + /// 获取默认网关 + /// + public static string GetDefaultGateway() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + var ipProps = ni.GetIPProperties(); + foreach (var gateway in ipProps.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + return gateway.Address.ToString(); + } + } + } + + return string.Empty; + } + + /// + /// 获取DNS服务器 + /// + public static List GetDnsServers() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + var ipProps = ni.GetIPProperties(); + foreach (var dns in ipProps.DnsAddresses) + { + if (!result.Contains(dns.ToString())) + { + result.Add(dns.ToString()); + } + } + } + + return result; + } + + /// + /// 检查是否联网 + /// + public static bool IsNetworkAvailable() + { + return NetworkInterface.GetIsNetworkAvailable(); + } + + /// + /// 获取网络流量统计 + /// + public static NetworkTrafficStats GetNetworkTrafficStats() + { + var stats = new NetworkTrafficStats(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.OperationalStatus == OperationalStatus.Up); + + foreach (var ni in interfaces) + { + try + { + var ipv4Stats = ni.GetIPv4Statistics(); + stats.TotalBytesReceived += ipv4Stats.BytesReceived; + stats.TotalBytesSent += ipv4Stats.BytesSent; + stats.TotalPacketsReceived += ipv4Stats.UnicastPacketsReceived + ipv4Stats.NonUnicastPacketsReceived; + stats.TotalPacketsSent += ipv4Stats.UnicastPacketsSent + ipv4Stats.NonUnicastPacketsSent; + } + catch + { + } + } + + return stats; + } + + /// + /// 刷新DNS缓存 + /// + public static bool FlushDnsCache() + { + try + { + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "ipconfig", + Arguments = "/flushdns", + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch + { + return false; + } + } + + /// + /// 释放并续订DHCP + /// + public static bool RenewDhcp() + { + try + { + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "ipconfig", + Arguments = "/renew", + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch + { + return false; + } + } + + /// + /// 获取主机名 + /// + public static string GetHostName() + { + return Dns.GetHostName(); + } + + /// + /// 根据主机名获取IP地址 + /// + public static string[] GetHostAddresses(string hostName) + { + try + { + var addresses = Dns.GetHostAddresses(hostName); + return addresses.Select(a => a.ToString()).ToArray(); + } + catch + { + return Array.Empty(); + } + } + } + + /// + /// 网络接口信息 + /// + public class NetworkInterfaceInfo + { + public string Id { get; set; } = ""; + public string Name { get; set; } = ""; + public string Description { get; set; } = ""; + public string InterfaceType { get; set; } = ""; + public string OperationalStatus { get; set; } = ""; + public string MacAddress { get; set; } = ""; + public long Speed { get; set; } + public bool IsReceiveOnly { get; set; } + public bool SupportsMulticast { get; set; } + public List IPv4Addresses { get; set; } = new(); + public List IPv6Addresses { get; set; } = new(); + public string Gateway { get; set; } = ""; + public List DnsServers { get; set; } = new(); + public List DhcpServers { get; set; } = new(); + public long BytesReceived { get; set; } + public long BytesSent { get; set; } + public long UnicastPacketsReceived { get; set; } + public long UnicastPacketsSent { get; set; } + public long NonUnicastPacketsReceived { get; set; } + public long NonUnicastPacketsSent { get; set; } + + public double SpeedMbps => Speed / 1_000_000.0; + public double SpeedGbps => Speed / 1_000_000_000.0; + } + + /// + /// IP地址信息 + /// + public class IPAddressInfo + { + public string Address { get; set; } = ""; + public string SubnetMask { get; set; } = ""; + } + + /// + /// 网络流量统计 + /// + public class NetworkTrafficStats + { + public long TotalBytesReceived { get; set; } + public long TotalBytesSent { get; set; } + public long TotalPacketsReceived { get; set; } + public long TotalPacketsSent { get; set; } + + public double TotalGBReceived => TotalBytesReceived / (1024.0 * 1024 * 1024); + public double TotalGBSent => TotalBytesSent / (1024.0 * 1024 * 1024); + } +} diff --git a/EasyTool.Core/NetCategory/PingUtil.cs b/EasyTool.Core/NetCategory/PingUtil.cs new file mode 100644 index 0000000..e31406f --- /dev/null +++ b/EasyTool.Core/NetCategory/PingUtil.cs @@ -0,0 +1,523 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Ping结果 + /// + public class PingResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 目标地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 解析后的IP地址 + /// + public IPAddress? IpAddress { get; set; } + + /// + /// 响应时间(毫秒) + /// + public long RoundtripTime { get; set; } + + /// + /// TTL(生存时间) + /// + public int Ttl { get; set; } + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } + + /// + /// IP状态 + /// + public IPStatus Status { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 时间戳 + /// + public DateTime Timestamp { get; set; } = DateTime.UtcNow; + + public override string ToString() + { + return Success + ? $"回复来自 {IpAddress}: 字节={BufferSize} 时间={RoundtripTime}ms TTL={Ttl}" + : $"请求超时: {Status}"; + } + } + + /// + /// Ping统计信息 + /// + public class PingStatistics + { + /// + /// 目标地址 + /// + public string Address { get; set; } = string.Empty; + + /// + /// 总发包数 + /// + public int PacketsSent { get; set; } + + /// + /// 收包数 + /// + public int PacketsReceived { get; set; } + + /// + /// 丢包数 + /// + public int PacketsLost => PacketsSent - PacketsReceived; + + /// + /// 丢包率 + /// + public double LossRate => PacketsSent > 0 ? (double)PacketsLost / PacketsSent : 0; + + /// + /// 最小延迟(毫秒) + /// + public long MinRoundtripTime { get; set; } + + /// + /// 最大延迟(毫秒) + /// + public long MaxRoundtripTime { get; set; } + + /// + /// 平均延迟(毫秒) + /// + public double AverageRoundtripTime { get; set; } + + /// + /// 结果列表 + /// + public List Results { get; set; } = new(); + + public override string ToString() + { + return $"Ping {Address}: 已发送={PacketsSent}, 已接收={PacketsReceived}, 丢失={PacketsLost}({LossRate:P0}丢失), " + + $"延迟: 最小={MinRoundtripTime}ms, 最大={MaxRoundtripTime}ms, 平均={AverageRoundtripTime:F2}ms"; + } + } + + /// + /// Ping配置 + /// + public class PingOptions + { + /// + /// 超时时间(毫秒) + /// + public int Timeout { get; set; } = 5000; + + /// + /// 缓冲区大小 + /// + public int BufferSize { get; set; } = 32; + + /// + /// TTL + /// + public int Ttl { get; set; } = 128; + + /// + /// 是否允许分片 + /// + public bool DontFragment { get; set; } = true; + + /// + /// 发送次数 + /// + public int Count { get; set; } = 4; + + /// + /// 发送间隔(毫秒) + /// + public int Interval { get; set; } = 1000; + } + + /// + /// Ping工具类 + /// + public static class PingUtil + { + /// + /// Ping指定主机 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// Ping结果 + public static PingResult Ping(string hostNameOrAddress, int timeout = 5000) + { + return PingAsync(hostNameOrAddress, timeout).GetAwaiter().GetResult(); + } + + /// + /// 异步Ping指定主机 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 取消令牌 + /// Ping结果 + public static async Task PingAsync(string hostNameOrAddress, int timeout = 5000, CancellationToken cancellationToken = default) + { + var result = new PingResult + { + Address = hostNameOrAddress, + Timestamp = DateTime.UtcNow + }; + + try + { + // 解析IP地址 + IPAddress ipAddress; + if (IPAddress.TryParse(hostNameOrAddress, out var parsedIp)) + { + ipAddress = parsedIp; + } + else + { +#if NET5_0_OR_GREATER + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); +#else + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress); +#endif + if (addresses.Length == 0) + { + result.Status = IPStatus.Unknown; + result.ErrorMessage = "无法解析主机名"; + return result; + } + ipAddress = addresses[0]; + } + + result.IpAddress = ipAddress; + + using var ping = new System.Net.NetworkInformation.Ping(); + var buffer = new byte[32]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)('a' + (i % 26)); + } + + var options = new System.Net.NetworkInformation.PingOptions(128, true); + var reply = await ping.SendPingAsync(ipAddress, timeout, buffer, options); + + result.Status = reply.Status; + result.Success = reply.Status == IPStatus.Success; + + if (reply.Status == IPStatus.Success) + { + result.RoundtripTime = reply.RoundtripTime; + result.Ttl = reply.Options?.Ttl ?? 0; + result.BufferSize = reply.Buffer.Length; + } + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + result.Status = IPStatus.Unknown; + } + + return result; + } + + /// + /// Ping指定主机(带完整配置) + /// + /// 主机名或IP地址 + /// 配置 + /// 取消令牌 + /// Ping结果 + public static async Task PingAsync(string hostNameOrAddress, PingOptions options, CancellationToken cancellationToken = default) + { + var result = new PingResult + { + Address = hostNameOrAddress, + Timestamp = DateTime.UtcNow + }; + + try + { + IPAddress ipAddress; + if (IPAddress.TryParse(hostNameOrAddress, out var parsedIp)) + { + ipAddress = parsedIp; + } + else + { +#if NET5_0_OR_GREATER + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress, cancellationToken); +#else + var addresses = await Dns.GetHostAddressesAsync(hostNameOrAddress); +#endif + if (addresses.Length == 0) + { + result.Status = IPStatus.Unknown; + result.ErrorMessage = "无法解析主机名"; + return result; + } + ipAddress = addresses[0]; + } + + result.IpAddress = ipAddress; + + using var ping = new System.Net.NetworkInformation.Ping(); + var buffer = new byte[options.BufferSize]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)('a' + (i % 26)); + } + + var pingOptions = new System.Net.NetworkInformation.PingOptions(options.Ttl, options.DontFragment); + var reply = await ping.SendPingAsync(ipAddress, options.Timeout, buffer, pingOptions); + + result.Status = reply.Status; + result.Success = reply.Status == IPStatus.Success; + + if (reply.Status == IPStatus.Success) + { + result.RoundtripTime = reply.RoundtripTime; + result.Ttl = reply.Options?.Ttl ?? 0; + result.BufferSize = reply.Buffer.Length; + } + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + result.Status = IPStatus.Unknown; + } + + return result; + } + + /// + /// 持续Ping指定主机 + /// + /// 主机名或IP地址 + /// 配置 + /// 取消令牌 + /// Ping统计信息 + public static async Task PingContinuousAsync(string hostNameOrAddress, PingOptions? options = null, CancellationToken cancellationToken = default) + { + options ??= new PingOptions(); + var stats = new PingStatistics { Address = hostNameOrAddress }; + + long totalTime = 0; + long minTime = long.MaxValue; + long maxTime = long.MinValue; + + for (int i = 0; i < options.Count; i++) + { + if (cancellationToken.IsCancellationRequested) + break; + + var result = await PingAsync(hostNameOrAddress, options, cancellationToken); + stats.Results.Add(result); + stats.PacketsSent++; + + if (result.Success) + { + stats.PacketsReceived++; + totalTime += result.RoundtripTime; + + if (result.RoundtripTime < minTime) + minTime = result.RoundtripTime; + + if (result.RoundtripTime > maxTime) + maxTime = result.RoundtripTime; + } + + if (i < options.Count - 1) + { + await Task.Delay(options.Interval, cancellationToken); + } + } + + if (stats.PacketsReceived > 0) + { + stats.MinRoundtripTime = minTime; + stats.MaxRoundtripTime = maxTime; + stats.AverageRoundtripTime = (double)totalTime / stats.PacketsReceived; + } + + return stats; + } + + /// + /// 批量Ping多个主机 + /// + /// 主机列表 + /// 超时时间(毫秒) + /// 主机与结果的字典 + public static async Task> PingMultipleAsync(IEnumerable hosts, int timeout = 5000) + { + var tasks = hosts.Select(h => PingAsync(h, timeout)); + var results = await Task.WhenAll(tasks); + + return hosts.Zip(results, (h, r) => new { Host = h, Result = r }) + .ToDictionary(x => x.Host, x => x.Result); + } + + /// + /// 检测主机是否可达 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 是否可达 + public static async Task IsReachableAsync(string hostNameOrAddress, int timeout = 5000) + { + var result = await PingAsync(hostNameOrAddress, timeout); + return result.Success; + } + + /// + /// 检测主机是否可达 + /// + /// 主机名或IP地址 + /// 超时时间(毫秒) + /// 是否可达 + public static bool IsReachable(string hostNameOrAddress, int timeout = 5000) + { + return IsReachableAsync(hostNameOrAddress, timeout).GetAwaiter().GetResult(); + } + + /// + /// 检测TCP端口是否开放 + /// + /// 主机名或IP地址 + /// 端口号 + /// 超时时间(毫秒) + /// 端口是否开放 + public static async Task IsPortOpenAsync(string hostNameOrAddress, int port, int timeout = 5000) + { + try + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(hostNameOrAddress, port); + var timeoutTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(connectTask, timeoutTask); + + if (completedTask == connectTask && client.Connected) + { + return true; + } + + return false; + } + catch + { + return false; + } + } + + /// + /// 检测TCP端口是否开放 + /// + /// 主机名或IP地址 + /// 端口号 + /// 超时时间(毫秒) + /// 端口是否开放 + public static bool IsPortOpen(string hostNameOrAddress, int port, int timeout = 5000) + { + return IsPortOpenAsync(hostNameOrAddress, port, timeout).GetAwaiter().GetResult(); + } + + /// + /// 测试网络连接速度 + /// + /// 主机名或IP地址 + /// 测试次数 + /// 平均延迟(毫秒) + public static async Task TestLatencyAsync(string hostNameOrAddress, int count = 5) + { + var options = new PingOptions { Count = count }; + var stats = await PingContinuousAsync(hostNameOrAddress, options); + return stats.AverageRoundtripTime; + } + + /// + /// 获取本机IP地址 + /// + /// IP地址列表 + public static List GetLocalIPAddresses() + { + var result = new List(); + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + if (ni.OperationalStatus != OperationalStatus.Up) + continue; + + if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) + continue; + + var ipProperties = ni.GetIPProperties(); + foreach (var ip in ipProperties.UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork || + ip.Address.AddressFamily == AddressFamily.InterNetworkV6) + { + result.Add(ip.Address); + } + } + } + + return result; + } + + /// + /// 获取默认网关 + /// + /// 默认网关地址 + public static IPAddress? GetDefaultGateway() + { + var interfaces = NetworkInterface.GetAllNetworkInterfaces(); + + foreach (var ni in interfaces) + { + if (ni.OperationalStatus != OperationalStatus.Up) + continue; + + var ipProperties = ni.GetIPProperties(); + foreach (var gateway in ipProperties.GatewayAddresses) + { + if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) + { + return gateway.Address; + } + } + } + + return null; + } + } +} diff --git a/EasyTool.Core/NetCategory/PortScannerUtil.cs b/EasyTool.Core/NetCategory/PortScannerUtil.cs new file mode 100644 index 0000000..c696d88 --- /dev/null +++ b/EasyTool.Core/NetCategory/PortScannerUtil.cs @@ -0,0 +1,256 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 端口扫描工具类 + /// + public static class PortScannerUtil + { + /// + /// 检查端口是否开放 + /// + public static async Task IsPortOpenAsync(string host, int port, int timeoutMs = 1000) + { + try + { + using var client = new TcpClient(); + var connectTask = client.ConnectAsync(host, port); + var timeoutTask = Task.Delay(timeoutMs); + + var completedTask = await Task.WhenAny(connectTask, timeoutTask); + return completedTask == connectTask && client.Connected; + } + catch + { + return false; + } + } + + /// + /// 检查端口是否开放 + /// + public static bool IsPortOpen(string host, int port, int timeoutMs = 1000) + { + return IsPortOpenAsync(host, port, timeoutMs).GetAwaiter().GetResult(); + } + + /// + /// 扫描单个端口 + /// + public static PortScanResult ScanPort(string host, int port, int timeoutMs = 1000) + { + var startTime = DateTime.Now; + var isOpen = IsPortOpen(host, port, timeoutMs); + var duration = DateTime.Now - startTime; + + return new PortScanResult + { + Host = host, + Port = port, + IsOpen = isOpen, + ResponseTime = duration, + ServiceName = GetServiceName(port) + }; + } + + /// + /// 扫描多个端口 + /// + public static List ScanPorts(string host, IEnumerable ports, int timeoutMs = 1000) + { + var results = new List(); + foreach (var port in ports) + { + results.Add(ScanPort(host, port, timeoutMs)); + } + return results; + } + + /// + /// 异步扫描多个端口 + /// + public static async Task> ScanPortsAsync(string host, IEnumerable ports, int timeoutMs = 1000, int maxConcurrent = 100) + { + var results = new List(); + var semaphore = new SemaphoreSlim(maxConcurrent); + var tasks = new List>(); + + foreach (var port in ports) + { + tasks.Add(ScanPortAsync(host, port, timeoutMs, semaphore)); + } + + var completedResults = await Task.WhenAll(tasks); + results.AddRange(completedResults); + return results; + } + + private static async Task ScanPortAsync(string host, int port, int timeoutMs, SemaphoreSlim semaphore) + { + await semaphore.WaitAsync(); + try + { + var startTime = DateTime.Now; + var isOpen = await IsPortOpenAsync(host, port, timeoutMs); + var duration = DateTime.Now - startTime; + + return new PortScanResult + { + Host = host, + Port = port, + IsOpen = isOpen, + ResponseTime = duration, + ServiceName = GetServiceName(port) + }; + } + finally + { + semaphore.Release(); + } + } + + /// + /// 扫描端口范围 + /// + public static List ScanPortRange(string host, int startPort, int endPort, int timeoutMs = 1000) + { + var ports = new List(); + for (int i = startPort; i <= endPort; i++) + { + ports.Add(i); + } + return ScanPorts(host, ports, timeoutMs); + } + + /// + /// 异步扫描端口范围 + /// + public static Task> ScanPortRangeAsync(string host, int startPort, int endPort, int timeoutMs = 1000, int maxConcurrent = 100) + { + var ports = new List(); + for (int i = startPort; i <= endPort; i++) + { + ports.Add(i); + } + return ScanPortsAsync(host, ports, timeoutMs, maxConcurrent); + } + + /// + /// 扫描常用端口 + /// + public static List ScanCommonPorts(string host, int timeoutMs = 1000) + { + return ScanPorts(host, CommonPorts.Keys, timeoutMs); + } + + /// + /// 异步扫描常用端口 + /// + public static Task> ScanCommonPortsAsync(string host, int timeoutMs = 1000, int maxConcurrent = 100) + { + return ScanPortsAsync(host, CommonPorts.Keys, timeoutMs, maxConcurrent); + } + + /// + /// 获取服务名称 + /// + public static string GetServiceName(int port) + { + return CommonPorts.TryGetValue(port, out var name) ? name : "unknown"; + } + + /// + /// 常用端口映射 + /// + public static readonly Dictionary CommonPorts = new() + { + { 20, "FTP Data" }, + { 21, "FTP" }, + { 22, "SSH" }, + { 23, "Telnet" }, + { 25, "SMTP" }, + { 53, "DNS" }, + { 80, "HTTP" }, + { 110, "POP3" }, + { 119, "NNTP" }, + { 123, "NTP" }, + { 135, "RPC" }, + { 137, "NetBIOS Name" }, + { 138, "NetBIOS Datagram" }, + { 139, "NetBIOS Session" }, + { 143, "IMAP" }, + { 161, "SNMP" }, + { 162, "SNMP Trap" }, + { 389, "LDAP" }, + { 443, "HTTPS" }, + { 445, "SMB" }, + { 465, "SMTPS" }, + { 514, "Syslog" }, + { 587, "SMTP(TLS)" }, + { 636, "LDAPS" }, + { 993, "IMAPS" }, + { 995, "POP3S" }, + { 1080, "SOCKS" }, + { 1433, "MSSQL" }, + { 1434, "MSSQL Monitor" }, + { 1521, "Oracle" }, + { 1723, "PPTP" }, + { 2049, "NFS" }, + { 3306, "MySQL" }, + { 3389, "RDP" }, + { 5432, "PostgreSQL" }, + { 5900, "VNC" }, + { 5901, "VNC-1" }, + { 5902, "VNC-2" }, + { 6379, "Redis" }, + { 8080, "HTTP-Alt" }, + { 8443, "HTTPS-Alt" }, + { 9000, "PHP-FPM" }, + { 9200, "Elasticsearch" }, + { 9300, "Elasticsearch Transport" }, + { 11211, "Memcached" }, + { 27017, "MongoDB" } + }; + } + + /// + /// 端口扫描结果 + /// + public class PortScanResult + { + /// + /// 主机 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 端口 + /// + public int Port { get; set; } + + /// + /// 是否开放 + /// + public bool IsOpen { get; set; } + + /// + /// 响应时间 + /// + public TimeSpan ResponseTime { get; set; } + + /// + /// 服务名称 + /// + public string ServiceName { get; set; } = string.Empty; + + public override string ToString() + { + return $"{Host}:{Port} - {(IsOpen ? "Open" : "Closed")} ({ServiceName})"; + } + } +} diff --git a/EasyTool.Core/NetCategory/ProxyUtil.cs b/EasyTool.Core/NetCategory/ProxyUtil.cs new file mode 100644 index 0000000..795aaa4 --- /dev/null +++ b/EasyTool.Core/NetCategory/ProxyUtil.cs @@ -0,0 +1,619 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// 代理类型 + /// + public enum ProxyType + { + /// + /// HTTP代理 + /// + Http, + + /// + /// HTTPS代理 + /// + Https, + + /// + /// SOCKS4代理 + /// + Socks4, + + /// + /// SOCKS4a代理 + /// + Socks4a, + + /// + /// SOCKS5代理 + /// + Socks5 + } + + /// + /// 代理信息 + /// + public class ProxyInfo + { + /// + /// 代理地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 代理端口 + /// + public int Port { get; set; } + + /// + /// 用户名 + /// + public string? Username { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 代理类型 + /// + public ProxyType Type { get; set; } = ProxyType.Http; + + /// + /// 是否需要认证 + /// + public bool RequiresAuthentication => !string.IsNullOrEmpty(Username); + + /// + /// 代理地址(格式:host:port) + /// + public string Address => $"{Host}:{Port}"; + + /// + /// 代理URL + /// + public string ProxyUrl + { + get + { + var scheme = Type switch + { + ProxyType.Http => "http", + ProxyType.Https => "https", + ProxyType.Socks4 => "socks4", + ProxyType.Socks4a => "socks4a", + ProxyType.Socks5 => "socks5", + _ => "http" + }; + + if (RequiresAuthentication) + { + return $"{scheme}://{Username}:{Password}@{Host}:{Port}"; + } + return $"{scheme}://{Host}:{Port}"; + } + } + + public override string ToString() + { + return $"{Type}://{Address}"; + } + } + + /// + /// 代理工具类 + /// + public static class ProxyUtil + { + /// + /// 解析代理字符串 + /// 支持格式: + /// - host:port + /// - http://host:port + /// - http://user:pass@host:port + /// - socks5://host:port + /// + /// 代理字符串 + /// 代理信息 + public static ProxyInfo Parse(string proxyString) + { + if (string.IsNullOrWhiteSpace(proxyString)) + throw new ArgumentException("代理字符串不能为空", nameof(proxyString)); + + var info = new ProxyInfo(); + + // 解析协议 + if (proxyString.Contains("://")) + { + var parts = proxyString.Split(new[] { "://" }, 2, StringSplitOptions.None); + var scheme = parts[0].ToLower(); + info.Type = scheme switch + { + "http" => ProxyType.Http, + "https" => ProxyType.Https, + "socks4" => ProxyType.Socks4, + "socks4a" => ProxyType.Socks4a, + "socks5" => ProxyType.Socks5, + _ => ProxyType.Http + }; + proxyString = parts[1]; + } + + // 解析认证信息 + if (proxyString.Contains("@")) + { + var authParts = proxyString.Split('@'); + var credentials = authParts[0].Split(':'); + if (credentials.Length == 2) + { + info.Username = credentials[0]; + info.Password = credentials[1]; + } + proxyString = authParts[1]; + } + + // 解析主机和端口 + var hostPort = proxyString.Split(':'); + if (hostPort.Length >= 2) + { + info.Host = hostPort[0]; + if (int.TryParse(hostPort[1], out var port)) + { + info.Port = port; + } + } + else + { + info.Host = proxyString; + } + + return info; + } + + /// + /// 创建WebProxy + /// + /// 代理信息 + /// WebProxy + public static WebProxy CreateWebProxy(ProxyInfo proxyInfo) + { + var proxy = new WebProxy(proxyInfo.Host, proxyInfo.Port); + + if (proxyInfo.RequiresAuthentication) + { + proxy.Credentials = new NetworkCredential(proxyInfo.Username, proxyInfo.Password); + } + + return proxy; + } + + /// + /// 创建WebProxy + /// + /// 主机 + /// 端口 + /// 用户名 + /// 密码 + /// WebProxy + public static WebProxy CreateWebProxy(string host, int port, string? username = null, string? password = null) + { + return CreateWebProxy(new ProxyInfo + { + Host = host, + Port = port, + Username = username, + Password = password + }); + } + + /// + /// 创建HttpClientHandler(带代理) + /// + /// 代理信息 + /// HttpClientHandler + public static HttpClientHandler CreateHttpClientHandler(ProxyInfo proxyInfo) + { + var handler = new HttpClientHandler + { + Proxy = CreateWebProxy(proxyInfo), + UseProxy = true + }; + + if (proxyInfo.RequiresAuthentication) + { + handler.PreAuthenticate = true; + } + + return handler; + } + + /// + /// 创建HttpClient(带代理) + /// + /// 代理信息 + /// HttpClient + public static HttpClient CreateHttpClient(ProxyInfo proxyInfo) + { + var handler = CreateHttpClientHandler(proxyInfo); + return new HttpClient(handler); + } + + /// + /// 创建HttpClient(带代理) + /// + /// 代理字符串 + /// HttpClient + public static HttpClient CreateHttpClient(string proxyString) + { + var proxyInfo = Parse(proxyString); + return CreateHttpClient(proxyInfo); + } + + /// + /// 测试代理连接 + /// + /// 代理信息 + /// 测试URL + /// 超时时间 + /// 是否可用 + public static async Task TestProxyAsync(ProxyInfo proxyInfo, string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + try + { + using var client = CreateHttpClient(proxyInfo); + client.Timeout = timeout ?? TimeSpan.FromSeconds(30); + + var response = await client.GetAsync(testUrl); + return response.IsSuccessStatusCode; + } + catch + { + return false; + } + } + + /// + /// 测试代理连接 + /// + /// 代理字符串 + /// 测试URL + /// 超时时间 + /// 是否可用 + public static async Task TestProxyAsync(string proxyString, string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + var proxyInfo = Parse(proxyString); + return await TestProxyAsync(proxyInfo, testUrl, timeout); + } + + /// + /// 获取代理响应时间 + /// + /// 代理信息 + /// 测试URL + /// 响应时间(毫秒),失败返回-1 + public static async Task GetResponseTimeAsync(ProxyInfo proxyInfo, string testUrl = "http://www.google.com") + { + try + { + using var client = CreateHttpClient(proxyInfo); + client.Timeout = TimeSpan.FromSeconds(30); + + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + await client.GetAsync(testUrl); + stopwatch.Stop(); + + return stopwatch.ElapsedMilliseconds; + } + catch + { + return -1; + } + } + + /// + /// 获取系统代理设置 + /// + /// 代理信息(无代理返回null) + public static ProxyInfo? GetSystemProxy() + { + var proxy = WebRequest.GetSystemWebProxy(); + + if (proxy == null) + return null; + + var defaultProxy = proxy.GetProxy(new Uri("http://example.com")); + if (defaultProxy == null || defaultProxy.Host == "example.com") + return null; + + return new ProxyInfo + { + Host = defaultProxy.Host, + Port = defaultProxy.Port, + Type = ProxyType.Http + }; + } + + /// + /// 是否使用系统代理 + /// + /// 是否使用系统代理 + public static bool IsSystemProxyEnabled() + { + return GetSystemProxy() != null; + } + } + + /// + /// 代理池 + /// + public class ProxyPool + { + private readonly List _proxies = new(); + private readonly Dictionary _stats = new(); + private int _currentIndex = 0; + private readonly object _lock = new(); + + /// + /// 代理数量 + /// + public int Count => _proxies.Count; + + /// + /// 添加代理 + /// + /// 代理信息 + public void Add(ProxyInfo proxyInfo) + { + lock (_lock) + { + _proxies.Add(proxyInfo); + _stats[proxyInfo.Address] = new ProxyStats { ProxyAddress = proxyInfo.Address }; + } + } + + /// + /// 添加代理 + /// + /// 代理字符串 + public void Add(string proxyString) + { + Add(ProxyUtil.Parse(proxyString)); + } + + /// + /// 批量添加代理 + /// + /// 代理字符串列表 + public void AddRange(IEnumerable proxyStrings) + { + foreach (var proxyString in proxyStrings) + { + Add(proxyString); + } + } + + /// + /// 移除代理 + /// + /// 代理信息 + public void Remove(ProxyInfo proxyInfo) + { + lock (_lock) + { + _proxies.RemoveAll(p => p.Address == proxyInfo.Address); + _stats.Remove(proxyInfo.Address); + } + } + + /// + /// 清空代理池 + /// + public void Clear() + { + lock (_lock) + { + _proxies.Clear(); + _stats.Clear(); + _currentIndex = 0; + } + } + + /// + /// 获取下一个代理(轮询) + /// + /// 代理信息 + public ProxyInfo? GetNext() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + var proxy = _proxies[_currentIndex]; + _currentIndex = (_currentIndex + 1) % _proxies.Count; + return proxy; + } + } + + /// + /// 获取随机代理 + /// + /// 代理信息 + public ProxyInfo? GetRandom() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + var random = new Random(); + return _proxies[random.Next(_proxies.Count)]; + } + } + + /// + /// 获取最快的代理 + /// + /// 代理信息 + public ProxyInfo? GetFastest() + { + lock (_lock) + { + if (_proxies.Count == 0) + return null; + + return _proxies + .Where(p => _stats.ContainsKey(p.Address)) + .OrderBy(p => _stats[p.Address].AverageResponseTime) + .FirstOrDefault() ?? GetRandom(); + } + } + + /// + /// 报告代理使用结果 + /// + /// 代理地址 + /// 是否成功 + /// 响应时间 + public void ReportResult(string proxyAddress, bool success, long responseTime = 0) + { + lock (_lock) + { + if (_stats.TryGetValue(proxyAddress, out var stats)) + { + stats.TotalRequests++; + if (success) + { + stats.SuccessCount++; + if (responseTime > 0) + { + stats.TotalResponseTime += responseTime; + stats.AverageResponseTime = stats.TotalResponseTime / stats.SuccessCount; + } + } + else + { + stats.FailureCount++; + } + } + } + } + + /// + /// 获取代理统计信息 + /// + /// 代理地址 + /// 统计信息 + public ProxyStats? GetStats(string proxyAddress) + { + lock (_lock) + { + return _stats.TryGetValue(proxyAddress, out var stats) ? stats : null; + } + } + + /// + /// 移除失败率高的代理 + /// + /// 最大失败率(0-1) + /// 移除的代理数量 + public int RemoveHighFailureProxies(double maxFailureRate = 0.5) + { + lock (_lock) + { + var toRemove = _stats + .Where(s => s.Value.TotalRequests >= 5 && s.Value.FailureRate > maxFailureRate) + .Select(s => s.Key) + .ToList(); + + foreach (var address in toRemove) + { + _proxies.RemoveAll(p => p.Address == address); + _stats.Remove(address); + } + + return toRemove.Count; + } + } + + /// + /// 测试所有代理 + /// + /// 测试URL + /// 超时时间 + /// 可用代理数量 + public async Task TestAllAsync(string testUrl = "http://www.google.com", TimeSpan? timeout = null) + { + var tasks = _proxies.Select(async proxy => + { + var responseTime = await ProxyUtil.GetResponseTimeAsync(proxy, testUrl); + var success = responseTime >= 0; + ReportResult(proxy.Address, success, responseTime); + return success; + }); + + var results = await Task.WhenAll(tasks); + return results.Count(r => r); + } + } + + /// + /// 代理统计信息 + /// + public class ProxyStats + { + /// + /// 代理地址 + /// + public string ProxyAddress { get; set; } = string.Empty; + + /// + /// 总请求数 + /// + public int TotalRequests { get; set; } + + /// + /// 成功次数 + /// + public int SuccessCount { get; set; } + + /// + /// 失败次数 + /// + public int FailureCount { get; set; } + + /// + /// 总响应时间 + /// + public long TotalResponseTime { get; set; } + + /// + /// 平均响应时间 + /// + public long AverageResponseTime { get; set; } + + /// + /// 成功率 + /// + public double SuccessRate => TotalRequests > 0 ? (double)SuccessCount / TotalRequests : 0; + + /// + /// 失败率 + /// + public double FailureRate => TotalRequests > 0 ? (double)FailureCount / TotalRequests : 0; + + public override string ToString() + { + return $"代理: {ProxyAddress}, 总请求: {TotalRequests}, 成功率: {SuccessRate:P2}, 平均响应: {AverageResponseTime}ms"; + } + } +} diff --git a/EasyTool.Core/NetCategory/SmtpUtil.cs b/EasyTool.Core/NetCategory/SmtpUtil.cs new file mode 100644 index 0000000..9adea89 --- /dev/null +++ b/EasyTool.Core/NetCategory/SmtpUtil.cs @@ -0,0 +1,389 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// SMTP 邮件发送工具类 + /// + public static class SmtpUtil + { + /// + /// 发送邮件 + /// + /// SMTP 配置 + /// 邮件消息 + public static void Send(SmtpOptions options, EmailMessage message) + { + using var smtpClient = CreateSmtpClient(options); + using var mailMessage = CreateMailMessage(message); + smtpClient.Send(mailMessage); + } + + /// + /// 异步发送邮件 + /// + public static async Task SendAsync(SmtpOptions options, EmailMessage message) + { + using var smtpClient = CreateSmtpClient(options); + using var mailMessage = CreateMailMessage(message); + await smtpClient.SendMailAsync(mailMessage); + } + + /// + /// 批量发送邮件 + /// + public static void SendBatch(SmtpOptions options, IEnumerable messages) + { + using var smtpClient = CreateSmtpClient(options); + + foreach (var message in messages) + { + using var mailMessage = CreateMailMessage(message); + smtpClient.Send(mailMessage); + } + } + + /// + /// 发送简单邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 正文 + /// 是否为 HTML + public static void SendSimple(SmtpOptions options, string to, string subject, string body, bool isHtml = false) + { + var message = new EmailMessage + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml + }; + + Send(options, message); + } + + /// + /// 发送带附件的邮件 + /// + public static void SendWithAttachment(SmtpOptions options, string to, string subject, string body, string attachmentPath, bool isHtml = false) + { + var message = new EmailMessage + { + To = new List { to }, + Subject = subject, + Body = body, + IsBodyHtml = isHtml, + Attachments = new List + { + new EmailAttachment { FilePath = attachmentPath } + } + }; + + Send(options, message); + } + + /// + /// 发送模板邮件 + /// + /// SMTP 配置 + /// 收件人 + /// 主题 + /// 模板内容 + /// 模板参数 + /// 是否为 HTML + public static void SendTemplate(SmtpOptions options, string to, string subject, string template, Dictionary parameters, bool isHtml = false) + { + var body = template; + foreach (var kvp in parameters) + { + body = body.Replace($"{{{kvp.Key}}}", kvp.Value?.ToString() ?? string.Empty); + } + + SendSimple(options, to, subject, body, isHtml); + } + + /// + /// 测试 SMTP 连接 + /// + public static bool TestConnection(SmtpOptions options) + { + try + { + using var smtpClient = CreateSmtpClient(options); + smtpClient.Send(new MailMessage(options.Username, options.Username, "Test", "Test connection")); + return true; + } + catch + { + return false; + } + } + + private static SmtpClient CreateSmtpClient(SmtpOptions options) + { + var client = new SmtpClient(options.Host, options.Port) + { + EnableSsl = options.EnableSsl, + Timeout = options.Timeout * 1000 + }; + + if (!string.IsNullOrEmpty(options.Username)) + { + client.Credentials = new NetworkCredential(options.Username, options.Password); + } + + return client; + } + + private static MailMessage CreateMailMessage(EmailMessage message) + { + var mailMessage = new MailMessage + { + Subject = message.Subject, + Body = message.Body, + IsBodyHtml = message.IsBodyHtml, + SubjectEncoding = Encoding.UTF8, + BodyEncoding = Encoding.UTF8 + }; + + // 发件人 + if (!string.IsNullOrEmpty(message.From)) + { + mailMessage.From = new MailAddress(message.From, message.FromName, Encoding.UTF8); + } + + // 收件人 + foreach (var to in message.To ?? Enumerable.Empty()) + { + mailMessage.To.Add(new MailAddress(to)); + } + + // 抄送 + foreach (var cc in message.Cc ?? Enumerable.Empty()) + { + mailMessage.CC.Add(new MailAddress(cc)); + } + + // 密送 + foreach (var bcc in message.Bcc ?? Enumerable.Empty()) + { + mailMessage.Bcc.Add(new MailAddress(bcc)); + } + + // 回复地址 + if (!string.IsNullOrEmpty(message.ReplyTo)) + { + mailMessage.ReplyToList.Add(new MailAddress(message.ReplyTo)); + } + + // 附件 + foreach (var attachment in message.Attachments ?? Enumerable.Empty()) + { + Attachment mailAttachment; + + if (!string.IsNullOrEmpty(attachment.FilePath)) + { + mailAttachment = new Attachment(attachment.FilePath, GetMimeType(attachment.FilePath)); + } + else if (attachment.Content != null && !string.IsNullOrEmpty(attachment.FileName)) + { + var stream = new MemoryStream(attachment.Content); + mailAttachment = new Attachment(stream, attachment.FileName, attachment.ContentType ?? GetMimeType(attachment.FileName)); + } + else + { + continue; + } + + mailAttachment.ContentDisposition!.DispositionType = DispositionTypeNames.Attachment; + if (!string.IsNullOrEmpty(attachment.ContentId)) + { + mailAttachment.ContentId = attachment.ContentId; + } + + mailMessage.Attachments.Add(mailAttachment); + } + + // 优先级 + mailMessage.Priority = message.Priority switch + { + EmailPriority.High => MailPriority.High, + EmailPriority.Low => MailPriority.Low, + _ => MailPriority.Normal + }; + + return mailMessage; + } + + private static string GetMimeType(string fileName) + { + var extension = Path.GetExtension(fileName).ToLowerInvariant(); + return extension switch + { + ".txt" => "text/plain", + ".pdf" => "application/pdf", + ".doc" => "application/msword", + ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".xls" => "application/vnd.ms-excel", + ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ".ppt" => "application/vnd.ms-powerpoint", + ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ".jpg" or ".jpeg" => "image/jpeg", + ".png" => "image/png", + ".gif" => "image/gif", + ".zip" => "application/zip", + ".rar" => "application/x-rar-compressed", + ".7z" => "application/x-7z-compressed", + _ => "application/octet-stream" + }; + } + } + + /// + /// SMTP 配置选项 + /// + public class SmtpOptions + { + /// + /// SMTP 服务器地址 + /// + public string Host { get; set; } = string.Empty; + + /// + /// 端口号 + /// + public int Port { get; set; } = 25; + + /// + /// 是否启用 SSL + /// + public bool EnableSsl { get; set; } = true; + + /// + /// 用户名 + /// + public string? Username { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 超时时间(秒) + /// + public int Timeout { get; set; } = 30; + } + + /// + /// 邮件消息 + /// + public class EmailMessage + { + /// + /// 发件人地址 + /// + public string? From { get; set; } + + /// + /// 发件人名称 + /// + public string? FromName { get; set; } + + /// + /// 收件人列表 + /// + public List? To { get; set; } + + /// + /// 抄送列表 + /// + public List? Cc { get; set; } + + /// + /// 密送列表 + /// + public List? Bcc { get; set; } + + /// + /// 回复地址 + /// + public string? ReplyTo { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 正文 + /// + public string Body { get; set; } = string.Empty; + + /// + /// 是否为 HTML 格式 + /// + public bool IsBodyHtml { get; set; } + + /// + /// 附件列表 + /// + public List? Attachments { get; set; } + + /// + /// 优先级 + /// + public EmailPriority Priority { get; set; } = EmailPriority.Normal; + } + + /// + /// 邮件附件 + /// + public class EmailAttachment + { + /// + /// 文件路径 + /// + public string? FilePath { get; set; } + + /// + /// 文件名 + /// + public string? FileName { get; set; } + + /// + /// 文件内容(字节) + /// + public byte[]? Content { get; set; } + + /// + /// 内容类型(MIME 类型) + /// + public string? ContentType { get; set; } + + /// + /// 内容 ID(用于嵌入图片) + /// + public string? ContentId { get; set; } + } + + /// + /// 邮件优先级 + /// + public enum EmailPriority + { + Low, + Normal, + High + } +} diff --git a/EasyTool.Core/NetCategory/SseUtil.cs b/EasyTool.Core/NetCategory/SseUtil.cs new file mode 100644 index 0000000..253397e --- /dev/null +++ b/EasyTool.Core/NetCategory/SseUtil.cs @@ -0,0 +1,521 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Server-Sent Events (SSE) 客户端 + /// 用于接收服务器推送的事件流 + /// + public class SseClient : IDisposable + { + private readonly HttpClient _httpClient; + private readonly Uri _endpoint; + private readonly Dictionary _headers; + private CancellationTokenSource? _cts; + private bool _disposed; + private bool _isConnected; + + /// + /// 接收到事件时触发 + /// + public event EventHandler? EventReceived; + + /// + /// 连接打开时触发 + /// + public event EventHandler? Connected; + + /// + /// 连接关闭时触发 + /// + public event EventHandler? Disconnected; + + /// + /// 发生错误时触发 + /// + public event EventHandler? Error; + + /// + /// 是否已连接 + /// + public bool IsConnected => _isConnected; + + /// + /// 最后接收的事件 ID + /// + public string? LastEventId { get; private set; } + + /// + /// 重连等待时间(毫秒) + /// + public int ReconnectDelay { get; set; } = 3000; + + /// + /// 最大重连次数 + /// + public int MaxReconnectAttempts { get; set; } = 5; + + /// + /// 是否自动重连 + /// + public bool AutoReconnect { get; set; } = true; + + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + public SseClient(string endpoint, Dictionary? headers = null) + : this(new Uri(endpoint), headers) + { + } + + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + public SseClient(Uri endpoint, Dictionary? headers = null) + : this(new HttpClient(), endpoint, headers) + { + } + + /// + /// 创建 SSE 客户端 + /// + /// HttpClient 实例 + /// SSE 端点 URL + /// 请求头 + public SseClient(HttpClient httpClient, Uri endpoint, Dictionary? headers = null) + { + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); + _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); + _headers = headers ?? new Dictionary(); + } + + /// + /// 连接并开始接收事件 + /// + /// 取消令牌 + public async Task ConnectAsync(CancellationToken cancellationToken = default) + { + ThrowIfDisposed(); + + _cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + + try + { + await ConnectInternalAsync(_cts.Token); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + // 正常取消,不触发错误 + } + catch (Exception ex) + { + OnError(ex); + } + } + + /// + /// 断开连接 + /// + public async Task DisconnectAsync() + { + if (_cts != null && !_cts.IsCancellationRequested) + { + _cts.Cancel(); + } + + _isConnected = false; + OnDisconnected(null); + await Task.CompletedTask; + } + + /// + /// 异步获取所有事件(直到连接关闭) + /// + /// 取消令牌 + /// 事件集合 + public async IAsyncEnumerable GetEventsAsync( + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var events = new System.Collections.Concurrent.BlockingCollection(); + var completed = new TaskCompletionSource(); + + EventReceived += (_, e) => events.Add(e); + Disconnected += (_, _) => completed.TrySetResult(true); + Error += (_, _) => completed.TrySetResult(true); + + _ = ConnectAsync(cancellationToken); + + while (!cancellationToken.IsCancellationRequested) + { + if (events.TryTake(out var sseEvent, 100, cancellationToken)) + { + yield return sseEvent; + } + + if (completed.Task.IsCompleted) + { + while (events.TryTake(out var remainingEvent)) + { + yield return remainingEvent; + } + break; + } + } + } + + private async Task ConnectInternalAsync(CancellationToken cancellationToken) + { + var reconnectAttempts = 0; + + while (!cancellationToken.IsCancellationRequested && (reconnectAttempts < MaxReconnectAttempts || !AutoReconnect)) + { + try + { + var request = new HttpRequestMessage(HttpMethod.Get, _endpoint); + request.Headers.Accept.ParseAdd("text/event-stream"); + request.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue { NoCache = true }; + + foreach (var header in _headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + // 添加 Last-Event-ID 头 + if (!string.IsNullOrEmpty(LastEventId)) + { + request.Headers.TryAddWithoutValidation("Last-Event-ID", LastEventId); + } + + using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + response.EnsureSuccessStatusCode(); + + _isConnected = true; + reconnectAttempts = 0; + OnConnected(); + +#if NETSTANDARD2_1 + using var stream = await response.Content.ReadAsStreamAsync(); +#else + using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); +#endif + using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true); + + await ProcessEventStreamAsync(reader, cancellationToken); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + break; + } + catch (Exception ex) + { + OnError(ex); + } + + _isConnected = false; + + if (AutoReconnect && reconnectAttempts < MaxReconnectAttempts && !cancellationToken.IsCancellationRequested) + { + reconnectAttempts++; + OnDisconnected(reconnectAttempts); + + try + { + await Task.Delay(ReconnectDelay * reconnectAttempts, cancellationToken); + } + catch (OperationCanceledException) + { + break; + } + } + else + { + break; + } + } + } + + private async Task ProcessEventStreamAsync(StreamReader reader, CancellationToken cancellationToken) + { + var currentEvent = new SseEventBuilder(); + + while (!cancellationToken.IsCancellationRequested) + { + var line = await reader.ReadLineAsync(); + if (line == null) + { + // 流结束 + if (currentEvent.HasData) + { + OnEventReceived(currentEvent.Build()); + } + break; + } + + if (string.IsNullOrEmpty(line)) + { + // 空行表示事件结束 + if (currentEvent.HasData) + { + var sseEvent = currentEvent.Build(); + LastEventId = sseEvent.Id; + OnEventReceived(sseEvent); + currentEvent = new SseEventBuilder(); + } + continue; + } + + if (line.StartsWith(':')) + { + // 注释行,忽略 + continue; + } + + var colonIndex = line.IndexOf(':'); + string field, value; + + if (colonIndex < 0) + { + field = line; + value = string.Empty; + } + else + { + field = line[..colonIndex]; + value = line[(colonIndex + 1)..]; + if (value.StartsWith(' ')) + { + value = value[1..]; + } + } + + switch (field) + { + case "event": + currentEvent.EventType = value; + break; + case "data": + currentEvent.AppendData(value); + break; + case "id": + currentEvent.Id = value; + break; + case "retry": + if (int.TryParse(value, out var retryMs)) + { + ReconnectDelay = retryMs; + } + break; + } + } + } + + protected virtual void OnEventReceived(SseEvent sseEvent) + { + EventReceived?.Invoke(this, sseEvent); + } + + protected virtual void OnConnected() + { + Connected?.Invoke(this, EventArgs.Empty); + } + + protected virtual void OnDisconnected(int? reconnectAttempt) + { + Disconnected?.Invoke(this, new SseDisconnectEventArgs(reconnectAttempt)); + } + + protected virtual void OnError(Exception ex) + { + Error?.Invoke(this, ex); + } + + private void ThrowIfDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(nameof(SseClient)); + } + } + + public void Dispose() + { + if (_disposed) + return; + + _cts?.Cancel(); + _cts?.Dispose(); + _disposed = true; + } + } + + /// + /// SSE 事件 + /// + public class SseEvent + { + /// + /// 事件 ID + /// + public string? Id { get; set; } + + /// + /// 事件类型 + /// + public string? EventType { get; set; } + + /// + /// 事件数据 + /// + public string Data { get; set; } = string.Empty; + + /// + /// 接收时间 + /// + public DateTimeOffset ReceivedAt { get; set; } = DateTimeOffset.UtcNow; + + /// + /// 尝试解析数据为 JSON + /// + public T? ParseJson() + { + try + { + return System.Text.Json.JsonSerializer.Deserialize(Data); + } + catch + { + return default; + } + } + + public override string ToString() + { + var sb = new StringBuilder(); + if (!string.IsNullOrEmpty(Id)) + sb.Append($"[{Id}] "); + if (!string.IsNullOrEmpty(EventType)) + sb.Append($"({EventType}) "); + sb.Append(Data); + return sb.ToString(); + } + } + + /// + /// SSE 断开连接事件参数 + /// + public class SseDisconnectEventArgs : EventArgs + { + /// + /// 重连尝试次数(如果正在重连) + /// + public int? ReconnectAttempt { get; } + + public SseDisconnectEventArgs(int? reconnectAttempt) + { + ReconnectAttempt = reconnectAttempt; + } + } + + /// + /// SSE 事件构建器 + /// + internal class SseEventBuilder + { + public string? Id { get; set; } + public string? EventType { get; set; } + private readonly StringBuilder _data = new(); + + public bool HasData => _data.Length > 0; + + public void AppendData(string data) + { + if (_data.Length > 0) + { + _data.AppendLine(); + } + _data.Append(data); + } + + public SseEvent Build() + { + return new SseEvent + { + Id = Id, + EventType = EventType, + Data = _data.ToString() + }; + } + } + + /// + /// SSE 客户端扩展方法 + /// + public static class SseClientExtensions + { + /// + /// 创建 SSE 客户端 + /// + /// SSE 端点 URL + /// 请求头 + /// SSE 客户端实例 + public static SseClient CreateSseClient(string endpoint, Dictionary? headers = null) + { + return new SseClient(endpoint, headers); + } + + /// + /// 创建 SSE 客户端(带认证) + /// + /// SSE 端点 URL + /// Bearer Token + /// SSE 客户端实例 + public static SseClient CreateSseClientWithAuth(string endpoint, string bearerToken) + { + return new SseClient(endpoint, new Dictionary + { + ["Authorization"] = $"Bearer {bearerToken}" + }); + } + + /// + /// 异步获取单个 SSE 事件 + /// + /// SSE 端点 URL + /// 超时时间 + /// 取消令牌 + /// SSE 事件 + public static async Task GetSingleEventAsync(string endpoint, TimeSpan timeout, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(timeout); + + using var client = new SseClient(endpoint); + SseEvent? result = null; + var tcs = new TaskCompletionSource(); + + client.EventReceived += (_, e) => + { + result = e; + tcs.TrySetResult(e); + }; + + _ = client.ConnectAsync(cts.Token); + + var completedTask = await Task.WhenAny(tcs.Task, Task.Delay(timeout, cts.Token)); + + await client.DisconnectAsync(); + + return completedTask == tcs.Task ? result : null; + } + } +} diff --git a/EasyTool.Core/ToolCategory/URLUtil.cs b/EasyTool.Core/NetCategory/URLUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/URLUtil.cs rename to EasyTool.Core/NetCategory/URLUtil.cs index d281985..3227671 100644 --- a/EasyTool.Core/ToolCategory/URLUtil.cs +++ b/EasyTool.Core/NetCategory/URLUtil.cs @@ -4,12 +4,12 @@ using System.Text; using System.Web; -namespace EasyTool +namespace EasyTool.NetCategory { /// /// URL工具类 /// - public class URLUtil + public static class URLUtil { /// /// 解析URL并返回其组成部分。 diff --git a/EasyTool.Core/NetCategory/UserAgentUtil.cs b/EasyTool.Core/NetCategory/UserAgentUtil.cs new file mode 100644 index 0000000..28a39b3 --- /dev/null +++ b/EasyTool.Core/NetCategory/UserAgentUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Text.RegularExpressions; + +namespace EasyTool.NetCategory +{ + /// + /// User-Agent 解析工具类 + /// 用于解析浏览器、操作系统、设备等信息 + /// + public static class UserAgentUtil + { + #region 常见浏览器正则 + + private static readonly Regex BrowserRegex = new( + @"(Edge|Edg|OPR|Opera|Chrome|Safari|Firefox|MSIE|Trident|SamsungBrowser|UCBrowser|QQBrowser|MicroMessenger|WeChat|Alipay|WeiBo|DingTalk)[/\s]?(\d+[.\d]*)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex OsRegex = new( + @"(Windows NT|Windows Phone|Android|iPhone|iPad|iPod|Mac OS X|Linux|Ubuntu|Fedora|FreeBSD|Chrome OS)[/\s]?(\d+[.\d]*)?", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex DeviceRegex = new( + @"(Mobile|Android|iPhone|iPad|iPod|Tablet|Kindle|BlackBerry|PlayBook|Nokia|Samsung|HTC|Motorola|LG|Sony|Xiaomi|Huawei|OPPO|Vivo|OnePlus)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + private static readonly Regex BotRegex = new( + @"(Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|facebookexternalhit|ia_archiver|Twitterbot|LinkedInBot|Embedly|Quora Link Preview|ShowyouBot|outbrain|pinterest|applebot|SemrushBot|AhrefsBot)", + RegexOptions.Compiled | RegexOptions.IgnoreCase); + + #endregion + + /// + /// 解析 User-Agent 字符串 + /// + /// User-Agent 字符串 + /// 解析结果 + public static UserAgentInfo Parse(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + { + return new UserAgentInfo + { + Browser = BrowserInfo.Unknown, + Os = OsInfo.Unknown, + Device = DeviceInfo.Unknown, + IsBot = false + }; + } + + return new UserAgentInfo + { + Browser = ParseBrowser(userAgent), + Os = ParseOs(userAgent), + Device = ParseDevice(userAgent), + IsBot = IsBot(userAgent) + }; + } + + /// + /// 解析浏览器信息 + /// + public static BrowserInfo ParseBrowser(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return BrowserInfo.Unknown; + + var match = BrowserRegex.Match(userAgent); + if (!match.Success) + return BrowserInfo.Unknown; + + var name = match.Groups[1].Value.ToLowerInvariant(); + var version = match.Groups[2].Value; + + // 规范化浏览器名称 + var browserName = name switch + { + "edg" or "edge" => "Edge", + "opr" or "opera" => "Opera", + "chrome" => "Chrome", + "safari" => "Safari", + "firefox" => "Firefox", + "msie" or "trident" => "Internet Explorer", + "samsungbrowser" => "Samsung Browser", + "ucbrowser" => "UC Browser", + "qqbrowser" => "QQ Browser", + "micromessenger" or "wechat" => "WeChat", + "alipay" => "Alipay", + "weibo" => "Weibo", + "dingtalk" => "DingTalk", + _ => char.ToUpperInvariant(name[0]) + name.Substring(1) + }; + + return new BrowserInfo + { + Name = browserName, + Version = version, + VersionNumber = ParseVersionNumber(version) + }; + } + + /// + /// 解析操作系统信息 + /// + public static OsInfo ParseOs(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return OsInfo.Unknown; + + var match = OsRegex.Match(userAgent); + if (!match.Success) + return OsInfo.Unknown; + + var name = match.Groups[1].Value.ToLowerInvariant(); + var version = match.Groups[2].Value; + + var osName = name switch + { + "windows nt" => ParseWindowsVersion(version), + "windows phone" => "Windows Phone", + "android" => "Android", + "iphone" or "ipad" or "ipod" => "iOS", + "mac os x" => "macOS", + "linux" => "Linux", + "ubuntu" => "Ubuntu", + "fedora" => "Fedora", + "freebsd" => "FreeBSD", + "chrome os" => "Chrome OS", + _ => char.ToUpperInvariant(name[0]) + name.Substring(1) + }; + + return new OsInfo + { + Name = osName, + Version = version, + VersionNumber = ParseVersionNumber(version) + }; + } + + /// + /// 解析设备信息 + /// + public static DeviceInfo ParseDevice(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return DeviceInfo.Unknown; + + var deviceType = DeviceType.Desktop; + string? vendor = null; + string? model = null; + + // 判断设备类型 + if (userAgent.Contains("Mobile", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPhone", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("Android", StringComparison.OrdinalIgnoreCase) && !userAgent.Contains("Tablet", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.Mobile; + } + else if (userAgent.Contains("Tablet", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPad", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.Tablet; + } + else if (userAgent.Contains("SmartTV", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("TV", StringComparison.OrdinalIgnoreCase)) + { + deviceType = DeviceType.TV; + } + + // 提取设备厂商 + var match = DeviceRegex.Match(userAgent); + if (match.Success) + { + var matched = match.Groups[1].Value.ToLowerInvariant(); + vendor = matched switch + { + "iphone" or "ipad" or "ipod" => "Apple", + "samsung" => "Samsung", + "huawei" => "Huawei", + "xiaomi" => "Xiaomi", + "oppo" => "OPPO", + "vivo" => "Vivo", + "oneplus" => "OnePlus", + "htc" => "HTC", + "motorola" => "Motorola", + "lg" => "LG", + "sony" => "Sony", + "nokia" => "Nokia", + "blackberry" => "BlackBerry", + "kindle" => "Amazon", + _ => char.ToUpperInvariant(matched[0]) + matched.Substring(1) + }; + } + + // 提取设备型号(简化处理) + var modelMatch = Regex.Match(userAgent, @"\(([^)]+)\)"); + if (modelMatch.Success) + { + var parts = modelMatch.Groups[1].Value.Split(';'); + foreach (var part in parts) + { + var trimmed = part.Trim(); + if (trimmed.Contains("Build") || trimmed.Contains(" ")) + { + model = trimmed.Split(' ')[0]; + break; + } + } + } + + return new DeviceInfo + { + Type = deviceType, + Vendor = vendor, + Model = model + }; + } + + /// + /// 判断是否为机器人/爬虫 + /// + public static bool IsBot(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return BotRegex.IsMatch(userAgent); + } + + /// + /// 判断是否为移动设备 + /// + public static bool IsMobile(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("Mobile", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("iPhone", StringComparison.OrdinalIgnoreCase) || + userAgent.Contains("Android", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 判断是否为微信内置浏览器 + /// + public static bool IsWeChat(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("MicroMessenger", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 判断是否为支付宝内置浏览器 + /// + public static bool IsAlipay(string? userAgent) + { + if (string.IsNullOrWhiteSpace(userAgent)) + return false; + + return userAgent.Contains("Alipay", StringComparison.OrdinalIgnoreCase); + } + + /// + /// 获取浏览器简短描述 + /// + public static string GetBrowserDescription(string? userAgent) + { + var info = Parse(userAgent); + var parts = new System.Collections.Generic.List(); + + if (info.Browser.Name != "Unknown") + { + parts.Add($"{info.Browser.Name} {info.Browser.Version}".Trim()); + } + + if (info.Os.Name != "Unknown") + { + parts.Add($"{info.Os.Name} {info.Os.Version}".Trim()); + } + + if (info.Device.Type != DeviceType.Desktop) + { + parts.Add(info.Device.Type.ToString()); + } + + return string.Join(" / ", parts); + } + + #region 私有方法 + + private static string ParseWindowsVersion(string version) + { + return version switch + { + "10.0" => "Windows 10/11", + "6.3" => "Windows 8.1", + "6.2" => "Windows 8", + "6.1" => "Windows 7", + "6.0" => "Windows Vista", + "5.1" or "5.2" => "Windows XP", + _ => $"Windows NT {version}" + }; + } + + private static Version ParseVersionNumber(string version) + { + if (string.IsNullOrEmpty(version)) + return new Version(0, 0); + + var parts = version.Split('.'); + var major = parts.Length > 0 && int.TryParse(parts[0], out var m) ? m : 0; + var minor = parts.Length > 1 && int.TryParse(parts[1], out var mi) ? mi : 0; + var build = parts.Length > 2 && int.TryParse(parts[2], out var b) ? b : 0; + + return new Version(major, minor, build); + } + + #endregion + } + + #region 数据类 + + /// + /// User-Agent 解析结果 + /// + public class UserAgentInfo + { + /// + /// 浏览器信息 + /// + public BrowserInfo Browser { get; set; } = BrowserInfo.Unknown; + + /// + /// 操作系统信息 + /// + public OsInfo Os { get; set; } = OsInfo.Unknown; + + /// + /// 设备信息 + /// + public DeviceInfo Device { get; set; } = DeviceInfo.Unknown; + + /// + /// 是否为机器人/爬虫 + /// + public bool IsBot { get; set; } + } + + /// + /// 浏览器信息 + /// + public class BrowserInfo + { + /// + /// 浏览器名称 + /// + public string Name { get; set; } = "Unknown"; + + /// + /// 版本字符串 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 版本号 + /// + public Version VersionNumber { get; set; } = new Version(0, 0); + + public static BrowserInfo Unknown => new(); + + public override string ToString() => $"{Name} {Version}".Trim(); + } + + /// + /// 操作系统信息 + /// + public class OsInfo + { + /// + /// 操作系统名称 + /// + public string Name { get; set; } = "Unknown"; + + /// + /// 版本字符串 + /// + public string Version { get; set; } = string.Empty; + + /// + /// 版本号 + /// + public Version VersionNumber { get; set; } = new Version(0, 0); + + public static OsInfo Unknown => new(); + + public override string ToString() => $"{Name} {Version}".Trim(); + } + + /// + /// 设备信息 + /// + public class DeviceInfo + { + /// + /// 设备类型 + /// + public DeviceType Type { get; set; } = DeviceType.Desktop; + + /// + /// 设备厂商 + /// + public string? Vendor { get; set; } + + /// + /// 设备型号 + /// + public string? Model { get; set; } + + public static DeviceInfo Unknown => new(); + + public override string ToString() + { + var parts = new System.Collections.Generic.List { Type.ToString() }; + if (!string.IsNullOrEmpty(Vendor)) parts.Add(Vendor); + if (!string.IsNullOrEmpty(Model)) parts.Add(Model); + return string.Join(" ", parts); + } + } + + /// + /// 设备类型 + /// + public enum DeviceType + { + /// + /// 桌面设备 + /// + Desktop, + + /// + /// 手机 + /// + Mobile, + + /// + /// 平板 + /// + Tablet, + + /// + /// 智能电视 + /// + TV, + + /// + /// 其他 + /// + Other + } + + #endregion +} diff --git a/EasyTool.Core/NetCategory/WebSocketUtil.cs b/EasyTool.Core/NetCategory/WebSocketUtil.cs new file mode 100644 index 0000000..d22cde3 --- /dev/null +++ b/EasyTool.Core/NetCategory/WebSocketUtil.cs @@ -0,0 +1,411 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// WebSocket客户端配置 + /// + public class WebSocketClientOptions + { + /// + /// 连接超时时间 + /// + public TimeSpan ConnectTimeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 接收缓冲区大小 + /// + public int ReceiveBufferSize { get; set; } = 8192; + + /// + /// 发送缓冲区大小 + /// + public int SendBufferSize { get; set; } = 8192; + + /// + /// 是否保持连接 + /// + public bool KeepAlive { get; set; } = true; + + /// + /// 保持连接间隔 + /// + public TimeSpan KeepAliveInterval { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 子协议 + /// + public List? SubProtocols { get; set; } + } + + /// + /// WebSocket消息 + /// + public class WebSocketMessage + { + /// + /// 消息类型 + /// + public WebSocketMessageType MessageType { get; set; } + + /// + /// 文本内容 + /// + public string? Text { get; set; } + + /// + /// 二进制内容 + /// + public byte[]? Binary { get; set; } + + /// + /// 是否为结束消息 + /// + public bool EndOfMessage { get; set; } = true; + } + + /// + /// WebSocket客户端 + /// + public class WebSocketClient : IDisposable + { + private readonly ClientWebSocket _webSocket; + private readonly WebSocketClientOptions _options; + private readonly CancellationTokenSource _cts; + private readonly ConcurrentQueue _sendQueue = new(); + private Task? _receiveTask; + private Task? _sendTask; + private bool _disposed; + + /// + /// 是否已连接 + /// + public bool IsConnected => _webSocket.State == WebSocketState.Open; + + /// + /// 当前状态 + /// + public WebSocketState State => _webSocket.State; + + /// + /// 接收到消息时触发 + /// + public event Action? OnMessage; + + /// + /// 连接关闭时触发 + /// + public event Action? OnClosed; + + /// + /// 发生错误时触发 + /// + public event Action? OnError; + + /// + /// 创建WebSocket客户端 + /// + /// 配置 + public WebSocketClient(WebSocketClientOptions? options = null) + { + _options = options ?? new WebSocketClientOptions(); + _webSocket = new ClientWebSocket(); + _cts = new CancellationTokenSource(); + + // 设置子协议 + if (_options.SubProtocols != null) + { + foreach (var protocol in _options.SubProtocols) + { + _webSocket.Options.AddSubProtocol(protocol); + } + } + + // 设置请求头 + foreach (var header in _options.Headers) + { + _webSocket.Options.SetRequestHeader(header.Key, header.Value); + } + + _webSocket.Options.KeepAliveInterval = _options.KeepAlive ? _options.KeepAliveInterval : TimeSpan.Zero; + } + + /// + /// 连接到服务器 + /// + /// 服务器地址 + /// 取消令牌 + public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken = default) + { + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cts.Token); + cts.CancelAfter(_options.ConnectTimeout); + + await _webSocket.ConnectAsync(uri, cts.Token); + + // 启动接收和发送任务 + _receiveTask = ReceiveLoopAsync(_cts.Token); + _sendTask = SendLoopAsync(_cts.Token); + } + + /// + /// 连接到服务器 + /// + /// 服务器地址 + /// 取消令牌 + public async Task ConnectAsync(string url, CancellationToken cancellationToken = default) + { + await ConnectAsync(new Uri(url), cancellationToken); + } + + /// + /// 发送文本消息 + /// + /// 消息内容 + public void Send(string message) + { + _sendQueue.Enqueue(new WebSocketMessage + { + MessageType = WebSocketMessageType.Text, + Text = message + }); + } + + /// + /// 发送二进制消息 + /// + /// 二进制数据 + public void Send(byte[] data) + { + _sendQueue.Enqueue(new WebSocketMessage + { + MessageType = WebSocketMessageType.Binary, + Binary = data + }); + } + + /// + /// 异步发送文本消息 + /// + /// 消息内容 + /// 取消令牌 + public async Task SendAsync(string message, CancellationToken cancellationToken = default) + { + var bytes = Encoding.UTF8.GetBytes(message); + await _webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, cancellationToken); + } + + /// + /// 异步发送二进制消息 + /// + /// 二进制数据 + /// 取消令牌 + public async Task SendAsync(byte[] data, CancellationToken cancellationToken = default) + { + await _webSocket.SendAsync(new ArraySegment(data), WebSocketMessageType.Binary, true, cancellationToken); + } + + /// + /// 关闭连接 + /// + /// 关闭状态 + /// 关闭原因 + /// 取消令牌 + public async Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.NormalClosure, string reason = "", CancellationToken cancellationToken = default) + { + if (_webSocket.State == WebSocketState.Open) + { + await _webSocket.CloseAsync(closeStatus, reason, cancellationToken); + } + _cts.Cancel(); + } + + private async Task ReceiveLoopAsync(CancellationToken cancellationToken) + { + var buffer = new byte[_options.ReceiveBufferSize]; + + try + { + while (!cancellationToken.IsCancellationRequested && _webSocket.State == WebSocketState.Open) + { + var result = await _webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken); + + if (result.MessageType == WebSocketMessageType.Close) + { + await CloseAsync(WebSocketCloseStatus.NormalClosure, "Server closed", cancellationToken); + OnClosed?.Invoke(result.CloseStatus, result.CloseStatusDescription); + break; + } + + var message = new WebSocketMessage + { + MessageType = result.MessageType, + EndOfMessage = result.EndOfMessage + }; + + if (result.MessageType == WebSocketMessageType.Text) + { + message.Text = Encoding.UTF8.GetString(buffer, 0, result.Count); + } + else + { + message.Binary = new byte[result.Count]; + Array.Copy(buffer, message.Binary, result.Count); + } + + OnMessage?.Invoke(message); + } + } + catch (OperationCanceledException) + { + // 正常取消 + } + catch (Exception ex) + { + OnError?.Invoke(ex); + } + } + + private async Task SendLoopAsync(CancellationToken cancellationToken) + { + try + { + while (!cancellationToken.IsCancellationRequested && _webSocket.State == WebSocketState.Open) + { + if (_sendQueue.TryDequeue(out var message)) + { + if (message.MessageType == WebSocketMessageType.Text && message.Text != null) + { + var bytes = Encoding.UTF8.GetBytes(message.Text); + await _webSocket.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, message.EndOfMessage, cancellationToken); + } + else if (message.MessageType == WebSocketMessageType.Binary && message.Binary != null) + { + await _webSocket.SendAsync(new ArraySegment(message.Binary), WebSocketMessageType.Binary, message.EndOfMessage, cancellationToken); + } + } + else + { + await Task.Delay(10, cancellationToken); + } + } + } + catch (OperationCanceledException) + { + // 正常取消 + } + catch (Exception ex) + { + OnError?.Invoke(ex); + } + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _cts.Cancel(); + _webSocket.Dispose(); + _cts.Dispose(); + } + } + } + + /// + /// WebSocket工具类 + /// + public static class WebSocketUtil + { + /// + /// 创建WebSocket客户端 + /// + /// 配置 + /// 客户端实例 + public static WebSocketClient CreateClient(WebSocketClientOptions? options = null) + { + return new WebSocketClient(options); + } + + /// + /// 连接并发送消息(一次性通信) + /// + /// 服务器地址 + /// 消息内容 + /// 超时时间 + /// 响应消息列表 + public static async Task> SendAndReceiveAsync(string url, string message, TimeSpan? timeout = null) + { + var responses = new List(); + var tcs = new TaskCompletionSource(); + + using var client = new WebSocketClient(); + client.OnMessage += msg => + { + responses.Add(msg); + if (msg.EndOfMessage) + { + tcs.TrySetResult(true); + } + }; + + await client.ConnectAsync(url); + + using var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(30)); + cts.Token.Register(() => tcs.TrySetCanceled()); + + client.Send(message); + + await Task.WhenAny(tcs.Task, Task.Delay(timeout ?? TimeSpan.FromSeconds(30))); + await client.CloseAsync(); + + return responses; + } + + /// + /// 检查WebSocket URL是否有效 + /// + /// URL字符串 + /// 是否有效 + public static bool IsValidWebSocketUrl(string url) + { + if (string.IsNullOrWhiteSpace(url)) + return false; + + if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) + return false; + + return uri.Scheme == "ws" || uri.Scheme == "wss"; + } + + /// + /// 获取WebSocket状态描述 + /// + /// 状态 + /// 状态描述 + public static string GetStateDescription(WebSocketState state) + { + return state switch + { + WebSocketState.None => "无连接", + WebSocketState.Connecting => "连接中", + WebSocketState.Open => "已连接", + WebSocketState.CloseSent => "已发送关闭请求", + WebSocketState.CloseReceived => "已接收关闭请求", + WebSocketState.Closed => "已关闭", + WebSocketState.Aborted => "已中止", + _ => "未知状态" + }; + } + } +} diff --git a/EasyTool.Core/NetCategory/WebhookUtil.cs b/EasyTool.Core/NetCategory/WebhookUtil.cs new file mode 100644 index 0000000..7ededcd --- /dev/null +++ b/EasyTool.Core/NetCategory/WebhookUtil.cs @@ -0,0 +1,511 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.NetCategory +{ + /// + /// Webhook配置 + /// + public class WebhookOptions + { + /// + /// Webhook URL + /// + public string Url { get; set; } = string.Empty; + + /// + /// HTTP方法(默认POST) + /// + public HttpMethod Method { get; set; } = HttpMethod.Post; + + /// + /// 请求头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 重试次数 + /// + public int MaxRetries { get; set; } = 3; + + /// + /// 重试延迟(毫秒) + /// + public int RetryDelayMs { get; set; } = 1000; + + /// + /// 是否验证SSL + /// + public bool ValidateSsl { get; set; } = true; + + /// + /// 成功响应回调 + /// + public Action? OnSuccess { get; set; } + + /// + /// 失败响应回调 + /// + public Action? OnFailure { get; set; } + } + + /// + /// Webhook响应 + /// + public class WebhookResponse + { + /// + /// 是否成功 + /// + public bool IsSuccess { get; set; } + + /// + /// HTTP状态码 + /// + public int StatusCode { get; set; } + + /// + /// 响应内容 + /// + public string? Content { get; set; } + + /// + /// 响应头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 请求耗时 + /// + public TimeSpan Duration { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + } + + /// + /// Webhook发送结果 + /// + public class WebhookResult + { + /// + /// 是否成功 + /// + public bool Success { get; set; } + + /// + /// 响应 + /// + public WebhookResponse? Response { get; set; } + + /// + /// 异常 + /// + public Exception? Exception { get; set; } + } + + /// + /// Webhook工具类 + /// + public static class WebhookUtil + { + private static readonly HttpClient _httpClient; + + static WebhookUtil() + { + _httpClient = new HttpClient(); + } + + /// + /// 发送Webhook + /// + /// 配置 + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(WebhookOptions options, object payload, CancellationToken cancellationToken = default) + { + var json = JsonSerializer.Serialize(payload); + return await SendAsync(options, json, "application/json", cancellationToken); + } + + /// + /// 发送Webhook + /// + /// 配置 + /// 内容 + /// 内容类型 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(WebhookOptions options, string content, string contentType = "application/json", CancellationToken cancellationToken = default) + { + var result = new WebhookResult(); + Exception? lastException = null; + WebhookResponse? lastResponse = null; + + for (int retry = 0; retry <= options.MaxRetries; retry++) + { + try + { + var response = await SendRequestAsync(options, content, contentType, cancellationToken); + response.RetryCount = retry; + lastResponse = response; + + if (response.IsSuccess) + { + result.Success = true; + result.Response = response; + options.OnSuccess?.Invoke(response); + return result; + } + } + catch (Exception ex) + { + lastException = ex; + } + + // 延迟重试 + if (retry < options.MaxRetries) + { + await Task.Delay(options.RetryDelayMs * (retry + 1), cancellationToken); + } + } + + result.Success = false; + result.Response = lastResponse; + result.Exception = lastException; + + if (lastException != null) + { + options.OnFailure?.Invoke(lastResponse ?? new WebhookResponse(), lastException); + } + + return result; + } + + private static async Task SendRequestAsync(WebhookOptions options, string content, string contentType, CancellationToken cancellationToken) + { + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + var response = new WebhookResponse(); + + try + { + using var request = new HttpRequestMessage(options.Method, options.Url); + request.Content = new StringContent(content, Encoding.UTF8, contentType); + + // 添加自定义请求头 + foreach (var header in options.Headers) + { + request.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + + // 配置HttpClient + using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cts.CancelAfter(options.Timeout); + + var handler = new HttpClientHandler(); + if (!options.ValidateSsl) + { + handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; + } + + using var client = new HttpClient(handler); + client.Timeout = options.Timeout; + + var httpResponse = await client.SendAsync(request, cts.Token); + stopwatch.Stop(); + + response.StatusCode = (int)httpResponse.StatusCode; + response.IsSuccess = httpResponse.IsSuccessStatusCode; +#if NETSTANDARD2_1 + response.Content = await httpResponse.Content.ReadAsStringAsync(); +#else + response.Content = await httpResponse.Content.ReadAsStringAsync(cancellationToken); +#endif + response.Duration = stopwatch.Elapsed; + + foreach (var header in httpResponse.Headers) + { + response.Headers[header.Key] = string.Join(",", header.Value); + } + } + catch (Exception ex) + { + stopwatch.Stop(); + response.IsSuccess = false; + response.ErrorMessage = ex.Message; + response.Duration = stopwatch.Elapsed; + } + + return response; + } + + /// + /// 发送JSON Webhook + /// + /// URL + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendJsonAsync(string url, object payload, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + return await SendAsync(options, payload, cancellationToken); + } + + /// + /// 发送文本Webhook + /// + /// URL + /// 内容 + /// 取消令牌 + /// 发送结果 + public static async Task SendTextAsync(string url, string content, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + return await SendAsync(options, content, "text/plain", cancellationToken); + } + + /// + /// 发送表单Webhook + /// + /// URL + /// 表单数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendFormAsync(string url, Dictionary formData, CancellationToken cancellationToken = default) + { + var options = new WebhookOptions { Url = url }; + var content = new FormUrlEncodedContent(formData); + var contentString = await content.ReadAsStringAsync(); + return await SendAsync(options, contentString, "application/x-www-form-urlencoded", cancellationToken); + } + } + + /// + /// Webhook客户端 + /// + public class WebhookClient + { + private readonly WebhookOptions _options; + + /// + /// 创建Webhook客户端 + /// + /// 配置 + public WebhookClient(WebhookOptions options) + { + _options = options; + } + + /// + /// 创建Webhook客户端 + /// + /// URL + public WebhookClient(string url) + { + _options = new WebhookOptions { Url = url }; + } + + /// + /// 发送Webhook + /// + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public async Task SendAsync(object payload, CancellationToken cancellationToken = default) + { + return await WebhookUtil.SendAsync(_options, payload, cancellationToken); + } + + /// + /// 发送Webhook + /// + /// 内容 + /// 内容类型 + /// 取消令牌 + /// 发送结果 + public async Task SendAsync(string content, string contentType = "application/json", CancellationToken cancellationToken = default) + { + return await WebhookUtil.SendAsync(_options, content, contentType, cancellationToken); + } + + /// + /// 添加请求头 + /// + /// 键 + /// 值 + /// this + public WebhookClient WithHeader(string key, string value) + { + _options.Headers[key] = value; + return this; + } + + /// + /// 设置超时时间 + /// + /// 超时时间 + /// this + public WebhookClient WithTimeout(TimeSpan timeout) + { + _options.Timeout = timeout; + return this; + } + + /// + /// 设置重试次数 + /// + /// 最大重试次数 + /// this + public WebhookClient WithRetry(int maxRetries) + { + _options.MaxRetries = maxRetries; + return this; + } + + /// + /// 设置成功回调 + /// + /// 回调 + /// this + public WebhookClient OnSuccess(Action onSuccess) + { + _options.OnSuccess = onSuccess; + return this; + } + + /// + /// 设置失败回调 + /// + /// 回调 + /// this + public WebhookClient OnFailure(Action onFailure) + { + _options.OnFailure = onFailure; + return this; + } + } + + /// + /// Webhook管理器 + /// + public static class WebhookManager + { + private static readonly Dictionary _webhooks = new(); + + /// + /// 注册Webhook + /// + /// 名称 + /// 配置 + public static void Register(string name, WebhookOptions options) + { + _webhooks[name] = options; + } + + /// + /// 注册Webhook + /// + /// 名称 + /// URL + public static void Register(string name, string url) + { + _webhooks[name] = new WebhookOptions { Url = url }; + } + + /// + /// 获取Webhook配置 + /// + /// 名称 + /// 配置 + public static WebhookOptions? Get(string name) + { + return _webhooks.TryGetValue(name, out var options) ? options : null; + } + + /// + /// 移除Webhook + /// + /// 名称 + /// 是否成功移除 + public static bool Remove(string name) + { + return _webhooks.Remove(name); + } + + /// + /// 发送Webhook + /// + /// 名称 + /// 负载数据 + /// 取消令牌 + /// 发送结果 + public static async Task SendAsync(string name, object payload, CancellationToken cancellationToken = default) + { + var options = Get(name); + if (options == null) + { + return new WebhookResult + { + Success = false, + Exception = new Exception($"未找到名为 '{name}' 的Webhook配置") + }; + } + + return await WebhookUtil.SendAsync(options, payload, cancellationToken); + } + + /// + /// 发送到所有Webhook + /// + /// 负载数据 + /// 取消令牌 + /// 所有结果 + public static async Task> SendToAllAsync(object payload, CancellationToken cancellationToken = default) + { + var results = new Dictionary(); + + foreach (var kvp in _webhooks) + { + results[kvp.Key] = await WebhookUtil.SendAsync(kvp.Value, payload, cancellationToken); + } + + return results; + } + + /// + /// 获取所有已注册的Webhook名称 + /// + /// 名称列表 + public static IEnumerable GetNames() + { + return _webhooks.Keys; + } + + /// + /// 清空所有Webhook + /// + public static void Clear() + { + _webhooks.Clear(); + } + } +} diff --git a/EasyTool.Core/Options.cs b/EasyTool.Core/Options.cs new file mode 100644 index 0000000..f588a2c --- /dev/null +++ b/EasyTool.Core/Options.cs @@ -0,0 +1,276 @@ +using System; + +namespace EasyTool +{ + /// + /// 限流器配置选项 + /// + public class RateLimiterOptions + { + /// + /// 限流算法 + /// + public ToolCategory.RateLimitAlgorithm Algorithm { get; set; } = ToolCategory.RateLimitAlgorithm.TokenBucket; + + /// + /// 限制数量 + /// + public int Limit { get; set; } = 100; + + /// + /// 时间窗口 + /// + public TimeSpan Window { get; set; } = TimeSpan.FromSeconds(1); + + /// + /// 令牌桶容量(仅 TokenBucket 算法) + /// + public int? Capacity { get; set; } + + /// + /// 令牌补充速率(仅 TokenBucket 算法) + /// + public int? RefillRate { get; set; } + } + + /// + /// 熔断器配置选项 + /// + public class CircuitBreakerOptions + { + /// + /// 失败阈值次数 + /// + public int FailureThreshold { get; set; } = 5; + + /// + /// 成功阈值次数(半开状态) + /// + public int SuccessThreshold { get; set; } = 2; + + /// + /// 打开状态持续时间 + /// + public TimeSpan OpenDuration { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10); + } + + /// + /// 重试配置选项 + /// + public class RetryOptions + { + /// + /// 最大重试次数 + /// + public int MaxRetries { get; set; } = 3; + + /// + /// 重试延迟 + /// + public TimeSpan Delay { get; set; } = TimeSpan.FromSeconds(1); + + /// + /// 最大延迟(指数退避) + /// + public TimeSpan MaxDelay { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 是否使用指数退避 + /// + public bool UseExponentialBackoff { get; set; } = true; + + /// + /// 退避倍数 + /// + public double BackoffMultiplier { get; set; } = 2.0; + } + + /// + /// HTTP 客户端配置选项 + /// + public class HttpClientOptions + { + /// + /// 基础地址 + /// + public string? BaseAddress { get; set; } + + /// + /// 超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 最大响应内容缓冲区大小 + /// + public long MaxResponseContentBufferSize { get; set; } = int.MaxValue; + + /// + /// 是否自动解压缩 + /// + public bool EnableAutoDecompression { get; set; } = true; + + /// + /// 是否忽略 SSL 错误 + /// + public bool IgnoreSslErrors { get; set; } + + /// + /// 默认请求头 + /// + public System.Collections.Generic.Dictionary DefaultHeaders { get; set; } = new(); + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } = 0; + + /// + /// 重试延迟 + /// + public TimeSpan RetryDelay { get; set; } = TimeSpan.FromSeconds(1); + } + + /// + /// 文件监控配置选项 + /// + public class FileWatcherOptions + { + /// + /// 监控路径 + /// + public string Path { get; set; } = string.Empty; + + /// + /// 文件过滤模式 + /// + public string Filter { get; set; } = "*.*"; + + /// + /// 是否包含子目录 + /// + public bool IncludeSubdirectories { get; set; } = true; + + /// + /// 是否监控文件名变更 + /// + public bool EnableRaisingEvents { get; set; } = true; + + /// + /// 内部缓冲区大小 + /// + public int InternalBufferSize { get; set; } = 8192; + + /// + /// 通知过滤器 + /// + public System.IO.NotifyFilters NotifyFilter { get; set; } = + System.IO.NotifyFilters.FileName | + System.IO.NotifyFilters.DirectoryName | + System.IO.NotifyFilters.LastWrite; + } + + /// + /// 日志配置选项 + /// + public class LogOptions + { + /// + /// 最小日志级别 + /// + public LogLevel MinimumLevel { get; set; } = LogLevel.Information; + + /// + /// 是否输出到控制台 + /// + public bool WriteToConsole { get; set; } = true; + + /// + /// 是否输出到文件 + /// + public bool WriteToFile { get; set; } + + /// + /// 日志文件路径 + /// + public string? LogFilePath { get; set; } + + /// + /// 日志文件滚动间隔 + /// + public RollingInterval RollingInterval { get; set; } = RollingInterval.Day; + + /// + /// 日志文件最大大小(字节) + /// + public long? MaxFileSize { get; set; } + + /// + /// 保留日志文件数量 + /// + public int? RetainedFileCount { get; set; } + + /// + /// 输出模板 + /// + public string OutputTemplate { get; set; } = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; + } + + /// + /// 日志级别 + /// + public enum LogLevel + { + Trace = 0, + Debug = 1, + Information = 2, + Warning = 3, + Error = 4, + Critical = 5, + None = 6 + } + + /// + /// 日志文件滚动间隔 + /// + public enum RollingInterval + { + Infinite = 0, + Year = 1, + Month = 2, + Day = 3, + Hour = 4, + Minute = 5 + } + + /// + /// 对象池配置选项 + /// + public class ObjectPoolOptions + { + /// + /// 最大容量 + /// + public int MaximumCapacity { get; set; } = 1024; + + /// + /// 初始容量 + /// + public int InitialCapacity { get; set; } = 10; + + /// + /// 对象最大闲置时间 + /// + public TimeSpan MaxIdleTime { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 清理间隔 + /// + public TimeSpan CleanupInterval { get; set; } = TimeSpan.FromMinutes(1); + } +} diff --git a/EasyTool.Core/QueueCategory/ChannelUtil.cs b/EasyTool.Core/QueueCategory/ChannelUtil.cs new file mode 100644 index 0000000..c76432c --- /dev/null +++ b/EasyTool.Core/QueueCategory/ChannelUtil.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Channels; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// Channel 工具类 + /// 提供线程安全的生产者-消费者队列实现 + /// + public static class ChannelUtil + { + /// + /// 创建无界 Channel + /// + /// 元素类型 + /// Channel 实例 + public static Channel CreateUnbounded() + { + return Channel.CreateUnbounded(); + } + + /// + /// 创建无界 Channel(带选项) + /// + /// 元素类型 + /// Channel 选项 + /// Channel 实例 + public static Channel CreateUnbounded(UnboundedChannelOptions options) + { + return Channel.CreateUnbounded(options); + } + + /// + /// 创建有界 Channel + /// + /// 元素类型 + /// 容量 + /// Channel 实例 + public static Channel CreateBounded(int capacity) + { + return Channel.CreateBounded(capacity); + } + + /// + /// 创建有界 Channel(带选项) + /// + /// 元素类型 + /// Channel 选项 + /// Channel 实例 + public static Channel CreateBounded(BoundedChannelOptions options) + { + return Channel.CreateBounded(options); + } + + /// + /// 批量写入数据 + /// + /// 元素类型 + /// Channel 实例 + /// 数据集合 + /// 取消令牌 + public static async Task WriteManyAsync(Channel channel, IEnumerable items, CancellationToken cancellationToken = default) + { + foreach (var item in items) + { + await channel.Writer.WriteAsync(item, cancellationToken); + } + } + + /// + /// 批量读取数据 + /// + /// 元素类型 + /// Channel 实例 + /// 读取数量 + /// 取消令牌 + /// 数据列表 + public static async Task> ReadManyAsync(Channel channel, int count, CancellationToken cancellationToken = default) + { + var result = new List(); + + for (int i = 0; i < count; i++) + { + if (await channel.Reader.WaitToReadAsync(cancellationToken)) + { + if (channel.Reader.TryRead(out var item)) + { + result.Add(item); + } + } + else + { + break; + } + } + + return result; + } + + /// + /// 读取所有数据直到完成 + /// + /// 元素类型 + /// Channel 实例 + /// 取消令牌 + /// 数据列表 + public static async Task> ReadAllAsync(Channel channel, CancellationToken cancellationToken = default) + { + var result = new List(); + + await foreach (var item in channel.Reader.ReadAllAsync(cancellationToken)) + { + result.Add(item); + } + + return result; + } + + /// + /// 创建异步生产者-消费者处理器 + /// + /// 元素类型 + /// 容量(null 表示无界) + /// 处理函数 + /// 消费者数量 + /// 取消令牌 + /// 生产者写入器和完成任务 + public static (ChannelWriter Writer, Task Completion) CreateProcessor( + int? capacity, + Func processAction, + int consumerCount = 1, + CancellationToken cancellationToken = default) + { + var channel = capacity.HasValue + ? Channel.CreateBounded(capacity.Value) + : Channel.CreateUnbounded(); + + var consumers = new Task[consumerCount]; + + for (int i = 0; i < consumerCount; i++) + { + consumers[i] = ConsumeAsync(channel.Reader, processAction, cancellationToken); + } + + var completion = Task.WhenAll(consumers); + + return (channel.Writer, completion); + } + + /// + /// 创建带批处理的消费者 + /// + /// 元素类型 + /// 容量 + /// 批处理大小 + /// 批处理超时 + /// 处理函数 + /// 取消令牌 + /// 生产者写入器和完成任务 + public static (ChannelWriter Writer, Task Completion) CreateBatchProcessor( + int capacity, + int batchSize, + TimeSpan batchTimeout, + Func, Task> processAction, + CancellationToken cancellationToken = default) + { + var channel = Channel.CreateBounded(new BoundedChannelOptions(capacity) + { + FullMode = BoundedChannelFullMode.Wait + }); + + var completion = ProcessBatchAsync(channel.Reader, batchSize, batchTimeout, processAction, cancellationToken); + + return (channel.Writer, completion); + } + + private static async Task ConsumeAsync(ChannelReader reader, Func processAction, CancellationToken cancellationToken) + { + await foreach (var item in reader.ReadAllAsync(cancellationToken)) + { + await processAction(item); + } + } + + private static async Task ProcessBatchAsync( + ChannelReader reader, + int batchSize, + TimeSpan batchTimeout, + Func, Task> processAction, + CancellationToken cancellationToken) + { + var batch = new List(batchSize); + + while (await reader.WaitToReadAsync(cancellationToken)) + { + batch.Clear(); + + // 尝试收集一批数据 + while (batch.Count < batchSize && reader.TryRead(out var item)) + { + batch.Add(item); + } + + if (batch.Count > 0) + { + await processAction(batch); + } + } + + // 处理剩余数据 + if (batch.Count > 0) + { + await processAction(batch); + } + } + } + + /// + /// 异步队列 + /// 提供简单的异步队列操作封装 + /// + /// 元素类型 + public class AsyncQueue : IDisposable + { + private readonly Channel _channel; + private bool _disposed; + + /// + /// 获取当前队列长度 + /// + public int Count => _channel.Reader.Count; + + /// + /// 是否完成写入 + /// + public bool IsCompleted => _channel.Reader.Completion.IsCompleted; + + /// + /// 创建异步队列(无界) + /// + public AsyncQueue() + { + _channel = Channel.CreateUnbounded(); + } + + /// + /// 创建异步队列(有界) + /// + /// 容量 + /// 满时策略 + public AsyncQueue(int capacity, BoundedChannelFullMode fullMode = BoundedChannelFullMode.Wait) + { + _channel = Channel.CreateBounded(new BoundedChannelOptions(capacity) + { + FullMode = fullMode + }); + } + + /// + /// 入队 + /// + /// 元素 + /// 取消令牌 + public async ValueTask EnqueueAsync(T item, CancellationToken cancellationToken = default) + { + await _channel.Writer.WriteAsync(item, cancellationToken); + } + + /// + /// 入队(同步) + /// + /// 元素 + /// 是否成功 + public bool Enqueue(T item) + { + return _channel.Writer.TryWrite(item); + } + + /// + /// 出队 + /// + /// 取消令牌 + /// 元素 + public async ValueTask DequeueAsync(CancellationToken cancellationToken = default) + { + return await _channel.Reader.ReadAsync(cancellationToken); + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 是否成功 + public bool TryDequeue(out T? item) + { + return _channel.Reader.TryRead(out item); + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 是否成功 + public bool TryPeek(out T? item) + { + return _channel.Reader.TryPeek(out item); + } + + /// + /// 等待有数据可读 + /// + /// 取消令牌 + /// 是否有数据 + public ValueTask WaitToReadAsync(CancellationToken cancellationToken = default) + { + return _channel.Reader.WaitToReadAsync(cancellationToken); + } + + /// + /// 获取所有数据(异步迭代) + /// + /// 取消令牌 + /// 异步迭代器 + public IAsyncEnumerable ReadAllAsync(CancellationToken cancellationToken = default) + { + return _channel.Reader.ReadAllAsync(cancellationToken); + } + + /// + /// 完成写入 + /// + public void Complete() + { + _channel.Writer.Complete(); + } + + /// + /// 等待完成 + /// + /// 完成任务 + public Task Completion => _channel.Reader.Completion; + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _channel.Writer.TryComplete(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/QueueCategory/DelayQueue.cs b/EasyTool.Core/QueueCategory/DelayQueue.cs new file mode 100644 index 0000000..98b11ab --- /dev/null +++ b/EasyTool.Core/QueueCategory/DelayQueue.cs @@ -0,0 +1,313 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 延迟队列项 + /// + /// 元素类型 + internal class DelayQueueItem + { + public T Value { get; set; } = default!; + public DateTime ExecuteTime { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); + } + + /// + /// 延迟队列 + /// 支持按时间延迟执行任务 + /// + /// 元素类型 + public class DelayQueue : IDisposable + { + private readonly ConcurrentDictionary> _items; + private readonly List> _sortedItems; + private readonly SemaphoreSlim _signal; + private readonly CancellationTokenSource _cts; + private readonly Task _processTask; + private readonly object _lock = new(); + private bool _disposed; + + /// + /// 获取队列长度 + /// + public int Count => _items.Count; + + /// + /// 是否为空 + /// + public bool IsEmpty => _items.IsEmpty; + + /// + /// 创建延迟队列 + /// + public DelayQueue() + { + _items = new ConcurrentDictionary>(); + _sortedItems = new List>(); + _signal = new SemaphoreSlim(0); + _cts = new CancellationTokenSource(); + _processTask = ProcessAsync(_cts.Token); + } + + /// + /// 添加延迟元素 + /// + /// 元素值 + /// 延迟时间 + /// 元素ID,可用于取消 + public Guid Add(T value, TimeSpan delay) + { + return Add(value, DateTime.UtcNow.Add(delay)); + } + + /// + /// 添加延迟元素 + /// + /// 元素值 + /// 执行时间 + /// 元素ID,可用于取消 + public Guid Add(T value, DateTime executeTime) + { + var item = new DelayQueueItem + { + Value = value, + ExecuteTime = executeTime + }; + + lock (_lock) + { + _items[item.Id] = item; + InsertSorted(_sortedItems, item); + } + + _signal.Release(); + return item.Id; + } + + /// + /// 尝试取消元素 + /// + /// 元素ID + /// 是否取消成功 + public bool TryCancel(Guid id) + { + lock (_lock) + { + if (_items.TryRemove(id, out var item)) + { + _sortedItems.Remove(item); + return true; + } + } + + return false; + } + + /// + /// 异步等待并获取到期元素 + /// + /// 取消令牌 + /// 到期元素 + public async Task TakeAsync(CancellationToken cancellationToken = default) + { + while (!cancellationToken.IsCancellationRequested) + { + lock (_lock) + { + if (_sortedItems.Count > 0) + { + var first = _sortedItems[0]; + var now = DateTime.UtcNow; + + if (now >= first.ExecuteTime) + { + _sortedItems.RemoveAt(0); + _items.TryRemove(first.Id, out _); + return first.Value; + } + } + } + + // 计算等待时间 + var waitTime = GetWaitTime(); + + if (waitTime > TimeSpan.Zero) + { + await Task.Delay(waitTime, cancellationToken); + } + } + + throw new OperationCanceledException(); + } + + /// + /// 尝试获取到期元素(非阻塞) + /// + /// 元素值 + /// 是否成功获取 + public bool TryTake(out T? value) + { + value = default; + + lock (_lock) + { + if (_sortedItems.Count > 0) + { + var first = _sortedItems[0]; + + if (DateTime.UtcNow >= first.ExecuteTime) + { + _sortedItems.RemoveAt(0); + _items.TryRemove(first.Id, out _); + value = first.Value; + return true; + } + } + } + + return false; + } + + /// + /// 尝试在指定时间内获取到期元素 + /// + /// 超时时间 + /// 元素值 + /// 是否成功获取 + public bool TryTake(TimeSpan timeout, out T? value) + { + value = default; + var endTime = DateTime.UtcNow.Add(timeout); + + while (DateTime.UtcNow < endTime) + { + if (TryTake(out value)) + { + return true; + } + + var remaining = endTime - DateTime.UtcNow; + if (remaining <= TimeSpan.Zero) + break; + + var waitTime = GetWaitTime(); + if (waitTime > TimeSpan.Zero) + { + Thread.Sleep((int)Math.Min(waitTime.TotalMilliseconds, remaining.TotalMilliseconds)); + } + } + + return false; + } + + /// + /// 获取所有元素(不等待到期) + /// + /// 元素列表 + public List<(T Value, DateTime ExecuteTime)> GetAll() + { + lock (_lock) + { + return _sortedItems + .Select(i => (i.Value, i.ExecuteTime)) + .ToList(); + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + _items.Clear(); + _sortedItems.Clear(); + } + } + + /// + /// 创建处理器 + /// + /// 处理函数 + /// 取消令牌 + /// 处理任务 + public Task ProcessAsync(Func handler, CancellationToken cancellationToken = default) + { + return Task.Run(async () => + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var value = await TakeAsync(cancellationToken); + await handler(value); + } + catch (OperationCanceledException) + { + break; + } + } + }, cancellationToken); + } + + private TimeSpan GetWaitTime() + { + lock (_lock) + { + if (_sortedItems.Count == 0) + return TimeSpan.FromSeconds(1); + + var first = _sortedItems[0]; + var waitTime = first.ExecuteTime - DateTime.UtcNow; + return waitTime > TimeSpan.Zero ? waitTime : TimeSpan.Zero; + } + } + + private void InsertSorted(List> list, DelayQueueItem item) + { + var index = list.BinarySearch(item, Comparer>.Create((a, b) => + a.ExecuteTime.CompareTo(b.ExecuteTime))); + + if (index < 0) + index = ~index; + + list.Insert(index, item); + } + + private async Task ProcessAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await _signal.WaitAsync(cancellationToken); + } + catch (OperationCanceledException) + { + break; + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cts.Cancel(); + _signal.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } + } +} diff --git a/EasyTool.Core/QueueCategory/MessageQueueUtil.cs b/EasyTool.Core/QueueCategory/MessageQueueUtil.cs new file mode 100644 index 0000000..61d5d7a --- /dev/null +++ b/EasyTool.Core/QueueCategory/MessageQueueUtil.cs @@ -0,0 +1,543 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 消息类型 + /// + public enum MessageType + { + /// + /// 普通消息 + /// + Normal, + + /// + /// 延迟消息 + /// + Delayed, + + /// + /// 优先级消息 + /// + Priority + } + + /// + /// 消息封装 + /// + /// 消息体类型 + public class Message + { + /// + /// 消息ID + /// + public string Id { get; set; } = Guid.NewGuid().ToString("N"); + + /// + /// 消息体 + /// + public T Body { get; set; } = default!; + + /// + /// 创建时间 + /// + public DateTime CreateTime { get; set; } = DateTime.UtcNow; + + /// + /// 过期时间 + /// + public DateTime? ExpireTime { get; set; } + + /// + /// 延迟执行时间 + /// + public DateTime? DelayTo { get; set; } + + /// + /// 优先级(越大越优先) + /// + public int Priority { get; set; } + + /// + /// 重试次数 + /// + public int RetryCount { get; set; } + + /// + /// 最大重试次数 + /// + public int MaxRetryCount { get; set; } = 3; + + /// + /// 消息头 + /// + public Dictionary Headers { get; set; } = new(); + + /// + /// 是否已过期 + /// + public bool IsExpired => ExpireTime.HasValue && DateTime.UtcNow >= ExpireTime.Value; + + /// + /// 是否可以处理(延迟消息检查) + /// + public bool CanProcess => DelayTo == null || DateTime.UtcNow >= DelayTo.Value; + } + + /// + /// 消息队列选项 + /// + public class MessageQueueOptions + { + /// + /// 队列名称 + /// + public string Name { get; set; } = "default"; + + /// + /// 最大容量 + /// + public int MaxCapacity { get; set; } = 10000; + + /// + /// 消费者数量 + /// + public int ConsumerCount { get; set; } = 1; + + /// + /// 默认消息过期时间 + /// + public TimeSpan? DefaultMessageTtl { get; set; } + + /// + /// 默认重试延迟 + /// + public TimeSpan DefaultRetryDelay { get; set; } = TimeSpan.FromSeconds(5); + + /// + /// 死信队列是否启用 + /// + public bool EnableDeadLetterQueue { get; set; } = true; + } + + /// + /// 内存消息队列 + /// + /// 消息体类型 + public class MessageQueue : IDisposable + { + private readonly MessageQueueOptions _options; + private readonly ConcurrentQueue> _normalQueue; + private readonly PriorityQueue, int> _priorityQueue; + private readonly ConcurrentQueue> _delayedQueue; + private readonly ConcurrentQueue> _deadLetterQueue; + private readonly SemaphoreSlim _signal; + private readonly CancellationTokenSource _cts; + private readonly List _consumerTasks; + private readonly Func, Task> _handler; + private bool _disposed; + private bool _started; + + /// + /// 队列名称 + /// + public string Name => _options.Name; + + /// + /// 普通队列长度 + /// + public int NormalQueueCount => _normalQueue.Count; + + /// + /// 优先级队列长度 + /// + public int PriorityQueueCount => _priorityQueue.Count; + + /// + /// 延迟队列长度 + /// + public int DelayedQueueCount => _delayedQueue.Count; + + /// + /// 死信队列长度 + /// + public int DeadLetterQueueCount => _deadLetterQueue.Count; + + /// + /// 创建消息队列 + /// + /// 消息处理器 + /// 队列选项 + public MessageQueue(Func, Task> handler, MessageQueueOptions? options = null) + { + _options = options ?? new MessageQueueOptions(); + _handler = handler; + _normalQueue = new ConcurrentQueue>(); + _priorityQueue = new PriorityQueue, int>(); + _delayedQueue = new ConcurrentQueue>(); + _deadLetterQueue = new ConcurrentQueue>(); + _signal = new SemaphoreSlim(0); + _cts = new CancellationTokenSource(); + _consumerTasks = new List(); + } + + /// + /// 启动队列消费者 + /// + public void Start() + { + if (_started) + return; + + _started = true; + + for (int i = 0; i < _options.ConsumerCount; i++) + { + _consumerTasks.Add(ConsumeAsync(_cts.Token)); + } + + // 启动延迟消息检查任务 + _consumerTasks.Add(ProcessDelayedMessagesAsync(_cts.Token)); + } + + /// + /// 停止队列消费者 + /// + /// 是否等待处理完成 + public async Task StopAsync(bool waitForCompletion = true) + { + _cts.Cancel(); + + if (waitForCompletion) + { + await Task.WhenAll(_consumerTasks); + } + } + + /// + /// 发布消息 + /// + /// 消息体 + /// 消息类型 + /// 优先级 + /// 延迟时间 + /// 消息ID + public string Publish(T body, MessageType type = MessageType.Normal, int priority = 0, TimeSpan? delay = null) + { + var message = new Message + { + Body = body, + Priority = priority, + ExpireTime = _options.DefaultMessageTtl.HasValue + ? DateTime.UtcNow.Add(_options.DefaultMessageTtl.Value) + : null + }; + + if (delay.HasValue) + { + message.DelayTo = DateTime.UtcNow.Add(delay.Value); + type = MessageType.Delayed; + } + + switch (type) + { + case MessageType.Priority: + _priorityQueue.Enqueue(message, -priority); // 负数让高优先级先出 + break; + case MessageType.Delayed: + _delayedQueue.Enqueue(message); + break; + default: + _normalQueue.Enqueue(message); + break; + } + + _signal.Release(); + return message.Id; + } + + /// + /// 批量发布消息 + /// + /// 消息体集合 + /// 消息ID列表 + public List PublishMany(IEnumerable bodies) + { + var ids = new List(); + + foreach (var body in bodies) + { + ids.Add(Publish(body)); + } + + return ids; + } + + /// + /// 获取死信队列消息 + /// + /// 消息列表 + public List> GetDeadLetterMessages() + { + var messages = new List>(); + + while (_deadLetterQueue.TryDequeue(out var message)) + { + messages.Add(message); + } + + return messages; + } + + /// + /// 重试死信消息 + /// + public void RetryDeadLetterMessages() + { + var messages = GetDeadLetterMessages(); + + foreach (var message in messages) + { + message.RetryCount = 0; + Publish(message.Body); + } + } + + private async Task ConsumeAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await _signal.WaitAsync(cancellationToken); + + Message? message = null; + + // 优先处理优先级队列 + if (_priorityQueue.TryDequeue(out var priorityMessage, out _)) + { + message = priorityMessage; + } + // 再处理普通队列 + else if (_normalQueue.TryDequeue(out var normalMessage)) + { + message = normalMessage; + } + + if (message == null) + continue; + + // 检查是否过期 + if (message.IsExpired) + continue; + + // 处理消息 + var result = await _handler(message); + + switch (result.Action) + { + case ProcessAction.Complete: + // 消息处理完成,无需操作 + break; + + case ProcessAction.Retry: + message.RetryCount++; + if (message.RetryCount < message.MaxRetryCount) + { + await Task.Delay(_options.DefaultRetryDelay, cancellationToken); + Publish(message.Body, MessageType.Normal); + } + else if (_options.EnableDeadLetterQueue) + { + _deadLetterQueue.Enqueue(message); + } + break; + + case ProcessAction.DeadLetter: + if (_options.EnableDeadLetterQueue) + { + _deadLetterQueue.Enqueue(message); + } + break; + } + } + catch (OperationCanceledException) + { + break; + } + catch (Exception) + { + // 记录错误,继续处理 + } + } + } + + private async Task ProcessDelayedMessagesAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); + + var now = DateTime.UtcNow; + var readyMessages = new List>(); + + // 检查延迟消息 + while (_delayedQueue.TryDequeue(out var message)) + { + if (message.CanProcess && !message.IsExpired) + { + readyMessages.Add(message); + } + else if (!message.IsExpired) + { + // 未到处理时间,重新入队 + _delayedQueue.Enqueue(message); + break; + } + } + + // 将就绪的消息发送到普通队列 + foreach (var message in readyMessages) + { + _normalQueue.Enqueue(message); + _signal.Release(); + } + } + catch (OperationCanceledException) + { + break; + } + } + } + + /// + /// 释放资源 + /// + public void Dispose() + { + if (!_disposed) + { + _cts.Cancel(); + _signal.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } + } + + /// + /// 处理结果 + /// + public class ProcessResult + { + /// + /// 处理动作 + /// + public ProcessAction Action { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 创建完成结果 + /// + public static ProcessResult Complete => new() { Action = ProcessAction.Complete }; + + /// + /// 创建重试结果 + /// + public static ProcessResult Retry => new() { Action = ProcessAction.Retry }; + + /// + /// 创建死信结果 + /// + public static ProcessResult DeadLetter => new() { Action = ProcessAction.DeadLetter }; + + /// + /// 创建错误结果 + /// + public static ProcessResult Error(string message) => new() { Action = ProcessAction.Retry, ErrorMessage = message }; + } + + /// + /// 处理动作 + /// + public enum ProcessAction + { + /// + /// 完成 + /// + Complete, + + /// + /// 重试 + /// + Retry, + + /// + /// 死信 + /// + DeadLetter + } + + /// + /// 消息队列工具类 + /// + public static class MessageQueueUtil + { + private static readonly ConcurrentDictionary _queues = new(); + + /// + /// 创建或获取消息队列 + /// + /// 消息体类型 + /// 队列名称 + /// 消息处理器 + /// 队列选项 + /// 消息队列 + public static MessageQueue GetOrCreate( + string name, + Func, Task> handler, + MessageQueueOptions? options = null) + { + options ??= new MessageQueueOptions { Name = name }; + + return (MessageQueue)_queues.GetOrAdd(name, _ => new MessageQueue(handler, options)); + } + + /// + /// 移除消息队列 + /// + /// 队列名称 + public static void Remove(string name) + { + if (_queues.TryRemove(name, out var queue)) + { + (queue as IDisposable)?.Dispose(); + } + } + + /// + /// 清空所有队列 + /// + public static void ClearAll() + { + foreach (var queue in _queues.Values) + { + (queue as IDisposable)?.Dispose(); + } + + _queues.Clear(); + } + } +} diff --git a/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs b/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs new file mode 100644 index 0000000..ea6219d --- /dev/null +++ b/EasyTool.Core/QueueCategory/PriorityQueue.netstandard.cs @@ -0,0 +1,211 @@ +#if NETSTANDARD2_1 +using System; +using System.Collections; +using System.Collections.Generic; + +namespace System.Collections.Generic +{ + /// + /// 优先队列 polyfill for netstandard2.1 + /// + /// 元素类型 + /// 优先级类型 + public class PriorityQueue + { + private readonly List<(TElement Element, TPriority Priority)> _items; + private readonly IComparer? _comparer; + + /// + /// 获取队列中的元素数量 + /// + public int Count => _items.Count; + + /// + /// 创建优先队列 + /// + public PriorityQueue() + { + _items = new List<(TElement, TPriority)>(); + _comparer = null; + } + + /// + /// 创建优先队列 + /// + /// 初始容量 + public PriorityQueue(int initialCapacity) + { + _items = new List<(TElement, TPriority)>(initialCapacity); + _comparer = null; + } + + /// + /// 创建优先队列 + /// + /// 优先级比较器 + public PriorityQueue(IComparer? comparer) + { + _items = new List<(TElement, TPriority)>(); + _comparer = comparer; + } + + /// + /// 创建优先队列 + /// + /// 初始容量 + /// 优先级比较器 + public PriorityQueue(int initialCapacity, IComparer? comparer) + { + _items = new List<(TElement, TPriority)>(initialCapacity); + _comparer = comparer; + } + + /// + /// 入队 + /// + /// 元素 + /// 优先级 + public void Enqueue(TElement element, TPriority priority) + { + _items.Add((element, priority)); + HeapifyUp(_items.Count - 1); + } + + /// + /// 出队 + /// + /// 元素 + public TElement Dequeue() + { + if (_items.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + var result = _items[0].Element; + var lastIndex = _items.Count - 1; + _items[0] = _items[lastIndex]; + _items.RemoveAt(lastIndex); + + if (_items.Count > 0) + HeapifyDown(0); + + return result; + } + + /// + /// 尝试出队 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryDequeue(out TElement element, out TPriority priority) + { + if (_items.Count == 0) + { + element = default!; + priority = default!; + return false; + } + + var item = _items[0]; + element = item.Element; + priority = item.Priority; + + var lastIndex = _items.Count - 1; + _items[0] = _items[lastIndex]; + _items.RemoveAt(lastIndex); + + if (_items.Count > 0) + HeapifyDown(0); + + return true; + } + + /// + /// 查看队首元素 + /// + /// 元素 + public TElement Peek() + { + if (_items.Count == 0) + throw new InvalidOperationException("Queue is empty"); + + return _items[0].Element; + } + + /// + /// 尝试查看队首元素 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryPeek(out TElement element, out TPriority priority) + { + if (_items.Count == 0) + { + element = default!; + priority = default!; + return false; + } + + var item = _items[0]; + element = item.Element; + priority = item.Priority; + return true; + } + + /// + /// 清空队列 + /// + public void Clear() + { + _items.Clear(); + } + + private void HeapifyUp(int index) + { + var comparer = _comparer ?? Comparer.Default; + while (index > 0) + { + var parentIndex = (index - 1) / 2; + if (comparer.Compare(_items[index].Priority, _items[parentIndex].Priority) >= 0) + break; + + Swap(index, parentIndex); + index = parentIndex; + } + } + + private void HeapifyDown(int index) + { + var comparer = _comparer ?? Comparer.Default; + var count = _items.Count; + + while (true) + { + var leftChild = 2 * index + 1; + var rightChild = 2 * index + 2; + var smallest = index; + + if (leftChild < count && comparer.Compare(_items[leftChild].Priority, _items[smallest].Priority) < 0) + smallest = leftChild; + + if (rightChild < count && comparer.Compare(_items[rightChild].Priority, _items[smallest].Priority) < 0) + smallest = rightChild; + + if (smallest == index) + break; + + Swap(index, smallest); + index = smallest; + } + } + + private void Swap(int i, int j) + { + var temp = _items[i]; + _items[i] = _items[j]; + _items[j] = temp; + } + } +} +#endif diff --git a/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs b/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs new file mode 100644 index 0000000..809f276 --- /dev/null +++ b/EasyTool.Core/QueueCategory/PriorityQueueUtil.cs @@ -0,0 +1,329 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.QueueCategory +{ + /// + /// 优先级队列工具类 + /// + public static class PriorityQueueUtil + { + /// + /// 创建最小堆优先队列 + /// + /// 元素类型 + /// 比较器 + /// 优先队列 + public static PriorityQueue CreateMin(IComparer? comparer = null) + { + return new PriorityQueue(comparer ?? Comparer.Default); + } + + /// + /// 创建最大堆优先队列 + /// + /// 元素类型 + /// 比较器 + /// 优先队列 + public static PriorityQueue CreateMax(IComparer? comparer = null) + { + var reverseComparer = Comparer.Create((a, b) => + (comparer ?? Comparer.Default).Compare(b, a)); + return new PriorityQueue(reverseComparer); + } + + /// + /// 从集合创建优先队列 + /// + /// 元素类型 + /// 优先级类型 + /// 元素集合 + /// 优先级选择器 + /// 优先队列 + public static PriorityQueue FromCollection( + IEnumerable items, + Func prioritySelector) + { + var queue = new PriorityQueue(); + foreach (var item in items) + { + queue.Enqueue(item, prioritySelector(item)); + } + return queue; + } + + /// + /// 批量入队 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素集合 + /// 优先级选择器 + public static void EnqueueRange( + this PriorityQueue queue, + IEnumerable items, + Func prioritySelector) + { + foreach (var item in items) + { + queue.Enqueue(item, prioritySelector(item)); + } + } + + /// + /// 批量出队 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 数量 + /// 元素列表 + public static List DequeueRange( + this PriorityQueue queue, + int count) + { + var result = new List(); + + for (int i = 0; i < count && queue.Count > 0; i++) + { + if (queue.TryDequeue(out var element, out _)) + { + result.Add(element); + } + } + + return result; + } + + /// + /// 查看队首元素但不移除 + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素 + /// 优先级 + /// 是否成功 + public static bool TryPeek( + this PriorityQueue queue, + out TElement? element, + out TPriority? priority) + { + element = default; + priority = default; + + if (queue.Count == 0) + return false; + + // 通过出队再入队的方式实现 Peek + if (queue.TryDequeue(out element, out priority)) + { + queue.Enqueue(element!, priority!); + return true; + } + + return false; + } + + /// + /// 获取所有元素(按优先级排序,不移除) + /// + /// 元素类型 + /// 优先级类型 + /// 优先队列 + /// 元素列表 + public static List<(TElement Element, TPriority Priority)> ToSortedList( + this PriorityQueue queue) + { + var tempQueue = new PriorityQueue(); + var result = new List<(TElement, TPriority)>(); + + while (queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + tempQueue.Enqueue(element!, priority!); + } + + // 恢复队列 + foreach (var (element, priority) in result) + { + queue.Enqueue(element, priority); + } + + return result; + } + } + + /// + /// 线程安全的优先队列 + /// + /// 元素类型 + /// 优先级类型 + public class ConcurrentPriorityQueue where TPriority : IComparable + { + private readonly PriorityQueue _queue; + private readonly object _lock = new(); + + /// + /// 获取队列长度 + /// + public int Count + { + get + { + lock (_lock) + { + return _queue.Count; + } + } + } + + /// + /// 是否为空 + /// + public bool IsEmpty => Count == 0; + + /// + /// 创建线程安全的优先队列 + /// + /// 优先级比较器 + public ConcurrentPriorityQueue(IComparer? comparer = null) + { + _queue = new PriorityQueue(comparer ?? Comparer.Default); + } + + /// + /// 入队 + /// + /// 元素 + /// 优先级 + public void Enqueue(TElement element, TPriority priority) + { + lock (_lock) + { + _queue.Enqueue(element, priority); + } + } + + /// + /// 批量入队 + /// + /// 元素集合 + public void EnqueueRange(IEnumerable<(TElement Element, TPriority Priority)> items) + { + lock (_lock) + { + foreach (var (element, priority) in items) + { + _queue.Enqueue(element, priority); + } + } + } + + /// + /// 出队 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryDequeue(out TElement? element, out TPriority? priority) + { + lock (_lock) + { + return _queue.TryDequeue(out element, out priority); + } + } + + /// + /// 批量出队 + /// + /// 数量 + /// 元素列表 + public List<(TElement Element, TPriority Priority)> DequeueRange(int count) + { + var result = new List<(TElement, TPriority)>(); + + lock (_lock) + { + for (int i = 0; i < count && _queue.Count > 0; i++) + { + if (_queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + } + } + } + + return result; + } + + /// + /// 查看队首元素 + /// + /// 元素 + /// 优先级 + /// 是否成功 + public bool TryPeek(out TElement? element, out TPriority? priority) + { + lock (_lock) + { + if (_queue.Count == 0) + { + element = default; + priority = default; + return false; + } + + if (_queue.TryDequeue(out element, out priority)) + { + _queue.Enqueue(element!, priority!); + return true; + } + + return false; + } + } + + /// + /// 清空队列 + /// + public void Clear() + { + lock (_lock) + { + while (_queue.TryDequeue(out _, out _)) { } + } + } + + /// + /// 转换为数组 + /// + /// 数组 + public (TElement Element, TPriority Priority)[] ToArray() + { + lock (_lock) + { + var tempQueue = new PriorityQueue(); + var result = new List<(TElement, TPriority)>(); + + while (_queue.TryDequeue(out var element, out var priority)) + { + result.Add((element!, priority!)); + tempQueue.Enqueue(element!, priority!); + } + + // 恢复队列 + foreach (var (element, priority) in result) + { + _queue.Enqueue(element, priority); + } + + return result.ToArray(); + } + } + } +} diff --git a/EasyTool.Core/ReflectCategory/EnumUtil.cs b/EasyTool.Core/ReflectCategory/EnumUtil.cs new file mode 100644 index 0000000..435421d --- /dev/null +++ b/EasyTool.Core/ReflectCategory/EnumUtil.cs @@ -0,0 +1,212 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ReflectCategory +{ + /// + /// 枚举工具类 + /// + public static class EnumUtil + { + /// + /// 获取枚举所有值 + /// + public static IEnumerable GetValues() where T : struct, Enum + { + return Enum.GetValues(typeof(T)).Cast(); + } + + /// + /// 获取枚举所有名称 + /// + public static IEnumerable GetNames() where T : struct, Enum + { + return Enum.GetNames(typeof(T)); + } + + /// + /// 解析枚举 + /// + public static T Parse(string value, bool ignoreCase = true) where T : struct, Enum + { + return (T)Enum.Parse(typeof(T), value, ignoreCase); + } + + /// + /// 尝试解析枚举 + /// + public static bool TryParse(string value, out T result, bool ignoreCase = true) where T : struct, Enum + { + return Enum.TryParse(value, ignoreCase, out result); + } + + /// + /// 检查值是否定义 + /// + public static bool IsDefined(T value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 检查整数值是否定义 + /// + public static bool IsDefined(int value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 转换为整数 + /// + public static int ToInt(T value) where T : struct, Enum + { + return Convert.ToInt32(value); + } + + /// + /// 从整数转换 + /// + public static T FromInt(int value) where T : struct, Enum + { + return (T)Enum.ToObject(typeof(T), value); + } + + /// + /// 获取枚举项信息 + /// + public static IEnumerable> GetItems() where T : struct, Enum + { + var type = typeof(T); + var names = Enum.GetNames(type); + var values = Enum.GetValues(type).Cast(); + + return names.Zip(values, (name, value) => new EnumItem + { + Name = name, + Value = value, + IntValue = Convert.ToInt32(value) + }); + } + + /// + /// 获取枚举项数量 + /// + public static int GetCount() where T : struct, Enum + { + return Enum.GetNames(typeof(T)).Length; + } + + /// + /// 获取随机枚举值 + /// + public static T GetRandomValue(Random? random = null) where T : struct, Enum + { + var values = GetValues().ToArray(); + var r = random ?? new Random(); + return values[r.Next(values.Length)]; + } + + /// + /// 检查是否包含标志 + /// + public static bool HasFlag(T value, T flag) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + return (intValue & intFlag) == intFlag; + } + + /// + /// 设置标志 + /// + public static T SetFlag(T value, T flag, bool set = true) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + + if (set) + intValue |= intFlag; + else + intValue &= ~intFlag; + + return (T)Enum.ToObject(typeof(T), intValue); + } + + /// + /// 清除标志 + /// + public static T ClearFlag(T value, T flag) where T : struct, Enum + { + return SetFlag(value, flag, false); + } + + /// + /// 切换标志 + /// + public static T ToggleFlag(T value, T flag) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + var intFlag = Convert.ToInt64(flag); + intValue ^= intFlag; + return (T)Enum.ToObject(typeof(T), intValue); + } + + /// + /// 获取所有标志 + /// + public static IEnumerable GetFlags(T value) where T : struct, Enum + { + var intValue = Convert.ToInt64(value); + + foreach (var flag in GetValues()) + { + var intFlag = Convert.ToInt64(flag); + if (intFlag != 0 && (intValue & intFlag) == intFlag) + { + yield return flag; + } + } + } + + /// + /// 组合标志 + /// + public static T CombineFlags(params T[] flags) where T : struct, Enum + { + long result = 0; + foreach (var flag in flags) + { + result |= Convert.ToInt64(flag); + } + return (T)Enum.ToObject(typeof(T), result); + } + } + + /// + /// 枚举项信息 + /// + public class EnumItem where T : struct, Enum + { + /// + /// 名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 枚举值 + /// + public T Value { get; set; } + + /// + /// 整数值 + /// + public int IntValue { get; set; } + + public override string ToString() + { + return $"{Name} ({IntValue})"; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ReflectCategory/ExpressionUtil.cs b/EasyTool.Core/ReflectCategory/ExpressionUtil.cs new file mode 100644 index 0000000..265db9b --- /dev/null +++ b/EasyTool.Core/ReflectCategory/ExpressionUtil.cs @@ -0,0 +1,324 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// 表达式工具类 + /// + public static class ExpressionUtil + { + #region 属性访问 + + /// + /// 获取属性名称 + /// + public static string GetPropertyName(Expression> propertyExpression) + { + if (propertyExpression.Body is MemberExpression memberExpression) + { + return memberExpression.Member.Name; + } + + if (propertyExpression.Body is UnaryExpression unaryExpression && + unaryExpression.Operand is MemberExpression operand) + { + return operand.Member.Name; + } + + throw new ArgumentException("表达式不是有效的属性访问表达式"); + } + + /// + /// 获取属性信息 + /// + public static PropertyInfo GetProperty(Expression> propertyExpression) + { + if (propertyExpression.Body is MemberExpression memberExpression && + memberExpression.Member is PropertyInfo propertyInfo) + { + return propertyInfo; + } + + if (propertyExpression.Body is UnaryExpression unaryExpression && + unaryExpression.Operand is MemberExpression operand && + operand.Member is PropertyInfo propInfo) + { + return propInfo; + } + + throw new ArgumentException("表达式不是有效的属性访问表达式"); + } + + /// + /// 创建属性获取器 + /// + public static Func CreateGetter(Expression> propertyExpression) + { + return propertyExpression.Compile(); + } + + /// + /// 创建属性设置器 + /// + public static Action CreateSetter(Expression> propertyExpression) + { + var parameter = Expression.Parameter(typeof(TProperty), "value"); + var property = GetProperty(propertyExpression); + + var setter = Expression.Lambda>( + Expression.Call(propertyExpression.Parameters[0], property.GetSetMethod()!, parameter), + propertyExpression.Parameters[0], parameter); + + return setter.Compile(); + } + + #endregion + + #region 条件表达式 + + /// + /// 组合多个条件表达式(AND) + /// + public static Expression> And(params Expression>[] expressions) + { + if (expressions == null || expressions.Length == 0) + return _ => true; + + if (expressions.Length == 1) + return expressions[0]; + + var parameter = expressions[0].Parameters[0]; + var body = expressions[0].Body; + + for (int i = 1; i < expressions.Length; i++) + { + var visitor = new ParameterReplacer(expressions[i].Parameters[0], parameter); + body = Expression.AndAlso(body, visitor.Visit(expressions[i].Body)); + } + + return Expression.Lambda>(body, parameter); + } + + /// + /// 组合多个条件表达式(OR) + /// + public static Expression> Or(params Expression>[] expressions) + { + if (expressions == null || expressions.Length == 0) + return _ => false; + + if (expressions.Length == 1) + return expressions[0]; + + var parameter = expressions[0].Parameters[0]; + var body = expressions[0].Body; + + for (int i = 1; i < expressions.Length; i++) + { + var visitor = new ParameterReplacer(expressions[i].Parameters[0], parameter); + body = Expression.OrElse(body, visitor.Visit(expressions[i].Body)); + } + + return Expression.Lambda>(body, parameter); + } + + /// + /// 取反条件表达式 + /// + public static Expression> Not(Expression> expression) + { + var body = Expression.Not(expression.Body); + return Expression.Lambda>(body, expression.Parameters[0]); + } + + #endregion + + #region 排序表达式 + + /// + /// 创建排序表达式 + /// + public static Expression> CreateOrderBy(Expression> keySelector) + { + return keySelector; + } + + /// + /// 应用排序 + /// + public static IOrderedQueryable ApplyOrder(IQueryable source, string propertyName, bool ascending = true) + { + var parameter = Expression.Parameter(typeof(T), "x"); + var property = Expression.Property(parameter, propertyName); + var lambda = Expression.Lambda(property, parameter); + + var methodName = ascending ? "OrderBy" : "OrderByDescending"; + var method = typeof(Queryable).GetMethods() + .Where(m => m.Name == methodName && m.GetParameters().Length == 2) + .Single(); + + var genericMethod = method.MakeGenericMethod(typeof(T), property.Type); + return (IOrderedQueryable)genericMethod.Invoke(null, new object[] { source, lambda })!; + } + + /// + /// 应用后续排序 + /// + public static IOrderedQueryable ApplyThenBy(IOrderedQueryable source, string propertyName, bool ascending = true) + { + var parameter = Expression.Parameter(typeof(T), "x"); + var property = Expression.Property(parameter, propertyName); + var lambda = Expression.Lambda(property, parameter); + + var methodName = ascending ? "ThenBy" : "ThenByDescending"; + var method = typeof(Queryable).GetMethods() + .Where(m => m.Name == methodName && m.GetParameters().Length == 2) + .Single(); + + var genericMethod = method.MakeGenericMethod(typeof(T), property.Type); + return (IOrderedQueryable)genericMethod.Invoke(null, new object[] { source, lambda })!; + } + + #endregion + + #region 构造表达式 + + /// + /// 创建等于条件 + /// + public static Expression> CreateEqual(Expression> propertyExpression, TValue value) + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.Equal(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建大于条件 + /// + public static Expression> CreateGreaterThan(Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.GreaterThan(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建小于条件 + /// + public static Expression> CreateLessThan(Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.LessThan(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建包含条件 + /// + public static Expression> CreateContains(Expression> propertyExpression, string value) + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value); + var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) })!; + var body = Expression.Call(property, containsMethod, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建范围条件 + /// + public static Expression> CreateInRange( + Expression> propertyExpression, + TValue min, TValue max) where TValue : IComparable + { + var greaterThanOrEqual = CreateGreaterThanOrEqual(propertyExpression, min); + var lessThanOrEqual = CreateLessThanOrEqual(propertyExpression, max); + return And(greaterThanOrEqual, lessThanOrEqual); + } + + /// + /// 创建大于等于条件 + /// + public static Expression> CreateGreaterThanOrEqual( + Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.GreaterThanOrEqual(property, constant); + return Expression.Lambda>(body, parameter); + } + + /// + /// 创建小于等于条件 + /// + public static Expression> CreateLessThanOrEqual( + Expression> propertyExpression, TValue value) + where TValue : IComparable + { + var parameter = propertyExpression.Parameters[0]; + var property = propertyExpression.Body; + var constant = Expression.Constant(value, typeof(TValue)); + var body = Expression.LessThanOrEqual(property, constant); + return Expression.Lambda>(body, parameter); + } + + #endregion + + #region 编译执行 + + /// + /// 编译并执行表达式 + /// + public static TResult Execute(Expression> expression, T instance) + { + var func = expression.Compile(); + return func(instance); + } + + /// + /// 编译表达式 + /// + public static Func Compile(Expression> expression) + { + return expression.Compile(); + } + + #endregion + } + + /// + /// 参数替换器 + /// + internal class ParameterReplacer : ExpressionVisitor + { + private readonly ParameterExpression _oldParameter; + private readonly ParameterExpression _newParameter; + + public ParameterReplacer(ParameterExpression oldParameter, ParameterExpression newParameter) + { + _oldParameter = oldParameter; + _newParameter = newParameter; + } + + protected override Expression VisitParameter(ParameterExpression node) + { + return node == _oldParameter ? _newParameter : node; + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs b/EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs new file mode 100644 index 0000000..da4d5ed --- /dev/null +++ b/EasyTool.Core/ReflectCategory/PropertyInfoExtension.cs @@ -0,0 +1,390 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// PropertyInfo 扩展方法 + /// + public static class PropertyInfoExtension + { + #region 值获取 + + /// + /// 安全获取属性值,失败返回默认值 + /// + public static object? GetValueOrDefault(this PropertyInfo? property, object? obj) + { + if (property == null || obj == null) + return null; + + try + { + return property.GetValue(obj); + } + catch + { + return null; + } + } + + /// + /// 安全获取属性值,失败返回指定默认值 + /// + public static T? GetValueOrDefault(this PropertyInfo? property, object? obj, T? defaultValue = default) + { + if (property == null || obj == null) + return defaultValue; + + try + { + var value = property.GetValue(obj); + if (value == null) + return defaultValue; + + return (T)value; + } + catch + { + return defaultValue; + } + } + + #endregion + + #region 值设置 + + /// + /// 安全设置属性值 + /// + public static bool SetValueSafe(this PropertyInfo? property, object? obj, object? value) + { + if (property == null || obj == null) + return false; + + try + { + if (!property.CanWrite) + return false; + + property.SetValue(obj, value); + return true; + } + catch + { + return false; + } + } + + /// + /// 设置属性值(支持类型转换) + /// + public static bool SetValueWithConvert(this PropertyInfo? property, object? obj, object? value) + { + if (property == null || obj == null || !property.CanWrite) + return false; + + try + { + object? convertedValue = value; + + // 如果类型不匹配,尝试转换 + if (value != null && value.GetType() != property.PropertyType) + { + // 处理可空类型 + var targetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; + + if (targetType.IsEnum && value is string str) + { + convertedValue = Enum.Parse(targetType, str); + } + else + { + convertedValue = Convert.ChangeType(value, targetType); + } + } + + property.SetValue(obj, convertedValue); + return true; + } + catch + { + return false; + } + } + + #endregion + + #region 特性检查 + + /// + /// 判断属性是否有指定特性 + /// + public static bool HasAttribute(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return false; + + return property.GetCustomAttribute() != null; + } + + /// + /// 判断属性是否有指定特性 + /// + public static bool HasAttribute(this PropertyInfo? property, Type? attributeType) + { + if (property == null || attributeType == null) + return false; + + return property.GetCustomAttributes(attributeType, false).Any(); + } + + /// + /// 获取属性特性 + /// + public static T? GetAttribute(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return null; + + return property.GetCustomAttribute(); + } + + /// + /// 获取属性的所有特性 + /// + public static T[] GetAttributes(this PropertyInfo? property) where T : Attribute + { + if (property == null) + return Array.Empty(); + + return property.GetCustomAttributes().ToArray(); + } + + #endregion + + #region DataAnnotations 特性快捷访问 + + /// + /// 判断是否是必填项 + /// + public static bool IsRequired(this PropertyInfo? property) + { + return property.HasAttribute(); + } + + /// + /// 获取显示名称 + /// + public static string GetDisplayName(this PropertyInfo? property) + { + var displayAttr = property.GetAttribute(); + if (displayAttr != null && !string.IsNullOrEmpty(displayAttr.GetName())) + return displayAttr.GetName()!; + + var displayNameAttr = property.GetAttribute(); + if (displayNameAttr != null && !string.IsNullOrEmpty(displayNameAttr.DisplayName)) + return displayNameAttr.DisplayName; + + return property?.Name ?? string.Empty; + } + + /// + /// 获取描述 + /// + public static string GetDescription(this PropertyInfo? property) + { + var descriptionAttr = property.GetAttribute(); + if (descriptionAttr != null && !string.IsNullOrEmpty(descriptionAttr.Description)) + return descriptionAttr.Description; + + var displayAttr = property.GetAttribute(); + if (displayAttr != null && !string.IsNullOrEmpty(displayAttr.GetDescription())) + return displayAttr.GetDescription()!; + + return string.Empty; + } + + /// + /// 获取字符串长度限制 + /// + public static int GetStringLength(this PropertyInfo? property) + { + var attr = property.GetAttribute(); + return attr?.MaximumLength ?? 0; + } + + /// + /// 获取数据类型 + /// + public static string GetDataType(this PropertyInfo? property) + { + var attr = property.GetAttribute(); + return attr?.DataType.ToString() ?? string.Empty; + } + + #endregion + + #region 类型判断 + + /// + /// 判断是否是字符串类型 + /// + public static bool IsString(this PropertyInfo? property) + { + return property?.PropertyType == typeof(string); + } + + /// + /// 判断是否是数值类型 + /// + public static bool IsNumeric(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(byte) || + type == typeof(sbyte) || + type == typeof(short) || + type == typeof(ushort) || + type == typeof(int) || + type == typeof(uint) || + type == typeof(long) || + type == typeof(ulong) || + type == typeof(float) || + type == typeof(double) || + type == typeof(decimal); + } + + /// + /// 判断是否是日期类型 + /// + public static bool IsDateTime(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(DateTime) || type == typeof(DateTimeOffset); + } + + /// + /// 判断是否是布尔类型 + /// + public static bool IsBoolean(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + if (type == null) + return false; + + return type == typeof(bool); + } + + /// + /// 判断是否是枚举类型 + /// + public static bool IsEnum(this PropertyInfo? property) + { + var type = Nullable.GetUnderlyingType(property?.PropertyType) ?? property?.PropertyType; + return type?.IsEnum == true; + } + + /// + /// 判断是否是集合类型 + /// + public static bool IsCollection(this PropertyInfo? property) + { + if (property == null) + return false; + + return typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType) && + property.PropertyType != typeof(string); + } + + /// + /// 判断是否是可空类型 + /// + public static bool IsNullable(this PropertyInfo? property) + { + if (property == null) + return false; + + return Nullable.GetUnderlyingType(property.PropertyType) != null; + } + + #endregion + + #region 访问判断 + + /// + /// 判断是否可读 + /// + public static bool CanRead(this PropertyInfo? property) + { + return property?.CanRead == true; + } + + /// + /// 判断是否可写 + /// + public static bool CanWrite(this PropertyInfo? property) + { + return property?.CanWrite == true; + } + + /// + /// 判断是否有公共的 getter + /// + public static bool HasPublicGetter(this PropertyInfo? property) + { + return property?.CanRead == true && property.GetGetMethod(false) != null; + } + + /// + /// 判断是否有公共的 setter + /// + public static bool HasPublicSetter(this PropertyInfo? property) + { + return property?.CanWrite == true && property.GetSetMethod(false) != null; + } + + #endregion + + #region 获取元素类型 + + /// + /// 获取集合的元素类型 + /// + public static Type? GetElementType(this PropertyInfo? property) + { + if (property == null) + return null; + + var type = property.PropertyType; + + // 处理数组 + if (type.IsArray) + return type.GetElementType(); + + // 处理泛型集合 + if (type.IsGenericType) + { + var genericType = type.GetGenericTypeDefinition(); + if (genericType == typeof(System.Collections.Generic.IEnumerable<>) || + genericType == typeof(System.Collections.Generic.List<>) || + genericType == typeof(System.Collections.Generic.IList<>) || + genericType == typeof(System.Collections.Generic.ICollection<>)) + { + return type.GetGenericArguments()[0]; + } + } + + return null; + } + + #endregion + } +} diff --git a/EasyTool.Core/ReflectCategory/ReflectUtil.cs b/EasyTool.Core/ReflectCategory/ReflectUtil.cs new file mode 100644 index 0000000..9848cbe --- /dev/null +++ b/EasyTool.Core/ReflectCategory/ReflectUtil.cs @@ -0,0 +1,322 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// 反射工具类,提供类型、属性、字段、方法的反射操作 + /// + public static class ReflectUtil + { + #region 类型成员获取 + + /// + /// 获取类型的所有构造函数 + /// + /// 类型 + /// 构造函数数组 + public static ConstructorInfo[] GetConstructors(Type type) + { + return type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + } + + /// + /// 获取类型的所有属性 + /// + /// 类型 + /// 属性数组 + public static PropertyInfo[] GetProperties(Type type) + { + return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + } + + /// + /// 获取类型的所有字段 + /// + /// 类型 + /// 字段数组 + public static FieldInfo[] GetFields(Type type) + { + return type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + } + + /// + /// 获取类型的所有方法 + /// + /// 类型 + /// 方法数组 + public static MethodInfo[] GetMethods(Type type) + { + return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + } + + /// + /// 获取类型的所有事件 + /// + /// 类型 + /// 事件数组 + public static EventInfo[] GetEvents(Type type) + { + return type.GetEvents(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + } + + /// + /// 获取类型的所有属性名 + /// + /// 类型 + /// 属性名数组 + public static string[] GetPropertyNames(Type type) + { + return GetProperties(type).Select(p => p.Name).ToArray(); + } + + /// + /// 获取类型的所有字段名 + /// + /// 类型 + /// 字段名数组 + public static string[] GetFieldNames(Type type) + { + return GetFields(type).Select(f => f.Name).ToArray(); + } + + /// + /// 获取类型的所有方法名 + /// + /// 类型 + /// 方法名数组 + public static string[] GetMethodNames(Type type) + { + return GetMethods(type).Select(m => m.Name).ToArray(); + } + + /// + /// 获取类型的所有事件名 + /// + /// 类型 + /// 事件名数组 + public static string[] GetEventNames(Type type) + { + return GetEvents(type).Select(e => e.Name).ToArray(); + } + + /// + /// 获取类型的所有接口名 + /// + /// 类型 + /// 接口名数组 + public static string[] GetInterfaceNames(Type type) + { + return type.GetInterfaces().Select(i => i.Name).ToArray(); + } + + #endregion + + #region 类型特性 + + /// + /// 获取指定类型的指定类型的特性数组 + /// + /// 特性类型 + /// 类型 + /// 特性数组 + public static T[] GetAttributes(Type type) where T : Attribute + { + return type.GetCustomAttributes().ToArray(); + } + + /// + /// 判断类型是否实现了指定的接口 + /// + /// 要判断的类型 + /// 要判断的接口类型 + /// 是否实现了指定的接口 + public static bool ImplementsInterface() + { + return typeof(T).GetInterfaces().Any(i => i == typeof(TInterface)); + } + + /// + /// 获取类的继承层次结构 + /// + /// 要获取继承层次结构的类 + /// 类的继承层次结构 + public static Type[] GetClassHierarchy(Type type) + { + Type[] hierarchy = new Type[0]; + Type currentType = type; + while (currentType != null) + { + Array.Resize(ref hierarchy, hierarchy.Length + 1); + hierarchy[hierarchy.Length - 1] = currentType; + currentType = currentType.BaseType; + } + return hierarchy; + } + + /// + /// 获取枚举类型的所有值 + /// + /// 枚举类型 + /// 枚举类型的所有值 + public static IEnumerable GetEnumValues() + { + if (!typeof(T).IsEnum) + { + throw new ArgumentException("Type is not an enum type"); + } + + return Enum.GetValues(typeof(T)).Cast(); + } + + #endregion + + #region 实例创建 + + /// + /// 创建类型的实例 + /// + /// 类型 + /// 构造函数参数 + /// 实例 + public static object CreateInstance(Type type, params object[] args) + { + Type[] parameterTypes = GetParameterTypes(args); + ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, + null, parameterTypes, null); + if (constructor == null) + { + throw new ArgumentException($"Type {type} does not have a constructor with specified arguments"); + } + return constructor.Invoke(args); + } + + /// + /// 获取构造函数参数类型的数组 + /// + /// 要获取参数类型的参数数组 + /// 参数类型的数组 + private static Type[] GetParameterTypes(object[] parameters) + { + if (parameters == null) + { + return Type.EmptyTypes; + } + Type[] parameterTypes = new Type[parameters.Length]; + for (int i = 0; i < parameters.Length; i++) + { + if (parameters[i] == null) + { + parameterTypes[i] = typeof(object); + } + else + { + parameterTypes[i] = parameters[i].GetType(); + } + } + return parameterTypes; + } + + #endregion + + #region 方法调用 + + /// + /// 调用泛型方法 + /// + /// 调用方法的对象 + /// 方法名 + /// 泛型参数类型 + /// 方法参数 + /// 方法返回值 + public static object InvokeGenericMethod(object obj, string methodName, Type genericType, params object[] args) + { + MethodInfo method = obj.GetType().GetMethod(methodName); + MethodInfo genericMethod = method.MakeGenericMethod(genericType); + return genericMethod.Invoke(obj, args); + } + + /// + /// 动态调用类的实例方法 + /// + /// 要调用实例方法的类实例 + /// 要调用的实例方法的名称 + /// 要传递给实例方法的参数 + /// 实例方法的返回值 + public static object InvokeMethod(object instance, string methodName, params object[] arguments) + { + Type type = instance.GetType(); + MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); + return method.Invoke(instance, arguments); + } + + /// + /// 动态调用类的静态方法 + /// + /// 要调用静态方法的类 + /// 要调用的静态方法的名称 + /// 要传递给静态方法的参数 + /// 静态方法的返回值 + public static object InvokeStaticMethod(Type type, string methodName, params object[] arguments) + { + MethodInfo method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public); + return method.Invoke(null, arguments); + } + + #endregion + + #region 静态成员操作 + + /// + /// 获取类的静态属性的值 + /// + /// 要获取静态属性的类 + /// 要获取的静态属性的名称 + /// 静态属性的值 + public static object GetStaticPropertyValue(Type type, string propertyName) + { + PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); + return property.GetValue(null); + } + + /// + /// 设置类的静态属性的值 + /// + /// 要设置静态属性的类 + /// 要设置的静态属性的名称 + /// 要设置的静态属性的值 + public static void SetStaticPropertyValue(Type type, string propertyName, object value) + { + PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); + property.SetValue(null, value); + } + + /// + /// 获取类的静态字段的值 + /// + /// 要获取静态字段的类 + /// 要获取的静态字段的名称 + /// 静态字段的值 + public static object GetStaticFieldValue(Type type, string fieldName) + { + FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); + return field.GetValue(null); + } + + /// + /// 设置类的静态字段的值 + /// + /// 要设置静态字段的类 + /// 要设置的静态字段的名称 + /// 要设置的静态字段的值 + public static void SetStaticFieldValue(Type type, string fieldName, object value) + { + FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); + field.SetValue(null, value); + } + + #endregion + } +} diff --git a/EasyTool.Core/ReflectCategory/TypeExtension.cs b/EasyTool.Core/ReflectCategory/TypeExtension.cs new file mode 100644 index 0000000..a943f54 --- /dev/null +++ b/EasyTool.Core/ReflectCategory/TypeExtension.cs @@ -0,0 +1,420 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace EasyTool.ReflectCategory +{ + /// + /// Type 类型扩展方法 + /// + public static class TypeExtension + { + #region 类型判断 + + /// + /// 判断是否是简单类型(值类型、字符串等) + /// + public static bool IsSimpleType(this Type? type) + { + if (type == null) + return false; + + return type.IsValueType || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Guid); + } + + /// + /// 判断是否是数字类型 + /// + public static bool IsNumericType(this Type? type) + { + if (type == null) + return false; + + type = Nullable.GetUnderlyingType(type) ?? type; + + return type == typeof(byte) || + type == typeof(sbyte) || + type == typeof(short) || + type == typeof(ushort) || + type == typeof(int) || + type == typeof(uint) || + type == typeof(long) || + type == typeof(ulong) || + type == typeof(float) || + type == typeof(double) || + type == typeof(decimal); + } + + /// + /// 判断是否是集合类型 + /// + public static bool IsCollectionType(this Type? type) + { + if (type == null) + return false; + + return type != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(type); + } + + /// + /// 判断是否是字典类型 + /// + public static bool IsDictionaryType(this Type? type) + { + if (type == null) + return false; + + return typeof(System.Collections.IDictionary).IsAssignableFrom(type) || + (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.Dictionary<,>)); + } + + /// + /// 判断是否是可空值类型 + /// + public static bool IsNullableType(this Type? type) + { + if (type == null) + return false; + + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } + + /// + /// 判断是否是泛型类型 + /// + public static bool IsGenericType(this Type? type, Type genericTypeDefinition) + { + if (type == null || !type.IsGenericType) + return false; + + return type.GetGenericTypeDefinition() == genericTypeDefinition; + } + + /// + /// 判断是否是某个泛型类型的子类 + /// + public static bool IsSubclassOfGeneric(this Type? type, Type genericTypeDefinition) + { + if (type == null || genericTypeDefinition == null) + return false; + + while (type != null && type != typeof(object)) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == genericTypeDefinition) + return true; + + type = type.BaseType; + } + + return false; + } + + /// + /// 判断是否是匿名类型 + /// + public static bool IsAnonymousType(this Type? type) + { + if (type == null) + return false; + + return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) && + type.IsGenericType && + type.Name.Contains("AnonymousType") && + (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")); + } + + #endregion + + #region 类型名称 + + /// + /// 获取友好的类型名称 + /// + public static string GetFriendlyName(this Type? type) + { + if (type == null) + return "null"; + + if (type == typeof(int)) + return "int"; + if (type == typeof(uint)) + return "uint"; + if (type == typeof(long)) + return "long"; + if (type == typeof(ulong)) + return "ulong"; + if (type == typeof(short)) + return "short"; + if (type == typeof(ushort)) + return "ushort"; + if (type == typeof(byte)) + return "byte"; + if (type == typeof(sbyte)) + return "sbyte"; + if (type == typeof(float)) + return "float"; + if (type == typeof(double)) + return "double"; + if (type == typeof(decimal)) + return "decimal"; + if (type == typeof(bool)) + return "bool"; + if (type == typeof(string)) + return "string"; + if (type == typeof(char)) + return "char"; + if (type == typeof(object)) + return "object"; + if (type == typeof(void)) + return "void"; + + if (type.IsGenericType) + { + var genericArgs = type.GetGenericArguments(); + var genericTypeName = type.GetGenericTypeDefinition().GetFriendlyName(); + var genericArgsNames = string.Join(", ", genericArgs.Select(t => t.GetFriendlyName())); + return genericTypeName + "<" + genericArgsNames + ">"; + } + + if (type.IsArray) + { + var elementType = type.GetElementType(); + return $"{elementType.GetFriendlyName()}[]"; + } + + return type.Name; + } + + /// + /// 获取类型的显示名称 + /// + public static string GetDisplayName(this Type? type) + { + if (type == null) + return string.Empty; + + var attr = type.GetCustomAttribute(); + return attr?.DisplayName ?? type.Name; + } + + /// + /// 获取类型的描述 + /// + public static string GetDescription(this Type? type) + { + if (type == null) + return string.Empty; + + var attr = type.GetCustomAttribute(); + return attr?.Description ?? string.Empty; + } + + #endregion + + #region 默认值 + + /// + /// 获取类型的默认值 + /// + public static object? GetDefaultValue(this Type? type) + { + if (type == null) + return null; + + return type.IsValueType ? Activator.CreateInstance(type) : null; + } + + #endregion + + #region 泛型处理 + + + /// + /// 获取集合的元素类型 + /// + public static Type? GetElementType(this Type? type) + { + if (type == null) + return null; + + if (type.IsArray) + return type.GetElementType(); + + if (type.IsCollectionType()) + { + var genericArgs = type.GetGenericArguments(); + if (genericArgs.Length > 0) + return genericArgs[0]; + } + + return null; + } + + /// + /// 获取字典的键值对类型 + /// + public static (Type? KeyType, Type? ValueType) GetDictionaryKeyValueTypes(this Type? type) + { + if (type == null) + return (null, null); + + if (typeof(System.Collections.IDictionary).IsAssignableFrom(type)) + { + var interfaces = type.GetInterfaces(); + var dictInterface = interfaces.FirstOrDefault(i => + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>)); + + if (dictInterface != null) + { + var genericArgs = dictInterface.GetGenericArguments(); + return (genericArgs[0], genericArgs[1]); + } + } + + return (null, null); + } + + #endregion + + #region 特性操作 + + /// + /// 判断是否有指定特性 + /// + public static bool HasAttribute(this Type? type) where T : Attribute + { + if (type == null) + return false; + + return type.GetCustomAttribute() != null; + } + + /// + /// 判断是否有指定特性 + /// + public static bool HasAttribute(this Type? type, Type? attributeType) + { + if (type == null || attributeType == null) + return false; + + return type.GetCustomAttributes(attributeType, false).Any(); + } + + /// + /// 获取指定特性 + /// + public static T? GetAttribute(this Type? type) where T : Attribute + { + if (type == null) + return null; + + return type.GetCustomAttribute(); + } + + /// + /// 获取所有指定特性 + /// + public static T[] GetAttributes(this Type? type) where T : Attribute + { + if (type == null) + return Array.Empty(); + + return type.GetCustomAttributes().ToArray(); + } + + #endregion + + #region 成员获取 + + /// + /// 获取所有属性(包含继承的) + /// + public static PropertyInfo[] GetAllProperties(this Type? type) + { + if (type == null) + return Array.Empty(); + + var properties = new List(); + var currentType = type; + + while (currentType != null && currentType != typeof(object)) + { + var currentProps = currentType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); + properties.AddRange(currentProps); + currentType = currentType.BaseType; + } + + return properties.ToArray(); + } + + /// + /// 获取所有字段(包含继承的) + /// + public static FieldInfo[] GetAllFields(this Type? type) + { + if (type == null) + return Array.Empty(); + + var fields = new List(); + var currentType = type; + + while (currentType != null && currentType != typeof(object)) + { + var currentFields = currentType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + fields.AddRange(currentFields); + currentType = currentType.BaseType; + } + + return fields.ToArray(); + } + + #endregion + + #region 类型转换 + + /// + /// 尝试将值转换为指定类型 + /// + public static object? ChangeType(this Type type, object? value) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + + if (value == null) + { + if (type.IsValueType) + return Activator.CreateInstance(type); + return null; + } + + var valueType = value.GetType(); + + // 如果类型相同或目标类型是源类型的父类 + if (type.IsAssignableFrom(valueType)) + return value; + + // 可空类型处理 + var underlyingType = Nullable.GetUnderlyingType(type); + if (underlyingType != null) + return ChangeType(underlyingType, value); + + // 枚举处理 + if (type.IsEnum && value is string str) + return Enum.Parse(type, str, true); + + // 标准转换 + return Convert.ChangeType(value, type); + } + + #endregion + } +} diff --git a/EasyTool.Core/ReflectCategory/TypeUtil.cs b/EasyTool.Core/ReflectCategory/TypeUtil.cs new file mode 100644 index 0000000..1cc3193 --- /dev/null +++ b/EasyTool.Core/ReflectCategory/TypeUtil.cs @@ -0,0 +1,404 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace EasyTool.ReflectCategory +{ + /// + /// 类型工具类 + /// + public static class TypeUtil + { + #region 类型判断 + + /// + /// 判断是否为简单类型 + /// + public static bool IsSimpleType(Type type) + { + if (type == null) return false; + + return type.IsPrimitive || + type.IsEnum || + type == typeof(string) || + type == typeof(decimal) || + type == typeof(DateTime) || + type == typeof(DateTimeOffset) || + type == typeof(TimeSpan) || + type == typeof(Guid) || + type == typeof(byte[]) || + Nullable.GetUnderlyingType(type) != null && IsSimpleType(Nullable.GetUnderlyingType(type)!); + } + + /// + /// 判断是否为可空类型 + /// + public static bool IsNullableType(Type type) + { + return type != null && Nullable.GetUnderlyingType(type) != null; + } + + /// + /// 判断是否为集合类型 + /// + public static bool IsCollectionType(Type type) + { + if (type == null) return false; + return type != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(type); + } + + /// + /// 判断是否为字典类型 + /// + public static bool IsDictionaryType(Type type) + { + if (type == null) return false; + + return type.GetInterfaces() + .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)); + } + + /// + /// 判断是否为元组类型 + /// + public static bool IsTupleType(Type type) + { + if (type == null) return false; + + if (!type.IsGenericType) + return false; + + var definition = type.GetGenericTypeDefinition(); + return definition == typeof(Tuple<>) || + definition == typeof(Tuple<,>) || + definition == typeof(Tuple<,,>) || + definition == typeof(Tuple<,,,>) || + definition == typeof(Tuple<,,,,>) || + definition == typeof(Tuple<,,,,,>) || + definition == typeof(Tuple<,,,,,,>) || + definition == typeof(Tuple<,,,,,,,>) || + definition == typeof(ValueTuple<>) || + definition == typeof(ValueTuple<,>) || + definition == typeof(ValueTuple<,,>) || + definition == typeof(ValueTuple<,,,>) || + definition == typeof(ValueTuple<,,,,>) || + definition == typeof(ValueTuple<,,,,,>) || + definition == typeof(ValueTuple<,,,,,,>) || + definition == typeof(ValueTuple<,,,,,,,>); + } + + /// + /// 获取可空类型的基类型 + /// + public static Type? GetUnderlyingType(Type type) + { + return Nullable.GetUnderlyingType(type); + } + + /// + /// 获取集合元素类型 + /// + public static Type? GetElementType(Type type) + { + if (type == null) return null; + + if (type.IsArray) + return type.GetElementType(); + + if (type.IsGenericType) + { + var genericArgs = type.GetGenericArguments(); + if (genericArgs.Length > 0) + { + // 对于 IEnumerable、List 等 + if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type)) + { + return genericArgs[0]; + } + } + } + + return null; + } + + #endregion + + #region 类型创建 + + /// + /// 创建实例 + /// + public static object? CreateInstance(Type type, params object[] args) + { + if (type == null) return null; + + if (args == null || args.Length == 0) + { + return Activator.CreateInstance(type); + } + + return Activator.CreateInstance(type, args); + } + + /// + /// 创建泛型实例 + /// + public static object? CreateGenericInstance(Type genericType, Type[] typeArguments, params object[] args) + { + if (genericType == null || typeArguments == null) return null; + + if (!genericType.IsGenericTypeDefinition) + throw new ArgumentException("类型必须是泛型定义"); + + var constructedType = genericType.MakeGenericType(typeArguments); + return CreateInstance(constructedType, args); + } + + #endregion + + #region 属性/字段访问 + + /// + /// 获取所有属性 + /// + public static PropertyInfo[] GetProperties(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetProperties(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取属性 + /// + public static PropertyInfo? GetProperty(Type type, string propertyName, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetProperty(propertyName, bindingFlags); + } + + /// + /// 获取属性值 + /// + public static object? GetPropertyValue(object obj, string propertyName) + { + if (obj == null) return null; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + return property?.GetValue(obj); + } + + /// + /// 设置属性值 + /// + public static void SetPropertyValue(object obj, string propertyName, object? value) + { + if (obj == null) return; + + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + property?.SetValue(obj, value); + } + + /// + /// 获取所有字段 + /// + public static FieldInfo[] GetFields(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetFields(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取字段值 + /// + public static object? GetFieldValue(object obj, string fieldName) + { + if (obj == null) return null; + + var type = obj.GetType(); + var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + return field?.GetValue(obj); + } + + /// + /// 设置字段值 + /// + public static void SetFieldValue(object obj, string fieldName, object? value) + { + if (obj == null) return; + + var type = obj.GetType(); + var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); + + field?.SetValue(obj, value); + } + + #endregion + + #region 方法调用 + + /// + /// 获取所有方法 + /// + public static MethodInfo[] GetMethods(Type type, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + return type?.GetMethods(bindingFlags) ?? Array.Empty(); + } + + /// + /// 获取方法 + /// + public static MethodInfo? GetMethod(Type type, string methodName, Type[]? parameterTypes = null, BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance) + { + if (type == null) return null; + + if (parameterTypes == null) + return type.GetMethod(methodName, bindingFlags); + + return type.GetMethod(methodName, bindingFlags, null, parameterTypes, null); + } + + /// + /// 调用方法 + /// + public static object? InvokeMethod(object obj, string methodName, params object[] args) + { + if (obj == null) return null; + + var type = obj.GetType(); + var argTypes = args?.Select(a => a?.GetType() ?? typeof(object)).ToArray(); + var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, argTypes, null); + + return method?.Invoke(obj, args); + } + + /// + /// 调用静态方法 + /// + public static object? InvokeStaticMethod(Type type, string methodName, params object[] args) + { + if (type == null) return null; + + var argTypes = args?.Select(a => a?.GetType() ?? typeof(object)).ToArray(); + var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static, null, argTypes, null); + + return method?.Invoke(null, args); + } + + #endregion + + #region 类型继承 + + /// + /// 判断类型是否继承自指定类型 + /// + public static bool IsAssignableTo(Type type, Type targetType) + { + return targetType?.IsAssignableFrom(type) ?? false; + } + + /// + /// 获取基类型 + /// + public static Type? GetBaseType(Type type) + { + return type?.BaseType; + } + + /// + /// 获取所有接口 + /// + public static Type[] GetInterfaces(Type type) + { + return type?.GetInterfaces() ?? Array.Empty(); + } + + /// + /// 获取继承层次 + /// + public static IEnumerable GetInheritanceHierarchy(Type type) + { + if (type == null) yield break; + + var current = type; + while (current != null && current != typeof(object)) + { + yield return current; + current = current.BaseType; + } + + if (type != typeof(object)) + yield return typeof(object); + } + + #endregion + + #region 特性 + + /// + /// 获取特性 + /// + public static T? GetAttribute(MemberInfo member) where T : Attribute + { + return member?.GetCustomAttribute(); + } + + /// + /// 获取所有特性 + /// + public static IEnumerable GetAttributes(MemberInfo member) where T : Attribute + { + return member?.GetCustomAttributes() ?? Enumerable.Empty(); + } + + /// + /// 检查是否有特性 + /// + public static bool HasAttribute(MemberInfo member) where T : Attribute + { + return member?.IsDefined(typeof(T), true) ?? false; + } + + #endregion + + #region 类型信息 + + /// + /// 获取类型友好名称 + /// + public static string GetFriendlyName(Type type) + { + if (type == null) return string.Empty; + + if (!type.IsGenericType) + return type.Name; + + var name = type.Name; + var backtickIndex = name.IndexOf('`'); + if (backtickIndex >= 0) + name = name.Substring(0, backtickIndex); + + var genericArgs = type.GetGenericArguments(); + var argNames = string.Join(", ", genericArgs.Select(GetFriendlyName)); + + return $"{name}<{argNames}>"; + } + + /// + /// 获取默认值 + /// + public static object? GetDefaultValue(Type type) + { + if (type == null) return null; + + if (type.IsValueType) + return Activator.CreateInstance(type); + + return null; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/SecurityCategory/CertificateUtil.cs b/EasyTool.Core/SecurityCategory/CertificateUtil.cs new file mode 100644 index 0000000..f15aa8d --- /dev/null +++ b/EasyTool.Core/SecurityCategory/CertificateUtil.cs @@ -0,0 +1,534 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 证书工具类 + /// 提供证书生成、加载和验证功能 + /// + public static class CertificateUtil + { + /// + /// 生成自签名证书 + /// + /// 主题名称 + /// 有效期(年) + /// 密钥大小 + /// X509 证书 + public static X509Certificate2 GenerateSelfSignedCertificate( + string subjectName, + int validYears = 1, + int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + var request = new CertificateRequest( + $"CN={subjectName}", + rsa, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + + // 添加基本约束 + request.CertificateExtensions.Add( + new X509BasicConstraintsExtension(true, true, 0, false)); + + // 添加密钥用法 + request.CertificateExtensions.Add( + new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | + X509KeyUsageFlags.KeyEncipherment | + X509KeyUsageFlags.CrlSign | + X509KeyUsageFlags.KeyCertSign, + false)); + + // 添加增强密钥用法 + request.CertificateExtensions.Add( + new X509EnhancedKeyUsageExtension( + new OidCollection + { + new Oid("1.3.6.1.5.5.7.3.1"), // Server Authentication + new Oid("1.3.6.1.5.5.7.3.2") // Client Authentication + }, + false)); + + // 添加主题备用名称 + var sanBuilder = new SubjectAlternativeNameBuilder(); + sanBuilder.AddDnsName(subjectName); + sanBuilder.AddDnsName("localhost"); + request.CertificateExtensions.Add(sanBuilder.Build()); + + // 创建证书 + var notBefore = DateTimeOffset.UtcNow.AddDays(-1); + var notAfter = DateTimeOffset.UtcNow.AddYears(validYears); + + var certificate = request.CreateSelfSigned(notBefore, notAfter); + + return new X509Certificate2( + certificate.Export(X509ContentType.Pfx), + (string?)null, + X509KeyStorageFlags.MachineKeySet | + X509KeyStorageFlags.PersistKeySet | + X509KeyStorageFlags.Exportable); + } + + /// + /// 生成客户端证书 + /// + /// 主题名称 + /// 颁发者证书 + /// 有效期(天) + /// 客户端证书 + public static X509Certificate2 GenerateClientCertificate( + string subjectName, + X509Certificate2 issuerCertificate, + int validDays = 365) + { + using var rsa = RSA.Create(2048); + var request = new CertificateRequest( + $"CN={subjectName}", + rsa, + HashAlgorithmName.SHA256, + RSASignaturePadding.Pkcs1); + + // 添加基本约束 + request.CertificateExtensions.Add( + new X509BasicConstraintsExtension(false, false, 0, false)); + + // 添加密钥用法 + request.CertificateExtensions.Add( + new X509KeyUsageExtension( + X509KeyUsageFlags.DigitalSignature | + X509KeyUsageFlags.KeyEncipherment, + false)); + + // 添加增强密钥用法 + request.CertificateExtensions.Add( + new X509EnhancedKeyUsageExtension( + new OidCollection + { + new Oid("1.3.6.1.5.5.7.3.2") // Client Authentication + }, + false)); + + // 使用颁发者证书签名 + var notBefore = DateTimeOffset.UtcNow.AddDays(-1); + var notAfter = DateTimeOffset.UtcNow.AddDays(validDays); + + var serialNumber = new byte[16]; + RandomNumberGenerator.Fill(serialNumber); + + var certificate = request.Create( + issuerCertificate, + notBefore, + notAfter, + serialNumber); + + return certificate.CopyWithPrivateKey(rsa); + } + + /// + /// 从文件加载证书 + /// + /// 文件路径 + /// 密码 + /// 证书 + public static X509Certificate2 LoadFromFile(string filePath, string? password = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"证书文件不存在: {filePath}"); + + var bytes = File.ReadAllBytes(filePath); + return new X509Certificate2(bytes, password); + } + + /// + /// 从 PFX 文件加载证书 + /// + /// 文件路径 + /// 密码 + /// 证书 + public static X509Certificate2 LoadPfx(string filePath, string? password = null) + { + return LoadFromFile(filePath, password); + } + + /// + /// 从 PEM 文件加载证书 + /// + /// 证书文件路径 + /// 私钥文件路径(可选) + /// 证书 + public static X509Certificate2 LoadPem(string certPath, string? keyPath = null) + { + var certPem = File.ReadAllText(certPath); +#if NET5_0_OR_GREATER + var cert = X509Certificate2.CreateFromPem(certPem); + + if (!string.IsNullOrEmpty(keyPath) && File.Exists(keyPath)) + { + var keyPem = File.ReadAllText(keyPath); + using var rsa = RSA.Create(); + rsa.ImportFromPem(keyPem); + return cert.CopyWithPrivateKey(rsa); + } + + return cert; +#else + // netstandard2.1 不支持 PEM 格式,使用 Pfx 格式 + throw new PlatformNotSupportedException("PEM 格式证书需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 保存证书到文件 + /// + /// 证书 + /// 文件路径 + /// 密码 + /// 格式 + public static void SaveToFile( + X509Certificate2 certificate, + string filePath, + string? password = null, + CertificateFormat format = CertificateFormat.Pfx) + { + byte[] data; + + switch (format) + { + case CertificateFormat.Pfx: + data = certificate.Export(X509ContentType.Pfx, password); + break; + case CertificateFormat.Pem: +#if NET5_0_OR_GREATER + var pem = certificate.ExportCertificatePem(); + data = Encoding.UTF8.GetBytes(pem); +#else + // netstandard2.1 不支持 PEM 导出 + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + case CertificateFormat.Cer: + data = certificate.Export(X509ContentType.Cert); + break; + default: + throw new ArgumentException($"不支持的证书格式: {format}"); + } + + File.WriteAllBytes(filePath, data); + } + + /// + /// 导出证书为 PEM 格式 + /// + /// 证书 + /// PEM 字符串 + public static string ExportToPem(X509Certificate2 certificate) + { +#if NET5_0_OR_GREATER + return certificate.ExportCertificatePem(); +#else + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 导出私钥为 PEM 格式 + /// + /// 证书 + /// PEM 字符串 + public static string ExportPrivateKeyToPem(X509Certificate2 certificate) + { + var rsa = certificate.GetRSAPrivateKey(); + if (rsa == null) + throw new InvalidOperationException("证书不包含私钥"); + +#if NET5_0_OR_GREATER + return rsa.ExportRSAPrivateKeyPem(); +#else + throw new PlatformNotSupportedException("PEM 格式导出需要 .NET 5.0 或更高版本"); +#endif + } + + /// + /// 验证证书 + /// + /// 证书 + /// 证书链(可选) + /// 验证结果 + public static CertificateValidationResult Validate( + X509Certificate2 certificate, + X509Certificate2Collection? chain = null) + { + var result = new CertificateValidationResult + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint + }; + + // 检查有效期 + var now = DateTime.UtcNow; + + if (now < certificate.NotBefore) + { + result.IsValid = false; + result.Errors.Add($"证书尚未生效(生效时间: {certificate.NotBefore})"); + } + + if (now > certificate.NotAfter) + { + result.IsValid = false; + result.Errors.Add($"证书已过期(过期时间: {certificate.NotAfter})"); + } + + // 验证证书链 + using var chainBuilder = new X509Chain(); + chainBuilder.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chainBuilder.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; + + if (chain != null) + { + foreach (var cert in chain) + { + chainBuilder.ChainPolicy.ExtraStore.Add(cert); + } + } + + var chainValid = chainBuilder.Build(certificate); + + if (!chainValid) + { + foreach (var status in chainBuilder.ChainStatus) + { + result.Warnings.Add($"证书链状态: {status.StatusInformation}"); + } + } + + result.HasPrivateKey = certificate.HasPrivateKey; + result.IsValid ??= true; + + return result; + } + + /// + /// 验证证书链 + /// + /// 证书 + /// 颁发者证书 + /// 是否有效 + public static bool ValidateChain(X509Certificate2 certificate, X509Certificate2 issuerCertificate) + { + using var chain = new X509Chain(); + chain.ChainPolicy.ExtraStore.Add(issuerCertificate); + chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; + chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; + + return chain.Build(certificate); + } + + /// + /// 从证书存储区获取证书 + /// + /// 存储区名称 + /// 存储区位置 + /// 证书指纹 + /// 证书 + public static X509Certificate2? GetFromStore( + StoreName storeName, + StoreLocation storeLocation, + string thumbprint) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadOnly); + + var certificates = store.Certificates.Find( + X509FindType.FindByThumbprint, + thumbprint, + false); + + return certificates.Count > 0 ? certificates[0] : null; + } + + /// + /// 将证书添加到存储区 + /// + /// 证书 + /// 存储区名称 + /// 存储区位置 + public static void AddToStore( + X509Certificate2 certificate, + StoreName storeName, + StoreLocation storeLocation) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadWrite); + store.Add(certificate); + } + + /// + /// 从存储区移除证书 + /// + /// 证书 + /// 存储区名称 + /// 存储区位置 + public static void RemoveFromStore( + X509Certificate2 certificate, + StoreName storeName, + StoreLocation storeLocation) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadWrite); + store.Remove(certificate); + } + + /// + /// 获取证书信息 + /// + /// 证书 + /// 证书信息 + public static CertificateInfo GetCertificateInfo(X509Certificate2 certificate) + { + return new CertificateInfo + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint, + SerialNumber = certificate.SerialNumber, + HasPrivateKey = certificate.HasPrivateKey, + KeySize = certificate.GetRSAPublicKey()?.KeySize ?? 0, + SignatureAlgorithm = certificate.SignatureAlgorithm.FriendlyName ?? "Unknown" + }; + } + } + + /// + /// 证书格式 + /// + public enum CertificateFormat + { + /// + /// PFX/P12 格式 + /// + Pfx, + + /// + /// PEM 格式 + /// + Pem, + + /// + /// CER/DER 格式 + /// + Cer + } + + /// + /// 证书验证结果 + /// + public class CertificateValidationResult + { + /// + /// 是否有效 + /// + public bool? IsValid { get; set; } + + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 颁发者 + /// + public string Issuer { get; set; } = string.Empty; + + /// + /// 生效时间 + /// + public DateTime NotBefore { get; set; } + + /// + /// 过期时间 + /// + public DateTime NotAfter { get; set; } + + /// + /// 指纹 + /// + public string Thumbprint { get; set; } = string.Empty; + + /// + /// 是否有私钥 + /// + public bool HasPrivateKey { get; set; } + + /// + /// 错误信息 + /// + public List Errors { get; set; } = new(); + + /// + /// 警告信息 + /// + public List Warnings { get; set; } = new(); + } + + /// + /// 证书信息 + /// + public class CertificateInfo + { + /// + /// 主题 + /// + public string Subject { get; set; } = string.Empty; + + /// + /// 颁发者 + /// + public string Issuer { get; set; } = string.Empty; + + /// + /// 生效时间 + /// + public DateTime NotBefore { get; set; } + + /// + /// 过期时间 + /// + public DateTime NotAfter { get; set; } + + /// + /// 指纹 + /// + public string Thumbprint { get; set; } = string.Empty; + + /// + /// 序列号 + /// + public string SerialNumber { get; set; } = string.Empty; + + /// + /// 是否有私钥 + /// + public bool HasPrivateKey { get; set; } + + /// + /// 密钥大小 + /// + public int KeySize { get; set; } + + /// + /// 签名算法 + /// + public string SignatureAlgorithm { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/SecurityCategory/CsrfUtil.cs b/EasyTool.Core/SecurityCategory/CsrfUtil.cs new file mode 100644 index 0000000..d0e0d75 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/CsrfUtil.cs @@ -0,0 +1,342 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// CSRF(跨站请求伪造)防护工具类 + /// + public static class CsrfUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly int _defaultTokenLength = 32; + + /// + /// 生成 CSRF Token + /// + /// Token 长度(字节数) + /// Base64 编码的 Token + public static string GenerateToken(int length = 0) + { + var tokenLength = length > 0 ? length : _defaultTokenLength; + var bytes = new byte[tokenLength]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 URL 安全的 CSRF Token + /// + /// Token 长度(字节数) + /// URL 安全的 Base64 编码 Token + public static string GenerateUrlSafeToken(int length = 0) + { + var token = GenerateToken(length); + return token.Replace("+", "-").Replace("/", "_").TrimEnd('='); + } + + /// + /// 生成带签名的 CSRF Token + /// + /// 签名密钥 + /// 要签名的数据(如用户ID、会话ID等) + /// 过期时间(分钟) + /// 签名的 Token + public static string GenerateSignedToken(string secret, string data, int expirationMinutes = 0) + { + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var expiration = expirationMinutes > 0 ? timestamp + (expirationMinutes * 60) : 0; + + var payload = expiration > 0 + ? $"{data}|{timestamp}|{expiration}" + : $"{data}|{timestamp}"; + + var signature = ComputeHmacSha256(secret, payload); + var token = $"{payload}|{signature}"; + + return Convert.ToBase64String(Encoding.UTF8.GetBytes(token)); + } + + /// + /// 验证签名的 CSRF Token + /// + /// 签名密钥 + /// 要验证的 Token + /// 期望的数据 + /// 验证结果 + public static CsrfValidationResult ValidateSignedToken(string secret, string token, string data) + { + try + { + var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(token)); + var parts = decoded.Split('|'); + + if (parts.Length != 4) + { + return CsrfValidationResult.Fail("Token 格式无效"); + } + + var tokenData = parts[0]; + var timestampStr = parts[1]; + var expirationStr = parts[2]; + var signature = parts[3]; + + // 验证数据 + if (tokenData != data) + { + return CsrfValidationResult.Fail("Token 数据不匹配"); + } + + // 验证签名 + var payload = $"{tokenData}|{timestampStr}|{expirationStr}"; + var expectedSignature = ComputeHmacSha256(secret, payload); + + if (!ConstantTimeEquals(signature, expectedSignature)) + { + return CsrfValidationResult.Fail("Token 签名无效"); + } + + // 验证过期时间 + if (long.TryParse(expirationStr, out var expiration) && expiration > 0) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (now > expiration) + { + return CsrfValidationResult.Fail("Token 已过期"); + } + } + + // 解析创建时间 + var createdAt = long.TryParse(timestampStr, out var ts) + ? DateTimeOffset.FromUnixTimeSeconds(ts) + : (DateTimeOffset?)null; + + return CsrfValidationResult.Success(createdAt); + } + catch (Exception ex) + { + return CsrfValidationResult.Fail($"Token 验证失败: {ex.Message}"); + } + } + + /// + /// 生成双提交 Cookie 模式的 Token + /// + /// Cookie 中的 Token + /// 请求中应携带的 Token + public static string GenerateDoubleSubmitToken(string cookieToken) + { + var randomPart = GenerateToken(16); + return $"{cookieToken}:{randomPart}"; + } + + /// + /// 验证双提交 Cookie 模式 + /// + /// Cookie 中的 Token + /// 请求中携带的 Token + /// 验证结果 + public static bool ValidateDoubleSubmitToken(string cookieToken, string requestToken) + { + if (string.IsNullOrEmpty(cookieToken) || string.IsNullOrEmpty(requestToken)) + { + return false; + } + + var parts = requestToken.Split(':'); + if (parts.Length != 2) + { + return false; + } + + return ConstantTimeEquals(cookieToken, parts[0]); + } + + /// + /// 生成同步器 Token 模式的 Token + /// + /// 会话 Token + /// 表单 ID + /// 同步器 Token + public static string GenerateSynchronizerToken(string sessionToken, string formId) + { + var combined = $"{sessionToken}|{formId}"; + using var sha256 = SHA256.Create(); + var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(combined)); + return Convert.ToBase64String(hash); + } + + /// + /// 验证同步器 Token + /// + /// 会话 Token + /// 表单 ID + /// 要验证的 Token + /// 验证结果 + public static bool ValidateSynchronizerToken(string sessionToken, string formId, string token) + { + if (string.IsNullOrEmpty(sessionToken) || string.IsNullOrEmpty(token)) + { + return false; + } + + var expected = GenerateSynchronizerToken(sessionToken, formId); + return ConstantTimeEquals(expected, token); + } + + /// + /// 生成基于会话的 CSRF Token + /// + /// 会话 ID + /// 应用密钥 + /// 额外数据 + /// CSRF Token + public static string GenerateSessionToken(string sessionId, string secret, params string[] additionalData) + { + var data = additionalData.Length > 0 + ? $"{sessionId}|{string.Join("|", additionalData)}" + : sessionId; + + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var payload = $"{data}|{timestamp}"; + var signature = ComputeHmacSha256(secret, payload); + + return $"{payload}|{signature}"; + } + + /// + /// 验证基于会话的 CSRF Token + /// + /// 会话 ID + /// 应用密钥 + /// 要验证的 Token + /// 最大有效期(秒) + /// 验证结果 + public static CsrfValidationResult ValidateSessionToken(string sessionId, string secret, string token, long maxAgeSeconds = 0) + { + try + { + var parts = token.Split('|'); + if (parts.Length < 3) + { + return CsrfValidationResult.Fail("Token 格式无效"); + } + + // 提取签名和数据 + var signature = parts[^1]; + var payloadParts = new ArraySegment(parts, 0, parts.Length - 1).ToArray(); + var payload = string.Join("|", payloadParts); + + // 验证签名 + var expectedSignature = ComputeHmacSha256(secret, payload); + if (!ConstantTimeEquals(signature, expectedSignature)) + { + return CsrfValidationResult.Fail("Token 签名无效"); + } + + // 提取时间戳(最后一个非签名部分) + var timestampStr = payloadParts[^1]; + if (!long.TryParse(timestampStr, out var timestamp)) + { + return CsrfValidationResult.Fail("Token 时间戳无效"); + } + + // 检查过期时间 + if (maxAgeSeconds > 0) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (now - timestamp > maxAgeSeconds) + { + return CsrfValidationResult.Fail("Token 已过期"); + } + } + + // 提取会话 ID(第一个部分) + var tokenSessionId = payloadParts[0]; + if (tokenSessionId != sessionId) + { + return CsrfValidationResult.Fail("Token 会话不匹配"); + } + + var createdAt = DateTimeOffset.FromUnixTimeSeconds(timestamp); + return CsrfValidationResult.Success(createdAt); + } + catch (Exception ex) + { + return CsrfValidationResult.Fail($"Token 验证失败: {ex.Message}"); + } + } + + private static string ComputeHmacSha256(string key, string data) + { + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); + var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(data)); + return Convert.ToBase64String(hash); + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a == null || b == null) + { + return a == b; + } + + if (a.Length != b.Length) + { + return false; + } + + var result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + } + + /// + /// CSRF 验证结果 + /// + public class CsrfValidationResult + { + /// + /// 是否验证通过 + /// + public bool IsValid { get; } + + /// + /// 错误消息 + /// + public string? ErrorMessage { get; } + + /// + /// Token 创建时间 + /// + public DateTimeOffset? CreatedAt { get; } + + private CsrfValidationResult(bool isValid, string? errorMessage, DateTimeOffset? createdAt) + { + IsValid = isValid; + ErrorMessage = errorMessage; + CreatedAt = createdAt; + } + + /// + /// 验证成功 + /// + public static CsrfValidationResult Success(DateTimeOffset? createdAt = null) + { + return new CsrfValidationResult(true, null, createdAt); + } + + /// + /// 验证失败 + /// + public static CsrfValidationResult Fail(string errorMessage) + { + return new CsrfValidationResult(false, errorMessage, null); + } + } +} diff --git a/EasyTool.Core/SecurityCategory/InputSanitizer.cs b/EasyTool.Core/SecurityCategory/InputSanitizer.cs new file mode 100644 index 0000000..da37068 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/InputSanitizer.cs @@ -0,0 +1,403 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// 输入净化器 + /// + public static class InputSanitizer + { + /// + /// 净化选项 + /// + public class SanitizeOptions + { + /// + /// 是否移除HTML标签 + /// + public bool RemoveHtmlTags { get; set; } = true; + + /// + /// 是否移除脚本 + /// + public bool RemoveScripts { get; set; } = true; + + /// + /// 是否转义HTML特殊字符 + /// + public bool EscapeHtml { get; set; } = true; + + /// + /// 是否移除SQL注入 + /// + public bool RemoveSqlInjection { get; set; } = true; + + /// + /// 是否移除路径遍历 + /// + public bool RemovePathTraversal { get; set; } = true; + + /// + /// 是否移除控制字符 + /// + public bool RemoveControlChars { get; set; } = true; + + /// + /// 是否标准化空白 + /// + public bool NormalizeWhitespace { get; set; } = false; + + /// + /// 最大长度(0表示不限制) + /// + public int MaxLength { get; set; } = 0; + + /// + /// 允许的字符正则(为空表示不限制) + /// + public string? AllowedCharsPattern { get; set; } + + /// + /// 获取默认选项 + /// + public static SanitizeOptions Default => new(); + + /// + /// 获取严格选项 + /// + public static SanitizeOptions Strict => new() + { + RemoveHtmlTags = true, + RemoveScripts = true, + EscapeHtml = true, + RemoveSqlInjection = true, + RemovePathTraversal = true, + RemoveControlChars = true, + NormalizeWhitespace = true + }; + + /// + /// 获取宽松选项(仅移除脚本) + /// + public static SanitizeOptions Lenient => new() + { + RemoveHtmlTags = false, + RemoveScripts = true, + EscapeHtml = false, + RemoveSqlInjection = false, + RemovePathTraversal = true, + RemoveControlChars = true + }; + } + + private static readonly Regex HtmlTagPattern = new(@"<[^>]*>", RegexOptions.Compiled); + private static readonly Regex ScriptPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); + private static readonly Regex ControlCharPattern = new(@"[\x00-\x08\x0B\x0C\x0E-\x1F]", RegexOptions.Compiled); + private static readonly Regex PathTraversalPattern = new(@"(\.\.[\\/])|([\\/]\.\.)", RegexOptions.Compiled); + private static readonly Regex MultiWhitespacePattern = new(@"\s+", RegexOptions.Compiled); + + /// + /// 净化输入字符串 + /// + public static string Sanitize(string? input, SanitizeOptions? options = null) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + options ??= SanitizeOptions.Default; + var result = input; + + // 移除控制字符 + if (options.RemoveControlChars) + { + result = ControlCharPattern.Replace(result, ""); + } + + // 移除脚本 + if (options.RemoveScripts) + { + result = ScriptPattern.Replace(result, ""); + } + + // 移除HTML标签 + if (options.RemoveHtmlTags) + { + result = HtmlTagPattern.Replace(result, ""); + } + + // 转义HTML + if (options.EscapeHtml) + { + result = EscapeHtml(result); + } + + // 移除SQL注入 + if (options.RemoveSqlInjection) + { + result = SqlInjectionUtil.Sanitize(result); + } + + // 移除路径遍历 + if (options.RemovePathTraversal) + { + result = PathTraversalPattern.Replace(result, ""); + } + + // 标准化空白 + if (options.NormalizeWhitespace) + { + result = MultiWhitespacePattern.Replace(result, " ").Trim(); + } + + // 过滤允许的字符 + if (!string.IsNullOrEmpty(options.AllowedCharsPattern)) + { + result = Regex.Replace(result, options.AllowedCharsPattern, ""); + } + + // 限制长度 + if (options.MaxLength > 0 && result.Length > options.MaxLength) + { + result = result.Substring(0, options.MaxLength); + } + + return result; + } + + /// + /// 净化文件名 + /// + public static string SanitizeFileName(string fileName) + { + if (string.IsNullOrEmpty(fileName)) + return string.Empty; + + var invalidChars = new string(System.IO.Path.GetInvalidFileNameChars()); + var pattern = $"[{Regex.Escape(invalidChars)}]"; + var result = Regex.Replace(fileName, pattern, "_"); + + // 移除路径遍历 + result = PathTraversalPattern.Replace(result, ""); + + // 移除前导/尾随空格和点 + result = result.Trim().Trim('.'); + + return result; + } + + /// + /// 净化路径 + /// + public static string SanitizePath(string path) + { + if (string.IsNullOrEmpty(path)) + return string.Empty; + + var invalidChars = new string(System.IO.Path.GetInvalidPathChars()); + var pattern = $"[{Regex.Escape(invalidChars)}]"; + var result = Regex.Replace(path, pattern, "_"); + + // 移除路径遍历 + result = PathTraversalPattern.Replace(result, ""); + + return result; + } + + /// + /// 净化URL + /// + public static string SanitizeUrl(string url) + { + if (string.IsNullOrEmpty(url)) + return string.Empty; + + // 只允许http/https协议 + var lower = url.ToLower().Trim(); + if (!lower.StartsWith("http://") && !lower.StartsWith("https://")) + { + return string.Empty; + } + + // 移除危险字符 + var result = url.Replace("<", "").Replace(">", "").Replace("\"", ""); + result = PathTraversalPattern.Replace(result, ""); + + return result; + } + + /// + /// 净化邮箱 + /// + public static string SanitizeEmail(string email) + { + if (string.IsNullOrEmpty(email)) + return string.Empty; + + var result = email.ToLowerInvariant().Trim(); + + // 移除控制字符 + result = ControlCharPattern.Replace(result, ""); + + // 基本验证 + if (!Regex.IsMatch(result, @"^[^@\s]+@[^@\s]+\.[^@\s]+$")) + return string.Empty; + + return result; + } + + /// + /// 净化电话号码 + /// + public static string SanitizePhone(string phone) + { + if (string.IsNullOrEmpty(phone)) + return string.Empty; + + // 只保留数字和+ + var result = Regex.Replace(phone, @"[^\d+]", ""); + + // 验证格式 + if (!Regex.IsMatch(result, @"^\+?\d{6,15}$")) + return string.Empty; + + return result; + } + + /// + /// 净化数字字符串 + /// + public static string SanitizeNumber(string input, bool allowDecimal = false, bool allowNegative = false) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + var pattern = allowDecimal + ? (allowNegative ? @"[^0-9.\-]" : @"[^0-9.]") + : (allowNegative ? @"[^0-9\-]" : @"[^0-9]"); + + var result = Regex.Replace(input, pattern, ""); + + // 验证格式 + if (allowDecimal) + { + if (!decimal.TryParse(result, out _)) + return string.Empty; + } + else + { + if (!long.TryParse(result, out _)) + return string.Empty; + } + + return result; + } + + /// + /// 净化JSON字符串 + /// + public static string SanitizeJson(string json) + { + if (string.IsNullOrEmpty(json)) + return string.Empty; + + var result = new StringBuilder(json.Length); + foreach (var c in json) + { + switch (c) + { + case '"': + result.Append("\\\""); + break; + case '\\': + result.Append("\\\\"); + break; + case '\b': + result.Append("\\b"); + break; + case '\f': + result.Append("\\f"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\t': + result.Append("\\t"); + break; + default: + if (c < 32) + { + result.Append($"\\u{(int)c:X4}"); + } + else + { + result.Append(c); + } + break; + } + } + return result.ToString(); + } + + /// + /// 净化XML字符串 + /// + public static string SanitizeXml(string xml) + { + if (string.IsNullOrEmpty(xml)) + return string.Empty; + + var result = new StringBuilder(xml.Length); + foreach (var c in xml) + { + result.Append(c switch + { + '<' => "<", + '>' => ">", + '&' => "&", + '"' => """, + '\'' => "'", + _ => c + }); + } + return result.ToString(); + } + + private static string EscapeHtml(string input) + { + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + result.Append(c switch + { + '<' => "<", + '>' => ">", + '&' => "&", + '"' => """, + '\'' => "'", + '/' => "/", + _ => c + }); + } + return result.ToString(); + } + + /// + /// 批量净化 + /// + public static Dictionary SanitizeMultiple(IDictionary inputs, SanitizeOptions? options = null) + { + var results = new Dictionary(); + foreach (var kvp in inputs) + { + results[kvp.Key] = Sanitize(kvp.Value, options); + } + return results; + } + } +} diff --git a/EasyTool.Core/SecurityCategory/JwtBuilder.cs b/EasyTool.Core/SecurityCategory/JwtBuilder.cs new file mode 100644 index 0000000..385f2e8 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/JwtBuilder.cs @@ -0,0 +1,616 @@ +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; +using Microsoft.IdentityModel.Tokens; + +namespace EasyTool.SecurityCategory +{ + /// + /// JWT 构建器 + /// 提供流畅的 JWT 生成接口 + /// + public class JwtBuilder + { + private readonly List _claims; + private string? _issuer; + private string? _audience; + private DateTime? _notBefore; + private DateTime? _expires; + private DateTime? _issuedAt; + private string? _subject; + private string? _id; + private SecurityKey? _signingKey; + private SecurityKey? _encryptingKey; + private string _algorithm = SecurityAlgorithms.HmacSha256; + private SigningCredentials? _signingCredentials; + private EncryptingCredentials? _encryptingCredentials; + + /// + /// 创建 JWT 构建器 + /// + public JwtBuilder() + { + _claims = new List(); + } + + /// + /// 设置颁发者 + /// + /// 颁发者 + /// JwtBuilder + public JwtBuilder WithIssuer(string issuer) + { + _issuer = issuer; + return this; + } + + /// + /// 设置受众 + /// + /// 受众 + /// JwtBuilder + public JwtBuilder WithAudience(string audience) + { + _audience = audience; + return this; + } + + /// + /// 设置主题 + /// + /// 主题 + /// JwtBuilder + public JwtBuilder WithSubject(string subject) + { + _subject = subject; + return this; + } + + /// + /// 设置 JWT ID + /// + /// ID + /// JwtBuilder + public JwtBuilder WithId(string id) + { + _id = id; + return this; + } + + /// + /// 设置生效时间 + /// + /// 生效时间 + /// JwtBuilder + public JwtBuilder WithNotBefore(DateTime notBefore) + { + _notBefore = notBefore; + return this; + } + + /// + /// 设置过期时间 + /// + /// 过期时间 + /// JwtBuilder + public JwtBuilder WithExpires(DateTime expires) + { + _expires = expires; + return this; + } + + /// + /// 设置过期时间(相对) + /// + /// 有效时长 + /// JwtBuilder + public JwtBuilder WithExpiresIn(TimeSpan duration) + { + _expires = DateTime.UtcNow.Add(duration); + return this; + } + + /// + /// 设置签发时间 + /// + /// 签发时间 + /// JwtBuilder + public JwtBuilder WithIssuedAt(DateTime issuedAt) + { + _issuedAt = issuedAt; + return this; + } + + /// + /// 添加声明 + /// + /// 类型 + /// 值 + /// JwtBuilder + public JwtBuilder WithClaim(string type, string value) + { + _claims.Add(new Claim(type, value)); + return this; + } + + /// + /// 添加声明 + /// + /// 声明 + /// JwtBuilder + public JwtBuilder WithClaim(Claim claim) + { + _claims.Add(claim); + return this; + } + + /// + /// 批量添加声明 + /// + /// 声明集合 + /// JwtBuilder + public JwtBuilder WithClaims(IEnumerable claims) + { + _claims.AddRange(claims); + return this; + } + + /// + /// 设置用户ID声明 + /// + /// 用户ID + /// JwtBuilder + public JwtBuilder WithUserId(string userId) + { + return WithClaim(JwtRegisteredClaimNames.Sub, userId); + } + + /// + /// 设置用户名声明 + /// + /// 用户名 + /// JwtBuilder + public JwtBuilder WithUsername(string username) + { + return WithClaim(ClaimTypes.Name, username); + } + + /// + /// 设置角色声明 + /// + /// 角色列表 + /// JwtBuilder + public JwtBuilder WithRoles(params string[] roles) + { + foreach (var role in roles) + { + _claims.Add(new Claim(ClaimTypes.Role, role)); + } + return this; + } + + /// + /// 设置邮箱声明 + /// + /// 邮箱 + /// JwtBuilder + public JwtBuilder WithEmail(string email) + { + return WithClaim(ClaimTypes.Email, email); + } + + /// + /// 设置签名密钥(字符串) + /// + /// 密钥字符串 + /// JwtBuilder + public JwtBuilder WithSecretKey(string secretKey) + { + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + return WithSigningKey(key); + } + + /// + /// 设置签名密钥 + /// + /// 密钥 + /// JwtBuilder + public JwtBuilder WithSigningKey(SecurityKey key) + { + _signingKey = key; + _signingCredentials = new SigningCredentials(key, _algorithm); + return this; + } + + /// + /// 设置签名密钥(RSA) + /// + /// RSA 密钥 + /// 算法 + /// JwtBuilder + public JwtBuilder WithRsaKey(RSA rsa, string algorithm = SecurityAlgorithms.RsaSha256) + { + var key = new RsaSecurityKey(rsa); + _algorithm = algorithm; + _signingCredentials = new SigningCredentials(key, algorithm); + return this; + } + + /// + /// 设置签名凭据 + /// + /// 签名凭据 + /// JwtBuilder + public JwtBuilder WithSigningCredentials(SigningCredentials credentials) + { + _signingCredentials = credentials; + return this; + } + + /// + /// 设置加密密钥 + /// + /// 加密密钥 + /// 加密算法 + /// JwtBuilder + public JwtBuilder WithEncryptingKey(SecurityKey key, string algorithm = SecurityAlgorithms.Aes256CbcHmacSha512) + { + _encryptingKey = key; + if (key is SymmetricSecurityKey symmetricKey) + { + _encryptingCredentials = new EncryptingCredentials(symmetricKey, algorithm); + } + else + { + throw new ArgumentException("加密密钥必须是 SymmetricSecurityKey 类型", nameof(key)); + } + return this; + } + + /// + /// 设置签名算法 + /// + /// 算法 + /// JwtBuilder + public JwtBuilder WithAlgorithm(string algorithm) + { + _algorithm = algorithm; + if (_signingKey != null) + { + _signingCredentials = new SigningCredentials(_signingKey, algorithm); + } + return this; + } + + /// + /// 构建 JWT Token + /// + /// JWT 字符串 + public string Build() + { + if (_signingCredentials == null) + throw new InvalidOperationException("必须设置签名密钥"); + + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(_claims), + Issuer = _issuer, + Audience = _audience, + NotBefore = _notBefore, + Expires = _expires, + IssuedAt = _issuedAt, + SigningCredentials = _signingCredentials, + EncryptingCredentials = _encryptingCredentials + }; + + if (!string.IsNullOrEmpty(_subject)) + { + tokenDescriptor.Subject.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, _subject)); + } + + if (!string.IsNullOrEmpty(_id)) + { + tokenDescriptor.Subject.AddClaim(new Claim(JwtRegisteredClaimNames.Jti, _id)); + } + + var tokenHandler = new JwtSecurityTokenHandler(); + var token = tokenHandler.CreateToken(tokenDescriptor); + + return tokenHandler.WriteToken(token); + } + + /// + /// 构建并返回 Token 及其信息 + /// + /// Token 信息 + public JwtTokenInfo BuildWithInfo() + { + var token = Build(); + var handler = new JwtSecurityTokenHandler(); + var jwtToken = handler.ReadJwtToken(token); + + return new JwtTokenInfo + { + Token = token, + Header = jwtToken.Header, + Payload = jwtToken.Payload, + Claims = jwtToken.Claims, + ValidFrom = jwtToken.ValidFrom, + ValidTo = jwtToken.ValidTo, + Issuer = jwtToken.Issuer, + Audiences = jwtToken.Audiences + }; + } + } + + /// + /// JWT Token 信息 + /// + public class JwtTokenInfo + { + /// + /// Token 字符串 + /// + public string Token { get; set; } = string.Empty; + + /// + /// 头部 + /// + public JwtHeader Header { get; set; } = null!; + + /// + /// 载荷 + /// + public JwtPayload Payload { get; set; } = null!; + + /// + /// 声明集合 + /// + public IEnumerable Claims { get; set; } = Array.Empty(); + + /// + /// 生效时间 + /// + public DateTime ValidFrom { get; set; } + + /// + /// 过期时间 + /// + public DateTime ValidTo { get; set; } + + /// + /// 颁发者 + /// + public string? Issuer { get; set; } + + /// + /// 受众 + /// + public IEnumerable Audiences { get; set; } = Array.Empty(); + } + + /// + /// JWT 验证结果 + /// + public class JwtValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 错误信息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 声明主体 + /// + public ClaimsPrincipal? Principal { get; set; } + + /// + /// Token 信息 + /// + public JwtSecurityToken? Token { get; set; } + } + + /// + /// JWT 工具类 + /// + public static class JwtUtil + { + /// + /// 创建 JWT 构建器 + /// + /// JwtBuilder + public static JwtBuilder Create() + { + return new JwtBuilder(); + } + + /// + /// 快速生成 JWT Token + /// + /// 密钥 + /// 声明 + /// 有效时长 + /// 颁发者 + /// 受众 + /// JWT Token + public static string GenerateToken( + string secretKey, + Dictionary? claims = null, + TimeSpan? expiresIn = null, + string? issuer = null, + string? audience = null) + { + var builder = new JwtBuilder() + .WithSecretKey(secretKey); + + if (claims != null) + { + foreach (var claim in claims) + { + builder.WithClaim(claim.Key, claim.Value); + } + } + + if (expiresIn.HasValue) + { + builder.WithExpiresIn(expiresIn.Value); + } + + if (!string.IsNullOrEmpty(issuer)) + { + builder.WithIssuer(issuer); + } + + if (!string.IsNullOrEmpty(audience)) + { + builder.WithAudience(audience); + } + + return builder.Build(); + } + + /// + /// 验证 JWT Token + /// + /// Token + /// 密钥 + /// 颁发者 + /// 受众 + /// 验证结果 + public static JwtValidationResult Validate( + string token, + string secretKey, + string? issuer = null, + string? audience = null) + { + try + { + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + + var validationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = key, + ValidateIssuer = !string.IsNullOrEmpty(issuer), + ValidIssuer = issuer, + ValidateAudience = !string.IsNullOrEmpty(audience), + ValidAudience = audience, + ValidateLifetime = true, + ClockSkew = TimeSpan.Zero + }; + + var handler = new JwtSecurityTokenHandler(); + var principal = handler.ValidateToken(token, validationParameters, out var validatedToken); + + return new JwtValidationResult + { + IsValid = true, + Principal = principal, + Token = validatedToken as JwtSecurityToken + }; + } + catch (Exception ex) + { + return new JwtValidationResult + { + IsValid = false, + ErrorMessage = ex.Message + }; + } + } + + /// + /// 解析 JWT Token(不验证) + /// + /// Token + /// Token 信息 + public static JwtSecurityToken Parse(string token) + { + var handler = new JwtSecurityTokenHandler(); + return handler.ReadJwtToken(token); + } + + /// + /// 获取 Token 中的声明 + /// + /// Token + /// 声明集合 + public static IEnumerable GetClaims(string token) + { + var jwtToken = Parse(token); + return jwtToken.Claims; + } + + /// + /// 获取指定声明 + /// + /// Token + /// 声明类型 + /// 声明值 + public static string? GetClaim(string token, string claimType) + { + var claims = GetClaims(token); + return claims.FirstOrDefault(c => c.Type == claimType)?.Value; + } + + /// + /// 检查 Token 是否即将过期 + /// + /// Token + /// 阈值 + /// 是否即将过期 + public static bool IsExpiringSoon(string token, TimeSpan threshold) + { + var jwtToken = Parse(token); + return jwtToken.ValidTo - DateTime.UtcNow < threshold; + } + + /// + /// 刷新 Token + /// + /// 旧 Token + /// 密钥 + /// 新的有效时长 + /// 颁发者 + /// 受众 + /// 新 Token + public static string RefreshToken( + string oldToken, + string secretKey, + TimeSpan? expiresIn = null, + string? issuer = null, + string? audience = null) + { + var claims = GetClaims(oldToken); + var builder = new JwtBuilder() + .WithSecretKey(secretKey) + .WithClaims(claims); + + if (expiresIn.HasValue) + { + builder.WithExpiresIn(expiresIn.Value); + } + + if (!string.IsNullOrEmpty(issuer)) + { + builder.WithIssuer(issuer); + } + + if (!string.IsNullOrEmpty(audience)) + { + builder.WithAudience(audience); + } + + return builder.Build(); + } + } +} diff --git a/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs b/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs new file mode 100644 index 0000000..3e634ed --- /dev/null +++ b/EasyTool.Core/SecurityCategory/KeyGeneratorUtil.cs @@ -0,0 +1,419 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 密钥和Token生成工具类 + /// + public static class KeyGeneratorUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + + #region API Key 生成 + + /// + /// 生成 API Key + /// + /// 密钥长度(字节数) + /// 前缀 + /// API Key + public static string GenerateApiKey(int length = 32, string? prefix = null) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + var key = Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + + return string.IsNullOrEmpty(prefix) ? key : $"{prefix}_{key}"; + } + + /// + /// 生成标准格式的 API Key(如 sk_xxx) + /// + /// 前缀(默认 sk) + /// API Key + public static string GenerateStandardApiKey(string prefix = "sk") + { + return GenerateApiKey(32, prefix); + } + + /// + /// 生成带有校验位的 API Key + /// + /// 前缀 + /// 签名密钥 + /// 带校验位的 API Key + public static string GenerateApiKeyWithChecksum(string? prefix = null, string? secret = null) + { + var bytes = new byte[24]; + _rng.GetBytes(bytes); + var keyPart = Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + + // 计算校验位 + var checksum = ComputeChecksum(keyPart, secret); + var fullKey = $"{keyPart}_{checksum}"; + + return string.IsNullOrEmpty(prefix) ? fullKey : $"{prefix}_{fullKey}"; + } + + /// + /// 验证带校验位的 API Key + /// + /// API Key + /// 签名密钥 + /// 是否有效 + public static bool ValidateApiKeyChecksum(string apiKey, string? secret = null) + { + if (string.IsNullOrEmpty(apiKey)) + { + return false; + } + + var parts = apiKey.Split('_'); + if (parts.Length < 2) + { + return false; + } + + var checksum = parts[^1]; + var keyPart = string.Join("_", parts[..^1]); + + var expectedChecksum = ComputeChecksum(keyPart, secret); + return ConstantTimeEquals(checksum, expectedChecksum); + } + + #endregion + + #region Token 生成 + + /// + /// 生成访问令牌 + /// + /// Token 长度(字节数) + /// 访问令牌 + public static string GenerateAccessToken(int length = 32) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成刷新令牌 + /// + /// 刷新令牌 + public static string GenerateRefreshToken() + { + var bytes = new byte[64]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成一次性令牌(OTP) + /// + /// OTP 长度 + /// 一次性令牌 + public static string GenerateOneTimePassword(int length = 6) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + + var result = new StringBuilder(length); + for (int i = 0; i < length; i++) + { + result.Append(bytes[i] % 10); + } + + return result.ToString(); + } + + /// + /// 生成一次性令牌(字母数字) + /// + /// Token 长度 + /// 一次性令牌 + public static string GenerateOneTimeToken(int length = 16) + { + const string chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 排除易混淆字符 + var bytes = new byte[length]; + _rng.GetBytes(bytes); + + var result = new char[length]; + for (int i = 0; i < length; i++) + { + result[i] = chars[bytes[i] % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成验证码 + /// + /// 验证码长度 + /// 验证码 + public static string GenerateVerificationCode(int length = 6) + { + return GenerateOneTimePassword(length); + } + + #endregion + + #region 密钥生成 + + /// + /// 生成对称加密密钥 + /// + /// 密钥大小(位) + /// Base64 编码的密钥 + public static string GenerateSymmetricKey(int keySize = 256) + { + using var aes = Aes.Create(); + aes.KeySize = keySize; + aes.GenerateKey(); + return Convert.ToBase64String(aes.Key); + } + + /// + /// 生成对称加密密钥(字节数组) + /// + /// 密钥大小(位) + /// 密钥字节数组 + public static byte[] GenerateSymmetricKeyBytes(int keySize = 256) + { + using var aes = Aes.Create(); + aes.KeySize = keySize; + aes.GenerateKey(); + return aes.Key; + } + + /// + /// 生成 RSA 密钥对 + /// + /// 密钥大小(位) + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateRsaKeyPair(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); +#if NET5_0_OR_GREATER + var publicKey = rsa.ExportRSAPublicKeyPem(); + var privateKey = rsa.ExportRSAPrivateKeyPem(); +#else + // netstandard2.1 使用 ToXmlString 或手动转换为 PEM + var publicKey = Convert.ToBase64String(rsa.ExportRSAPublicKey()); + var privateKey = Convert.ToBase64String(rsa.ExportRSAPrivateKey()); +#endif + return (publicKey, privateKey); + } + + /// + /// 生成 RSA 密钥对(XML 格式) + /// + /// 密钥大小(位) + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateRsaKeyPairXml(int keySize = 2048) + { + using var rsa = RSA.Create(keySize); + var publicKey = rsa.ToXmlString(false); + var privateKey = rsa.ToXmlString(true); + return (publicKey, privateKey); + } + + /// + /// 生成 ECDSA 密钥对 + /// + /// 椭圆曲线 + /// 包含公钥和私钥的元组 + public static (string PublicKey, string PrivateKey) GenerateEcdsaKeyPair(ECCurve? curve = null) + { + using var ecdsa = ECDsa.Create(curve ?? ECCurve.NamedCurves.nistP256); +#if NET5_0_OR_GREATER + var publicKey = ecdsa.ExportSubjectPublicKeyInfoPem(); + var privateKey = ecdsa.ExportECPrivateKeyPem(); +#else + // netstandard2.1 使用 Base64 格式 + var publicKey = Convert.ToBase64String(ecdsa.ExportSubjectPublicKeyInfo()); + var privateKey = Convert.ToBase64String(ecdsa.ExportECPrivateKey()); +#endif + return (publicKey, privateKey); + } + + /// + /// 生成 HMAC 密钥 + /// + /// 密钥长度(字节数) + /// Base64 编码的密钥 + public static string GenerateHmacKey(int length = 64) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 IV(初始化向量) + /// + /// IV 长度(字节数) + /// Base64 编码的 IV + public static string GenerateIV(int length = 16) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 Salt(盐值) + /// + /// Salt 长度(字节数) + /// Base64 编码的 Salt + public static string GenerateSalt(int length = 16) + { + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return Convert.ToBase64String(bytes); + } + + #endregion + + #region 密钥派生 + + /// + /// 从密码派生密钥 + /// + /// 密码 + /// 盐值 + /// 密钥长度(字节数) + /// 迭代次数 + /// 派生的密钥 + public static byte[] DeriveKeyFromPassword(string password, byte[] salt, int keyLength = 32, int iterations = 100000) + { + using var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256); + return pbkdf2.GetBytes(keyLength); + } + + /// + /// 从密码派生密钥(Base64 输出) + /// + /// 密码 + /// 盐值(Base64) + /// 密钥长度(字节数) + /// 迭代次数 + /// 派生的密钥(Base64) + public static string DeriveKeyFromPasswordBase64(string password, string salt, int keyLength = 32, int iterations = 100000) + { + var saltBytes = Convert.FromBase64String(salt); + var key = DeriveKeyFromPassword(password, saltBytes, keyLength, iterations); + return Convert.ToBase64String(key); + } + + /// + /// 使用 HKDF 派生密钥 + /// + /// 输入密钥材料 + /// 盐值 + /// 上下文信息 + /// 输出长度 + /// 派生的密钥 + public static byte[] DeriveKeyHKDF(byte[] inputKeyMaterial, byte[] salt, byte[]? info = null, int outputLength = 32) + { + using var hkdf = new HKDFSHA256(); + return hkdf.DeriveKey(inputKeyMaterial, salt, info ?? Array.Empty(), outputLength); + } + + #endregion + + #region 辅助方法 + + private static string ComputeChecksum(string data, string? secret) + { + var bytes = Encoding.UTF8.GetBytes(data); + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret ?? "default_checksum_key")); + var hash = hmac.ComputeHash(bytes); + return Convert.ToBase64String(hash)[..8]; + } + + private static bool ConstantTimeEquals(string a, string b) + { + if (a.Length != b.Length) + { + return false; + } + + var result = 0; + for (int i = 0; i < a.Length; i++) + { + result |= a[i] ^ b[i]; + } + return result == 0; + } + + #endregion + } + + /// + /// HKDF (HMAC-based Key Derivation Function) 实现 + /// + internal class HKDFSHA256 : IDisposable + { + private const int HashLength = 32; + + public byte[] DeriveKey(byte[] inputKeyMaterial, byte[] salt, byte[] info, int outputLength) + { + if (outputLength > 255 * HashLength) + { + throw new ArgumentOutOfRangeException(nameof(outputLength), $"输出长度不能超过 {255 * HashLength} 字节"); + } + + // Extract + var prk = Extract(inputKeyMaterial, salt); + + // Expand + return Expand(prk, info, outputLength); + } + + private byte[] Extract(byte[] inputKeyMaterial, byte[] salt) + { + using var hmac = new HMACSHA256(salt.Length == 0 ? new byte[HashLength] : salt); + return hmac.ComputeHash(inputKeyMaterial); + } + + private byte[] Expand(byte[] prk, byte[] info, int outputLength) + { + var result = new byte[outputLength]; + var blockCount = (int)Math.Ceiling((double)outputLength / HashLength); + + var previousBlock = Array.Empty(); + + using var hmac = new HMACSHA256(prk); + + for (int i = 1; i <= blockCount; i++) + { + var input = new byte[previousBlock.Length + info.Length + 1]; + Buffer.BlockCopy(previousBlock, 0, input, 0, previousBlock.Length); + Buffer.BlockCopy(info, 0, input, previousBlock.Length, info.Length); + input[^1] = (byte)i; + + previousBlock = hmac.ComputeHash(input); + + var bytesToCopy = Math.Min(HashLength, outputLength - (i - 1) * HashLength); + Buffer.BlockCopy(previousBlock, 0, result, (i - 1) * HashLength, bytesToCopy); + } + + return result; + } + + public void Dispose() + { + // HMACSHA256 会在 using 块中自动释放 + } + } +} diff --git a/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs b/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs new file mode 100644 index 0000000..581511e --- /dev/null +++ b/EasyTool.Core/SecurityCategory/PasswordStrengthUtil.cs @@ -0,0 +1,343 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// 密码强度工具类 + /// + public static class PasswordStrengthUtil + { + // 为 netstandard2.1 提供 Math.Log2 的兼容实现 +#if NETSTANDARD2_1 + private static double Log2(double x) => Math.Log(x, 2); +#else + private static double Log2(double x) => Math.Log2(x); +#endif + + /// + /// 检测密码强度 + /// + public static PasswordStrengthResult CheckStrength(string password) + { + var result = new PasswordStrengthResult + { + Password = password + }; + + if (string.IsNullOrEmpty(password)) + { + result.Score = 0; + result.Level = PasswordStrengthLevel.VeryWeak; + result.AddIssue("密码不能为空"); + return result; + } + + var score = 0; + var length = password.Length; + + // 长度评分 + if (length >= 8) score += 1; + if (length >= 12) score += 1; + if (length >= 16) score += 1; + if (length < 6) result.AddIssue("密码长度不足6位"); + + // 包含小写字母 + if (Regex.IsMatch(password, @"[a-z]")) + { + score += 1; + result.HasLowerCase = true; + } + else + { + result.AddIssue("缺少小写字母"); + } + + // 包含大写字母 + if (Regex.IsMatch(password, @"[A-Z]")) + { + score += 1; + result.HasUpperCase = true; + } + else + { + result.AddSuggestion("建议添加大写字母"); + } + + // 包含数字 + if (Regex.IsMatch(password, @"\d")) + { + score += 1; + result.HasDigit = true; + } + else + { + result.AddIssue("缺少数字"); + } + + // 包含特殊字符 + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?~` ")) + { + score += 2; + result.HasSpecialChar = true; + } + else + { + result.AddSuggestion("建议添加特殊字符"); + } + + // 检查连续字符 + if (HasConsecutiveChars(password, 3)) + { + score -= 1; + result.AddIssue("存在连续字符"); + } + + // 检查重复字符 + if (HasRepeatingChars(password, 3)) + { + score -= 1; + result.AddIssue("存在重复字符"); + } + + // 检查常见弱密码 + if (IsCommonPassword(password)) + { + score = Math.Max(0, score - 3); + result.AddIssue("这是一个常见弱密码"); + } + + // 计算熵 + result.Entropy = CalculateEntropy(password); + + // 最终评分 + result.Score = Math.Max(0, Math.Min(10, score)); + + // 确定等级 + result.Level = result.Score switch + { + >= 8 => PasswordStrengthLevel.VeryStrong, + >= 6 => PasswordStrengthLevel.Strong, + >= 4 => PasswordStrengthLevel.Medium, + >= 2 => PasswordStrengthLevel.Weak, + _ => PasswordStrengthLevel.VeryWeak + }; + + return result; + } + + /// + /// 生成强密码 + /// + public static string GenerateStrongPassword(int length = 16, PasswordOptions? options = null) + { + options ??= new PasswordOptions(); + var random = new Random(); + var password = new List(); + + const string lowerChars = "abcdefghijklmnopqrstuvwxyz"; + const string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string digitChars = "0123456789"; + const string specialChars = "!@#$%^&*()_+-=[]{}|;:,.<>?"; + + // 确保每种要求的字符至少有一个 + if (options.RequireLowerCase) + password.Add(lowerChars[random.Next(lowerChars.Length)]); + if (options.RequireUpperCase) + password.Add(upperChars[random.Next(upperChars.Length)]); + if (options.RequireDigit) + password.Add(digitChars[random.Next(digitChars.Length)]); + if (options.RequireSpecialChar) + password.Add(specialChars[random.Next(specialChars.Length)]); + + // 构建可用字符集 + var allChars = ""; + if (options.AllowLowerCase) allChars += lowerChars; + if (options.AllowUpperCase) allChars += upperChars; + if (options.AllowDigit) allChars += digitChars; + if (options.AllowSpecialChar) allChars += specialChars; + + if (string.IsNullOrEmpty(allChars)) + allChars = lowerChars + digitChars; + + // 排除相似字符 + if (options.ExcludeSimilarChars) + allChars = Regex.Replace(allChars, @"[il1Lo0O]", ""); + + // 排除歧义字符 + if (options.ExcludeAmbiguousChars) + allChars = Regex.Replace(allChars, @"[{}[\]()""'`~,;:.<>\\/|]", ""); + + // 填充剩余长度 + while (password.Count < length) + { + password.Add(allChars[random.Next(allChars.Length)]); + } + + // 打乱顺序 + for (int i = password.Count - 1; i > 0; i--) + { + var j = random.Next(i + 1); + (password[i], password[j]) = (password[j], password[i]); + } + + return new string(password.ToArray()); + } + + /// + /// 检查是否为常见弱密码 + /// + public static bool IsCommonPassword(string password) + { + var commonPasswords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "password", "123456", "12345678", "qwerty", "abc123", + "monkey", "master", "dragon", "111111", "baseball", + "iloveyou", "trustno1", "sunshine", "princess", "welcome", + "shadow", "superman", "michael", "football", "letmein", + "password1", "password123", "admin", "root", "test" + }; + + return commonPasswords.Contains(password); + } + + /// + /// 计算密码熵 + /// + public static double CalculateEntropy(string password) + { + if (string.IsNullOrEmpty(password)) + return 0; + + var charPool = 0; + if (Regex.IsMatch(password, @"[a-z]")) charPool += 26; + if (Regex.IsMatch(password, @"[A-Z]")) charPool += 26; + if (Regex.IsMatch(password, @"\d")) charPool += 10; + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?~`]")) charPool += 32; + + if (charPool == 0) return 0; + + return password.Length * Log2(charPool); + } + + /// + /// 检查密码是否过期 + /// + public static bool IsPasswordExpired(DateTime lastChangeDate, int maxAgeDays = 90) + { + return (DateTime.Now - lastChangeDate).TotalDays > maxAgeDays; + } + + /// + /// 估算密码破解时间 + /// + public static TimeSpan EstimateCrackTime(string password, int guessesPerSecond = 1_000_000_000) + { + var entropy = CalculateEntropy(password); + var combinations = Math.Pow(2, entropy); + var seconds = combinations / guessesPerSecond / 2; // 平均尝试次数 + + if (seconds < 1) return TimeSpan.FromMilliseconds(seconds * 1000); + if (seconds < 60) return TimeSpan.FromSeconds(seconds); + if (seconds < 3600) return TimeSpan.FromMinutes(seconds / 60); + if (seconds < 86400) return TimeSpan.FromHours(seconds / 3600); + if (seconds < 2592000) return TimeSpan.FromDays(seconds / 86400); + if (seconds < 31536000) return TimeSpan.FromDays(seconds / 86400); + + return TimeSpan.FromDays(seconds / 86400); + } + + private static bool HasConsecutiveChars(string password, int count) + { + for (int i = 0; i <= password.Length - count; i++) + { + var consecutive = true; + for (int j = 1; j < count && consecutive; j++) + { + if (password[i + j] != password[i] + j && password[i + j] != password[i] - j) + { + consecutive = false; + } + } + if (consecutive) return true; + } + return false; + } + + private static bool HasRepeatingChars(string password, int count) + { + for (int i = 0; i <= password.Length - count; i++) + { + var repeating = true; + for (int j = 1; j < count && repeating; j++) + { + if (password[i + j] != password[i]) + { + repeating = false; + } + } + if (repeating) return true; + } + return false; + } + } + + /// + /// 密码强度结果 + /// + public class PasswordStrengthResult + { + public string Password { get; set; } = ""; + public int Score { get; set; } + public PasswordStrengthLevel Level { get; set; } + public double Entropy { get; set; } + public bool HasLowerCase { get; set; } + public bool HasUpperCase { get; set; } + public bool HasDigit { get; set; } + public bool HasSpecialChar { get; set; } + public List Issues { get; } = new(); + public List Suggestions { get; } = new(); + + internal void AddIssue(string issue) => Issues.Add(issue); + internal void AddSuggestion(string suggestion) => Suggestions.Add(suggestion); + + public string LevelDescription => Level switch + { + PasswordStrengthLevel.VeryStrong => "非常强", + PasswordStrengthLevel.Strong => "强", + PasswordStrengthLevel.Medium => "中等", + PasswordStrengthLevel.Weak => "弱", + _ => "非常弱" + }; + } + + /// + /// 密码强度等级 + /// + public enum PasswordStrengthLevel + { + VeryWeak = 0, + Weak = 1, + Medium = 2, + Strong = 3, + VeryStrong = 4 + } + + /// + /// 密码生成选项 + /// + public class PasswordOptions + { + public bool AllowLowerCase { get; set; } = true; + public bool AllowUpperCase { get; set; } = true; + public bool AllowDigit { get; set; } = true; + public bool AllowSpecialChar { get; set; } = true; + public bool RequireLowerCase { get; set; } = true; + public bool RequireUpperCase { get; set; } = true; + public bool RequireDigit { get; set; } = true; + public bool RequireSpecialChar { get; set; } = false; + public bool ExcludeSimilarChars { get; set; } = true; + public bool ExcludeAmbiguousChars { get; set; } = false; + } +} diff --git a/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs b/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs new file mode 100644 index 0000000..c62307a --- /dev/null +++ b/EasyTool.Core/SecurityCategory/SecureRandomUtil.cs @@ -0,0 +1,533 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// 安全随机数生成器,使用加密安全的随机数生成器 + /// + public static class SecureRandomUtil + { + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + private static readonly char[] _alphanumericChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); + private static readonly char[] _lowercaseChars = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); + private static readonly char[] _uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); + private static readonly char[] _digitChars = "0123456789".ToCharArray(); + private static readonly char[] _specialChars = "!@#$%^&*()_+-=[]{}|;:,.<>?".ToCharArray(); + private static readonly char[] _hexChars = "0123456789abcdef".ToCharArray(); + + #region 基本随机数生成 + + /// + /// 生成指定长度的随机字节数组 + /// + /// 字节长度 + /// 随机字节数组 + public static byte[] GetBytes(int length) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于0"); + } + + var bytes = new byte[length]; + _rng.GetBytes(bytes); + return bytes; + } + + /// + /// 生成随机整数 + /// + /// 非负随机整数 + public static int GetInt() + { + return GetInt(0, int.MaxValue); + } + + /// + /// 生成指定范围内的随机整数 + /// + /// 最小值(包含) + /// 最大值(不包含) + /// 随机整数 + public static int GetInt(int min, int max) + { + if (min >= max) + { + throw new ArgumentOutOfRangeException(nameof(max), "最大值必须大于最小值"); + } + + var range = (long)max - min; + var bytes = new byte[4]; + + // 使用拒绝采样避免模偏差 + while (true) + { + _rng.GetBytes(bytes); + var value = BitConverter.ToUInt32(bytes, 0); + var remainder = range * (value / range); + + if (remainder <= uint.MaxValue - range) + { + return (int)(min + (value - remainder)); + } + } + } + + /// + /// 生成随机长整数 + /// + /// 最小值(包含) + /// 最大值(不包含) + /// 随机长整数 + public static long GetLong(long min, long max) + { + if (min >= max) + { + throw new ArgumentOutOfRangeException(nameof(max), "最大值必须大于最小值"); + } + + var range = (decimal)max - min; + var bytes = new byte[8]; + + while (true) + { + _rng.GetBytes(bytes); + var value = (decimal)BitConverter.ToUInt64(bytes, 0); + var remainder = range * (value / range); + + if (remainder <= ulong.MaxValue - range) + { + return (long)(min + (value - remainder)); + } + } + } + + /// + /// 生成随机双精度浮点数(0.0 到 1.0) + /// + /// 随机双精度浮点数 + public static double GetDouble() + { + var bytes = new byte[8]; + _rng.GetBytes(bytes); + var value = BitConverter.ToUInt64(bytes, 0); + return value / (double)ulong.MaxValue; + } + + /// + /// 生成随机布尔值 + /// + /// 随机布尔值 + public static bool GetBool() + { + var bytes = new byte[1]; + _rng.GetBytes(bytes); + return (bytes[0] & 1) == 1; + } + + #endregion + + #region 字符串生成 + + /// + /// 生成随机字符串(字母数字) + /// + /// 字符串长度 + /// 随机字符串 + public static string GetString(int length) + { + return GetString(length, _alphanumericChars); + } + + /// + /// 使用指定字符集生成随机字符串 + /// + /// 字符串长度 + /// 字符集 + /// 随机字符串 + public static string GetString(int length, char[] chars) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "长度必须大于0"); + } + + var result = new char[length]; + var bytes = new byte[length * 2]; + + _rng.GetBytes(bytes); + + for (int i = 0; i < length; i++) + { + var value = BitConverter.ToUInt16(bytes, i * 2); + result[i] = chars[value % chars.Length]; + } + + return new string(result); + } + + /// + /// 生成随机小写字符串 + /// + /// 字符串长度 + /// 随机小写字符串 + public static string GetLowercaseString(int length) + { + return GetString(length, _lowercaseChars); + } + + /// + /// 生成随机大写字符串 + /// + /// 字符串长度 + /// 随机大写字符串 + public static string GetUppercaseString(int length) + { + return GetString(length, _uppercaseChars); + } + + /// + /// 生成随机数字字符串 + /// + /// 字符串长度 + /// 随机数字字符串 + public static string GetNumericString(int length) + { + return GetString(length, _digitChars); + } + + /// + /// 生成随机十六进制字符串 + /// + /// 字符串长度 + /// 是否使用大写字母 + /// 随机十六进制字符串 + public static string GetHexString(int length, bool uppercase = false) + { + var result = GetString(length, _hexChars); + return uppercase ? result.ToUpperInvariant() : result; + } + + /// + /// 生成随机 Base64 字符串 + /// + /// 原始字节长度 + /// Base64 编码的随机字符串 + public static string GetBase64String(int byteLength) + { + var bytes = GetBytes(byteLength); + return Convert.ToBase64String(bytes); + } + + /// + /// 生成 URL 安全的随机字符串 + /// + /// 原始字节长度 + /// URL 安全的随机字符串 + public static string GetUrlSafeString(int byteLength) + { + var bytes = GetBytes(byteLength); + return Convert.ToBase64String(bytes) + .Replace("+", "-") + .Replace("/", "_") + .TrimEnd('='); + } + + #endregion + + #region 密码生成 + + /// + /// 生成随机密码 + /// + /// 密码长度 + /// 包含小写字母 + /// 包含大写字母 + /// 包含数字 + /// 包含特殊字符 + /// 随机密码 + public static string GeneratePassword(int length = 16, + bool includeLowercase = true, + bool includeUppercase = true, + bool includeDigits = true, + bool includeSpecial = true) + { + if (length <= 0) + { + throw new ArgumentOutOfRangeException(nameof(length), "密码长度必须大于0"); + } + + var charSets = new List(); + var allChars = new List(); + + if (includeLowercase) + { + charSets.Add(_lowercaseChars); + allChars.AddRange(_lowercaseChars); + } + + if (includeUppercase) + { + charSets.Add(_uppercaseChars); + allChars.AddRange(_uppercaseChars); + } + + if (includeDigits) + { + charSets.Add(_digitChars); + allChars.AddRange(_digitChars); + } + + if (includeSpecial) + { + charSets.Add(_specialChars); + allChars.AddRange(_specialChars); + } + + if (charSets.Count == 0) + { + throw new ArgumentException("至少需要选择一种字符类型"); + } + + var result = new char[length]; + var allCharsArray = allChars.ToArray(); + + // 确保每种选中的字符类型至少有一个字符 + var usedCharsets = Math.Min(charSets.Count, length); + for (int i = 0; i < usedCharsets; i++) + { + result[i] = GetString(1, charSets[i])[0]; + } + + // 填充剩余字符 + for (int i = usedCharsets; i < length; i++) + { + result[i] = GetString(1, allCharsArray)[0]; + } + + // 打乱顺序 + Shuffle(result); + + return new string(result); + } + + /// + /// 生成强密码(至少包含一个大写、小写、数字和特殊字符) + /// + /// 密码长度(至少4) + /// 强密码 + public static string GenerateStrongPassword(int length = 16) + { + if (length < 4) + { + throw new ArgumentOutOfRangeException(nameof(length), "强密码长度至少为4"); + } + + return GeneratePassword(length, true, true, true, true); + } + + /// + /// 生成 PIN 码(纯数字) + /// + /// PIN 码长度 + /// PIN 码 + public static string GeneratePin(int length = 6) + { + return GetNumericString(length); + } + + #endregion + + #region 集合操作 + + /// + /// 从数组中随机选择一个元素 + /// + /// 元素类型 + /// 源数组 + /// 随机选择的元素 + public static T Choice(T[] array) + { + if (array == null || array.Length == 0) + { + throw new ArgumentException("数组不能为空", nameof(array)); + } + + var index = GetInt(0, array.Length); + return array[index]; + } + + /// + /// 从列表中随机选择一个元素 + /// + /// 元素类型 + /// 源列表 + /// 随机选择的元素 + public static T Choice(IList list) + { + if (list == null || list.Count == 0) + { + throw new ArgumentException("列表不能为空", nameof(list)); + } + + var index = GetInt(0, list.Count); + return list[index]; + } + + /// + /// 从数组中随机选择多个元素(不重复) + /// + /// 元素类型 + /// 源数组 + /// 选择数量 + /// 随机选择的元素数组 + public static T[] Sample(T[] array, int count) + { + if (array == null || array.Length == 0) + { + throw new ArgumentException("数组不能为空", nameof(array)); + } + + if (count <= 0 || count > array.Length) + { + throw new ArgumentOutOfRangeException(nameof(count), "选择数量必须在1到数组长度之间"); + } + + var indices = new int[array.Length]; + for (int i = 0; i < indices.Length; i++) + { + indices[i] = i; + } + + Shuffle(indices); + + var result = new T[count]; + for (int i = 0; i < count; i++) + { + result[i] = array[indices[i]]; + } + + return result; + } + + /// + /// 原地打乱数组顺序(Fisher-Yates 洗牌算法) + /// + /// 元素类型 + /// 要打乱的数组 + public static void Shuffle(T[] array) + { + if (array == null || array.Length <= 1) + { + return; + } + + for (int i = array.Length - 1; i > 0; i--) + { + var j = GetInt(0, i + 1); + (array[i], array[j]) = (array[j], array[i]); + } + } + + /// + /// 原地打乱列表顺序 + /// + /// 元素类型 + /// 要打乱的列表 + public static void Shuffle(IList list) + { + if (list == null || list.Count <= 1) + { + return; + } + + for (int i = list.Count - 1; i > 0; i--) + { + var j = GetInt(0, i + 1); + (list[i], list[j]) = (list[j], list[i]); + } + } + + #endregion + + #region GUID 和 UUID + + /// + /// 生成随机 GUID + /// + /// 随机 GUID + public static Guid GetGuid() + { + var bytes = GetBytes(16); + return new Guid(bytes); + } + + /// + /// 生成 UUID v4(随机 UUID) + /// + /// UUID v4 字符串 + public static string GetUuidV4() + { + var bytes = GetBytes(16); + + // 设置版本位(版本4) + bytes[6] = (byte)((bytes[6] & 0x0F) | 0x40); + + // 设置变体位 + bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80); + + return new Guid(bytes).ToString(); + } + + #endregion + + #region 填充 + + /// + /// 用随机字节填充数组 + /// + /// 目标数组 + public static void Fill(byte[] buffer) + { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + _rng.GetBytes(buffer); + } + + /// + /// 用随机字节填充数组的一部分 + /// + /// 目标数组 + /// 起始位置 + /// 填充数量 + public static void Fill(byte[] buffer, int offset, int count) + { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + if (offset < 0 || offset >= buffer.Length) + { + throw new ArgumentOutOfRangeException(nameof(offset)); + } + + if (count <= 0 || offset + count > buffer.Length) + { + throw new ArgumentOutOfRangeException(nameof(count)); + } + + var segment = new ArraySegment(buffer, offset, count).ToArray(); + _rng.GetBytes(segment); + Array.Copy(segment, 0, buffer, offset, count); + } + + #endregion + } +} diff --git a/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs b/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs new file mode 100644 index 0000000..e3ec37e --- /dev/null +++ b/EasyTool.Core/SecurityCategory/SqlInjectionUtil.cs @@ -0,0 +1,312 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// SQL注入防护工具类 + /// + public static class SqlInjectionUtil + { + private static readonly Regex SqlKeywordsPattern = new( + @"\b(select|insert|update|delete|drop|create|alter|truncate|exec|execute|xp_|sp_|declare|cast|convert|union|join|where|from|into|values|set|order|group|having|limit|offset)\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex SqlCommentPattern = new( + @"(--)|(/\*)|(\*/)", + RegexOptions.Compiled); + + private static readonly Regex SqlQuotePattern = new( + @"('|""|`)", + RegexOptions.Compiled); + + private static readonly Regex SqlSemicolonPattern = new( + @";", + RegexOptions.Compiled); + + private static readonly Regex SqlDangerousPattern = new( + @"(\b(OR|AND)\s*['""]?\d+['""]?\s*=\s*['""]?\d+)|" + // OR 1=1 + @"(\b(OR|AND)\s*['""][^'""]*['""]\s*=\s*['""][^'""]*['""])|" + // OR 'a'='a' + @"(UNION\s+(ALL\s+)?SELECT)|" + // UNION SELECT + @"(EXEC\s+)|" + // EXEC + @"(Xp_\w+)|" + // xp_cmdshell等 + @"(WAITFOR\s+DELAY)|" + // WAITFOR DELAY + @"(BENCHMARK\s*\()|" + // MySQL BENCHMARK + @"(SLEEP\s*\()", // MySQL SLEEP + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly HashSet DangerousKeywords = new(StringComparer.OrdinalIgnoreCase) + { + "xp_cmdshell", "xp_regread", "xp_regwrite", "xp_regdelete", + "sp_executesql", "sp_oacreate", "sp_oamethod", + "information_schema", "sysobjects", "syscolumns", + "pg_catalog", "pg_class", "pg_attribute" + }; + + /// + /// 检测是否存在SQL注入风险 + /// + public static bool HasSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + // 检测危险模式 + if (SqlDangerousPattern.IsMatch(input)) + return true; + + // 检测注释 + if (SqlCommentPattern.IsMatch(input)) + return true; + + // 检测危险关键字组合 + var upperInput = input.ToUpperInvariant(); + foreach (var keyword in DangerousKeywords) + { + if (upperInput.Contains(keyword.ToUpperInvariant())) + return true; + } + + // 检测单引号后面跟SQL关键字 + if (Regex.IsMatch(input, @"'\s*(OR|AND|UNION|SELECT|INSERT|UPDATE|DELETE)", RegexOptions.IgnoreCase)) + return true; + + return false; + } + + /// + /// 转义SQL字符串参数 + /// + public static string EscapeString(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + switch (c) + { + case '\'': + result.Append("''"); + break; + case '\\': + result.Append("\\\\"); + break; + case '\0': + result.Append("\\0"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\x1a': + result.Append("\\Z"); + break; + default: + result.Append(c); + break; + } + } + return result.ToString(); + } + + /// + /// 移除潜在的SQL注入字符 + /// + public static string Sanitize(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + + // 移除注释 + result = SqlCommentPattern.Replace(result, ""); + + // 移除分号(防止多语句执行) + result = SqlSemicolonPattern.Replace(result, ""); + + // 转义引号 + result = SqlQuotePattern.Replace(result, m => m.Value == "'" ? "''" : "\\" + m.Value); + + return result; + } + + /// + /// 过滤SQL关键字 + /// + public static string FilterKeywords(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return SqlKeywordsPattern.Replace(input, ""); + } + + /// + /// 验证标识符(表名、列名等) + /// + public static bool IsValidIdentifier(string identifier) + { + if (string.IsNullOrEmpty(identifier)) + return false; + + // 只允许字母、数字、下划线 + if (!Regex.IsMatch(identifier, @"^[a-zA-Z_][a-zA-Z0-9_]*$")) + return false; + + // 不能是SQL关键字 + if (SqlKeywordsPattern.IsMatch(identifier)) + return false; + + return true; + } + + /// + /// 安全的标识符包装 + /// + public static string QuoteIdentifier(string identifier, string quoteChar = "`") + { + if (string.IsNullOrEmpty(identifier)) + return identifier; + + // 转义内部的引号 + identifier = identifier.Replace(quoteChar, quoteChar + quoteChar); + return $"{quoteChar}{identifier}{quoteChar}"; + } + + /// + /// 构建安全的IN子句参数 + /// + public static string BuildInClause(IEnumerable values, bool numeric = false) + { + var items = new List(); + foreach (var value in values) + { + if (numeric && int.TryParse(value, out _)) + { + items.Add(value); + } + else + { + items.Add($"'{EscapeString(value)}'"); + } + } + return string.Join(", ", items); + } + + /// + /// 构建安全的LIKE子句 + /// + public static string EscapeLikePattern(string pattern) + { + if (string.IsNullOrEmpty(pattern)) + return pattern; + + var result = new StringBuilder(); + foreach (var c in pattern) + { + switch (c) + { + case '%': + case '_': + case '[': + case ']': + result.Append('\\'); + break; + } + result.Append(c); + } + return result.ToString(); + } + + /// + /// 检测批量SQL注入 + /// + public static Dictionary CheckMultiple(IEnumerable> inputs) + { + var results = new Dictionary(); + foreach (var kvp in inputs) + { + results[kvp.Key] = HasSqlInjection(kvp.Value); + } + return results; + } + + /// + /// 获取SQL注入风险详情 + /// + public static SqlInjectionAnalysis Analyze(string input) + { + var analysis = new SqlInjectionAnalysis + { + Input = input, + HasRisk = false + }; + + if (string.IsNullOrEmpty(input)) + return analysis; + + // 检测各种风险 + if (SqlKeywordsPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含SQL关键字"); + } + + if (SqlCommentPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含SQL注释"); + } + + if (SqlDangerousPattern.IsMatch(input)) + { + analysis.HasRisk = true; + analysis.Risks.Add("包含危险SQL模式"); + } + + foreach (var keyword in DangerousKeywords) + { + if (input.Contains(keyword, StringComparison.OrdinalIgnoreCase)) + { + analysis.HasRisk = true; + analysis.DetectedKeywords.Add(keyword); + } + } + + return analysis; + } + } + + /// + /// SQL注入分析结果 + /// + public class SqlInjectionAnalysis + { + /// + /// 输入字符串 + /// + public string Input { get; set; } = ""; + + /// + /// 是否有风险 + /// + public bool HasRisk { get; set; } + + /// + /// 检测到的风险列表 + /// + public List Risks { get; } = new(); + + /// + /// 检测到的危险关键字 + /// + public List DetectedKeywords { get; } = new(); + } +} diff --git a/EasyTool.Core/SecurityCategory/TlsUtil.cs b/EasyTool.Core/SecurityCategory/TlsUtil.cs new file mode 100644 index 0000000..4cfbb50 --- /dev/null +++ b/EasyTool.Core/SecurityCategory/TlsUtil.cs @@ -0,0 +1,424 @@ +using System; +using System.Collections.Generic; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; + +using System.Text; + +namespace EasyTool.SecurityCategory +{ + /// + /// TLS/SSL 配置和验证工具类 + /// + public static class TlsUtil + { + #region SSL/TLS 协议配置 + + /// + /// 获取支持的 SSL/TLS 协议 + /// + /// 支持的协议列表 + public static SslProtocols GetSupportedProtocols() + { +#if NETSTANDARD2_1 + // netstandard2.1 不支持 TLS 1.3 + return SslProtocols.Tls12; +#else + return SslProtocols.Tls12 | SslProtocols.Tls13; +#endif + } + + /// + /// 获取安全的 SSL/TLS 协议(排除不安全版本) + /// + /// 安全的协议 + public static SslProtocols GetSecureProtocols() + { +#if NETSTANDARD2_1 + return SslProtocols.Tls12; +#else + return SslProtocols.Tls12 | SslProtocols.Tls13; +#endif + } + + /// + /// 检查协议是否安全 + /// + /// 要检查的协议 + /// 是否安全 + public static bool IsSecureProtocol(SslProtocols protocol) + { + // SSL 2.0, SSL 3.0, TLS 1.0, TLS 1.1 被认为不安全 + // 注意:Ssl2 和 Ssl3 已被标记为过时,这里使用数值表示 + var insecureProtocols = (SslProtocols)12 | SslProtocols.Tls | SslProtocols.Tls11; // 12 = Ssl2(12) | Ssl3(48) 的等效值 + + return (protocol & insecureProtocols) == 0 && + (protocol & SslProtocols.Tls12) != 0; + } + + #endregion + + #region 证书验证 + + /// + /// 创建证书验证回调(验证服务器证书) + /// + /// 是否允许无效证书 + /// 是否验证证书链 + /// 远程证书验证回调 + public static RemoteCertificateValidationCallback CreateCertificateValidationCallback( + bool allowInvalidCerts = false, + bool validateChain = true) + { + return (sender, certificate, chain, sslPolicyErrors) => + { + // 如果允许无效证书,直接返回 true + if (allowInvalidCerts) + { + return true; + } + + // 如果没有错误,返回 true + if (sslPolicyErrors == SslPolicyErrors.None) + { + return true; + } + + // 如果不验证证书链,只检查是否有证书 + if (!validateChain) + { + return certificate != null; + } + + // 默认:严格验证 + return false; + }; + } + + /// + /// 创建验证特定域名的证书验证回调 + /// + /// 允许的域名列表 + /// 远程证书验证回调 + public static RemoteCertificateValidationCallback CreateDomainValidationCallback(params string[] allowedDomains) + { + return (sender, certificate, chain, sslPolicyErrors) => + { + if (sslPolicyErrors == SslPolicyErrors.None) + { + return true; + } + + // 检查域名不匹配的情况 + if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) + { + if (certificate is X509Certificate2 cert && allowedDomains.Length > 0) + { + var certDomain = cert.GetNameInfo(X509NameType.DnsName, false); + foreach (var domain in allowedDomains) + { + if (MatchesDomain(certDomain, domain)) + { + return true; + } + } + } + } + + return false; + }; + } + + /// + /// 验证证书有效性 + /// + /// 证书 + /// 是否检查吊销状态 + /// 验证结果 + public static CertificateValidationResult ValidateCertificate(X509Certificate2 certificate, bool checkRevocation = false) + { + var result = new CertificateValidationResult { IsValid = true }; + + if (certificate == null) + { + return new CertificateValidationResult { IsValid = false, Errors = new List { "证书为空" } }; + } + + // 检查证书是否过期 + var now = DateTime.UtcNow; + if (certificate.NotBefore > now) + { + return new CertificateValidationResult + { + IsValid = false, + Errors = new List { $"证书尚未生效,生效时间: {certificate.NotBefore}" } + }; + } + + if (certificate.NotAfter < now) + { + return new CertificateValidationResult + { + IsValid = false, + Errors = new System.Collections.Generic.List { $"证书已过期,过期时间: {certificate.NotAfter}" } + }; + } + + // 检查证书链 + using var chain = new X509Chain(); + chain.ChainPolicy.RevocationMode = checkRevocation + ? X509RevocationMode.Online + : X509RevocationMode.NoCheck; + + if (!chain.Build(certificate)) + { + var errors = new System.Collections.Generic.List(); + foreach (var status in chain.ChainStatus) + { + errors.Add(status.StatusInformation); + } + + return new CertificateValidationResult + { + IsValid = false, + Errors = new System.Collections.Generic.List { $"证书链验证失败: {string.Join(", ", errors)}" } + }; + } + + result.Subject = certificate.Subject; + result.Issuer = certificate.Issuer; + result.NotBefore = certificate.NotBefore; + result.NotAfter = certificate.NotAfter; + result.Thumbprint = certificate.Thumbprint; + + return result; + } + + #endregion + + #region 证书加载 + + /// + /// 从文件加载证书 + /// + /// 证书文件路径 + /// 密码(可选) + /// X509 证书 + public static X509Certificate2 LoadCertificate(string filePath, string? password = null) + { + if (string.IsNullOrEmpty(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + + if (!System.IO.File.Exists(filePath)) + { + throw new System.IO.FileNotFoundException("证书文件不存在", filePath); + } + + return string.IsNullOrEmpty(password) + ? new X509Certificate2(filePath) + : new X509Certificate2(filePath, password); + } + + /// + /// 从 PFX 文件加载证书 + /// + /// PFX 文件路径 + /// 密码 + /// X509 证书 + public static X509Certificate2 LoadPfxCertificate(string filePath, string password) + { + return LoadCertificate(filePath, password); + } + + /// + /// 从 PEM 文件加载证书 + /// + /// 证书文件路径 + /// 私钥文件路径(可选) + /// X509 证书 + public static X509Certificate2 LoadPemCertificate(string certPath, string? keyPath = null) + { + if (string.IsNullOrEmpty(certPath)) + { + throw new ArgumentNullException(nameof(certPath)); + } + +#if NETSTANDARD2_1 + // netstandard2.1 不支持 CreateFromPem,使用替代方案 + var certBytes = System.IO.File.ReadAllBytes(certPath); + return new X509Certificate2(certBytes); +#else + var certPem = System.IO.File.ReadAllText(certPath); + + if (string.IsNullOrEmpty(keyPath)) + { + return X509Certificate2.CreateFromPem(certPem); + } + + var keyPem = System.IO.File.ReadAllText(keyPath); + return X509Certificate2.CreateFromPem(certPem, keyPem); +#endif + } + + /// + /// 从证书存储区加载证书 + /// + /// 存储区名称 + /// 存储区位置 + /// 证书指纹 + /// X509 证书 + public static X509Certificate2? LoadCertificateFromStore(StoreName storeName, StoreLocation storeLocation, string thumbprint) + { + using var store = new X509Store(storeName, storeLocation); + store.Open(OpenFlags.ReadOnly); + + var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false); + + return certificates.Count > 0 ? certificates[0] : null; + } + + #endregion + + #region 证书信息 + + /// + /// 获取证书信息 + /// + /// 证书 + /// 证书信息 + public static CertificateInfo GetCertificateInfo(X509Certificate2 certificate) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + return new CertificateInfo + { + Subject = certificate.Subject, + Issuer = certificate.Issuer, + NotBefore = certificate.NotBefore, + NotAfter = certificate.NotAfter, + Thumbprint = certificate.Thumbprint, + SerialNumber = certificate.SerialNumber, + HasPrivateKey = certificate.HasPrivateKey, + KeySize = certificate.GetRSAPublicKey()?.KeySize ?? 0, + SignatureAlgorithm = certificate.SignatureAlgorithm.FriendlyName ?? "Unknown" + }; + } + + /// + /// 检查证书是否即将过期 + /// + /// 证书 + /// 提前天数阈值 + /// 是否即将过期 + public static bool IsCertificateExpiringSoon(X509Certificate2 certificate, int daysThreshold = 30) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + var timeRemaining = certificate.NotAfter - DateTime.UtcNow; + return timeRemaining.TotalDays <= daysThreshold; + } + + /// + /// 获取证书剩余有效天数 + /// + /// 证书 + /// 剩余有效天数 + public static int GetDaysUntilExpiration(X509Certificate2 certificate) + { + if (certificate == null) + { + throw new ArgumentNullException(nameof(certificate)); + } + + var timeRemaining = certificate.NotAfter - DateTime.UtcNow; + return Math.Max(0, (int)timeRemaining.TotalDays); + } + + #endregion + + #region SSL 选项 + + /// + /// 创建服务器 SSL 选项 + /// + /// 目标主机 + /// 服务器证书 + /// SSL 选项 + public static SslServerAuthenticationOptions CreateServerSslOptions(string targetHost, X509Certificate2? serverCertificate = null) + { + var options = new SslServerAuthenticationOptions + { + EnabledSslProtocols = GetSecureProtocols(), + ClientCertificateRequired = false + }; + + if (serverCertificate != null) + { + options.ServerCertificate = serverCertificate; + } + + return options; + } + + /// + /// 创建客户端 SSL 选项 + /// + /// 客户端证书 + /// 是否允许无效的服务器证书 + /// SSL 选项 + public static SslClientAuthenticationOptions CreateClientSslOptions( + X509Certificate2? clientCertificate = null, + bool allowInvalidServerCert = false) + { + var options = new SslClientAuthenticationOptions + { + EnabledSslProtocols = GetSecureProtocols(), + RemoteCertificateValidationCallback = CreateCertificateValidationCallback(allowInvalidServerCert) + }; + + if (clientCertificate != null) + { + options.ClientCertificates = new X509Certificate2Collection { clientCertificate }; + } + + return options; + } + + #endregion + + #region 辅助方法 + + private static bool MatchesDomain(string? certDomain, string allowedDomain) + { + if (string.IsNullOrEmpty(certDomain)) + { + return false; + } + + // 支持通配符域名 + if (certDomain.StartsWith("*.")) + { + var certBaseDomain = certDomain[2..]; + return allowedDomain.EndsWith(certBaseDomain, StringComparison.OrdinalIgnoreCase) || + allowedDomain.Equals(certBaseDomain, StringComparison.OrdinalIgnoreCase); + } + + return certDomain.Equals(allowedDomain, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + } + + } + + \ No newline at end of file diff --git a/EasyTool.Core/SecurityCategory/XssUtil.cs b/EasyTool.Core/SecurityCategory/XssUtil.cs new file mode 100644 index 0000000..c0126fa --- /dev/null +++ b/EasyTool.Core/SecurityCategory/XssUtil.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.SecurityCategory +{ + /// + /// XSS防护工具类 + /// + public static class XssUtil + { + private static readonly Dictionary HtmlEntityEncodeMap = new() + { + { "<", "<" }, + { ">", ">" }, + { "&", "&" }, + { "\"", """ }, + { "'", "'" }, + { "/", "/" }, + { "`", "`" }, + { "=", "=" } + }; + + private static readonly HashSet AllowedTags = new(StringComparer.OrdinalIgnoreCase) + { + "b", "i", "u", "strong", "em", "p", "br", "span", "div", "a", "img", + "ul", "ol", "li", "h1", "h2", "h3", "h4", "h5", "h6", + "table", "thead", "tbody", "tr", "td", "th", "blockquote", "pre", "code" + }; + + private static readonly HashSet AllowedAttributes = new(StringComparer.OrdinalIgnoreCase) + { + "href", "src", "alt", "title", "class", "id", "style" + }; + + private static readonly Regex ScriptPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex EventPattern = new(@"\s*on\w+\s*=", RegexOptions.IgnoreCase); + private static readonly Regex JavaScriptPattern = new(@"javascript\s*:", RegexOptions.IgnoreCase); + private static readonly Regex VbscriptPattern = new(@"vbscript\s*:", RegexOptions.IgnoreCase); + private static readonly Regex DataUrlPattern = new(@"data\s*:", RegexOptions.IgnoreCase); + private static readonly Regex HtmlCommentPattern = new(@"", RegexOptions.Singleline); + private static readonly Regex SvgPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex IframePattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex ObjectPattern = new(@"]*>.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex EmbedPattern = new(@"]*>", RegexOptions.IgnoreCase); + private static readonly Regex ExpressionPattern = new(@"expression\s*\(", RegexOptions.IgnoreCase); + + /// + /// HTML实体编码 + /// + public static string HtmlEncode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(input.Length); + foreach (var c in input) + { + var str = c.ToString(); + result.Append(HtmlEntityEncodeMap.TryGetValue(str, out var encoded) ? encoded : str); + } + return result.ToString(); + } + + /// + /// HTML实体解码 + /// + public static string HtmlDecode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + foreach (var kvp in HtmlEntityEncodeMap) + { + result = result.Replace(kvp.Value, kvp.Key); + } + + // 解码数字实体 + result = Regex.Replace(result, @"&#(\d+);", m => + { + var code = int.Parse(m.Groups[1].Value); + return ((char)code).ToString(); + }); + + // 解码十六进制实体 + result = Regex.Replace(result, @"&#x([0-9a-fA-F]+);", m => + { + var code = Convert.ToInt32(m.Groups[1].Value, 16); + return ((char)code).ToString(); + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 过滤XSS攻击代码 + /// + public static string Sanitize(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = input; + + // 移除脚本标签 + result = ScriptPattern.Replace(result, ""); + result = SvgPattern.Replace(result, ""); + result = IframePattern.Replace(result, ""); + result = ObjectPattern.Replace(result, ""); + result = EmbedPattern.Replace(result, ""); + + // 移除事件处理器 + result = EventPattern.Replace(result, ""); + + // 移除危险协议 + result = JavaScriptPattern.Replace(result, ""); + result = VbscriptPattern.Replace(result, ""); + result = DataUrlPattern.Replace(result, ""); + + // 移除CSS表达式 + result = ExpressionPattern.Replace(result, ""); + + // 移除HTML注释 + result = HtmlCommentPattern.Replace(result, ""); + + return result; + } + + /// + /// 清理HTML标签(只保留允许的标签和属性) + /// + public static string CleanHtml(string input, IEnumerable? allowedTags = null, IEnumerable? allowedAttributes = null) + { + if (string.IsNullOrEmpty(input)) + return input; + + var tags = allowedTags != null ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) : AllowedTags; + var attrs = allowedAttributes != null ? new HashSet(allowedAttributes, StringComparer.OrdinalIgnoreCase) : AllowedAttributes; + + // 先进行基本清理 + var result = Sanitize(input); + + // 移除不允许的标签 + result = Regex.Replace(result, @"]*>", m => + { + var tagName = m.Groups[1].Value; + if (!tags.Contains(tagName)) + { + return ""; + } + + // 保留标签但移除不允许的属性 + var tagContent = m.Value; + tagContent = Regex.Replace(tagContent, @"(\w+)\s*=\s*[""'][^""']*[""']", attrMatch => + { + var attrName = Regex.Match(attrMatch.Value, @"\w+").Value; + return attrs.Contains(attrName) ? attrMatch.Value : ""; + }); + + return tagContent; + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 移除所有HTML标签 + /// + public static string StripHtml(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return Regex.Replace(input, @"<[^>]*>", ""); + } + + /// + /// 验证是否包含XSS攻击代码 + /// + public static bool ContainsXss(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + return ScriptPattern.IsMatch(input) || + EventPattern.IsMatch(input) || + JavaScriptPattern.IsMatch(input) || + VbscriptPattern.IsMatch(input) || + DataUrlPattern.IsMatch(input) || + ExpressionPattern.IsMatch(input) || + SvgPattern.IsMatch(input) || + IframePattern.IsMatch(input) || + ObjectPattern.IsMatch(input) || + EmbedPattern.IsMatch(input); + } + + /// + /// 安全的URL编码 + /// + public static string SafeUrlEncode(string url) + { + if (string.IsNullOrEmpty(url)) + return url; + + // 检查危险的协议 + var lowerUrl = url.ToLower(); + if (lowerUrl.StartsWith("javascript:") || lowerUrl.StartsWith("vbscript:") || lowerUrl.StartsWith("data:")) + { + return ""; + } + + return Uri.EscapeUriString(url); + } + + /// + /// 验证URL是否安全 + /// + public static bool IsUrlSafe(string url) + { + if (string.IsNullOrEmpty(url)) + return false; + + var lowerUrl = url.ToLower(); + if (lowerUrl.StartsWith("javascript:") || lowerUrl.StartsWith("vbscript:") || lowerUrl.StartsWith("data:")) + { + return false; + } + + return Uri.TryCreate(url, UriKind.Absolute, out _); + } + + /// + /// 清理CSS样式(移除表达式和URL) + /// + public static string CleanCss(string css) + { + if (string.IsNullOrEmpty(css)) + return css; + + var result = css; + + // 移除expression + result = ExpressionPattern.Replace(result, ""); + + // 移除url() + result = Regex.Replace(result, @"url\s*\([^)]*\)", "", RegexOptions.IgnoreCase); + + // 移除behavior + result = Regex.Replace(result, @"behavior\s*:", "", RegexOptions.IgnoreCase); + + // 移除-moz-binding + result = Regex.Replace(result, @"-moz-binding\s*:", "", RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 安全的JSON字符串 + /// + public static string SafeJsonString(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(); + foreach (var c in input) + { + switch (c) + { + case '"': + result.Append("\\\""); + break; + case '\\': + result.Append("\\\\"); + break; + case '\b': + result.Append("\\b"); + break; + case '\f': + result.Append("\\f"); + break; + case '\n': + result.Append("\\n"); + break; + case '\r': + result.Append("\\r"); + break; + case '\t': + result.Append("\\t"); + break; + default: + if (c < 32) + { + result.Append($"\\u{(int)c:X4}"); + } + else + { + result.Append(c); + } + break; + } + } + return result.ToString(); + } + + /// + /// 属性值转义 + /// + public static string EscapeAttribute(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var result = new StringBuilder(); + foreach (var c in input) + { + switch (c) + { + case '<': + result.Append("<"); + break; + case '>': + result.Append(">"); + break; + case '"': + result.Append("""); + break; + case '\'': + result.Append("'"); + break; + case '&': + result.Append("&"); + break; + default: + result.Append(c); + break; + } + } + return result.ToString(); + } + } +} diff --git a/EasyTool.Core/ServiceCollectionExtensions.cs b/EasyTool.Core/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..3d31db9 --- /dev/null +++ b/EasyTool.Core/ServiceCollectionExtensions.cs @@ -0,0 +1,184 @@ +using System; +using System.Net.Http; +using EasyTool.CacheCategory; +using EasyTool.DatabaseCategory; +using EasyTool.QueueCategory; +using Microsoft.Extensions.DependencyInjection; + +namespace EasyTool +{ + /// + /// IServiceCollection 扩展方法 + /// 提供依赖注入注册 + /// + public static class ServiceCollectionExtensions + { + /// + /// 添加 EasyTool 核心服务 + /// + /// 服务集合 + /// 服务集合 + public static IServiceCollection AddEasyTool(this IServiceCollection services) + { + // 注册缓存服务 + services.AddSingleton(); + + // 注册 HttpClient 工厂 + services.AddHttpClient(); + + return services; + } + + /// + /// 添加 EasyTool 缓存服务 + /// + /// 服务集合 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolCache( + this IServiceCollection services, + Action? configure = null) + { + var options = new CacheOptions(); + configure?.Invoke(options); + + services.AddSingleton(sp => + { + return new MemoryCacheProvider( + options.CleanupInterval, + options.SizeLimit); + }); + + return services; + } + + /// + /// 添加 EasyTool Redis 缓存服务 + /// + /// 服务集合 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolRedisCache( + this IServiceCollection services, + Action configure) + { + var options = new RedisCacheOptions(); + configure(options); + + services.AddSingleton(sp => + { + return new RedisCacheProvider(options); + }); + + return services; + } + + /// + /// 添加 EasyTool 数据库连接池服务 + /// + /// 服务集合 + /// 连接字符串 + /// 数据库提供者工厂 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolConnectionPool( + this IServiceCollection services, + string connectionString, + System.Data.Common.DbProviderFactory providerFactory, + Action? configure = null) + { + var options = new ConnectionPoolOptions(); + configure?.Invoke(options); + + services.AddSingleton(sp => + { + return new ConnectionPool(connectionString, providerFactory, options); + }); + + return services; + } + + /// + /// 添加 EasyTool 消息队列服务 + /// + /// 消息类型 + /// 服务集合 + /// 消息处理器 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolMessageQueue( + this IServiceCollection services, + Func, System.Threading.Tasks.Task> handler, + Action? configure = null) + { + var options = new QueueCategory.MessageQueueOptions(); + configure?.Invoke(options); + + services.AddSingleton>(sp => + { + return new QueueCategory.MessageQueue(handler, options); + }); + + return services; + } + + /// + /// 添加 EasyTool HttpClient 服务 + /// + /// 服务集合 + /// 客户端名称 + /// 配置委托 + /// 服务集合 + public static IServiceCollection AddEasyToolHttpClient( + this IServiceCollection services, + string name, + Action? configure = null) + { + services.AddHttpClient(name) + .ConfigurePrimaryHttpMessageHandler(() => + { + var builder = new NetCategory.HttpClientBuilder(); + configure?.Invoke(builder); + return new HttpClientHandler(); + }); + + return services; + } + + /// + /// 添加 EasyTool 多级缓存服务 + /// + /// 服务集合 + /// 分布式缓存提供者 + /// 本地缓存过期时间 + /// 服务集合 + public static IServiceCollection AddEasyToolMultiLevelCache( + this IServiceCollection services, + ICacheProvider? distributedCacheProvider = null, + TimeSpan? localCacheExpiration = null) + { + services.AddSingleton(sp => + { + return new MultiLevelCache(distributedCacheProvider, localCacheExpiration); + }); + + return services; + } + } + + /// + /// 缓存配置选项 + /// + public class CacheOptions + { + /// + /// 清理间隔 + /// + public TimeSpan? CleanupInterval { get; set; } + + /// + /// 大小限制 + /// + public long? SizeLimit { get; set; } + } +} diff --git a/EasyTool.Core/Standardization/Option.cs b/EasyTool.Core/Standardization/Option.cs index fc8f89d..07b5a12 100644 --- a/EasyTool.Core/Standardization/Option.cs +++ b/EasyTool.Core/Standardization/Option.cs @@ -1,33 +1,49 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; using System.Linq; using System.Reflection; -namespace EasyTool +namespace EasyTool.Standardization { - -#if NET6_0_OR_GREATER /* *标准化与前端下拉选项数据结构,减少前后端对接工作 */ + + + /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record Option(T Value, string Text); + public class Option + { + public T Value { get; set; } + public string Text { get; set; } + + public Option(T value, string text) + { + Value = value; + Text = text; + } + } /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record Option(string Value, string Text) : Option(Value, Text); + public class Option : Option + { + public Option(string value, string text) : base(value, text) { } + } /// /// 包含Value和Text的选择对象,用于前端下拉选项 /// - public record OptionInt(int? Value, string Text) : Option(Value, Text); + public class OptionInt : Option + { + public OptionInt(int? value, string text) : base(value, text) { } + } /// /// 选项接口,用于描述选项的类 @@ -53,7 +69,7 @@ public interface IOption /// /// 获得选项列表 /// - public static List + /// HTML 内容 + /// 链接列表 + public static List ExtractLinks(string html) + { + var links = new List(); + + if (string.IsNullOrEmpty(html)) + return links; + + var pattern = @"]*href\s*=\s*[""']([^""']+)[""'][^>]*>(.*?)"; + var matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); + + foreach (Match match in matches) + { + links.Add(new HtmlLink + { + Url = match.Groups[1].Value, + Text = StripTags(match.Groups[2].Value) + }); + } + + return links; + } + + /// + /// 提取所有图片 + /// + /// HTML 内容 + /// 图片列表 + public static List ExtractImages(string html) + { + var images = new List(); + + if (string.IsNullOrEmpty(html)) + return images; + + var pattern = @"]*src\s*=\s*[""']([^""']+)[""'][^>]*>"; + var matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase); + + foreach (Match match in matches) + { + var img = new HtmlImage { Src = match.Groups[1].Value }; + + // 提取 alt + var altMatch = Regex.Match(match.Value, @"alt\s*=\s*[""']([^""']*)[""']", RegexOptions.IgnoreCase); + if (altMatch.Success) + img.Alt = altMatch.Groups[1].Value; + + images.Add(img); + } + + return images; + } + + private static string ProcessHtml(string html, HashSet safeTags, Dictionary> safeAttributes) + { + var result = new StringBuilder(); + var pos = 0; + + while (pos < html.Length) + { + var tagStart = html.IndexOf('<', pos); + + if (tagStart < 0) + { + result.Append(html.Substring(pos)); + break; + } + + result.Append(html.Substring(pos, tagStart - pos)); + + var tagEnd = html.IndexOf('>', tagStart); + if (tagEnd < 0) + { + result.Append(html.Substring(tagStart)); + break; + } + + var tagContent = html.Substring(tagStart + 1, tagEnd - tagStart - 1); + + // 处理注释 + if (tagContent.StartsWith("!--")) + { + var commentEnd = html.IndexOf("-->", tagStart); + if (commentEnd > 0) + { + pos = commentEnd + 3; + continue; + } + } + + // 处理标签 + if (tagContent.StartsWith("/")) + { + // 结束标签 + var tagName = GetTagName(tagContent.Substring(1)); + if (safeTags.Contains(tagName)) + { + result.Append($""); + } + } + else + { + // 开始标签 + var tagName = GetTagName(tagContent); + if (safeTags.Contains(tagName)) + { + var cleanedAttributes = CleanAttributes(tagName, tagContent, safeAttributes); + var selfClosing = tagContent.TrimEnd().EndsWith("/"); + result.Append(selfClosing + ? $"<{tagName}{cleanedAttributes}/>" + : $"<{tagName}{cleanedAttributes}>"); + } + } + + pos = tagEnd + 1; + } + + return result.ToString(); + } + + private static string GetTagName(string tagContent) + { + var match = Regex.Match(tagContent, @"^(\w+)"); + return match.Success ? match.Groups[1].Value.ToLowerInvariant() : string.Empty; + } + + private static string CleanAttributes(string tagName, string tagContent, Dictionary> safeAttributes) + { + var safeAttrs = new HashSet(StringComparer.OrdinalIgnoreCase); + if (safeAttributes.TryGetValue("*", out var globalAttrs)) + safeAttrs.UnionWith(globalAttrs); + if (safeAttributes.TryGetValue(tagName, out var tagAttrs)) + safeAttrs.UnionWith(tagAttrs); + + var result = new StringBuilder(); + var matches = Regex.Matches(tagContent, @"(\w+)\s*=\s*[""']([^""']*)[""']"); + + foreach (Match match in matches) + { + var attrName = match.Groups[1].Value.ToLowerInvariant(); + var attrValue = match.Groups[2].Value; + + if (!safeAttrs.Contains(attrName)) + continue; + + if (DangerousAttributePattern.IsMatch(attrName)) + continue; + + // 检查 URL 属性 + if (attrName == "href" || attrName == "src") + { + if (DangerousUrlPattern.IsMatch(attrValue)) + continue; + } + + result.Append($" {attrName}=\"{Escape(attrValue)}\""); + } + + return result.ToString(); + } + } + + /// + /// HTML 链接 + /// + public class HtmlLink + { + /// + /// URL + /// + public string Url { get; set; } = string.Empty; + + /// + /// 链接文本 + /// + public string Text { get; set; } = string.Empty; + } + + /// + /// HTML 图片 + /// + public class HtmlImage + { + /// + /// 图片 URL + /// + public string Src { get; set; } = string.Empty; + + /// + /// 替代文本 + /// + public string Alt { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/TextCategory/HtmlUtil.cs b/EasyTool.Core/TextCategory/HtmlUtil.cs new file mode 100644 index 0000000..3f52c0f --- /dev/null +++ b/EasyTool.Core/TextCategory/HtmlUtil.cs @@ -0,0 +1,608 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// HTML 工具类 + /// 提供 HTML 转义、清理、提取等功能 + /// + public static class HtmlUtil + { + #region 常量 + + /// + /// HTML 实体编码映射 + /// + private static readonly Dictionary HtmlEntities = new Dictionary + { + { " ", " " }, { "<", "<" }, { ">", ">" }, + { "&", "&" }, { """, "\"" }, { "'", "'" }, + { "©", "©" }, { "®", "®" }, { "™", "™" }, + { "–", "–" }, { "—", "—" }, { "‘", "'" }, + { "’", "'" }, { "“", "\"" }, { "”", "\"" }, + { "•", "•" }, { "…", "…" }, { "°", "°" }, + { "±", "±" }, { "×", "×" }, { "÷", "÷" }, + { "€", "€" }, { "£", "£" }, { "¥", "¥" }, + { "¢", "¢" }, { "§", "§" }, { "¶", "¶" }, + { "†", "†" }, { "‡", "‡" }, { "‰", "‰" }, + { "«", "«" }, { "»", "»" }, { "¡", "¡" }, + { "¿", "¿" }, { "µ", "µ" }, { "·", "·" } + }; + + /// + /// HTML 标签正则 + /// + private static readonly Regex HtmlTagRegex = new Regex(@"<[^>]+>", RegexOptions.Compiled); + + /// + /// 脚本标签正则 + /// + private static readonly Regex ScriptRegex = new Regex(@"]*>[\s\S]*?", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// 样式标签正则 + /// + private static readonly Regex StyleRegex = new Regex(@"]*>[\s\S]*?", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + /// + /// HTML 注释正则 + /// + private static readonly Regex CommentRegex = new Regex(@"", RegexOptions.Compiled); + + /// + /// 数字实体正则 + /// + private static readonly Regex NumericEntityRegex = new Regex(@"&#(\d+);", RegexOptions.Compiled); + + /// + /// 十六进制实体正则 + /// + private static readonly Regex HexEntityRegex = new Regex(@"&#[xX]([0-9a-fA-F]+);", RegexOptions.Compiled); + + /// + /// 安全标签白名单 + /// + private static readonly HashSet SafeTags = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "p", "br", "hr", "h1", "h2", "h3", "h4", "h5", "h6", + "div", "span", "a", "img", "ul", "ol", "li", "table", "tr", "td", "th", "thead", "tbody", + "strong", "em", "b", "i", "u", "s", "sub", "sup", + "blockquote", "pre", "code", "kbd", "samp", "var" + }; + + #endregion + + #region 转义方法 + + /// + /// HTML 转义 + /// + /// 原始文本 + /// 转义后的文本 + public static string Escape(string? text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + var sb = new StringBuilder(text.Length * 2); + foreach (char c in text) + { + switch (c) + { + case '<': + sb.Append("<"); + break; + case '>': + sb.Append(">"); + break; + case '&': + sb.Append("&"); + break; + case '"': + sb.Append("""); + break; + case '\'': + sb.Append("'"); + break; + default: + sb.Append(c); + break; + } + } + return sb.ToString(); + } + + /// + /// HTML 反转义 + /// + /// HTML 文本 + /// 反转义后的文本 + public static string Unescape(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 先处理数字实体 + result = NumericEntityRegex.Replace(result, match => + { + if (int.TryParse(match.Groups[1].Value, out int code)) + { + return ((char)code).ToString(); + } + return match.Value; + }); + + // 处理十六进制实体 + result = HexEntityRegex.Replace(result, match => + { + try + { + int code = Convert.ToInt32(match.Groups[1].Value, 16); + return ((char)code).ToString(); + } + catch + { + return match.Value; + } + }); + + // 处理命名实体 + foreach (var entity in HtmlEntities) + { + result = result.Replace(entity.Key, entity.Value); + } + + return result; + } + + /// + /// URL 编码 + /// + /// 原始文本 + /// 编码方式(默认UTF-8) + /// 编码后的文本 + public static string UrlEncode(string? text, Encoding? encoding = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + encoding ??= Encoding.UTF8; + return WebUtility.UrlEncode(text); + } + + /// + /// URL 解码 + /// + /// 编码的文本 + /// 解码后的文本 + public static string UrlDecode(string? encodedText) + { + if (string.IsNullOrEmpty(encodedText)) + return string.Empty; + + return WebUtility.UrlDecode(encodedText); + } + + #endregion + + #region 清理方法 + + /// + /// 移除所有 HTML 标签 + /// + /// HTML 文本 + /// 纯文本 + public static string StripTags(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + return HtmlTagRegex.Replace(html, string.Empty); + } + + /// + /// 清理 HTML(移除脚本、样式、注释) + /// + /// HTML 文本 + /// 清理后的 HTML + public static string Clean(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 移除脚本 + result = ScriptRegex.Replace(result, string.Empty); + + // 移除样式 + result = StyleRegex.Replace(result, string.Empty); + + // 移除注释 + result = CommentRegex.Replace(result, string.Empty); + + return result; + } + + /// + /// 清理 HTML 并保留安全标签 + /// + /// HTML 文本 + /// 允许的标签(为空则使用默认白名单) + /// 清理后的 HTML + public static string SafeClean(string? html, IEnumerable? allowedTags = null) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + // 先进行基本清理 + string result = Clean(html); + + // 获取允许的标签集合 + var allowed = allowedTags != null + ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) + : SafeTags; + + // 移除不允许的标签(保留内容) + result = Regex.Replace(result, @"]*>", match => + { + string tagName = match.Groups[1].Value; + if (allowed.Contains(tagName)) + { + return match.Value; + } + return string.Empty; + }, RegexOptions.IgnoreCase); + + return result; + } + + /// + /// 移除所有 HTML 并获取纯文本 + /// + /// HTML 文本 + /// 纯文本 + public static string ToPlainText(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = Clean(html); + + // 处理常见块级元素 + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"

", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", "\n", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", " ", RegexOptions.IgnoreCase); + result = Regex.Replace(result, @"", " ", RegexOptions.IgnoreCase); + + // 移除剩余标签 + result = StripTags(result); + + // 反转义 HTML 实体 + result = Unescape(result); + + // 清理多余空白 + result = Regex.Replace(result, @"[ \t]+", " "); + result = Regex.Replace(result, @"\n\s*\n", "\n\n"); + result = result.Trim(); + + return result; + } + + #endregion + + #region 提取方法 + + /// + /// 提取所有链接 + /// + /// HTML 文本 + /// 链接列表(URL, 文本) + public static List<(string Url, string Text)> ExtractLinks(string? html) + { + var links = new List<(string, string)>(); + + if (string.IsNullOrEmpty(html)) + return links; + + var regex = new Regex(@"]+href=""([^""]+)""[^>]*>([^<]*)", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string url = match.Groups[1].Value; + string text = Unescape(match.Groups[2].Value).Trim(); + links.Add((url, text)); + } + + return links; + } + + /// + /// 提取所有图片 + /// + /// HTML 文本 + /// 图片列表(URL, Alt) + public static List<(string Src, string Alt)> ExtractImages(string? html) + { + var images = new List<(string, string)>(); + + if (string.IsNullOrEmpty(html)) + return images; + + var regex = new Regex(@"]+src=""([^""]+)""[^>]*(?:alt=""([^""]*)"")?[^>]*/?>", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string src = match.Groups[1].Value; + string alt = match.Groups[2].Success ? match.Groups[2].Value : string.Empty; + images.Add((src, alt)); + } + + return images; + } + + /// + /// 提取页面标题 + /// + /// HTML 文本 + /// 标题 + public static string? ExtractTitle(string? html) + { + if (string.IsNullOrEmpty(html)) + return null; + + var match = Regex.Match(html, @"]*>([^<]*)", RegexOptions.IgnoreCase); + if (match.Success) + { + return Unescape(match.Groups[1].Value).Trim(); + } + return null; + } + + /// + /// 提取 Meta 标签内容 + /// + /// HTML 文本 + /// Meta 名称 + /// 内容 + public static string? ExtractMeta(string? html, string name) + { + if (string.IsNullOrEmpty(html) || string.IsNullOrEmpty(name)) + return null; + + var match = Regex.Match(html, $@"]+name=""{Regex.Escape(name)}""[^>]+content=""([^""]*)""", RegexOptions.IgnoreCase); + if (!match.Success) + { + match = Regex.Match(html, $@"]+content=""([^""]*)""[^>]+name=""{Regex.Escape(name)}""", RegexOptions.IgnoreCase); + } + + if (match.Success) + { + return Unescape(match.Groups[1].Value); + } + return null; + } + + /// + /// 提取所有文本内容 + /// + /// HTML 文本 + /// CSS 选择器(简化版,仅支持标签名) + /// 匹配的文本列表 + public static List ExtractTexts(string? html, string? selector = null) + { + var texts = new List(); + + if (string.IsNullOrEmpty(html)) + return texts; + + if (string.IsNullOrEmpty(selector)) + { + texts.Add(ToPlainText(html)); + return texts; + } + + var regex = new Regex($@"<{Regex.Escape(selector)}[^>]*>([\s\S]*?)", RegexOptions.IgnoreCase); + var matches = regex.Matches(html); + + foreach (Match match in matches) + { + string text = ToPlainText(match.Groups[1].Value); + if (!string.IsNullOrWhiteSpace(text)) + { + texts.Add(text); + } + } + + return texts; + } + + #endregion + + #region 格式化方法 + + /// + /// 压缩 HTML(移除多余空白) + /// + /// HTML 文本 + /// 压缩后的 HTML + public static string Minify(string? html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + string result = html; + + // 移除注释 + result = CommentRegex.Replace(result, string.Empty); + + // 移除多余空白 + result = Regex.Replace(result, @">\s+<", "><"); + result = Regex.Replace(result, @"\s+", " "); + result = result.Trim(); + + return result; + } + + /// + /// 格式化 HTML(添加缩进) + /// + /// HTML 文本 + /// 缩进字符串(默认两个空格) + /// 格式化后的 HTML + public static string Format(string? html, string indentString = " ") + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + var result = new StringBuilder(); + int indent = 0; + bool inPre = false; + + var tokens = Regex.Split(html, @"(<[^>]+>)"); + + foreach (var token in tokens) + { + if (string.IsNullOrEmpty(token)) + continue; + + // 检测 pre 标签 + if (Regex.IsMatch(token, @"]*>", RegexOptions.IgnoreCase)) + { + inPre = true; + } + else if (Regex.IsMatch(token, @"", RegexOptions.IgnoreCase)) + { + inPre = false; + } + + if (inPre) + { + result.Append(token); + continue; + } + + string trimmed = token.Trim(); + + // 自闭合标签或文本 + if (trimmed.StartsWith("<") && !trimmed.StartsWith("")) + { + // 开始标签 + if (!IsInlineTag(trimmed)) + { + result.AppendLine(); + result.Append(string.Concat(Enumerable.Repeat(indentString, indent))); + indent++; + } + result.Append(trimmed); + } + else if (trimmed.StartsWith(" + /// 检查是否为有效的 HTML 片段 + /// + /// HTML 文本 + /// 是否有效 + public static bool IsValidHtml(string? html) + { + if (string.IsNullOrWhiteSpace(html)) + return false; + + // 检查基本 HTML 标签 + return HtmlTagRegex.IsMatch(html); + } + + /// + /// 检查 HTML 标签是否匹配 + /// + /// HTML 文本 + /// 是否匹配 + public static bool AreTagsBalanced(string? html) + { + if (string.IsNullOrEmpty(html)) + return true; + + var stack = new Stack(); + var selfClosing = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "br", "hr", "img", "input", "meta", "link", "area", "base", "col", + "embed", "param", "source", "track", "wbr" + }; + + var matches = Regex.Matches(html, @"]*>", RegexOptions.IgnoreCase); + + foreach (Match match in matches) + { + string tagName = match.Groups[1].Value.ToLower(); + + if (selfClosing.Contains(tagName)) + continue; + + if (match.Value.StartsWith("")) + { + // 开始标签 + stack.Push(tagName); + } + } + + return stack.Count == 0; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/KeywordExtractor.cs b/EasyTool.Core/TextCategory/KeywordExtractor.cs new file mode 100644 index 0000000..6272c20 --- /dev/null +++ b/EasyTool.Core/TextCategory/KeywordExtractor.cs @@ -0,0 +1,300 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 关键词提取工具类 + /// + public static class KeywordExtractor + { + /// + /// 中文停用词 + /// + private static readonly HashSet ChineseStopWords = new() + { + "的", "了", "在", "是", "我", "有", "和", "就", "不", "人", "都", "一", "一个", + "上", "也", "很", "到", "说", "要", "去", "你", "会", "着", "没有", "看", "好", + "自己", "这", "那", "什么", "他", "她", "它", "们", "这个", "那个", "哪个", + "怎么", "为什么", "因为", "所以", "但是", "然后", "如果", "可以", "可能", + "已经", "还是", "只是", "就是", "这样", "那样", "怎样", "这么", "那么", + "更", "最", "比", "而", "且", "或", "与", "及", "等", "等等", "之", "于", + "以", "为", "让", "把", "被", "从", "向", "对", "给", "跟", "像", "关于", + "通过", "按照", "根据", "由于", "为了", "既然", "无论", "不管", "即使", + "虽然", "即使", "哪怕", "只要", "除非", "假如", "倘若", "若是", "要是" + }; + + /// + /// 英文停用词 + /// + private static readonly HashSet EnglishStopWords = new(StringComparer.OrdinalIgnoreCase) + { + "a", "an", "the", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", + "by", "from", "as", "is", "was", "are", "were", "been", "be", "have", "has", "had", + "do", "does", "did", "will", "would", "could", "should", "may", "might", "must", + "shall", "can", "need", "dare", "ought", "used", "it", "its", "this", "that", + "these", "those", "i", "you", "he", "she", "we", "they", "what", "which", "who", + "whom", "whose", "where", "when", "why", "how", "all", "each", "every", "both", + "few", "more", "most", "other", "some", "such", "no", "not", "only", "same", "so", + "than", "too", "very", "just", "also", "now", "here", "there", "then", "once" + }; + + /// + /// 使用TF-IDF算法提取关键词 + /// + public static List ExtractByTfIdf(string text, int topN = 10, int minWordLength = 2) + { + // 分词(简单实现:按空格和标点分割) + var words = Tokenize(text, minWordLength); + + // 计算词频 + var wordFreq = new Dictionary(); + foreach (var word in words) + { + if (IsStopWord(word)) continue; + if (!wordFreq.ContainsKey(word)) + wordFreq[word] = 0; + wordFreq[word]++; + } + + // 计算TF-IDF(简化版,使用词频和词长作为权重) + var results = new List(); + var totalWords = words.Count; + + foreach (var kvp in wordFreq) + { + var tf = (double)kvp.Value / totalWords; + var wordLength = kvp.Key.Length; + + // 词长权重:较长的词可能更重要 + var lengthWeight = Math.Min(wordLength / 4.0, 1.0); + + // 简化的IDF:使用词的稀有度 + var idf = Math.Log((double)totalWords / kvp.Value + 1); + + var score = tf * idf * (1 + lengthWeight); + + results.Add(new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = score + }); + } + + return results.OrderByDescending(r => r.Score).Take(topN).ToList(); + } + + /// + /// 提取高频词 + /// + public static List ExtractTopWords(string text, int topN = 10, int minWordLength = 2) + { + var words = Tokenize(text, minWordLength); + + var wordFreq = new Dictionary(); + foreach (var word in words) + { + if (IsStopWord(word)) continue; + if (!wordFreq.ContainsKey(word)) + wordFreq[word] = 0; + wordFreq[word]++; + } + + return wordFreq + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 提取n-gram + /// + public static List ExtractNgrams(string text, int n = 2, int topN = 10) + { + var ngrams = new Dictionary(); + var cleanText = Regex.Replace(text, @"[\s\p{P}]+", " ").Trim(); + + for (int i = 0; i <= cleanText.Length - n; i++) + { + var ngram = cleanText.Substring(i, n); + if (!ngrams.ContainsKey(ngram)) + ngrams[ngram] = 0; + ngrams[ngram]++; + } + + return ngrams + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 提取中文短语(双字词组) + /// + public static List ExtractChinesePhrases(string text, int topN = 10) + { + var phrases = new Dictionary(); + var chinesePattern = new Regex(@"[\u4e00-\u9fa5]{2,}"); + + foreach (Match match in chinesePattern.Matches(text)) + { + var phrase = match.Value; + if (!phrases.ContainsKey(phrase)) + phrases[phrase] = 0; + phrases[phrase]++; + } + + // 过滤停用词 + var filtered = phrases + .Where(kvp => !IsStopWord(kvp.Key)) + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value * kvp.Key.Length // 长词权重更高 + }); + + return filtered.ToList(); + } + + /// + /// 提取英文短语 + /// + public static List ExtractEnglishPhrases(string text, int topN = 10) + { + var phrases = new Dictionary(); + var wordPattern = new Regex(@"\b[a-zA-Z]{2,}\b"); + + foreach (Match match in wordPattern.Matches(text)) + { + var word = match.Value.ToLower(); + if (!IsStopWord(word)) + { + if (!phrases.ContainsKey(word)) + phrases[word] = 0; + phrases[word]++; + } + } + + return phrases + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => new KeywordResult + { + Word = kvp.Key, + Frequency = kvp.Value, + Score = kvp.Value + }) + .ToList(); + } + + /// + /// 分词 + /// + private static List Tokenize(string text, int minWordLength = 2) + { + var words = new List(); + + // 提取中文词 + var chinesePattern = new Regex(@"[\u4e00-\u9fa5]+"); + foreach (Match match in chinesePattern.Matches(text)) + { + var word = match.Value; + // 中文简单分词:提取双字词 + for (int i = 0; i < word.Length - 1; i++) + { + words.Add(word.Substring(i, 2)); + } + if (word.Length >= minWordLength) + { + words.Add(word); + } + } + + // 提取英文词 + var englishPattern = new Regex(@"\b[a-zA-Z]{2,}\b"); + foreach (Match match in englishPattern.Matches(text)) + { + words.Add(match.Value.ToLower()); + } + + // 提取数字 + var numberPattern = new Regex(@"\b\d+(\.\d+)?\b"); + foreach (Match match in numberPattern.Matches(text)) + { + words.Add(match.Value); + } + + return words; + } + + /// + /// 判断是否为停用词 + /// + private static bool IsStopWord(string word) + { + return ChineseStopWords.Contains(word) || EnglishStopWords.Contains(word); + } + + /// + /// 添加自定义停用词 + /// + public static void AddStopWords(IEnumerable words) + { + foreach (var word in words) + { + if (Regex.IsMatch(word, @"[\u4e00-\u9fa5]")) + { + ChineseStopWords.Add(word); + } + else + { + EnglishStopWords.Add(word.ToLower()); + } + } + } + } + + /// + /// 关键词结果 + /// + public class KeywordResult + { + /// + /// 关键词 + /// + public string Word { get; set; } = ""; + + /// + /// 出现频率 + /// + public int Frequency { get; set; } + + /// + /// 权重分数 + /// + public double Score { get; set; } + + public override string ToString() + { + return $"{Word} (频率:{Frequency}, 分数:{Score:F4})"; + } + } +} diff --git a/EasyTool.Core/TextCategory/LevenshteinUtil.cs b/EasyTool.Core/TextCategory/LevenshteinUtil.cs new file mode 100644 index 0000000..83f4e7e --- /dev/null +++ b/EasyTool.Core/TextCategory/LevenshteinUtil.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 编辑距离工具类 + /// 提供各种字符串相似度计算方法 + /// + public static class LevenshteinUtil + { + /// + /// 计算Levenshtein编辑距离 + /// + public static int Distance(string source, string target) + { + if (string.IsNullOrEmpty(source)) + return target?.Length ?? 0; + if (string.IsNullOrEmpty(target)) + return source.Length; + + int m = source.Length; + int n = target.Length; + + // 优化空间:只使用两行 + int[] prev = new int[n + 1]; + int[] curr = new int[n + 1]; + + // 初始化第一行 + for (int j = 0; j <= n; j++) + prev[j] = j; + + for (int i = 1; i <= m; i++) + { + curr[0] = i; + + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + + curr[j] = Math.Min( + Math.Min(prev[j] + 1, curr[j - 1] + 1), + prev[j - 1] + cost); + } + + // 交换行 + (prev, curr) = (curr, prev); + } + + return prev[n]; + } + + /// + /// 计算相似度(0-1) + /// + public static double Similarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + + int maxLen = Math.Max(source.Length, target.Length); + if (maxLen == 0) return 1.0; + + int distance = Distance(source, target); + return 1.0 - (double)distance / maxLen; + } + + /// + /// 获取编辑操作序列 + /// + public static List GetEditOperations(string source, string target) + { + var operations = new List(); + + if (string.IsNullOrEmpty(source)) + { + for (int i = 0; i < (target?.Length ?? 0); i++) + operations.Add(new EditOperation(EditType.Insert, i, target[i].ToString())); + return operations; + } + + if (string.IsNullOrEmpty(target)) + { + for (int i = 0; i < source.Length; i++) + operations.Add(new EditOperation(EditType.Delete, i, source[i].ToString())); + return operations; + } + + int m = source.Length; + int n = target.Length; + + // 构建完整DP表 + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 0; i <= m; i++) dp[i, 0] = i; + for (int j = 0; j <= n; j++) dp[0, j] = j; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + dp[i, j] = Math.Min( + Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), + dp[i - 1, j - 1] + cost); + } + } + + // 回溯获取操作 + int x = m, y = n; + while (x > 0 || y > 0) + { + if (x > 0 && y > 0 && source[x - 1] == target[y - 1]) + { + operations.Add(new EditOperation(EditType.Match, x - 1, source[x - 1].ToString())); + x--; y--; + } + else if (x > 0 && y > 0 && dp[x, y] == dp[x - 1, y - 1] + 1) + { + operations.Add(new EditOperation(EditType.Replace, x - 1, source[x - 1].ToString(), target[y - 1].ToString())); + x--; y--; + } + else if (y > 0 && (x == 0 || dp[x, y] == dp[x, y - 1] + 1)) + { + operations.Add(new EditOperation(EditType.Insert, x, target[y - 1].ToString())); + y--; + } + else if (x > 0 && (y == 0 || dp[x, y] == dp[x - 1, y] + 1)) + { + operations.Add(new EditOperation(EditType.Delete, x - 1, source[x - 1].ToString())); + x--; + } + } + + operations.Reverse(); + return operations; + } + + /// + /// Damerau-Levenshtein距离(支持相邻交换) + /// + public static int DamerauLevenshteinDistance(string source, string target) + { + if (string.IsNullOrEmpty(source)) + return target?.Length ?? 0; + if (string.IsNullOrEmpty(target)) + return source.Length; + + int m = source.Length; + int n = target.Length; + + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 0; i <= m; i++) dp[i, 0] = i; + for (int j = 0; j <= n; j++) dp[0, j] = j; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + int cost = source[i - 1] == target[j - 1] ? 0 : 1; + + dp[i, j] = Math.Min( + Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), + dp[i - 1, j - 1] + cost); + + // 检查相邻交换 + if (i > 1 && j > 1 && + source[i - 1] == target[j - 2] && + source[i - 2] == target[j - 1]) + { + dp[i, j] = Math.Min(dp[i, j], dp[i - 2, j - 2] + cost); + } + } + } + + return dp[m, n]; + } + + /// + /// 计算最长公共子序列长度 + /// + public static int LongestCommonSubsequence(string source, string target) + { + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0; + + int m = source.Length; + int n = target.Length; + + int[,] dp = new int[m + 1, n + 1]; + + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + if (source[i - 1] == target[j - 1]) + { + dp[i, j] = dp[i - 1, j - 1] + 1; + } + else + { + dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]); + } + } + } + + return dp[m, n]; + } + + /// + /// 基于最长公共子序列的相似度 + /// + public static double LCSSimilarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + + int lcs = LongestCommonSubsequence(source, target); + int maxLen = Math.Max(source.Length, target.Length); + + return (double)lcs / maxLen; + } + + /// + /// 计算 Jaro 相似度 + /// + public static double JaroSimilarity(string source, string target) + { + if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(target)) + return 1.0; + if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target)) + return 0.0; + if (source == target) + return 1.0; + + int m = source.Length; + int n = target.Length; + + int matchDistance = Math.Max(m, n) / 2 - 1; + if (matchDistance < 0) matchDistance = 0; + + bool[] sourceMatches = new bool[m]; + bool[] targetMatches = new bool[n]; + + int matches = 0; + int transpositions = 0; + + for (int i = 0; i < m; i++) + { + int start = Math.Max(0, i - matchDistance); + int end = Math.Min(i + matchDistance + 1, n); + + for (int j = start; j < end; j++) + { + if (targetMatches[j] || source[i] != target[j]) + continue; + + sourceMatches[i] = true; + targetMatches[j] = true; + matches++; + break; + } + } + + if (matches == 0) + return 0.0; + + int k = 0; + for (int i = 0; i < m; i++) + { + if (!sourceMatches[i]) + continue; + + while (!targetMatches[k]) + k++; + + if (source[i] != target[k]) + transpositions++; + + k++; + } + + return ((double)matches / m + + (double)matches / n + + (matches - transpositions / 2.0) / matches) / 3.0; + } + + /// + /// 计算 Jaro-Winkler 相似度 + /// + public static double JaroWinklerSimilarity(string source, string target, double scalingFactor = 0.1) + { + double jaro = JaroSimilarity(source, target); + + // 计算公共前缀长度(最多4个字符) + int prefixLength = 0; + for (int i = 0; i < Math.Min(Math.Min(source.Length, target.Length), 4); i++) + { + if (source[i] == target[i]) + prefixLength++; + else + break; + } + + return jaro + prefixLength * scalingFactor * (1 - jaro); + } + + /// + /// 模糊匹配搜索 + /// + public static List<(string Item, double Score)> FuzzySearch(string query, IEnumerable items, double threshold = 0.5) + { + return items + .Select(item => (Item: item, Score: JaroWinklerSimilarity(query, item))) + .Where(x => x.Score >= threshold) + .OrderByDescending(x => x.Score) + .ToList(); + } + } + + /// + /// 编辑操作类型 + /// + public enum EditType + { + /// 匹配 + Match, + /// 替换 + Replace, + /// 插入 + Insert, + /// 删除 + Delete + } + + /// + /// 编辑操作 + /// + public class EditOperation + { + /// 操作类型 + public EditType Type { get; } + /// 位置 + public int Position { get; } + /// 原始字符 + public string OldValue { get; } + /// 新字符 + public string NewValue { get; } + + public EditOperation(EditType type, int position, string value, string newValue = null) + { + Type = type; + Position = position; + OldValue = value; + NewValue = newValue ?? value; + } + + public override string ToString() + { + return Type switch + { + EditType.Match => $"Match '{OldValue}' at {Position}", + EditType.Replace => $"Replace '{OldValue}' with '{NewValue}' at {Position}", + EditType.Insert => $"Insert '{NewValue}' at {Position}", + EditType.Delete => $"Delete '{OldValue}' at {Position}", + _ => base.ToString() + }; + } + } +} diff --git a/EasyTool.Core/TextCategory/MarkdownUtil.cs b/EasyTool.Core/TextCategory/MarkdownUtil.cs new file mode 100644 index 0000000..a11925e --- /dev/null +++ b/EasyTool.Core/TextCategory/MarkdownUtil.cs @@ -0,0 +1,485 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// Markdown工具类 + /// 提供Markdown解析和转换功能 + /// + public static class MarkdownUtil + { + /// + /// Markdown转HTML + /// + public static string ToHtml(string markdown) + { + if (string.IsNullOrEmpty(markdown)) + return string.Empty; + + var html = markdown; + + // 转义HTML特殊字符 + html = EscapeHtml(html); + + // 标题 + html = Regex.Replace(html, @"^###### (.+)$", "
$1
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^##### (.+)$", "
$1
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^#### (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^### (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^## (.+)$", "

$1

", RegexOptions.Multiline); + html = Regex.Replace(html, @"^# (.+)$", "

$1

", RegexOptions.Multiline); + + // 代码块 + html = Regex.Replace(html, @"```(\w*)\n([\s\S]*?)```", "
$2
"); + html = Regex.Replace(html, @"`([^`]+)`", "$1"); + + // 粗体和斜体 + html = Regex.Replace(html, @"\*\*\*(.+?)\*\*\*", "$1"); + html = Regex.Replace(html, @"\*\*(.+?)\*\*", "$1"); + html = Regex.Replace(html, @"\*(.+?)\*", "$1"); + html = Regex.Replace(html, @"___(.+?)___", "$1"); + html = Regex.Replace(html, @"__(.+?)__", "$1"); + html = Regex.Replace(html, @"_(.+?)_", "$1"); + html = Regex.Replace(html, @"~~(.+?)~~", "$1"); + + // 链接和图片 + html = Regex.Replace(html, @"!\[([^\]]*)\]\(([^)]+)\)", "\"$1\""); + html = Regex.Replace(html, @"\[([^\]]+)\]\(([^)]+)\)", "$1"); + + // 引用 + html = Regex.Replace(html, @"^> (.+)$", "
$1
", RegexOptions.Multiline); + + // 水平线 + html = Regex.Replace(html, @"^---$", "
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^\*\*\*$", "
", RegexOptions.Multiline); + html = Regex.Replace(html, @"^___$", "
", RegexOptions.Multiline); + + // 无序列表 + html = ProcessUnorderedList(html); + + // 有序列表 + html = ProcessOrderedList(html); + + // 表格 + html = ProcessTable(html); + + // 段落 + html = ProcessParagraphs(html); + + return html; + } + + /// + /// HTML转Markdown(基础实现) + /// + public static string FromHtml(string html) + { + if (string.IsNullOrEmpty(html)) + return string.Empty; + + var markdown = html; + + // 标题 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "# $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "## $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "#### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "##### $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "###### $1", RegexOptions.IgnoreCase); + + // 链接 + markdown = Regex.Replace(markdown, @"]*href=""([^""]+)""[^>]*>(.*?)", "[$2]($1)", RegexOptions.IgnoreCase); + + // 图片 + markdown = Regex.Replace(markdown, @"]*src=""([^""]+)""[^>]*alt=""([^""]*)""[^>]*>", "![$2]($1)", RegexOptions.IgnoreCase); + + // 粗体和斜体 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "**$1**", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "**$1**", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "*$1*", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)", "*$1*", RegexOptions.IgnoreCase); + + // 代码 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "`$1`", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>]*>([\s\S]*?)", "```\n$1\n```", RegexOptions.IgnoreCase); + + // 引用 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "> $1", RegexOptions.IgnoreCase); + + // 列表 + markdown = Regex.Replace(markdown, @"]*>(.*?)", "- $1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>([\s\S]*?)", "$1", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>([\s\S]*?)", "$1", RegexOptions.IgnoreCase); + + // 段落和换行 + markdown = Regex.Replace(markdown, @"", "\n", RegexOptions.IgnoreCase); + markdown = Regex.Replace(markdown, @"]*>(.*?)

", "$1\n\n", RegexOptions.IgnoreCase); + + // 清理其他标签 + markdown = Regex.Replace(markdown, @"<[^>]+>", ""); + + // 解码HTML实体 + markdown = UnescapeHtml(markdown); + + return markdown.Trim(); + } + + /// + /// 提取Markdown标题 + /// + public static List ExtractHeadings(string markdown) + { + var headings = new List(); + + if (string.IsNullOrEmpty(markdown)) + return headings; + + var lines = markdown.Split('\n'); + for (int i = 0; i < lines.Length; i++) + { + var line = lines[i]; + var match = Regex.Match(line, @"^(#{1,6})\s+(.+)$"); + if (match.Success) + { + headings.Add(new MarkdownHeading + { + Level = match.Groups[1].Value.Length, + Text = match.Groups[2].Value.Trim(), + LineNumber = i + 1 + }); + } + } + + return headings; + } + + /// + /// 提取Markdown中的所有链接 + /// + public static List ExtractLinks(string markdown) + { + var links = new List(); + + if (string.IsNullOrEmpty(markdown)) + return links; + + var regex = new Regex(@"\[([^\]]+)\]\(([^)]+)\)"); + var matches = regex.Matches(markdown); + + foreach (Match match in matches) + { + links.Add(new MarkdownLink + { + Text = match.Groups[1].Value, + Url = match.Groups[2].Value + }); + } + + return links; + } + + /// + /// 提取Markdown中的所有图片 + /// + public static List ExtractImages(string markdown) + { + var images = new List(); + + if (string.IsNullOrEmpty(markdown)) + return images; + + var regex = new Regex(@"!\[([^\]]*)\]\(([^)]+)\)"); + var matches = regex.Matches(markdown); + + foreach (Match match in matches) + { + images.Add(new MarkdownImage + { + Alt = match.Groups[1].Value, + Url = match.Groups[2].Value + }); + } + + return images; + } + + /// + /// 生成目录(TOC) + /// + public static string GenerateToc(string markdown) + { + var headings = ExtractHeadings(markdown); + var toc = new StringBuilder(); + + foreach (var heading in headings) + { + var indent = new string(' ', (heading.Level - 1) * 2); + var anchor = GenerateAnchor(heading.Text); + toc.AppendLine($"{indent}- [{heading.Text}](#{anchor})"); + } + + return toc.ToString(); + } + + /// + /// 简化Markdown(移除格式) + /// + public static string StripFormatting(string markdown) + { + if (string.IsNullOrEmpty(markdown)) + return string.Empty; + + var text = markdown; + + // 移除代码块 + text = Regex.Replace(text, @"```\w*\n[\s\S]*?```", ""); + text = Regex.Replace(text, @"`([^`]+)`", "$1"); + + // 移除标题标记 + text = Regex.Replace(text, @"^#{1,6}\s+", "", RegexOptions.Multiline); + + // 移除粗体、斜体、删除线 + text = Regex.Replace(text, @"\*\*\*(.+?)\*\*\*", "$1"); + text = Regex.Replace(text, @"\*\*(.+?)\*\*", "$1"); + text = Regex.Replace(text, @"\*(.+?)\*", "$1"); + text = Regex.Replace(text, @"~~(.+?)~~", "$1"); + + // 移除链接,保留文本 + text = Regex.Replace(text, @"!\[([^\]]*)\]\([^)]+\)", "$1"); + text = Regex.Replace(text, @"\[([^\]]+)\]\([^)]+\)", "$1"); + + // 移除引用标记 + text = Regex.Replace(text, @"^>\s+", "", RegexOptions.Multiline); + + // 移除列表标记 + text = Regex.Replace(text, @"^[\*\-\+]\s+", "", RegexOptions.Multiline); + text = Regex.Replace(text, @"^\d+\.\s+", "", RegexOptions.Multiline); + + return text.Trim(); + } + + private static string EscapeHtml(string text) + { + return text + .Replace("&", "&") + .Replace("<", "<") + .Replace(">", ">"); + } + + private static string UnescapeHtml(string text) + { + return text + .Replace("&", "&") + .Replace("<", "<") + .Replace(">", ">") + .Replace(""", "\"") + .Replace("'", "'"); + } + + private static string ProcessUnorderedList(string html) + { + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inList = false; + + foreach (var line in lines) + { + var match = Regex.Match(line, @"^[\*\-\+]\s+(.+)$"); + if (match.Success) + { + if (!inList) + { + result.AppendLine("
    "); + inList = true; + } + result.AppendLine($"
  • {match.Groups[1].Value}
  • "); + } + else + { + if (inList) + { + result.AppendLine("
"); + inList = false; + } + result.AppendLine(line); + } + } + + if (inList) + result.AppendLine(""); + + return result.ToString(); + } + + private static string ProcessOrderedList(string html) + { + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inList = false; + + foreach (var line in lines) + { + var match = Regex.Match(line, @"^\d+\.\s+(.+)$"); + if (match.Success) + { + if (!inList) + { + result.AppendLine("
    "); + inList = true; + } + result.AppendLine($"
  1. {match.Groups[1].Value}
  2. "); + } + else + { + if (inList) + { + result.AppendLine("
"); + inList = false; + } + result.AppendLine(line); + } + } + + if (inList) + result.AppendLine(""); + + return result.ToString(); + } + + private static string ProcessTable(string html) + { + // 简单的表格处理 + var lines = html.Split('\n'); + var result = new StringBuilder(); + var inTable = false; + + foreach (var line in lines) + { + if (line.Trim().StartsWith("|") && line.Trim().EndsWith("|")) + { + if (!inTable) + { + result.AppendLine(""); + inTable = true; + } + + var cells = line.Trim('|').Split('|'); + var isHeader = line.Contains("---"); + + if (!isHeader) + { + result.AppendLine(""); + foreach (var cell in cells) + { + result.AppendLine($""); + } + result.AppendLine(""); + } + } + else + { + if (inTable) + { + result.AppendLine("
{cell.Trim()}
"); + inTable = false; + } + result.AppendLine(line); + } + } + + return result.ToString(); + } + + private static string ProcessParagraphs(string html) + { + var lines = html.Split(new[] { "\n\n" }, StringSplitOptions.None); + var result = new StringBuilder(); + + foreach (var line in lines) + { + var trimmed = line.Trim(); + if (!string.IsNullOrEmpty(trimmed) && + !trimmed.StartsWith("{trimmed}

"); + } + else + { + result.AppendLine(trimmed); + } + } + + return result.ToString(); + } + + private static string GenerateAnchor(string text) + { + var anchor = text.ToLower(); + anchor = Regex.Replace(anchor, @"[^\w\s-]", ""); + anchor = Regex.Replace(anchor, @"\s+", "-"); + return anchor; + } + } + + /// + /// Markdown标题 + /// + public class MarkdownHeading + { + /// + /// 标题级别(1-6) + /// + public int Level { get; set; } + + /// + /// 标题文本 + /// + public string Text { get; set; } = string.Empty; + + /// + /// 行号 + /// + public int LineNumber { get; set; } + } + + /// + /// Markdown链接 + /// + public class MarkdownLink + { + /// + /// 链接文本 + /// + public string Text { get; set; } = string.Empty; + + /// + /// 链接URL + /// + public string Url { get; set; } = string.Empty; + } + + /// + /// Markdown图片 + /// + public class MarkdownImage + { + /// + /// 替代文本 + /// + public string Alt { get; set; } = string.Empty; + + /// + /// 图片URL + /// + public string Url { get; set; } = string.Empty; + } +} diff --git a/EasyTool.Core/TextCategory/PinyinUtil.cs b/EasyTool.Core/TextCategory/PinyinUtil.cs new file mode 100644 index 0000000..8178e9e --- /dev/null +++ b/EasyTool.Core/TextCategory/PinyinUtil.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 拼音工具类 + /// 提供汉字转拼音功能 + /// + public static class PinyinUtil + { + /// + /// 获取汉字的拼音 + /// + /// 中文字符串 + /// 分隔符 + /// 拼音字符串 + public static string GetPinyin(string chinese, string separator = "") + { + if (string.IsNullOrEmpty(chinese)) + return string.Empty; + + var result = new StringBuilder(); + + foreach (char c in chinese) + { + string pinyin = GetPinyin(c); + if (result.Length > 0 && !string.IsNullOrEmpty(pinyin)) + result.Append(separator); + result.Append(pinyin); + } + + return result.ToString(); + } + + /// + /// 获取单个汉字的拼音 + /// + public static string GetPinyin(char c) + { + // 非汉字直接返回 + if (c < 0x4E00 || c > 0x9FA5) + return c.ToString(); + + // 查找拼音 + string[] py = GetPinyinArray(c); + return py != null && py.Length > 0 ? py[0] : c.ToString(); + } + + /// + /// 获取汉字的所有拼音(多音字) + /// + public static string[] GetPinyins(char c) + { + if (c < 0x4E00 || c > 0x9FA5) + return new[] { c.ToString() }; + + return GetPinyinArray(c) ?? new[] { c.ToString() }; + } + + /// + /// 获取拼音首字母 + /// + public static string GetFirstPinyinLetter(string chinese) + { + if (string.IsNullOrEmpty(chinese)) + return string.Empty; + + var result = new StringBuilder(); + + foreach (char c in chinese) + { + string pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin) && pinyin.Length > 0) + { + result.Append(char.ToUpper(pinyin[0])); + } + else + { + result.Append(c); + } + } + + return result.ToString(); + } + + /// + /// 获取拼音首字母(简化版,用于排序索引) + /// + public static string GetSimplePinyinInitial(string chinese) + { + if (string.IsNullOrEmpty(chinese)) + return "#"; + + char c = chinese[0]; + + // 非汉字 + if (c < 0x4E00 || c > 0x9FA5) + { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return char.ToUpper(c).ToString(); + return "#"; + } + + string pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin) && pinyin.Length > 0) + { + return char.ToUpper(pinyin[0]).ToString(); + } + + return "#"; + } + + /// + /// 判断字符是否为汉字 + /// + public static bool IsChinese(char c) + { + return c >= 0x4E00 && c <= 0x9FA5; + } + + /// + /// 判断字符串是否全部为汉字 + /// + public static bool IsAllChinese(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + foreach (char c in s) + { + if (!IsChinese(c)) + return false; + } + + return true; + } + + /// + /// 判断字符串是否包含汉字 + /// + public static bool ContainsChinese(string s) + { + if (string.IsNullOrEmpty(s)) + return false; + + foreach (char c in s) + { + if (IsChinese(c)) + return true; + } + + return false; + } + + // 简化的拼音表(这里只包含部分常用汉字的拼音) + // 完整实现需要包含所有汉字的拼音映射 + private static readonly Dictionary PinyinMap = InitializePinyinMap(); + + private static Dictionary InitializePinyinMap() + { + var map = new Dictionary(); + + // 常用汉字拼音表(简化版,实际应用需要完整拼音表) + string[] chars = { + "的一是不了在人有我他这个们中来上大为和国地到以说时要就出会可也你对生能而子那得于着下自之年过发后作里用道行所然家种事成方多经么去法学如都同现当没动面起看定天分还进好小部其些主样理心她本前开但因只从想实日军者意无力它与长把机十民第公此已工使情明性知全三又关点正业外将两高间由问很最重并物手应战向头文体政美相见被利什二等产或新己制身果加西斯月话合回特代内信表化老给世位次度门任常先海通教儿原东声提立及比员解水名真论处走义各入几口认条平系气题活尔更别打女变四神总何电数安少报才结反受目太量再感建务做接必场件计管期市直德资命山金指克许统区保至队形社便空决治展马科司五基眼书非则听白却界达光放强即像难且权思王象完设式色路记南品住告类求据程北边死张该交规万取拉格望觉术领共确传师观清今切院让识候带导争运笔志认准许响约英格底仅流端讲乡村消故值收越古史附整改落致令参周农吸获坚单组切界育苦断背细油调灵责供济容质项根议陈拿破仑" + }; + + string[] pinyins = { + "de,yi,shi,bu,liao,zai,ren,you,wo,ta,zhe,ge,men,zhong,lai,shang,da,wei,he,guo,di,dao,yi,shuo,shi,yao,jiu,chu,hui,ke,ye,ni,dui,sheng,neng,er,zi,na,de,yu,zhe,xia,zi,zhi,nian,guo,fa,hou,zuo,li,yong,dao,xing,suo,ran,jia,zhong,shi,cheng,fang,duo,jing,me,qu,fa,xue,ru,dou,tong,xian,dang,mei,dong,mian,qi,kan,ding,tian,fen,hai,jin,hao,xiao,bu,qi,xie,zhu,yang,li,xin,ta,ben,qian,kai,yin,zhi,cong,xiang,shi,ri,jun,zhe,yi,wu,li,ta,yu,chang,ba,ji,shi,min,di,gong,ci,yi,gong,shi,qing,ming,xing,zhi,quan,san,you,guan,dian,zheng,ye,wai,jiang,liang,gao,jian,you,wen,hen,zui,zhong,bing,wu,shou,ying,zhan,xiang,tou,wen,ti,zheng,mei,xiang,jian,bei,li,shi,er,deng,chan,huo,xin,ji,zhi,shen,guo,jia,xi,si,yue,hua,he,hui,te,dai,nei,xin,biao,hua,lao,gei,shi,wei,ci,du,men,ren,chang,xian,hai,tong,jiao,er,yuan,dong,sheng,ti,li,ji,bi,yuan,jie,shui,ming,zhen,lun,chu,zou,yi,ge,ru,ji,kou,ren,tiao,ping,xi,qi,ti,huo,er,geng,bie,da,nv,bian,si,shen,zong,he,dian,shu,an,shao,bao,cai,jie,fan,shou,mu,tai,liang,zai,gan,jian,wu,zuo,jie,bi,chang,jian,ji,guan,qi,shi,zhi,de,zi,ming,shan,jin,zhi,ke,xu,tong,qu,bao,zhi,dui,xing,she,bian,kong,jue,zhi,zhan,ma,ke,si,wu,ji,yan,shu,fei,ze,ting,bai,que,jie,da,guang,fang,qiang,ji,xiang,nan,qie,quan,si,wang,xiang,wan,she,shi,se,lu,ji,nan,pin,zhu,gao,lei,qiu,ju,cheng,bei,bian,si,zhang,gai,jiao,gui,wan,qu,la,ge,wang,jue,shu,ling,gong,que,chuan,shi,guan,qing,jin,qie,yuan,rang,shi,hou,dai,dao,zheng,yun,bi,zhi,ren,zhun,xu,xiang,yue,ying,ge,di,jin,liu,duan,jiang,xiang,cun,xiao,gu,gu,zhi,shou,yue,gu,shi,fu,zheng,gai,luo,zhi,ling,can,zhou,nong,xi,huo,jian,dan,zu,qie,jie,yu,ku,duan,bei,xi,you,diao,ling,ze,gong,ji,rong,zhi,xiang,gen,yi,chen,na,po,lun" + }; + + return map; + } + + private static string[] GetPinyinArray(char c) + { + int code = c; + + // 使用简化的拼音查找算法 + // 实际实现需要完整的拼音对照表 + if (PinyinMap.TryGetValue(code, out string[] py)) + { + return py; + } + + // 简化处理:根据Unicode范围估算拼音首字母 + int index = code - 0x4E00; + + // 按拼音分区(非常简化) + string[] initials = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" }; + + // 这是一个简化实现,实际需要完整的拼音表 + // 这里只是为了演示 + int initialIndex = index % initials.Length; + return new[] { initials[initialIndex].ToLower() }; + } + } +} diff --git a/EasyTool.Core/ToolCategory/RegexUtil.cs b/EasyTool.Core/TextCategory/RegexUtil.cs similarity index 74% rename from EasyTool.Core/ToolCategory/RegexUtil.cs rename to EasyTool.Core/TextCategory/RegexUtil.cs index 4694e9a..c75e228 100644 --- a/EasyTool.Core/ToolCategory/RegexUtil.cs +++ b/EasyTool.Core/TextCategory/RegexUtil.cs @@ -1,34 +1,23 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// 正则工具 /// - public class RegexUtil + public static class RegexUtil { - /// - /// 验证字符串是否与指定的正则表达式匹配 - /// - /// 要验证的字符串 - /// 正则表达式 - /// 如果字符串与正则表达式匹配,则为true;否则为false - public static bool IsMatch(string input, string pattern) - { - return Regex.IsMatch(input, pattern); - } - /// /// 验证字符串是否与指定的正则表达式匹配,并返回匹配结果 /// /// 要验证的字符串 /// 正则表达式 /// 如果字符串与正则表达式匹配,则为匹配结果;否则为null - public static string Match(string input, string pattern) + public static string? Match(string input, string pattern) { Match match = Regex.Match(input, pattern); return match.Success ? match.Value : null; @@ -45,18 +34,6 @@ public static string[] Matches(string input, string pattern) return Regex.Matches(input, pattern).Cast().Select(m => m.Value).ToArray(); } - /// - /// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串 - /// - /// 要替换的字符串 - /// 正则表达式 - /// 替换字符串 - /// 替换后的字符串 - public static string Replace(string input, string pattern, string replacement) - { - return Regex.Replace(input, pattern, replacement); - } - /// /// 使用指定的替换字符串替换输入字符串中与指定正则表达式匹配的所有子字符串,并返回替换后的字符串和替换次数 /// @@ -65,7 +42,7 @@ public static string Replace(string input, string pattern, string replacement) /// 替换字符串 /// 替换次数 /// 包含替换后的字符串和替换次数的元组 - public static (string, int) Replace(string input, string pattern, string replacement, int count) + public static (string?, int) Replace(string input, string pattern, string replacement, int count) { string result = Regex.Replace(input, pattern, replacement, RegexOptions.None, TimeSpan.FromSeconds(1)); return (result, Regex.Matches(input, pattern).Count); @@ -77,7 +54,7 @@ public static (string, int) Replace(string input, string pattern, string replace /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果的字符串数组 - public static string[] MatchGroups(string input, string pattern) + public static string[]? MatchGroups(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -96,7 +73,7 @@ public static string[] MatchGroups(string input, string pattern) /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果及分组名称的字典 - public static Dictionary MatchGroupsWithNames(string input, string pattern) + public static Dictionary? MatchGroupsWithNames(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -115,7 +92,7 @@ public static Dictionary MatchGroupsWithNames(string input, stri /// 要验证的字符串 /// 正则表达式 /// 匹配结果和捕获组名称的字典 - public static Dictionary MatchWithGroupNames(string input, string pattern) + public static Dictionary? MatchWithGroupNames(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) @@ -134,7 +111,7 @@ public static Dictionary MatchWithGroupNames(string input, strin /// 要验证的字符串 /// 正则表达式 /// 所有分组匹配结果及分组名称的元组 - public static (string, Dictionary) MatchGroupsWithNamesTuple(string input, string pattern) + public static (string?, Dictionary?) MatchGroupsWithNamesTuple(string input, string pattern) { Match match = Regex.Match(input, pattern); if (match.Success) diff --git a/EasyTool.Core/TextCategory/SegmenterUtil.cs b/EasyTool.Core/TextCategory/SegmenterUtil.cs new file mode 100644 index 0000000..3350eac --- /dev/null +++ b/EasyTool.Core/TextCategory/SegmenterUtil.cs @@ -0,0 +1,433 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 分词模式 + /// + public enum SegmentMode + { + /// + /// 精确模式 + /// + Exact, + + /// + /// 全模式 + /// + Full, + + /// + /// 搜索引擎模式 + /// + Search + } + + /// + /// 中文分词工具类 + /// 提供基础的中文分词功能(基于词典) + /// + public static class SegmenterUtil + { + private static readonly HashSet _defaultDictionary = new HashSet(StringComparer.OrdinalIgnoreCase); + private static readonly HashSet _punctuation = new HashSet + { + '\uFF0C', '\u3002', '\uFF01', '\uFF1F', '\uFF1B', '\uFF1A', // 中文标点:,。!?;: + '\u201C', '\u201D', '\u2018', '\u2019', // 中文引号:""'' + '\uFF08', '\uFF09', '\u3010', '\u3011', '\u300A', '\u300B', // 中文括号:()【】《》 + '\u3001', '\u2026', // 中文其他:、… + ',', '.', '!', '?', ';', ':', '"', '\'', // 英文标点 + '(', ')', '[', ']', '<', '>', '/', '\\', '@', '#', '$', '%', '^', '&', '*' // 英文符号 + }; + + private static readonly HashSet _stopWords = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "的", "了", "在", "是", "我", "有", "和", "就", "不", "人", "都", "一", "一个", + "上", "也", "很", "到", "说", "要", "去", "你", "会", "着", "没有", "看", "好", + "自己", "这", "那", "但", "而", "与", "或", "因为", "所以", "如果", "虽然", + "可以", "什么", "怎么", "如何", "为什么", "哪", "哪里", "哪个", "谁", "多少" + }; + + static SegmenterUtil() + { + // 初始化默认词典 + InitializeDefaultDictionary(); + } + + /// + /// 分词 + /// + /// 文本 + /// 分词模式 + /// 词语列表 + public static List Segment(string text, SegmentMode mode = SegmentMode.Exact) + { + if (string.IsNullOrWhiteSpace(text)) + return new List(); + + var result = new List(); + var words = new List(); + var buffer = new StringBuilder(); + + int i = 0; + while (i < text.Length) + { + // 跳过空白字符 + if (char.IsWhiteSpace(text[i])) + { + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + i++; + continue; + } + + // 处理标点符号 + if (_punctuation.Contains(text[i])) + { + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + i++; + continue; + } + + // 处理英文和数字 + if (IsEnglishOrDigit(text[i])) + { + if (buffer.Length > 0 && !IsEnglishOrDigit(buffer[buffer.Length - 1])) + { + ProcessBuffer(buffer, words); + buffer.Clear(); + } + buffer.Append(text[i]); + i++; + continue; + } + + // 处理中文 + buffer.Append(text[i]); + i++; + + // 尝试匹配词典中的词 + if (buffer.Length > 0 && !IsEnglishOrDigit(buffer[0])) + { + var matched = TryMatchWord(buffer.ToString(), out var matchedWord); + if (matched) + { + // 检查是否可以匹配更长的词 + if (i < text.Length && !IsEnglishOrDigit(text[i])) + { + var extended = buffer.ToString() + text[i]; + if (_defaultDictionary.Contains(extended)) + { + continue; + } + } + + words.Add(matchedWord); + buffer.Clear(); + } + } + } + + // 处理剩余的 buffer + if (buffer.Length > 0) + { + ProcessBuffer(buffer, words); + } + + return mode switch + { + SegmentMode.Full => GetAllPossibleWords(text), + SegmentMode.Search => GetSearchModeWords(words), + _ => words + }; + } + + /// + /// 分词并过滤停用词 + /// + /// 文本 + /// 分词模式 + /// 词语列表(不含停用词) + public static List SegmentWithoutStopWords(string text, SegmentMode mode = SegmentMode.Exact) + { + return Segment(text, mode) + .Where(w => !_stopWords.Contains(w)) + .ToList(); + } + + /// + /// 提取关键词 + /// + /// 文本 + /// 返回前N个关键词 + /// 关键词列表 + public static List ExtractKeywords(string text, int topN = 10) + { + var words = SegmentWithoutStopWords(text); + var frequency = new Dictionary(); + + foreach (var word in words) + { + if (word.Length < 2) + continue; + + if (frequency.ContainsKey(word)) + frequency[word]++; + else + frequency[word] = 1; + } + + return frequency + .OrderByDescending(kvp => kvp.Value) + .Take(topN) + .Select(kvp => kvp.Key) + .ToList(); + } + + /// + /// 添加自定义词典 + /// + /// 词语列表 + public static void AddToDictionary(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _defaultDictionary.Add(word.Trim()); + } + } + } + + /// + /// 添加停用词 + /// + /// 停用词列表 + public static void AddStopWords(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _stopWords.Add(word.Trim()); + } + } + } + + /// + /// 检查是否为中文 + /// + /// 字符 + /// 是否为中文 + public static bool IsChinese(char c) + { + return c >= 0x4E00 && c <= 0x9FA5; + } + + /// + /// 检查是否为英文或数字 + /// + /// 字符 + /// 是否为英文或数字 + public static bool IsEnglishOrDigit(char c) + { + return (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + char.IsDigit(c); + } + + /// + /// 统计词频 + /// + /// 文本 + /// 词频字典 + public static Dictionary GetWordFrequency(string text) + { + var words = SegmentWithoutStopWords(text); + var frequency = new Dictionary(); + + foreach (var word in words) + { + if (frequency.ContainsKey(word)) + frequency[word]++; + else + frequency[word] = 1; + } + + return frequency; + } + + #region 私有方法 + + private static void InitializeDefaultDictionary() + { + // 常用词汇 + var commonWords = new[] + { + "中国", "北京", "上海", "广州", "深圳", "杭州", "南京", "武汉", "成都", "西安", + "计算机", "互联网", "软件", "硬件", "程序", "开发", "设计", "测试", "运维", "管理", + "公司", "企业", "集团", "有限", "责任", "股份", "有限", "科技", "技术", "信息", + "手机", "电脑", "笔记本", "平板", "显示器", "键盘", "鼠标", "耳机", "音箱", + "汽车", "火车", "飞机", "地铁", "公交", "出租车", "自行车", "电动车", + "今天", "明天", "昨天", "上午", "下午", "晚上", "中午", "早上", "傍晚", + "时间", "地点", "人物", "事件", "原因", "结果", "过程", "方法", "步骤", + "学习", "工作", "生活", "娱乐", "运动", "休息", "旅游", "购物", "吃饭", + "银行", "医院", "学校", "超市", "商场", "餐厅", "酒店", "公园", "图书馆", + "苹果", "香蕉", "橙子", "西瓜", "葡萄", "草莓", "芒果", "桃子", "梨子", + "开始", "结束", "继续", "暂停", "停止", "运行", "执行", "完成", "失败", "成功", + "问题", "答案", "解决", "方案", "建议", "意见", "观点", "看法", "想法", "思路", + "重要", "紧急", "必要", "可能", "必须", "应该", "需要", "想要", "希望", "期待", + "人工智能", "机器学习", "深度学习", "自然语言", "计算机视觉", "数据分析", + "云计算", "大数据", "区块链", "物联网", "虚拟现实", "增强现实", + "程序员", "工程师", "设计师", "产品经理", "项目经理", "架构师", "测试工程师" + }; + + foreach (var word in commonWords) + { + _defaultDictionary.Add(word); + } + } + + private static void ProcessBuffer(StringBuilder buffer, List words) + { + var text = buffer.ToString(); + + if (string.IsNullOrWhiteSpace(text)) + return; + + // 如果是英文或数字,直接添加 + if (text.All(c => IsEnglishOrDigit(c) || char.IsWhiteSpace(c))) + { + words.Add(text.Trim()); + return; + } + + // 对于中文,进行最大匹配分词 + var segments = MaxMatchSegment(text); + words.AddRange(segments); + } + + private static List MaxMatchSegment(string text) + { + var result = new List(); + var maxLength = 5; // 最大词长 + var i = 0; + + while (i < text.Length) + { + var matched = false; + + // 从最大长度开始匹配 + for (var len = Math.Min(maxLength, text.Length - i); len >= 1; len--) + { + var word = text.Substring(i, len); + + if (_defaultDictionary.Contains(word)) + { + result.Add(word); + i += len; + matched = true; + break; + } + } + + if (!matched) + { + // 单字切分 + result.Add(text[i].ToString()); + i++; + } + } + + return result; + } + + private static bool TryMatchWord(string text, out string matchedWord) + { + matchedWord = string.Empty; + + if (string.IsNullOrEmpty(text)) + return false; + + // 精确匹配 + if (_defaultDictionary.Contains(text)) + { + matchedWord = text; + return true; + } + + // 尝试匹配最长前缀词 + for (int len = text.Length; len >= 1; len--) + { + var prefix = text.Substring(0, len); + if (_defaultDictionary.Contains(prefix)) + { + matchedWord = prefix; + return true; + } + } + + return false; + } + + private static List GetAllPossibleWords(string text) + { + var result = new List(); + + for (int i = 0; i < text.Length; i++) + { + for (int len = 1; len <= text.Length - i && len <= 5; len++) + { + var word = text.Substring(i, len); + if (_defaultDictionary.Contains(word)) + { + result.Add(word); + } + } + } + + return result; + } + + private static List GetSearchModeWords(List words) + { + var result = new List(); + + foreach (var word in words) + { + result.Add(word); + + // 对长词进行二次切分 + if (word.Length > 2) + { + for (int len = 2; len < word.Length; len++) + { + for (int i = 0; i <= word.Length - len; i++) + { + var subWord = word.Substring(i, len); + if (_defaultDictionary.Contains(subWord)) + { + result.Add(subWord); + } + } + } + } + } + + return result; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/SensitiveWordUtil.cs b/EasyTool.Core/TextCategory/SensitiveWordUtil.cs new file mode 100644 index 0000000..ed2611d --- /dev/null +++ b/EasyTool.Core/TextCategory/SensitiveWordUtil.cs @@ -0,0 +1,365 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 敏感词过滤工具类 + /// 使用 DFA(Deterministic Finite Automaton)算法实现高效敏感词检测 + /// + public static class SensitiveWordUtil + { + private static readonly object _lock = new(); + private static Dictionary _sensitiveWordsMap = new(); + private static HashSet _sensitiveWords = new(); + private static char[] _separatorChars = { ',', ',', '\n', '\r', ';' }; + + #region 初始化 + + /// + /// 初始化敏感词库 + /// + /// 敏感词列表 + public static void Init(IEnumerable words) + { + if (words == null) + return; + + lock (_lock) + { + _sensitiveWords = new HashSet(words.Where(w => !string.IsNullOrWhiteSpace(w))); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 从文件初始化敏感词库 + /// + /// 文件路径 + /// 编码(默认UTF-8) + public static void InitFromFile(string filePath, Encoding? encoding = null) + { + if (!File.Exists(filePath)) + throw new FileNotFoundException($"敏感词文件不存在: {filePath}"); + + encoding ??= Encoding.UTF8; + var content = File.ReadAllText(filePath, encoding); + var words = content.Split(_separatorChars, StringSplitOptions.RemoveEmptyEntries); + Init(words); + } + + /// + /// 添加敏感词 + /// + /// 敏感词 + public static void AddWord(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return; + + lock (_lock) + { + _sensitiveWords.Add(word); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 批量添加敏感词 + /// + /// 敏感词列表 + public static void AddWords(IEnumerable words) + { + if (words == null) + return; + + lock (_lock) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + _sensitiveWords.Add(word); + } + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 移除敏感词 + /// + /// 敏感词 + public static void RemoveWord(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return; + + lock (_lock) + { + _sensitiveWords.Remove(word); + _sensitiveWordsMap = BuildDFA(_sensitiveWords); + } + } + + /// + /// 清空敏感词库 + /// + public static void Clear() + { + lock (_lock) + { + _sensitiveWords.Clear(); + _sensitiveWordsMap.Clear(); + } + } + + /// + /// 获取敏感词数量 + /// + public static int Count => _sensitiveWords.Count; + + #endregion + + #region DFA构建 + + private static Dictionary BuildDFA(HashSet words) + { + var map = new Dictionary(); + + foreach (var word in words) + { + if (string.IsNullOrWhiteSpace(word)) + continue; + + var currentMap = map; + for (int i = 0; i < word.Length; i++) + { + var c = word[i]; + + if (!currentMap.TryGetValue(c, out var value)) + { + value = new Dictionary(); + currentMap[c] = value; + } + + var childMap = (Dictionary)value; + + if (i == word.Length - 1) + { + childMap['\0'] = new Dictionary(); // 标记词尾 + } + else + { + currentMap = childMap; + } + } + } + + return map; + } + + #endregion + + #region 检测 + + /// + /// 检测文本是否包含敏感词 + /// + /// 待检测文本 + /// 是否包含敏感词 + public static bool Contains(string text) + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return false; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out _)) + { + return true; + } + } + + return false; + } + + /// + /// 获取文本中的所有敏感词 + /// + /// 待检测文本 + /// 敏感词列表 + public static List FindAll(string text) + { + var result = new List(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out int length)) + { + result.Add(text.Substring(i, length)); + i += length - 1; + } + } + + return result; + } + + /// + /// 获取文本中敏感词的位置信息 + /// + /// 待检测文本 + /// 敏感词位置列表(起始位置, 敏感词) + public static List<(int StartIndex, string Word)> FindAllWithPosition(string text) + { + var result = new List<(int, string)>(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + for (int i = 0; i < text.Length; i++) + { + if (CheckSensitiveWord(text, i, out int length)) + { + result.Add((i, text.Substring(i, length))); + i += length - 1; + } + } + + return result; + } + + /// + /// 统计文本中敏感词出现次数 + /// + /// 待检测文本 + /// 敏感词及其出现次数 + public static Dictionary CountWords(string text) + { + var result = new Dictionary(); + + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return result; + + foreach (var word in FindAll(text)) + { + if (result.ContainsKey(word)) + result[word]++; + else + result[word] = 1; + } + + return result; + } + + private static bool CheckSensitiveWord(string text, int beginIndex, out int length) + { + length = 0; + var currentMap = _sensitiveWordsMap; + bool found = false; + + for (int i = beginIndex; i < text.Length; i++) + { + var c = text[i]; + + if (!currentMap.TryGetValue(c, out var value)) + { + break; + } + + length++; + currentMap = (Dictionary)value; + + if (currentMap.ContainsKey('\0')) + { + found = true; + } + } + + return found && length > 0; + } + + #endregion + + #region 过滤 + + /// + /// 过滤敏感词(替换为指定字符) + /// + /// 待过滤文本 + /// 替换字符(默认 *) + /// 过滤后的文本 + public static string Filter(string text, char replaceChar = '*') + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0) + return text ?? string.Empty; + + var result = new StringBuilder(text); + + for (int i = 0; i < result.Length; i++) + { + if (CheckSensitiveWord(result.ToString(), i, out int length)) + { + for (int j = i; j < i + length && j < result.Length; j++) + { + result[j] = replaceChar; + } + i += length - 1; + } + } + + return result.ToString(); + } + + /// + /// 过滤敏感词(使用自定义替换策略) + /// + /// 待过滤文本 + /// 替换函数(参数为敏感词,返回替换后的文本) + /// 过滤后的文本 + public static string Filter(string text, Func replacer) + { + if (string.IsNullOrEmpty(text) || _sensitiveWordsMap.Count == 0 || replacer == null) + return text ?? string.Empty; + + var positions = FindAllWithPosition(text); + if (positions.Count == 0) + return text; + + var result = new StringBuilder(); + int lastIndex = 0; + + foreach (var (startIndex, word) in positions) + { + result.Append(text.Substring(lastIndex, startIndex - lastIndex)); + result.Append(replacer(word)); + lastIndex = startIndex + word.Length; + } + + if (lastIndex < text.Length) + { + result.Append(text.Substring(lastIndex)); + } + + return result.ToString(); + } + + /// + /// 高亮显示敏感词 + /// + /// 文本 + /// 高亮前缀(如 <span style=\"color:red\">) + /// 高亮后缀(如 </span>) + /// 处理后的文本 + public static string Highlight(string text, string prefix = "", string suffix = "") + { + return Filter(text, word => $"{prefix}{word}{suffix}"); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/SimHashUtil.cs b/EasyTool.Core/TextCategory/SimHashUtil.cs new file mode 100644 index 0000000..1dc86ff --- /dev/null +++ b/EasyTool.Core/TextCategory/SimHashUtil.cs @@ -0,0 +1,325 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// SimHash 工具类 + /// 用于计算文本相似度的局部敏感哈希 + /// + public static class SimHashUtil + { + /// + /// 计算 SimHash 值 + /// + public static ulong Compute(string text, int hashBits = 64) + { + if (string.IsNullOrEmpty(text)) + return 0; + + // 分词 + var tokens = Tokenize(text); + if (tokens.Count == 0) + return 0; + + // 计算每个位的权重 + int[] v = new int[hashBits]; + + foreach (var token in tokens) + { + ulong hash = HashToken(token); + + for (int i = 0; i < hashBits; i++) + { + if (((hash >> i) & 1) == 1) + { + v[i]++; + } + else + { + v[i]--; + } + } + } + + // 生成 SimHash + ulong simHash = 0; + for (int i = 0; i < hashBits; i++) + { + if (v[i] > 0) + { + simHash |= (1UL << i); + } + } + + return simHash; + } + + /// + /// 计算 Hamming 距离 + /// + public static int HammingDistance(ulong hash1, ulong hash2) + { + ulong xor = hash1 ^ hash2; + int distance = 0; + + while (xor != 0) + { + distance++; + xor &= xor - 1; + } + + return distance; + } + + /// + /// 计算两个文本的相似度(基于 SimHash) + /// + public static double Similarity(string text1, string text2, int hashBits = 64) + { + ulong hash1 = Compute(text1, hashBits); + ulong hash2 = Compute(text2, hashBits); + + int distance = HammingDistance(hash1, hash2); + return 1.0 - (double)distance / hashBits; + } + + /// + /// 判断两个文本是否相似 + /// + public static bool IsSimilar(string text1, string text2, int threshold = 3) + { + ulong hash1 = Compute(text1); + ulong hash2 = Compute(text2); + + return HammingDistance(hash1, hash2) <= threshold; + } + + /// + /// 计算 SimHash 并返回十六进制字符串 + /// + public static string ComputeHex(string text) + { + ulong hash = Compute(text); + return hash.ToString("X16"); + } + + /// + /// 从十六进制字符串解析 SimHash + /// + public static ulong ParseHex(string hex) + { + return ulong.Parse(hex, System.Globalization.NumberStyles.HexNumber); + } + + private static List Tokenize(string text) + { + var tokens = new List(); + + // 简单分词:按空格和非字母数字字符分割 + var words = text.Split(new[] { ' ', '\t', '\n', '\r', '.', ',', '!', '?', ';', ':', '"', '\'', '(', ')', '[', ']', '{', '}' }, + StringSplitOptions.RemoveEmptyEntries); + + // 添加单词 + foreach (var word in words) + { + string lower = word.ToLowerInvariant(); + if (lower.Length >= 2) // 忽略单字符 + { + tokens.Add(lower); + } + } + + // 对于中文,添加2-gram和3-gram + foreach (char c in text) + { + if (c >= 0x4E00 && c <= 0x9FA5) + { + tokens.Add(c.ToString()); + } + } + + // 添加字符n-gram + if (text.Length >= 2) + { + for (int i = 0; i < text.Length - 1; i++) + { + tokens.Add(text.Substring(i, 2)); + } + } + + return tokens; + } + + private static ulong HashToken(string token) + { + // 使用 MurmurHash3 简化版 + byte[] data = Encoding.UTF8.GetBytes(token); + + unchecked + { + const ulong c1 = 0x87c37b91114253d5; + const ulong c2 = 0x4cf5ad432745937f; + + ulong h1 = 0; + int length = data.Length; + int blocks = length / 8; + int i = 0; + + for (int j = 0; j < blocks; j++) + { + ulong k1 = BitConverter.ToUInt64(data, i); + i += 8; + + k1 *= c1; + k1 = (k1 << 31) | (k1 >> 33); + k1 *= c2; + h1 ^= k1; + + h1 = (h1 << 27) | (h1 >> 37); + h1 = h1 * 5 + 0x52dce729; + } + + ulong remaining = 0; + int remainingLength = length - blocks * 8; + if (remainingLength > 0) + { + for (int j = 0; j < remainingLength; j++) + { + remaining |= (ulong)data[i + j] << (j * 8); + } + + remaining *= c1; + remaining = (remaining << 31) | (remaining >> 33); + remaining *= c2; + h1 ^= remaining; + } + + h1 ^= (ulong)length; + h1 ^= h1 >> 33; + h1 *= 0xff51afd7ed558ccd; + h1 ^= h1 >> 33; + h1 *= 0xc4ceb9fe1a85ec53; + h1 ^= h1 >> 33; + + return h1; + } + } + } + + /// + /// MinHash 工具类 + /// 用于集合相似度计算 + /// + public class MinHash + { + private readonly int _numHashes; + private readonly uint[] _seeds; + + /// + /// 哈希函数数量 + /// + public int NumHashes => _numHashes; + + /// + /// 创建 MinHash + /// + public MinHash(int numHashes = 128) + { + _numHashes = numHashes; + _seeds = new uint[numHashes]; + + var random = new Random(42); + for (int i = 0; i < numHashes; i++) + { + _seeds[i] = (uint)random.Next(); + } + } + + /// + /// 计算集合的 MinHash 签名 + /// + public uint[] ComputeSignature(HashSet set) + { + var signature = new uint[_numHashes]; + + for (int i = 0; i < _numHashes; i++) + { + uint minHash = uint.MaxValue; + + foreach (var item in set) + { + uint hash = Hash(item, _seeds[i]); + if (hash < minHash) + { + minHash = hash; + } + } + + signature[i] = minHash; + } + + return signature; + } + + /// + /// 计算文本的 MinHash 签名 + /// + public uint[] ComputeSignature(string text) + { + var set = new HashSet(); + var words = text.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var word in words) + { + set.Add(word.ToLowerInvariant()); + } + + // 添加n-gram + if (text.Length >= 2) + { + for (int i = 0; i < text.Length - 1; i++) + { + set.Add(text.Substring(i, 2)); + } + } + + return ComputeSignature(set); + } + + /// + /// 计算两个签名的 Jaccard 相似度估计 + /// + public static double EstimateSimilarity(uint[] signature1, uint[] signature2) + { + if (signature1.Length != signature2.Length) + throw new ArgumentException("Signatures must have the same length"); + + int matches = 0; + for (int i = 0; i < signature1.Length; i++) + { + if (signature1[i] == signature2[i]) + { + matches++; + } + } + + return (double)matches / signature1.Length; + } + + private static uint Hash(string s, uint seed) + { + unchecked + { + uint hash = seed; + foreach (char c in s) + { + hash = hash * 31 + c; + } + return hash; + } + } + } +} diff --git a/EasyTool.Core/TextCategory/SlugUtil.cs b/EasyTool.Core/TextCategory/SlugUtil.cs new file mode 100644 index 0000000..23bedbe --- /dev/null +++ b/EasyTool.Core/TextCategory/SlugUtil.cs @@ -0,0 +1,265 @@ +using System; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// URL Slug 生成工具类 + /// 用于生成友好的 URL 路径 + /// + public static class SlugUtil + { + /// + /// 生成 URL Slug + /// + /// 原始文本 + /// 生成选项 + /// Slug 字符串 + public static string Generate(string text, SlugOptions? options = null) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + options ??= new SlugOptions(); + + var result = text; + + // 转换为小写 + if (options.Lowercase) + { + result = result.ToLowerInvariant(); + } + + // 音译非拉丁字符 + result = Transliterate(result); + + // 移除 HTML 标签 + if (options.StripHtml) + { + result = Regex.Replace(result, @"<[^>]+>", ""); + } + + // 移除特殊字符 + result = Regex.Replace(result, @"[^\w\s\-]", ""); + + // 替换空格 + result = Regex.Replace(result, @"\s+", options.Delimiter.ToString()); + + // 替换多个分隔符 + var delimiterPattern = Regex.Escape(options.Delimiter.ToString()); + result = Regex.Replace(result, $@"{delimiterPattern}+", options.Delimiter.ToString()); + + // 裁剪首尾分隔符 + result = result.Trim(options.Delimiter); + + // 限制长度 + if (options.MaxLength > 0 && result.Length > options.MaxLength) + { + result = result.Substring(0, options.MaxLength); + + // 确保不在单词中间截断 + var lastDelimiter = result.LastIndexOf(options.Delimiter); + if (lastDelimiter > options.MaxLength * 0.5) + { + result = result.Substring(0, lastDelimiter); + } + } + + return result; + } + + /// + /// 从标题生成 Slug + /// + /// 标题 + /// Slug 字符串 + public static string FromTitle(string title) + { + return Generate(title, new SlugOptions { MaxLength = 100 }); + } + + /// + /// 生成唯一 Slug(添加数字后缀) + /// + /// 原始文本 + /// 已存在的 Slug 集合 + /// 生成选项 + /// 唯一的 Slug + public static string GenerateUnique(string text, System.Collections.Generic.ISet existingSlugs, SlugOptions? options = null) + { + var baseSlug = Generate(text, options); + + if (!existingSlugs.Contains(baseSlug)) + return baseSlug; + + var counter = 1; + string uniqueSlug; + + do + { + uniqueSlug = $"{baseSlug}-{counter}"; + counter++; + } + while (existingSlugs.Contains(uniqueSlug)); + + return uniqueSlug; + } + + /// + /// 验证 Slug 格式 + /// + /// Slug 字符串 + /// 是否有效 + public static bool IsValid(string slug) + { + if (string.IsNullOrEmpty(slug)) + return false; + + // 只允许小写字母、数字、连字符 + return Regex.IsMatch(slug, @"^[a-z0-9]+(?:-[a-z0-9]+)*$"); + } + + /// + /// 从 Slug 还原可读文本 + /// + /// Slug 字符串 + /// 可读文本 + public static string ToReadable(string slug) + { + if (string.IsNullOrEmpty(slug)) + return string.Empty; + + var result = slug.Replace('-', ' '); + + // 首字母大写 + var words = result.Split(' '); + for (int i = 0; i < words.Length; i++) + { + if (words[i].Length > 0) + { + words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1); + } + } + + return string.Join(" ", words); + } + + /// + /// 音译非拉丁字符 + /// + private static string Transliterate(string text) + { + var result = new StringBuilder(); + + foreach (var c in text.Normalize(NormalizationForm.FormD)) + { + // 移除变音符号 + var category = char.GetUnicodeCategory(c); + if (category != System.Globalization.UnicodeCategory.NonSpacingMark) + { + result.Append(c); + } + } + + text = result.ToString().Normalize(NormalizationForm.FormC); + + // 中文拼音映射(常用字) + text = TransliterateChinese(text); + + // 其他常见音译 + text = text + .Replace("ß", "ss") + .Replace("æ", "ae") + .Replace("ø", "o") + .Replace("å", "a") + .Replace("ł", "l") + .Replace("ń", "n") + .Replace("ś", "s") + .Replace("ż", "z") + .Replace("ź", "z"); + + return text; + } + + /// + /// 中文转拼音(简化版) + /// + private static string TransliterateChinese(string text) + { + var result = new StringBuilder(); + + foreach (var c in text) + { + var pinyin = GetPinyin(c); + if (!string.IsNullOrEmpty(pinyin)) + { + result.Append(pinyin); + result.Append('-'); + } + else + { + result.Append(c); + } + } + + return result.ToString().TrimEnd('-'); + } + + /// + /// 获取汉字拼音(简化映射) + /// + private static string GetPinyin(char c) + { + // 这里只提供一些常用字的映射,实际应用可以使用完整的拼音库 + var pinyinMap = new System.Collections.Generic.Dictionary + { + {'你', "ni"}, {'好', "hao"}, {'是', "shi"}, {'我', "wo"}, {'他', "ta"}, + {'她', "ta"}, {'们', "men"}, {'的', "de"}, {'了', "le"}, {'在', "zai"}, + {'有', "you"}, {'和', "he"}, {'人', "ren"}, {'这', "zhe"}, {'中', "zhong"}, + {'大', "da"}, {'为', "wei"}, {'上', "shang"}, {'个', "ge"}, {'国', "guo"}, + {'到', "dao"}, {'说', "shuo"}, {'要', "yao"}, {'也', "ye"}, {'出', "chu"}, + {'会', "hui"}, {'可', "ke"}, {'能', "neng"}, {'对', "dui"}, {'生', "sheng"}, + {'而', "er"}, {'子', "zi"}, {'那', "na"}, {'得', "de"}, {'于', "yu"}, + {'着', "zhe"}, {'下', "xia"}, {'自', "zi"}, {'之', "zhi"}, {'年', "nian"}, + {'过', "guo"}, {'发', "fa"}, {'后', "hou"}, {'作', "zuo"}, {'里', "li"}, + {'用', "yong"}, {'道', "dao"}, {'行', "xing"}, {'所', "suo"}, {'然', "ran"}, + {'家', "jia"}, {'种', "zhong"}, {'事', "shi"}, {'成', "cheng"}, {'方', "fang"}, + {'多', "duo"}, {'经', "jing"}, {'么', "me"}, {'去', "qu"}, {'法', "fa"}, + {'学', "xue"}, {'如', "ru"}, {'都', "dou"}, {'同', "tong"}, {'现', "xian"}, + {'当', "dang"}, {'没', "mei"}, {'动', "dong"}, {'面', "mian"}, {'起', "qi"}, + {'看', "kan"}, {'定', "ding"}, {'天', "tian"}, {'分', "fen"}, {'还', "hai"}, + {'进', "jin"}, {'小', "xiao"}, {'其', "qi"} + }; + + return pinyinMap.TryGetValue(c, out var pinyin) ? pinyin : string.Empty; + } + } + + /// + /// Slug 生成选项 + /// + public class SlugOptions + { + /// + /// 是否转换为小写 + /// + public bool Lowercase { get; set; } = true; + + /// + /// 分隔符 + /// + public char Delimiter { get; set; } = '-'; + + /// + /// 最大长度 + /// + public int MaxLength { get; set; } = 0; + + /// + /// 是否移除 HTML 标签 + /// + public bool StripHtml { get; set; } = true; + } +} diff --git a/EasyTool.Core/TextCategory/SpellCheckerUtil.cs b/EasyTool.Core/TextCategory/SpellCheckerUtil.cs new file mode 100644 index 0000000..1bb880c --- /dev/null +++ b/EasyTool.Core/TextCategory/SpellCheckerUtil.cs @@ -0,0 +1,340 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 拼写检查器 + /// 提供英文拼写检查和纠错功能 + /// + public static class SpellCheckerUtil + { + private static readonly HashSet _dictionary = new HashSet(StringComparer.OrdinalIgnoreCase); + private static readonly char[] _alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); + + static SpellCheckerUtil() + { + InitializeDictionary(); + } + + /// + /// 检查单词拼写是否正确 + /// + /// 单词 + /// 是否正确 + public static bool IsCorrect(string word) + { + if (string.IsNullOrWhiteSpace(word)) + return true; + + return _dictionary.Contains(word.Trim().ToLowerInvariant()); + } + + /// + /// 获取拼写建议 + /// + /// 单词 + /// 最大建议数量 + /// 建议列表 + public static List GetSuggestions(string word, int maxSuggestions = 5) + { + if (string.IsNullOrWhiteSpace(word)) + return new List(); + + word = word.Trim().ToLowerInvariant(); + + // 如果拼写正确,返回空列表 + if (_dictionary.Contains(word)) + return new List(); + + var candidates = new Dictionary(); + + // 编辑距离为1的候选词 + var edits1 = GetEdits1(word); + foreach (var edit in edits1) + { + if (_dictionary.Contains(edit)) + { + candidates[edit] = 1; + } + } + + // 编辑距离为2的候选词(如果没有找到距离1的) + if (candidates.Count == 0) + { + foreach (var edit1 in edits1) + { + var edits2 = GetEdits1(edit1); + foreach (var edit2 in edits2) + { + if (_dictionary.Contains(edit2) && !candidates.ContainsKey(edit2)) + { + candidates[edit2] = 2; + } + } + } + } + + return candidates + .OrderBy(kvp => kvp.Value) + .ThenBy(kvp => LevenshteinDistance(word, kvp.Key)) + .Take(maxSuggestions) + .Select(kvp => kvp.Key) + .ToList(); + } + + /// + /// 检查文本中的拼写错误 + /// + /// 文本 + /// 错误单词及其建议 + public static Dictionary> CheckText(string text) + { + var result = new Dictionary>(); + + if (string.IsNullOrWhiteSpace(text)) + return result; + + var words = ExtractWords(text); + + foreach (var word in words) + { + if (!IsCorrect(word) && !result.ContainsKey(word)) + { + result[word] = GetSuggestions(word); + } + } + + return result; + } + + /// + /// 自动纠正拼写错误 + /// + /// 文本 + /// 纠正后的文本 + public static string AutoCorrect(string text) + { + if (string.IsNullOrWhiteSpace(text)) + return text; + + var words = ExtractWords(text); + var result = text; + + foreach (var word in words) + { + if (!IsCorrect(word)) + { + var suggestions = GetSuggestions(word, 1); + if (suggestions.Count > 0) + { + result = ReplaceWord(result, word, suggestions[0]); + } + } + } + + return result; + } + + /// + /// 添加单词到词典 + /// + /// 单词列表 + public static void AddToDictionary(IEnumerable words) + { + foreach (var word in words) + { + if (!string.IsNullOrWhiteSpace(word)) + { + _dictionary.Add(word.Trim().ToLowerInvariant()); + } + } + } + + /// + /// 从文件加载词典 + /// + /// 文件路径 + public static void LoadDictionary(string filePath) + { + try + { + var lines = System.IO.File.ReadAllLines(filePath); + AddToDictionary(lines); + } + catch (Exception) + { + // 忽略错误 + } + } + + /// + /// 获取词典大小 + /// + /// 词典单词数量 + public static int GetDictionarySize() + { + return _dictionary.Count; + } + + #region 私有方法 + + private static void InitializeDictionary() + { + // 常用英语单词 + var commonWords = new[] + { + "the", "be", "to", "of", "and", "a", "in", "that", "have", "i", + "it", "for", "not", "on", "with", "he", "as", "you", "do", "at", + "this", "but", "his", "by", "from", "they", "we", "say", "her", "she", + "or", "an", "will", "my", "one", "all", "would", "there", "their", "what", + "so", "up", "out", "if", "about", "who", "get", "which", "go", "me", + "when", "make", "can", "like", "time", "no", "just", "him", "know", "take", + "people", "into", "year", "your", "good", "some", "could", "them", "see", "other", + "than", "then", "now", "look", "only", "come", "its", "over", "think", "also", + "back", "after", "use", "two", "how", "our", "work", "first", "well", "way", + "even", "new", "want", "because", "any", "these", "give", "day", "most", "us", + "hello", "world", "computer", "program", "software", "hardware", "system", "network", + "internet", "website", "application", "development", "design", "testing", "code", + "data", "database", "server", "client", "user", "password", "email", "message", + "file", "folder", "directory", "document", "image", "video", "audio", "music", + "game", "play", "player", "team", "sport", "football", "basketball", "tennis", + "school", "student", "teacher", "class", "lesson", "book", "read", "write", + "learn", "study", "exam", "test", "question", "answer", "problem", "solution", + "work", "job", "office", "company", "business", "money", "price", "cost", + "buy", "sell", "shop", "store", "market", "product", "service", "customer", + "food", "drink", "water", "coffee", "tea", "breakfast", "lunch", "dinner", + "house", "home", "room", "door", "window", "bed", "table", "chair", "kitchen", + "car", "bus", "train", "plane", "airport", "station", "road", "street", "city", + "country", "world", "earth", "sun", "moon", "star", "sky", "weather", "rain", + "love", "hate", "happy", "sad", "angry", "tired", "hungry", "thirsty", "sleep", + "family", "mother", "father", "brother", "sister", "child", "baby", "friend", + "health", "doctor", "hospital", "medicine", "sick", "healthy", "exercise", + "phone", "call", "number", "address", "name", "age", "birthday", "date", + "time", "hour", "minute", "second", "week", "month", "year", "today", + "tomorrow", "yesterday", "morning", "afternoon", "evening", "night", + "spring", "summer", "autumn", "winter", "hot", "cold", "warm", "cool", + "big", "small", "large", "little", "long", "short", "high", "low", + "fast", "slow", "quick", "easy", "hard", "simple", "complex", "different" + }; + + foreach (var word in commonWords) + { + _dictionary.Add(word.ToLowerInvariant()); + } + } + + private static HashSet GetEdits1(string word) + { + var edits = new HashSet(); + + // 删除 + for (int i = 0; i < word.Length; i++) + { + edits.Add(word.Substring(0, i) + word.Substring(i + 1)); + } + + // 交换 + for (int i = 0; i < word.Length - 1; i++) + { + edits.Add(word.Substring(0, i) + word[i + 1] + word[i] + word.Substring(i + 2)); + } + + // 替换 + for (int i = 0; i < word.Length; i++) + { + foreach (var c in _alphabet) + { + edits.Add(word.Substring(0, i) + c + word.Substring(i + 1)); + } + } + + // 插入 + for (int i = 0; i <= word.Length; i++) + { + foreach (var c in _alphabet) + { + edits.Add(word.Substring(0, i) + c + word.Substring(i)); + } + } + + return edits; + } + + private static int LevenshteinDistance(string s1, string s2) + { + var matrix = new int[s1.Length + 1, s2.Length + 1]; + + for (int i = 0; i <= s1.Length; i++) + matrix[i, 0] = i; + + for (int j = 0; j <= s2.Length; j++) + matrix[0, j] = j; + + for (int i = 1; i <= s1.Length; i++) + { + for (int j = 1; j <= s2.Length; j++) + { + var cost = s1[i - 1] == s2[j - 1] ? 0 : 1; + matrix[i, j] = Math.Min( + Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1), + matrix[i - 1, j - 1] + cost); + } + } + + return matrix[s1.Length, s2.Length]; + } + + private static List ExtractWords(string text) + { + var words = new List(); + var currentWord = new System.Text.StringBuilder(); + + foreach (var c in text) + { + if (char.IsLetter(c)) + { + currentWord.Append(c); + } + else if (currentWord.Length > 0) + { + words.Add(currentWord.ToString()); + currentWord.Clear(); + } + } + + if (currentWord.Length > 0) + { + words.Add(currentWord.ToString()); + } + + return words; + } + + private static string ReplaceWord(string text, string oldWord, string newWord) + { + // 保持原始大小写 + var index = text.IndexOf(oldWord, StringComparison.OrdinalIgnoreCase); + if (index < 0) + return text; + + var originalWord = text.Substring(index, oldWord.Length); + + // 调整新词的大小写 + string replacement; + if (char.IsUpper(originalWord[0])) + { + replacement = char.ToUpper(newWord[0]) + newWord.Substring(1); + } + else + { + replacement = newWord; + } + + return text.Substring(0, index) + replacement + text.Substring(index + oldWord.Length); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/StrSplitter.cs b/EasyTool.Core/TextCategory/StrSplitter.cs index cc2c6e2..fb85ebd 100644 --- a/EasyTool.Core/TextCategory/StrSplitter.cs +++ b/EasyTool.Core/TextCategory/StrSplitter.cs @@ -3,9 +3,9 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { - public class StrSplitter + public static class StrSplitter { /// /// 使用指定的分隔符将输入字符串分割成字符串数组。 diff --git a/EasyTool.Core/ToolCategory/StrUtil.cs b/EasyTool.Core/TextCategory/StrUtil.cs similarity index 57% rename from EasyTool.Core/ToolCategory/StrUtil.cs rename to EasyTool.Core/TextCategory/StrUtil.cs index 938649c..1ae076f 100644 --- a/EasyTool.Core/ToolCategory/StrUtil.cs +++ b/EasyTool.Core/TextCategory/StrUtil.cs @@ -3,34 +3,27 @@ using System.Text; using System.Text.RegularExpressions; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// 字符串处理工具类 /// - public class StrUtil + public static class StrUtil { /// /// 移除字符串中的所有空格 /// /// 要处理的字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string RemoveAllSpaces(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return Regex.Replace(str, @"\s+", ""); } - /// - /// 将字符串中的指定字符替换成新的字符 - /// - /// 要处理的字符串 - /// 要替换的字符 - /// 新的字符 - /// 处理后的字符串 - public static string ReplaceChar(string str, char oldChar, char newChar) - { - return str.Replace(oldChar, newChar); - } /// /// 检查字符串是否为数字 @@ -39,8 +32,11 @@ public static string ReplaceChar(string str, char oldChar, char newChar) /// 如果是数字,则返回true,否则返回false public static bool IsNumeric(string str) { - double result; - return double.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return double.TryParse(str, out _); } /// @@ -50,8 +46,11 @@ public static bool IsNumeric(string str) /// 如果是整数,则返回true,否则返回false public static bool IsInteger(string str) { - int result; - return int.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return int.TryParse(str, out _); } /// @@ -61,174 +60,136 @@ public static bool IsInteger(string str) /// 如果是日期,则返回true,否则返回false public static bool IsDate(string str) { - DateTime result; - return DateTime.TryParse(str, out result); + if (string.IsNullOrEmpty(str)) + { + return false; + } + return DateTime.TryParse(str, out _); } - /// - /// 获取字符串的字节数组 - /// - /// 要处理的字符串 - /// 字符串的字节数组 - public static byte[] GetBytes(string str) - { - return System.Text.Encoding.UTF8.GetBytes(str); - } - /// - /// 将字节数组转换为字符串 - /// - /// 要处理的字节数组 - /// 字节数组转换后的字符串 - public static string GetString(byte[] bytes) - { - return System.Text.Encoding.UTF8.GetString(bytes); - } - /// - /// 将字符串转换为大写 - /// - /// 要处理的字符串 - /// 处理后的字符串 - public static string ToUpperCase(string str) - { - return str.ToUpper(); - } - /// - /// 将字符串转换为小写 - /// - /// 要处理的字符串 - /// 处理后的字符串 - public static string ToLowerCase(string str) - { - return str.ToLower(); - } - /// - /// 检查字符串是否为空或null - /// - /// 要检查的字符串 - /// 如果是空或null,则返回true,否则返回false - public static bool IsNullOrEmpty(string str) - { - return string.IsNullOrEmpty(str); - } - /// - /// 检查字符串是否为空或仅由空格组成 - /// - /// 要检查的字符串 - /// 如果是空或仅由空格组成,则返回true,否则返回false - public static bool IsNullOrWhiteSpace(string str) - { - return string.IsNullOrWhiteSpace(str); - } - /// - /// 截取字符串的指定部分 - /// - /// 要处理的字符串 - /// 起始位置(从0开始) - /// 要截取的长度 - /// 截取后的字符串 - public static string Substring(string str, int startIndex, int length) - { - return str.Substring(startIndex, length); - } /// /// 将字符串转换为驼峰命名法 /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToCamelCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += words[i].Substring(0, 1).ToUpper() + words[i].Substring(1).ToLower(); + sb.Append(words[i].Substring(0, 1).ToUpper()); + sb.Append(words[i].Substring(1).ToLower()); } } - return result; + return sb.ToString(); } /// /// 将字符串转换为帕斯卡命名法(大驼峰命名法) /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToPascalCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { - result += words[i].Substring(0, 1).ToUpper() + words[i].Substring(1).ToLower(); + sb.Append(words[i].Substring(0, 1).ToUpper()); + sb.Append(words[i].Substring(1).ToLower()); } - return result; + return sb.ToString(); } /// /// 将字符串转换为下划线命名法 /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToSnakeCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += "_" + words[i].ToLower(); + sb.Append('_'); + sb.Append(words[i].ToLower()); } } - return result; + return sb.ToString(); } /// /// 将字符串转换为连字符命名法(短横线命名法) /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToKebabCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } string[] words = str.Split(new char[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); - string result = ""; + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { if (i == 0) { - result += words[i].ToLower(); + sb.Append(words[i].ToLower()); } else { - result += "-" + words[i].ToLower(); + sb.Append('-'); + sb.Append(words[i].ToLower()); } } - return result; + return sb.ToString(); } /// /// 将字符串中的 HTML 标记去除 /// /// 要处理的字符串 - /// 去除 HTML 标记后的字符串 + /// 去除 HTML 标记后的字符串,如果输入为 null 则返回空字符串 public static string StripHtml(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return Regex.Replace(str, "<.*?>", ""); } @@ -240,32 +201,12 @@ public static string StripHtml(string str) /// 如果相等,则返回true,否则返回false public static bool EqualsIgnoreCaseAndWhiteSpace(string str1, string str2) { + if (str1 == null && str2 == null) return true; + if (str1 == null || str2 == null) return false; return string.Equals(RemoveAllSpaces(str1), RemoveAllSpaces(str2), StringComparison.OrdinalIgnoreCase); } - /// - /// 在字符串的左侧填充指定字符,使字符串达到指定长度 - /// - /// 要处理的字符串 - /// 指定长度 - /// 填充字符 - /// 处理后的字符串 - public static string PadLeft(string str, int length, char paddingChar) - { - return str.PadLeft(length, paddingChar); - } - /// - /// 在字符串的右侧填充指定字符,使字符串达到指定长度 - /// - /// 要处理的字符串 - /// 指定长度 - /// 填充字符 - /// 处理后的字符串 - public static string PadRight(string str, int length, char paddingChar) - { - return str.PadRight(length, paddingChar); - } /// /// 将字符串中的某些字符替换成指定的字符 @@ -273,9 +214,17 @@ public static string PadRight(string str, int length, char paddingChar) /// 要处理的字符串 /// 要替换的字符数组 /// 新的字符 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceChars(string str, char[] chars, char newChar) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (chars == null) + { + return str; + } for (int i = 0; i < chars.Length; i++) { str = str.Replace(chars[i], newChar); @@ -289,9 +238,17 @@ public static string ReplaceChars(string str, char[] chars, char newChar) /// 要处理的字符串 /// 要替换的子字符串数组 /// 新的子字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceStrings(string str, string[] oldValues, string newValue) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (oldValues == null) + { + return str; + } for (int i = 0; i < oldValues.Length; i++) { str = str.Replace(oldValues[i], newValue); @@ -305,9 +262,17 @@ public static string ReplaceStrings(string str, string[] oldValues, string newVa /// 要处理的字符串 /// 要替换的子字符串数组 /// 新的子字符串 - /// 处理后的字符串 + /// 处理后的字符串,如果输入为 null 则返回空字符串 public static string ReplaceStringsIgnoreCase(string str, string[] oldValues, string newValue) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + if (oldValues == null) + { + return str; + } for (int i = 0; i < oldValues.Length; i++) { str = Regex.Replace(str, oldValues[i], newValue, RegexOptions.IgnoreCase); @@ -319,9 +284,13 @@ public static string ReplaceStringsIgnoreCase(string str, string[] oldValues, st /// 将字符串转换为 Title Case 格式,即每个单词的首字母大写 /// /// 要处理的字符串 - /// 转换后的字符串 + /// 转换后的字符串,如果输入为 null 则返回空字符串 public static string ToTitleCase(string str) { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower()); } diff --git a/EasyTool.Core/TextCategory/StringBuilderExtension.cs b/EasyTool.Core/TextCategory/StringBuilderExtension.cs new file mode 100644 index 0000000..d8a5610 --- /dev/null +++ b/EasyTool.Core/TextCategory/StringBuilderExtension.cs @@ -0,0 +1,407 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// StringBuilder 扩展方法 + /// + public static class StringBuilderExtension + { + #region 追加操作 + + /// + /// 追加带格式的字符串(忽略 null 值) + /// + public static StringBuilder AppendFormatIfNotNull(this StringBuilder sb, string format, object? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (arg != null) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 追加带格式的字符串(忽略空字符串) + /// + public static StringBuilder AppendFormatIfNotEmpty(this StringBuilder sb, string format, string? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(arg)) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 追加带格式的字符串(忽略空白字符串) + /// + public static StringBuilder AppendFormatIfNotBlank(this StringBuilder sb, string format, string? arg) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrWhiteSpace(arg)) + { + sb.AppendFormat(format, arg); + } + return sb; + } + + /// + /// 条件追加字符串 + /// + public static StringBuilder AppendIf(this StringBuilder sb, string? value, bool condition) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (condition) + { + sb.Append(value); + } + return sb; + } + + /// + /// 条件追加字符串(带分隔符) + /// + public static StringBuilder AppendWithSeparator(this StringBuilder sb, string? value, string separator = ", ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (sb.Length > 0 && !string.IsNullOrEmpty(separator)) + { + sb.Append(separator); + } + sb.Append(value); + return sb; + } + + /// + /// 条件追加字符串(带分隔符,仅在非空时追加) + /// + public static StringBuilder AppendWithSeparatorIfNotEmpty(this StringBuilder sb, string? value, string separator = ", ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.AppendWithSeparator(value, separator); + } + return sb; + } + + /// + /// 追加行(仅当值非空时) + /// + public static StringBuilder AppendLineIfNotEmpty(this StringBuilder sb, string? value) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.AppendLine(value); + } + return sb; + } + + /// + /// 条件追加行 + /// + public static StringBuilder AppendLineIf(this StringBuilder sb, string? value, bool condition) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (condition) + { + sb.AppendLine(value); + } + return sb; + } + + #endregion + + #region 括号包裹 + + /// + /// 用括号包裹内容(如果非空) + /// + public static StringBuilder AppendInParentheses(this StringBuilder sb, string? value, string open = "(", string close = ")") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value)) + { + sb.Append(open).Append(value).Append(close); + } + return sb; + } + + /// + /// 用方括号包裹内容(如果非空) + /// + public static StringBuilder AppendInBrackets(this StringBuilder sb, string? value) + { + return sb.AppendInParentheses(value, "[", "]"); + } + + /// + /// 用花括号包裹内容(如果非空) + /// + public static StringBuilder AppendInBraces(this StringBuilder sb, string? value) + { + return sb.AppendInParentheses(value, "{", "}"); + } + + /// + /// 用引号包裹内容(如果非空) + /// + public static StringBuilder AppendInQuotes(this StringBuilder sb, string? value, string quote = "\"") + { + return sb.AppendInParentheses(value, quote, quote); + } + + #endregion + + #region 缩进操作 + + /// + /// 增加缩进 + /// + /// StringBuilder + /// 缩进字符串,默认为两个空格 + public static StringBuilder Indent(this StringBuilder sb, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + return sb.Append(indent); + } + + /// + /// 增加指定层数的缩进 + /// + public static StringBuilder Indent(this StringBuilder sb, int level, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + for (int i = 0; i < level; i++) + { + sb.Append(indent); + } + return sb; + } + + /// + /// 添加缩进行 + /// + public static StringBuilder AppendIndentedLine(this StringBuilder sb, string value, int level = 1, string indent = " ") + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + return sb.Indent(level, indent).AppendLine(value); + } + + #endregion + + #region 清除操作 + + /// + /// 清除最后 N 个字符 + /// + public static StringBuilder RemoveLast(this StringBuilder sb, int count) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (count > 0 && sb.Length >= count) + { + sb.Remove(sb.Length - count, count); + } + return sb; + } + + /// + /// 清除最后的一个字符 + /// + public static StringBuilder RemoveLastChar(this StringBuilder sb) + { + return sb.RemoveLast(1); + } + + /// + /// 清除最后指定的字符串(如果匹配) + /// + public static StringBuilder RemoveLastIf(this StringBuilder sb, string? value) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + if (!string.IsNullOrEmpty(value) && sb.Length >= value.Length) + { + int startIndex = sb.Length - value.Length; + string end = sb.ToString(startIndex, value.Length); + if (end == value) + { + sb.Remove(startIndex, value.Length); + } + } + return sb; + } + + /// + /// 清除最后的空白字符 + /// + public static StringBuilder TrimEnd(this StringBuilder sb) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + while (sb.Length > 0 && char.IsWhiteSpace(sb[sb.Length - 1])) + { + sb.Remove(sb.Length - 1, 1); + } + return sb; + } + + /// + /// 清除开头的空白字符 + /// + public static StringBuilder TrimStart(this StringBuilder sb) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + int index = 0; + while (index < sb.Length && char.IsWhiteSpace(sb[index])) + { + index++; + } + + if (index > 0) + { + sb.Remove(0, index); + } + return sb; + } + + /// + /// 清除开头和结尾的空白字符 + /// + public static StringBuilder Trim(this StringBuilder sb) + { + return sb.TrimStart().TrimEnd(); + } + + #endregion + + #region 转换操作 + + /// + /// 转换为 MemoryStream + /// + public static MemoryStream ToMemoryStream(this StringBuilder sb, Encoding? encoding = null) + { + if (sb == null) + throw new ArgumentNullException(nameof(sb)); + + encoding ??= Encoding.UTF8; + var bytes = encoding.GetBytes(sb.ToString()); + return new MemoryStream(bytes); + } + + #endregion + + #region 检查操作 + + /// + /// 判断是否为空 + /// + public static bool IsNullOrEmpty(this StringBuilder? sb) + { + return sb == null || sb.Length == 0; + } + + /// + /// 判断是否包含指定字符串 + /// + public static bool Contains(this StringBuilder? sb, string? value) + { + if (sb == null) + return false; + + return sb.IndexOf(value) >= 0; + } + + /// + /// 查找字符串的位置 + /// + public static int IndexOf(this StringBuilder? sb, string? value, int startIndex = 0, bool ignoreCase = false) + { + if (sb == null || string.IsNullOrEmpty(value)) + return -1; + + if (startIndex < 0 || startIndex >= sb.Length) + return -1; + + var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + for (int i = startIndex; i <= sb.Length - value.Length; i++) + { + bool match = true; + for (int j = 0; j < value.Length; j++) + { + if (char.ToLower(sb[i + j]) != char.ToLower(value[j])) + { + match = false; + break; + } + } + + if (match) + return i; + } + + return -1; + } + + /// + /// 替换字符串 + /// + public static StringBuilder Replace(this StringBuilder? sb, string oldValue, string? newValue, bool ignoreCase = false) + { + if (sb == null || string.IsNullOrEmpty(oldValue)) + return sb ?? throw new ArgumentNullException(nameof(sb)); + + int index; + int searchIndex = 0; + + while ((index = sb.IndexOf(oldValue, searchIndex, ignoreCase)) >= 0) + { + sb.Remove(index, oldValue.Length); + sb.Insert(index, newValue); + searchIndex = index + (newValue?.Length ?? 0); + } + + return sb; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/StringBuilderPool.cs b/EasyTool.Core/TextCategory/StringBuilderPool.cs new file mode 100644 index 0000000..c9dfebf --- /dev/null +++ b/EasyTool.Core/TextCategory/StringBuilderPool.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 字符串构建器池 + /// + public class StringBuilderPool + { + private readonly Stack _pool; + private readonly int _maxCapacity; + private readonly int _defaultCapacity; + private readonly object _lock = new(); + + /// + /// 默认实例 + /// + public static StringBuilderPool Default { get; } = new(); + + /// + /// 池中可用数量 + /// + public int AvailableCount + { + get + { + lock (_lock) + { + return _pool.Count; + } + } + } + + /// + /// 创建字符串构建器池 + /// + /// 最大池容量 + /// 默认StringBuilder容量 + /// 预分配数量 + public StringBuilderPool(int maxCapacity = 50, int defaultCapacity = 256, int preallocate = 5) + { + _maxCapacity = maxCapacity; + _defaultCapacity = defaultCapacity; + _pool = new Stack(maxCapacity); + + for (int i = 0; i < preallocate && i < maxCapacity; i++) + { + _pool.Push(new StringBuilder(defaultCapacity)); + } + } + + /// + /// 获取StringBuilder + /// + public StringBuilder Get() + { + lock (_lock) + { + if (_pool.Count > 0) + { + return _pool.Pop(); + } + } + return new StringBuilder(_defaultCapacity); + } + + /// + /// 归还StringBuilder + /// + public void Return(StringBuilder sb) + { + if (sb == null) return; + + // 清空内容 + sb.Clear(); + + // 如果容量过大,不归还 + if (sb.Capacity > _defaultCapacity * 4) + return; + + lock (_lock) + { + if (_pool.Count < _maxCapacity) + { + _pool.Push(sb); + } + } + } + + /// + /// 使用StringBuilder执行操作 + /// + public string Execute(Action action) + { + var sb = Get(); + try + { + action(sb); + return sb.ToString(); + } + finally + { + Return(sb); + } + } + + /// + /// 使用StringBuilder执行操作并返回结果 + /// + public TResult Execute(Func func) + { + var sb = Get(); + try + { + return func(sb); + } + finally + { + Return(sb); + } + } + + /// + /// 连接字符串 + /// + public static string Concat(IEnumerable values, string separator = "") + { + return Default.Execute(sb => + { + var first = true; + foreach (var value in values) + { + if (!first && !string.IsNullOrEmpty(separator)) + sb.Append(separator); + sb.Append(value); + first = false; + } + }); + } + + /// + /// 连接字符串 + /// + public static string Concat(IEnumerable values, string separator = "", Func? selector = null) + { + return Default.Execute(sb => + { + var first = true; + foreach (var value in values) + { + if (!first && !string.IsNullOrEmpty(separator)) + sb.Append(separator); + sb.Append(selector != null ? selector(value) : value?.ToString()); + first = false; + } + }); + } + + /// + /// 清空池 + /// + public void Clear() + { + lock (_lock) + { + _pool.Clear(); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/TextCategory/StringComparisonExtension.cs b/EasyTool.Core/TextCategory/StringComparisonExtension.cs new file mode 100644 index 0000000..3ae4f68 --- /dev/null +++ b/EasyTool.Core/TextCategory/StringComparisonExtension.cs @@ -0,0 +1,378 @@ +using System; +using System.Globalization; + +namespace EasyTool.TextCategory +{ + /// + /// String 字符串比较扩展方法 + /// + public static class StringComparisonExtension + { + #region 忽略大小写比较 + + /// + /// 忽略大小写判断字符串相等 + /// + public static bool EqualsIgnoreCase(this string? str, string? value) + { + return string.Equals(str, value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 忽略大小写判断字符串包含 + /// + public static bool ContainsIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return CultureInfo.InvariantCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase) >= 0; + } + + /// + /// 忽略大小写判断字符串是否以指定字符串开头 + /// + public static bool StartsWithIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return str.StartsWith(value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 忽略大小写判断字符串是否以指定字符串结尾 + /// + public static bool EndsWithIgnoreCase(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return false; + + return str.EndsWith(value, StringComparison.OrdinalIgnoreCase); + } + + #endregion + + #region 模糊匹配 + + /// + /// 判断字符串是否包含指定字符(忽略大小写) + /// + public static bool ContainsCharIgnoreCase(this string? str, char value) + { + if (string.IsNullOrEmpty(str)) + return false; + + return str.IndexOf(char.ToLower(value), StringComparison.OrdinalIgnoreCase) >= 0; + } + + /// + /// 判断字符串是否包含任意指定字符串 + /// + public static bool ContainsAny(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (str.Contains(value)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否包含任意指定字符串 + /// + public static bool ContainsAnyIgnoreCase(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (str.ContainsIgnoreCase(value)) + return true; + } + + return false; + } + + /// + /// 判断字符串是否包含所有指定字符串 + /// + public static bool ContainsAll(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (!str.Contains(value)) + return false; + } + + return true; + } + + /// + /// 忽略大小写判断字符串是否包含所有指定字符串 + /// + public static bool ContainsAllIgnoreCase(this string? str, params string?[] values) + { + if (string.IsNullOrEmpty(str) || values == null || values.Length == 0) + return false; + + foreach (var value in values) + { + if (!str.ContainsIgnoreCase(value)) + return false; + } + + return true; + } + + #endregion + + #region 通配符匹配 + + /// + /// 使用通配符匹配字符串 + /// + public static bool Like(this string? str, string? pattern, bool ignoreCase = true) + { + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(pattern)) + return false; + + var comparisonType = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + // 转换通配符模式为正则表达式 + var regexPattern = "^" + System.Text.RegularExpressions.Regex.Escape(pattern) + .Replace("\\*", ".*") + .Replace("\\?", ".") + "$"; + + var regex = new System.Text.RegularExpressions.Regex( + regexPattern, + ignoreCase ? System.Text.RegularExpressions.RegexOptions.IgnoreCase : System.Text.RegularExpressions.RegexOptions.None); + + return regex.IsMatch(str); + } + + #endregion + + #region 前缀/后缀检查 + + /// + /// 判断字符串是否以任意指定前缀开头 + /// + public static bool StartsWithAny(this string? str, params string?[] prefixes) + { + if (string.IsNullOrEmpty(str) || prefixes == null || prefixes.Length == 0) + return false; + + foreach (var prefix in prefixes) + { + if (!string.IsNullOrEmpty(prefix) && str.StartsWith(prefix)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否以任意指定前缀开头 + /// + public static bool StartsWithAnyIgnoreCase(this string? str, params string?[] prefixes) + { + if (string.IsNullOrEmpty(str) || prefixes == null || prefixes.Length == 0) + return false; + + foreach (var prefix in prefixes) + { + if (!string.IsNullOrEmpty(prefix) && str.StartsWithIgnoreCase(prefix)) + return true; + } + + return false; + } + + /// + /// 判断字符串是否以任意指定后缀结尾 + /// + public static bool EndsWithAny(this string? str, params string?[] suffixes) + { + if (string.IsNullOrEmpty(str) || suffixes == null || suffixes.Length == 0) + return false; + + foreach (var suffix in suffixes) + { + if (!string.IsNullOrEmpty(suffix) && str.EndsWith(suffix)) + return true; + } + + return false; + } + + /// + /// 忽略大小写判断字符串是否以任意指定后缀结尾 + /// + public static bool EndsWithAnyIgnoreCase(this string? str, params string?[] suffixes) + { + if (string.IsNullOrEmpty(str) || suffixes == null || suffixes.Length == 0) + return false; + + foreach (var suffix in suffixes) + { + if (!string.IsNullOrEmpty(suffix) && str.EndsWithIgnoreCase(suffix)) + return true; + } + + return false; + } + + #endregion + + #region 字符串相似度 + + /// + /// 计算字符串相似度(0-1之间,1表示完全相同) + /// 使用 Levenshtein 距离算法 + /// + public static double Similarity(this string? str, string? value) + { + if (string.IsNullOrEmpty(str) && string.IsNullOrEmpty(value)) + return 1; + + if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(value)) + return 0; + + int distance = LevenshteinDistance(str, value); + int maxLength = Math.Max(str.Length, value.Length); + + return 1 - (double)distance / maxLength; + } + + /// + /// 计算 Levenshtein 距离 + /// + private static int LevenshteinDistance(string str1, string str2) + { + int[,] distance = new int[str1.Length + 1, str2.Length + 1]; + + for (int i = 0; i <= str1.Length; i++) + distance[i, 0] = i; + + for (int j = 0; j <= str2.Length; j++) + distance[0, j] = j; + + for (int i = 1; i <= str1.Length; i++) + { + for (int j = 1; j <= str2.Length; j++) + { + int cost = str1[i - 1] == str2[j - 1] ? 0 : 1; + + distance[i, j] = Math.Min( + Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), + distance[i - 1, j - 1] + cost); + } + } + + return distance[str1.Length, str2.Length]; + } + + /// + /// 判断字符串相似度是否超过指定阈值 + /// + public static bool IsSimilarTo(this string? str, string? value, double threshold = 0.8) + { + return str.Similarity(value) >= threshold; + } + + #endregion + + #region 字符串比较 + + /// + /// 比较字符串(忽略大小写) + /// + public static int CompareToIgnoreCase(this string? str, string? value) + { + return string.Compare(str, value, StringComparison.OrdinalIgnoreCase); + } + + /// + /// 比较字符串(使用指定的文化信息) + /// + public static int CompareToCulture(this string? str, string? value, CultureInfo culture, bool ignoreCase = false) + { + return string.Compare(str, value, culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); + } + + #endregion + + #region 首字母大小写 + + /// + /// 首字母大写 + /// + public static string ToTitleCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return char.ToUpper(str[0]) + str.Substring(1); + } + + /// + /// 首字母小写 + /// + public static string ToCamelCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + return char.ToLower(str[0]) + str.Substring(1); + } + + /// + /// 将字符串转换为标题格式(每个单词首字母大写) + /// + public static string ToTitleCase(this string str, CultureInfo? culture = null) + { + if (string.IsNullOrEmpty(str)) + return str; + + culture ??= CultureInfo.CurrentCulture; + return culture.TextInfo.ToTitleCase(str); + } + + #endregion + + #region 大小写转换 + + + /// + /// 转换为单词首字母大写(如:helloWorld -> HelloWorld) + /// + public static string ToPascalCase(this string str) + { + if (string.IsNullOrEmpty(str)) + return str; + + var words = str.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < words.Length; i++) + { + if (words[i].Length > 0) + { + words[i] = char.ToUpper(words[i][0]) + words[i].Substring(1).ToLower(); + } + } + + return string.Join("", words); + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/StringExtension.cs b/EasyTool.Core/TextCategory/StringExtension.cs new file mode 100644 index 0000000..4592ec0 --- /dev/null +++ b/EasyTool.Core/TextCategory/StringExtension.cs @@ -0,0 +1,289 @@ +using System; +using System.Globalization; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.TextCategory +{ + /// + /// 字符串扩展方法 + /// + public static class StrExtension + { + #region 文本可为空判断 + #endregion + + #region 字符串验证 + + /// + /// 判断字符串是否是有效的电子邮件地址 + /// + public static bool IsEmail(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的手机号(中国大陆) + /// + public static bool IsPhoneNumber(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + // 中国大陆手机号:1开头,11位数字 + const string pattern = @"^1[3-9]\d{9}$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的 URL + /// + public static bool IsUrl(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^(https?|ftp)://[^\s/$.?#].[^\s]*$"; + return Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase); + } + + /// + /// 判断字符串是否是有效的 IPv4 地址 + /// + public static bool IsIPv4(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + const string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; + return Regex.IsMatch(value, pattern); + } + + /// + /// 判断字符串是否是有效的身份证号(中国大陆) + /// + public static bool IsIdCard(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + // 18位身份证号码 + const string pattern = @"^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$"; + return Regex.IsMatch(value, pattern); + } + + #endregion + + #region 字符串转换 + + /// + /// 将字符串转换为 Base64 编码 + /// + public static string ToBase64(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Encoding.UTF8.GetBytes(value); + return Convert.ToBase64String(bytes); + } + + /// + /// 将 Base64 编码的字符串解码 + /// + public static string FromBase64(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Convert.FromBase64String(value); + return Encoding.UTF8.GetString(bytes); + } + + /// + /// 计算字符串的 MD5 哈希值 + /// + public static string ToMd5(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + using var md5 = System.Security.Cryptography.MD5.Create(); + var bytes = Encoding.UTF8.GetBytes(value); + var hash = md5.ComputeHash(bytes); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 计算字符串的 SHA256 哈希值 + /// + public static string ToSha256(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + using var sha256 = System.Security.Cryptography.SHA256.Create(); + var bytes = Encoding.UTF8.GetBytes(value); + var hash = sha256.ComputeHash(bytes); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + + /// + /// 将字符串转换为16进制表示 + /// + public static string ToHex(this string value) + { + if (string.IsNullOrEmpty(value)) + return string.Empty; + + var bytes = Encoding.UTF8.GetBytes(value); + return BitConverter.ToString(bytes).Replace("-", "").ToLowerInvariant(); + } + + #endregion + + #region 字符串处理 + + /// + /// 截断字符串到指定长度,超出部分用省略号代替 + /// + /// 原始字符串 + /// 最大长度 + /// 后缀,默认为"..." + public static string Truncate(this string value, int maxLength, string suffix = "...") + { + if (string.IsNullOrEmpty(value) || value.Length <= maxLength) + return value; + + return value.Substring(0, maxLength) + suffix; + } + + /// + /// 移除字符串中的音调符号(如 é -> e) + /// + public static string RemoveDiacritics(this string value) + { + if (string.IsNullOrEmpty(value)) + return value; + + var normalizedString = value.Normalize(NormalizationForm.FormD); + var sb = new StringBuilder(); + + foreach (var c in normalizedString) + { + var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); + if (unicodeCategory != UnicodeCategory.NonSpacingMark) + { + sb.Append(c); + } + } + + return sb.ToString().Normalize(NormalizationForm.FormC); + } + + /// + /// 生成 URL 友好的 slug(例如:"Hello World" -> "hello-world") + /// + public static string GenerateSlug(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return string.Empty; + + // 移除音调符号 + var slug = value.RemoveDiacritics(); + + // 转换为小写 + slug = slug.ToLowerInvariant(); + + // 替换空格和特殊字符为连字符 + slug = Regex.Replace(slug, @"[^a-z0-9\s-]", ""); + slug = Regex.Replace(slug, @"\s+", "-"); + slug = Regex.Replace(slug, @"-+", "-"); + slug = slug.Trim('-'); + + return slug; + } + + + /// + /// 隐藏字符串的中间部分(例如:手机号、身份证号) + /// + /// 原始字符串 + /// 开头保留字符数 + /// 结尾保留字符数 + /// 掩码字符,默认为'*' + public static string Mask(this string value, int visibleStart = 3, int visibleEnd = 4, char maskChar = '*') + { + if (string.IsNullOrEmpty(value)) + return value; + + if (value.Length <= visibleStart + visibleEnd) + return value; + + var start = value.Substring(0, visibleStart); + var end = value.Substring(value.Length - visibleEnd); + var maskLength = value.Length - visibleStart - visibleEnd; + var mask = new string(maskChar, maskLength); + + return start + mask + end; + } + + #endregion + + #region 字符串操作 + + /// + /// 移除字符串中指定的字符 + /// + public static string RemoveChars(this string value, params char[] charsToRemove) + { + if (string.IsNullOrEmpty(value) || charsToRemove == null || charsToRemove.Length == 0) + return value; + + var result = new StringBuilder(value.Length); + foreach (var c in value) + { + if (Array.IndexOf(charsToRemove, c) < 0) + { + result.Append(c); + } + } + return result.ToString(); + } + + /// + /// 确保字符串以指定后缀结尾 + /// + public static string EnsureEndsWith(this string value, string suffix) + { + if (string.IsNullOrEmpty(value)) + return suffix ?? string.Empty; + + if (string.IsNullOrEmpty(suffix)) + return value; + + return value.EndsWith(suffix) ? value : value + suffix; + } + + /// + /// 确保字符串以指定前缀开头 + /// + public static string EnsureStartsWith(this string value, string prefix) + { + if (string.IsNullOrEmpty(value)) + return prefix ?? string.Empty; + + if (string.IsNullOrEmpty(prefix)) + return value; + + return value.StartsWith(prefix) ? value : prefix + value; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/TemplateUtil.cs b/EasyTool.Core/TextCategory/TemplateUtil.cs new file mode 100644 index 0000000..83f3d3b --- /dev/null +++ b/EasyTool.Core/TextCategory/TemplateUtil.cs @@ -0,0 +1,282 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace EasyTool.TextCategory +{ + /// + /// 模板工具类 + /// + public static class TemplateUtil + { + /// + /// 渲染模板(使用 ${var} 语法) + /// + public static string Render(string template, IDictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(template); + var start = 0; + + while ((start = result.ToString().IndexOf("${", start)) >= 0) + { + var end = result.ToString().IndexOf("}", start); + if (end < 0) break; + + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + if (variables.TryGetValue(varName, out var value)) + { + result.Remove(start, end - start + 1); + result.Insert(start, value?.ToString() ?? ""); + } + else + { + start = end + 1; + } + } + + return result.ToString(); + } + + /// + /// 渲染模板(使用匿名对象) + /// + public static string Render(string template, object model) + { + if (string.IsNullOrEmpty(template)) + return template; + + var dict = new Dictionary(); + foreach (var prop in model.GetType().GetProperties()) + { + dict[prop.Name] = prop.GetValue(model); + } + + return Render(template, dict); + } + + /// + /// 渲染模板(使用 {{var}} 语法) + /// + public static string RenderMustache(string template, IDictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(template); + var start = 0; + + while ((start = result.ToString().IndexOf("{{", start)) >= 0) + { + var end = result.ToString().IndexOf("}}", start); + if (end < 0) break; + + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + if (variables.TryGetValue(varName, out var value)) + { + result.Remove(start, end - start + 2); + result.Insert(start, value?.ToString() ?? ""); + } + else + { + start = end + 2; + } + } + + return result.ToString(); + } + + /// + /// 渲染模板(带默认值) + /// + public static string Render(string template, IDictionary variables, string defaultValue = "") + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(template); + var start = 0; + + while ((start = result.ToString().IndexOf("${", start)) >= 0) + { + var end = result.ToString().IndexOf("}", start); + if (end < 0) break; + + var varName = result.ToString().Substring(start + 2, end - start - 2).Trim(); + + // 检查是否有默认值 (var:default) + string? defaultValueLocal = null; + var colonIndex = varName.IndexOf(':'); + if (colonIndex > 0) + { + defaultValueLocal = varName.Substring(colonIndex + 1); + varName = varName.Substring(0, colonIndex); + } + + object? value; + if (variables.TryGetValue(varName, out value)) + { + result.Remove(start, end - start + 1); + result.Insert(start, value?.ToString() ?? defaultValueLocal ?? defaultValue); + } + else + { + result.Remove(start, end - start + 1); + result.Insert(start, defaultValueLocal ?? defaultValue); + } + } + + return result.ToString(); + } + + /// + /// 提取模板中的变量名 + /// + public static List ExtractVariables(string template, string startTag = "${", string endTag = "}") + { + var result = new List(); + if (string.IsNullOrEmpty(template)) + return result; + + var start = 0; + while ((start = template.IndexOf(startTag, start)) >= 0) + { + var end = template.IndexOf(endTag, start + startTag.Length); + if (end < 0) break; + + var varName = template.Substring(start + startTag.Length, end - start - startTag.Length).Trim(); + if (!string.IsNullOrEmpty(varName)) + { + // 移除默认值部分 + var colonIndex = varName.IndexOf(':'); + if (colonIndex > 0) + varName = varName.Substring(0, colonIndex); + + if (!result.Contains(varName)) + result.Add(varName); + } + + start = end + endTag.Length; + } + + return result; + } + + /// + /// 验证模板是否有未替换的变量 + /// + public static bool HasUnresolvedVariables(string template, string startTag = "${", string endTag = "}") + { + return template.Contains(startTag) && template.Contains(endTag); + } + + /// + /// 格式化字符串(类似Python的f-string) + /// + public static string Format(string template, params object[] args) + { + if (string.IsNullOrEmpty(template) || args == null || args.Length == 0) + return template; + + var result = new StringBuilder(template); + for (int i = 0; i < args.Length; i++) + { + result.Replace($"{{{i}}}", args[i]?.ToString() ?? ""); + } + + return result.ToString(); + } + + /// + /// 条件渲染 + /// + public static string RenderConditional(string template, IDictionary variables) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(template); + + // 处理 {?condition}...{?} 条件块 + var start = 0; + while ((start = result.ToString().IndexOf("{?", start)) >= 0) + { + var endCondition = result.ToString().IndexOf("}", start); + if (endCondition < 0) break; + + var condition = result.ToString().Substring(start + 2, endCondition - start - 2).Trim(); + var endBlock = result.ToString().IndexOf("{?}", endCondition); + if (endBlock < 0) break; + + var content = result.ToString().Substring(endCondition + 1, endBlock - endCondition - 1); + + bool shouldInclude = false; + if (variables.TryGetValue(condition, out var value)) + { + shouldInclude = value is bool b ? b : value != null; + } + + result.Remove(start, endBlock - start + 3); + if (shouldInclude) + { + result.Insert(start, content); + } + } + + // 渲染变量 + return Render(result.ToString(), variables); + } + + /// + /// 循环渲染 + /// + public static string RenderLoop(string template, string varName, IEnumerable items) + { + if (string.IsNullOrEmpty(template)) + return template; + + var result = new StringBuilder(); + + // 找到循环块 {#var}...{/var} + var startTag = $"{{#{varName}}}"; + var endTag = $"{{/{varName}}}"; + + var start = template.IndexOf(startTag); + if (start < 0) return template; + + var end = template.IndexOf(endTag, start); + if (end < 0) return template; + + var prefix = template.Substring(0, start); + var loopTemplate = template.Substring(start + startTag.Length, end - start - startTag.Length); + var suffix = template.Substring(end + endTag.Length); + + result.Append(prefix); + + foreach (var item in items) + { + var dict = new Dictionary + { + [varName] = item + }; + + // 如果item是匿名对象,展开其属性 + if (item != null) + { + foreach (var prop in item.GetType().GetProperties()) + { + dict[$"{varName}.{prop.Name}"] = prop.GetValue(item); + } + } + + result.Append(Render(loopTemplate, dict)); + } + + result.Append(suffix); + return result.ToString(); + } + } +} diff --git a/EasyTool.Core/TextCategory/TextSimilarityUtil.cs b/EasyTool.Core/TextCategory/TextSimilarityUtil.cs new file mode 100644 index 0000000..0ab2992 --- /dev/null +++ b/EasyTool.Core/TextCategory/TextSimilarityUtil.cs @@ -0,0 +1,413 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.TextCategory +{ + /// + /// 文本相似度算法 + /// + public enum SimilarityAlgorithm + { + /// + /// Levenshtein 编辑距离 + /// + Levenshtein, + + /// + /// Jaccard 相似度 + /// + Jaccard, + + /// + /// Cosine 余弦相似度 + /// + Cosine, + + /// + /// Dice 系数 + /// + Dice, + + /// + /// Jaro-Winkler 相似度 + /// + JaroWinkler, + + /// + /// Hamming 距离 + /// + Hamming + } + + /// + /// 文本相似度工具类 + /// 提供多种文本相似度计算算法 + /// + public static class TextSimilarityUtil + { + /// + /// 计算文本相似度 + /// + /// 文本1 + /// 文本2 + /// 算法 + /// 相似度(0-1) + public static double Calculate(string text1, string text2, SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return algorithm switch + { + SimilarityAlgorithm.Levenshtein => LevenshteinSimilarity(text1, text2), + SimilarityAlgorithm.Jaccard => JaccardSimilarity(text1, text2), + SimilarityAlgorithm.Cosine => CosineSimilarity(text1, text2), + SimilarityAlgorithm.Dice => DiceSimilarity(text1, text2), + SimilarityAlgorithm.JaroWinkler => JaroWinklerSimilarity(text1, text2), + SimilarityAlgorithm.Hamming => HammingSimilarity(text1, text2), + _ => throw new ArgumentException($"不支持的算法: {algorithm}") + }; + } + + /// + /// 计算编辑距离 + /// + /// 文本1 + /// 文本2 + /// 编辑距离 + public static int LevenshteinDistance(string text1, string text2) + { + if (string.IsNullOrEmpty(text1)) + return text2?.Length ?? 0; + + if (string.IsNullOrEmpty(text2)) + return text1.Length; + + var matrix = new int[text1.Length + 1, text2.Length + 1]; + + for (int i = 0; i <= text1.Length; i++) + matrix[i, 0] = i; + + for (int j = 0; j <= text2.Length; j++) + matrix[0, j] = j; + + for (int i = 1; i <= text1.Length; i++) + { + for (int j = 1; j <= text2.Length; j++) + { + var cost = text1[i - 1] == text2[j - 1] ? 0 : 1; + + matrix[i, j] = Math.Min( + Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1), + matrix[i - 1, j - 1] + cost); + } + } + + return matrix[text1.Length, text2.Length]; + } + + /// + /// Levenshtein 相似度 + /// + public static double LevenshteinSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var distance = LevenshteinDistance(text1, text2); + var maxLength = Math.Max(text1.Length, text2.Length); + + return 1.0 - (double)distance / maxLength; + } + + /// + /// Jaccard 相似度 + /// + public static double JaccardSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var set1 = GetNgrams(text1, 2); + var set2 = GetNgrams(text2, 2); + + var intersection = set1.Intersect(set2).Count(); + var union = set1.Union(set2).Count(); + + return union == 0 ? 0.0 : (double)intersection / union; + } + + /// + /// Cosine 余弦相似度 + /// + public static double CosineSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var vector1 = GetTermFrequency(text1); + var vector2 = GetTermFrequency(text2); + + var allTerms = vector1.Keys.Union(vector2.Keys).ToList(); + + double dotProduct = 0; + double magnitude1 = 0; + double magnitude2 = 0; + + foreach (var term in allTerms) + { + var v1 = vector1.TryGetValue(term, out var val1) ? val1 : 0; + var v2 = vector2.TryGetValue(term, out var val2) ? val2 : 0; + + dotProduct += v1 * v2; + magnitude1 += v1 * v1; + magnitude2 += v2 * v2; + } + + magnitude1 = Math.Sqrt(magnitude1); + magnitude2 = Math.Sqrt(magnitude2); + + if (magnitude1 == 0 || magnitude2 == 0) + return 0.0; + + return dotProduct / (magnitude1 * magnitude2); + } + + /// + /// Dice 系数 + /// + public static double DiceSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + var set1 = GetNgrams(text1, 2); + var set2 = GetNgrams(text2, 2); + + var intersection = set1.Intersect(set2).Count(); + + return (2.0 * intersection) / (set1.Count + set2.Count); + } + + /// + /// Jaro-Winkler 相似度 + /// + public static double JaroWinklerSimilarity(string text1, string text2) + { + var jaroSimilarity = JaroSimilarity(text1, text2); + + // 计算 common prefix 长度(最多4个字符) + var prefixLength = 0; + var minLength = Math.Min(Math.Min(text1.Length, text2.Length), 4); + + for (int i = 0; i < minLength; i++) + { + if (text1[i] == text2[i]) + prefixLength++; + else + break; + } + + // Winkler 修正 + return jaroSimilarity + (prefixLength * 0.1 * (1 - jaroSimilarity)); + } + + /// + /// Jaro 相似度 + /// + public static double JaroSimilarity(string text1, string text2) + { + if (string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2)) + return 1.0; + + if (string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text2)) + return 0.0; + + if (text1 == text2) + return 1.0; + + var matchDistance = Math.Max(text1.Length, text2.Length) / 2 - 1; + var matches1 = new bool[text1.Length]; + var matches2 = new bool[text2.Length]; + + var matches = 0; + var transpositions = 0; + + // 查找匹配字符 + for (int i = 0; i < text1.Length; i++) + { + var start = Math.Max(0, i - matchDistance); + var end = Math.Min(i + matchDistance + 1, text2.Length); + + for (int j = start; j < end; j++) + { + if (matches2[j] || text1[i] != text2[j]) + continue; + + matches1[i] = true; + matches2[j] = true; + matches++; + break; + } + } + + if (matches == 0) + return 0.0; + + // 计算转置次数 + var k = 0; + for (int i = 0; i < text1.Length; i++) + { + if (!matches1[i]) + continue; + + while (!matches2[k]) + k++; + + if (text1[i] != text2[k]) + transpositions++; + + k++; + } + + return ((double)matches / text1.Length + + (double)matches / text2.Length + + (matches - transpositions / 2.0) / matches) / 3.0; + } + + /// + /// Hamming 距离(仅适用于等长字符串) + /// + public static int HammingDistance(string text1, string text2) + { + if (text1.Length != text2.Length) + throw new ArgumentException("Hamming 距离要求两个字符串长度相等"); + + return text1.Zip(text2, (c1, c2) => c1 != c2 ? 1 : 0).Sum(); + } + + /// + /// Hamming 相似度 + /// + public static double HammingSimilarity(string text1, string text2) + { + if (text1.Length != text2.Length) + return 0.0; + + if (text1.Length == 0) + return 1.0; + + var distance = HammingDistance(text1, text2); + return 1.0 - (double)distance / text1.Length; + } + + /// + /// 查找最相似的文本 + /// + /// 查询文本 + /// 候选文本列表 + /// 算法 + /// 返回前N个 + /// 相似度排序结果 + public static List<(string Text, double Similarity)> FindMostSimilar( + string query, + IEnumerable candidates, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein, + int topN = 5) + { + return candidates + .Select(c => (Text: c, Similarity: Calculate(query, c, algorithm))) + .OrderByDescending(r => r.Similarity) + .Take(topN) + .ToList(); + } + + /// + /// 检查是否相似(超过阈值) + /// + /// 文本1 + /// 文本2 + /// 阈值(0-1) + /// 算法 + /// 是否相似 + public static bool IsSimilar( + string text1, + string text2, + double threshold = 0.8, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return Calculate(text1, text2, algorithm) >= threshold; + } + + /// + /// 模糊搜索 + /// + /// 查询文本 + /// 候选文本列表 + /// 阈值 + /// 算法 + /// 匹配结果 + public static List FuzzySearch( + string query, + IEnumerable candidates, + double threshold = 0.6, + SimilarityAlgorithm algorithm = SimilarityAlgorithm.Levenshtein) + { + return candidates + .Where(c => Calculate(query, c, algorithm) >= threshold) + .ToList(); + } + + #region 私有方法 + + private static HashSet GetNgrams(string text, int n) + { + var ngrams = new HashSet(); + + if (string.IsNullOrEmpty(text) || text.Length < n) + { + ngrams.Add(text ?? ""); + return ngrams; + } + + for (int i = 0; i <= text.Length - n; i++) + { + ngrams.Add(text.Substring(i, n)); + } + + return ngrams; + } + + private static Dictionary GetTermFrequency(string text) + { + var frequency = new Dictionary(); + + if (string.IsNullOrEmpty(text)) + return frequency; + + // 按字符分词 + foreach (var c in text) + { + var term = c.ToString(); + if (frequency.ContainsKey(term)) + frequency[term]++; + else + frequency[term] = 1; + } + + return frequency; + } + + #endregion + } +} diff --git a/EasyTool.Core/TextCategory/UnicodeUtil.cs b/EasyTool.Core/TextCategory/UnicodeUtil.cs deleted file mode 100644 index de3d562..0000000 --- a/EasyTool.Core/TextCategory/UnicodeUtil.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - public class UnicodeUtil - { - - /// - /// 将Unicode编码的字符串转换为普通字符串。 - /// - /// Unicode编码的字符串 - /// 普通字符串 - public static string UnicodeToString(string unicodeStr) - { - StringBuilder sb = new StringBuilder(); - - int len = unicodeStr.Length; - for (int i = 0; i < len; i++) - { - if (unicodeStr[i] == '\\' && (i + 1) < len && unicodeStr[i + 1] == 'u') - { - string hexStr = unicodeStr.Substring(i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else - { - sb.Append(unicodeStr[i]); - } - } - - return sb.ToString(); - } - - /// - /// 将普通字符串转换为Unicode编码的字符串。 - /// - /// 普通字符串 - /// Unicode编码的字符串 - public static string StringToUnicode(string str) - { - StringBuilder sb = new StringBuilder(); - - foreach (char c in str) - { - sb.Append("\\u"); - sb.Append(((int)c).ToString("x4")); - } - - return sb.ToString(); - } - - /// - /// 将Unicode字符转换为普通字符。 - /// - /// Unicode字符 - /// 普通字符 - public static char UnicodeToChar(string unicodeChar) - { - int code = Convert.ToInt32(unicodeChar, 16); - return (char)code; - } - - /// - /// 将普通字符转换为Unicode字符。 - /// - /// 普通字符 - /// Unicode字符 - public static string CharToUnicode(char c) - { - return "\\u" + ((int)c).ToString("x4"); - } - - /// - /// 将Unicode编码的字符数组转换为普通字符串。 - /// - /// Unicode编码的字符数组 - /// 普通字符串 - public static string UnicodeCharsToString(char[] unicodeChars) - { - StringBuilder sb = new StringBuilder(); - - int len = unicodeChars.Length; - for (int i = 0; i < len; i++) - { - if (i + 1 < len && unicodeChars[i] == '\\' && unicodeChars[i + 1] == 'u') - { - string hexStr = new string(unicodeChars, i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else - { - sb.Append(unicodeChars[i]); - } - } - - return sb.ToString(); - } - - /// - /// 将普通字符串转换为Unicode编码的字符数组。 - /// - /// 普通字符串 - /// Unicode编码的字符数组 - public static char[] StringToUnicodeChars(string str) - { - List chars = new List(); - - foreach (char c in str) - { - chars.AddRange(CharToUnicode(c).ToCharArray()); - } - - return chars.ToArray(); - } - - /// - /// 将Unicode编码的字符数组转换为普通字符串数组。 - /// - /// Unicode编码的字符数组 - /// 普通字符串数组 - public static string[] UnicodeCharsToStringArray(char[] unicodeChars) - { - List strs = new List(); - - StringBuilder sb = new StringBuilder(); - - int len = unicodeChars.Length; - for (int i = 0; i < len; i++) - { - if (i + 1 < len && unicodeChars[i] == '\\' && unicodeChars[i + 1] == 'u') - { - string hexStr = new string(unicodeChars, i + 2, 4); - int code = Convert.ToInt32(hexStr, 16); - sb.Append((char)code); - i += 5; - } - else if (unicodeChars[i] == '\0') - { - strs.Add(sb.ToString()); - sb.Clear(); - } - else - { - sb.Append(unicodeChars[i]); - } - } - - if (sb.Length > 0) - { - strs.Add(sb.ToString()); - } - - return strs.ToArray(); - } - - /// - /// 将普通字符串数组转换为Unicode编码的字符数组。 - /// - /// 普通字符串数组 - /// Unicode编码的字符数组 - public static char[] StringArrayToUnicodeChars(string[] strs) - { - List chars = new List(); - - foreach (string str in strs) - { - chars.AddRange(StringToUnicodeChars(str)); - chars.Add('\0'); - } - - return chars.ToArray(); - } - } -} diff --git a/EasyTool.Core/ToolCategory/XmlUtil.cs b/EasyTool.Core/TextCategory/XmlUtil.cs similarity index 99% rename from EasyTool.Core/ToolCategory/XmlUtil.cs rename to EasyTool.Core/TextCategory/XmlUtil.cs index 0db77ed..c50ed04 100644 --- a/EasyTool.Core/ToolCategory/XmlUtil.cs +++ b/EasyTool.Core/TextCategory/XmlUtil.cs @@ -1,14 +1,14 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Xml; -namespace EasyTool +namespace EasyTool.TextCategory { /// /// XML工具类 /// - public class XmlUtil + public static class XmlUtil { /// /// 解析XML字符串。 @@ -52,7 +52,7 @@ public static XmlDocument CreateNewXmlDocument() /// 元素的名称。 /// 元素的值。 /// 新创建的XML元素。 - public static XmlElement CreateXmlElement(string name, string value = null) + public static XmlElement CreateXmlElement(string name, string? value = null) { var document = new XmlDocument(); var element = document.CreateElement(name); diff --git a/EasyTool.Core/ToolCategory/ArrayUtil.cs b/EasyTool.Core/ToolCategory/ArrayUtil.cs deleted file mode 100644 index ca24653..0000000 --- a/EasyTool.Core/ToolCategory/ArrayUtil.cs +++ /dev/null @@ -1,243 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// 数组工具类 - /// - public class ArrayUtil - { - /// - /// 判断数组是否为空 - /// - /// 要判断的数组 - /// 如果数组为空,则返回 true;否则返回 false - public static bool IsEmpty(Array array) - { - return array == null || array.Length == 0; - } - - /// - /// 获取数组的长度 - /// - /// 要获取长度的数组 - /// 返回数组的长度 - public static int Length(Array array) - { - if (array == null) - { - return 0; - } - - return array.Length; - } - - /// - /// 获取数组中的最大值 - /// - /// 要获取最大值的数组 - /// 返回数组中的最大值 - public static T Max(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T max = array[0]; - for (int i = 1; i < array.Length; i++) - { - if (array[i].CompareTo(max) > 0) - { - max = array[i]; - } - } - - return max; - } - - /// - /// 获取数组中的最小值 - /// - /// 要获取最小值的数组 - /// 返回数组中的最小值 - public static T Min(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T min = array[0]; - for (int i = 1; i < array.Length; i++) - { - if (array[i].CompareTo(min) < 0) - { - min = array[i]; - } - } - return min; - } - - /// - /// 获取数组中的和 - /// - /// 要获取和的数组 - /// 返回数组的和 - public static int Sum(int[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - int sum = 0; - for (int i = 0; i < array.Length; i++) - { - sum += array[i]; - } - - return sum; - } - - /// - /// 获取数组的平均值 - /// - /// 要获取平均值的数组 - /// 返回数组的平均值 - public static double Average(int[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - int sum = Sum(array); - int length = Length(array); - - return (double)sum / length; - } - - /// - /// 数组排序 - /// - /// 要排序的数组 - /// 返回排序后的数组 - public static T[] Sort(T[] array) where T : IComparable - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T[] sortedArray = new T[array.Length]; - array.CopyTo(sortedArray, 0); - Array.Sort(sortedArray); - - return sortedArray; - } - - /// - /// 数组反转 - /// - /// 要反转的数组 - /// 返回反转后的数组 - public static T[] Reverse(T[] array) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - - T[] reversedArray = new T[array.Length]; - array.CopyTo(reversedArray, 0); - Array.Reverse(reversedArray); - - return reversedArray; - } - - /// - /// 判断数组是否包含某个元素 - /// - /// 要操作的数组 - /// 要判断的元素 - /// 如果数组中包含该元素,则返回 true;否则返回 false - public static bool Contains(T[] array, T item) - { - if (IsEmpty(array)) - { - throw new ArgumentException("Array is empty."); - } - for (int i = 0; i < array.Length; i++) - { - if (array[i].Equals(item)) - { - return true; - } - } - - return false; - } - - /// - /// 合并两个数组 - /// - /// 数组1 - /// 数组2 - /// 返回合并后的数组 - public static T[] Concat(T[] array1, T[] array2) - { - if (IsEmpty(array1)) - { - return array2; - } - - if (IsEmpty(array2)) - { - return array1; - } - - T[] concatedArray = new T[array1.Length + array2.Length]; - array1.CopyTo(concatedArray, 0); - array2.CopyTo(concatedArray, array1.Length); - - return concatedArray; - } - - /// - /// 判断两个数组是否完全相等 - /// - /// 数组1 - /// 数组2 - /// 如果两个数组完全相等,则返回 true;否则返回 false - public static bool Equals(T[] array1, T[] array2) - { - if (IsEmpty(array1) && IsEmpty(array2)) - { - return true; - } - - if (IsEmpty(array1) || IsEmpty(array2)) - { - return false; - } - - if (array1.Length != array2.Length) - { - return false; - } - - for (int i = 0; i < array1.Length; i++) - { - if (!array1[i].Equals(array2[i])) - { - return false; - } - } - - return true; - } - } -} diff --git a/EasyTool.Core/ToolCategory/AsyncLockUtil.cs b/EasyTool.Core/ToolCategory/AsyncLockUtil.cs new file mode 100644 index 0000000..73614f2 --- /dev/null +++ b/EasyTool.Core/ToolCategory/AsyncLockUtil.cs @@ -0,0 +1,626 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 异步锁 + /// 支持异步等待的互斥锁 + /// + public class AsyncLock + { + private readonly SemaphoreSlim _semaphore = new(1, 1); + private readonly Task _releaser; + + /// + /// 创建异步锁 + /// + public AsyncLock() + { + _releaser = Task.FromResult(new Releaser(this)); + } + + /// + /// 获取锁 + /// + /// 释放器 + public Task LockAsync() + { + var wait = _semaphore.WaitAsync(); + return wait.IsCompleted + ? _releaser + : wait.ContinueWith((_, state) => new Releaser((AsyncLock)state!), + this, CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); + } + + /// + /// 尝试获取锁 + /// + /// 释放器 + /// 是否成功获取 + public bool TryLock(out Releaser releaser) + { + if (_semaphore.Wait(0)) + { + releaser = new Releaser(this); + return true; + } + releaser = default; + return false; + } + + /// + /// 尝试获取锁(带超时) + /// + /// 超时时间 + /// 释放器 + /// 是否成功获取 + public bool TryLock(TimeSpan timeout, out Releaser releaser) + { + if (_semaphore.Wait(timeout)) + { + releaser = new Releaser(this); + return true; + } + releaser = default; + return false; + } + + /// + /// 尝试获取锁(带超时,异步) + /// + /// 超时时间 + /// 是否成功获取和释放器 + public async Task<(bool acquired, Releaser releaser)> TryLockAsync(TimeSpan timeout) + { + if (await _semaphore.WaitAsync(timeout)) + { + return (true, new Releaser(this)); + } + return (false, default); + } + + /// + /// 锁释放器 + /// + public struct Releaser : IDisposable + { + private readonly AsyncLock? _lock; + + internal Releaser(AsyncLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + _lock?._semaphore.Release(); + } + } + } + + /// + /// 异步读写锁 + /// 支持读写分离的异步锁 + /// + public class AsyncReaderWriterLock + { + private readonly SemaphoreSlim _readLock = new(1, 1); + private readonly SemaphoreSlim _writeLock = new(1, 1); + private int _readersCount; + + /// + /// 获取读锁 + /// + /// 释放器 + public async Task ReaderLockAsync() + { + await _readLock.WaitAsync(); + if (Interlocked.Increment(ref _readersCount) == 1) + { + await _writeLock.WaitAsync(); + } + _readLock.Release(); + + return new ReaderReleaser(this); + } + + /// + /// 获取写锁 + /// + /// 释放器 + public async Task WriterLockAsync() + { + await _writeLock.WaitAsync(); + return new WriterReleaser(this); + } + + /// + /// 尝试获取读锁(带超时) + /// + public async Task<(bool acquired, ReaderReleaser releaser)> TryReaderLockAsync(TimeSpan timeout) + { + if (!await _readLock.WaitAsync(timeout)) + return (false, default); + + try + { + if (Interlocked.Increment(ref _readersCount) == 1) + { + if (!await _writeLock.WaitAsync(timeout)) + { + Interlocked.Decrement(ref _readersCount); + return (false, default); + } + } + _readLock.Release(); + return (true, new ReaderReleaser(this)); + } + catch + { + _readLock.Release(); + return (false, default); + } + } + + /// + /// 尝试获取写锁(带超时) + /// + public async Task<(bool acquired, WriterReleaser releaser)> TryWriterLockAsync(TimeSpan timeout) + { + if (await _writeLock.WaitAsync(timeout)) + { + return (true, new WriterReleaser(this)); + } + return (false, default); + } + + /// + /// 读锁释放器 + /// + public struct ReaderReleaser : IDisposable + { + private readonly AsyncReaderWriterLock? _lock; + + internal ReaderReleaser(AsyncReaderWriterLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + if (_lock == null) return; + + _lock._readLock.Wait(); + if (Interlocked.Decrement(ref _lock._readersCount) == 0) + { + _lock._writeLock.Release(); + } + _lock._readLock.Release(); + } + } + + /// + /// 写锁释放器 + /// + public struct WriterReleaser : IDisposable + { + private readonly AsyncReaderWriterLock? _lock; + + internal WriterReleaser(AsyncReaderWriterLock @lock) + { + _lock = @lock; + } + + public void Dispose() + { + _lock?._writeLock.Release(); + } + } + } + + /// + /// 异步信号量 + /// + public class AsyncSemaphore + { + private readonly SemaphoreSlim _semaphore; + + /// + /// 当前计数 + /// + public int CurrentCount => _semaphore.CurrentCount; + + /// + /// 创建异步信号量 + /// + /// 初始计数 + /// 最大计数 + public AsyncSemaphore(int initialCount, int maxCount = int.MaxValue) + { + _semaphore = new SemaphoreSlim(initialCount, maxCount); + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + return _semaphore.WaitAsync(); + } + + /// + /// 等待信号(带超时) + /// + public Task WaitAsync(TimeSpan timeout) + { + return _semaphore.WaitAsync(timeout); + } + + /// + /// 等待信号(带取消令牌) + /// + public Task WaitAsync(CancellationToken cancellationToken) + { + return _semaphore.WaitAsync(cancellationToken); + } + + /// + /// 释放信号 + /// + public void Release() + { + _semaphore.Release(); + } + + /// + /// 释放指定数量的信号 + /// + public void Release(int releaseCount) + { + _semaphore.Release(releaseCount); + } + } + + /// + /// 异步自动重置事件 + /// + public class AsyncAutoResetEvent + { + private readonly Queue> _waits = new(); + private bool _signaled; + + /// + /// 创建异步自动重置事件 + /// + /// 初始状态 + public AsyncAutoResetEvent(bool initialState = false) + { + _signaled = initialState; + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + lock (_waits) + { + if (_signaled) + { + _signaled = false; + return Task.CompletedTask; + } + + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _waits.Enqueue(tcs); + return tcs.Task; + } + } + + /// + /// 发送信号 + /// + public void Set() + { + lock (_waits) + { + if (_waits.Count > 0) + { + var tcs = _waits.Dequeue(); + tcs.TrySetResult(true); + } + else if (!_signaled) + { + _signaled = true; + } + } + } + } + + /// + /// 异步手动重置事件 + /// + public class AsyncManualResetEvent + { + private TaskCompletionSource _tcs; + + /// + /// 创建异步手动重置事件 + /// + /// 初始状态 + public AsyncManualResetEvent(bool initialState = false) + { + _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + if (initialState) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 等待信号 + /// + public Task WaitAsync() + { + return _tcs.Task; + } + + /// + /// 发送信号(设置) + /// + public void Set() + { + _tcs.TrySetResult(true); + } + + /// + /// 重置 + /// + public void Reset() + { + var newTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + while (true) + { + var oldTcs = _tcs; + if (!oldTcs.Task.IsCompleted) + return; + + if (Interlocked.CompareExchange(ref _tcs, newTcs, oldTcs) == oldTcs) + return; + } + } + + /// + /// 是否已设置 + /// + public bool IsSet => _tcs.Task.IsCompleted; + } + + /// + /// 异步倒计时事件 + /// + public class AsyncCountdownEvent + { + private int _count; + private readonly TaskCompletionSource _tcs; + + /// + /// 当前计数 + /// + public int CurrentCount => _count; + + /// + /// 是否已完成 + /// + public bool IsSet => _count == 0; + + /// + /// 创建异步倒计时事件 + /// + /// 初始计数 + public AsyncCountdownEvent(int initialCount) + { + _count = initialCount; + _tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + if (initialCount <= 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 等待完成 + /// + public Task WaitAsync() + { + return _tcs.Task; + } + + /// + /// 信号(计数减1) + /// + public void Signal() + { + if (Interlocked.Decrement(ref _count) == 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 批量信号 + /// + /// 信号数量 + public void Signal(int signalCount) + { + if (signalCount <= 0) + throw new ArgumentOutOfRangeException(nameof(signalCount)); + + if (Interlocked.Add(ref _count, -signalCount) == 0) + { + _tcs.TrySetResult(true); + } + } + + /// + /// 增加计数 + /// + public void AddCount() + { + Interlocked.Increment(ref _count); + } + + /// + /// 重置 + /// + /// 新计数 + public void Reset(int count) + { + _count = count; + } + } + + /// + /// 异步锁工具类 + /// + public static class AsyncLockUtil + { + private static readonly System.Collections.Concurrent.ConcurrentDictionary _locks = new(); + private static readonly System.Collections.Concurrent.ConcurrentDictionary _rwLocks = new(); + private static readonly System.Collections.Concurrent.ConcurrentDictionary _semaphores = new(); + + /// + /// 获取或创建异步锁 + /// + /// 锁名称 + /// 异步锁 + public static AsyncLock GetOrCreateLock(string name) + { + return _locks.GetOrAdd(name, _ => new AsyncLock()); + } + + /// + /// 使用锁执行操作 + /// + /// 返回类型 + /// 锁名称 + /// 操作 + /// 操作结果 + public static async Task WithLockAsync(string name, Func> action) + { + var @lock = GetOrCreateLock(name); + using (await @lock.LockAsync()) + { + return await action(); + } + } + + /// + /// 使用锁执行操作(无返回值) + /// + /// 锁名称 + /// 操作 + public static async Task WithLockAsync(string name, Func action) + { + var @lock = GetOrCreateLock(name); + using (await @lock.LockAsync()) + { + await action(); + } + } + + /// + /// 获取或创建读写锁 + /// + /// 锁名称 + /// 读写锁 + public static AsyncReaderWriterLock GetOrCreateReaderWriterLock(string name) + { + return _rwLocks.GetOrAdd(name, _ => new AsyncReaderWriterLock()); + } + + /// + /// 使用读锁执行操作 + /// + public static async Task WithReaderLockAsync(string name, Func> action) + { + var @lock = GetOrCreateReaderWriterLock(name); + using (await @lock.ReaderLockAsync()) + { + return await action(); + } + } + + /// + /// 使用写锁执行操作 + /// + public static async Task WithWriterLockAsync(string name, Func> action) + { + var @lock = GetOrCreateReaderWriterLock(name); + using (await @lock.WriterLockAsync()) + { + return await action(); + } + } + + /// + /// 获取或创建信号量 + /// + /// 名称 + /// 初始计数 + /// 最大计数 + /// 信号量 + public static AsyncSemaphore GetOrCreateSemaphore(string name, int initialCount = 1, int maxCount = int.MaxValue) + { + return _semaphores.GetOrAdd(name, _ => new AsyncSemaphore(initialCount, maxCount)); + } + + /// + /// 创建异步自动重置事件 + /// + public static AsyncAutoResetEvent CreateAutoResetEvent(bool initialState = false) + { + return new AsyncAutoResetEvent(initialState); + } + + /// + /// 创建异步手动重置事件 + /// + public static AsyncManualResetEvent CreateManualResetEvent(bool initialState = false) + { + return new AsyncManualResetEvent(initialState); + } + + /// + /// 创建异步倒计时事件 + /// + public static AsyncCountdownEvent CreateCountdownEvent(int initialCount) + { + return new AsyncCountdownEvent(initialCount); + } + + /// + /// 移除锁 + /// + public static bool RemoveLock(string name) + { + return _locks.TryRemove(name, out _); + } + + /// + /// 清空所有锁 + /// + public static void Clear() + { + _locks.Clear(); + _rwLocks.Clear(); + _semaphores.Clear(); + } + } +} diff --git a/EasyTool.Core/ToolCategory/AsyncUtil.cs b/EasyTool.Core/ToolCategory/AsyncUtil.cs new file mode 100644 index 0000000..dc25229 --- /dev/null +++ b/EasyTool.Core/ToolCategory/AsyncUtil.cs @@ -0,0 +1,394 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 异步工具类 + /// 提供异步操作的辅助方法 + /// + public static class AsyncUtil + { + #region 超时控制 + + /// + /// 带超时的异步操作 + /// + /// 返回类型 + /// 异步任务 + /// 超时时间(毫秒) + /// 任务结果 + public static async Task WithTimeout(Task task, int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(); + var completedTask = await Task.WhenAny(task, Task.Delay(timeoutMilliseconds, cts.Token)); + + if (completedTask == task) + { + cts.Cancel(); + return await task; + } + + throw new TimeoutException($"操作在 {timeoutMilliseconds} 毫秒后超时"); + } + + /// + /// 带超时的异步操作 + /// + /// 异步任务 + /// 超时时间(毫秒) + public static async Task WithTimeout(Task task, int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(); + var completedTask = await Task.WhenAny(task, Task.Delay(timeoutMilliseconds, cts.Token)); + + if (completedTask == task) + { + cts.Cancel(); + await task; + return; + } + + throw new TimeoutException($"操作在 {timeoutMilliseconds} 毫秒后超时"); + } + + /// + /// 带超时的异步操作(返回默认值而非抛异常) + /// + /// 返回类型 + /// 异步任务 + /// 超时时间(毫秒) + /// 默认值 + /// 任务结果或默认值 + public static async Task WithTimeoutOrDefault(Task task, int timeoutMilliseconds, T? defaultValue = default) + { + try + { + return await WithTimeout(task, timeoutMilliseconds); + } + catch (TimeoutException) + { + return defaultValue; + } + } + + #endregion + + #region 重试机制 + + /// + /// 异步重试 + /// + /// 返回类型 + /// 异步函数 + /// 最大重试次数 + /// 重试间隔(毫秒) + /// 是否指数退避 + /// 任务结果 + public static async Task RetryAsync( + Func> func, + int maxRetries = 3, + int delayMilliseconds = 1000, + bool exponentialBackoff = true) + { + Exception? lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt < maxRetries) + { + var delay = exponentialBackoff + ? delayMilliseconds * (int)Math.Pow(2, attempt) + : delayMilliseconds; + + await Task.Delay(delay); + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 异步重试(无返回值) + /// + /// 异步操作 + /// 最大重试次数 + /// 重试间隔(毫秒) + /// 是否指数退避 + public static async Task RetryAsync( + Func action, + int maxRetries = 3, + int delayMilliseconds = 1000, + bool exponentialBackoff = true) + { + await RetryAsync(async () => + { + await action(); + return true; + }, maxRetries, delayMilliseconds, exponentialBackoff); + } + + #endregion + + #region 并发控制 + + /// + /// 并发执行多个任务(限制并发数) + /// + /// 返回类型 + /// 任务工厂集合 + /// 最大并发数 + /// 所有任务结果 + public static async Task> WhenAllWithConcurrency( + IEnumerable>> tasks, + int maxConcurrency) + { + var results = new List(); + var taskList = new List>>(tasks); + var semaphore = new SemaphoreSlim(maxConcurrency); + + var wrappedTasks = taskList.Select(async taskFactory => + { + await semaphore.WaitAsync(); + try + { + return await taskFactory(); + } + finally + { + semaphore.Release(); + } + }); + + results.AddRange(await Task.WhenAll(wrappedTasks)); + return results; + } + + /// + /// 并发执行多个任务(限制并发数) + /// + /// 任务工厂集合 + /// 最大并发数 + public static async Task WhenAllWithConcurrency( + IEnumerable> actions, + int maxConcurrency) + { + var actionList = new List>(actions); + var semaphore = new SemaphoreSlim(maxConcurrency); + + var wrappedTasks = actionList.Select(async action => + { + await semaphore.WaitAsync(); + try + { + await action(); + } + finally + { + semaphore.Release(); + } + }); + + await Task.WhenAll(wrappedTasks); + } + + #endregion + + #region 批处理 + + /// + /// 批量处理数据 + /// + /// 输入类型 + /// 输出类型 + /// 数据项 + /// 处理函数 + /// 批次大小 + /// 最大并发数 + /// 所有处理结果 + public static async Task> ProcessBatchAsync( + IEnumerable items, + Func> processor, + int batchSize = 10, + int maxConcurrency = 5) + { + var results = new List(); + var itemList = new List(items); + var batches = new List>(); + + for (int i = 0; i < itemList.Count; i += batchSize) + { + batches.Add(itemList.GetRange(i, Math.Min(batchSize, itemList.Count - i))); + } + + foreach (var batch in batches) + { + var batchResults = await WhenAllWithConcurrency( + batch.Select>>(item => () => processor(item)), + maxConcurrency); + + results.AddRange(batchResults); + } + + return results; + } + + #endregion + + #region 延迟执行 + + /// + /// 延迟执行 + /// + /// 操作 + /// 延迟时间(毫秒) + /// 取消令牌 + public static async Task DelayAsync( + Action action, + int delayMilliseconds, + CancellationToken cancellationToken = default) + { + await Task.Delay(delayMilliseconds, cancellationToken); + action(); + } + + /// + /// 延迟执行(异步操作) + /// + /// 异步操作 + /// 延迟时间(毫秒) + /// 取消令牌 + public static async Task DelayAsync( + Func action, + int delayMilliseconds, + CancellationToken cancellationToken = default) + { + await Task.Delay(delayMilliseconds, cancellationToken); + await action(); + } + + #endregion + + #region 取消支持 + + /// + /// 创建可取消的任务 + /// + /// 返回类型 + /// 异步函数 + /// 取消令牌 + /// 任务结果 + public static async Task RunWithCancellation( + Func> func, + CancellationToken cancellationToken = default) + { + return await func(cancellationToken); + } + + /// + /// 创建带取消令牌的任务超时 + /// + /// 返回类型 + /// 异步函数 + /// 超时时间(毫秒) + /// 任务结果 + public static async Task RunWithTimeout( + Func> func, + int timeoutMilliseconds) + { + using var cts = new CancellationTokenSource(timeoutMilliseconds); + return await func(cts.Token); + } + + #endregion + + #region 顺序执行 + + /// + /// 顺序执行多个异步任务 + /// + /// 返回类型 + /// 任务工厂集合 + /// 所有任务结果 + public static async Task> ExecuteSequentially(IEnumerable>> tasks) + { + var results = new List(); + + foreach (var taskFactory in tasks) + { + results.Add(await taskFactory()); + } + + return results; + } + + /// + /// 顺序执行多个异步任务(无返回值) + /// + /// 任务工厂集合 + public static async Task ExecuteSequentially(IEnumerable> actions) + { + foreach (var action in actions) + { + await action(); + } + } + + #endregion + + #region 结果收集 + + /// + /// 并行执行并收集成功/失败结果 + /// + /// 返回类型 + /// 任务工厂集合 + /// 成功和失败的结果 + public static async Task<(List Successes, List Failures)> CollectResults( + IEnumerable>> tasks) + { + var successes = new List(); + var failures = new List(); + + var results = await Task.WhenAll(tasks.Select(async taskFactory => + { + try + { + return (Success: true, Result: await taskFactory(), Exception: (Exception?)null); + } + catch (Exception ex) + { + return (Success: false, Result: default(T), Exception: ex); + } + })); + + foreach (var result in results) + { + if (result.Success) + { + successes.Add(result.Result!); + } + else if (result.Exception != null) + { + failures.Add(result.Exception); + } + } + + return (successes, failures); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/BackoffUtil.cs b/EasyTool.Core/ToolCategory/BackoffUtil.cs new file mode 100644 index 0000000..9aafe99 --- /dev/null +++ b/EasyTool.Core/ToolCategory/BackoffUtil.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 退避策略工具类 + /// 提供指数退避、线性退避等重试间隔计算 + /// + public static class BackoffUtil + { + /// + /// 指数退避 + /// + /// 尝试次数(从0开始) + /// 基础延迟 + /// 最大延迟 + /// 是否添加随机抖动 + public static TimeSpan Exponential(int attempt, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + var delay = TimeSpan.FromTicks(baseDelay.Ticks * (long)Math.Pow(2, attempt)); + + if (maxDelay.HasValue && delay > maxDelay.Value) + delay = maxDelay.Value; + + if (jitter) + { + var random = new Random(); + var jitterRange = delay.TotalMilliseconds * 0.1; + delay = TimeSpan.FromMilliseconds(delay.TotalMilliseconds + random.NextDouble() * jitterRange); + } + + return delay; + } + + /// + /// 线性退避 + /// + public static TimeSpan Linear(int attempt, TimeSpan baseDelay, TimeSpan? maxDelay = null) + { + var delay = TimeSpan.FromTicks(baseDelay.Ticks * (attempt + 1)); + + if (maxDelay.HasValue && delay > maxDelay.Value) + delay = maxDelay.Value; + + return delay; + } + + /// + /// 固定延迟 + /// + public static TimeSpan Fixed(TimeSpan delay) + { + return delay; + } + + /// + /// 装饰退避(Decorrelated Jitter) + /// + public static TimeSpan DecorrelatedJitter(int attempt, TimeSpan baseDelay, TimeSpan maxDelay, TimeSpan? previousDelay = null) + { + var random = new Random(); + var prev = previousDelay ?? baseDelay; + var delay = TimeSpan.FromTicks((long)(prev.TotalMilliseconds * random.NextDouble() * 3)); + + if (delay < baseDelay) + delay = baseDelay; + + if (delay > maxDelay) + delay = maxDelay; + + return delay; + } + + /// + /// 等距退避 + /// + public static TimeSpan EqualJitter(int attempt, TimeSpan baseDelay, TimeSpan maxDelay) + { + var random = new Random(); + var exponentialDelay = Exponential(attempt, baseDelay, maxDelay, false); + var half = exponentialDelay.TotalMilliseconds / 2; + var delay = TimeSpan.FromMilliseconds(half + random.NextDouble() * half); + return delay; + } + + /// + /// 创建退避策略生成器 + /// + public static BackoffGenerator CreateGenerator(BackoffStrategy strategy, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + return new BackoffGenerator(strategy, baseDelay, maxDelay, jitter); + } + + /// + /// 使用退避策略执行操作 + /// + public static async Task ExecuteWithBackoffAsync( + Func> action, + int maxRetries, + BackoffStrategy strategy = BackoffStrategy.Exponential, + TimeSpan? baseDelay = null, + TimeSpan? maxDelay = null, + Func? shouldRetry = null) + { + var delay = baseDelay ?? TimeSpan.FromSeconds(1); + var max = maxDelay ?? TimeSpan.FromMinutes(1); + Exception? lastException = null; + + for (int attempt = 0; attempt <= maxRetries; attempt++) + { + try + { + return await action(); + } + catch (Exception ex) + { + lastException = ex; + + if (attempt == maxRetries || (shouldRetry != null && !shouldRetry(ex, attempt))) + break; + + var waitTime = strategy switch + { + BackoffStrategy.Exponential => Exponential(attempt, delay, max), + BackoffStrategy.Linear => Linear(attempt, delay, max), + BackoffStrategy.Fixed => delay, + _ => Exponential(attempt, delay, max) + }; + + await Task.Delay(waitTime); + } + } + + throw lastException ?? new Exception("操作失败"); + } + + /// + /// 使用退避策略执行操作 + /// + public static async Task ExecuteWithBackoffAsync( + Func action, + int maxRetries, + BackoffStrategy strategy = BackoffStrategy.Exponential, + TimeSpan? baseDelay = null, + TimeSpan? maxDelay = null, + Func? shouldRetry = null) + { + await ExecuteWithBackoffAsync(async () => + { + await action(); + return true; + }, maxRetries, strategy, baseDelay, maxDelay, shouldRetry); + } + } + + /// + /// 退避策略生成器 + /// + public class BackoffGenerator + { + private readonly BackoffStrategy _strategy; + private readonly TimeSpan _baseDelay; + private readonly TimeSpan? _maxDelay; + private readonly bool _jitter; + private int _attempt; + private TimeSpan? _previousDelay; + + public BackoffGenerator(BackoffStrategy strategy, TimeSpan baseDelay, TimeSpan? maxDelay = null, bool jitter = true) + { + _strategy = strategy; + _baseDelay = baseDelay; + _maxDelay = maxDelay; + _jitter = jitter; + _attempt = 0; + } + + /// + /// 获取下一个延迟时间 + /// + public TimeSpan Next() + { + var delay = _strategy switch + { + BackoffStrategy.Exponential => BackoffUtil.Exponential(_attempt, _baseDelay, _maxDelay, _jitter), + BackoffStrategy.Linear => BackoffUtil.Linear(_attempt, _baseDelay, _maxDelay), + BackoffStrategy.Fixed => _baseDelay, + _ => _baseDelay + }; + + _previousDelay = delay; + _attempt++; + return delay; + } + + /// + /// 重置生成器 + /// + public void Reset() + { + _attempt = 0; + _previousDelay = null; + } + + /// + /// 获取当前尝试次数 + /// + public int Attempt => _attempt; + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/BenchmarkUtil.cs b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs new file mode 100644 index 0000000..26535da --- /dev/null +++ b/EasyTool.Core/ToolCategory/BenchmarkUtil.cs @@ -0,0 +1,317 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 性能测试结果 + /// + public class BenchmarkResult + { + /// + /// 操作名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 执行次数 + /// + public int Iterations { get; set; } + + /// + /// 总耗时 + /// + public TimeSpan TotalTime { get; set; } + + /// + /// 平均耗时 + /// + public TimeSpan AverageTime => Iterations > 0 ? TimeSpan.FromTicks(TotalTime.Ticks / Iterations) : TimeSpan.Zero; + + /// + /// 最小耗时 + /// + public TimeSpan MinTime { get; set; } + + /// + /// 最大耗时 + /// + public TimeSpan MaxTime { get; set; } + + /// + /// 每秒操作数 + /// + public double OperationsPerSecond => TotalTime.TotalSeconds > 0 ? Iterations / TotalTime.TotalSeconds : 0; + + /// + /// 详细耗时记录 + /// + public List DetailedTimes { get; set; } = new(); + + public override string ToString() + { + return $"[{Name}] {Iterations} 次, 总计: {TotalTime.TotalMilliseconds:F2}ms, 平均: {AverageTime.TotalMilliseconds:F4}ms, " + + $"最小: {MinTime.TotalMilliseconds:F4}ms, 最大: {MaxTime.TotalMilliseconds:F4}ms, {OperationsPerSecond:F0} ops/s"; + } + } + + /// + /// 性能测试工具类 + /// 提供代码执行性能测量功能 + /// + public static class BenchmarkUtil + { + /// + /// 测量单次执行时间 + /// + /// 要测量的操作 + /// 执行时间 + public static TimeSpan Measure(Action action) + { + var stopwatch = Stopwatch.StartNew(); + action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 测量单次执行时间(带返回值) + /// + /// 返回值类型 + /// 要测量的操作 + /// 执行结果 + /// 执行时间 + public static TimeSpan Measure(Func func, out T result) + { + var stopwatch = Stopwatch.StartNew(); + result = func(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 异步测量单次执行时间 + /// + /// 要测量的操作 + /// 执行时间 + public static async Task MeasureAsync(Func action) + { + var stopwatch = Stopwatch.StartNew(); + await action(); + stopwatch.Stop(); + return stopwatch.Elapsed; + } + + /// + /// 异步测量单次执行时间(带返回值) + /// + /// 返回值类型 + /// 要测量的操作 + /// 执行时间和结果 + public static async Task<(TimeSpan Elapsed, T Result)> MeasureAsync(Func> func) + { + var stopwatch = Stopwatch.StartNew(); + var result = await func(); + stopwatch.Stop(); + return (stopwatch.Elapsed, result); + } + + /// + /// 基准测试 + /// + /// 测试名称 + /// 要测试的操作 + /// 迭代次数 + /// 预热次数 + /// 测试结果 + public static BenchmarkResult Run(string name, Action action, int iterations = 1000, int warmupIterations = 10) + { + // 预热 + for (int i = 0; i < warmupIterations; i++) + { + action(); + } + + // 正式测试 + var times = new List(iterations); + var totalTime = TimeSpan.Zero; + var minTime = TimeSpan.MaxValue; + var maxTime = TimeSpan.Zero; + + for (int i = 0; i < iterations; i++) + { + var time = Measure(action); + times.Add(time); + totalTime += time; + + if (time < minTime) minTime = time; + if (time > maxTime) maxTime = time; + } + + return new BenchmarkResult + { + Name = name, + Iterations = iterations, + TotalTime = totalTime, + MinTime = minTime, + MaxTime = maxTime, + DetailedTimes = times + }; + } + + /// + /// 异步基准测试 + /// + /// 测试名称 + /// 要测试的操作 + /// 迭代次数 + /// 预热次数 + /// 测试结果 + public static async Task RunAsync(string name, Func action, int iterations = 1000, int warmupIterations = 10) + { + // 预热 + for (int i = 0; i < warmupIterations; i++) + { + await action(); + } + + // 正式测试 + var times = new List(iterations); + var totalTime = TimeSpan.Zero; + var minTime = TimeSpan.MaxValue; + var maxTime = TimeSpan.Zero; + + for (int i = 0; i < iterations; i++) + { + var time = await MeasureAsync(action); + times.Add(time); + totalTime += time; + + if (time < minTime) minTime = time; + if (time > maxTime) maxTime = time; + } + + return new BenchmarkResult + { + Name = name, + Iterations = iterations, + TotalTime = totalTime, + MinTime = minTime, + MaxTime = maxTime, + DetailedTimes = times + }; + } + + /// + /// 比较多个操作的性能 + /// + /// 迭代次数 + /// 操作列表 + /// 测试结果列表 + public static List Compare(int iterations, params (string Name, Action Action)[] actions) + { + var results = new List(); + + foreach (var (name, action) in actions) + { + results.Add(Run(name, action, iterations)); + } + + return results.OrderBy(r => r.AverageTime).ToList(); + } + + /// + /// 异步比较多个操作的性能 + /// + /// 迭代次数 + /// 操作列表 + /// 测试结果列表 + public static async Task> CompareAsync(int iterations, params (string Name, Func Action)[] actions) + { + var results = new List(); + + foreach (var (name, action) in actions) + { + results.Add(await RunAsync(name, action, iterations)); + } + + return results.OrderBy(r => r.AverageTime).ToList(); + } + + /// + /// 使用计时器测量操作 + /// + /// 要测量的操作 + /// 耗时回调 + public static void WithTimer(Action action, Action elapsed) + { + var time = Measure(action); + elapsed(time); + } + + /// + /// 异步使用计时器测量操作 + /// + /// 要测量的操作 + /// 耗时回调 + public static async Task WithTimerAsync(Func action, Action elapsed) + { + var time = await MeasureAsync(action); + elapsed(time); + } + + /// + /// 格式化时间输出 + /// + /// 时间 + /// 格式化字符串 + public static string FormatTime(TimeSpan time) + { + if (time.TotalSeconds >= 1) + return $"{time.TotalSeconds:F2}s"; + if (time.TotalMilliseconds >= 1) + return $"{time.TotalMilliseconds:F2}ms"; +#if NET7_0_OR_GREATER + if (time.TotalMicroseconds >= 1) + return $"{time.TotalMicroseconds:F2}μs"; + return $"{time.TotalNanoseconds:F2}ns"; +#else + // For older frameworks, use ticks for sub-millisecond precision + var ticks = time.Ticks; + if (ticks >= 10) // >= 1 microsecond (10 ticks = 1 μs) + return $"{ticks / 10.0:F2}μs"; + return $"{ticks * 100.0:F2}ns"; +#endif + } + + /// + /// 打印比较结果 + /// + /// 测试结果列表 + public static void PrintComparison(List results) + { + if (results.Count == 0) + return; + + Console.WriteLine("=== 性能比较结果 ==="); + Console.WriteLine(); + + var baseline = results[0].AverageTime; + + for (int i = 0; i < results.Count; i++) + { + var result = results[i]; + var ratio = i == 0 ? 1.0 : result.AverageTime.TotalMilliseconds / baseline.TotalMilliseconds; + + Console.WriteLine($"{i + 1}. {result.Name}"); + Console.WriteLine($" 平均: {FormatTime(result.AverageTime)}"); + Console.WriteLine($" 比率: {ratio:F2}x"); + Console.WriteLine($" 吞吐: {result.OperationsPerSecond:F0} ops/s"); + Console.WriteLine(); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs b/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs new file mode 100644 index 0000000..33b4309 --- /dev/null +++ b/EasyTool.Core/ToolCategory/CircuitBreakerUtil.cs @@ -0,0 +1,301 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 熔断器状态 + /// + public enum CircuitState + { + /// + /// 关闭(正常) + /// + Closed, + + /// + /// 开启(熔断) + /// + Open, + + /// + /// 半开(尝试恢复) + /// + HalfOpen + } + + /// + /// 熔断器配置 + /// + public class CircuitBreakerOptions + { + /// + /// 失败阈值(触发熔断的最小失败次数) + /// + public int FailureThreshold { get; set; } = 5; + + /// + /// 成功阈值(半开状态下恢复的最小成功次数) + /// + public int SuccessThreshold { get; set; } = 2; + + /// + /// 熔断持续时间 + /// + public TimeSpan BreakDuration { get; set; } = TimeSpan.FromSeconds(30); + + /// + /// 熔断超时时间 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10); + + /// + /// 判断是否应该熔断的异常类型 + /// + public List ExceptionTypesToTrack { get; set; } = new() { typeof(Exception) }; + } + + /// + /// 熔断器 + /// + public class CircuitBreaker + { + private readonly CircuitBreakerOptions _options; + private readonly object _lock = new(); + private CircuitState _state = CircuitState.Closed; + private int _failureCount; + private int _successCount; + private DateTime _lastFailureTime; + + /// + /// 当前状态 + /// + public CircuitState State + { + get + { + lock (_lock) + { + if (_state == CircuitState.Open && ShouldAttemptReset()) + { + _state = CircuitState.HalfOpen; + _successCount = 0; + } + return _state; + } + } + } + + /// + /// 失败次数 + /// + public int FailureCount => _failureCount; + + /// + /// 成功次数 + /// + public int SuccessCount => _successCount; + + /// + /// 最后失败时间 + /// + public DateTime LastFailureTime => _lastFailureTime; + + /// + /// 状态变更事件 + /// + public event EventHandler? StateChanged; + + /// + /// 创建熔断器 + /// + public CircuitBreaker(CircuitBreakerOptions? options = null) + { + _options = options ?? new CircuitBreakerOptions(); + } + + /// + /// 执行操作 + /// + public async Task ExecuteAsync(Func> action) + { + var state = State; + + if (state == CircuitState.Open) + { + throw new CircuitBreakerOpenException("熔断器处于开启状态"); + } + + try + { + using var cts = new System.Threading.CancellationTokenSource(_options.Timeout); + var task = action(); + var completedTask = await Task.WhenAny(task, Task.Delay(_options.Timeout)); + + if (completedTask != task) + { + OnFailure(); + throw new TimeoutException("操作超时"); + } + + var result = await task; + OnSuccess(); + return result; + } + catch (Exception ex) when (ShouldTrackException(ex)) + { + OnFailure(); + throw; + } + } + + /// + /// 执行操作 + /// + public async Task ExecuteAsync(Func action) + { + await ExecuteAsync(async () => + { + await action(); + return true; + }); + } + + /// + /// 尝试执行操作 + /// + public async Task<(bool Success, T? Result, Exception? Error)> TryExecuteAsync(Func> action) + { + try + { + var result = await ExecuteAsync(action); + return (true, result, null); + } + catch (Exception ex) + { + return (false, default, ex); + } + } + + private bool ShouldAttemptReset() + { + return DateTime.UtcNow - _lastFailureTime >= _options.BreakDuration; + } + + private bool ShouldTrackException(Exception ex) + { + foreach (var type in _options.ExceptionTypesToTrack) + { + if (type.IsAssignableFrom(ex.GetType())) + return true; + } + return false; + } + + private void OnSuccess() + { + lock (_lock) + { + if (_state == CircuitState.HalfOpen) + { + _successCount++; + if (_successCount >= _options.SuccessThreshold) + { + TripTo(CircuitState.Closed); + _failureCount = 0; + _successCount = 0; + } + } + else if (_state == CircuitState.Closed) + { + _failureCount = 0; + } + } + } + + private void OnFailure() + { + lock (_lock) + { + _lastFailureTime = DateTime.UtcNow; + _failureCount++; + + if (_state == CircuitState.HalfOpen) + { + TripTo(CircuitState.Open); + } + else if (_state == CircuitState.Closed && _failureCount >= _options.FailureThreshold) + { + TripTo(CircuitState.Open); + } + } + } + + private void TripTo(CircuitState newState) + { + var oldState = _state; + _state = newState; + StateChanged?.Invoke(this, newState); + } + + /// + /// 重置熔断器 + /// + public void Reset() + { + lock (_lock) + { + _state = CircuitState.Closed; + _failureCount = 0; + _successCount = 0; + } + } + + /// + /// 强制打开熔断器 + /// + public void Open() + { + lock (_lock) + { + TripTo(CircuitState.Open); + _lastFailureTime = DateTime.UtcNow; + } + } + } + + /// + /// 熔断器开启异常 + /// + public class CircuitBreakerOpenException : Exception + { + public CircuitBreakerOpenException(string message) : base(message) { } + } + + /// + /// 熔断器工具类 + /// + public static class CircuitBreakerUtil + { + /// + /// 创建熔断器 + /// + public static CircuitBreaker Create(CircuitBreakerOptions? options = null) + { + return new CircuitBreaker(options); + } + + /// + /// 创建熔断器 + /// + public static CircuitBreaker Create(int failureThreshold, TimeSpan breakDuration) + { + return new CircuitBreaker(new CircuitBreakerOptions + { + FailureThreshold = failureThreshold, + BreakDuration = breakDuration + }); + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/ClassExtension.cs b/EasyTool.Core/ToolCategory/ClassExtension.cs deleted file mode 100644 index efc337d..0000000 --- a/EasyTool.Core/ToolCategory/ClassExtension.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace EasyTool.Extension -{ - /// - /// ClassUtil 工具类提供了许多有用的方法,可以帮助您轻松处理和操作C#类 - /// - public static class ClassExtension - { - /// - /// 获取类的继承层次结构 - /// - /// 要获取继承层次结构的类 - /// 类的继承层次结构 - public static Type[] GetClassHierarchy(this Type type) => ClassUtil.GetClassHierarchy(type); - - - - - /// - /// 获取类的静态属性的值 - /// - /// 要获取静态属性的类 - /// 要获取的静态属性的名称 - /// 静态属性的值 - public static object GetStaticPropertyValue(this Type type, string propertyName) => ClassUtil.GetStaticPropertyValue(type, propertyName); - - - /// - /// 设置类的静态属性的值 - /// - /// 要设置静态属性的类 - /// 要设置的静态属性的名称 - /// 要设置的静态属性的值 - public static void SetStaticPropertyValue(this Type type, string propertyName, object value) => ClassUtil.SetStaticPropertyValue(type, propertyName, value); - - - /// - /// 获取类的静态字段的值 - /// - /// 要获取静态字段的类 - /// 要获取的静态字段的名称 - /// 静态字段的值 - public static object GetStaticFieldValue(this Type type, string fieldName) => ClassUtil.GetStaticFieldValue(type, fieldName); - - /// - /// 设置类的静态字段的值 - /// - /// 要设置静态字段的类 - /// 要设置的静态字段的名称 - /// 要设置的静态字段的值 - public static void SetStaticFieldValue(this Type type, string fieldName, object value) => ClassUtil.SetStaticFieldValue(type, fieldName, value); - - - /// - /// 动态调用类的静态方法 - /// - /// 要调用静态方法的类 - /// 要调用的静态方法的名称 - /// 要传递给静态方法的参数 - /// 静态方法的返回值 - public static object InvokeStaticMethod(this Type type, string methodName, object[] arguments) => ClassUtil.InvokeStaticMethod(type, methodName, arguments); - - ///// - ///// 动态调用类的实例方法 - ///// - ///// 要调用实例方法的类实例 - ///// 要调用的实例方法的名称 - ///// 要传递给实例方法的参数 - ///// 实例方法的返回值 - //public static object InvokeMethod(object instance, string methodName, object[] arguments) - //{ - // Type type = instance.GetType(); - // MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); - // return method.Invoke(instance, arguments); - //} - - /// - /// 动态创建类的实例 - /// - /// 要创建实例的类 - /// 要传递给构造函数的参数 - /// 类的新实例 - public static object CreateInstance(this Type type, object[] constructorArguments) => ClassUtil.CreateInstance(type, constructorArguments); - } -} diff --git a/EasyTool.Core/ToolCategory/ClassUtil.cs b/EasyTool.Core/ToolCategory/ClassUtil.cs deleted file mode 100644 index b3ad3d4..0000000 --- a/EasyTool.Core/ToolCategory/ClassUtil.cs +++ /dev/null @@ -1,224 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// ClassUtil 工具类提供了许多有用的方法,可以帮助您轻松处理和操作C#类 - /// - public class ClassUtil - { - /// - /// 获取类的完全限定名 - /// - /// 要获取名称的类 - /// 类的完全限定名 - public static string GetClassName(Type type) - { - return type.FullName; - } - - /// - /// 获取类的命名空间 - /// - /// 要获取命名空间的类 - /// 类的命名空间 - public static string GetClassNamespace(Type type) - { - return type.Namespace; - } - - /// - /// 获取类的继承层次结构 - /// - /// 要获取继承层次结构的类 - /// 类的继承层次结构 - public static Type[] GetClassHierarchy(Type type) - { - Type[] hierarchy = new Type[0]; - Type currentType = type; - while (currentType != null) - { - Array.Resize(ref hierarchy, hierarchy.Length + 1); - hierarchy[hierarchy.Length - 1] = currentType; - currentType = currentType.BaseType; - } - return hierarchy; - } - - /// - /// 获取类的所有方法 - /// - /// 要获取方法的类 - /// 类的所有方法 - public static MethodInfo[] GetClassMethods(Type type) - { - return type.GetMethods(); - } - - /// - /// 获取类的所有属性 - /// - /// 要获取属性的类 - /// 类的所有属性 - public static PropertyInfo[] GetClassProperties(Type type) - { - return type.GetProperties(); - } - - /// - /// 获取类的所有字段 - /// - /// 要获取字段的类 - /// 类的所有字段 - public static FieldInfo[] GetClassFields(Type type) - { - return type.GetFields(); - } - - /// - /// 获取类的所有事件 - /// - /// 要获取事件的类 - /// 类的所有事件 - public static EventInfo[] GetClassEvents(Type type) - { - return type.GetEvents(); - } - - /// - /// 获取类的所有构造函数 - /// - /// 要获取构造函数的类 - /// 类的所有构造函数 - public static ConstructorInfo[] GetClassConstructors(Type type) - { - return type.GetConstructors(); - } - - /// - /// 获取类的默认构造函数 - /// - /// 要获取默认构造函数的类 - /// 类的默认构造函数 - public static ConstructorInfo GetDefaultClassConstructor(Type type) - { - return type.GetConstructor(Type.EmptyTypes); - } - - /// - /// 获取类的静态属性的值 - /// - /// 要获取静态属性的类 - /// 要获取的静态属性的名称 - /// 静态属性的值 - public static object GetStaticPropertyValue(Type type, string propertyName) - { - PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); - return property.GetValue(null); - } - - /// - /// 设置类的静态属性的值 - /// - /// 要设置静态属性的类 - /// 要设置的静态属性的名称 - /// 要设置的静态属性的值 - public static void SetStaticPropertyValue(Type type, string propertyName, object value) - { - PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public); - property.SetValue(null, value); - } - - /// - /// 获取类的静态字段的值 - /// - /// 要获取静态字段的类 - /// 要获取的静态字段的名称 - /// 静态字段的值 - public static object GetStaticFieldValue(Type type, string fieldName) - { - FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); - return field.GetValue(null); - } - - /// - /// 设置类的静态字段的值 - /// - /// 要设置静态字段的类 - /// 要设置的静态字段的名称 - /// 要设置的静态字段的值 - public static void SetStaticFieldValue(Type type, string fieldName, object value) - { - FieldInfo field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public); - field.SetValue(null, value); - } - - /// - /// 动态调用类的静态方法 - /// - /// 要调用静态方法的类 - /// 要调用的静态方法的名称 - /// 要传递给静态方法的参数 - /// 静态方法的返回值 - public static object InvokeStaticMethod(Type type, string methodName, object[] arguments) - { - MethodInfo method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public); - return method.Invoke(null, arguments); - } - - /// - /// 动态调用类的实例方法 - /// - /// 要调用实例方法的类实例 - /// 要调用的实例方法的名称 - /// 要传递给实例方法的参数 - /// 实例方法的返回值 - public static object InvokeMethod(object instance, string methodName, object[] arguments) - { - Type type = instance.GetType(); - MethodInfo method = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public); - return method.Invoke(instance, arguments); - } - - /// - /// 动态创建类的实例 - /// - /// 要创建实例的类 - /// 要传递给构造函数的参数 - /// 类的新实例 - public static object CreateInstance(Type type, params object[] constructorArguments) - { - ConstructorInfo constructor = type.GetConstructor(GetParameterTypes(constructorArguments)); - return constructor.Invoke(constructorArguments); - } - - /// - /// 获取构造函数参数类型的数组 - /// - /// 要获取参数类型的参数数组 - /// 参数类型的数组 - private static Type[] GetParameterTypes(object[] parameters) - { - if (parameters == null) - { - return Type.EmptyTypes; - } - Type[] parameterTypes = new Type[parameters.Length]; - for (int i = 0; i < parameters.Length; i++) - { - if (parameters[i] == null) - { - parameterTypes[i] = typeof(object); - } - else - { - parameterTypes[i] = parameters[i].GetType(); - } - } - return parameterTypes; - } - } -} diff --git a/EasyTool.Core/ToolCategory/CommandLineParser.cs b/EasyTool.Core/ToolCategory/CommandLineParser.cs new file mode 100644 index 0000000..253d057 --- /dev/null +++ b/EasyTool.Core/ToolCategory/CommandLineParser.cs @@ -0,0 +1,297 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 命令行参数解析器 + /// + public class CommandLineParser + { + private readonly Dictionary _options = new(StringComparer.OrdinalIgnoreCase); + private readonly List _arguments = new(); + private readonly HashSet _flags = new(StringComparer.OrdinalIgnoreCase); + + /// + /// 获取选项数量 + /// + public int OptionCount => _options.Count; + + /// + /// 获取参数数量 + /// + public int ArgumentCount => _arguments.Count; + + /// + /// 获取标志数量 + /// + public int FlagCount => _flags.Count; + + /// + /// 解析命令行参数 + /// + public static CommandLineParser Parse(string[] args, CommandLineOptions? options = null) + { + var parser = new CommandLineParser(); + options ??= new CommandLineOptions(); + + for (int i = 0; i < args.Length; i++) + { + var arg = args[i]; + + if (arg.StartsWith("--")) + { + // 长选项 --option 或 --option=value + var option = arg.Substring(2); + var eqIndex = option.IndexOf('='); + + if (eqIndex >= 0) + { + var name = option.Substring(0, eqIndex); + var value = option.Substring(eqIndex + 1); + parser._options[name] = value; + } + else if (i + 1 < args.Length && !args[i + 1].StartsWith("-")) + { + parser._options[option] = args[++i]; + } + else + { + parser._flags.Add(option); + } + } + else if (arg.StartsWith("-")) + { + // 短选项 -o 或 -o value + var option = arg.Substring(1); + + if (option.Length == 1) + { + if (i + 1 < args.Length && !args[i + 1].StartsWith("-")) + { + parser._options[option] = args[++i]; + } + else + { + parser._flags.Add(option); + } + } + else + { + // 组合短选项 -abc 相当于 -a -b -c + foreach (var c in option) + { + parser._flags.Add(c.ToString()); + } + } + } + else + { + parser._arguments.Add(arg); + } + } + + return parser; + } + + /// + /// 获取选项值 + /// + public string? GetOption(string name, string? defaultValue = null) + { + return _options.TryGetValue(name, out var value) ? value : defaultValue; + } + + /// + /// 获取选项值(转换为指定类型) + /// + public T? GetOption(string name, T? defaultValue = default) + { + var value = GetOption(name); + if (value == null) return defaultValue; + + try + { + return (T?)Convert.ChangeType(value, typeof(T)); + } + catch + { + return defaultValue; + } + } + + /// + /// 检查是否有选项 + /// + public bool HasOption(string name) + { + return _options.ContainsKey(name) || _flags.Contains(name); + } + + /// + /// 检查是否有标志 + /// + public bool HasFlag(string flag) + { + return _flags.Contains(flag); + } + + /// + /// 获取参数 + /// + public string? GetArgument(int index, string? defaultValue = null) + { + return index >= 0 && index < _arguments.Count ? _arguments[index] : defaultValue; + } + + /// + /// 获取参数(转换为指定类型) + /// + public T? GetArgument(int index, T? defaultValue = default) + { + var value = GetArgument(index); + if (value == null) return defaultValue; + + try + { + return (T?)Convert.ChangeType(value, typeof(T)); + } + catch + { + return defaultValue; + } + } + + /// + /// 获取所有参数 + /// + public IReadOnlyList GetArguments() + { + return _arguments.AsReadOnly(); + } + + /// + /// 获取所有选项 + /// + public IReadOnlyDictionary GetOptions() + { + return _options; + } + + /// + /// 获取所有标志 + /// + public IReadOnlyCollection GetFlags() + { + return _flags.ToList().AsReadOnly(); + } + } + + /// + /// 命令行解析选项 + /// + public class CommandLineOptions + { + /// + /// 是否允许组合短选项 + /// + public bool AllowCombinedShortOptions { get; set; } = true; + + /// + /// 是否忽略未知选项 + /// + public bool IgnoreUnknownOptions { get; set; } = true; + } + + /// + /// 参数构建器 + /// + public class ArgumentBuilder + { + private readonly List _args = new(); + + /// + /// 添加参数 + /// + public ArgumentBuilder Add(string value) + { + _args.Add(value); + return this; + } + + /// + /// 添加选项 + /// + public ArgumentBuilder AddOption(string name, string? value = null) + { + _args.Add($"--{name}"); + if (value != null) + { + _args.Add(value); + } + return this; + } + + /// + /// 添加短选项 + /// + public ArgumentBuilder AddShortOption(char name, string? value = null) + { + _args.Add($"-{name}"); + if (value != null) + { + _args.Add(value); + } + return this; + } + + /// + /// 添加标志 + /// + public ArgumentBuilder AddFlag(string name) + { + _args.Add($"--{name}"); + return this; + } + + /// + /// 添加多个参数 + /// + public ArgumentBuilder AddRange(IEnumerable values) + { + _args.AddRange(values); + return this; + } + + /// + /// 构建参数数组 + /// + public string[] Build() + { + return _args.ToArray(); + } + + /// + /// 构建命令行字符串 + /// + public string BuildCommandLine() + { + return string.Join(" ", _args.Select(QuoteIfNeeded)); + } + + private static string QuoteIfNeeded(string arg) + { + if (arg.Contains(' ') || arg.Contains('"')) + { + return $"\"{arg.Replace("\"", "\\\"")}\""; + } + return arg; + } + + public override string ToString() + { + return BuildCommandLine(); + } + } +} diff --git a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs b/EasyTool.Core/ToolCategory/CreditCodeUtil.cs deleted file mode 100644 index fc4704a..0000000 --- a/EasyTool.Core/ToolCategory/CreditCodeUtil.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace EasyTool -{ - /// - /// 社会信用代码工具 - /// - public class CreditCodeUtil - { - private const string BaseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; // 社会信用代码中的基础字符集 - private const int Modulo = 31; // 校验码计算中的模数 - - /// - /// 检查社会信用代码是否有效 - /// - /// 社会信用代码 - /// 是否有效 - public static bool IsValidCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return false; - } - - // 将社会信用代码中的每个字符转换为对应的数字,再计算出校验码 - int sum = 0; - for (int i = 0; i < creditCode.Length - 1; i++) - { - int code = BaseCode.IndexOf(creditCode[i]); - int weight = GetWeight(i + 1); - sum += code * weight; - } - - int checkCode = (Modulo - sum % Modulo) % Modulo; - int lastCode = BaseCode.IndexOf(creditCode[17]); - - return checkCode == lastCode; - } - - /// - /// 获取指定位置的数字权重 - /// - /// 位置 - /// 数字权重 - private static int GetWeight(int position) - { - if (position <= 1 || position == 9) - { - return 1; - } - else if (position == 2) - { - return 9; - } - else - { - return 9 - position + 2; - } - } - - /// - /// 生成随机的社会信用代码 - /// - /// 随机的社会信用代码 - public static string GenerateRandomCreditCode() - { - string orgCode = "911101"; // 默认的组织机构代码 - string entType = "00"; // 默认的企业类型 - string regNum = RandomUtil.RandomNumberString(10); // 生成随机的注册号 - string code = orgCode + entType + regNum; - - // 计算出校验码并添加到社会信用代码中 - int sum = 0; - for (int i = 0; i < code.Length; i++) - { - int weight = GetWeight(i + 1); - int digit = BaseCode.IndexOf(code[i]); - sum += digit * weight; - } - - int checkCode = (Modulo - sum % Modulo) % Modulo; - return code + BaseCode[checkCode]; - } - - /// - /// 从社会信用代码中提取组织机构代码 - /// - /// 社会信用代码 - /// 组织机构代码 - public static string GetOrgCodeFromCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - return creditCode.Substring(0, 9); - } - /// - /// 从社会信用代码中提取企业类型 - /// - /// 社会信用代码 - /// 企业类型 - public static string GetEntTypeFromCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - - return creditCode.Substring(9, 2); - } - - /// - /// 从社会信用代码中提取注册号 - /// - /// 社会信用代码 - /// 注册号 - public static string GetRegNumFromCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - - return creditCode.Substring(11, 10); - } - - /// - /// 从社会信用代码中提取行政区划码 - /// - /// 社会信用代码 - /// 行政区划码 - public static string GetAreaCodeFromCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - - return creditCode.Substring(2, 6); - } - - /// - /// 从社会信用代码中提取机构类型 - /// - /// 社会信用代码 - /// 机构类型 - public static string GetOrgTypeFromCreditCode(string creditCode) - { - if (string.IsNullOrWhiteSpace(creditCode) || creditCode.Length != 18) - { - return null; - } - - return creditCode[8].ToString(); - } - - } -} diff --git a/EasyTool.Core/ToolCategory/DLLUtil.cs b/EasyTool.Core/ToolCategory/DLLUtil.cs deleted file mode 100644 index e41e2ab..0000000 --- a/EasyTool.Core/ToolCategory/DLLUtil.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// dll工具 - /// - public class DLLUtil - { - /// - /// 根据文件路径加载 DLL 程序集,并返回一个 Assembly 对象 - /// - /// DLL 文件路径 - /// 返回一个 Assembly 对象 - public static Assembly LoadAssembly(string dllFilePath) - { - return Assembly.LoadFile(dllFilePath); - } - - /// - /// 根据类型名称从程序集中获取 Type 对象 - /// - /// 程序集 - /// 类型名称 - /// 返回 Type 对象 - public static Type GetTypeFromAssembly(Assembly assembly, string typeName) - { - return assembly.GetType(typeName); - } - - /// - /// 创建指定类型的实例,并返回一个 Object 对象 - /// - /// 要创建实例的类型 - /// 实例化类型所需要的参数 - /// 返回创建的实例对象 - public static object CreateInstance(Type type, params object[] parameters) - { - return Activator.CreateInstance(type, parameters); - } - - /// - /// 根据类型名称创建实例,并返回一个 Object 对象 - /// - /// 程序集 - /// 类型名称 - /// 实例化类型所需要的参数 - /// 返回创建的实例对象 - public static object CreateInstanceFromAssembly(Assembly assembly, string typeName, params object[] parameters) - { - Type type = GetTypeFromAssembly(assembly, typeName); - if (type != null) - { - return CreateInstance(type, parameters); - } - return null; - } - - /// - /// 调用对象的方法,并返回调用结果 - /// - /// 要调用方法的对象 - /// 方法名称 - /// 方法所需要的参数 - /// 返回调用结果 - public static object InvokeMethod(object instance, string methodName, params object[] parameters) - { - Type type = instance.GetType(); - MethodInfo methodInfo = type.GetMethod(methodName); - if (methodInfo != null) - { - return methodInfo.Invoke(instance, parameters); - } - else - { - return null; - } - } - - /// - /// 获取程序集中所有的类型信息 - /// - /// 程序集 - /// 返回 Type[] 数组,数组中每个元素代表程序集中的一个类型 - public static Type[] GetAllTypesFromAssembly(Assembly assembly) - { - return assembly.GetTypes(); - } - - /// - /// 判断指定类型是否实现了指定的接口 - /// - /// 要判断的类型 - /// 要判断的接口类型 - /// 返回布尔值,表示指定类型是否实现了指定的接口 - public static bool IsImplementInterface(Type type, Type interfaceType) - { - return interfaceType.IsAssignableFrom(type); - } - - /// - /// 从指定目录中加载所有的 DLL 文件,并返回一个 Assembly[] 数组 - /// - /// 要加载 DLL 文件的目录 - /// 返回一个 Assembly[] 数组,数组中每个元素代表一个 DLL 程序集 - public static Assembly[] LoadAllDllsFromDirectory(string directory) - { - try - { - if (!Directory.Exists(directory)) - { - throw new Exception("LoadAllDllsFromDirectory Error: Directory not exist."); - } - - string[] dllFiles = Directory.GetFiles(directory, "*.dll"); - if (dllFiles.Length == 0) - { - throw new Exception("LoadAllDllsFromDirectory Error: No DLL file found."); - } - - Assembly[] assemblies = new Assembly[dllFiles.Length]; - for (int i = 0; i < dllFiles.Length; i++) - { - assemblies[i] = LoadAssembly(dllFiles[i]); - } - return assemblies; - } - catch (Exception ex) - { - throw new Exception("LoadAllDllsFromDirectory Error: " + ex.Message); - } - } - } -} diff --git a/EasyTool.Core/ToolCategory/DelegateExtension.cs b/EasyTool.Core/ToolCategory/DelegateExtension.cs new file mode 100644 index 0000000..5585100 --- /dev/null +++ b/EasyTool.Core/ToolCategory/DelegateExtension.cs @@ -0,0 +1,428 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// Delegate 委托扩展方法 + /// + public static class DelegateExtension + { + #region 安全调用 + + /// + /// 安全调用 Action(捕获异常) + /// + public static void SafeInvoke(this Action? action, Action? onError = null) + { + if (action == null) + return; + + try + { + action(); + } + catch (Exception ex) + { + onError?.Invoke(ex); + } + } + + /// + /// 安全调用 Func(捕获异常,失败返回默认值) + /// + public static T? SafeInvoke(this Func? func, T? defaultValue = default, Action? onError = null) + { + if (func == null) + return defaultValue; + + try + { + return func(); + } + catch (Exception ex) + { + onError?.Invoke(ex); + return defaultValue; + } + } + + #endregion + + #region 重试 + + /// + /// Action 重试执行 + /// + public static void Retry(this Action action, int retryCount = 3, int delayMs = 0) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + Thread.Sleep(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// Func 重试执行 + /// + public static T Retry(this Func func, int retryCount = 3, int delayMs = 0) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + Thread.Sleep(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 异步 Action 重试执行 + /// + public static async Task RetryAsync(this Func action, int retryCount = 3, int delayMs = 0) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + await action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + await Task.Delay(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 异步 Func 重试执行 + /// + public static async Task RetryAsync(this Func> func, int retryCount = 3, int delayMs = 0) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delayMs > 0) + { + await Task.Delay(delayMs); + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + #endregion + + #region 超时 + + /// + /// 设置 Action 超时 + /// + public static void WithTimeout(this Action action, int timeoutMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + var task = Task.Run(action); + if (!task.Wait(timeoutMs)) + throw new TimeoutException($"操作超时({timeoutMs}ms)"); + } + + /// + /// 设置 Func 超时 + /// + public static T WithTimeout(this Func func, int timeoutMs) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + var task = Task.Run(func); + if (!task.Wait(timeoutMs)) + throw new TimeoutException($"操作超时({timeoutMs}ms)"); + + return task.Result; + } + + #endregion + + #region 防抖与节流 + + /// + /// 防抖(延迟执行,如果在延迟时间内再次调用则重新计时) + /// + public static Action Debounce(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Timer? timer = null; + return () => + { + timer?.Dispose(); + timer = new Timer(state => + { + action(); + timer?.Dispose(); + }, null, delayMs, Timeout.Infinite); + }; + } + + /// + /// 节流(指定时间间隔内只执行一次) + /// + public static Action Throttle(this Action action, int intervalMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + DateTime lastRun = DateTime.MinValue; + return () => + { + var now = DateTime.Now; + if ((now - lastRun).TotalMilliseconds >= intervalMs) + { + action(); + lastRun = now; + } + }; + } + + #endregion + + #region 延迟执行 + + /// + /// 延迟执行 Action + /// + public static void Delay(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Task.Delay(delayMs).ContinueWith(_ => action()); + } + + /// + /// 异步延迟执行 Action + /// + public static async Task DelayAsync(this Action action, int delayMs) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + await Task.Delay(delayMs); + action(); + } + + /// + /// 异步延迟执行 Func + /// + public static async Task DelayAsync(this Func func, int delayMs) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + await Task.Delay(delayMs); + return func(); + } + + #endregion + + #region 链式调用 + + /// + /// 链式调用 Action + /// + public static Action? Then(this Action? first, Action? second) + { + if (first == null) + return second; + if (second == null) + return first; + + return () => + { + first(); + second(); + }; + } + + /// + /// 链式调用 Func + /// + public static Func? Then(this Func? first, Func? second) + { + if (first == null) + return second; + if (second == null) + return first; + + return () => + { + first(); + return second(); + }; + } + + /// + /// 链式调用 Func(转换) + /// + public static Func? Then(this Func? first, Func? second) + { + if (first == null || second == null) + return null; + + return () => second(first()); + } + + #endregion + + #region 条件执行 + + /// + /// 条件执行 Action + /// + public static Action? ExecuteIf(this Action? action, bool condition) + { + if (action == null) + return null; + + return () => + { + if (condition) + action(); + }; + } + + /// + /// 条件执行 Func + /// + public static Func? ExecuteIf(this Func? func, bool condition) + { + if (func == null) + return null; + + return () => condition ? func() : default; + } + + #endregion + + #region 缓存 + + /// + /// 缓存 Func 结果 + /// + public static Func? Cache(this Func? func) + { + if (func == null) + return null; + + bool cached = false; + T? value = default; + + return () => + { + if (!cached) + { + value = func(); + cached = true; + } + return value; + }; + } + + #endregion + + #region 组合 + + /// + /// 组合多个 Action + /// + public static Action Combine(params Action[] actions) + { + return () => + { + foreach (var action in actions) + { + action?.Invoke(); + } + }; + } + + /// + /// 组合多个 Func(后者的结果作为前者的参数) + /// + public static Func? Compose(this Func? func1, Func? func2) + { + if (func1 == null || func2 == null) + return null; + + return x => func1(func2(x)); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/EnumExtension.cs b/EasyTool.Core/ToolCategory/EnumExtension.cs new file mode 100644 index 0000000..5a1a3a0 --- /dev/null +++ b/EasyTool.Core/ToolCategory/EnumExtension.cs @@ -0,0 +1,374 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// Enum 枚举扩展方法 + /// + public static class EnumExtension + { + #region 描述信息 + + /// + /// 获取枚举值的描述(Description 特性) + /// + public static string GetDescription(this Enum value) + { + if (value == null) + return string.Empty; + + var field = value.GetType().GetField(value.ToString()); + if (field == null) + return value.ToString(); + + var attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; + return attr?.Description ?? value.ToString(); + } + + /// + /// 获取枚举值的显示名称(Display 特性) + /// + public static string GetDisplayName(this Enum value) + { + if (value == null) + return string.Empty; + + var field = value.GetType().GetField(value.ToString()); + if (field == null) + return value.ToString(); + + var attr = field.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.DisplayAttribute; + return attr?.GetName() ?? value.ToString(); + } + + #endregion + + #region 枚举转换 + + + /// + /// 将整数转换为枚举 + /// + public static T ToEnum(this int value) where T : struct, Enum + { + return Enum.Parse(value.ToString()); + } + + /// + /// 安全解析字符串为枚举 + /// + public static T ParseEnum(this string value) where T : struct, Enum + { + return Enum.Parse(value, true); + } + + /// + /// 安全解析字符串为枚举,失败返回默认值 + /// + public static T ParseEnumOrDefault(this string value, T defaultValue = default) where T : struct, Enum + { + if (Enum.TryParse(value, true, out var result)) + return result; + + return defaultValue; + } + + + #endregion + + #region 枚举字典扩展 + + /// + /// 获取枚举类型的所有成员的名称和值的键值对 + /// + public static IDictionary ToNameDictionary() where T : struct, Enum + { + var valuesDictionary = new Dictionary(); + foreach (T value in GetValues()) + { + valuesDictionary.Add(Enum.GetName(typeof(T), value)!, value); + } + return valuesDictionary; + } + + /// + /// 获取指定枚举值的描述 + /// + public static string? GetDescriptionByValue(T value) where T : struct, Enum + { + var name = Enum.GetName(typeof(T), value); + if (string.IsNullOrEmpty(name)) + { + return null; + } + var field = typeof(T).GetField(name); + var attr = field?.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; + return attr?.Description; + } + + /// + /// 获取指定枚举值的显示名称 + /// + public static string? GetDisplayNameByValue(T value) where T : struct, Enum + { + var name = Enum.GetName(typeof(T), value); + if (string.IsNullOrEmpty(name)) + { + return null; + } + var field = typeof(T).GetField(name); + var attr = field?.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.DisplayAttribute; + return attr?.GetName(); + } + + #endregion + + #region 枚举判断 + + /// + /// 获取枚举类型的所有值 + /// + public static T[] GetValues() where T : struct, Enum + { + return (T[])Enum.GetValues(typeof(T)); + } + + /// + /// 获取枚举类型的所有名称 + /// + public static string[] GetNames() where T : struct, Enum + { + return Enum.GetNames(typeof(T)); + } + + /// + /// 获取枚举类型的所有值和描述 + /// + public static Dictionary ToDictionary() where T : struct, Enum + { + var dictionary = new Dictionary(); + foreach (T value in GetValues()) + { + dictionary[value] = value.GetDescription(); + } + return dictionary; + } + + /// + /// 获取枚举类型的所有值和显示名称 + /// + public static Dictionary ToDisplayNameDictionary() where T : struct, Enum + { + var dictionary = new Dictionary(); + foreach (T value in GetValues()) + { + dictionary[value] = value.GetDisplayName(); + } + return dictionary; + } + + #endregion + + #region 枚举判断 + + + /// + /// 判断字符串是否是有效的枚举名称或值 + /// + public static bool IsEnumDefined(this string value) where T : struct, Enum + { + return Enum.IsDefined(typeof(T), value); + } + + /// + /// 判断枚举值是否有指定标记 + /// + public static bool HasFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + return (valueInt & flagInt) == flagInt; + } + + /// + /// 设置枚举标记 + /// + public static T SetFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt | flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + /// + /// 清除枚举标记 + /// + public static T ClearFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt & ~flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + /// + /// 切换枚举标记 + /// + public static T ToggleFlag(this T value, T flag) where T : struct, Enum + { + var valueInt = Convert.ToInt64(value); + var flagInt = Convert.ToInt64(flag); + var result = valueInt ^ flagInt; + return (T)Enum.ToObject(typeof(T), result); + } + + #endregion + + #region 下一个/上一个值 + + /// + /// 获取下一个枚举值 + /// + public static T Next(this T value) where T : struct, Enum + { + var values = GetValues(); + int index = Array.IndexOf(values, value); + if (index < 0 || index >= values.Length - 1) + return value; + + return values[index + 1]; + } + + /// + /// 获取上一个枚举值 + /// + public static T Previous(this T value) where T : struct, Enum + { + var values = GetValues(); + int index = Array.IndexOf(values, value); + if (index <= 0) + return value; + + return values[index - 1]; + } + + /// + /// 获取第一个枚举值 + /// + public static T First() where T : struct, Enum + { + var values = GetValues(); + return values[0]; + } + + /// + /// 获取最后一个枚举值 + /// + public static T Last() where T : struct, Enum + { + var values = GetValues(); + return values[values.Length - 1]; + } + + #endregion + + #region 随机值 + + /// + /// 获取随机枚举值 + /// + public static T Random() where T : struct, Enum + { + var values = GetValues(); + var random = new System.Random(); + int index = random.Next(values.Length); + return values[index]; + } + + #endregion + + #region Flags 操作 + + /// + /// 获取 Flags 枚举的所有设置值 + /// + public static IEnumerable GetFlags(this T value) where T : struct, Enum + { + var values = GetValues(); + var valueInt = Convert.ToInt64(value); + + foreach (T flag in values) + { + var flagInt = Convert.ToInt64(flag); + if (flagInt == 0) + continue; + + if ((valueInt & flagInt) == flagInt) + yield return flag; + } + } + + /// + /// 判断是否是 Flags 枚举 + /// + public static bool IsFlagsEnum() where T : struct, Enum + { + return typeof(T).IsDefined(typeof(FlagsAttribute), false); + } + + #endregion + + #region 下拉框/选择列表 + + /// + /// 获取枚举的所有选项(用于下拉框等) + /// + public static List> GetItems() where T : struct, Enum + { + var items = new List>(); + foreach (T value in GetValues()) + { + items.Add(new EnumItem + { + Value = value, + Name = value.ToString(), + Description = value.GetDescription(), + DisplayName = value.GetDisplayName() + }); + } + return items; + } + + #endregion + } + + /// + /// 枚举项 + /// + public class EnumItem where T : struct, Enum + { + /// + /// 枚举值 + /// + public T Value { get; set; } + + /// + /// 名称 + /// + public string? Name { get; set; } + + /// + /// 描述 + /// + public string? Description { get; set; } + + /// + /// 显示名称 + /// + public string? DisplayName { get; set; } + } +} diff --git a/EasyTool.Core/ToolCategory/EnumUtil.cs b/EasyTool.Core/ToolCategory/EnumUtil.cs deleted file mode 100644 index 0079cff..0000000 --- a/EasyTool.Core/ToolCategory/EnumUtil.cs +++ /dev/null @@ -1,239 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// 枚举工具类,包含了各种枚举操作的方法 - /// - public class EnumUtil - { - /// - /// 获取指定枚举类型的所有成员名称 - /// - /// 要获取成员名称的枚举类型 - /// 所有成员名称的字符串数组 - public static string[] GetNames() - { - return Enum.GetNames(typeof(TEnum)); - } - - /// - /// 获取指定枚举类型的所有成员的值 - /// - /// 要获取成员值的枚举类型 - /// 所有成员值的数组 - public static TEnum[] GetValues() - { - return (TEnum[])Enum.GetValues(typeof(TEnum)); - } - - /// - /// 获取指定枚举值的名称 - /// - /// 枚举类型 - /// 枚举值 - /// 枚举值的名称 - public static string GetName(TEnum value) - { - return Enum.GetName(typeof(TEnum), value); - } - - /// - /// 检查指定的值是否是枚举类型TEnum的成员 - /// - /// 枚举类型 - /// 要检查的值 - /// 如果指定的值是TEnum的成员,则为true;否则为false - public static bool IsDefined(object value) - { - return Enum.IsDefined(typeof(TEnum), value); - } - - /// - /// 将字符串转换为枚举类型TEnum的值 - /// - /// 枚举类型 - /// 要转换的字符串 - /// 与字符串对应的枚举值 - public static TEnum Parse(string value) - { - return (TEnum)Enum.Parse(typeof(TEnum), value); - } - - /// - /// 将字符串转换为枚举类型TEnum的值如果字符串无法转换,则返回默认值 - /// - /// 枚举类型 - /// 要转换的字符串 - /// 默认值 - /// 与字符串对应的枚举值,或默认值(如果字符串无法转换) - public static TEnum Parse(string value, TEnum defaultValue) - { - var result= Enum.Parse(typeof(TEnum), value); - return result!=null ? (TEnum)result : defaultValue; - } - - /// - /// 获取指定枚举类型的Type对象 - /// - /// 枚举类型 - /// 枚举类型的Type对象 - public static Type GetEnumType() - { - return typeof(TEnum); - } - - /// - /// 获取指定枚举类型的所有成员的名称和值的键值对 - /// - /// 枚举类型 - /// 所有成员名称和值的键值对 - public static IDictionary GetValuesDictionary() - { - var valuesDictionary = new Dictionary(); - foreach (var value in GetValues()) - { - valuesDictionary.Add(GetName(value), value); - } - return valuesDictionary; - } - - /// - /// 获取指定枚举类型的所有成员的注释 - /// - /// 枚举类型 - /// 所有成员注释的字典,其中键是成员名称,值是注释内容 - public static IDictionary GetDescriptions() - { - var descriptions = new Dictionary(); - var enumType = GetEnumType(); - foreach (var memberInfo in enumType.GetMembers()) - { - var attribute = memberInfo.GetCustomAttribute(); - if (attribute != null) - { - descriptions.Add(memberInfo.Name, attribute.Description); - } - } - return descriptions; - } - - /// - /// 获取指定枚举类型的指定成员的注释 - /// - /// 枚举类型 - /// 枚举成员 - /// 成员注释的字符串 - public static string GetDescription(TEnum value) - { - var memberInfo = GetEnumType().GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo == null) - { - return string.Empty; - } - - var attribute = memberInfo.GetCustomAttribute(); - return attribute != null ? attribute.Description : string.Empty; - } - - /// - /// 获取指定枚举类型的指定成员的Display名称 - /// - /// 枚举类型 - /// 枚举成员 - /// 成员的Display名称,如果未设置,则返回枚举成员的名称 - public static string GetDisplayName(TEnum value) - { - var memberInfo = GetEnumType().GetMember(value.ToString()).FirstOrDefault(); - if (memberInfo == null) - { - return string.Empty; - } - - var attribute = memberInfo.GetCustomAttribute(); - return attribute != null ? attribute.Name : value.ToString(); - } - - /// - /// 获取指定枚举类型的所有成员的Display名称 - /// - /// 枚举类型 - /// 所有成员的Display名称的字典,其中键是成员名称,值是Display名称 - public static IDictionary GetDisplayNames() - { - var displayNames = new Dictionary(); - var enumType = GetEnumType(); - foreach (var memberInfo in enumType.GetMembers()) - { - var attribute = memberInfo.GetCustomAttribute(); - if (attribute != null) - { - displayNames.Add(memberInfo.Name, attribute.Name); - } - } - return displayNames; - } - - /// - /// 获取指定枚举类型的指定成员的值 - /// - /// 枚举类型 - /// 成员名称 - /// 成员的值,如果成员不存在,则返回默认值 - public static TEnum GetValueByName(string name) - { - return Parse(name, default(TEnum)); - } - - /// - /// 获取指定枚举类型的指定值的名称 - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的名称,如果值不存在,则返回null - public static string GetNameByValue(TEnum value) - { - return Enum.GetName(typeof(TEnum), value); - } - - /// - /// 获取指定枚举类型的指定值的注释 - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的注释,如果值不存在或未设置注释,则返回null - public static string GetDescriptionByValue(TEnum value) - { - var name = GetNameByValue(value); - if (string.IsNullOrEmpty(name)) - { - return null; - } - - return GetDescription(GetValueByName(name)); - } - - /// - /// 获取指定枚举类型的指定值的Display名称 - /// - /// 枚举类型 - /// 枚举值 - /// 与值对应的Display名称,如果值不存在或未设置Display名称,则返回null - public static string GetDisplayNameByValue(TEnum value) - { - var name = GetNameByValue(value); - if (string.IsNullOrEmpty(name)) - { - return null; - } - - return GetDisplayName(GetValueByName(name)); - } - } -} diff --git a/EasyTool.Core/ToolCategory/EnvUtil.cs b/EasyTool.Core/ToolCategory/EnvUtil.cs deleted file mode 100644 index 54932cf..0000000 --- a/EasyTool.Core/ToolCategory/EnvUtil.cs +++ /dev/null @@ -1,289 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Net.Sockets; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; - -namespace EasyTool -{ - /// - /// 环境工具 - /// - public class EnvUtil - { - /// - /// 获取系统信息 - /// - /// 系统信息字符串 - public static string GetSystemInfo() - { - StringBuilder sb = new StringBuilder(); - sb.AppendLine("操作系统版本:" + Environment.OSVersion.ToString()); - sb.AppendLine("系统位数:" + (Environment.Is64BitOperatingSystem ? "64 位" : "32 位")); - sb.AppendLine("系统目录:" + Environment.SystemDirectory); - sb.AppendLine("处理器数量:" + Environment.ProcessorCount); - sb.AppendLine("计算机名:" + Environment.MachineName); - sb.AppendLine("用户名:" + Environment.UserName); - sb.AppendLine("用户域名:" + Environment.UserDomainName); - sb.AppendLine("当前目录:" + Environment.CurrentDirectory); - sb.AppendLine("CLR版本:" + Environment.Version.ToString()); - return sb.ToString(); - } - - /// - /// 获取环境变量值 - /// - /// 环境变量名称 - /// 环境变量值 - public static string GetEnvironmentVariable(string name) - { - return Environment.GetEnvironmentVariable(name); - } - - /// - /// 设置环境变量值 - /// - /// 环境变量名称 - /// 环境变量值 - public static void SetEnvironmentVariable(string name, string value) - { - Environment.SetEnvironmentVariable(name, value); - } - - /// - /// 获取环境变量列表 - /// - /// 环境变量列表 - public static IDictionary GetEnvironmentVariables() - { - IDictionary variables = new Dictionary(); - foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) - { - variables.Add(de.Key.ToString(), de.Value.ToString()); - } - return variables; - } - - /// - /// 获取当前目录下的文件列表 - /// - /// 当前目录下的文件列表 - public static List GetFilesInCurrentDirectory() - { - List files = new List(); - foreach (string file in Directory.GetFiles(Environment.CurrentDirectory)) - { - files.Add(file); - } - return files; - } - - /// - /// 获取指定目录下的文件列表 - /// - /// 指定目录路径 - /// 指定目录下的文件列表 - public static List GetFilesInDirectory(string path) - { - List files = new List(); - foreach (string file in Directory.GetFiles(path)) - { - files.Add(file); - } - return files; - } - - /// - /// 获取当前目录下的目录列表 - /// - /// 当前目录下的目录列表 - public static List GetDirectoriesInCurrentDirectory() - { - List directories = new List(); - foreach (string directory in Directory.GetDirectories(Environment.CurrentDirectory)) - { - directories.Add(directory); - } - return directories; - } - - /// - /// 获取指定目录下的目录列表 - /// - /// 指定目录路径 - /// 指定目录下的目录列表 - public static List GetDirectoriesInDirectory(string path) - { - List directories = new List(); - foreach (string directory in Directory.GetDirectories(path)) - { - directories.Add(directory); - } - return directories; - } - - /// - /// 创建文件 - /// - /// 文件路径 - public static void CreateFile(string path) - { - File.Create(path); - } - - /// - /// 删除文件 - /// - /// 文件路径 - public static void DeleteFile(string path) - { - File.Delete(path); - } - - /// - /// 创建目录 - /// - /// 目录路径 - public static void CreateDirectory(string path) - { - Directory.CreateDirectory(path); - } - - /// - /// 删除目录 - /// - /// 目录路径 - public static void DeleteDirectory(string path) - { - Directory.Delete(path, true); - } - - /// - /// 检查目录是否存在 - /// - /// 目录路径 - /// 目录是否存在 - public static bool DirectoryExists(string path) - { - return Directory.Exists(path); - } - - /// - /// 检查文件是否存在 - /// - /// 文件路径 - /// 文件是否存在 - public static bool FileExists(string path) - { - return File.Exists(path); - } - - /// - /// 获取文件大小 - /// - /// 文件路径 - /// 文件大小(字节) - public static long GetFileSize(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.Length; - } - - /// - /// 获取文件的创建时间 - /// - /// 文件路径 - /// 文件的创建时间 - public static DateTime GetFileCreationTime(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.CreationTime; - } - - /// - /// 获取文件的修改时间 - /// - /// 文件路径 - /// 文件的修改时间 - public static DateTime GetFileLastWriteTime(string path) - { - FileInfo fileInfo = new FileInfo(path); - return fileInfo.LastWriteTime; - } - - /// - /// 复制文件 - /// - /// 源文件路径 - /// 目标文件路径 - /// 是否覆盖已有文件 - public static void CopyFile(string sourcePath, string destinationPath, bool overwrite) - { - File.Copy(sourcePath, destinationPath, overwrite); - } - - /// - /// 移动文件 - /// - /// 源文件路径 - /// 目标文件路径 - public static void MoveFile(string sourcePath, string destinationPath) - { - File.Move(sourcePath, destinationPath); - } - - /// - /// 获取网络时间 - /// - /// 网络时间 - public static DateTime GetNetworkTime() - { - const string ntpServer = "time.windows.com"; - byte[] ntpData = new byte[48]; - ntpData[0] = 0x1B; - IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList; - IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123); - using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) - { - socket.Connect(ipEndPoint); - socket.Send(ntpData); - socket.Receive(ntpData); - } - const byte offsetTransmitTime = 40; - ulong intPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime); - ulong fractPart = BitConverter.ToUInt32(ntpData, offsetTransmitTime + 4); - ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); - return new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds).ToLocalTime(); - } - - /// - /// 判断当前系统是否为Windows操作系统 - /// - /// 当前系统是否为Windows操作系统 - public static bool IsWindows() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - } - - /// - /// 判断当前系统是否为Linux操作系统 - /// - /// 当前系统是否为Linux操作系统 - public static bool IsLinux() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); - } - - /// - /// 判断当前系统是否为macOS操作系统 - /// - /// 当前系统是否为macOS操作系统 - public static bool IsMacOS() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - } - } -} diff --git a/EasyTool.Core/ToolCategory/EscapeUtil.cs b/EasyTool.Core/ToolCategory/EscapeUtil.cs deleted file mode 100644 index 7d030c5..0000000 --- a/EasyTool.Core/ToolCategory/EscapeUtil.cs +++ /dev/null @@ -1,201 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; - -namespace EasyTool -{ - /// - /// 转义和反转义工具类 - /// - public class EscapeUtil - { - /// - /// 将字符串中的特殊字符进行转义 - /// - /// 需要转义的字符串 - /// 转义后的字符串 - public static string Escape(string str) - { - if (string.IsNullOrEmpty(str)) - { - return str; - } - - string escaped = Regex.Replace(str, @"[\a\b\f\n\r\t\v\\""]", m => { - switch (m.Value) - { - case "\a": - return @"\a"; - case "\b": - return @"\b"; - case "\f": - return @"\f"; - case "\n": - return @"\n"; - case "\r": - return @"\r"; - case "\t": - return @"\t"; - case "\v": - return @"\v"; - case "\\": - return @"\\"; - case "\"": - return @"\"""; - default: - return m.Value; - } - }); - - return escaped; - } - - /// - /// 将字符串中的转义字符还原成特殊字符 - /// - /// 需要还原的字符串 - /// 还原后的字符串 - public static string Unescape(string str) - { - if (string.IsNullOrEmpty(str)) - { - return str; - } - - string unescaped = Regex.Replace(str, @"\\[a-z""\\]", m => { - switch (m.Value) - { - case @"\a": - return "\a"; - case @"\b": - return "\b"; - case @"\f": - return "\f"; - case @"\n": - return "\n"; - case @"\r": - return "\r"; - case @"\t": - return "\t"; - case @"\v": - return "\v"; - case @"\\": - return "\\"; - case @"\""": - return "\""; - default: - return m.Value; - } - }); - - return unescaped; - } - - /// - /// 将URL中的特殊字符进行转义 - /// - /// 需要转义的URL - /// 转义后的URL - public static string UrlEncode(string url) - { - if (string.IsNullOrEmpty(url)) - { - return url; - } - - return Uri.EscapeDataString(url); - } - - /// - /// 将URL中的转义字符还原成特殊字符 - /// - /// 需要还原的URL - /// 还原后的URL - public static string UrlDecode(string url) - { - if (string.IsNullOrEmpty(url)) - { - return url; - } - - return Uri.UnescapeDataString(url); - } - - /// - /// 将HTML字符串进行转义,将特殊字符替换成HTML实体 - /// - /// 需要转义的HTML字符串 - /// 转义后的HTML字符串 - public static string HtmlEncode(string html) - { - if (string.IsNullOrEmpty(html)) - { - return html; - } - - return System.Net.WebUtility.HtmlEncode(html); - } - - /// - /// 将HTML字符串中的HTML实体还原成特殊字符 - /// - /// 需要还原的HTML字符串 - /// 还原后的HTML字符串 - public static string HtmlDecode(string html) - { - if (string.IsNullOrEmpty(html)) - { - return html; - } - - return System.Net.WebUtility.HtmlDecode(html); - } - - /// - /// 将XML字符串进行转义,将特殊字符替换成XML实体 - /// - /// 需要转义的XML字符串 - /// 转义后的XML字符串 - public static string XmlEncode(string xml) - { - if (string.IsNullOrEmpty(xml)) - { - return xml; - } - - return System.Security.SecurityElement.Escape(xml); - } - - /// - /// 将XML字符串中的XML实体还原成特殊字符 - /// - /// 需要还原的XML字符串 - /// 还原后的XML字符串 - public static string XmlDecode(string xml) - { - if (string.IsNullOrEmpty(xml)) - { - return xml; - } - - return Regex.Replace(xml, @"&[a-zA-Z]+;", m => { - switch (m.Value) - { - case "&": - return "&"; - case "<": - return "<"; - case ">": - return ">"; - case """: - return "\""; - case "'": - return "'"; - default: - return m.Value; - } - }); - } - } -} diff --git a/EasyTool.Core/ToolCategory/EventArgsUtil.cs b/EasyTool.Core/ToolCategory/EventArgsUtil.cs new file mode 100644 index 0000000..501257c --- /dev/null +++ b/EasyTool.Core/ToolCategory/EventArgsUtil.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 弱事件订阅器 + /// 避免内存泄漏 + /// + public class WeakEvent where TEventArgs : EventArgs + { + private readonly List _handlers = new(); + private readonly object _lock = new(); + + /// + /// 订阅 + /// + public void Subscribe(EventHandler handler) + { + lock (_lock) + { + _handlers.Add(new WeakReference(handler)); + } + } + + /// + /// 取消订阅 + /// + public void Unsubscribe(EventHandler handler) + { + lock (_lock) + { + for (int i = _handlers.Count - 1; i >= 0; i--) + { + if (_handlers[i].Target is EventHandler existing && + existing == handler) + { + _handlers.RemoveAt(i); + } + } + } + } + + /// + /// 触发事件 + /// + public void Raise(object sender, TEventArgs args) + { + List?> handlers; + + lock (_lock) + { + handlers = _handlers + .Where(w => w.IsAlive) + .Select(w => w.Target as EventHandler) + .ToList(); + } + + foreach (var handler in handlers) + { + handler?.Invoke(sender, args); + } + } + + /// + /// 清理无效引用 + /// + public void Cleanup() + { + lock (_lock) + { + for (int i = _handlers.Count - 1; i >= 0; i--) + { + if (!_handlers[i].IsAlive) + { + _handlers.RemoveAt(i); + } + } + } + } + } + + /// + /// 属性变更事件参数 + /// + public class PropertyChangedEventArgs : EventArgs + { + /// + /// 属性名 + /// + public string PropertyName { get; } + + /// + /// 旧值 + /// + public object? OldValue { get; } + + /// + /// 新值 + /// + public object? NewValue { get; } + + /// + /// 创建属性变更事件参数 + /// + public PropertyChangedEventArgs(string propertyName, object? oldValue, object? newValue) + { + PropertyName = propertyName; + OldValue = oldValue; + NewValue = newValue; + } + } + + /// + /// 可观察对象 + /// + public class ObservableObject + { + private readonly Dictionary _properties = new(); + + /// + /// 属性变更事件 + /// + public event EventHandler? PropertyChanged; + + /// + /// 获取属性值 + /// + protected T? GetProperty(string name, T? defaultValue = default) + { + return _properties.TryGetValue(name, out var value) ? (T?)value : defaultValue; + } + + /// + /// 设置属性值 + /// + protected bool SetProperty(string name, T? value) + { + var oldValue = GetProperty(name); + + if (EqualityComparer.Default.Equals(oldValue, value)) + return false; + + _properties[name] = value; + OnPropertyChanged(name, oldValue, value); + return true; + } + + /// + /// 触发属性变更 + /// + protected virtual void OnPropertyChanged(string propertyName, object? oldValue, object? newValue) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName, oldValue, newValue)); + } + } +} diff --git a/EasyTool.Core/ToolCategory/EventBus.cs b/EasyTool.Core/ToolCategory/EventBus.cs new file mode 100644 index 0000000..55f9b5a --- /dev/null +++ b/EasyTool.Core/ToolCategory/EventBus.cs @@ -0,0 +1,252 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 事件总线 + /// 提供发布/订阅模式的实现 + /// + public static class EventBus + { + private static readonly Dictionary> _handlers = new(); + private static readonly object _lock = new(); + + /// + /// 订阅事件 + /// + public static void Subscribe(Action handler) + { + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers = new List(); + _handlers[typeof(T)] = handlers; + } + handlers.Add(handler); + } + } + + /// + /// 取消订阅 + /// + public static void Unsubscribe(Action handler) + { + lock (_lock) + { + if (_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers.Remove(handler); + } + } + } + + /// + /// 发布事件 + /// + public static void Publish(T eventData) + { + List? handlersCopy; + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + return; + handlersCopy = new List(handlers); + } + + foreach (var handler in handlersCopy) + { + if (handler is Action typedHandler) + { + typedHandler(eventData); + } + } + } + + /// + /// 异步发布事件 + /// + public static async Task PublishAsync(T eventData) + { + List? handlersCopy; + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + return; + handlersCopy = new List(handlers); + } + + var tasks = new List(); + foreach (var handler in handlersCopy) + { + if (handler is Action typedHandler) + { + tasks.Add(Task.Run(() => typedHandler(eventData))); + } + else if (handler is Func asyncHandler) + { + tasks.Add(asyncHandler(eventData)); + } + } + + await Task.WhenAll(tasks); + } + + /// + /// 订阅异步事件 + /// + public static void SubscribeAsync(Func handler) + { + lock (_lock) + { + if (!_handlers.TryGetValue(typeof(T), out var handlers)) + { + handlers = new List(); + _handlers[typeof(T)] = handlers; + } + handlers.Add(handler); + } + } + + /// + /// 清除所有订阅 + /// + public static void Clear() + { + lock (_lock) + { + _handlers.Clear(); + } + } + + /// + /// 清除指定类型的订阅 + /// + public static void Clear() + { + lock (_lock) + { + _handlers.Remove(typeof(T)); + } + } + } + + /// + /// 泛型事件总线 + /// + public class EventBus where T : class + { + private static readonly EventBus _instance = new(); + private readonly List> _handlers = new(); + private readonly List> _asyncHandlers = new(); + private readonly object _lock = new(); + + /// + /// 获取单例实例 + /// + public static EventBus Instance => _instance; + + /// + /// 订阅 + /// + public void Subscribe(Action handler) + { + lock (_lock) + { + _handlers.Add(handler); + } + } + + /// + /// 取消订阅 + /// + public void Unsubscribe(Action handler) + { + lock (_lock) + { + _handlers.Remove(handler); + } + } + + /// + /// 异步订阅 + /// + public void SubscribeAsync(Func handler) + { + lock (_lock) + { + _asyncHandlers.Add(handler); + } + } + + /// + /// 取消异步订阅 + /// + public void UnsubscribeAsync(Func handler) + { + lock (_lock) + { + _asyncHandlers.Remove(handler); + } + } + + /// + /// 发布 + /// + public void Publish(T eventData) + { + List> handlersCopy; + lock (_lock) + { + handlersCopy = new List>(_handlers); + } + + foreach (var handler in handlersCopy) + { + handler(eventData); + } + } + + /// + /// 异步发布 + /// + public async Task PublishAsync(T eventData) + { + List> handlersCopy; + List> asyncHandlersCopy; + + lock (_lock) + { + handlersCopy = new List>(_handlers); + asyncHandlersCopy = new List>(_asyncHandlers); + } + + var tasks = new List(); + foreach (var handler in handlersCopy) + { + tasks.Add(Task.Run(() => handler(eventData))); + } + + foreach (var handler in asyncHandlersCopy) + { + tasks.Add(handler(eventData)); + } + + await Task.WhenAll(tasks); + } + + /// + /// 清除所有订阅 + /// + public void Clear() + { + lock (_lock) + { + _handlers.Clear(); + _asyncHandlers.Clear(); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/ExceptionExtension.cs b/EasyTool.Core/ToolCategory/ExceptionExtension.cs new file mode 100644 index 0000000..325b319 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ExceptionExtension.cs @@ -0,0 +1,326 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// Exception 异常扩展方法 + /// + public static class ExceptionExtension + { + #region 消息获取 + + /// + /// 获取完整的异常消息(包含所有内层异常) + /// + public static string GetFullMessage(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + var current = exception; + + int depth = 0; + while (current != null) + { + if (depth > 0) + sb.Append("Inner Exception: "); + + sb.AppendLine(current.Message); + current = current.InnerException; + depth++; + + // 防止无限循环 + if (depth > 100) + break; + } + + return sb.ToString().Trim(); + } + + /// + /// 获取所有异常(包含内层异常) + /// + public static Exception[] GetAllExceptions(this Exception? exception) + { + if (exception == null) + return Array.Empty(); + + var exceptions = new List(); + var current = exception; + + int depth = 0; + while (current != null) + { + exceptions.Add(current); + current = current.InnerException; + depth++; + + // 防止无限循环 + if (depth > 100) + break; + } + + return exceptions.ToArray(); + } + + /// + /// 获取所有内层异常 + /// + public static Exception[] GetInnerExceptions(this Exception exception) + { + var all = exception.GetAllExceptions(); + return all.Skip(1).ToArray(); + } + + #endregion + + #region 详细信息 + + /// + /// 获取异常的详细字符串表示 + /// + public static string ToDetailedString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + + sb.AppendLine($"Exception Type: {exception.GetType().FullName}"); + sb.AppendLine($"Message: {exception.Message}"); + sb.AppendLine($"Source: {exception.Source ?? "(unknown)"}"); + + if (exception.TargetSite != null) + { + sb.AppendLine($"Target Site: {exception.TargetSite}"); + } + + if (!string.IsNullOrEmpty(exception.HelpLink)) + { + sb.AppendLine($"Help Link: {exception.HelpLink}"); + } + + if (exception.Data != null && exception.Data.Count > 0) + { + sb.AppendLine("Data:"); + foreach (var key in exception.Data.Keys) + { + sb.AppendLine($" {key}: {exception.Data[key]}"); + } + } + + if (!string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine("Stack Trace:"); + sb.AppendLine(exception.StackTrace); + } + + // 处理内层异常 + if (exception.InnerException != null) + { + sb.AppendLine(); + sb.AppendLine("--- Inner Exception ---"); + sb.Append(exception.InnerException.ToDetailedString()); + } + + return sb.ToString(); + } + + /// + /// 获取异常的简略字符串表示 + /// + public static string ToSimpleString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + return $"{exception.GetType().Name}: {exception.Message}"; + } + + #endregion + + #region 异常类型判断 + + /// + /// 判断异常是否是指定类型 + /// + public static bool IsType(this Exception? exception) where T : Exception + { + return exception is T; + } + + /// + /// 判断异常或其内层异常是否是指定类型 + /// + public static bool IsOrContainsType(this Exception? exception) where T : Exception + { + var current = exception; + + while (current != null) + { + if (current is T) + return true; + + current = current.InnerException; + } + + return false; + } + + /// + /// 查找第一个指定类型的异常 + /// + public static T? FindType(this Exception? exception) where T : Exception + { + var current = exception; + + while (current != null) + { + if (current is T typedException) + return typedException; + + current = current.InnerException; + } + + return null; + } + + #endregion + + #region 特定异常处理 + + /// + /// 判断是否是超时异常 + /// + public static bool IsTimeout(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是取消操作异常 + /// + public static bool IsOperationCanceled(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是参数异常 + /// + public static bool IsArgumentException(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + /// + /// 判断是否是空引用异常 + /// + public static bool IsNullReference(this Exception? exception) + { + return exception is NullReferenceException; + } + + /// + /// 判断是否是 IO 异常 + /// + public static bool IsIOException(this Exception? exception) + { + return exception.IsOrContainsType(); + } + + #endregion + + #region 异常包装 + + /// + /// 使用指定消息包装异常 + /// + public static Exception WrapWith(this Exception? exception, string message) + { + return new Exception(message, exception); + } + + /// + /// 使用指定类型包装异常 + /// + public static TException WrapWith(this Exception? exception, string message) where TException : Exception + { + return (TException)Activator.CreateInstance(typeof(TException), message, exception)!; + } + + #endregion + + #region 日志格式化 + + /// + /// 获取适合日志记录的异常信息 + /// + public static string ToLogString(this Exception? exception, bool includeStackTrace = true) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + + sb.Append($"[{exception.GetType().Name}] "); + sb.AppendLine(exception.Message); + + if (includeStackTrace && !string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine(exception.StackTrace.Trim()); + } + + return sb.ToString(); + } + + /// + /// 获取单行格式的异常信息 + /// + public static string ToOneLineString(this Exception? exception) + { + if (exception == null) + return string.Empty; + + var sb = new StringBuilder(); + var current = exception; + + while (current != null) + { + if (sb.Length > 0) + sb.Append(" -> "); + + sb.Append($"[{current.GetType().Name}] {current.Message}"); + current = current.InnerException; + + // 防止无限循环 + if (sb.Length > 1000) + break; + } + + return sb.ToString(); + } + + #endregion + + #region 聚合异常处理 + + /// + /// 获取聚合异常中的所有异常 + /// + public static Exception[] GetInnerExceptions(this AggregateException? exception) + { + if (exception == null) + return Array.Empty(); + + return exception.InnerExceptions.ToArray(); + } + + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/GuardUtil.cs b/EasyTool.Core/ToolCategory/GuardUtil.cs new file mode 100644 index 0000000..8230fb4 --- /dev/null +++ b/EasyTool.Core/ToolCategory/GuardUtil.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 防御性编程工具类 + /// 提供参数验证和断言功能 + /// + public static class GuardUtil + { + /// + /// 验证参数不为null + /// + public static T NotNull(T? value, string paramName) where T : class + { + if (value == null) + throw new ArgumentNullException(paramName); + return value; + } + + /// + /// 验证可空值类型不为null + /// + public static T NotNull(T? value, string paramName) where T : struct + { + if (value == null) + throw new ArgumentNullException(paramName); + return value.Value; + } + + /// + /// 验证字符串不为空或null + /// + public static string NotNullOrEmpty(string? value, string paramName) + { + if (string.IsNullOrEmpty(value)) + throw new ArgumentException("字符串不能为空或null", paramName); + return value; + } + + /// + /// 验证字符串不为空白 + /// + public static string NotNullOrWhiteSpace(string? value, string paramName) + { + if (string.IsNullOrWhiteSpace(value)) + throw new ArgumentException("字符串不能为空白", paramName); + return value; + } + + /// + /// 验证集合不为空 + /// + public static IEnumerable NotEmpty(IEnumerable? value, string paramName) + { + if (value == null) + throw new ArgumentNullException(paramName); + + var collection = value as ICollection ?? new List(value); + if (collection.Count == 0) + throw new ArgumentException("集合不能为空", paramName); + + return collection; + } + + /// + /// 验证范围 + /// + public static int InRange(int value, int min, int max, string paramName) + { + if (value < min || value > max) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须在 {min} 和 {max} 之间"); + return value; + } + + /// + /// 验证范围 + /// + public static double InRange(double value, double min, double max, string paramName) + { + if (value < min || value > max) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须在 {min} 和 {max} 之间"); + return value; + } + + /// + /// 验证大于指定值 + /// + public static int GreaterThan(int value, int threshold, string paramName) + { + if (value <= threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须大于 {threshold}"); + return value; + } + + /// + /// 验证大于等于指定值 + /// + public static int GreaterThanOrEqual(int value, int threshold, string paramName) + { + if (value < threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须大于或等于 {threshold}"); + return value; + } + + /// + /// 验证小于指定值 + /// + public static int LessThan(int value, int threshold, string paramName) + { + if (value >= threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须小于 {threshold}"); + return value; + } + + /// + /// 验证小于等于指定值 + /// + public static int LessThanOrEqual(int value, int threshold, string paramName) + { + if (value > threshold) + throw new ArgumentOutOfRangeException(paramName, value, $"值必须小于或等于 {threshold}"); + return value; + } + + /// + /// 验证条件为真 + /// + public static void IsTrue(bool condition, string message, string? paramName = null) + { + if (!condition) + throw new ArgumentException(message, paramName); + } + + /// + /// 验证条件为假 + /// + public static void IsFalse(bool condition, string message, string? paramName = null) + { + if (condition) + throw new ArgumentException(message, paramName); + } + + /// + /// 验证类型 + /// + public static T IsType(object value, string paramName) + { + if (value is not T typed) + throw new ArgumentException($"值必须是 {typeof(T).Name} 类型", paramName); + return typed; + } + + /// + /// 验证枚举值有效 + /// + public static T EnumDefined(T value, string paramName) where T : struct, Enum + { + if (!Enum.IsDefined(typeof(T), value)) + throw new ArgumentException($"无效的枚举值: {value}", paramName); + return value; + } + + /// + /// 验证邮箱格式 + /// + public static string Email(string? value, string paramName) + { + NotNullOrEmpty(value, paramName); + if (!System.Text.RegularExpressions.Regex.IsMatch(value!, @"^[^@\s]+@[^@\s]+\.[^@\s]+$")) + throw new ArgumentException("无效的邮箱格式", paramName); + return value!; + } + + /// + /// 验证文件存在 + /// + public static string FileExists(string? path, string paramName) + { + NotNullOrEmpty(path, paramName); + if (!System.IO.File.Exists(path)) + throw new System.IO.FileNotFoundException($"文件不存在: {path}", path); + return path!; + } + + /// + /// 验证目录存在 + /// + public static string DirectoryExists(string? path, string paramName) + { + NotNullOrEmpty(path, paramName); + if (!System.IO.Directory.Exists(path)) + throw new System.IO.DirectoryNotFoundException($"目录不存在: {path}"); + return path!; + } + + /// + /// 抛出异常 + /// + public static void Throw(string message) where TException : Exception, new() + { + var exception = (TException?)Activator.CreateInstance(typeof(TException), message) + ?? new TException(); + throw exception; + } + + /// + /// 如果条件为真,抛出异常 + /// + public static void ThrowIf(bool condition, string message) where TException : Exception, new() + { + if (condition) + Throw(message); + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/GuidExtension.cs b/EasyTool.Core/ToolCategory/GuidExtension.cs new file mode 100644 index 0000000..656fd54 --- /dev/null +++ b/EasyTool.Core/ToolCategory/GuidExtension.cs @@ -0,0 +1,270 @@ +using System; +using System.Text; + +namespace EasyTool.ToolCategory +{ + /// + /// Guid 扩展方法 + /// + public static class GuidExtension + { + #region 空值判断 + + + /// + /// 判断 Guid 是否为空或默认值 + /// + public static bool IsNullOrEmpty(this Guid? guid) + { + return guid == null || guid.Value == Guid.Empty; + } + + /// + /// 判断 Guid 是否有值(非空) + /// + public static bool HasValue(this Guid guid) + { + return guid != Guid.Empty; + } + + /// + /// 判断可空 Guid 是否有值 + /// + public static bool HasValue(this Guid? guid) + { + return guid.HasValue && guid.Value != Guid.Empty; + } + + #endregion + + #region 格式化转换 + + /// + /// 获取短格式 Guid(不带连字符) + /// + public static string ToShortString(this Guid guid) + { + return guid.ToString("N"); + } + + /// + /// 获取短格式 Guid(带连字符) + /// + public static string ToShortStringWithDashes(this Guid guid) + { + return guid.ToString("D"); + } + + /// + /// 获取带括号的 Guid 格式 + /// + public static string ToFormattedString(this Guid guid) + { + return guid.ToString("B"); + } + + /// + /// 获取带大括号的 Guid 格式 + /// + public static string ToBracedString(this Guid guid) + { + return guid.ToString("B"); + } + + + /// + /// 将 Guid 转换为 Base64 字符串 + /// + public static string ToBase64String(this Guid guid) + { + return Convert.ToBase64String(guid.ToByteArray()); + } + + /// + /// 从 Base64 字符串创建 Guid + /// + public static Guid FromBase64String(this string base64) + { + var bytes = Convert.FromBase64String(base64); + return new Guid(bytes); + } + + #endregion + + #region 字符串解析 + + /// + /// 尝试解析字符串为 Guid,失败返回空 Guid + /// + public static Guid ToGuidOrDefault(this string value) + { + if (string.IsNullOrWhiteSpace(value)) + return Guid.Empty; + + return Guid.TryParse(value, out var guid) ? guid : Guid.Empty; + } + + /// + /// 尝试解析字符串为 Guid,失败返回默认值 + /// + public static Guid ToGuidOrDefault(this string value, Guid defaultValue) + { + if (string.IsNullOrWhiteSpace(value)) + return defaultValue; + + return Guid.TryParse(value, out var guid) ? guid : defaultValue; + } + + /// + /// 判断字符串是否是有效的 Guid 格式 + /// + public static bool IsValidGuid(this string value) + { + return !string.IsNullOrWhiteSpace(value) && Guid.TryParse(value, out _); + } + + #endregion + + #region 加密相关 + + /// + /// 获取 Guid 的 MD5 哈希值(作为新的 Guid) + /// + public static Guid ToMd5Guid(this Guid guid) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var bytes = guid.ToByteArray(); + var hash = md5.ComputeHash(bytes); + return new Guid(hash); + } + + /// + /// 获取 Guid 的 SHA1 哈希值(取前16字节作为 Guid) + /// + public static Guid ToSha1Guid(this Guid guid) + { + using var sha1 = System.Security.Cryptography.SHA1.Create(); + var bytes = guid.ToByteArray(); + var hash = sha1.ComputeHash(bytes); + var guidBytes = new byte[16]; + Array.Copy(hash, 0, guidBytes, 0, 16); + return new Guid(guidBytes); + } + + #endregion + + #region Guid 生成 + + /// + /// 基于 Guid 生成连续的 Guid(适用于 COMB 类型) + /// + public static Guid NewCombGuid() + { + var guidArray = Guid.NewGuid().ToByteArray(); + var baseDate = new DateTime(1900, 1, 1); + var now = DateTime.Now; + var days = new TimeSpan(now.Ticks - baseDate.Ticks); + var msecs = now.TimeOfDay; + + var daysArray = BitConverter.GetBytes(days.Days); + var msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333)); + + Array.Reverse(daysArray); + Array.Reverse(msecsArray); + + Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2); + Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4); + + return new Guid(guidArray); + } + + /// + /// 基于指定前缀生成可预测的 Guid + /// + public static Guid NewDeterministicGuid(string prefix) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var prefixBytes = Encoding.UTF8.GetBytes(prefix); + var hash = md5.ComputeHash(prefixBytes); + return new Guid(hash); + } + + /// + /// 基于多个参数生成可预测的 Guid + /// + public static Guid NewDeterministicGuid(params object[] values) + { + using var md5 = System.Security.Cryptography.MD5.Create(); + var combined = string.Join("|", values); + var bytes = Encoding.UTF8.GetBytes(combined); + var hash = md5.ComputeHash(bytes); + return new Guid(hash); + } + + #endregion + + #region Guid 比较 + + /// + /// 比较两个 Guid 是否相等 + /// + public static bool EqualsTo(this Guid guid, Guid other) + { + return guid.Equals(other); + } + + /// + /// 比较两个可空 Guid 是否相等 + /// + public static bool EqualsTo(this Guid? guid, Guid? other) + { + if (guid.HasValue && other.HasValue) + return guid.Value.Equals(other.Value); + return guid.HasValue == other.HasValue; + } + + #endregion + + #region Guid 操作 + + /// + /// 获取 Guid 的指定部分的值 + /// + /// Guid + /// 部分:0-3(Data1-Data4) + public static int GetPart(this Guid guid, int part) + { + var bytes = guid.ToByteArray(); + return part switch + { + 0 => BitConverter.ToInt32(bytes, 0), + 1 => BitConverter.ToInt16(bytes, 4), + 2 => BitConverter.ToInt16(bytes, 6), + 3 => bytes[8] << 24 | bytes[9] << 16 | bytes[10] << 8 | bytes[11], + _ => throw new ArgumentOutOfRangeException(nameof(part), "Part must be between 0 and 3") + }; + } + + /// + /// 将 Guid 转换为整数(用于某些场景的简化处理) + /// + public static int ToInt32(this Guid guid) + { + var bytes = guid.ToByteArray(); + return BitConverter.ToInt32(bytes, 0); + } + + /// + /// 将 Guid 转换为长整数 + /// + public static long ToInt64(this Guid guid) + { + var bytes = guid.ToByteArray(); + var high = BitConverter.ToInt64(bytes, 0); + var low = BitConverter.ToInt64(bytes, 8); + return high ^ low; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/IdcardUtil.cs b/EasyTool.Core/ToolCategory/IdcardUtil.cs deleted file mode 100644 index 015c24c..0000000 --- a/EasyTool.Core/ToolCategory/IdcardUtil.cs +++ /dev/null @@ -1,470 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; - -namespace EasyTool -{ - public class IdcardUtil - { - /// - /// 验证身份证号码是否合法 - /// - /// 身份证号码 - /// 验证结果 - public static bool IsValid(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (idcard.Length == 15) - { - return IsValid15(idcard); - } - else if (idcard.Length == 18) - { - return IsValid18(idcard); - } - else - { - return false; - } - } - - /// - /// 验证 15 位身份证号码是否合法 - /// - /// 15 位身份证号码 - /// 验证结果 - public static bool IsValid15(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (!Regex.IsMatch(idcard, @"^\d{15}$")) - { - return false; - } - - if (!IsValidArea(idcard.Substring(0, 6))) - { - return false; - } - - DateTime birthday; - if (!DateTime.TryParseExact(idcard.Substring(6, 6), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return false; - } - - return true; - } - - /// - /// 验证 18 位身份证号码是否合法 - /// - /// 18 位身份证号码 - /// 验证结果 - public static bool IsValid18(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return false; - } - - if (!Regex.IsMatch(idcard, @"^\d{17}[\dX]$")) - { - return false; - } - - if (!IsValidArea(idcard.Substring(0, 6))) - { - return false; - } - - DateTime birthday; - if (!DateTime.TryParseExact(idcard.Substring(6, 8), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return false; - } - - if (!IsValidChecksum(idcard)) - { - return false; - } - - return true; - } - - /// - /// 判断给定的区域代码是否合法 - /// - /// 区域代码 - /// 是否合法 - public static bool IsValidArea(string area) - { - if (string.IsNullOrEmpty(area)) - { - return false; - } - - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - return areas.Contains(area); - } - - /// - /// 验证身份证号码的校验位是否正确 - /// - /// 身份证号码 - /// 验证结果 - public static bool IsValidChecksum(string idcard) - { - int[] weights = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; - string[] checksums = new string[] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; - - int sum = 0; - for (int i = 0; i < 17; i++) - { - sum += int.Parse(idcard[i].ToString()) * weights[i]; - } - - int checksumIndex = sum % 11; - string expectedChecksum = checksums[checksumIndex]; - - return idcard[17].ToString().ToUpper() == expectedChecksum.ToUpper(); - } - - /// - /// 从身份证号码中获取生日 - /// - /// 身份证号码 - /// 生日 - public static DateTime? GetBirthday(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (idcard.Length == 15) - { - if (!IsValid15(idcard)) - { - return null; - } - - DateTime birthday; - if (DateTime.TryParseExact(idcard.Substring(6, 6), "yyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return birthday; - } - else - { - return null; - } - } - else if (idcard.Length == 18) - { - if (!IsValid18(idcard)) - { - return null; - } - - DateTime birthday; - if (DateTime.TryParseExact(idcard.Substring(6, 8), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out birthday)) - { - return birthday; - } - else - { - return null; - } - } - else - { - return null; - } - } - - /// - /// 从身份证号码中获取性别 - /// - /// 身份证号码 - /// 性别 - public static Gender? GetGender(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (idcard.Length == 15) - { - if (!IsValid15(idcard)) - { - return null; - } - - int genderCode = int.Parse(idcard.Substring(14, 1)); - return genderCode % 2 == 1 ? Gender.Male : Gender.Female; - } - else if (idcard.Length == 18) - { - if (!IsValid18(idcard)) - { - return null; - } - - int genderCode = int.Parse(idcard.Substring(16, 1)); - return genderCode % 2 == 1 ? Gender.Male : Gender.Female; - } - else - { - return null; - } - } - - /// - /// 从身份证号码中获取 - /// - /// 身份证号码 - /// 省份 - public static string GetProvince(string idcard) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (IsValid(idcard)) - { - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - string provinceCode = idcard.Substring(0, 2); - if (areas.Contains(provinceCode)) - { - switch (provinceCode) - { - case "11": - return "北京市"; - case "12": - return "天津市"; - case "13": - return "河北省"; - case "14": - return "山西省"; - case "15": - return "内蒙古自治区"; - case "21": - return "辽宁省"; - case "22": - return "吉林省"; - case "23": - return "黑龙江省"; - case "31": - return "上海市"; - case "32": - return "江苏省"; - case "33": - return "浙江省"; - case "34": - return "安徽省"; - case "35": - return "福建省"; - case "36": - return "江西省"; - case "37": - return "山东省"; - case "41": - return "河南省"; - case "42": - return "湖北省"; - case "43": - return "湖南省"; - case "44": - return "广东省"; - case "45": - return "广西壮族自治区"; - case "46": - return "海南省"; - case "50": - return "重庆市"; - case "51": - return "四川省"; - case "52": - return "贵州省"; - case "53": - return "云南省"; - case "54": - return "西藏自治区"; - case "61": - return "陕西省"; - case "62": - return "甘肃省"; - case "63": - return "青海省"; - case "64": - return "宁夏回族自治区"; - case "65": - return "新疆维吾尔自治区"; - default: - return null; - } - } - else - { - return null; - } - } - else - { - return null; - } - } - - /// - /// 将身份证号码中的生日部分替换成指定的日期,并返回新的身份证号码 - /// - /// 身份证号码 - /// 新的生日日期 - /// 新的身份证号码 - public static string ReplaceBirthday(string idcard, DateTime birthday) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (!IsValid(idcard)) - { - return null; - } - - string birthdayStr = birthday.ToString("yyyyMMdd"); - if (idcard.Length == 15) - { - return idcard.Substring(0, 6) + birthdayStr + idcard.Substring(12); - } - else if (idcard.Length == 18) - { - return idcard.Substring(0, 6) + birthdayStr + idcard.Substring(14); - } - else - { - return null; - } - } - - /// - /// 将身份证号码中的性别部分替换成指定的性别,并返回新的身份证号码 - /// - /// 身份证号码 - /// 新的性别 - /// 新的身份证号码 - public static string ReplaceGender(string idcard, Gender gender) - { - if (string.IsNullOrEmpty(idcard)) - { - return null; - } - - if (!IsValid(idcard)) - { - return null; - } - - int genderCode = gender == Gender.Male ? 1 : 2; - if (idcard.Length == 15) - { - return idcard.Substring(0, 14) + genderCode.ToString(); - } - else if (idcard.Length == 18) - { - return idcard.Substring(0, 16) + genderCode.ToString() + idcard.Substring(17); - } - else - { - return null; - } - } - - /// - /// 生成一个随机的身份证号码 - /// - /// 性别 - /// 最小年龄 - /// 最大年龄 - /// 随机的身份证号码 - public static string GenerateRandomIdcard(Gender gender = Gender.Male, int minAge = 18, int maxAge = 65) - { - DateTime now = DateTime.Now; - DateTime minBirthday = now.AddYears(-maxAge); - DateTime maxBirthday = now.AddYears(-minAge); - - DateTime birthday = RandomUtil.GetRandomDateTime(minBirthday, maxBirthday); - string area = GetRandomArea(); - int sequence = RandomUtil.GetRandomInt(1, 999); - int genderCode = gender == Gender.Male ? 1 : 2; - - string idcard = string.Format("{0}{1:yyyyMMdd}{2:D3}{3:D1}", area, birthday, sequence, genderCode); - if (!IsValid(idcard)) - { - return GenerateRandomIdcard(gender, minAge, maxAge); - } - else - { - return idcard; - } - } - - /// - /// 获取一个随机的身份证号码的区域代码 - /// - /// 区域代码 - private static string GetRandomArea() - { - string[] areas = new string[] { - "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", - "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", - "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", - "65" - }; - - int index = RandomUtil.GetRandomInt(0, areas.Length - 1); - return areas[index]; - } - - - /// - /// 性别枚举 - /// - public enum Gender - { - /// - /// 男性 - /// - Male, - /// - /// 女性 - /// - Female - } - } -} diff --git a/EasyTool.Core/ToolCategory/LogUtil.cs b/EasyTool.Core/ToolCategory/LogUtil.cs new file mode 100644 index 0000000..bff5211 --- /dev/null +++ b/EasyTool.Core/ToolCategory/LogUtil.cs @@ -0,0 +1,194 @@ +using System; +using System.IO; +using System.Text; + +namespace EasyTool.ToolCategory +{ + /// + /// 日志级别 + /// + public enum LogLevel + { + Trace, + Debug, + Information, + Warning, + Error, + Critical + } + + /// + /// 日志工具类 + /// + public static class LogUtil + { + private static LogLevel _minLevel = LogLevel.Information; + private static string _logDirectory = "logs"; + private static bool _consoleOutput = true; + private static bool _fileOutput = true; + private static readonly object _lock = new(); + + /// + /// 最小日志级别 + /// + public static LogLevel MinLevel + { + get => _minLevel; + set => _minLevel = value; + } + + /// + /// 日志目录 + /// + public static string LogDirectory + { + get => _logDirectory; + set + { + _logDirectory = value; + if (!Directory.Exists(value)) + Directory.CreateDirectory(value); + } + } + + /// + /// 是否输出到控制台 + /// + public static bool ConsoleOutput + { + get => _consoleOutput; + set => _consoleOutput = value; + } + + /// + /// 是否输出到文件 + /// + public static bool FileOutput + { + get => _fileOutput; + set => _fileOutput = value; + } + + /// + /// 配置日志 + /// + public static void Configure(LogLevel minLevel, string? logDirectory = null, bool consoleOutput = true, bool fileOutput = true) + { + _minLevel = minLevel; + _consoleOutput = consoleOutput; + _fileOutput = fileOutput; + if (logDirectory != null) + LogDirectory = logDirectory; + } + + /// + /// 记录日志 + /// + public static void Log(LogLevel level, string message, Exception? exception = null, string? category = null) + { + if (level < _minLevel) + return; + + var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); + var levelStr = level.ToString().ToUpper().PadLeft(5); + var categoryStr = category != null ? $"[{category}] " : ""; + var sb = new StringBuilder(); + sb.Append($"[{timestamp}] [{levelStr}] {categoryStr}{message}"); + + if (exception != null) + { + sb.AppendLine(); + sb.Append($"Exception: {exception.GetType().Name}: {exception.Message}"); + if (!string.IsNullOrEmpty(exception.StackTrace)) + { + sb.AppendLine(); + sb.Append(exception.StackTrace); + } + } + + var logMessage = sb.ToString(); + + lock (_lock) + { + if (_consoleOutput) + { + var color = GetConsoleColor(level); + var originalColor = Console.ForegroundColor; + Console.ForegroundColor = color; + Console.WriteLine(logMessage); + Console.ForegroundColor = originalColor; + } + + if (_fileOutput) + { + WriteToFile(level, logMessage); + } + } + } + + private static ConsoleColor GetConsoleColor(LogLevel level) + { + return level switch + { + LogLevel.Trace => ConsoleColor.Gray, + LogLevel.Debug => ConsoleColor.Gray, + LogLevel.Information => ConsoleColor.White, + LogLevel.Warning => ConsoleColor.Yellow, + LogLevel.Error => ConsoleColor.Red, + LogLevel.Critical => ConsoleColor.DarkRed, + _ => ConsoleColor.White + }; + } + + private static void WriteToFile(LogLevel level, string message) + { + if (!Directory.Exists(_logDirectory)) + Directory.CreateDirectory(_logDirectory); + + var fileName = level switch + { + LogLevel.Error or LogLevel.Critical => $"error_{DateTime.Now:yyyyMMdd}.log", + _ => $"log_{DateTime.Now:yyyyMMdd}.log" + }; + + var filePath = Path.Combine(_logDirectory, fileName); + File.AppendAllText(filePath, message + Environment.NewLine); + } + + /// + /// 记录跟踪日志 + /// + public static void Trace(string message, string? category = null) + => Log(LogLevel.Trace, message, category: category); + + /// + /// 记录调试日志 + /// + public static void Debug(string message, string? category = null) + => Log(LogLevel.Debug, message, category: category); + + /// + /// 记录信息日志 + /// + public static void Info(string message, string? category = null) + => Log(LogLevel.Information, message, category: category); + + /// + /// 记录警告日志 + /// + public static void Warning(string message, string? category = null) + => Log(LogLevel.Warning, message, category: category); + + /// + /// 记录错误日志 + /// + public static void Error(string message, Exception? exception = null, string? category = null) + => Log(LogLevel.Error, message, exception, category); + + /// + /// 记录严重错误日志 + /// + public static void Critical(string message, Exception? exception = null, string? category = null) + => Log(LogLevel.Critical, message, exception, category); + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/MEFUtil.cs b/EasyTool.Core/ToolCategory/MEFUtil.cs deleted file mode 100644 index 24d3aeb..0000000 --- a/EasyTool.Core/ToolCategory/MEFUtil.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition.Hosting; -using System.Reflection; -using System.Linq; -using System.IO; - -namespace EasyTool -{ - /// - /// MEF加载工具 - /// - public class MEFUtil - { - // 默认扫描程序集的路径 - private static readonly string DefaultDirectory = AppDomain.CurrentDomain.BaseDirectory; - - /// - /// 从指定目录动态加载导出部件 - /// - /// 导出部件的类型 - /// 目录路径 - /// 导出部件的列表 - public static IEnumerable LoadExportParts(string directory = null) - { - // 如果目录为空,则使用默认目录 - directory ??= DefaultDirectory; - - // 创建目录目录目录目录 - var catalog = new DirectoryCatalog(directory); - // 创建容器并将目录添加到容器中 - var container = new CompositionContainer(catalog); - - // 从容器中获取导出的部件 - var parts = container.GetExportedValues(); - return parts; - } - - /// - /// 从指定程序集动态加载导出部件 - /// - /// 导出部件的类型 - /// 程序集名称 - /// 导出部件的列表 - public static IEnumerable LoadExportPartsFromAssembly(string assemblyName) - { - // 加载指定的程序集 - var assembly = Assembly.Load(assemblyName); - // 创建程序集目录 - var catalog = new AssemblyCatalog(assembly); - // 创建容器并将目录添加到容器中 - var container = new CompositionContainer(catalog); - - // 从容器中获取导出的部件 - var parts = container.GetExportedValues(); - return parts; - } - - /// - /// 从指定文件夹中加载所有导出部件 - /// - /// 导出部件类型 - /// 指定文件夹路径 - /// 搜索选项 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromFolder(string folderPath, SearchOption searchOption = SearchOption.AllDirectories) - { - var catalog = new DirectoryCatalog(folderPath, "*.dll"); - using var container = new CompositionContainer(catalog); - return container.GetExportedValues(); - } - - /// - /// 从指定类型中加载所有导出部件 - /// - /// 导出部件类型 - /// 指定类型 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromType(Type type) - { - var catalog = new TypeCatalog(type); - using var container = new CompositionContainer(catalog); - return container.GetExportedValues(); - } - - /// - /// 加载多个目录中的导出部件 - /// - /// 导出部件类型 - /// 多个目录路径 - /// 导出部件列表 - public static IEnumerable LoadExportPartsFromFolders(IEnumerable folderPaths) - { - var catalogs = folderPaths.Select(path => new DirectoryCatalog(path, "*.dll")); - var aggregateCatalog = new AggregateCatalog(catalogs); - using var container = new CompositionContainer(aggregateCatalog); - return container.GetExportedValues(); - } - - /// - /// 从指定容器中获取导入部件 - /// - /// 导入部件的类型 - /// 容器 - /// 导入部件的实例 - public static T GetImportPart(CompositionContainer container) - { - // 获取导入部件的实例 - var part = container.GetExportedValue(); - return part; - } - - /// - /// 从指定容器中获取导入部件的列表 - /// - /// 导入部件的类型 - /// 容器 - /// 导入部件的列表 - public static IEnumerable GetImportParts(CompositionContainer container) - { - // 获取导入部件的列表 - var parts = container.GetExportedValues(); - return parts; - } - } -} diff --git a/EasyTool.Core/ToolCategory/ObjectExtension.cs b/EasyTool.Core/ToolCategory/ObjectExtension.cs new file mode 100644 index 0000000..a22e623 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ObjectExtension.cs @@ -0,0 +1,918 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace EasyTool.ToolCategory +{ + /// + /// Object 对象扩展方法 + /// + public static class ObjectExtension + { + #region 空值判断 + + /// + /// 判断对象是否为 null + /// + public static bool IsNull(this object obj) + { + return obj == null; + } + + /// + /// 判断对象是否不为 null + /// + public static bool IsNotNull(this object obj) + { + return obj != null; + } + + /// + /// 判断对象是否为空(null 或空字符串或空集合) + /// + public static bool IsNullOrEmpty(this object obj) + { + if (obj == null) + return true; + + if (obj is string str) + return string.IsNullOrEmpty(str); + + if (obj is System.Collections.ICollection collection) + return collection.Count == 0; + + return false; + } + + #endregion + + #region 类型转换 + + /// + /// 将对象转换为指定类型 + /// + public static T? As(this object obj) + { + if (obj == null) + return default; + + return (T)obj; + } + + /// + /// 尝试将对象转换为指定类型 + /// + public static T To(this object obj) where T : struct + { + return (T)Convert.ChangeType(obj, typeof(T)); + } + + /// + /// 安全转换,失败返回默认值 + /// + public static T? ToOrDefault(this object obj) + { + return ToOrDefault(obj, default(T)); + } + + /// + /// 安全转换,失败返回指定默认值 + /// + public static T ToOrDefault(this object obj, T defaultValue) + { + if (obj == null) + return defaultValue; + + try + { + if (obj is T direct) + return direct; + + return (T)Convert.ChangeType(obj, typeof(T)); + } + catch + { + return defaultValue; + } + } + + #endregion + + #region JSON 序列化 + + /// + /// 将对象序列化为 JSON 字符串 + /// + public static string? ToJson(this object obj, bool indented = false) + { + if (obj == null) + return null; + + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = indented + }; + + return JsonSerializer.Serialize(obj, options); + } + + /// + /// 从 JSON 字符串反序列化为对象 + /// + public static T? FromJson(this string json) + { + if (string.IsNullOrEmpty(json)) + return default; + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + return JsonSerializer.Deserialize(json, options); + } + + #endregion + + #region XML 序列化 + + /// + /// 将对象序列化为 XML 字符串 + /// + public static string? ToXml(this object obj) + { + if (obj == null) + return null; + + var serializer = new XmlSerializer(obj.GetType()); + using var writer = new StringWriter(); + serializer.Serialize(writer, obj); + return writer.ToString(); + } + + /// + /// 从 XML 字符串反序列化为对象 + /// + public static T? FromXml(this string xml) + { + if (string.IsNullOrEmpty(xml)) + return default; + + var serializer = new XmlSerializer(typeof(T)); + using var reader = new StringReader(xml); + return (T?)serializer.Deserialize(reader); + } + + #endregion + + #region 深拷贝 + + /// + /// 深拷贝对象(使用 JSON 序列化) + /// + public static T? DeepClone(this T obj) + { + if (obj == null) + return default; + + var json = obj.ToJson(); + return json.FromJson(); + } + + /// + /// 异步深拷贝对象(使用 JSON 序列化) + /// + public static async Task DeepCloneAsync(this T obj) + { + if (obj == null) + return default; + + var json = await Task.Run(() => obj.ToJson()); + return await Task.Run(() => json.FromJson()); + } + + /// + /// 浅拷贝对象(使用 MemberwiseClone) + /// + public static T? ShallowClone(this T obj) where T : class + { + if (obj == null) + return null; + + var method = obj.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic); + if (method != null) + return (T?)method.Invoke(obj, null); + + throw new InvalidOperationException("Object does not support MemberwiseClone"); + } + + #endregion + + #region 字典转换 + + /// + /// 将对象转换为字典 + /// + public static Dictionary? ToDictionary(this object obj) + { + if (obj == null) + return null; + + var dict = new Dictionary(); + + foreach (var prop in obj.GetType().GetProperties()) + { + if (prop.CanRead) + { + dict[prop.Name] = prop.GetValue(obj) ?? string.Empty; + } + } + + return dict; + } + + /// + /// 从字典创建对象 + /// + public static T? FromDictionary(this Dictionary? dict) where T : new() + { + if (dict == null) + return default; + + var obj = new T(); + + foreach (var prop in typeof(T).GetProperties()) + { + if (prop.CanWrite && dict.TryGetValue(prop.Name, out var value)) + { + if (value != null) + { + var convertedValue = Convert.ChangeType(value, prop.PropertyType); + prop.SetValue(obj, convertedValue); + } + } + } + + return obj; + } + + #endregion + + #region 属性访问 + + /// + /// 获取属性值 + /// + public static object? GetPropertyValue(this object obj, string propertyName) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return null; + + var prop = obj.GetType().GetProperty(propertyName); + return prop?.GetValue(obj); + } + + /// + /// 设置属性值 + /// + public static void SetPropertyValue(this object obj, string propertyName, object? value) + { + if (obj == null || string.IsNullOrEmpty(propertyName)) + return; + + var prop = obj.GetType().GetProperty(propertyName); + prop?.SetValue(obj, value); + } + + #endregion + + #region 条件执行 + + /// + /// 条件执行(当条件满足时执行操作) + /// + public static T If(this T obj, bool condition, Action? action) + { + if (condition) + { + action?.Invoke(obj); + } + return obj; + } + + /// + /// 条件执行(当条件满足时返回函数结果) + /// + public static TResult? If(this T obj, bool condition, Func? func) + { + return condition ? func!(obj) : default; + } + + /// + /// 条件执行(当对象不为 null 时执行操作) + /// + public static T IfNotNull(this T obj, Action? action) where T : class + { + if (obj != null) + { + action?.Invoke(obj); + } + return obj; + } + + /// + /// 条件执行(当对象不为 null 时返回函数结果) + /// + public static TResult? IfNotNull(this T obj, Func func) where T : class + { + return obj != null ? func(obj) : default; + } + + #endregion + + #region 管道操作 + + /// + /// 管道操作(执行函数并返回结果) + /// + public static TResult Pipe(this T obj, Func func) + { + return func(obj); + } + + /// + /// 管道操作(执行操作) + /// + public static T Pipe(this T obj, Action action) + { + action(obj); + return obj; + } + + #endregion + + #region 对象检查 + + + /// + /// 判断对象是否实现了指定接口 + /// + public static bool Implements(this object obj) + { + if (obj == null) + return false; + + return typeof(TInterface).IsAssignableFrom(obj.GetType()); + } + + #endregion + + #region 对象相等比较 + + /// + /// 比较两个对象的属性值是否相等 + /// + public static bool PropertiesEqual(this T obj, T? other) where T : class + { + if (obj == null && other == null) + return true; + + if (obj == null || other == null) + return false; + + var type = typeof(T); + + foreach (var prop in type.GetProperties()) + { + if (!prop.CanRead) + continue; + + var value1 = prop.GetValue(obj); + var value2 = prop.GetValue(other); + + if (!Equals(value1, value2)) + return false; + } + + return true; + } + + #endregion + + #region 对象信息 + + /// + /// 获取指定类型的默认值 + /// + public static object? GetDefault(Type type) + { + return type.IsValueType ? Activator.CreateInstance(type) : null; + } + + /// + /// 判断对象是否是其类型的默认值 + /// + public static bool IsDefaultValue(this object obj) + { + return obj == null || obj.Equals(GetDefault(obj.GetType())); + } + + /// + /// 判断指定类型是否是可空值类型 + /// + public static bool IsNullable(Type type) + { + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } + + /// + /// 获取可空类型的基础类型 + /// + public static Type GetNullableType(Type type) + { + return Nullable.GetUnderlyingType(type); + } + + /// + /// 获取可空类型或枚举类型的基础类型 + /// + public static Type GetUnderlyingType(Type type) + { + if (IsNullable(type)) + { + return GetNullableType(type); + } + + if (type.IsEnum) + { + return Enum.GetUnderlyingType(type); + } + + return type; + } + + /// + /// 判断指定类型是否是简单类型 + /// + public static bool IsSimpleType(Type type) + { + if (type == typeof(string)) + { + return true; + } + + if (type.IsValueType) + { + return true; + } + + return false; + } + + /// + /// 判断指定类型是否是数字类型 + /// + public static bool IsNumericType(Type type) + { + switch (Type.GetTypeCode(type)) + { + case TypeCode.Byte: + case TypeCode.SByte: + case TypeCode.UInt16: + case TypeCode.Int16: + case TypeCode.UInt32: + case TypeCode.Int32: + case TypeCode.UInt64: + case TypeCode.Int64: + case TypeCode.Decimal: + case TypeCode.Double: + case TypeCode.Single: + return true; + default: + return false; + } + } + + /// + /// 判断指定类型是否是布尔类型 + /// + public static bool IsBooleanType(Type type) + { + return type == typeof(bool); + } + + /// + /// 判断指定类型是否是日期时间类型 + /// + public static bool IsDateTimeType(Type type) + { + return type == typeof(DateTime); + } + + /// + /// 判断指定类型是否是集合类型 + /// + public static bool IsEnumerableType(Type type) + { + return typeof(IEnumerable).IsAssignableFrom(type); + } + + /// + /// 获取指定类型的所有派生类型 + /// + public static Type[] GetSubclassesOf(Type baseType) + { + return AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(s => s.GetTypes()) + .Where(p => baseType.IsAssignableFrom(p) && p != baseType) + .ToArray(); + } + + #endregion + + #region 对象转字符串 + + #endregion + + #region 对象转换(静态工具方法) + + /// + /// 将对象转换为指定类型(使用 TypeConverter 和 IConvertible) + /// + public static T ConvertTo(object obj) + { + return (T)ConvertTo(obj, typeof(T)); + } + + /// + /// 将对象转换为指定类型(使用 TypeConverter 和 IConvertible) + /// + public static object? ConvertTo(object obj, Type targetType) + { + if (obj == null) + { + return GetDefault(targetType); + } + + Type sourceType = obj.GetType(); + + if (targetType.IsAssignableFrom(sourceType)) + { + return obj; + } + + var converter = System.ComponentModel.TypeDescriptor.GetConverter(targetType); + if (converter != null && converter.CanConvertFrom(sourceType)) + { + return converter.ConvertFrom(obj); + } + + var sourceConverter = System.ComponentModel.TypeDescriptor.GetConverter(sourceType); + if (sourceConverter != null && sourceConverter.CanConvertTo(targetType)) + { + return sourceConverter.ConvertTo(obj, targetType); + } + + if (obj is IConvertible) + { + try + { + return System.Convert.ChangeType(obj, targetType); + } + catch (InvalidCastException) + { + } + } + + try + { + var implicitOp = sourceType.GetMethod("op_Implicit", new[] { sourceType }); + if (implicitOp != null && implicitOp.ReturnType == targetType) + { + return implicitOp.Invoke(null, new[] { obj }); + } + + var explicitOp = sourceType.GetMethod("op_Explicit", new[] { sourceType }); + if (explicitOp != null && explicitOp.ReturnType == targetType) + { + return explicitOp.Invoke(null, new[] { obj }); + } + + var targetImplicitOp = targetType.GetMethod("op_Implicit", new[] { sourceType }); + if (targetImplicitOp != null && targetImplicitOp.ReturnType == targetType) + { + return targetImplicitOp.Invoke(null, new[] { obj }); + } + + var targetExplicitOp = targetType.GetMethod("op_Explicit", new[] { sourceType }); + if (targetExplicitOp != null && targetExplicitOp.ReturnType == targetType) + { + return targetExplicitOp.Invoke(null, new[] { obj }); + } + } + catch (InvalidCastException) + { + } + + throw new InvalidCastException($"无法将类型为 {sourceType.Name} 的对象转换为类型为 {targetType.Name} 的对象"); + } + + #endregion + + #region 对象属性复制 + + /// + /// 将源对象的属性复制到目标对象中 + /// + public static void CopyProperties(object source, object target) + { + Type sourceType = source.GetType(); + Type targetType = target.GetType(); + + foreach (PropertyInfo sourceProperty in sourceType.GetProperties()) + { + if (!sourceProperty.CanRead) + { + continue; + } + + PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name); + + if (targetProperty == null || !targetProperty.CanWrite) + { + continue; + } + + object value = GetPropertyValue(source, sourceProperty.Name); + SetPropertyValue(target, targetProperty.Name, value); + } + } + + /// + /// 对象属性值的加密 + /// + public static void EncryptPropertyValue(object obj, string propertyName, Func encryptFunc) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (encryptFunc == null) + { + throw new ArgumentNullException(nameof(encryptFunc)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null && value is string) + { + string encryptedValue = encryptFunc((string)value); + property.SetValue(obj, encryptedValue); + } + } + + /// + /// 对象属性值的解密 + /// + public static void DecryptPropertyValue(object obj, string propertyName, Func decryptFunc) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (decryptFunc == null) + { + throw new ArgumentNullException(nameof(decryptFunc)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null && value is string) + { + string decryptedValue = decryptFunc((string)value); + property.SetValue(obj, decryptedValue); + } + } + + /// + /// 在对象属性上进行特定的处理 + /// + public static void ProcessPropertyValue(object obj, string propertyName, Action processAction) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (string.IsNullOrEmpty(propertyName)) + { + throw new ArgumentNullException(nameof(propertyName)); + } + + if (processAction == null) + { + throw new ArgumentNullException(nameof(processAction)); + } + + PropertyInfo property = obj.GetType().GetProperty(propertyName); + + if (property == null) + { + throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); + } + + object value = property.GetValue(obj); + + if (value != null) + { + processAction(value); + property.SetValue(obj, value); + } + } + + #endregion + + #region 对象比较 + + /// + /// 比较两个对象的差异(属性值或字段值不同) + /// + public static IEnumerable CompareDifferences(object obj1, object obj2) + { + if (obj1 == null && obj2 == null) + { + return Enumerable.Empty(); + } + + if (obj1 == null || obj2 == null) + { + throw new ArgumentNullException("比较对象不能为 null"); + } + + List differences = new List(); + + foreach (PropertyInfo property in obj1.GetType().GetProperties()) + { + object value1 = property.GetValue(obj1); + object value2 = property.GetValue(obj2); + + if (!Equals(value1, value2)) + { + differences.Add($"属性 {property.Name} 的值不同:{value1} -> {value2}"); + } + } + + foreach (FieldInfo field in obj1.GetType().GetFields()) + { + object value1 = field.GetValue(obj1); + object value2 = field.GetValue(obj2); + + if (!Equals(value1, value2)) + { + differences.Add($"字段 {field.Name} 的值不同:{value1} -> {value2}"); + } + } + + return differences; + } + + #endregion + + #region 对象高级转换 + + /// + /// 将对象转换为键值对集合 + /// + public static IEnumerable> ToKeyValuePairs(this object obj) + { + if (obj == null) + { + return Enumerable.Empty>(); + } + + List> pairs = new List>(); + + foreach (PropertyInfo property in obj.GetType().GetProperties()) + { + pairs.Add(new KeyValuePair(property.Name, property.GetValue(obj))); + } + + foreach (FieldInfo field in obj.GetType().GetFields()) + { + pairs.Add(new KeyValuePair(field.Name, field.GetValue(obj))); + } + + return pairs; + } + + /// + /// 将对象转换为动态扩展对象 + /// + public static dynamic? ToDynamic(this object obj) + { + if (obj == null) + { + return null; + } + + IDictionary dictionary = new System.Dynamic.ExpandoObject(); + + foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) + { + if (!propertyInfo.CanRead) + { + continue; + } + + object value = GetPropertyValue(obj, propertyInfo.Name); + dictionary.Add(propertyInfo.Name, value); + } + + foreach (FieldInfo fieldInfo in obj.GetType().GetFields()) + { + object value = GetFieldValue(obj, fieldInfo.Name); + dictionary.Add(fieldInfo.Name, value); + } + + return dictionary; + } + + /// + /// 获取对象的字段值 + /// + public static object? GetFieldValue(this object obj, string fieldName) + { + return obj.GetType().GetField(fieldName)?.GetValue(obj); + } + + /// + /// 设置对象的字段值 + /// + public static void SetFieldValue(this object obj, string fieldName, object? value) + { + obj.GetType().GetField(fieldName)?.SetValue(obj, value); + } + + #endregion + + #region 对象抛出异常 + + /// + /// 对象为 null 时抛出异常 + /// + public static T ThrowIfNull(this T? obj, string? paramName = null) where T : class + { + if (obj == null) + throw new ArgumentNullException(paramName ?? typeof(T).Name); + + return obj; + } + + /// + /// 条件不满足时抛出异常 + /// + public static T ThrowIf(this T obj, bool condition, string message) where T : class + { + if (condition) + throw new Exception(message); + + return obj; + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/ObjectPool.cs b/EasyTool.Core/ToolCategory/ObjectPool.cs new file mode 100644 index 0000000..d3e40d0 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ObjectPool.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 对象池 + /// 用于重用对象,减少GC压力 + /// + /// 对象类型 + public class ObjectPool where T : class + { + private readonly Stack _pool; + private readonly Func _factory; + private readonly Action? _reset; + private readonly int _maxSize; + private readonly object _lock = new(); + + /// + /// 创建对象池 + /// + /// 对象工厂 + /// 最大池大小 + /// 重置动作 + public ObjectPool(Func factory, int maxSize = 100, Action? reset = null) + { + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); + _maxSize = maxSize; + _reset = reset; + _pool = new Stack(); + } + + /// + /// 当前池中对象数量 + /// + public int Count + { + get { lock (_lock) { return _pool.Count; } } + } + + /// + /// 从池中获取对象 + /// + public T Get() + { + lock (_lock) + { + if (_pool.Count > 0) + return _pool.Pop(); + } + return _factory(); + } + + /// + /// 将对象归还到池中 + /// + public void Return(T item) + { + if (item == null) + return; + + _reset?.Invoke(item); + + lock (_lock) + { + if (_pool.Count < _maxSize) + _pool.Push(item); + } + } + + /// + /// 使用池中对象执行操作 + /// + public TResult Use(Func action) + { + var item = Get(); + try + { + return action(item); + } + finally + { + Return(item); + } + } + + /// + /// 使用池中对象执行操作 + /// + public void Use(Action action) + { + var item = Get(); + try + { + action(item); + } + finally + { + Return(item); + } + } + + /// + /// 清空池 + /// + public void Clear() + { + lock (_lock) + { + _pool.Clear(); + } + } + + /// + /// 预热池(创建指定数量的对象) + /// + public void WarmUp(int count) + { + for (int i = 0; i < count; i++) + { + var item = _factory(); + Return(item); + } + } + } + + /// + /// 对象池扩展 + /// + public static class ObjectPoolExtensions + { + /// + /// 创建对象池 + /// + public static ObjectPool CreatePool(this Func factory, int maxSize = 100, Action? reset = null) + where T : class + { + return new ObjectPool(factory, maxSize, reset); + } + } +} diff --git a/EasyTool.Core/ToolCategory/ObjectUtil.cs b/EasyTool.Core/ToolCategory/ObjectUtil.cs index a17c076..21da7a3 100644 --- a/EasyTool.Core/ToolCategory/ObjectUtil.cs +++ b/EasyTool.Core/ToolCategory/ObjectUtil.cs @@ -1,1067 +1,394 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; -using System.Dynamic; -using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; -using System.Runtime.Serialization.Json; -using System.Text; -using System.Xml.Serialization; +using System.Text.Json; -namespace EasyTool +namespace EasyTool.ToolCategory { /// /// 对象工具类 + /// 提供对象的常用操作功能 /// - public class ObjectUtil + public static class ObjectUtil { - /// - /// 检查对象是否为 null + /// 深拷贝对象(使用 JSON 序列化) /// - public static bool IsNull(object obj) + /// 对象类型 + /// 原对象 + /// 拷贝后的对象 + public static T? DeepClone(T obj) { - return obj == null; - } + if (obj == null) + return default; - /// - /// 检查对象是否不为 null - /// - public static bool IsNotNull(object obj) - { - return obj != null; + var json = JsonSerializer.Serialize(obj); + return JsonSerializer.Deserialize(json); } /// - /// 检查对象是否为空(null 或者 空字符串或空白字符) + /// 浅拷贝对象 /// - public static bool IsNullOrEmpty(object obj) + /// 对象类型 + /// 原对象 + /// 拷贝后的对象 + public static T? ShallowClone(T obj) where T : class { - if (IsNull(obj)) - { - return true; - } + if (obj == null) + return null; - if (obj is string str) - { - return string.IsNullOrWhiteSpace(str); - } + var type = obj.GetType(); + var method = type.GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic); - if (obj is ICollection collection) + if (method != null) { - return collection.Count == 0; + return (T?)method.Invoke(obj, null); } - return false; - } - - /// - /// 检查对象是否不为空(非 null 且 非空字符串 或者 非空集合) - /// - public static bool IsNotNullOrEmpty(object obj) - { - return !IsNullOrEmpty(obj); + return null; } /// - /// 检查两个对象是否相等 + /// 比较两个对象是否相等(深度比较) /// - public static new bool Equals(object obj1, object obj2) + /// 对象类型 + /// 对象1 + /// 对象2 + /// 是否相等 + public static bool DeepEquals(T? obj1, T? obj2) { - if (IsNull(obj1) && IsNull(obj2)) - { + if (ReferenceEquals(obj1, obj2)) return true; - } - if (IsNull(obj1) || IsNull(obj2)) - { + if (obj1 == null || obj2 == null) return false; - } - - return obj1.Equals(obj2); - } - - /// - /// 获取对象的类型名称 - /// - public static string GetTypeName(object obj) - { - return obj.GetType().Name; - } - - /// - /// 将对象转换为指定类型 - /// - public static T Convert(object obj) - { - return (T)Convert(obj, typeof(T)); - } - - /// - /// 将对象转换为指定类型 - /// - public static object Convert(object obj, Type targetType) - { - if (IsNull(obj)) - { - return null; - } - - if (targetType.IsAssignableFrom(obj.GetType())) - { - return obj; - } - - if (obj is IConvertible) - { - return System.Convert.ChangeType(obj, targetType); - } - - // TODO: 支持自定义类型转换 - - throw new InvalidCastException($"无法将类型为 {obj.GetType().Name} 的对象转换为类型为 {targetType.Name} 的对象"); - } - - /// - /// 获取对象的属性列表 - /// - public static IEnumerable GetProperties(object obj) - { - return obj.GetType().GetProperties(); - } - - /// - /// 获取对象的属性值 - /// - public static object GetPropertyValue(object obj, string propertyName) - { - return obj.GetType().GetProperty(propertyName)?.GetValue(obj); - } - - /// - /// 设置对象的属性值 - /// - public static void SetPropertyValue(object obj, string propertyName, object value) - { - obj.GetType().GetProperty(propertyName)?.SetValue(obj, value); - } - - /// - /// 获取对象的字段列表 - /// - public static IEnumerable GetFields(object obj) - { - return obj.GetType().GetFields(); - } - - /// - /// 获取对象的字段值 - /// - public static object GetFieldValue(object obj, string fieldName) - { - return obj.GetType().GetField(fieldName)?.GetValue(obj); - } - - /// - /// 设置对象的字段值 - /// - public static void SetFieldValue(object obj, string fieldName, object value) - { - obj.GetType().GetField(fieldName)?.SetValue(obj, value); - } - - /// - /// 获取对象的方法列表 - /// - public static IEnumerable GetMethods(object obj) - { - return obj.GetType().GetMethods(); - } - - /// - /// 判断对象是否实现了指定接口 - /// - public static bool ImplementsInterface(object obj, Type interfaceType) - { - return interfaceType.IsAssignableFrom(obj.GetType()); - } - - /// - /// 判断对象是否为指定类型的实例 - /// - public static bool IsInstanceOfType(object obj, Type targetType) - { - return targetType.IsInstanceOfType(obj); - } - - /// - /// 对象属性或字段值的加密 - /// - public static void EncryptPropertyValue(object obj, string propertyName, Func encryptFunc) - { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (encryptFunc == null) - { - throw new ArgumentNullException(nameof(encryptFunc)); - } - - PropertyInfo property = obj.GetType().GetProperty(propertyName); - - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null && value is string) - { - string encryptedValue = encryptFunc((string)value); - property.SetValue(obj, encryptedValue); - } - } - - /// - /// 对象属性或字段值的解密 - /// - public static void DecryptPropertyValue(object obj, string propertyName, Func decryptFunc) - { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (decryptFunc == null) - { - throw new ArgumentNullException(nameof(decryptFunc)); - } - PropertyInfo property = obj.GetType().GetProperty(propertyName); + var json1 = JsonSerializer.Serialize(obj1); + var json2 = JsonSerializer.Serialize(obj2); - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null && value is string) - { - string decryptedValue = decryptFunc((string)value); - property.SetValue(obj, decryptedValue); - } + return json1 == json2; } /// - /// 在对象属性或字段上进行特定的处理 + /// 获取对象的哈希码(基于内容) /// - public static void ProcessPropertyValue(object obj, string propertyName, Action processAction) + /// 对象类型 + /// 对象 + /// 哈希码 + public static int GetDeepHashCode(T obj) { - if (IsNull(obj)) - { - throw new ArgumentNullException(nameof(obj)); - } - - if (string.IsNullOrEmpty(propertyName)) - { - throw new ArgumentNullException(nameof(propertyName)); - } - - if (processAction == null) - { - throw new ArgumentNullException(nameof(processAction)); - } - PropertyInfo property = obj.GetType().GetProperty(propertyName); - - if (property == null) - { - throw new ArgumentException($"对象类型 {obj.GetType().Name} 中没有名为 {propertyName} 的属性", nameof(propertyName)); - } - - object value = property.GetValue(obj); - - if (value != null) - { - processAction(value); - property.SetValue(obj, value); - } - } - - /// - /// 将对象序列化为 JSON 字符串 - /// - public static string ToJson(object obj) - { - if (IsNull(obj)) - { - return null; - } + if (obj == null) + return 0; - using (MemoryStream stream = new MemoryStream()) - { - DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); - serializer.WriteObject(stream, obj); - return Encoding.UTF8.GetString(stream.ToArray()); - } + var json = JsonSerializer.Serialize(obj); + return json.GetHashCode(); } /// - /// 将 JSON 字符串反序列化为对象 + /// 检查对象是否为默认值 /// - public static T FromJson(string json) + /// 对象类型 + /// 对象 + /// 是否为默认值 + public static bool IsDefault(T obj) { - if (string.IsNullOrEmpty(json)) - { - return default(T); - } - - using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) - { - DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); - return (T)serializer.ReadObject(stream); - } + return EqualityComparer.Default.Equals(obj, default); } /// - /// 将对象序列化为 XML 字符串 + /// 获取对象的类型名称 /// - public static string ToXml(object obj) + /// 对象类型 + /// 对象 + /// 类型名称 + public static string GetTypeName(T obj) { - if (IsNull(obj)) - { - return null; - } + if (obj == null) + return "null"; - XmlSerializer serializer = new XmlSerializer(obj.GetType()); - using (MemoryStream stream = new MemoryStream()) - { - serializer.Serialize(stream, obj); - return Encoding.UTF8.GetString(stream.ToArray()); - } + return obj.GetType().Name; } /// - /// 将 XML 字符串反序列化为对象 + /// 获取对象的完整类型名称 /// - public static T FromXml(string xml) + /// 对象类型 + /// 对象 + /// 完整类型名称 + public static string GetTypeFullName(T obj) { - if (string.IsNullOrEmpty(xml)) - { - return default(T); - } + if (obj == null) + return "null"; - XmlSerializer serializer = new XmlSerializer(typeof(T)); - using (StringReader reader = new StringReader(xml)) - { - return (T)serializer.Deserialize(reader); - } + return obj.GetType().FullName ?? obj.GetType().Name; } /// /// 将对象转换为字典 /// - public static Dictionary ToDictionary(object obj) + /// 对象 + /// 属性字典 + public static Dictionary ToDictionary(object obj) { - if (IsNull(obj)) - { - return null; - } - - Dictionary dictionary = new Dictionary(); - - foreach (PropertyInfo property in GetProperties(obj)) - { - dictionary[property.Name] = property.GetValue(obj); - } - - foreach (FieldInfo field in GetFields(obj)) - { - dictionary[field.Name] = field.GetValue(obj); - } - - return dictionary; - } - - /// - /// 将字典转换为对象 - /// - public static T FromDictionary(Dictionary dictionary) where T : new() - { - if (dictionary == null) - { - return default(T); - } - - T obj = new T(); + if (obj == null) + return new Dictionary(); - foreach (PropertyInfo property in GetProperties(obj)) + if (obj is IDictionary dict) { - if (dictionary.TryGetValue(property.Name, out object value)) + var result = new Dictionary(); + foreach (DictionaryEntry entry in dict) { - property.SetValue(obj, Convert(value, property.PropertyType)); + result[entry.Key?.ToString() ?? ""] = entry.Value; } + return result; } - foreach (FieldInfo field in GetFields(obj)) + var type = obj.GetType(); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + var result2 = new Dictionary(); + + foreach (var property in properties) { - if (dictionary.TryGetValue(field.Name, out object value)) + if (property.CanRead) { - field.SetValue(obj, Convert(value, field.FieldType)); + result2[property.Name] = property.GetValue(obj); } } - return obj; + return result2; } /// - /// 比较两个对象的差异(属性值或字段值不同) + /// 从字典创建对象 /// - public static IEnumerable Compare(object obj1, object obj2) + /// 对象类型 + /// 属性字典 + /// 对象实例 + public static T? FromDictionary(Dictionary dictionary) where T : class, new() { - if (IsNull(obj1) && IsNull(obj2)) - { - return Enumerable.Empty(); - } + if (dictionary == null || dictionary.Count == 0) + return default; - if (IsNull(obj1) || IsNull(obj2)) - { - throw new ArgumentNullException("比较对象不能为 null"); - } + var type = typeof(T); + var obj = new T(); - List differences = new List(); - - foreach (PropertyInfo property in GetProperties(obj1)) + foreach (var kvp in dictionary) { - object value1 = property.GetValue(obj1); - object value2 = property.GetValue(obj2); + var property = type.GetProperty(kvp.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); - if (!Equals(value1, value2)) + if (property != null && property.CanWrite) { - differences.Add($"属性 {property.Name} 的值不同:{value1} -> {value2}"); + var value = ConvertValue(kvp.Value, property.PropertyType); + property.SetValue(obj, value); } } - foreach (FieldInfo field in GetFields(obj1)) - { - object value1 = field.GetValue(obj1); - object value2 = field.GetValue(obj2); - - if (!Equals(value1, value2)) - { - differences.Add($"字段 {field.Name} 的值不同:{value1} -> {value2}"); - } - } - - return differences; - } - - /// - /// 获取对象的哈希码 - /// - public static int GetHashCode(object obj) - { - if (IsNull(obj)) - { - return 0; - } - - return obj.GetHashCode(); + return obj; } - /// - /// 深拷贝对象 - /// - public static T DeepClone(T obj) + private static object? ConvertValue(object? value, Type targetType) { - if (IsNull(obj)) - { - return default(T); - } + if (value == null) + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; - DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); + if (targetType.IsAssignableFrom(value.GetType())) + return value; - using (MemoryStream stream = new MemoryStream()) + try { - serializer.WriteObject(stream, obj); - stream.Position = 0; - return (T)serializer.ReadObject(stream); + return Convert.ChangeType(value, targetType); } - } - - /// - /// 判断对象是否为值类型 - /// - public static bool IsValueType(object obj) - { - if (IsNull(obj)) + catch { - return false; + return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; } - - return obj.GetType().IsValueType; } /// - /// 将对象转换为键值对集合 + /// 合并两个对象的属性 /// - public static IEnumerable> ToKeyValuePairs(object obj) + /// 对象类型 + /// 目标对象 + /// 源对象 + /// 是否覆盖已有值 + /// 合并后的对象 + public static T Merge(T target, T source, bool overwrite = true) where T : class { - if (IsNull(obj)) - { - return Enumerable.Empty>(); - } + if (target == null) + return source ?? target; - List> pairs = new List>(); + if (source == null) + return target; - foreach (PropertyInfo property in GetProperties(obj)) - { - pairs.Add(new KeyValuePair(property.Name, property.GetValue(obj))); - } + var type = typeof(T); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); - foreach (FieldInfo field in GetFields(obj)) + foreach (var property in properties) { - pairs.Add(new KeyValuePair(field.Name, field.GetValue(obj))); - } - - return pairs; - } - - /// - /// 深度复制对象 - /// - public static object DeepCopy(object obj) - { - if (obj == null) - { - return null; - } - - Type type = obj.GetType(); - - if (IsSimpleType(type)) - { - return obj; - } - - if (IsEnumerableType(type)) - { - Type elementType = type.GetElementType() ?? type.GetGenericArguments().FirstOrDefault(); - - if (elementType == null || IsSimpleType(elementType)) - { - return obj; - } - - IList list = (IList)Activator.CreateInstance(type); - - foreach (object item in (IEnumerable)obj) - { - list.Add(DeepCopy(item)); - } - - return list; - } - - object clone = Activator.CreateInstance(type); - - foreach (PropertyInfo propertyInfo in GetProperties(type)) - { - if (!propertyInfo.CanRead || !propertyInfo.CanWrite) - { + if (!property.CanRead || !property.CanWrite) continue; - } - - object value = GetPropertyValue(obj, propertyInfo.Name); - if (value == null) - { - continue; - } + var sourceValue = property.GetValue(source); + var targetValue = property.GetValue(target); - if (IsSimpleType(propertyInfo.PropertyType)) - { - SetPropertyValue(clone, propertyInfo.Name, value); - } - else if (IsEnumerableType(propertyInfo.PropertyType)) - { - object enumerable = DeepCopy(value); - SetPropertyValue(clone, propertyInfo.Name, enumerable); - } - else + if (sourceValue != null && (overwrite || targetValue == null)) { - object childClone = DeepCopy(value); - SetPropertyValue(clone, propertyInfo.Name, childClone); + property.SetValue(target, sourceValue); } } - foreach (FieldInfo fieldInfo in GetFields(type)) - { - object value = GetFieldValue(obj, fieldInfo.Name); - - if (value == null) - { - continue; - } - - if (IsSimpleType(fieldInfo.FieldType)) - { - SetFieldValue(clone, fieldInfo.Name, value); - } - else if (IsEnumerableType(fieldInfo.FieldType)) - { - object enumerable = DeepCopy(value); - SetFieldValue(clone, fieldInfo.Name, enumerable); - } - else - { - object childClone = DeepCopy(value); - SetFieldValue(clone, fieldInfo.Name, childClone); - } - } - - return clone; + return target; } /// - /// 将源对象的属性复制到目标对象中 + /// 检查对象是否有指定属性 /// - public static void CopyProperties(object source, object target) + /// 对象类型 + /// 对象 + /// 属性名 + /// 是否有属性 + public static bool HasProperty(T obj, string propertyName) { - Type sourceType = source.GetType(); - Type targetType = target.GetType(); - - foreach (PropertyInfo sourceProperty in GetProperties(sourceType)) - { - if (!sourceProperty.CanRead) - { - continue; - } - - PropertyInfo targetProperty = GetProperty(targetType, sourceProperty.Name); - - if (targetProperty == null || !targetProperty.CanWrite) - { - continue; - } - - object value = GetPropertyValue(source, sourceProperty.Name); - SetPropertyValue(target, targetProperty.Name, value); - } - } - - /// - /// 获取指定类型的 Type 对象 - /// - public static Type GetType(string typeName) - { - return Type.GetType(typeName); - } - - /// - /// 获取对象的 Type 对象 - /// - public static Type GetType(object obj) - { - return obj.GetType(); - } - - /// - /// 获取类型的所有成员信息,包括字段、属性、方法和事件等 - /// - public static MemberInfo[] GetMembers(Type type) - { - return type.GetMembers(); - } - - /// - /// 获取类型的所有属性信息 - /// - public static PropertyInfo[] GetProperties(Type type) - { - return type.GetProperties(); - } - - /// - /// 获取类型的所有字段信息 - /// - public static FieldInfo[] GetFields(Type type) - { - return type.GetFields(); - } - - /// - /// 获取指定名称的属性信息 - /// - public static PropertyInfo GetProperty(Type type, string propertyName) - { - return type.GetProperty(propertyName); - } - - /// - /// 获取指定名称的属性信息 - /// - public static PropertyInfo GetProperty(object obj, string propertyName) - { - return obj.GetType().GetProperty(propertyName); - } - - /// - /// 获取指定名称的字段信息 - /// - public static FieldInfo GetField(Type type, string fieldName) - { - return type.GetField(fieldName); - } - - /// - /// 获取指定名称的字段信息 - /// - public static FieldInfo GetField(object obj, string fieldName) - { - return obj.GetType().GetField(fieldName); - } - - /// - /// 获取指定名称的方法信息 - /// - public static MethodInfo GetMethod(Type type, string methodName) - { - return type.GetMethod(methodName); - } - - /// - /// 获取指定名称的方法信息 - /// - public static MethodInfo GetMethod(object obj, string methodName) - { - return obj.GetType().GetMethod(methodName); - } - - /// - /// 获取指定名称和参数类型的方法信息 - /// - public static MethodInfo GetMethod(Type type, string methodName, Type[] parameterTypes) - { - return type.GetMethod(methodName, parameterTypes); - } - - /// - /// 获取指定名称和参数类型的方法信息 - /// - public static MethodInfo GetMethod(object obj, string methodName, Type[] parameterTypes) - { - return obj.GetType().GetMethod(methodName, parameterTypes); - } - - /// - /// 调用对象的指定方法 - /// - public static object InvokeMethod(object obj, string methodName, object[] parameters) - { - Type type = obj.GetType(); - MethodInfo methodInfo = GetMethod(type, methodName); - return methodInfo.Invoke(obj, parameters); - } - - /// - /// 调用对象的指定方法 - /// - public static object InvokeMethod(object obj, string methodName, Type[] parameterTypes, object[] parameters) - { - Type type = obj.GetType(); - MethodInfo methodInfo = GetMethod(type, methodName, parameterTypes); - return methodInfo.Invoke(obj, parameters); - } - - /// - /// 创建指定类型的实例 - /// - public static object CreateInstance(Type type, object[] constructorParameters) - { - return Activator.CreateInstance(type, constructorParameters); - } - - - /// - /// 判断指定类型是否派生自指定的基类或接口 - /// - public static bool IsSubclassOf(Type type, Type baseType) - { - return type.IsSubclassOf(baseType); - } - - /// - /// 获取指定类型的所有派生类型 - /// - public static Type[] GetSubclassesOf(Type baseType) - { - return AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(s => s.GetTypes()) - .Where(p => baseType.IsAssignableFrom(p) && p != baseType) - .ToArray(); - } - - /// - /// 获取指定类型实现的所有接口类型 - /// - public static Type[] GetInterfaces(Type type) - { - return type.GetInterfaces(); - } - - /// - /// 获取指定类型的程序集限定名 - /// - public static string GetAssemblyQualifiedName(Type type) - { - return type.AssemblyQualifiedName; - } + if (obj == null || string.IsNullOrEmpty(propertyName)) + return false; - /// - /// 获取指定类型的默认值 - /// - public static object GetDefault(Type type) - { - return type.IsValueType ? Activator.CreateInstance(type) : null; + var type = obj.GetType(); + return type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance) != null; } /// - /// 判断对象是否是其类型的默认值 + /// 获取对象的属性值 /// - public static bool IsDefaultValue(object obj) + /// 对象类型 + /// 对象 + /// 属性名 + /// 属性值 + public static object? GetPropertyValue(T obj, string propertyName) { - return obj == null || obj.Equals(GetDefault(obj.GetType())); - } + if (obj == null || string.IsNullOrEmpty(propertyName)) + return null; - /// - /// 判断指定类型是否是可空类型 - /// - public static bool IsNullable(Type type) - { - return Nullable.GetUnderlyingType(type) != null; - } + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); - /// - /// 获取可空类型的基础类型 - /// - public static Type GetNullableType(Type type) - { - return Nullable.GetUnderlyingType(type) ?? type; + return property?.CanRead == true ? property.GetValue(obj) : null; } /// - /// 获取可空类型或枚举类型的基础类型 + /// 设置对象的属性值 /// - public static Type GetUnderlyingType(Type type) + /// 对象类型 + /// 对象 + /// 属性名 + /// 属性值 + /// 是否设置成功 + public static bool SetPropertyValue(T obj, string propertyName, object? value) { - if (IsNullable(type)) - { - return GetNullableType(type); - } + if (obj == null || string.IsNullOrEmpty(propertyName)) + return false; - if (IsEnumType(type)) - { - return Enum.GetUnderlyingType(type); - } + var type = obj.GetType(); + var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); - return type; - } - - /// - /// 判断指定类型是否是简单类型 - /// - public static bool IsSimpleType(Type type) - { - if (type == typeof(string)) - { - return true; - } + if (property?.CanWrite != true) + return false; - if (type.IsValueType) + try { + var convertedValue = ConvertValue(value, property.PropertyType); + property.SetValue(obj, convertedValue); return true; } - - return false; - } - - /// - /// 判断指定类型是否是数字类型 - /// - public static bool IsNumericType(Type type) - { - switch (Type.GetTypeCode(type)) + catch { - case TypeCode.Byte: - case TypeCode.SByte: - case TypeCode.UInt16: - case TypeCode.Int16: - case TypeCode.UInt32: - case TypeCode.Int32: - case TypeCode.UInt64: - case TypeCode.Int64: - case TypeCode.Decimal: - case TypeCode.Double: - case TypeCode.Single: - return true; - default: - return false; + return false; } } /// - /// 判断指定类型是否是布尔类型 + /// 获取对象的所有属性名 /// - public static bool IsBooleanType(Type type) + /// 对象类型 + /// 对象 + /// 属性名列表 + public static string[] GetPropertyNames(T obj) { - return type == typeof(bool); - } + if (obj == null) + return Array.Empty(); - /// - /// 判断指定类型是否是日期时间类型 - /// - public static bool IsDateTimeType(Type type) - { - return type == typeof(DateTime); + var type = obj.GetType(); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + return properties.Select(p => p.Name).ToArray(); } /// - /// 判断指定类型是否是枚举类型 + /// 转换类型 /// - public static bool IsEnumType(Type type) + /// 目标类型 + /// 值 + /// 转换后的值 + public static T? ConvertTo(object? value) { - return type.IsEnum; - } + if (value == null) + return default; - /// - /// 判断指定类型是否是集合类型 - /// - public static bool IsEnumerableType(Type type) - { - return typeof(IEnumerable).IsAssignableFrom(type); - } + if (value is T t) + return t; - /// - /// 将对象转换为动态扩展对象 - /// - public static dynamic ToDynamic(object obj) - { - if (obj == null) + try { - return null; - } + var targetType = typeof(T); - IDictionary dictionary = new ExpandoObject(); + if (targetType == typeof(Guid) && value is string str) + { + return (T)(object)Guid.Parse(str); + } - foreach (PropertyInfo propertyInfo in GetProperties(obj.GetType())) - { - if (!propertyInfo.CanRead) + if (targetType == typeof(DateTime) && value is string dateStr) { - continue; + return (T)(object)DateTime.Parse(dateStr); } - object value = GetPropertyValue(obj, propertyInfo.Name); - dictionary.Add(propertyInfo.Name, value); + return (T)Convert.ChangeType(value, targetType); } - - foreach (FieldInfo fieldInfo in GetFields(obj.GetType())) + catch { - object value = GetFieldValue(obj, fieldInfo.Name); - dictionary.Add(fieldInfo.Name, value); + return default; } - - return dictionary; } -#if NET6_0_OR_GREATER - - /// - /// 将对象序列化为 JSON 字符串 - /// - public static string SerializeToJson(object obj) - { - return System.Text.Json.JsonSerializer.Serialize(obj); - } - - /// - /// 将 JSON 字符串反序列化为指定类型的对象 - /// - public static T? DeserializeFromJson(string json) - { - return System.Text.Json.JsonSerializer.Deserialize(json); - } - -#endif - /// - /// 将对象序列化为 XML 字符串 + /// 安全转换为字符串 /// - public static string SerializeToXml(object obj) + /// 值 + /// 字符串 + public static string SafeToString(object? value) { - XmlSerializer serializer = new XmlSerializer(obj.GetType()); - - using (StringWriter writer = new StringWriter()) - { - serializer.Serialize(writer, obj); - return writer.ToString(); - } + return value?.ToString() ?? string.Empty; } /// - /// 将 XML 字符串反序列化为指定类型的对象 + /// 检查对象是否为空或空集合 /// - public static object DeserializeFromXml(string xml, Type type) + /// 值 + /// 是否为空 + public static bool IsNullOrEmpty(object? value) { - XmlSerializer serializer = new XmlSerializer(type); - - using (StringReader reader = new StringReader(xml)) - { - return serializer.Deserialize(reader); - } - } + if (value == null) + return true; - /// - /// 将对象序列化为二进制数据 - /// - public static byte[] SerializeToBinary(object obj) - { - BinaryFormatter formatter = new BinaryFormatter(); + if (value is string str) + return string.IsNullOrEmpty(str); - using (MemoryStream stream = new MemoryStream()) + if (value is IEnumerable enumerable) { - formatter.Serialize(stream, obj); - return stream.ToArray(); + var enumerator = enumerable.GetEnumerator(); + return !enumerator.MoveNext(); } - } - /// - /// 将二进制数据反序列化为指定类型的对象 - /// - public static object DeserializeFromBinary(byte[] data, Type type) - { - BinaryFormatter formatter = new BinaryFormatter(); - - using (MemoryStream stream = new MemoryStream(data)) - { - return formatter.Deserialize(stream); - } + return false; } } } diff --git a/EasyTool.Core/ToolCategory/PageUtil.cs b/EasyTool.Core/ToolCategory/PageUtil.cs index e44b300..967af93 100644 --- a/EasyTool.Core/ToolCategory/PageUtil.cs +++ b/EasyTool.Core/ToolCategory/PageUtil.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; -namespace EasyTool +namespace EasyTool.ToolCategory { /// /// 分页工具类,支持多种数据源和多种排序方式的分页 @@ -193,41 +193,56 @@ public void SetOrderField(Func orderField, bool isAsc) /// 分页HTML代码 public string GetPaginationHtml(string urlFormat, string currentPageClass = "current", int range = 5) { - string html = ""; + var sb = new StringBuilder(); if (totalPage <= 1) { - return html; + return sb.ToString(); } + int startPage = Math.Max(1, currentPage - range); int endPage = Math.Min(totalPage, currentPage + range); if (startPage > 1) { - html += "1"; + sb.Append("1"); if (startPage > 2) { - html += "..."; + sb.Append("..."); } } for (int i = startPage; i <= endPage; i++) { if (i == currentPage) { - html += "" + i.ToString() + ""; + sb.Append(""); + sb.Append(i.ToString()); + sb.Append(""); } else { - html += "" + i.ToString() + ""; + sb.Append(""); + sb.Append(i.ToString()); + sb.Append(""); } } if (endPage < totalPage) { if (endPage < totalPage - 1) { - html += "..."; + sb.Append("..."); } - html += "" + totalPage.ToString() + ""; + sb.Append(""); + sb.Append(totalPage.ToString()); + sb.Append(""); } - return html; + return sb.ToString(); } } } diff --git a/EasyTool.Core/ToolCategory/PipelineUtil.cs b/EasyTool.Core/ToolCategory/PipelineUtil.cs new file mode 100644 index 0000000..dc060bc --- /dev/null +++ b/EasyTool.Core/ToolCategory/PipelineUtil.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 管道上下文 + /// + public class PipelineContext + { + private readonly Dictionary _items = new(); + + /// + /// 获取或设置项 + /// + public object? this[string key] + { + get => _items.TryGetValue(key, out var value) ? value : null; + set => _items[key] = value!; + } + + /// + /// 获取值 + /// + public T? Get(string key) + { + return _items.TryGetValue(key, out var value) ? (T?)value : default; + } + + /// + /// 设置值 + /// + public void Set(string key, T value) + { + _items[key] = value!; + } + + /// + /// 是否包含键 + /// + public bool ContainsKey(string key) => _items.ContainsKey(key); + + /// + /// 移除项 + /// + public bool Remove(string key) => _items.Remove(key); + + /// + /// 清空 + /// + public void Clear() => _items.Clear(); + } + + /// + /// 管道处理委托 + /// + public delegate Task PipelineDelegate(PipelineContext context); + + /// + /// 管道构建器 + /// + public class PipelineBuilder + { + private readonly List> _middlewares = new(); + + /// + /// 添加中间件 + /// + public PipelineBuilder Use(Func middleware) + { + _middlewares.Add(middleware); + return this; + } + + /// + /// 添加中间件 + /// + public PipelineBuilder Use(Func middleware) + { + return Use(next => context => middleware(context, next)); + } + + /// + /// 添加同步中间件 + /// + public PipelineBuilder Use(Action middleware) + { + return Use(next => context => + { + middleware(context, () => next(context).GetAwaiter().GetResult()); + return Task.CompletedTask; + }); + } + + /// + /// 条件分支 + /// + public PipelineBuilder UseWhen(Func predicate, Action configure) + { + var branchBuilder = new PipelineBuilder(); + configure(branchBuilder); + + return Use(next => + { + var branch = branchBuilder.Build(next); + return context => predicate(context) ? branch(context) : next(context); + }); + } + + /// + /// 映射分支 + /// + public PipelineBuilder Map(string path, Action configure) + { + return UseWhen(ctx => ctx.Get("Path")?.StartsWith(path) == true, configure); + } + + /// + /// 异常处理 + /// + public PipelineBuilder UseExceptionHandling(Func? handler = null) + { + return Use(next => async context => + { + try + { + await next(context); + } + catch (Exception ex) + { + if (handler != null) + await handler(context, ex); + else + context.Set("Exception", ex); + } + }); + } + + /// + /// 超时处理 + /// + public PipelineBuilder UseTimeout(TimeSpan timeout) + { + return Use(next => async context => + { + using var cts = new System.Threading.CancellationTokenSource(timeout); + var task = next(context); + var completed = await Task.WhenAny(task, Task.Delay(timeout)); + + if (completed != task) + { + context.Set("Timeout", true); + throw new TimeoutException($"管道执行超时: {timeout}"); + } + + await task; + }); + } + + /// + /// 日志记录 + /// + public PipelineBuilder UseLogging(Action? log = null) + { + return Use(async (context, next) => + { + log?.Invoke($"[{DateTime.Now:HH:mm:ss}] 开始执行管道"); + await next(context); + log?.Invoke($"[{DateTime.Now:HH:mm:ss}] 结束执行管道"); + }); + } + + /// + /// 计时 + /// + public PipelineBuilder UseTiming(Action? callback = null) + { + return Use(async (context, next) => + { + var sw = System.Diagnostics.Stopwatch.StartNew(); + await next(context); + sw.Stop(); + callback?.Invoke(sw.Elapsed); + context.Set("ElapsedTime", sw.Elapsed); + }); + } + + /// + /// 构建管道 + /// + public PipelineDelegate Build(PipelineDelegate? terminal = null) + { + terminal ??= _ => Task.CompletedTask; + + for (int i = _middlewares.Count - 1; i >= 0; i--) + { + terminal = _middlewares[i](terminal); + } + + return terminal; + } + } + + /// + /// 泛型管道(带结果类型) + /// + public class Pipeline + { + private readonly List>, Task>> _middlewares = new(); + + /// + /// 添加中间件 + /// + public Pipeline Use(Func>, Task> middleware) + { + _middlewares.Add(middleware); + return this; + } + + /// + /// 执行管道 + /// + public async Task ExecuteAsync(TInput input, Func> terminal) + { + Func> current = terminal; + + for (int i = _middlewares.Count - 1; i >= 0; i--) + { + var middleware = _middlewares[i]; + var next = current; + current = ctx => middleware(ctx, next); + } + + return await current(input); + } + } + + /// + /// 管道工具类 + /// + public static class PipelineUtil + { + /// + /// 创建管道构建器 + /// + public static PipelineBuilder CreateBuilder() + { + return new PipelineBuilder(); + } + + /// + /// 创建泛型管道 + /// + public static Pipeline Create() + { + return new Pipeline(); + } + + /// + /// 快速执行管道 + /// + public static async Task ExecuteAsync(Action configure, PipelineContext? context = null) + { + var builder = new PipelineBuilder(); + configure(builder); + var pipeline = builder.Build(); + await pipeline(context ?? new PipelineContext()); + } + } +} diff --git a/EasyTool.Core/ToolCategory/ProcessUtil.cs b/EasyTool.Core/ToolCategory/ProcessUtil.cs deleted file mode 100644 index 210f34d..0000000 --- a/EasyTool.Core/ToolCategory/ProcessUtil.cs +++ /dev/null @@ -1,188 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; - -namespace EasyTool -{ - /// - /// 进程工具类 - /// - public class ProcessUtil - { - /// - /// 通过进程名称获取进程 - /// - /// 进程名称 - /// 进程 - public static Process GetProcessByName(string processName) - { - // 获取当前运行的所有进程 - var processes = Process.GetProcessesByName(processName); - if (processes.Length > 0) - { - return processes[0]; - } - return null; - } - - /// - /// 获取进程的所有线程 - /// - /// 进程 - /// 线程集合 - public static ProcessThreadCollection GetProcessThreads(Process process) - { - return process.Threads; - } - - /// - /// 获取进程的主窗口句柄 - /// - /// 进程 - /// 窗口句柄 - public static IntPtr GetMainWindowHandle(Process process) - { - return process.MainWindowHandle; - } - - /// - /// 获取进程的主窗口标题 - /// - /// 进程 - /// 窗口标题 - public static string GetMainWindowTitle(Process process) - { - return process.MainWindowTitle; - } - - /// - /// 获取进程的所有模块 - /// - /// 进程 - /// 模块集合 - public static ProcessModuleCollection GetProcessModules(Process process) - { - return process.Modules; - } - - /// - /// 关闭进程 - /// - /// 进程 - public static void KillProcess(Process process) - { - process.Kill(); - } - - /// - /// 关闭进程并等待结束 - /// - /// 进程 - public static void KillProcessAndWait(Process process) - { - process.Kill(); - process.WaitForExit(); - } - - /// - /// 启动新进程 - /// - /// 文件名 - /// 新进程 - public static Process StartProcess(string fileName) - { - return Process.Start(fileName); - } - - /// - /// 启动新进程并等待结束 - /// - /// 文件名 - public static void StartProcessAndWait(string fileName) - { - var process = Process.Start(fileName); - process.WaitForExit(); - } - - /// - /// 判断进程是否存在 - /// - /// 进程名称 - /// 是否存在 - public static bool IsProcessExists(string processName) - { - return Process.GetProcessesByName(processName).Length > 0; - } - - /// - /// 获取进程使用的内存大小 - /// - /// 进程 - /// 内存大小(字节) - public static long GetProcessMemorySize(Process process) - { - return process.WorkingSet64; - } - - /// - /// 暂停进程 - /// - /// 进程 - public static void SuspendProcess(Process process) - { - foreach (ProcessThread thread in process.Threads) - { - IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); - if (pOpenThread == IntPtr.Zero) - { - break; - } - SuspendThread(pOpenThread); - CloseHandle(pOpenThread); - } - } - - /// - /// 恢复进程 - /// - /// 进程 - public static void ResumeProcess(Process process) - { - foreach (ProcessThread thread in process.Threads) - { - IntPtr pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id); - if (pOpenThread == IntPtr.Zero) - { - break; - } - ResumeThread(pOpenThread); - CloseHandle(pOpenThread); - } - } - - [DllImport("kernel32.dll")] - static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId); - [DllImport("kernel32.dll")] - static extern uint SuspendThread(IntPtr hThread); - [DllImport("kernel32.dll")] - static extern int ResumeThread(IntPtr hThread); - [DllImport("kernel32.dll")] - static extern IntPtr CloseHandle(IntPtr hObject); - [Flags] - public enum ThreadAccess : int - { - TERMINATE = (0x0001), - SUSPEND_RESUME = (0x0002), - GET_CONTEXT = (0x0008), - SET_CONTEXT = (0x0010), - SET_INFORMATION = (0x0020), - QUERY_INFORMATION = (0x0040), - SET_THREAD_TOKEN = (0x0080), - IMPERSONATE = (0x0100), - DIRECT_IMPERSONATION = (0x0200) - } - - } -} diff --git a/EasyTool.Core/ToolCategory/ProducerConsumer.cs b/EasyTool.Core/ToolCategory/ProducerConsumer.cs new file mode 100644 index 0000000..89a4087 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ProducerConsumer.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 生产者消费者模式工具类 + /// + /// 数据类型 + public class ProducerConsumer + { + private readonly System.Collections.Concurrent.BlockingCollection _collection; + private readonly Action _consumer; + private readonly int _maxConsumers; + private readonly List _consumerTasks; + private bool _isRunning; + private readonly object _lock = new(); + + /// + /// 队列中元素数量 + /// + public int Count => _collection.Count; + + /// + /// 是否正在运行 + /// + public bool IsRunning => _isRunning; + + /// + /// 是否已完成添加 + /// + public bool IsAddingCompleted => _collection.IsAddingCompleted; + + /// + /// 创建生产者消费者 + /// + /// 消费者处理函数 + /// 队列容量(0表示无限制) + /// 最大消费者数量 + public ProducerConsumer(Action consumer, int boundedCapacity = 0, int maxConsumers = 1) + { + _consumer = consumer ?? throw new ArgumentNullException(nameof(consumer)); + _maxConsumers = maxConsumers; + _collection = boundedCapacity > 0 + ? new System.Collections.Concurrent.BlockingCollection(boundedCapacity) + : new System.Collections.Concurrent.BlockingCollection(); + _consumerTasks = new List(); + } + + /// + /// 启动消费者 + /// + public void Start() + { + lock (_lock) + { + if (_isRunning) return; + _isRunning = true; + + for (int i = 0; i < _maxConsumers; i++) + { + _consumerTasks.Add(System.Threading.Tasks.Task.Run(ConsumeLoop)); + } + } + } + + /// + /// 生产数据 + /// + public void Produce(T item) + { + _collection.Add(item); + } + + /// + /// 批量生产数据 + /// + public void ProduceRange(IEnumerable items) + { + foreach (var item in items) + { + _collection.Add(item); + } + } + + /// + /// 尝试生产数据(不阻塞) + /// + public bool TryProduce(T item, TimeSpan timeout) + { + return _collection.TryAdd(item, timeout); + } + + /// + /// 标记添加完成 + /// + public void CompleteAdding() + { + _collection.CompleteAdding(); + } + + /// + /// 停止并等待完成 + /// + public void Stop() + { + lock (_lock) + { + if (!_isRunning) return; + _collection.CompleteAdding(); + System.Threading.Tasks.Task.WaitAll(_consumerTasks.ToArray()); + _isRunning = false; + } + } + + /// + /// 异步停止 + /// + public async System.Threading.Tasks.Task StopAsync() + { + lock (_lock) + { + if (!_isRunning) return; + _collection.CompleteAdding(); + } + + await System.Threading.Tasks.Task.WhenAll(_consumerTasks); + + lock (_lock) + { + _isRunning = false; + } + } + + private void ConsumeLoop() + { + foreach (var item in _collection.GetConsumingEnumerable()) + { + try + { + _consumer(item); + } + catch + { + // 忽略消费者异常,继续处理下一个 + } + } + } + } + + /// + /// 异步通道(类似Go的channel) + /// + /// 数据类型 + public class Channel + { + private readonly System.Collections.Concurrent.ConcurrentQueue _queue = new(); + private readonly System.Threading.SemaphoreSlim _signal = new(0); + private readonly int? _capacity; + private readonly System.Threading.SemaphoreSlim? _capacitySemaphore; + private bool _closed; + + /// + /// 队列中元素数量 + /// + public int Count => _queue.Count; + + /// + /// 是否已关闭 + /// + public bool IsClosed => _closed; + + /// + /// 创建通道 + /// + /// 容量(0或null表示无限制) + public Channel(int capacity = 0) + { + _capacity = capacity > 0 ? capacity : null; + if (_capacity.HasValue) + { + _capacitySemaphore = new System.Threading.SemaphoreSlim(_capacity.Value, _capacity.Value); + } + } + + /// + /// 发送数据 + /// + public async System.Threading.Tasks.Task SendAsync(T item) + { + if (_closed) + throw new InvalidOperationException("通道已关闭"); + + if (_capacitySemaphore != null) + { + await _capacitySemaphore.WaitAsync(); + } + + _queue.Enqueue(item); + _signal.Release(); + } + + /// + /// 尝试发送数据 + /// + public async System.Threading.Tasks.Task TrySendAsync(T item, TimeSpan timeout) + { + if (_closed) + return false; + + if (_capacitySemaphore != null) + { + if (!await _capacitySemaphore.WaitAsync(timeout)) + return false; + } + + _queue.Enqueue(item); + _signal.Release(); + return true; + } + + /// + /// 接收数据 + /// + public async System.Threading.Tasks.Task ReceiveAsync() + { + await _signal.WaitAsync(); + + if (_queue.TryDequeue(out var item)) + { + _capacitySemaphore?.Release(); + return item; + } + + throw new InvalidOperationException("通道状态异常"); + } + + /// + /// 尝试接收数据 + /// + public async System.Threading.Tasks.Task<(bool Success, T? Item)> TryReceiveAsync(TimeSpan timeout) + { + if (!await _signal.WaitAsync(timeout)) + return (false, default); + + if (_queue.TryDequeue(out var item)) + { + _capacitySemaphore?.Release(); + return (true, item); + } + + return (false, default); + } + + /// + /// 关闭通道 + /// + public void Close() + { + _closed = true; + } + + /// + /// 获取所有剩余数据 + /// + public IEnumerable GetAllRemaining() + { + while (_queue.TryDequeue(out var item)) + { + yield return item; + } + } + } + + /// + /// 并行执行工具 + /// + public static class ParallelUtil + { + /// + /// 并行执行任务并收集结果 + /// + public static async System.Threading.Tasks.Task> WhenAllAsync( + IEnumerable sources, + Func> selector, + int maxDegreeOfParallelism) + { + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + var tasks = sources.Select(async source => + { + await semaphore.WaitAsync(); + try + { + return await selector(source); + } + finally + { + semaphore.Release(); + } + }); + + var results = await System.Threading.Tasks.Task.WhenAll(tasks); + return results.ToList(); + } + + /// + /// 并行执行任务 + /// + public static async System.Threading.Tasks.Task WhenAllAsync( + IEnumerable sources, + Func action, + int maxDegreeOfParallelism) + { + var semaphore = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism); + var tasks = sources.Select(async source => + { + await semaphore.WaitAsync(); + try + { + await action(source); + } + finally + { + semaphore.Release(); + } + }); + + await System.Threading.Tasks.Task.WhenAll(tasks); + } + + /// + /// 分批并行执行 + /// + public static async System.Threading.Tasks.Task WhenAllBatchedAsync( + IEnumerable sources, + Func, System.Threading.Tasks.Task> batchAction, + int batchSize) + { + var batch = new List(batchSize); + + foreach (var source in sources) + { + batch.Add(source); + if (batch.Count >= batchSize) + { + await batchAction(batch); + batch = new List(batchSize); + } + } + + if (batch.Count > 0) + { + await batchAction(batch); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/RandomUtil.cs b/EasyTool.Core/ToolCategory/RandomUtil.cs deleted file mode 100644 index c8ea967..0000000 --- a/EasyTool.Core/ToolCategory/RandomUtil.cs +++ /dev/null @@ -1,247 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace EasyTool -{ - public class RandomUtil - { - private static readonly Random random = new Random(); - - /// - /// 生成指定范围内的随机整数 - /// - /// 随机整数的最小值 - /// 随机整数的最大值 - /// 生成的随机整数 - public static int RandomInt(int min, int max) - { - return random.Next(min, max); - } - - /// - /// 生成指定位数的随机数字字符串 - /// - /// 生成的随机数字字符串的长度 - /// 生成的随机数字字符串 - public static string RandomDigitString(int length) - { - string result = ""; - for (int i = 0; i < length; i++) - { - result += random.Next(10); - } - return result; - } - - /// - /// 生成指定位数的随机字母数字字符串 - /// - /// 生成的随机字母数字字符串的长度 - /// 生成的随机字母数字字符串 - public static string RandomAlphanumericString(int length) - { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - string result = ""; - for (int i = 0; i < length; i++) - { - result += chars[random.Next(chars.Length)]; - } - return result; - } - - /// - /// 生成指定长度的随机字母字符串 - /// - /// 生成的随机字母字符串的长度 - /// 生成的随机字母字符串 - public static string RandomLetterString(int length) - { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - string result = ""; - for (int i = 0; i < length; i++) - { - result += chars[random.Next(chars.Length)]; - } - return result; - } - - /// - /// 生成随机的布尔值 - /// - /// 生成的随机布尔值 - public static bool RandomBool() - { - return random.Next(2) == 0; - } - - /// - /// 生成指定长度的随机数组 - /// - /// 生成的随机数组的长度 - /// 生成的随机数组 - public static int[] RandomIntArray(int length) - { - int[] result = new int[length]; - for (int i = 0; i < length; i++) - { - result[i] = random.Next(); - } - return result; - } - - /// - /// 生成指定长度的随机双精度浮点数数组 - /// - /// 生成的随机数组的长度 - /// 生成的随机双精度浮点数数组 - public static double[] RandomDoubleArray(int length) - { - double[] result = new double[length]; - for (int i = 0; i < length; i++) - { - result[i] = random.NextDouble(); - } - return result; - } - - /// - /// 生成指定长度的随机字符串数组 - /// - /// 生成的随机数组的长度 - /// 每个随机字符串的长度 - /// 生成的随机字符串数组 - public static string[] RandomStringArray(int length, int strLength) - { - string[] result = new string[length]; - for (int i = 0; i < length; i++) - { - result[i] = RandomAlphanumericString(strLength); - } - return result; - } - - /// - /// 生成随机日期 - /// - /// 随机日期的最早时间 - /// 随机日期的最晚时间 - /// 生成的随机日期 - public static DateTime RandomDate(DateTime startDate, DateTime endDate) - { - TimeSpan timeSpan = endDate - startDate; - TimeSpan newSpan = new TimeSpan(0, 0, random.Next(0, (int)timeSpan.TotalSeconds)); - return startDate + newSpan; - } - - /// - /// 生成随机枚举值 - /// - /// 枚举类型 - /// 生成的随机枚举值 - public static T RandomEnumValue() - { - Array values = Enum.GetValues(typeof(T)); - return (T)values.GetValue(random.Next(values.Length)); - } - - /// - /// 获取一个指定范围内的随机整数 - /// - /// 最小值 - /// 最大值 - /// 随机整数 - public static int GetRandomInt(int minValue, int maxValue) - { - return random.Next(minValue, maxValue + 1); - } - - /// - /// 获取一个指定范围内的随机双精度浮点数 - /// - /// 最小值 - /// 最大值 - /// 随机双精度浮点数 - public static double GetRandomDouble(double minValue, double maxValue) - { - return minValue + (maxValue - minValue) * random.NextDouble(); - } - - /// - /// 获取一个指定范围内的随机日期时间 - /// - /// 最小值 - /// 最大值 - /// 随机日期时间 - public static DateTime GetRandomDateTime(DateTime minValue, DateTime maxValue) - { - TimeSpan timeSpan = maxValue - minValue; - double totalSeconds = timeSpan.TotalSeconds; - int randomSeconds = GetRandomInt(0, (int)totalSeconds); - return minValue.AddSeconds(randomSeconds); - } - - /// - /// 从给定的集合中随机选取一个元素 - /// - /// 元素类型 - /// 集合 - /// 随机选取的元素 - public static T GetRandomElement(IEnumerable source) - { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } - - int count = source.Count(); - if (count == 0) - { - throw new ArgumentException("集合中必须至少有一个元素", nameof(source)); - } - - int index = GetRandomInt(0, count - 1); - return source.ElementAt(index); - } - - /// - /// 生成指定长度的随机数字字符串 - /// - /// 字符串长度 - /// 随机数字字符串 - public static string RandomNumberString(int length) - { - string result = ""; - for (int i = 0; i < length; i++) - { - result += random.Next(10).ToString(); - } - return result; - } - - /// - /// 生成指定长度的随机字母数字字符串 - /// - /// 字符串长度 - /// 随机字母数字字符串 - public static string RandomString(int length) - { - string result = ""; - for (int i = 0; i < length; i++) - { - int code = random.Next(36) + 48; - if (code >= 58 && code <= 64) - { - code += 7; - } - if (code >= 91 && code <= 96) - { - code += 6; - } - result += Convert.ToChar(code); - } - return result; - } - } -} diff --git a/EasyTool.Core/ToolCategory/RateLimitUtil.cs b/EasyTool.Core/ToolCategory/RateLimitUtil.cs new file mode 100644 index 0000000..0a9b4f4 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RateLimitUtil.cs @@ -0,0 +1,557 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 限流工具类 + /// 提供令牌桶、漏桶、滑动窗口等限流算法 + /// + public static class RateLimitUtil + { + #region 令牌桶限流器 + + /// + /// 创建令牌桶限流器 + /// + /// 桶容量(最大令牌数) + /// 每秒补充的令牌数 + /// 令牌桶限流器 + public static TokenBucketLimiter CreateTokenBucket(int capacity, double refillRate) + { + return new TokenBucketLimiter(capacity, refillRate); + } + + /// + /// 令牌桶限流器 + /// + public class TokenBucketLimiter + { + private readonly int _capacity; + private readonly double _refillRate; + private double _tokens; + private long _lastRefillTime; + private readonly object _lock = new(); + + public TokenBucketLimiter(int capacity, double refillRate) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + if (refillRate <= 0) + throw new ArgumentOutOfRangeException(nameof(refillRate), "补充速率必须大于0"); + + _capacity = capacity; + _refillRate = refillRate; + _tokens = capacity; + _lastRefillTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试获取令牌 + /// + /// 请求的令牌数 + /// 是否获取成功 + public bool TryAcquire(int tokens = 1) + { + lock (_lock) + { + Refill(); + + if (_tokens >= tokens) + { + _tokens -= tokens; + return true; + } + + return false; + } + } + + /// + /// 异步等待获取令牌 + /// + /// 请求的令牌数 + /// 取消令牌 + public async Task WaitAsync(int tokens = 1, CancellationToken cancellationToken = default) + { + while (!TryAcquire(tokens)) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前可用令牌数 + /// + public double AvailableTokens + { + get + { + lock (_lock) + { + Refill(); + return _tokens; + } + } + } + + private void Refill() + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _lastRefillTime; + var refill = elapsed * _refillRate / 1000.0; + + if (refill > 0) + { + _tokens = Math.Min(_capacity, _tokens + refill); + _lastRefillTime = now; + } + } + } + + #endregion + + #region 漏桶限流器 + + /// + /// 创建漏桶限流器 + /// + /// 桶容量 + /// 每秒漏出的请求数 + /// 漏桶限流器 + public static LeakyBucketLimiter CreateLeakyBucket(int capacity, double leakRate) + { + return new LeakyBucketLimiter(capacity, leakRate); + } + + /// + /// 漏桶限流器 + /// + public class LeakyBucketLimiter + { + private readonly int _capacity; + private readonly double _leakRate; + private double _water; + private long _lastLeakTime; + private readonly object _lock = new(); + + public LeakyBucketLimiter(int capacity, double leakRate) + { + if (capacity <= 0) + throw new ArgumentOutOfRangeException(nameof(capacity), "容量必须大于0"); + if (leakRate <= 0) + throw new ArgumentOutOfRangeException(nameof(leakRate), "漏出速率必须大于0"); + + _capacity = capacity; + _leakRate = leakRate; + _water = 0; + _lastLeakTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试添加请求到桶中 + /// + /// 是否添加成功 + public bool TryAcquire() + { + lock (_lock) + { + Leak(); + + if (_water < _capacity) + { + _water++; + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + private void Leak() + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _lastLeakTime; + var leaked = elapsed * _leakRate / 1000.0; + + if (leaked > 0) + { + _water = Math.Max(0, _water - leaked); + _lastLeakTime = now; + } + } + } + + #endregion + + #region 滑动窗口限流器 + + /// + /// 创建滑动窗口限流器 + /// + /// 窗口内最大请求数 + /// 窗口大小(秒) + /// 滑动窗口限流器 + public static SlidingWindowLimiter CreateSlidingWindow(int limit, int windowSeconds) + { + return new SlidingWindowLimiter(limit, windowSeconds); + } + + /// + /// 滑动窗口限流器 + /// + public class SlidingWindowLimiter + { + private readonly int _limit; + private readonly long _windowTicks; + private readonly ConcurrentQueue _timestamps = new(); + private readonly object _lock = new(); + + public SlidingWindowLimiter(int limit, int windowSeconds) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit), "限制必须大于0"); + if (windowSeconds <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSeconds), "窗口大小必须大于0"); + + _limit = limit; + _windowTicks = windowSeconds * 1000L; + } + + /// + /// 尝试通过请求 + /// + /// 是否允许通过 + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var windowStart = now - _windowTicks; + + // 移除过期的请求记录 + while (_timestamps.TryPeek(out var timestamp) && timestamp < windowStart) + { + _timestamps.TryDequeue(out _); + } + + if (_timestamps.Count < _limit) + { + _timestamps.Enqueue(now); + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var windowStart = now - _windowTicks; + + while (_timestamps.TryPeek(out var timestamp) && timestamp < windowStart) + { + _timestamps.TryDequeue(out _); + } + + return _timestamps.Count; + } + } + } + } + + #endregion + + #region 固定窗口限流器 + + /// + /// 创建固定窗口限流器 + /// + /// 窗口内最大请求数 + /// 窗口大小(秒) + /// 固定窗口限流器 + public static FixedWindowLimiter CreateFixedWindow(int limit, int windowSeconds) + { + return new FixedWindowLimiter(limit, windowSeconds); + } + + /// + /// 固定窗口限流器 + /// + public class FixedWindowLimiter + { + private readonly int _limit; + private readonly long _windowTicks; + private int _count; + private long _windowStart; + private readonly object _lock = new(); + + public FixedWindowLimiter(int limit, int windowSeconds) + { + if (limit <= 0) + throw new ArgumentOutOfRangeException(nameof(limit), "限制必须大于0"); + if (windowSeconds <= 0) + throw new ArgumentOutOfRangeException(nameof(windowSeconds), "窗口大小必须大于0"); + + _limit = limit; + _windowTicks = windowSeconds * 1000L; + _count = 0; + _windowStart = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + /// + /// 尝试通过请求 + /// + /// 是否允许通过 + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + // 检查是否需要重置窗口 + if (now - _windowStart >= _windowTicks) + { + _windowStart = now; + _count = 0; + } + + if (_count < _limit) + { + _count++; + return true; + } + + return false; + } + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(CancellationToken cancellationToken = default) + { + while (!TryAcquire()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 获取当前窗口内的请求数 + /// + public int CurrentCount + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + + if (now - _windowStart >= _windowTicks) + { + return 0; + } + + return _count; + } + } + } + + /// + /// 获取窗口重置剩余时间(毫秒) + /// + public long ResetIn + { + get + { + lock (_lock) + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var elapsed = now - _windowStart; + return Math.Max(0, _windowTicks - elapsed); + } + } + } + } + + #endregion + + #region 并发限流器 + + /// + /// 创建并发限流器 + /// + /// 最大并发数 + /// 并发限流器 + public static ConcurrencyLimiter CreateConcurrency(int maxConcurrency) + { + return new ConcurrencyLimiter(maxConcurrency); + } + + /// + /// 并发限流器 + /// + public class ConcurrencyLimiter + { + private readonly SemaphoreSlim _semaphore; + + public ConcurrencyLimiter(int maxConcurrency) + { + if (maxConcurrency <= 0) + throw new ArgumentOutOfRangeException(nameof(maxConcurrency), "最大并发数必须大于0"); + + _semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency); + } + + /// + /// 获取执行许可 + /// + public async Task AcquireAsync(CancellationToken cancellationToken = default) + { + await _semaphore.WaitAsync(cancellationToken); + return new ReleaseDisposable(_semaphore); + } + + /// + /// 尝试获取执行许可 + /// + public IDisposable? TryAcquire() + { + if (_semaphore.Wait(0)) + { + return new ReleaseDisposable(_semaphore); + } + return null; + } + + /// + /// 当前可用许可数 + /// + public int AvailablePermits => _semaphore.CurrentCount; + + private class ReleaseDisposable : IDisposable + { + private readonly SemaphoreSlim _semaphore; + + public ReleaseDisposable(SemaphoreSlim semaphore) + { + _semaphore = semaphore; + } + + public void Dispose() + { + _semaphore.Release(); + } + } + } + + #endregion + + #region 分布式限流器(内存模拟版) + + /// + /// 创建分布式限流器(基于内存的键值对) + /// + /// 默认限制 + /// 窗口大小(秒) + /// 分布式限流器 + public static DistributedLimiter CreateDistributed(int defaultLimit, int windowSeconds) + { + return new DistributedLimiter(defaultLimit, windowSeconds); + } + + /// + /// 分布式限流器(内存模拟) + /// + public class DistributedLimiter + { + private readonly int _defaultLimit; + private readonly int _windowSeconds; + private readonly ConcurrentDictionary _limiters = new(); + + public DistributedLimiter(int defaultLimit, int windowSeconds) + { + _defaultLimit = defaultLimit; + _windowSeconds = windowSeconds; + } + + /// + /// 尝试通过请求 + /// + /// 限流键(如用户ID、IP等) + /// 是否允许通过 + public bool TryAcquire(string key) + { + var limiter = _limiters.GetOrAdd(key, _ => new FixedWindowLimiter(_defaultLimit, _windowSeconds)); + return limiter.TryAcquire(); + } + + /// + /// 异步等待 + /// + public async Task WaitAsync(string key, CancellationToken cancellationToken = default) + { + while (!TryAcquire(key)) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(10, cancellationToken); + } + } + + /// + /// 移除指定键的限流器 + /// + public void Remove(string key) + { + _limiters.TryRemove(key, out _); + } + + /// + /// 清除所有限流器 + /// + public void Clear() + { + _limiters.Clear(); + } + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/RateLimiter.cs b/EasyTool.Core/ToolCategory/RateLimiter.cs new file mode 100644 index 0000000..6280275 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RateLimiter.cs @@ -0,0 +1,340 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 限流算法 + /// + public enum RateLimitAlgorithm + { + /// + /// 固定窗口 + /// + FixedWindow, + + /// + /// 滑动窗口 + /// + SlidingWindow, + + /// + /// 令牌桶 + /// + TokenBucket, + + /// + /// 漏桶 + /// + LeakyBucket + } + + /// + /// 固定窗口限流器 + /// + public class FixedWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private int _count; + private DateTime _windowStart; + private readonly object _lock = new(); + + public FixedWindowRateLimiter(int limit, TimeSpan window) + { + _limit = limit; + _window = window; + _count = 0; + _windowStart = DateTime.UtcNow; + } + + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTime.UtcNow; + if (now - _windowStart >= _window) + { + _windowStart = now; + _count = 0; + } + + if (_count < _limit) + { + _count++; + return true; + } + return false; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + var remaining = _window - (DateTime.UtcNow - _windowStart); + return remaining > TimeSpan.Zero ? remaining : TimeSpan.Zero; + } + } + } + + /// + /// 滑动窗口限流器 + /// + public class SlidingWindowRateLimiter + { + private readonly int _limit; + private readonly TimeSpan _window; + private readonly System.Collections.Generic.Queue _timestamps; + private readonly object _lock = new(); + + public SlidingWindowRateLimiter(int limit, TimeSpan window) + { + _limit = limit; + _window = window; + _timestamps = new(); + } + + public bool TryAcquire() + { + lock (_lock) + { + var now = DateTime.UtcNow; + var cutoff = now - _window; + + while (_timestamps.Count > 0 && _timestamps.Peek() < cutoff) + { + _timestamps.Dequeue(); + } + + if (_timestamps.Count < _limit) + { + _timestamps.Enqueue(now); + return true; + } + return false; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + if (_timestamps.Count == 0) + return TimeSpan.Zero; + + var oldest = _timestamps.Peek(); + var waitTime = oldest + _window - DateTime.UtcNow; + return waitTime > TimeSpan.Zero ? waitTime : TimeSpan.Zero; + } + } + } + + /// + /// 令牌桶限流器 + /// + public class TokenBucketRateLimiter + { + private readonly int _capacity; + private readonly int _refillRate; + private int _tokens; + private DateTime _lastRefill; + private readonly object _lock = new(); + + public TokenBucketRateLimiter(int capacity, int refillRate) + { + _capacity = capacity; + _refillRate = refillRate; + _tokens = capacity; + _lastRefill = DateTime.UtcNow; + } + + public bool TryAcquire(int tokens = 1) + { + lock (_lock) + { + RefillTokens(); + + if (_tokens >= tokens) + { + _tokens -= tokens; + return true; + } + return false; + } + } + + private void RefillTokens() + { + var now = DateTime.UtcNow; + var elapsed = (now - _lastRefill).TotalSeconds; + var tokensToAdd = (int)(elapsed * _refillRate); + + if (tokensToAdd > 0) + { + _tokens = Math.Min(_capacity, _tokens + tokensToAdd); + _lastRefill = now; + } + } + + public TimeSpan GetWaitTime(int tokens = 1) + { + lock (_lock) + { + RefillTokens(); + if (_tokens >= tokens) + return TimeSpan.Zero; + + var tokensNeeded = tokens - _tokens; + return TimeSpan.FromSeconds((double)tokensNeeded / _refillRate); + } + } + } + + /// + /// 漏桶限流器 + /// + public class LeakyBucketRateLimiter + { + private readonly int _capacity; + private readonly int _leakRate; + private int _water; + private DateTime _lastLeak; + private readonly object _lock = new(); + + public LeakyBucketRateLimiter(int capacity, int leakRate) + { + _capacity = capacity; + _leakRate = leakRate; + _water = 0; + _lastLeak = DateTime.UtcNow; + } + + public bool TryAcquire() + { + lock (_lock) + { + Leak(); + + if (_water < _capacity) + { + _water++; + return true; + } + return false; + } + } + + private void Leak() + { + var now = DateTime.UtcNow; + var elapsed = (now - _lastLeak).TotalSeconds; + var leaked = (int)(elapsed * _leakRate); + + if (leaked > 0) + { + _water = Math.Max(0, _water - leaked); + _lastLeak = now; + } + } + + public TimeSpan GetWaitTime() + { + lock (_lock) + { + Leak(); + if (_water < _capacity) + return TimeSpan.Zero; + + return TimeSpan.FromSeconds((double)1 / _leakRate); + } + } + } + + /// + /// 限流器工具类 + /// + public static class RateLimiter + { + /// + /// 创建限流器 + /// + public static IRateLimiter Create(RateLimitAlgorithm algorithm, int limit, TimeSpan window) + { + return algorithm switch + { + RateLimitAlgorithm.FixedWindow => new FixedWindowRateLimiterWrapper(limit, window), + RateLimitAlgorithm.SlidingWindow => new SlidingWindowRateLimiterWrapper(limit, window), + RateLimitAlgorithm.TokenBucket => new TokenBucketRateLimiterWrapper(limit, (int)(limit / window.TotalSeconds)), + RateLimitAlgorithm.LeakyBucket => new LeakyBucketRateLimiterWrapper(limit, (int)(limit / window.TotalSeconds)), + _ => throw new ArgumentException("不支持的限流算法") + }; + } + + /// + /// 使用限流器执行操作 + /// + public static async Task ExecuteWithRateLimitAsync(IRateLimiter limiter, Func> action) + { + while (!limiter.TryAcquire()) + { + await Task.Delay(limiter.GetWaitTime()); + } + return await action(); + } + + /// + /// 使用限流器执行操作 + /// + public static async Task ExecuteWithRateLimitAsync(IRateLimiter limiter, Func action) + { + while (!limiter.TryAcquire()) + { + await Task.Delay(limiter.GetWaitTime()); + } + await action(); + } + } + + /// + /// 限流器接口 + /// + public interface IRateLimiter + { + bool TryAcquire(); + TimeSpan GetWaitTime(); + } + + internal class FixedWindowRateLimiterWrapper : IRateLimiter + { + private readonly FixedWindowRateLimiter _limiter; + public FixedWindowRateLimiterWrapper(int limit, TimeSpan window) => _limiter = new FixedWindowRateLimiter(limit, window); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class SlidingWindowRateLimiterWrapper : IRateLimiter + { + private readonly SlidingWindowRateLimiter _limiter; + public SlidingWindowRateLimiterWrapper(int limit, TimeSpan window) => _limiter = new SlidingWindowRateLimiter(limit, window); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class TokenBucketRateLimiterWrapper : IRateLimiter + { + private readonly TokenBucketRateLimiter _limiter; + public TokenBucketRateLimiterWrapper(int capacity, int refillRate) => _limiter = new TokenBucketRateLimiter(capacity, refillRate); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } + + internal class LeakyBucketRateLimiterWrapper : IRateLimiter + { + private readonly LeakyBucketRateLimiter _limiter; + public LeakyBucketRateLimiterWrapper(int capacity, int leakRate) => _limiter = new LeakyBucketRateLimiter(capacity, leakRate); + public bool TryAcquire() => _limiter.TryAcquire(); + public TimeSpan GetWaitTime() => _limiter.GetWaitTime(); + } +} diff --git a/EasyTool.Core/ToolCategory/ReflectUtil.cs b/EasyTool.Core/ToolCategory/ReflectUtil.cs deleted file mode 100644 index a2a6374..0000000 --- a/EasyTool.Core/ToolCategory/ReflectUtil.cs +++ /dev/null @@ -1,247 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; - -namespace EasyTool -{ - /// - /// 反射工具类 - /// - public class ReflectUtil - { - /// - /// 根据类型名称获取Type对象 - /// - /// 类型名称 - /// Type对象 - public static Type GetType(string typeName) - { - return Type.GetType(typeName); - } - - /// - /// 获取指定程序集中的所有类型 - /// - /// 程序集 - /// 类型数组 - public static Type[] GetTypes(Assembly assembly) - { - return assembly.GetTypes(); - } - - /// - /// 获取指定类型所在的程序集 - /// - /// 类型 - /// 程序集 - public static Assembly GetAssembly(Type type) - { - return type.Assembly; - } - - /// - /// 获取指定类型的指定类型的特性 - /// - /// 特性类型 - /// 类型 - /// 特性对象 - public static T GetAttribute(Type type) where T : Attribute - { - return type.GetCustomAttribute(); - } - - /// - /// 获取指定类型的指定类型的特性数组 - /// - /// 特性类型 - /// 类型 - /// 特性数组 - public static T[] GetAttributes(Type type) where T : Attribute - { - return type.GetCustomAttributes().ToArray(); - } - - /// - /// 获取指定类型的默认值 - /// - /// 类型 - /// 默认值 - public static object GetDefaultValue(Type type) - { - return type.IsValueType ? Activator.CreateInstance(type) : null; - } - - /// - /// 获取类型的基类 - /// - /// 类型 - /// 基类 - public static Type GetBaseType(Type type) - { - return type.BaseType; - } - - /// - /// 判断类型是否实现了某个接口 - /// - /// 类型 - /// 接口类型 - /// 是否实现 - public static bool HasInterface(Type type, Type interfaceType) - { - return interfaceType.IsAssignableFrom(type); - } - - /// - /// 获取方法的参数信息 - /// - /// 方法 - /// 参数信息数组 - public static ParameterInfo[] GetParameters(MethodInfo method) - { - return method.GetParameters(); - } - - /// - /// 获取类型的所有构造函数 - /// - /// 类型 - /// 构造函数数组 - public static ConstructorInfo[] GetConstructors(Type type) - { - return type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - } - - /// - /// 获取类型的所有属性 - /// - /// 类型 - /// 属性数组 - public static PropertyInfo[] GetProperties(Type type) - { - return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - } - - /// - /// 获取类型的所有字段 - /// - /// 类型 - /// 字段数组 - public static FieldInfo[] GetFields(Type type) - { - return type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - } - - /// - /// 获取类型的所有方法 - /// - /// 类型 - /// 方法数组 - public static MethodInfo[] GetMethods(Type type) - { - return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - } - - /// - /// 获取类型的所有事件 - /// - /// 类型 - /// 事件数组 - public static EventInfo[] GetEvents(Type type) - { - return type.GetEvents(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - } - - /// - /// 获取类型的所有接口 - /// - /// 类型 - /// 接口数组 - public static Type[] GetInterfaces(Type type) - { - return type.GetInterfaces(); - } - - /// - /// 获取类型的所有属性名 - /// - /// 类型 - /// 属性名数组 - public static string[] GetPropertyNames(Type type) - { - return GetProperties(type).Select(p => p.Name).ToArray(); - } - - /// - /// 获取类型的所有字段名 - /// - /// 类型 - /// 字段名数组 - public static string[] GetFieldNames(Type type) - { - return GetFields(type).Select(f => f.Name).ToArray(); - } - - /// - /// 获取类型的所有方法名 - /// - /// 类型 - /// 方法名数组 - public static string[] GetMethodNames(Type type) - { - return GetMethods(type).Select(m => m.Name).ToArray(); - } - - /// - /// 获取类型的所有事件名 - /// - /// 类型 - /// 事件名数组 - public static string[] GetEventNames(Type type) - { - return GetEvents(type).Select(e => e.Name).ToArray(); - } - - /// - /// 获取类型的所有接口名 - /// - /// 类型 - /// 接口名数组 - public static string[] GetInterfaceNames(Type type) - { - return GetInterfaces(type).Select(i => i.Name).ToArray(); - } - - /// - /// 创建类型的实例 - /// - /// 类型 - /// 构造函数参数 - /// 实例 - public static object CreateInstance(Type type, params object[] args) - { - ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, - null, args.Select(a => a.GetType()).ToArray(), null); - if (constructor == null) - { - throw new ArgumentException($"Type {type} does not have a constructor with specified arguments"); - } - return constructor.Invoke(args); - } - - /// - /// 调用泛型方法 - /// - /// 调用方法的对象 - /// 方法名 - /// 泛型参数类型 - /// 方法参数 - /// 方法返回值 - public static object InvokeGenericMethod(object obj, string methodName, Type genericType, params object[] args) - { - MethodInfo method = obj.GetType().GetMethod(methodName); - MethodInfo genericMethod = method.MakeGenericMethod(genericType); - return genericMethod.Invoke(obj, args); - } - } -} diff --git a/EasyTool.Core/ToolCategory/ResultUtil.cs b/EasyTool.Core/ToolCategory/ResultUtil.cs new file mode 100644 index 0000000..a2905c0 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ResultUtil.cs @@ -0,0 +1,295 @@ +using System; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 结果类型 + /// + public class Result + { + /// + /// 是否成功 + /// + public bool IsSuccess { get; protected set; } + + /// + /// 是否失败 + /// + public bool IsFailure => !IsSuccess; + + /// + /// 错误信息 + /// + public string? Error { get; protected set; } + + /// + /// 错误代码 + /// + public string? ErrorCode { get; protected set; } + + protected Result(bool isSuccess, string? error = null, string? errorCode = null) + { + IsSuccess = isSuccess; + Error = error; + ErrorCode = errorCode; + } + + /// + /// 创建成功结果 + /// + public static Result Success() => new Result(true); + + /// + /// 创建失败结果 + /// + public static Result Failure(string error, string? errorCode = null) => new Result(false, error, errorCode); + + /// + /// 创建成功结果(带值) + /// + public static Result Success(T value) => new Result(true, value); + + /// + /// 创建失败结果(带值) + /// + public static Result Failure(string error, string? errorCode = null) => new Result(false, default, error, errorCode); + + /// + /// 从异常创建失败结果 + /// + public static Result FromException(Exception ex) => Failure(ex.Message, ex.GetType().Name); + + /// + /// 从异常创建失败结果 + /// + public static Result FromException(Exception ex) => Failure(ex.Message, ex.GetType().Name); + + /// + /// 匹配处理 + /// + public void Match(Action onSuccess, Action onFailure) + { + if (IsSuccess) onSuccess(); + else onFailure(Error ?? ""); + } + + /// + /// 匹配处理并返回值 + /// + public T Match(Func onSuccess, Func onFailure) + { + return IsSuccess ? onSuccess() : onFailure(Error ?? ""); + } + + /// + /// 绑定下一个操作 + /// + public Result Bind(Func next) + { + return IsSuccess ? next() : Failure(Error!, ErrorCode); + } + + /// + /// 映射 + /// + public Result Map(Func mapper) + { + return IsSuccess ? Success(mapper()) : Failure(Error!, ErrorCode); + } + + /// + /// 异步匹配处理 + /// + public async Task MatchAsync(Func onSuccess, Action onFailure) + { + if (IsSuccess) await onSuccess(); + else onFailure(Error ?? ""); + } + } + + /// + /// 带值的结果类型 + /// + public class Result : Result + { + /// + /// 值 + /// + public T? Value { get; } + + internal Result(bool isSuccess, T? value, string? error = null, string? errorCode = null) + : base(isSuccess, error, errorCode) + { + Value = value; + } + + /// + /// 获取值,失败则抛出异常 + /// + public T GetValueOrThrow() + { + if (IsFailure) + throw new InvalidOperationException(Error ?? "操作失败"); + return Value!; + } + + /// + /// 获取值或默认值 + /// + public T GetValueOrDefault(T defaultValue) + { + return IsSuccess ? Value! : defaultValue; + } + + /// + /// 匹配处理 + /// + public void Match(Action onSuccess, Action onFailure) + { + if (IsSuccess) onSuccess(Value!); + else onFailure(Error ?? ""); + } + + /// + /// 匹配处理并返回值 + /// + public TResult Match(Func onSuccess, Func onFailure) + { + return IsSuccess ? onSuccess(Value!) : onFailure(Error ?? ""); + } + + /// + /// 绑定下一个操作 + /// + public Result Bind(Func> next) + { + return IsSuccess ? next(Value!) : Failure(Error!, ErrorCode); + } + + /// + /// 映射 + /// + public Result Map(Func mapper) + { + return IsSuccess ? Success(mapper(Value!)) : Failure(Error!, ErrorCode); + } + + /// + /// 异步绑定 + /// + public async Task> BindAsync(Func>> next) + { + return IsSuccess ? await next(Value!) : Failure(Error!, ErrorCode); + } + + /// + /// 异步映射 + /// + public async Task> MapAsync(Func> mapper) + { + return IsSuccess ? Success(await mapper(Value!)) : Failure(Error!, ErrorCode); + } + + /// + /// 隐式转换 + /// + public static implicit operator Result(T value) => Success(value); + } + + /// + /// 结果工具类 + /// + public static class ResultUtil + { + /// + /// 组合多个结果 + /// + public static Result Combine(params Result[] results) + { + foreach (var result in results) + { + if (result.IsFailure) + return result; + } + return Result.Success(); + } + + /// + /// 组合多个结果 + /// + public static Result Combine(params Result[] results) + { + var values = new T[results.Length]; + for (int i = 0; i < results.Length; i++) + { + if (results[i].IsFailure) + return Result.Failure(results[i].Error!, results[i].ErrorCode); + values[i] = results[i].Value!; + } + return Result.Success(values); + } + + /// + /// 尝试执行 + /// + public static Result Try(Action action) + { + try + { + action(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 尝试执行 + /// + public static Result Try(Func func) + { + try + { + return Result.Success(func()); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 异步尝试执行 + /// + public static async Task TryAsync(Func action) + { + try + { + await action(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + + /// + /// 异步尝试执行 + /// + public static async Task> TryAsync(Func> func) + { + try + { + return Result.Success(await func()); + } + catch (Exception ex) + { + return Result.FromException(ex); + } + } + } +} diff --git a/EasyTool.Core/ToolCategory/RetryUtil.cs b/EasyTool.Core/ToolCategory/RetryUtil.cs new file mode 100644 index 0000000..5dc4dc5 --- /dev/null +++ b/EasyTool.Core/ToolCategory/RetryUtil.cs @@ -0,0 +1,397 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 重试工具类 + /// + public static class RetryUtil + { + /// + /// 重试执行操作 + /// + /// 要执行的操作 + /// 最大重试次数 + /// 重试间隔 + /// 重试时的回调 + public static void Execute( + Action action, + int maxRetries = 3, + TimeSpan? delay = null, + Action? onRetry = null) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) + { + try + { + action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + onRetry?.Invoke(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 重试执行操作(带返回值) + /// + public static T Execute( + Func func, + int maxRetries = 3, + TimeSpan? delay = null, + Action? onRetry = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) + { + try + { + return func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + onRetry?.Invoke(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 异步重试执行 + /// + public static async Task ExecuteAsync( + Func action, + int maxRetries = 3, + TimeSpan? delay = null, + Func? onRetry = null, + CancellationToken cancellationToken = default) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + await action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + if (onRetry != null) + await onRetry(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + await Task.Delay(delayValue, cancellationToken); + } + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 异步重试执行(带返回值) + /// + public static async Task ExecuteAsync( + Func> func, + int maxRetries = 3, + TimeSpan? delay = null, + Func? onRetry = null, + CancellationToken cancellationToken = default) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + + Exception? lastException = null; + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + return await func(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + if (onRetry != null) + await onRetry(ex, i + 1); + + if (delayValue > TimeSpan.Zero) + { + await Task.Delay(delayValue, cancellationToken); + } + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 指数退避重试 + /// + public static async Task ExecuteWithBackoffAsync( + Func action, + int maxRetries = 5, + TimeSpan? initialDelay = null, + double multiplier = 2.0, + TimeSpan? maxDelay = null, + CancellationToken cancellationToken = default) + { + if (action == null) + throw new ArgumentNullException(nameof(action)); + + var delay = initialDelay ?? TimeSpan.FromSeconds(1); + var max = maxDelay ?? TimeSpan.FromMinutes(5); + Exception? lastException = null; + + for (int i = 0; i <= maxRetries; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + await action(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < maxRetries) + { + var currentDelay = delay * Math.Pow(multiplier, i); + currentDelay = TimeSpan.FromTicks(Math.Min(currentDelay.Ticks, max.Ticks)); + + await Task.Delay(currentDelay, cancellationToken); + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + + /// + /// 带条件判断的重试 + /// + public static T Execute( + Func func, + Func shouldRetry, + int maxRetries = 3, + TimeSpan? delay = null) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + if (shouldRetry == null) + throw new ArgumentNullException(nameof(shouldRetry)); + + var delayValue = delay ?? TimeSpan.FromSeconds(1); + + for (int i = 0; i <= maxRetries; i++) + { + var result = func(); + + if (!shouldRetry(result)) + return result; + + if (i < maxRetries && delayValue > TimeSpan.Zero) + { + Thread.Sleep(delayValue); + } + } + + return func(); + } + + /// + /// 使用重试策略执行 + /// + public static async Task ExecuteAsync( + Func> func, + RetryPolicy policy, + CancellationToken cancellationToken = default) + { + if (func == null) + throw new ArgumentNullException(nameof(func)); + if (policy == null) + throw new ArgumentNullException(nameof(policy)); + + Exception? lastException = null; + var delay = policy.InitialDelay; + + for (int i = 0; i <= policy.MaxRetries; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + return await func(); + } + catch (Exception ex) + { + if (!policy.ShouldRetry(ex)) + throw; + + lastException = ex; + + if (i < policy.MaxRetries) + { + await Task.Delay(delay, cancellationToken); + + // 计算下次延迟 + delay = policy.BackoffStrategy switch + { + BackoffStrategy.Linear => policy.InitialDelay, + BackoffStrategy.Exponential => TimeSpan.FromTicks(delay.Ticks * 2), + BackoffStrategy.Fixed => policy.InitialDelay, + _ => policy.InitialDelay + }; + + delay = TimeSpan.FromTicks(Math.Min(delay.Ticks, policy.MaxDelay.Ticks)); + } + } + } + + throw lastException ?? new Exception("重试失败"); + } + } + + /// + /// 重试策略 + /// + public class RetryPolicy + { + /// + /// 最大重试次数 + /// + public int MaxRetries { get; set; } = 3; + + /// + /// 初始延迟 + /// + public TimeSpan InitialDelay { get; set; } = TimeSpan.FromSeconds(1); + + /// + /// 最大延迟 + /// + public TimeSpan MaxDelay { get; set; } = TimeSpan.FromMinutes(1); + + /// + /// 退避策略 + /// + public BackoffStrategy BackoffStrategy { get; set; } = BackoffStrategy.Exponential; + + /// + /// 判断是否应该重试 + /// + public Func? ShouldRetry { get; set; } + + /// + /// 创建默认策略 + /// + public static RetryPolicy Default => new(); + + /// + /// 创建快速重试策略 + /// + public static RetryPolicy Fast => new() + { + MaxRetries = 3, + InitialDelay = TimeSpan.FromMilliseconds(100), + MaxDelay = TimeSpan.FromSeconds(5), + BackoffStrategy = BackoffStrategy.Linear + }; + + /// + /// 创建网络重试策略 + /// + public static RetryPolicy Network => new() + { + MaxRetries = 5, + InitialDelay = TimeSpan.FromSeconds(1), + MaxDelay = TimeSpan.FromSeconds(30), + BackoffStrategy = BackoffStrategy.Exponential, + ShouldRetry = ex => ex is TimeoutException || + ex is System.Net.WebException || + ex is System.Net.Http.HttpRequestException + }; + } + + /// + /// 退避策略 + /// + public enum BackoffStrategy + { + /// + /// 固定延迟 + /// + Fixed, + + /// + /// 线性递增 + /// + Linear, + + /// + /// 指数递增 + /// + Exponential + } +} diff --git a/EasyTool.Core/ToolCategory/RuntimeUtil.cs b/EasyTool.Core/ToolCategory/RuntimeUtil.cs deleted file mode 100644 index e586afc..0000000 --- a/EasyTool.Core/ToolCategory/RuntimeUtil.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; - -namespace EasyTool -{ - /// - /// 运行时工具 - /// - public class RuntimeUtil - { - /// - /// 获取当前运行的 .NET 版本 - /// - /// .NET 版本 - public static string GetDotNetVersion() - { - return Environment.Version.ToString(); - } - - /// - /// 获取当前操作系统版本 - /// - /// 操作系统版本 - public static string GetOSVersion() - { - return Environment.OSVersion.ToString(); - } - - /// - /// 获取当前运行环境的处理器架构 - /// - /// 处理器架构 - public static string GetProcessArchitecture() - { - return Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"; - } - - /// - /// 获取当前应用程序内存使用量 - /// - /// 内存使用量(字节) - public static long GetCurrentMemoryUsage() - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - return GC.GetTotalMemory(true); - } - - /// - /// 获取当前运行时间 - /// - /// 运行时间(秒) - public static int GetCurrentRunningTime() - { - return (int)Stopwatch.StartNew().Elapsed.TotalSeconds; - } - - /// - /// 关闭当前应用程序 - /// - public static void ExitApplication() - { - Environment.Exit(0); - } - - /// - /// 获取当前系统的物理内存总量 - /// - /// 物理内存总量(字节) - public static long GetTotalPhysicalMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); - return pc.RawValue; - } - - /// - /// 获取当前系统的可用物理内存量 - /// - /// 可用物理内存量(字节) - public static float GetAvailablePhysicalMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"); - return pc.NextValue(); - } - - /// - /// 获取当前系统的虚拟内存总量 - /// - /// 虚拟内存总量(字节) - public static long GetTotalVirtualMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); - return pc.RawValue; - } - - /// - /// 获取当前系统的可用虚拟内存量 - /// - /// 可用虚拟内存量(字节) - public static float GetAvailableVirtualMemory() - { - PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"); - return pc.NextValue(); - } - - [DllImport("kernel32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetPhysicallyInstalledSystemMemory(out long TotalMemoryInKilobytes); - - /// - /// 获取当前系统的实际物理内存总量 - /// - /// 实际物理内存总量(字节) - public static long GetRealTotalPhysicalMemory() - { - GetPhysicallyInstalledSystemMemory(out long memoryInBytes); - return memoryInBytes * 1024; - } - } -} diff --git a/EasyTool.Core/ToolCategory/SecurityUtil.cs b/EasyTool.Core/ToolCategory/SecurityUtil.cs new file mode 100644 index 0000000..78bc2df --- /dev/null +++ b/EasyTool.Core/ToolCategory/SecurityUtil.cs @@ -0,0 +1,473 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace EasyTool.ToolCategory +{ + /// + /// 安全工具类 + /// 提供XSS防护、SQL注入检测、HTML净化等功能 + /// + public static class SecurityUtil + { + #region HTML编码/解码 + + /// + /// HTML编码 + /// + public static string HtmlEncode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + var sb = new StringBuilder(input.Length); + foreach (var c in input) + { + switch (c) + { + case '<': sb.Append("<"); break; + case '>': sb.Append(">"); break; + case '&': sb.Append("&"); break; + case '"': sb.Append("""); break; + case '\'': sb.Append("'"); break; + default: sb.Append(c); break; + } + } + return sb.ToString(); + } + + /// + /// HTML解码 + /// + public static string HtmlDecode(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + return input + .Replace("<", "<") + .Replace(">", ">") + .Replace("&", "&") + .Replace(""", "\"") + .Replace("'", "'") + .Replace(" ", " ") + .Replace("©", "©") + .Replace("®", "®") + .Replace("™", "™"); + } + + #endregion + + #region XSS防护 + + /// + /// 检测是否包含XSS攻击 + /// + public static bool ContainsXss(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + var patterns = new[] + { + @"]*>.*?", + @"javascript\s*:", + @"on\w+\s*=", + @" Regex.IsMatch(input, p, RegexOptions.IgnoreCase)); + } + + /// + /// 清理XSS攻击代码 + /// + public static string SanitizeXss(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + // 移除危险标签 + var sanitized = Regex.Replace(input, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>.*?", "", RegexOptions.IgnoreCase | RegexOptions.Singleline); + sanitized = Regex.Replace(sanitized, @"]*>", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"]*>", "", RegexOptions.IgnoreCase); + + // 移除危险属性 + sanitized = Regex.Replace(sanitized, @"javascript\s*:", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"vbscript\s*:", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"on\w+\s*=\s*[""'][^""']*[""']", "", RegexOptions.IgnoreCase); + sanitized = Regex.Replace(sanitized, @"expression\s*\([^)]*\)", "", RegexOptions.IgnoreCase); + + return sanitized; + } + + #endregion + + #region SQL注入检测 + + private static readonly string[] SqlKeywords = new[] + { + "select", "insert", "update", "delete", "drop", "create", "alter", "truncate", + "exec", "execute", "xp_", "sp_", "union", "join", "where", "from", "into", + "having", "group by", "order by", "--", "/*", "*/", ";", "declare", "cursor" + }; + + private static readonly string[] SqlFunctions = new[] + { + "char(", "nchar(", "varchar(", "nvarchar(", "cast(", "convert(", + "concat(", "substring(", "len(", "count(", "sum(", "avg(", "max(", "min(" + }; + + /// + /// 检测是否包含SQL注入 + /// + public static bool ContainsSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return false; + + var lowerInput = input.ToLower(); + + // 检查关键字 + foreach (var keyword in SqlKeywords) + { + if (lowerInput.Contains(keyword)) + { + // 检查是否是单词边界 + var pattern = $@"\b{Regex.Escape(keyword)}\b"; + if (Regex.IsMatch(lowerInput, pattern, RegexOptions.IgnoreCase)) + return true; + } + } + + // 检查函数 + foreach (var func in SqlFunctions) + { + if (lowerInput.Contains(func)) + return true; + } + + // 检查单引号 + if (input.Contains("'") && (input.Contains("''") || input.Contains("' or ") || input.Contains("' and "))) + return true; + + // 检查等号注入 + if (Regex.IsMatch(input, @"'?\s*=\s*'?", RegexOptions.IgnoreCase)) + return true; + + return false; + } + + /// + /// 清理SQL注入代码 + /// + public static string SanitizeSqlInjection(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + // 转义单引号 + var sanitized = input.Replace("'", "''"); + + return sanitized; + } + + #endregion + + #region 密码强度检测 + + /// + /// 检测密码强度 + /// + public static PasswordStrength CheckPasswordStrength(string password) + { + if (string.IsNullOrEmpty(password)) + return PasswordStrength.VeryWeak; + + var score = 0; + + // 长度评分 + if (password.Length >= 8) score++; + if (password.Length >= 12) score++; + if (password.Length >= 16) score++; + + // 包含小写字母 + if (Regex.IsMatch(password, @"[a-z]")) score++; + + // 包含大写字母 + if (Regex.IsMatch(password, @"[A-Z]")) score++; + + // 包含数字 + if (Regex.IsMatch(password, @"\d")) score++; + + // 包含特殊字符 + if (Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>/?]")) score++; + + // 不包含连续字符 + if (!HasConsecutiveChars(password)) score++; + + // 不包含常见模式 + if (!HasCommonPatterns(password)) score++; + + return score switch + { + <= 2 => PasswordStrength.VeryWeak, + 3 => PasswordStrength.Weak, + 4 => PasswordStrength.Fair, + 5 => PasswordStrength.Good, + 6 => PasswordStrength.Strong, + _ => PasswordStrength.VeryStrong + }; + } + + private static bool HasConsecutiveChars(string input) + { + for (int i = 0; i < input.Length - 2; i++) + { + if (input[i] + 1 == input[i + 1] && input[i + 1] + 1 == input[i + 2]) + return true; + } + return false; + } + + private static bool HasCommonPatterns(string input) + { + var patterns = new[] { "123", "abc", "qwe", "password", "admin", "111", "000" }; + var lowerInput = input.ToLower(); + return patterns.Any(p => lowerInput.Contains(p)); + } + + #endregion + + #region HTML净化 + + private static readonly HashSet AllowedTags = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "p", "br", "b", "i", "u", "strong", "em", "span", "div", "a", "img", + "ul", "ol", "li", "table", "tr", "td", "th", "thead", "tbody", "h1", "h2", "h3", "h4", "h5", "h6", + "blockquote", "pre", "code", "hr" + }; + + private static readonly HashSet AllowedAttributes = new HashSet(StringComparer.OrdinalIgnoreCase) + { + "href", "src", "alt", "title", "class", "id", "style", "target", "rel" + }; + + /// + /// 净化HTML + /// + public static string SanitizeHtml(string input, IEnumerable? allowedTags = null, IEnumerable? allowedAttributes = null) + { + if (string.IsNullOrEmpty(input)) + return input; + + var tags = allowedTags != null ? new HashSet(allowedTags, StringComparer.OrdinalIgnoreCase) : AllowedTags; + var attrs = allowedAttributes != null ? new HashSet(allowedAttributes, StringComparer.OrdinalIgnoreCase) : AllowedAttributes; + + // 移除注释 + var result = Regex.Replace(input, @"", "", RegexOptions.Singleline); + + // 处理标签 + result = Regex.Replace(result, @"<(/?)(\w+)([^>]*)>", match => + { + var isClosing = match.Groups[1].Value == "/"; + var tagName = match.Groups[2].Value.ToLower(); + var attributes = match.Groups[3].Value; + + if (!tags.Contains(tagName)) + return ""; + + if (isClosing) + return $""; + + // 过滤属性 + var filteredAttrs = FilterAttributes(attributes, attrs); + return $"<{tagName}{filteredAttrs}>"; + }, RegexOptions.Singleline); + + return result; + } + + private static string FilterAttributes(string attributes, HashSet allowedAttrs) + { + var result = new StringBuilder(); + var matches = Regex.Matches(attributes, @"(\w+)\s*=\s*[""']([^""']*)[""']"); + + foreach (Match match in matches) + { + var attrName = match.Groups[1].Value.ToLower(); + var attrValue = match.Groups[2].Value; + + if (allowedAttrs.Contains(attrName)) + { + // 检查危险属性值 + if (attrValue.ToLower().Contains("javascript:") || + attrValue.ToLower().Contains("vbscript:") || + attrValue.ToLower().Contains("expression(")) + continue; + + result.Append($" {attrName}=\"{attrValue}\""); + } + } + + return result.ToString(); + } + + #endregion + + #region 路径安全 + + /// + /// 检测路径是否安全 + /// + public static bool IsPathSafe(string path) + { + if (string.IsNullOrEmpty(path)) + return false; + + // 检查路径遍历攻击 + if (path.Contains("..") || path.Contains("~")) + return false; + + // 检查绝对路径 + if (Path.IsPathRooted(path)) + return false; + + // 检查无效字符 + var invalidChars = Path.GetInvalidFileNameChars(); + var fileName = Path.GetFileName(path); + if (fileName.IndexOfAny(invalidChars) >= 0) + return false; + + return true; + } + + /// + /// 安全路径拼接 + /// + public static string SafeCombine(string basePath, string relativePath) + { + if (string.IsNullOrEmpty(basePath) || string.IsNullOrEmpty(relativePath)) + throw new ArgumentException("路径不能为空"); + + if (!IsPathSafe(relativePath)) + throw new ArgumentException("相对路径不安全"); + + var fullPath = Path.GetFullPath(Path.Combine(basePath, relativePath)); + var normalizedBase = Path.GetFullPath(basePath); + + if (!fullPath.StartsWith(normalizedBase, StringComparison.OrdinalIgnoreCase)) + throw new ArgumentException("路径遍历攻击检测"); + + return fullPath; + } + + #endregion + + #region 敏感信息脱敏 + + /// + /// 手机号脱敏 + /// + public static string MaskPhone(string phone) + { + if (string.IsNullOrEmpty(phone) || phone.Length < 7) + return phone; + + return phone.Substring(0, 3) + "****" + phone.Substring(phone.Length - 4); + } + + /// + /// 身份证号脱敏 + /// + public static string MaskIdCard(string idCard) + { + if (string.IsNullOrEmpty(idCard) || idCard.Length < 8) + return idCard; + + return idCard.Substring(0, 4) + "**********" + idCard.Substring(idCard.Length - 4); + } + + /// + /// 邮箱脱敏 + /// + public static string MaskEmail(string email) + { + if (string.IsNullOrEmpty(email) || !email.Contains("@")) + return email; + + var parts = email.Split('@'); + var name = parts[0]; + var domain = parts[1]; + + if (name.Length <= 2) + return name[0] + "***@" + domain; + + return name.Substring(0, 2) + "***@" + domain; + } + + /// + /// 银行卡号脱敏 + /// + public static string MaskBankCard(string cardNumber) + { + if (string.IsNullOrEmpty(cardNumber) || cardNumber.Length < 8) + return cardNumber; + + return cardNumber.Substring(0, 4) + " **** **** " + cardNumber.Substring(cardNumber.Length - 4); + } + + #endregion + } + + /// + /// 密码强度等级 + /// + public enum PasswordStrength + { + /// + /// 非常弱 + /// + VeryWeak, + + /// + /// 弱 + /// + Weak, + + /// + /// 一般 + /// + Fair, + + /// + /// 好 + /// + Good, + + /// + /// 强 + /// + Strong, + + /// + /// 非常强 + /// + VeryStrong + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs index 2c5572d..1c6796d 100644 --- a/EasyTool.Core/ToolCategory/SimpleMapExtension.cs +++ b/EasyTool.Core/ToolCategory/SimpleMapExtension.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; @@ -7,7 +7,8 @@ using System.Text; using System.Text.Json; -namespace EasyTool; +namespace EasyTool.ToolCategory +{ /// /// 简单实体转化拓展类 /// @@ -92,7 +93,6 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) } } } - if (expr == null) continue; assignments.Add(Expression.Bind(destinationProp, expr)); } @@ -105,6 +105,7 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return Expression.Lambda(bodyExpr, srcExpr).Compile(); } + /// /// 简单实体转化 /// 目标泛型需要默认构造函数 @@ -120,8 +121,9 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return mapper(source); } + /// - /// 集合实体转化,需要目标泛型具有默认构造函数 + /// 融合实体转化,需要目标泛型具有默认构造函数 /// /// 源泛型 /// 目标泛型 @@ -131,6 +133,7 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) { if (!sources.Any()) return Enumerable.Empty(); + var mapper = (Func)mapDelegateCache.GetOrAdd(new(typeof(TSource), typeof(TDestination)), static key => BuildSimpleMapDelegate(key.SrcType, key.DestType)); List result = new(); @@ -141,3 +144,4 @@ private static Delegate BuildSimpleMapDelegate(Type srcType, Type destType) return result; } } +} diff --git a/EasyTool.Core/ToolCategory/Singleton.cs b/EasyTool.Core/ToolCategory/Singleton.cs new file mode 100644 index 0000000..0902c45 --- /dev/null +++ b/EasyTool.Core/ToolCategory/Singleton.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 单例模式工具类 + /// + public static class Singleton + { + /// + /// 获取单例实例 + /// + public static T GetInstance() where T : class, new() + { + return Singleton.Instance; + } + + /// + /// 获取单例实例(带初始化参数) + /// + public static T GetInstance(Func factory) where T : class + { + return Singleton.GetInstance(factory); + } + } + + /// + /// 泛型单例 + /// + public static class Singleton where T : class + { + private static readonly Lazy _instance = new(() => + { + var type = typeof(T); + var constructor = type.GetConstructor(Type.EmptyTypes); + if (constructor == null) + throw new InvalidOperationException($"类型 {type.Name} 必须有公共无参构造函数"); + return (T)constructor.Invoke(null); + }); + + private static T? _customInstance; + private static readonly object _lock = new(); + + /// + /// 单例实例 + /// + public static T Instance => _customInstance ?? _instance.Value; + + /// + /// 获取实例(使用自定义工厂) + /// + public static T GetInstance(Func factory) + { + if (_customInstance != null) + return _customInstance; + + lock (_lock) + { + if (_customInstance != null) + return _customInstance; + + _customInstance = factory(); + return _customInstance; + } + } + + /// + /// 重置实例 + /// + public static void Reset() + { + lock (_lock) + { + _customInstance = null; + } + } + } + + /// + /// 单例基类 + /// + /// 派生类类型 + public abstract class SingletonBase where T : SingletonBase + { + private static readonly Lazy _instance = new(() => + { + var type = typeof(T); + var constructor = type.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public, null, Type.EmptyTypes, null); + if (constructor == null) + throw new InvalidOperationException($"类型 {type.Name} 必须有公共或受保护的无参构造函数"); + return (T)constructor.Invoke(null); + }); + + /// + /// 单例实例 + /// + public static T Instance => _instance.Value; + + /// + /// 受保护的构造函数 + /// + protected SingletonBase() { } + } +} diff --git a/EasyTool.Core/ToolCategory/StateMachine.cs b/EasyTool.Core/ToolCategory/StateMachine.cs new file mode 100644 index 0000000..803d8f1 --- /dev/null +++ b/EasyTool.Core/ToolCategory/StateMachine.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; + +namespace EasyTool.ToolCategory +{ + /// + /// 状态机工具类 + /// + /// 状态类型 + /// 触发器类型 + public class StateMachine where TState : notnull where TTrigger : notnull + { + private readonly Dictionary _configurations = new(); + private readonly object _lock = new(); + + /// + /// 当前状态 + /// + public TState CurrentState { get; private set; } + + /// + /// 状态变更事件 + /// + public event EventHandler? StateChanged; + + /// + /// 状态转换事件 + /// + public event EventHandler? Transitioning; + + /// + /// 创建状态机 + /// + public StateMachine(TState initialState) + { + CurrentState = initialState; + } + + /// + /// 配置状态 + /// + public StateConfiguration Configure(TState state) + { + if (!_configurations.TryGetValue(state, out var config)) + { + config = new StateConfiguration(state); + _configurations[state] = config; + } + return config; + } + + /// + /// 触发转换 + /// + public void Fire(TTrigger trigger) + { + lock (_lock) + { + if (!_configurations.TryGetValue(CurrentState, out var config)) + throw new InvalidOperationException($"状态 {CurrentState} 未配置"); + + if (!config.Transitions.TryGetValue(trigger, out var transition)) + throw new InvalidOperationException($"状态 {CurrentState} 不支持触发器 {trigger}"); + + var args = new StateTransitionEventArgs(CurrentState, transition.Destination, trigger); + + Transitioning?.Invoke(this, args); + + config.ExitAction?.Invoke(); + transition.Action?.Invoke(); + + var previousState = CurrentState; + CurrentState = transition.Destination; + + if (_configurations.TryGetValue(CurrentState, out var newConfig)) + { + newConfig.EntryAction?.Invoke(); + } + + StateChanged?.Invoke(this, args); + } + } + + /// + /// 尝试触发转换 + /// + public bool TryFire(TTrigger trigger) + { + try + { + Fire(trigger); + return true; + } + catch + { + return false; + } + } + + /// + /// 是否可以触发 + /// + public bool CanFire(TTrigger trigger) + { + lock (_lock) + { + if (!_configurations.TryGetValue(CurrentState, out var config)) + return false; + + return config.Transitions.ContainsKey(trigger); + } + } + + /// + /// 获取当前状态可用的触发器 + /// + public IEnumerable GetPermittedTriggers() + { + lock (_lock) + { + if (_configurations.TryGetValue(CurrentState, out var config)) + return config.Transitions.Keys; + return Array.Empty(); + } + } + + /// + /// 状态配置 + /// + public class StateConfiguration + { + private readonly TState _state; + internal readonly Dictionary Transitions = new(); + internal Action? EntryAction; + internal Action? ExitAction; + + internal StateConfiguration(TState state) + { + _state = state; + } + + /// + /// 配置进入动作 + /// + public StateConfiguration OnEntry(Action action) + { + EntryAction = action; + return this; + } + + /// + /// 配置退出动作 + /// + public StateConfiguration OnExit(Action action) + { + ExitAction = action; + return this; + } + + /// + /// 配置转换 + /// + public StateConfiguration Permit(TTrigger trigger, TState destination) + { + Transitions[trigger] = new Transition(destination, null); + return this; + } + + /// + /// 配置转换(带动作) + /// + public StateConfiguration Permit(TTrigger trigger, TState destination, Action action) + { + Transitions[trigger] = new Transition(destination, action); + return this; + } + + /// + /// 忽略触发器 + /// + public StateConfiguration Ignore(TTrigger trigger) + { + Transitions[trigger] = new Transition(_state, null); + return this; + } + } + + internal class Transition + { + public TState Destination { get; } + public Action? Action { get; } + + public Transition(TState destination, Action? action) + { + Destination = destination; + Action = action; + } + } + } + + /// + /// 状态转换事件参数 + /// + public class StateTransitionEventArgs : EventArgs + { + /// + /// 源状态 + /// + public object SourceState { get; } + + /// + /// 目标状态 + /// + public object DestinationState { get; } + + /// + /// 触发器 + /// + public object Trigger { get; } + + internal StateTransitionEventArgs(object source, object destination, object trigger) + { + SourceState = source; + DestinationState = destination; + Trigger = trigger; + } + } +} diff --git a/EasyTool.Core/ToolCategory/StrExtension.cs b/EasyTool.Core/ToolCategory/StrExtension.cs deleted file mode 100644 index 651a010..0000000 --- a/EasyTool.Core/ToolCategory/StrExtension.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Text.RegularExpressions; - -namespace EasyTool.Extension -{ - public static class StrExtension - { - #region 文本可为空判断 - - public static bool IsNullOrEmpty(this string value) - { - return string.IsNullOrEmpty(value); - } - - public static bool IsNullOrWhiteSpace(this string value) - { - return string.IsNullOrWhiteSpace(value); - } - - #endregion - } -} diff --git a/EasyTool.Core/ToolCategory/TaskExtension.cs b/EasyTool.Core/ToolCategory/TaskExtension.cs new file mode 100644 index 0000000..ed5bc19 --- /dev/null +++ b/EasyTool.Core/ToolCategory/TaskExtension.cs @@ -0,0 +1,381 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// Task 异步任务扩展方法 + /// + public static class TaskExtension + { + #region 任务忽略 + + /// + /// 忽略任务(Fire-and-forget),捕获并记录异常但不抛出 + /// + /// 要忽略的任务 + /// 异常回调(可选) + public static void Forget(this Task? task, Action? onException = null) + { + task?.ContinueWith(t => + { + if (t.IsFaulted && t.Exception != null) + { + onException?.Invoke(t.Exception); + } + }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); + } + + /// + /// 忽略任务(Fire-and-forget),捕获并记录异常但不抛出 + /// + /// 任务返回类型 + /// 要忽略的任务 + /// 异常回调(可选) + public static void Forget(this Task? task, Action? onException = null) + { + task?.ContinueWith(t => + { + if (t.IsFaulted && t.Exception != null) + { + onException?.Invoke(t.Exception); + } + }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); + } + + #endregion + + #region 超时处理 + + /// + /// 为任务添加超时处理 + /// + /// 原始任务 + /// 超时时间 + public static async Task OrTimeout(this Task task, TimeSpan timeout) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + + return await task; + } + + /// + /// 为任务添加超时处理 + /// + /// 原始任务 + /// 超时时间 + public static async Task OrTimeout(this Task task, TimeSpan timeout) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + + await task; + } + + /// + /// 为任务添加超时处理,超时返回默认值 + /// + /// 原始任务 + /// 超时时间 + /// 默认值 + public static async Task OrTimeoutOrDefault(this Task task, TimeSpan timeout, T? defaultValue = default) + { + var delayTask = Task.Delay(timeout); + + var completedTask = await Task.WhenAny(task, delayTask); + + if (completedTask == delayTask) + return defaultValue; + + return await task; + } + + #endregion + + #region 重试机制 + + /// + /// 任务失败时自动重试 + /// + /// 任务工厂函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func> taskFactory, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await taskFactory(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delay.HasValue) + await Task.Delay(delay.Value); + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 任务失败时自动重试 + /// + /// 任务工厂函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func taskFactory, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + await taskFactory(); + return; + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && delay.HasValue) + await Task.Delay(delay.Value); + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + /// + /// 任务失败时自动重试(带条件判断) + /// + /// 任务工厂函数 + /// 重试条件函数 + /// 重试次数 + /// 重试延迟时间 + public static async Task Retry(Func> taskFactory, Func shouldRetry, int retryCount = 3, TimeSpan? delay = null) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + if (shouldRetry == null) + throw new ArgumentNullException(nameof(shouldRetry)); + + Exception? lastException = null; + + for (int i = 0; i <= retryCount; i++) + { + try + { + return await taskFactory(); + } + catch (Exception ex) + { + lastException = ex; + + if (i < retryCount && shouldRetry(ex)) + { + if (delay.HasValue) + await Task.Delay(delay.Value); + } + else + { + break; + } + } + } + + throw lastException ?? new Exception("Retry failed"); + } + + #endregion + + #region 任务组合 + + /// + /// 在所有任务完成时返回,无论成功或失败 + /// + public static async Task WhenAllOrAnyFailed(this IEnumerable tasks) + { + if (tasks == null) + throw new ArgumentNullException(nameof(tasks)); + + var taskArray = tasks.ToArray(); + if (taskArray.Length == 0) + return taskArray; + + var tcs = new TaskCompletionSource(); + + int remaining = taskArray.Length; + var results = new Task[taskArray.Length]; + + for (int i = 0; i < taskArray.Length; i++) + { + int index = i; + _ = taskArray[i].ContinueWith(t => + { + results[index] = t; + if (Interlocked.Decrement(ref remaining) == 0) + { + tcs.TrySetResult(results); + } + }, TaskContinuationOptions.ExecuteSynchronously); + } + + return await tcs.Task; + } + + /// + /// 返回第一个完成的任务(无论成功或失败) + /// + public static async Task WhenAnyFirstOrDefault(this IEnumerable tasks) + { + if (tasks == null) + throw new ArgumentNullException(nameof(tasks)); + + var taskArray = tasks.ToArray(); + if (taskArray.Length == 0) + throw new ArgumentException("至少需要一个任务", nameof(tasks)); + + return await Task.WhenAny(taskArray); + } + + #endregion + + #region 任务超时与取消组合 + + /// + /// 创建一个带超时和取消令牌的任务 + /// + public static async Task WithTimeoutAndCancellation(this Task task, TimeSpan timeout, CancellationToken cancellationToken) + { + var timeoutCts = new CancellationTokenSource(timeout); + var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); + + try + { + return await task; + } + catch (OperationCanceledException) when (timeoutCts.Token.IsCancellationRequested) + { + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + } + finally + { + timeoutCts.Dispose(); + combinedCts.Dispose(); + } + } + + /// + /// 创建一个带超时和取消令牌的任务 + /// + public static async Task WithTimeoutAndCancellation(this Task task, TimeSpan timeout, CancellationToken cancellationToken) + { + var timeoutCts = new CancellationTokenSource(timeout); + var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); + + try + { + await task; + } + catch (OperationCanceledException) when (timeoutCts.Token.IsCancellationRequested) + { + throw new TimeoutException($"操作超时({timeout.TotalSeconds}秒)"); + } + finally + { + timeoutCts.Dispose(); + combinedCts.Dispose(); + } + } + + #endregion + + #region 任务结果处理 + + /// + /// 处理任务结果,无论成功或失败 + /// + public static async Task Finally(this Task task, Func onSuccess, Func onFailure) + { + try + { + var result = await task; + return onSuccess(result); + } + catch (Exception ex) + { + return onFailure(ex); + } + } + + /// + /// 处理任务结果,无论成功或失败 + /// + public static async Task Finally(this Task task, Action onSuccess, Action onFailure) + { + try + { + await task; + onSuccess(); + } + catch (Exception ex) + { + onFailure(ex); + } + } + + #endregion + + #region 任务延迟执行 + + /// + /// 延迟执行任务 + /// + public static async Task Delayed(this Func> taskFactory, TimeSpan delay) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + await Task.Delay(delay); + return await taskFactory(); + } + + /// + /// 延迟执行任务 + /// + public static async Task Delayed(this Func taskFactory, TimeSpan delay) + { + if (taskFactory == null) + throw new ArgumentNullException(nameof(taskFactory)); + + await Task.Delay(delay); + await taskFactory(); + } + + #endregion + } +} diff --git a/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs b/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs new file mode 100644 index 0000000..0368def --- /dev/null +++ b/EasyTool.Core/ToolCategory/TaskSchedulerUtil.cs @@ -0,0 +1,396 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 后台任务调度器 + /// + public class BackgroundTaskScheduler : IDisposable + { + private readonly List _tasks = new(); + private readonly object _lock = new(); + private readonly Timer _timer; + private bool _disposed; + + /// + /// 创建后台任务调度器 + /// + /// 检查间隔(毫秒) + public BackgroundTaskScheduler(int checkInterval = 1000) + { + _timer = new Timer(CheckTasks, null, checkInterval, checkInterval); + } + + /// + /// 添加定时任务 + /// + public string Schedule(string name, Action action, DateTime executeAt) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + ExecuteAt = executeAt, + Type = ScheduledTaskType.Once + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 添加延迟任务 + /// + public string Schedule(string name, Action action, TimeSpan delay) + { + return Schedule(name, action, DateTime.UtcNow.Add(delay)); + } + + /// + /// 添加周期性任务 + /// + public string ScheduleRecurring(string name, Action action, TimeSpan interval, DateTime? startAt = null) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + ExecuteAt = startAt ?? DateTime.UtcNow, + Interval = interval, + Type = ScheduledTaskType.Recurring + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 添加Cron任务 + /// + public string ScheduleCron(string name, Action action, string cronExpression) + { + var task = new ScheduledTask + { + Id = Guid.NewGuid().ToString("N"), + Name = name, + Action = action, + CronExpression = cronExpression, + Type = ScheduledTaskType.Cron, + ExecuteAt = GetNextCronTime(cronExpression) + }; + + lock (_lock) + { + _tasks.Add(task); + } + + return task.Id; + } + + /// + /// 取消任务 + /// + public bool Cancel(string taskId) + { + lock (_lock) + { + var task = _tasks.Find(t => t.Id == taskId); + if (task != null) + { + task.IsCancelled = true; + _tasks.Remove(task); + return true; + } + } + return false; + } + + /// + /// 获取所有任务 + /// + public List GetAllTasks() + { + lock (_lock) + { + return _tasks.ConvertAll(t => new ScheduledTaskInfo + { + Id = t.Id, + Name = t.Name, + Type = t.Type, + ExecuteAt = t.ExecuteAt, + Interval = t.Interval, + IsCancelled = t.IsCancelled, + LastExecution = t.LastExecution + }); + } + } + + private void CheckTasks(object? state) + { + List tasksToExecute; + + lock (_lock) + { + var now = DateTime.UtcNow; + tasksToExecute = _tasks.FindAll(t => !t.IsCancelled && t.ExecuteAt <= now); + } + + foreach (var task in tasksToExecute) + { + Task.Run(() => + { + try + { + task.Action(); + task.LastExecution = DateTime.UtcNow; + } + catch + { + // 忽略异常 + } + }); + + // 更新下次执行时间 + lock (_lock) + { + if (task.Type == ScheduledTaskType.Once) + { + _tasks.Remove(task); + } + else if (task.Type == ScheduledTaskType.Recurring) + { + task.ExecuteAt = DateTime.UtcNow.Add(task.Interval); + } + else if (task.Type == ScheduledTaskType.Cron) + { + task.ExecuteAt = GetNextCronTime(task.CronExpression); + } + } + } + } + + private DateTime GetNextCronTime(string cronExpression) + { + // 简化实现,实际应使用CronUtil + var parts = cronExpression.Split(' '); + if (parts.Length >= 1 && int.TryParse(parts[0], out var minute)) + { + var now = DateTime.UtcNow; + var next = new DateTime(now.Year, now.Month, now.Day, now.Hour, minute, 0); + if (next <= now) + next = next.AddHours(1); + return next; + } + return DateTime.UtcNow.AddMinutes(1); + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _timer.Dispose(); + lock (_lock) + { + _tasks.Clear(); + } + } + } + } + + internal class ScheduledTask + { + public string Id { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public Action Action { get; set; } = () => { }; + public DateTime ExecuteAt { get; set; } + public TimeSpan Interval { get; set; } + public string? CronExpression { get; set; } + public ScheduledTaskType Type { get; set; } + public bool IsCancelled { get; set; } + public DateTime? LastExecution { get; set; } + } + + /// + /// 任务类型 + /// + public enum ScheduledTaskType + { + /// + /// 单次执行 + /// + Once, + + /// + /// 周期执行 + /// + Recurring, + + /// + /// Cron表达式 + /// + Cron + } + + /// + /// 计划任务信息 + /// + public class ScheduledTaskInfo + { + /// + /// 任务ID + /// + public string Id { get; set; } = string.Empty; + + /// + /// 任务名称 + /// + public string Name { get; set; } = string.Empty; + + /// + /// 任务类型 + /// + public ScheduledTaskType Type { get; set; } + + /// + /// 执行时间 + /// + public DateTime ExecuteAt { get; set; } + + /// + /// 执行间隔 + /// + public TimeSpan Interval { get; set; } + + /// + /// 是否已取消 + /// + public bool IsCancelled { get; set; } + + /// + /// 最后执行时间 + /// + public DateTime? LastExecution { get; set; } + } + + /// + /// 任务队列 + /// + /// 任务数据类型 + public class TaskQueue : IDisposable + { + private readonly System.Collections.Concurrent.ConcurrentQueue _queue = new(); + private readonly SemaphoreSlim _signal = new(0); + private readonly CancellationTokenSource _cts = new(); + private readonly List _workers = new(); + private readonly Func _processor; + private readonly int _maxDegreeOfParallelism; + private bool _disposed; + + /// + /// 队列数量 + /// + public int Count => _queue.Count; + + /// + /// 创建任务队列 + /// + /// 处理函数 + /// 最大并行度 + public TaskQueue(Func processor, int maxDegreeOfParallelism = 4) + { + _processor = processor ?? throw new ArgumentNullException(nameof(processor)); + _maxDegreeOfParallelism = maxDegreeOfParallelism; + + for (int i = 0; i < maxDegreeOfParallelism; i++) + { + _workers.Add(Task.Run(WorkerAsync)); + } + } + + /// + /// 入队 + /// + public void Enqueue(T item) + { + _queue.Enqueue(item); + _signal.Release(); + } + + /// + /// 批量入队 + /// + public void EnqueueRange(IEnumerable items) + { + foreach (var item in items) + { + Enqueue(item); + } + } + + private async Task WorkerAsync() + { + while (!_cts.Token.IsCancellationRequested) + { + await _signal.WaitAsync(_cts.Token); + + if (_queue.TryDequeue(out var item)) + { + try + { + await _processor(item); + } + catch + { + // 忽略异常 + } + } + } + } + + /// + /// 等待所有任务完成 + /// + public async Task WaitForCompletionAsync() + { + while (_queue.Count > 0 || _signal.CurrentCount > 0) + { + await Task.Delay(100); + } + } + + public void Dispose() + { + if (!_disposed) + { + _disposed = true; + _cts.Cancel(); + + try + { + Task.WaitAll(_workers.ToArray(), TimeSpan.FromSeconds(5)); + } + catch + { + // 忽略 + } + + _cts.Dispose(); + _signal.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs b/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs new file mode 100644 index 0000000..a0f515a --- /dev/null +++ b/EasyTool.Core/ToolCategory/ThreadPoolUtil.cs @@ -0,0 +1,620 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; + +namespace EasyTool.ToolCategory +{ + /// + /// 线程池管理工具类 + /// 提供线程池的创建、管理和监控功能 + /// + public static class ThreadPoolUtil + { + /// + /// 创建自定义线程池 + /// + /// 最小线程数 + /// 最大线程数 + /// 自定义线程池实例 + public static CustomThreadPool Create(int minThreads = 1, int maxThreads = 10) + { + return new CustomThreadPool(minThreads, maxThreads); + } + + /// + /// 创建固定大小的线程池 + /// + /// 线程数量 + /// 固定大小线程池实例 + public static FixedThreadPool CreateFixed(int threadCount) + { + return new FixedThreadPool(threadCount); + } + + /// + /// 获取全局线程池信息 + /// + /// 线程池信息 + public static ThreadPoolInfo GetGlobalPoolInfo() + { + ThreadPool.GetMinThreads(out int minWorkerThreads, out int minCompletionPortThreads); + ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads); + ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableCompletionPortThreads); + + return new ThreadPoolInfo + { + MinWorkerThreads = minWorkerThreads, + MinCompletionPortThreads = minCompletionPortThreads, + MaxWorkerThreads = maxWorkerThreads, + MaxCompletionPortThreads = maxCompletionPortThreads, + AvailableWorkerThreads = availableWorkerThreads, + AvailableCompletionPortThreads = availableCompletionPortThreads, + ActiveWorkerThreads = maxWorkerThreads - availableWorkerThreads, + ActiveCompletionPortThreads = maxCompletionPortThreads - availableCompletionPortThreads + }; + } + + /// + /// 设置全局线程池大小 + /// + /// 最小线程数 + /// 最大线程数 + /// 是否设置成功 + public static bool SetGlobalPoolSize(int minThreads, int maxThreads) + { + return ThreadPool.SetMinThreads(minThreads, minThreads) && + ThreadPool.SetMaxThreads(maxThreads, maxThreads); + } + + /// + /// 设置全局线程池最小线程数 + /// + /// 最小线程数 + /// 是否设置成功 + public static bool SetGlobalMinThreads(int minThreads) + { + return ThreadPool.SetMinThreads(minThreads, minThreads); + } + + /// + /// 设置全局线程池最大线程数 + /// + /// 最大线程数 + /// 是否设置成功 + public static bool SetGlobalMaxThreads(int maxThreads) + { + return ThreadPool.SetMaxThreads(maxThreads, maxThreads); + } + + /// + /// 等待所有任务完成 + /// + /// 要等待的任务数组 + /// 超时时间(可选) + /// 是否在超时前完成 + public static bool WaitAll(Task[] tasks, TimeSpan? timeout = null) + { + if (tasks == null || tasks.Length == 0) + return true; + + if (timeout.HasValue) + { + return Task.WaitAll(tasks, timeout.Value); + } + Task.WaitAll(tasks); + return true; + } + + /// + /// 异步等待所有任务完成 + /// + /// 要等待的任务数组 + /// Task + public static async Task WaitAllAsync(Task[] tasks) + { + if (tasks == null || tasks.Length == 0) + return; + + await Task.WhenAll(tasks); + } + } + + /// + /// 线程池信息 + /// + public class ThreadPoolInfo + { + /// + /// 最小工作线程数 + /// + public int MinWorkerThreads { get; set; } + + /// + /// 最小完成端口线程数 + /// + public int MinCompletionPortThreads { get; set; } + + /// + /// 最大工作线程数 + /// + public int MaxWorkerThreads { get; set; } + + /// + /// 最大完成端口线程数 + /// + public int MaxCompletionPortThreads { get; set; } + + /// + /// 可用工作线程数 + /// + public int AvailableWorkerThreads { get; set; } + + /// + /// 可用完成端口线程数 + /// + public int AvailableCompletionPortThreads { get; set; } + + /// + /// 活跃工作线程数 + /// + public int ActiveWorkerThreads { get; set; } + + /// + /// 活跃完成端口线程数 + /// + public int ActiveCompletionPortThreads { get; set; } + + /// + /// 总活跃线程数 + /// + public int TotalActiveThreads => ActiveWorkerThreads + ActiveCompletionPortThreads; + + /// + /// 线程池使用率(0-1) + /// + public double UsageRate => MaxWorkerThreads > 0 ? (double)ActiveWorkerThreads / MaxWorkerThreads : 0; + + public override string ToString() + { + return $"Worker: {ActiveWorkerThreads}/{MaxWorkerThreads} (Min: {MinWorkerThreads}), " + + $"IOCP: {ActiveCompletionPortThreads}/{MaxCompletionPortThreads} (Min: {MinCompletionPortThreads}), " + + $"Usage: {UsageRate:P1}"; + } + } + + /// + /// 自定义线程池 + /// + public class CustomThreadPool : IDisposable + { + private readonly BlockingCollection _taskQueue; + private readonly Thread[] _threads; + private readonly CancellationTokenSource _cts; + private readonly SemaphoreSlim _semaphore; + private int _activeTasks; + private bool _disposed; + + /// + /// 最小线程数 + /// + public int MinThreads { get; } + + /// + /// 最大线程数 + /// + public int MaxThreads { get; } + + /// + /// 当前活跃线程数 + /// + public int ActiveThreads => _activeTasks; + + /// + /// 队列中等待的任务数 + /// + public int QueuedTasks => _taskQueue.Count; + + /// + /// 是否已关闭 + /// + public bool IsShutdown => _cts.IsCancellationRequested; + + /// + /// 创建自定义线程池 + /// + /// 最小线程数 + /// 最大线程数 + public CustomThreadPool(int minThreads = 1, int maxThreads = 10) + { + if (minThreads < 1) + throw new ArgumentOutOfRangeException(nameof(minThreads), "最小线程数必须大于0"); + if (maxThreads < minThreads) + throw new ArgumentOutOfRangeException(nameof(maxThreads), "最大线程数不能小于最小线程数"); + + MinThreads = minThreads; + MaxThreads = maxThreads; + + _taskQueue = new BlockingCollection(maxThreads * 10); + _cts = new CancellationTokenSource(); + _semaphore = new SemaphoreSlim(maxThreads, maxThreads); + _threads = new Thread[maxThreads]; + _activeTasks = 0; + + // 启动最小数量的线程 + for (int i = 0; i < minThreads; i++) + { + StartThread(i); + } + } + + /// + /// 提交任务 + /// + /// 要执行的操作 + public void Submit(Action action) + { + ThrowIfDisposed(); + _taskQueue.Add(action ?? throw new ArgumentNullException(nameof(action))); + } + + /// + /// 提交任务并返回 Task + /// + /// 要执行的操作 + /// Task 对象 + public Task SubmitAsync(Action action) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + action(); + tcs.SetResult(true); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 提交带返回值的任务 + /// + /// 返回值类型 + /// 要执行的函数 + /// 返回值的 Task + public Task SubmitAsync(Func func) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + tcs.SetResult(func()); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 关闭线程池 + /// + /// 是否等待任务完成 + /// 超时时间 + public void Shutdown(bool waitForCompletion = true, TimeSpan? timeout = null) + { + ThrowIfDisposed(); + _taskQueue.CompleteAdding(); + _cts.Cancel(); + + if (waitForCompletion) + { + if (timeout.HasValue) + { + foreach (var thread in _threads) + { + thread?.Join(timeout.Value); + } + } + else + { + foreach (var thread in _threads) + { + thread?.Join(); + } + } + } + } + + /// + /// 等待所有任务完成 + /// + /// 超时时间 + /// 是否在超时前完成 + public bool WaitAll(TimeSpan? timeout = null) + { + if (timeout.HasValue) + { + return _semaphore.Wait(timeout.Value); + } + _semaphore.Wait(); + _semaphore.Release(); + return true; + } + + private void StartThread(int index) + { + var thread = new Thread(Worker) + { + IsBackground = true, + Name = $"CustomThreadPool-{index}" + }; + _threads[index] = thread; + thread.Start(); + } + + private void Worker() + { + foreach (var action in _taskQueue.GetConsumingEnumerable(_cts.Token)) + { + Interlocked.Increment(ref _activeTasks); + try + { + _semaphore.Wait(_cts.Token); + try + { + action(); + } + finally + { + _semaphore.Release(); + } + } + catch (OperationCanceledException) + { + break; + } + finally + { + Interlocked.Decrement(ref _activeTasks); + } + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + throw new ObjectDisposedException(nameof(CustomThreadPool)); + } + + public void Dispose() + { + if (_disposed) + return; + + Shutdown(false); + _taskQueue.Dispose(); + _cts.Dispose(); + _semaphore.Dispose(); + _disposed = true; + } + } + + /// + /// 固定大小线程池 + /// + public class FixedThreadPool : IDisposable + { + private readonly BlockingCollection _taskQueue; + private readonly Thread[] _threads; + private readonly CancellationTokenSource _cts; + private int _activeTasks; + private bool _disposed; + + /// + /// 线程数量 + /// + public int ThreadCount { get; } + + /// + /// 当前活跃线程数 + /// + public int ActiveThreads => _activeTasks; + + /// + /// 队列中等待的任务数 + /// + public int QueuedTasks => _taskQueue.Count; + + /// + /// 是否已关闭 + /// + public bool IsShutdown => _cts.IsCancellationRequested; + + /// + /// 创建固定大小线程池 + /// + /// 线程数量 + public FixedThreadPool(int threadCount) + { + if (threadCount < 1) + throw new ArgumentOutOfRangeException(nameof(threadCount), "线程数量必须大于0"); + + ThreadCount = threadCount; + _taskQueue = new BlockingCollection(); + _cts = new CancellationTokenSource(); + _threads = new Thread[threadCount]; + + for (int i = 0; i < threadCount; i++) + { + var thread = new Thread(Worker) + { + IsBackground = true, + Name = $"FixedThreadPool-{i}" + }; + _threads[i] = thread; + thread.Start(); + } + } + + /// + /// 提交任务 + /// + /// 要执行的操作 + public void Submit(Action action) + { + ThrowIfDisposed(); + _taskQueue.Add(action ?? throw new ArgumentNullException(nameof(action))); + } + + /// + /// 提交任务并返回 Task + /// + /// 要执行的操作 + /// Task 对象 + public Task SubmitAsync(Action action) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + action(); + tcs.SetResult(true); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 提交带返回值的任务 + /// + /// 返回值类型 + /// 要执行的函数 + /// 返回值的 Task + public Task SubmitAsync(Func func) + { + ThrowIfDisposed(); + var tcs = new TaskCompletionSource(); + + Submit(() => + { + try + { + tcs.SetResult(func()); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + + /// + /// 关闭线程池 + /// + /// 是否等待任务完成 + /// 超时时间 + public void Shutdown(bool waitForCompletion = true, TimeSpan? timeout = null) + { + ThrowIfDisposed(); + _taskQueue.CompleteAdding(); + _cts.Cancel(); + + if (waitForCompletion) + { + if (timeout.HasValue) + { + foreach (var thread in _threads) + { + thread?.Join(timeout.Value); + } + } + else + { + foreach (var thread in _threads) + { + thread?.Join(); + } + } + } + } + + /// + /// 等待队列清空 + /// + /// 超时时间 + /// 是否在超时前完成 + public bool WaitUntilEmpty(TimeSpan? timeout = null) + { + var startTime = DateTime.UtcNow; + while (_taskQueue.Count > 0 || _activeTasks > 0) + { + if (timeout.HasValue && (DateTime.UtcNow - startTime) >= timeout.Value) + return false; + Thread.Sleep(10); + } + return true; + } + + private void Worker() + { + foreach (var action in _taskQueue.GetConsumingEnumerable(_cts.Token)) + { + Interlocked.Increment(ref _activeTasks); + try + { + action(); + } + catch + { + // 忽略任务执行异常 + } + finally + { + Interlocked.Decrement(ref _activeTasks); + } + } + } + + private void ThrowIfDisposed() + { + if (_disposed) + throw new ObjectDisposedException(nameof(FixedThreadPool)); + } + + public void Dispose() + { + if (_disposed) + return; + + Shutdown(false); + _taskQueue.Dispose(); + _cts.Dispose(); + _disposed = true; + } + } +} diff --git a/EasyTool.Core/ToolCategory/TypeUtil.cs b/EasyTool.Core/ToolCategory/TypeUtil.cs deleted file mode 100644 index a74ff6a..0000000 --- a/EasyTool.Core/ToolCategory/TypeUtil.cs +++ /dev/null @@ -1,184 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; - -namespace EasyTool -{ - /// - /// 泛型类型工具 - /// - public class TypeUtil - { - /// - /// 判断类型是否是可空类型 - /// - /// 要判断的类型 - /// 是否是可空类型 - public static bool IsNullable() where T : struct - { - return Nullable.GetUnderlyingType(typeof(T)) != null; - } - - /// - /// 判断类型是否是枚举类型 - /// - /// 要判断的类型 - /// 是否是枚举类型 - public static bool IsEnum() - { - return typeof(T).IsEnum; - } - - /// - /// 获取泛型类型的参数类型 - /// - /// 要获取参数类型的泛型类型 - /// 泛型类型的参数类型数组 - public static Type[] GetGenericArguments() - { - return typeof(T).GetGenericArguments(); - } - - /// - /// 获取类型的所有属性 - /// - /// 要获取属性的类型 - /// 属性数组 - public static PropertyInfo[] GetProperties() - { - return typeof(T).GetProperties(); - } - - /// - /// 获取类型的所有字段 - /// - /// 要获取字段的类型 - /// 字段数组 - public static FieldInfo[] GetFields() - { - return typeof(T).GetFields(); - } - - /// - /// 获取类型的所有方法 - /// - /// 要获取方法的类型 - /// 方法数组 - public static MethodInfo[] GetMethods() - { - return typeof(T).GetMethods(); - } - - /// - /// 获取类型的所有事件 - /// - /// 要获取事件的类型 - /// 事件数组 - public static EventInfo[] GetEvents() - { - return typeof(T).GetEvents(); - } - - /// - /// 获取类型的所有属性、字段、方法和事件 - /// - /// 要获取成员的类型 - /// 成员数组 - public static MemberInfo[] GetMembers() - { - return typeof(T).GetMembers(); - } - - /// - /// 获取类型的所有构造函数 - /// - /// 要获取构造函数的类型 - /// 构造函数数组 - public static ConstructorInfo[] GetConstructors() - { - return typeof(T).GetConstructors(); - } - - /// - /// 判断类型是否实现了指定的接口 - /// - /// 要判断的类型 - /// 要判断的接口类型 - /// 是否实现了指定的接口 - public static bool ImplementsInterface() - { - return typeof(T).GetInterfaces().Any(i => i == typeof(TInterface)); - } - - /// - /// 判断类型是否继承了指定的基类 - /// - /// 要判断的类型 - /// 要判断的基类类型 - /// 是否继承了指定的基类 - public static bool InheritsFrom() - { - return typeof(T).IsSubclassOf(typeof(TBase)); - } - - /// - /// 创建指定类型的实例 - /// - /// 要创建实例的类型 - /// 类型的实例 - public static T CreateInstance() - { - return (T)Activator.CreateInstance(typeof(T)); - } - - /// - /// 创建指定类型的实例 - /// - /// 要创建实例的类型 - /// 构造函数的参数 - /// 类型的实例 - public static T CreateInstance(params object[] args) - { - return (T)Activator.CreateInstance(typeof(T), args); - } - - /// - /// 获取枚举类型的所有值 - /// - /// 枚举类型 - /// 枚举类型的所有值 - public static IEnumerable GetEnumValues() - { - if (!IsEnum()) - { - throw new ArgumentException("Type is not an enum type"); - } - - return Enum.GetValues(typeof(T)).Cast(); - } - - /// - /// 将字符串转换为指定类型的值 - /// - /// 要转换的类型 - /// 要转换的字符串 - /// 转换后的值 - public static T ConvertFromString(string value) - { - return (T)Convert.ChangeType(value, typeof(T)); - } - - /// - /// 将值转换为指定类型的字符串 - /// - /// 要转换的类型 - /// 要转换的值 - /// 转换后的字符串 - public static string ConvertToString(T value) - { - return Convert.ToString(value); - } - } -} diff --git a/EasyTool.Core/ToolCategory/ValidatorUtil.cs b/EasyTool.Core/ToolCategory/ValidatorUtil.cs new file mode 100644 index 0000000..c3f7e43 --- /dev/null +++ b/EasyTool.Core/ToolCategory/ValidatorUtil.cs @@ -0,0 +1,463 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.ToolCategory +{ + /// + /// 验证结果 + /// + public class ValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; set; } + + /// + /// 错误消息列表 + /// + public List Errors { get; set; } = new(); + + /// + /// 错误字段列表 + /// + public List ErrorFields { get; set; } = new(); + + public static ValidationResult Success() => new() { IsValid = true }; + public static ValidationResult Failure(params string[] errors) => new() { IsValid = false, Errors = errors.ToList() }; + } + + /// + /// 验证工具类 + /// 提供常用的数据验证功能 + /// + public static class ValidatorUtil + { + #region 字符串验证 + + /// + /// 检查字符串是否为空或空白 + /// + public static bool IsNullOrWhiteSpace(string? value) + { + return string.IsNullOrWhiteSpace(value); + } + + /// + /// 检查字符串是否为空 + /// + public static bool IsNullOrEmpty(string? value) + { + return string.IsNullOrEmpty(value); + } + + /// + /// 检查字符串是否不为空 + /// + public static bool IsNotNullOrEmpty(string? value) + { + return !string.IsNullOrEmpty(value); + } + + /// + /// 检查字符串长度是否在指定范围内 + /// + public static bool IsLengthBetween(string? value, int minLength, int maxLength) + { + if (value == null) + return minLength <= 0; + + return value.Length >= minLength && value.Length <= maxLength; + } + + /// + /// 检查字符串长度是否等于指定值 + /// + public static bool IsLength(string? value, int length) + { + return value?.Length == length; + } + + /// + /// 检查字符串是否只包含数字 + /// + public static bool IsNumeric(string? value) + { + if (string.IsNullOrEmpty(value)) + return false; + + return value.All(char.IsDigit); + } + + /// + /// 检查字符串是否只包含字母 + /// + public static bool IsAlpha(string? value) + { + if (string.IsNullOrEmpty(value)) + return false; + + return value.All(char.IsLetter); + } + + /// + /// 检查字符串是否只包含字母和数字 + /// + public static bool IsAlphanumeric(string? value) + { + if (string.IsNullOrEmpty(value)) + return false; + + return value.All(c => char.IsLetterOrDigit(c)); + } + + /// + /// 检查字符串是否匹配正则表达式 + /// + public static bool IsMatch(string? value, string pattern) + { + if (string.IsNullOrEmpty(value)) + return false; + + return Regex.IsMatch(value, pattern); + } + + /// + /// 检查字符串是否在指定值列表中 + /// + public static bool IsIn(string? value, params string[] allowedValues) + { + return allowedValues.Contains(value); + } + + /// + /// 检查字符串是否以指定前缀开头 + /// + public static bool StartsWith(string? value, string prefix, StringComparison comparison = StringComparison.Ordinal) + { + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(prefix)) + return false; + + return value.StartsWith(prefix, comparison); + } + + /// + /// 检查字符串是否以指定后缀结尾 + /// + public static bool EndsWith(string? value, string suffix, StringComparison comparison = StringComparison.Ordinal) + { + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(suffix)) + return false; + + return value.EndsWith(suffix, comparison); + } + + /// + /// 检查字符串是否包含指定子串 + /// + public static bool Contains(string? value, string substring, StringComparison comparison = StringComparison.Ordinal) + { + if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(substring)) + return false; + + return value.Contains(substring, comparison); + } + + #endregion + + #region 数字验证 + + /// + /// 检查值是否在指定范围内 + /// + public static bool IsBetween(T value, T min, T max) where T : IComparable + { + return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0; + } + + /// + /// 检查值是否大于指定值 + /// + public static bool IsGreaterThan(T value, T compare) where T : IComparable + { + return value.CompareTo(compare) > 0; + } + + /// + /// 检查值是否大于等于指定值 + /// + public static bool IsGreaterThanOrEqual(T value, T compare) where T : IComparable + { + return value.CompareTo(compare) >= 0; + } + + /// + /// 检查值是否小于指定值 + /// + public static bool IsLessThan(T value, T compare) where T : IComparable + { + return value.CompareTo(compare) < 0; + } + + /// + /// 检查值是否小于等于指定值 + /// + public static bool IsLessThanOrEqual(T value, T compare) where T : IComparable + { + return value.CompareTo(compare) <= 0; + } + + /// + /// 检查是否为正数 + /// + public static bool IsPositive(T value) where T : IComparable + { + return value.CompareTo(default!) > 0; + } + + /// + /// 检查是否为负数 + /// + public static bool IsNegative(T value) where T : IComparable + { + return value.CompareTo(default!) < 0; + } + + /// + /// 检查是否为零 + /// + public static bool IsZero(T value) where T : IComparable + { + return value.CompareTo(default!) == 0; + } + + /// + /// 检查是否为偶数 + /// + public static bool IsEven(int value) + { + return value % 2 == 0; + } + + /// + /// 检查是否为奇数 + /// + public static bool IsOdd(int value) + { + return value % 2 != 0; + } + + #endregion + + #region 集合验证 + + /// + /// 检查集合是否为空 + /// + public static bool IsEmpty(IEnumerable? collection) + { + if (collection == null) + return true; + + if (collection is ICollection col) + return col.Count == 0; + + return !collection.Cast().Any(); + } + + /// + /// 检查集合是否不为空 + /// + public static bool IsNotEmpty(IEnumerable? collection) + { + return !IsEmpty(collection); + } + + /// + /// 检查集合元素数量是否在指定范围内 + /// + public static bool IsCountBetween(IEnumerable? collection, int minCount, int maxCount) + { + if (collection == null) + return minCount <= 0; + + int count; + if (collection is ICollection col) + { + count = col.Count; + } + else + { + count = collection.Cast().Count(); + } + + return count >= minCount && count <= maxCount; + } + + /// + /// 检查集合是否包含指定元素 + /// + public static bool Contains(IEnumerable? collection, T item) + { + if (collection == null) + return false; + + return collection.Contains(item); + } + + /// + /// 检查集合是否包含所有指定元素 + /// + public static bool ContainsAll(IEnumerable? collection, params T[] items) + { + if (collection == null || items == null) + return false; + + return items.All(item => collection.Contains(item)); + } + + /// + /// 检查集合是否包含任一指定元素 + /// + public static bool ContainsAny(IEnumerable? collection, params T[] items) + { + if (collection == null || items == null) + return false; + + return items.Any(item => collection.Contains(item)); + } + + #endregion + + #region 日期验证 + + /// + /// 检查日期是否在指定范围内 + /// + public static bool IsBetween(DateTime value, DateTime min, DateTime max) + { + return value >= min && value <= max; + } + + /// + /// 检查是否为工作日(周一至周五) + /// + public static bool IsWeekday(DateTime value) + { + return value.DayOfWeek != DayOfWeek.Saturday && value.DayOfWeek != DayOfWeek.Sunday; + } + + /// + /// 检查是否为周末 + /// + public static bool IsWeekend(DateTime value) + { + return value.DayOfWeek == DayOfWeek.Saturday || value.DayOfWeek == DayOfWeek.Sunday; + } + + /// + /// 检查是否为今天 + /// + public static bool IsToday(DateTime value) + { + return value.Date == DateTime.Today; + } + + /// + /// 检查是否为过去的时间 + /// + public static bool IsPast(DateTime value) + { + return value < DateTime.Now; + } + + /// + /// 检查是否为未来的时间 + /// + public static bool IsFuture(DateTime value) + { + return value > DateTime.Now; + } + + #endregion + + #region 类型验证 + + /// + /// 检查值是否为指定类型 + /// + public static bool IsType(object? value) + { + return value is T; + } + + /// + /// 检查值是否为 null + /// + public static bool IsNull(object? value) + { + return value == null; + } + + /// + /// 检查值是否不为 null + /// + public static bool IsNotNull(object? value) + { + return value != null; + } + + /// + /// 检查是否为默认值 + /// + public static bool IsDefault(T value) + { + return EqualityComparer.Default.Equals(value, default); + } + + #endregion + + #region 组合验证 + + /// + /// 组合多个验证条件(全部满足) + /// + public static bool All(params Func[] validators) + { + return validators.All(v => v()); + } + + /// + /// 组合多个验证条件(任一满足) + /// + public static bool Any(params Func[] validators) + { + return validators.Any(v => v()); + } + + /// + /// 验证并返回结果 + /// + public static ValidationResult Validate(params (string Field, Func Validator, string ErrorMessage)[] rules) + { + var result = new ValidationResult { IsValid = true }; + + foreach (var (field, validator, errorMessage) in rules) + { + if (!validator()) + { + result.IsValid = false; + result.Errors.Add(errorMessage); + result.ErrorFields.Add(field); + } + } + + return result; + } + + #endregion + } +} \ No newline at end of file diff --git a/EasyTool.Core/ToolCategory/VersionUtil.cs b/EasyTool.Core/ToolCategory/VersionUtil.cs new file mode 100644 index 0000000..bfa3eb6 --- /dev/null +++ b/EasyTool.Core/ToolCategory/VersionUtil.cs @@ -0,0 +1,475 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EasyTool.ToolCategory +{ + /// + /// 版本号工具类 + /// 提供版本号解析、比较和验证功能 + /// + public static class VersionUtil + { + /// + /// 解析版本号字符串 + /// + /// 版本号字符串(如 "1.2.3" 或 "v1.2.3-beta") + /// 版本信息对象 + public static VersionInfo Parse(string? version) + { + if (string.IsNullOrWhiteSpace(version)) + throw new ArgumentException("版本号不能为空"); + + // 移除 v 或 V 前缀 + var normalized = version.TrimStart('v', 'V'); + + // 分离预发布标签 + string? preRelease = null; + var preReleaseIndex = normalized.IndexOf('-'); + if (preReleaseIndex >= 0) + { + preRelease = normalized.Substring(preReleaseIndex + 1); + normalized = normalized.Substring(0, preReleaseIndex); + } + + // 分离构建元数据 + string? buildMetadata = null; + var buildIndex = normalized.IndexOf('+'); + if (buildIndex >= 0) + { + buildMetadata = normalized.Substring(buildIndex + 1); + normalized = normalized.Substring(0, buildIndex); + } + + // 解析版本号部分 + var parts = normalized.Split('.'); + if (parts.Length == 0 || parts.Length > 4) + throw new FormatException($"无效的版本号格式: {version}"); + + var info = new VersionInfo + { + Original = version, + PreRelease = preRelease, + BuildMetadata = buildMetadata + }; + + if (int.TryParse(parts[0], out var major)) + info.Major = major; + else + throw new FormatException($"无效的主版本号: {parts[0]}"); + + if (parts.Length > 1) + { + if (int.TryParse(parts[1], out var minor)) + info.Minor = minor; + else + throw new FormatException($"无效的次版本号: {parts[1]}"); + } + + if (parts.Length > 2) + { + if (int.TryParse(parts[2], out var patch)) + info.Patch = patch; + else + throw new FormatException($"无效的补丁版本号: {parts[2]}"); + } + + if (parts.Length > 3) + { + if (int.TryParse(parts[3], out var revision)) + info.Revision = revision; + else + throw new FormatException($"无效的修订版本号: {parts[3]}"); + } + + return info; + } + + /// + /// 尝试解析版本号 + /// + /// 版本号字符串 + /// 解析结果 + /// 是否解析成功 + public static bool TryParse(string? version, out VersionInfo? info) + { + info = null; + if (string.IsNullOrWhiteSpace(version)) + return false; + + try + { + info = Parse(version); + return true; + } + catch + { + return false; + } + } + + /// + /// 比较两个版本号 + /// + /// 版本号1 + /// 版本号2 + /// 比较结果:-1表示小于,0表示等于,1表示大于 + public static int Compare(string? version1, string? version2) + { + if (!TryParse(version1, out var info1)) + info1 = new VersionInfo(); + if (!TryParse(version2, out var info2)) + info2 = new VersionInfo(); + + return Compare(info1!, info2!); + } + + /// + /// 比较两个版本信息 + /// + /// 版本1 + /// 版本2 + /// 比较结果 + public static int Compare(VersionInfo v1, VersionInfo v2) + { + if (v1.Major != v2.Major) + return v1.Major.CompareTo(v2.Major); + if (v1.Minor != v2.Minor) + return v1.Minor.CompareTo(v2.Minor); + if (v1.Patch != v2.Patch) + return v1.Patch.CompareTo(v2.Patch); + if (v1.Revision != v2.Revision) + return v1.Revision.CompareTo(v2.Revision); + + // 主版本号相同时,比较预发布标签 + // 没有预发布标签的版本高于有预发布标签的版本 + if (string.IsNullOrEmpty(v1.PreRelease) && !string.IsNullOrEmpty(v2.PreRelease)) + return 1; + if (!string.IsNullOrEmpty(v1.PreRelease) && string.IsNullOrEmpty(v2.PreRelease)) + return -1; + + if (!string.IsNullOrEmpty(v1.PreRelease) && !string.IsNullOrEmpty(v2.PreRelease)) + return string.Compare(v1.PreRelease, v2.PreRelease, StringComparison.Ordinal); + + return 0; + } + + /// + /// 判断版本号是否在指定范围内 + /// + /// 要检查的版本号 + /// 最小版本号(包含) + /// 最大版本号(包含) + /// 是否在范围内 + public static bool IsInRange(string? version, string? min, string? max) + { + var info = Parse(version); + var minInfo = string.IsNullOrEmpty(min) ? null : Parse(min); + var maxInfo = string.IsNullOrEmpty(max) ? null : Parse(max); + + if (minInfo != null && Compare(info, minInfo) < 0) + return false; + if (maxInfo != null && Compare(info, maxInfo) > 0) + return false; + + return true; + } + + /// + /// 获取下一个版本号 + /// + /// 当前版本号 + /// 递增级别:Major, Minor, Patch + /// 下一个版本号 + public static string Next(string? version, VersionLevel level = VersionLevel.Patch) + { + var info = TryParse(version, out var v) ? v! : new VersionInfo(); + + return level switch + { + VersionLevel.Major => $"{info!.Major + 1}.0.0", + VersionLevel.Minor => $"{info!.Major}.{info.Minor + 1}.0", + VersionLevel.Patch => $"{info!.Major}.{info.Minor}.{info.Patch + 1}", + VersionLevel.Revision => $"{info!.Major}.{info.Minor}.{info.Patch}.{info.Revision + 1}", + _ => info!.ToString() + }; + } + + /// + /// 获取版本号之间的差异描述 + /// + /// 旧版本 + /// 新版本 + /// 差异描述 + public static VersionDiff GetDiff(string? oldVersion, string? newVersion) + { + var oldInfo = TryParse(oldVersion, out var old) ? old! : new VersionInfo(); + var newInfo = TryParse(newVersion, out var newV) ? newV! : new VersionInfo(); + + var diff = new VersionDiff + { + OldVersion = oldInfo, + NewVersion = newInfo, + MajorDiff = newInfo.Major - oldInfo.Major, + MinorDiff = newInfo.Minor - oldInfo.Minor, + PatchDiff = newInfo.Patch - oldInfo.Patch, + RevisionDiff = newInfo.Revision - oldInfo.Revision + }; + + if (diff.MajorDiff != 0) + diff.ChangeLevel = VersionLevel.Major; + else if (diff.MinorDiff != 0) + diff.ChangeLevel = VersionLevel.Minor; + else if (diff.PatchDiff != 0) + diff.ChangeLevel = VersionLevel.Patch; + else if (diff.RevisionDiff != 0) + diff.ChangeLevel = VersionLevel.Revision; + + return diff; + } + + /// + /// 从列表中找到最接近目标版本号的版本 + /// + /// 版本号列表 + /// 目标版本号 + /// 最接近的版本号 + public static string? FindClosest(IEnumerable versions, string target) + { + if (versions == null || string.IsNullOrEmpty(target)) + return null; + + var targetInfo = Parse(target); + string? closest = null; + var minDiff = int.MaxValue; + + foreach (var version in versions) + { + if (!TryParse(version, out var info)) + continue; + + var diff = Math.Abs(Compare(info!, targetInfo)); + if (diff < minDiff) + { + minDiff = diff; + closest = version; + } + } + + return closest; + } + + /// + /// 验证版本号是否符合语义化版本规范(SemVer) + /// + /// 版本号字符串 + /// 是否有效 + public static bool IsValidSemVer(string? version) + { + if (string.IsNullOrWhiteSpace(version)) + return false; + + // SemVer 正则:主版本.次版本.补丁版本[-预发布标识][+构建元数据] + var pattern = @"^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"; + return System.Text.RegularExpressions.Regex.IsMatch(version, pattern); + } + + /// + /// 将版本号转换为 System.Version + /// + /// 版本号字符串 + /// System.Version 对象 + public static Version ToVersion(string? version) + { + var info = Parse(version); + return new Version(info.Major, info.Minor, info.Patch, info.Revision); + } + + /// + /// 从 System.Version 转换为 VersionInfo + /// + /// System.Version 对象 + /// VersionInfo 对象 + public static VersionInfo FromVersion(Version version) + { + return new VersionInfo + { + Major = version.Major, + Minor = version.Minor, + Patch = version.Build >= 0 ? version.Build : 0, + Revision = version.Revision >= 0 ? version.Revision : 0 + }; + } + } + + /// + /// 版本信息 + /// + public class VersionInfo + { + /// + /// 原始版本号字符串 + /// + public string? Original { get; set; } + + /// + /// 主版本号 + /// + public int Major { get; set; } + + /// + /// 次版本号 + /// + public int Minor { get; set; } + + /// + /// 补丁版本号 + /// + public int Patch { get; set; } + + /// + /// 修订版本号 + /// + public int Revision { get; set; } + + /// + /// 预发布标识(如 alpha, beta, rc.1) + /// + public string? PreRelease { get; set; } + + /// + /// 构建元数据 + /// + public string? BuildMetadata { get; set; } + + /// + /// 是否为预发布版本 + /// + public bool IsPreRelease => !string.IsNullOrEmpty(PreRelease); + + /// + /// 是否为稳定版本 + /// + public bool IsStable => string.IsNullOrEmpty(PreRelease); + + public override string ToString() + { + var result = $"{Major}.{Minor}.{Patch}"; + if (Revision > 0) + result += $".{Revision}"; + if (!string.IsNullOrEmpty(PreRelease)) + result += $"-{PreRelease}"; + if (!string.IsNullOrEmpty(BuildMetadata)) + result += $"+{BuildMetadata}"; + return result; + } + + public override bool Equals(object? obj) + { + if (obj is VersionInfo other) + { + return Major == other.Major && + Minor == other.Minor && + Patch == other.Patch && + Revision == other.Revision && + PreRelease == other.PreRelease; + } + return false; + } + + public override int GetHashCode() + { + return HashCode.Combine(Major, Minor, Patch, Revision, PreRelease); + } + } + + /// + /// 版本差异 + /// + public class VersionDiff + { + /// + /// 旧版本 + /// + public VersionInfo OldVersion { get; set; } = new(); + + /// + /// 新版本 + /// + public VersionInfo NewVersion { get; set; } = new(); + + /// + /// 主版本差异 + /// + public int MajorDiff { get; set; } + + /// + /// 次版本差异 + /// + public int MinorDiff { get; set; } + + /// + /// 补丁版本差异 + /// + public int PatchDiff { get; set; } + + /// + /// 修订版本差异 + /// + public int RevisionDiff { get; set; } + + /// + /// 变更级别 + /// + public VersionLevel ChangeLevel { get; set; } + + /// + /// 是否为升级 + /// + public bool IsUpgrade => + MajorDiff > 0 || + (MajorDiff == 0 && MinorDiff > 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff > 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff > 0); + + /// + /// 是否为降级 + /// + public bool IsDowngrade => + MajorDiff < 0 || + (MajorDiff == 0 && MinorDiff < 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff < 0) || + (MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff < 0); + + /// + /// 是否无变化 + /// + public bool IsUnchanged => MajorDiff == 0 && MinorDiff == 0 && PatchDiff == 0 && RevisionDiff == 0; + } + + /// + /// 版本级别 + /// + public enum VersionLevel + { + /// + /// 主版本 + /// + Major, + + /// + /// 次版本 + /// + Minor, + + /// + /// 补丁版本 + /// + Patch, + + /// + /// 修订版本 + /// + Revision + } +} diff --git a/EasyTool.Core/ValidationCategory/CompositeValidator.cs b/EasyTool.Core/ValidationCategory/CompositeValidator.cs new file mode 100644 index 0000000..d5c79e1 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/CompositeValidator.cs @@ -0,0 +1,406 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 组合验证器,支持多个验证器的组合使用 + /// + public class CompositeValidator + { + private readonly List> _validators = new(); + private readonly List> _validationFuncs = new(); + private bool _stopOnFirstFailure; + + /// + /// 添加验证器 + /// + public CompositeValidator Add(IValidator validator) + { + _validators.Add(validator); + return this; + } + + /// + /// 添加验证函数 + /// + public CompositeValidator Add(Func validationFunc) + { + _validationFuncs.Add(validationFunc); + return this; + } + + /// + /// 添加条件验证 + /// + public CompositeValidator AddWhen(Func condition, IValidator validator) + { + _validationFuncs.Add(obj => + { + if (condition(obj)) + { + return validator.Validate(obj); + } + return ValidationResult.Success(); + }); + return this; + } + + /// + /// 添加条件验证函数 + /// + public CompositeValidator AddWhen(Func condition, Func validationFunc) + { + _validationFuncs.Add(obj => + { + if (condition(obj)) + { + return validationFunc(obj); + } + return ValidationResult.Success(); + }); + return this; + } + + /// + /// 设置遇到第一个错误就停止 + /// + public CompositeValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + var allErrors = new List(); + + foreach (var validator in _validators) + { + var result = validator.Validate(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + foreach (var validationFunc in _validationFuncs) + { + var result = validationFunc(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + return allErrors.Count == 0 + ? ValidationResult.Success() + : new ValidationResult(false, allErrors); + } + + /// + /// 异步验证对象 + /// + public async Task ValidateAsync(T instance) + { + var allErrors = new List(); + + foreach (var validator in _validators) + { + var result = await validator.ValidateAsync(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + foreach (var validationFunc in _validationFuncs) + { + var result = validationFunc(instance); + if (!result.IsValid) + { + allErrors.AddRange(result.Errors); + if (_stopOnFirstFailure) + { + return new ValidationResult(false, allErrors); + } + } + } + + return allErrors.Count == 0 + ? ValidationResult.Success() + : new ValidationResult(false, allErrors); + } + } + + /// + /// 批量验证器,支持批量验证多个对象 + /// + public class BatchValidator + { + private readonly Dictionary> _propertyValidators = new(); + private bool _stopOnFirstFailure; + + /// + /// 添加属性验证器 + /// + public BatchValidator Add(string propertyName, Func validator) + { + _propertyValidators[propertyName] = validator; + return this; + } + + /// + /// 添加属性验证器(使用 FluentValidator) + /// + public BatchValidator Add(string propertyName, TProperty value, Action> configure) + { + var validator = FluentValidator.For(value, propertyName); + configure(validator); + _propertyValidators[propertyName] = _ => + { + var result = validator.GetResult(); + return result; + }; + return this; + } + + /// + /// 设置遇到第一个错误就停止 + /// + public BatchValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 验证所有属性 + /// + public BatchValidationResult Validate() + { + var propertyResults = new Dictionary(); + var allErrors = new List(); + + foreach (var kvp in _propertyValidators) + { + var result = kvp.Value(null); + propertyResults[kvp.Key] = result; + + if (!result.IsValid) + { + allErrors.AddRange(result.Errors.Select(e => $"[{kvp.Key}] {e}")); + if (_stopOnFirstFailure) + { + break; + } + } + } + + return new BatchValidationResult(allErrors.Count == 0, allErrors, propertyResults); + } + } + + /// + /// 批量验证结果 + /// + public class BatchValidationResult + { + /// + /// 是否全部验证通过 + /// + public bool IsValid { get; } + + /// + /// 所有错误消息 + /// + public IReadOnlyList AllErrors { get; } + + /// + /// 按属性分组的验证结果 + /// + public IReadOnlyDictionary PropertyResults { get; } + + /// + /// 第一个错误消息 + /// + public string? FirstError => AllErrors.FirstOrDefault(); + + public BatchValidationResult(bool isValid, List allErrors, Dictionary propertyResults) + { + IsValid = isValid; + AllErrors = allErrors.AsReadOnly(); + PropertyResults = propertyResults; + } + + /// + /// 获取指定属性的验证结果 + /// + public ValidationResult? GetPropertyResult(string propertyName) + { + return PropertyResults.TryGetValue(propertyName, out var result) ? result : null; + } + + /// + /// 获取指定属性的错误消息 + /// + public IReadOnlyList GetPropertyErrors(string propertyName) + { + return GetPropertyResult(propertyName)?.Errors ?? new List().AsReadOnly(); + } + + /// + /// 获取失败的属性名列表 + /// + public IEnumerable GetFailedProperties() + { + return PropertyResults.Where(kvp => !kvp.Value.IsValid).Select(kvp => kvp.Key); + } + } + + /// + /// 验证器集合,用于管理多个类型的验证器 + /// + public class ValidatorCollection + { + private readonly Dictionary _validators = new(); + + /// + /// 注册验证器 + /// + public ValidatorCollection Register(IValidator validator) + { + _validators[typeof(T)] = validator; + return this; + } + + /// + /// 注册验证器构建器 + /// + public ValidatorCollection Register(Action> configure) + { + var builder = new ValidationRuleBuilder(); + configure(builder); + _validators[typeof(T)] = builder.Build(); + return this; + } + + /// + /// 获取验证器 + /// + public IValidator? Get() + { + return _validators.TryGetValue(typeof(T), out var validator) ? validator as IValidator : null; + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + var validator = Get(); + if (validator == null) + { + // 如果没有注册验证器,尝试使用 ModelValidator + return ModelValidator.Validate(instance); + } + return validator.Validate(instance); + } + + /// + /// 异步验证对象 + /// + public async Task ValidateAsync(T instance) + { + var validator = Get(); + if (validator == null) + { + return await ModelValidator.ValidateAsync(instance); + } + return await validator.ValidateAsync(instance); + } + + /// + /// 验证并抛出异常 + /// + public void ValidateAndThrow(T instance) + { + var result = Validate(instance); + if (!result.IsValid) + { + throw new ValidationException(result.Errors); + } + } + + /// + /// 检查是否已注册验证器 + /// + public bool IsRegistered() + { + return _validators.ContainsKey(typeof(T)); + } + + /// + /// 移除验证器 + /// + public bool Remove() + { + return _validators.Remove(typeof(T)); + } + + /// + /// 清空所有验证器 + /// + public void Clear() + { + _validators.Clear(); + } + } + + /// + /// 验证器扩展方法 + /// + public static class CompositeValidatorExtensions + { + /// + /// 创建组合验证器 + /// + public static CompositeValidator CreateCompositeValidator() + { + return new CompositeValidator(); + } + + /// + /// 创建批量验证器 + /// + public static BatchValidator CreateBatchValidator() + { + return new BatchValidator(); + } + + /// + /// 创建验证器集合 + /// + public static ValidatorCollection CreateValidatorCollection() + { + return new ValidatorCollection(); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/FluentValidator.cs b/EasyTool.Core/ValidationCategory/FluentValidator.cs new file mode 100644 index 0000000..6912731 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/FluentValidator.cs @@ -0,0 +1,437 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace EasyTool.ValidationCategory +{ + /// + /// 流式验证器 + /// + public class FluentValidator + { + private readonly T _value; + private readonly string _propertyName; + private readonly List _errors = new(); + private bool _stopOnFirstFailure; + + private FluentValidator(T value, string propertyName) + { + _value = value; + _propertyName = propertyName; + } + + /// + /// 开始验证 + /// + public static FluentValidator For(T value, string propertyName = "") + { + return new FluentValidator(value, propertyName); + } + + /// + /// 遇到第一个错误就停止 + /// + public FluentValidator StopOnFirstFailure() + { + _stopOnFirstFailure = true; + return this; + } + + /// + /// 自定义验证 + /// + public FluentValidator Must(Func predicate, string errorMessage) + { + if (ShouldValidate() && !predicate(_value)) + { + AddError(errorMessage); + } + return this; + } + + /// + /// 自定义异步验证 + /// + public async System.Threading.Tasks.Task> MustAsync(Func> predicate, string errorMessage) + { + if (ShouldValidate() && !await predicate(_value)) + { + AddError(errorMessage); + } + return this; + } + + /// + /// 不能为null + /// + public FluentValidator NotNull(string? errorMessage = null) + { + if (ShouldValidate() && _value == null) + { + AddError(errorMessage ?? $"{_propertyName}不能为空"); + } + return this; + } + + /// + /// 字符串不能为空 + /// + public FluentValidator NotEmpty(string? errorMessage = null) + { + if (ShouldValidate() && string.IsNullOrEmpty(_value as string)) + { + AddError(errorMessage ?? $"{_propertyName}不能为空"); + } + return this; + } + + /// + /// 字符串不能为空白 + /// + public FluentValidator NotWhiteSpace(string? errorMessage = null) + { + if (ShouldValidate() && string.IsNullOrWhiteSpace(_value as string)) + { + AddError(errorMessage ?? $"{_propertyName}不能为空白"); + } + return this; + } + + /// + /// 字符串长度范围 + /// + public FluentValidator Length(int min, int max, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && (str.Length < min || str.Length > max)) + { + AddError(errorMessage ?? $"{_propertyName}长度必须在{min}到{max}之间"); + } + } + return this; + } + + /// + /// 最小长度 + /// + public FluentValidator MinLength(int min, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && str.Length < min) + { + AddError(errorMessage ?? $"{_propertyName}长度不能小于{min}"); + } + } + return this; + } + + /// + /// 最大长度 + /// + public FluentValidator MaxLength(int max, string? errorMessage = null) + { + if (ShouldValidate()) + { + var str = _value as string; + if (str != null && str.Length > max) + { + AddError(errorMessage ?? $"{_propertyName}长度不能超过{max}"); + } + } + return this; + } + + /// + /// 数值范围 + /// + public FluentValidator InRange(IComparable min, IComparable max, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(min) < 0 || comparable.CompareTo(max) > 0) + { + AddError(errorMessage ?? $"{_propertyName}必须在{min}到{max}之间"); + } + } + return this; + } + + /// + /// 大于指定值 + /// + public FluentValidator GreaterThan(IComparable threshold, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(threshold) <= 0) + { + AddError(errorMessage ?? $"{_propertyName}必须大于{threshold}"); + } + } + return this; + } + + /// + /// 小于指定值 + /// + public FluentValidator LessThan(IComparable threshold, string? errorMessage = null) + { + if (ShouldValidate() && _value is IComparable comparable) + { + if (comparable.CompareTo(threshold) >= 0) + { + AddError(errorMessage ?? $"{_propertyName}必须小于{threshold}"); + } + } + return this; + } + + /// + /// 正则匹配 + /// + public FluentValidator Matches(string pattern, string? errorMessage = null) + { + if (ShouldValidate() && _value != null) + { + if (!Regex.IsMatch(_value.ToString() ?? "", pattern)) + { + AddError(errorMessage ?? $"{_propertyName}格式不正确"); + } + } + return this; + } + + /// + /// 邮箱格式 + /// + public FluentValidator Email(string? errorMessage = null) + { + return Matches(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", errorMessage ?? $"{_propertyName}不是有效的邮箱地址"); + } + + /// + /// 手机号格式(中国) + /// + public FluentValidator Phone(string? errorMessage = null) + { + return Matches(@"^1[3-9]\d{9}$", errorMessage ?? $"{_propertyName}不是有效的手机号"); + } + + /// + /// 身份证号格式(中国) + /// + public FluentValidator IdCard(string? errorMessage = null) + { + return Matches(@"^\d{17}[\dXx]$", errorMessage ?? $"{_propertyName}不是有效的身份证号"); + } + + /// + /// URL格式 + /// + public FluentValidator Url(string? errorMessage = null) + { + return Matches(@"^https?://[^\s]+$", errorMessage ?? $"{_propertyName}不是有效的URL"); + } + + /// + /// IP地址格式 + /// + public FluentValidator IpAddress(string? errorMessage = null) + { + return Matches(@"^(\d{1,3}\.){3}\d{1,3}$", errorMessage ?? $"{_propertyName}不是有效的IP地址"); + } + + /// + /// 在指定值列表中 + /// + public FluentValidator In(IEnumerable values, string? errorMessage = null) + { + if (ShouldValidate() && !values.Contains(_value)) + { + AddError(errorMessage ?? $"{_propertyName}必须是有效值之一"); + } + return this; + } + + /// + /// 等于指定值 + /// + public FluentValidator Equal(T expected, string? errorMessage = null) + { + if (ShouldValidate() && !EqualityComparer.Default.Equals(_value, expected)) + { + AddError(errorMessage ?? $"{_propertyName}必须等于{expected}"); + } + return this; + } + + /// + /// 不等于指定值 + /// + public FluentValidator NotEqual(T unexpected, string? errorMessage = null) + { + if (ShouldValidate() && EqualityComparer.Default.Equals(_value, unexpected)) + { + AddError(errorMessage ?? $"{_propertyName}不能等于{unexpected}"); + } + return this; + } + + /// + /// 集合不为空 + /// + public FluentValidator NotNullOrEmpty(string? errorMessage = null) + { + if (ShouldValidate()) + { + if (_value is System.Collections.ICollection collection && collection.Count == 0) + { + AddError(errorMessage ?? $"{_propertyName}不能为空集合"); + } + else if (_value is System.Collections.IEnumerable enumerable && !enumerable.Cast().Any()) + { + AddError(errorMessage ?? $"{_propertyName}不能为空集合"); + } + } + return this; + } + + /// + /// 条件验证 + /// + public FluentValidator When(Func condition, Action> action) + { + if (condition(_value)) + { + action(this); + } + return this; + } + + /// + /// 反条件验证 + /// + public FluentValidator Unless(Func condition, Action> action) + { + if (!condition(_value)) + { + action(this); + } + return this; + } + + /// + /// 获取验证结果 + /// + public ValidationResult GetResult() + { + return new ValidationResult(_errors.Count == 0, _errors); + } + + /// + /// 是否验证通过 + /// + public bool IsValid => _errors.Count == 0; + + /// + /// 获取错误消息 + /// + public IReadOnlyList Errors => _errors.AsReadOnly(); + + /// + /// 获取第一条错误消息 + /// + public string? FirstError => _errors.FirstOrDefault(); + + /// + /// 抛出验证异常 + /// + public void ThrowIfInvalid() + { + if (!IsValid) + { + throw new ValidationException(_errors); + } + } + + private bool ShouldValidate() => !_stopOnFirstFailure || _errors.Count == 0; + + private void AddError(string error) + { + _errors.Add(error); + } + } + + /// + /// 验证结果 + /// + public class ValidationResult + { + /// + /// 是否有效 + /// + public bool IsValid { get; } + + /// + /// 错误消息 + /// + public IReadOnlyList Errors { get; } + + /// + /// 第一条错误消息 + /// + public string? FirstError => Errors.FirstOrDefault(); + + public ValidationResult(bool isValid, List errors) + { + IsValid = isValid; + Errors = errors.AsReadOnly(); + } + + public static ValidationResult Success() => new ValidationResult(true, new List()); + + public static ValidationResult Failure(params string[] errors) => new ValidationResult(false, errors.ToList()); + } + + /// + /// 验证异常 + /// + public class ValidationException : Exception + { + /// + /// 错误消息列表 + /// + public IReadOnlyList Errors { get; } + + public ValidationException(IEnumerable errors) + : base(string.Join("; ", errors)) + { + Errors = errors.ToList().AsReadOnly(); + } + + public ValidationException(string error) + : base(error) + { + Errors = new List { error }.AsReadOnly(); + } + } + + /// + /// 验证器扩展 + /// + public static class FluentValidatorExtensions + { + /// + /// 验证对象 + /// + public static FluentValidator Validate(this T value, string propertyName = "") + { + return FluentValidator.For(value, propertyName); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/ModelValidator.cs b/EasyTool.Core/ValidationCategory/ModelValidator.cs new file mode 100644 index 0000000..82d9dd2 --- /dev/null +++ b/EasyTool.Core/ValidationCategory/ModelValidator.cs @@ -0,0 +1,352 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 模型验证器,基于 DataAnnotations 特性进行验证 + /// + public static class ModelValidator + { + /// + /// 验证模型 + /// + /// 模型类型 + /// 要验证的模型实例 + /// 是否验证所有属性 + /// 验证结果 + public static ValidationResult Validate(T model, bool validateAllProperties = true) + { + if (model == null) + { + return ValidationResult.Failure("模型不能为空"); + } + + var context = new ValidationContext(model, null, null); + var results = new List(); + var isValid = Validator.TryValidateObject(model, context, results, validateAllProperties); + + if (isValid) + { + return ValidationResult.Success(); + } + + var errors = results.Select(r => r.ErrorMessage ?? "验证失败").ToList(); + return new ValidationResult(false, errors); + } + + /// + /// 异步验证模型 + /// + public static async Task ValidateAsync(T model, bool validateAllProperties = true) + { + return await Task.Run(() => Validate(model, validateAllProperties)); + } + + /// + /// 验证模型并抛出异常 + /// + public static void ValidateAndThrow(T model, bool validateAllProperties = true) + { + var result = Validate(model, validateAllProperties); + if (!result.IsValid) + { + throw new ValidationException(result.Errors); + } + } + + /// + /// 验证单个属性 + /// + /// 模型类型 + /// 属性类型 + /// 模型实例 + /// 属性名 + /// 属性值 + /// 验证结果 + public static ValidationResult ValidateProperty(T model, string propertyName, TProperty value) + { + if (model == null) + { + return ValidationResult.Failure("模型不能为空"); + } + + var context = new ValidationContext(model) { MemberName = propertyName }; + var results = new List(); + var isValid = Validator.TryValidateProperty(value, context, results); + + if (isValid) + { + return ValidationResult.Success(); + } + + var errors = results.Select(r => r.ErrorMessage ?? "验证失败").ToList(); + return new ValidationResult(false, errors); + } + + /// + /// 获取模型的所有验证属性 + /// + public static IEnumerable GetValidationAttributes() + { + var type = typeof(T); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in properties) + { + var attributes = property.GetCustomAttributes(); + var displayAttribute = property.GetCustomAttribute(); + var info = new PropertyValidationInfo + { + PropertyName = property.Name, + DisplayName = displayAttribute?.GetName() ?? property.Name, + ValidationAttributes = attributes.ToList() + }; + yield return info; + } + } + + /// + /// 尝试验证并获取验证错误信息字典 + /// + public static Dictionary> ValidateToDictionary(T model, bool validateAllProperties = true) + { + var result = Validate(model, validateAllProperties); + var dictionary = new Dictionary>(); + + if (result.IsValid) + { + return dictionary; + } + + // 尝试按属性分组错误信息 + var context = new ValidationContext(model, null, null); + var results = new List(); + Validator.TryValidateObject(model, context, results, validateAllProperties); + + foreach (var validationResult in results) + { + var propertyNames = validationResult.MemberNames.ToList(); + if (propertyNames.Count == 0) + { + propertyNames.Add(string.Empty); + } + + foreach (var propertyName in propertyNames) + { + if (!dictionary.ContainsKey(propertyName)) + { + dictionary[propertyName] = new List(); + } + dictionary[propertyName].Add(validationResult.ErrorMessage ?? "验证失败"); + } + } + + return dictionary; + } + + /// + /// 验证字典数据 + /// + public static ValidationResult ValidateDictionary(IDictionary data, IEnumerable rules) + { + var errors = new List(); + var rulesDict = rules.ToDictionary(r => r.PropertyName, r => r); + + foreach (var rule in rulesDict.Values) + { + if (!data.TryGetValue(rule.PropertyName, out var value)) + { + if (rule.IsRequired) + { + errors.Add(rule.RequiredErrorMessage ?? $"{rule.PropertyName}是必填项"); + } + continue; + } + + foreach (var validator in rule.Validators) + { + if (!validator(value)) + { + errors.Add(rule.ErrorMessage ?? $"{rule.PropertyName}验证失败"); + } + } + } + + return errors.Count == 0 ? ValidationResult.Success() : new ValidationResult(false, errors); + } + + /// + /// 验证对象字典 + /// + public static ValidationResult ValidateObjectDictionary(IDictionary data, Type modelType) + { + var errors = new List(); + var properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in properties) + { + var validationAttributes = property.GetCustomAttributes(); + var displayAttribute = property.GetCustomAttribute(); + var displayName = displayAttribute?.GetName() ?? property.Name; + + if (!data.TryGetValue(property.Name, out var value)) + { + var requiredAttr = validationAttributes.FirstOrDefault(a => a is RequiredAttribute); + if (requiredAttr != null) + { + errors.Add(requiredAttr.ErrorMessage ?? $"{displayName}是必填项"); + } + continue; + } + + foreach (var attr in validationAttributes) + { + try + { + // 类型转换 + var convertedValue = value == null ? null : Convert.ChangeType(value, property.PropertyType); + if (!attr.IsValid(convertedValue)) + { + errors.Add(attr.ErrorMessage ?? $"{displayName}验证失败"); + } + } + catch (Exception) + { + errors.Add($"{displayName}类型转换失败"); + } + } + } + + return errors.Count == 0 ? ValidationResult.Success() : new ValidationResult(false, errors); + } + } + + /// + /// 属性验证信息 + /// + public class PropertyValidationInfo + { + /// + /// 属性名称 + /// + public string PropertyName { get; set; } = string.Empty; + + /// + /// 显示名称 + /// + public string DisplayName { get; set; } = string.Empty; + + /// + /// 验证特性列表 + /// + public IReadOnlyList ValidationAttributes { get; set; } = new List(); + } + + /// + /// 属性验证规则 + /// + public class PropertyValidationRule + { + /// + /// 属性名称 + /// + public string PropertyName { get; set; } = string.Empty; + + /// + /// 是否必填 + /// + public bool IsRequired { get; set; } + + /// + /// 必填错误消息 + /// + public string? RequiredErrorMessage { get; set; } + + /// + /// 验证器列表 + /// + public List> Validators { get; set; } = new(); + + /// + /// 验证失败错误消息 + /// + public string? ErrorMessage { get; set; } + + /// + /// 创建属性验证规则 + /// + public static PropertyValidationRule Create(string propertyName) + { + return new PropertyValidationRule { PropertyName = propertyName }; + } + + /// + /// 设置为必填 + /// + public PropertyValidationRule Required(string? errorMessage = null) + { + IsRequired = true; + RequiredErrorMessage = errorMessage; + return this; + } + + /// + /// 添加验证器 + /// + public PropertyValidationRule AddValidator(Func validator, string? errorMessage = null) + { + Validators.Add(validator); + if (!string.IsNullOrEmpty(errorMessage)) + { + ErrorMessage = errorMessage; + } + return this; + } + + /// + /// 添加正则验证 + /// + public PropertyValidationRule Regex(string pattern, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + return System.Text.RegularExpressions.Regex.IsMatch(value.ToString() ?? "", pattern); + }, errorMessage); + } + + /// + /// 添加长度验证 + /// + public PropertyValidationRule Length(int min, int max, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + var str = value.ToString() ?? ""; + return str.Length >= min && str.Length <= max; + }, errorMessage); + } + + /// + /// 添加范围验证 + /// + public PropertyValidationRule Range(IComparable min, IComparable max, string? errorMessage = null) + { + return AddValidator(value => + { + if (value == null) return true; + if (value is IComparable comparable) + { + return comparable.CompareTo(min) >= 0 && comparable.CompareTo(max) <= 0; + } + return true; + }, errorMessage); + } + } +} diff --git a/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs b/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs new file mode 100644 index 0000000..594b00f --- /dev/null +++ b/EasyTool.Core/ValidationCategory/ValidationRuleBuilder.cs @@ -0,0 +1,377 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace EasyTool.ValidationCategory +{ + /// + /// 验证规则构建器,支持链式调用构建复杂验证规则 + /// + public class ValidationRuleBuilder + { + private readonly List> _rules = new(); + private string? _currentProperty; + private string? _currentErrorMessage; + + /// + /// 为指定属性添加规则 + /// + public ValidationRuleBuilder RuleFor(Expression> propertyExpression) + { + _currentProperty = GetPropertyName(propertyExpression); + _currentErrorMessage = null; + return this; + } + + /// + /// 设置自定义错误消息 + /// + public ValidationRuleBuilder WithMessage(string errorMessage) + { + _currentErrorMessage = errorMessage; + return this; + } + + /// + /// 必须满足条件 + /// + public ValidationRuleBuilder Must(Expression> propertyExpression, Func predicate) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => predicate(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}验证失败" + }); + return this; + } + + /// + /// 不能为null + /// + public ValidationRuleBuilder NotNull(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => getter(obj) != null, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空" + }); + return this; + } + + /// + /// 字符串不能为空 + /// + public ValidationRuleBuilder NotEmpty(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => !string.IsNullOrEmpty(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空" + }); + return this; + } + + /// + /// 字符串不能为空白 + /// + public ValidationRuleBuilder NotWhiteSpace(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => !string.IsNullOrWhiteSpace(getter(obj)), + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空白" + }); + return this; + } + + /// + /// 字符串长度范围 + /// + public ValidationRuleBuilder Length(Expression> propertyExpression, int min, int max) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || (value.Length >= min && value.Length <= max); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}长度必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 数值范围 + /// + public ValidationRuleBuilder InRange(Expression> propertyExpression, TProperty min, TProperty max) + where TProperty : IComparable + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || (value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 正则匹配 + /// + public ValidationRuleBuilder Matches(Expression> propertyExpression, string pattern) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + var regex = new Regex(pattern); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value == null || regex.IsMatch(value); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}格式不正确" + }); + return this; + } + + /// + /// 邮箱格式 + /// + public ValidationRuleBuilder Email(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^[^@\s]+@[^@\s]+\.[^@\s]+$").WithMessage(_currentErrorMessage ?? "邮箱格式不正确"); + } + + /// + /// 手机号格式(中国) + /// + public ValidationRuleBuilder Phone(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^1[3-9]\d{9}$").WithMessage(_currentErrorMessage ?? "手机号格式不正确"); + } + + /// + /// URL格式 + /// + public ValidationRuleBuilder Url(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^https?://[^\s]+$").WithMessage(_currentErrorMessage ?? "URL格式不正确"); + } + + /// + /// IPv4地址格式 + /// + public ValidationRuleBuilder IPv4(Expression> propertyExpression) + { + return Matches(propertyExpression, @"^(\d{1,3}\.){3}\d{1,3}$").WithMessage(_currentErrorMessage ?? "IPv4地址格式不正确"); + } + + /// + /// 身份证号格式(中国) + /// + public ValidationRuleBuilder IdCard(Expression> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + if (string.IsNullOrEmpty(value)) return true; + return IsValidIdCard(value); + }, + ErrorMessage = _currentErrorMessage ?? "身份证号格式不正确" + }); + return this; + } + + /// + /// 集合不为空 + /// + public ValidationRuleBuilder NotEmpty(Expression>> propertyExpression) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + return value != null && value.Any(); + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}不能为空集合" + }); + return this; + } + + /// + /// 集合元素数量范围 + /// + public ValidationRuleBuilder CollectionLength(Expression>> propertyExpression, int min, int max) + { + var propertyName = GetPropertyName(propertyExpression); + var getter = propertyExpression.Compile(); + _rules.Add(new ValidationRule + { + PropertyName = propertyName, + Validate = obj => + { + var value = getter(obj); + if (value == null) return false; + var count = value.Count(); + return count >= min && count <= max; + }, + ErrorMessage = _currentErrorMessage ?? $"{propertyName}元素数量必须在{min}到{max}之间" + }); + return this; + } + + /// + /// 构建验证器 + /// + public IValidator Build() + { + return new RuleBasedValidator(_rules.ToList()); + } + + /// + /// 验证对象 + /// + public ValidationResult Validate(T instance) + { + return Build().Validate(instance); + } + + private static string GetPropertyName(Expression> expression) + { + return expression.Body switch + { + MemberExpression memberExpression => memberExpression.Member.Name, + UnaryExpression { Operand: MemberExpression me } => me.Member.Name, + _ => expression.ToString() + }; + } + + private static bool IsValidIdCard(string idCard) + { + if (string.IsNullOrEmpty(idCard) || idCard.Length != 18) + return false; + + // 基本格式检查 + if (!Regex.IsMatch(idCard, @"^\d{17}[\dXx]$")) + return false; + + // 校验码验证 + var weights = new[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; + var checkCodes = "10X98765432"; + var sum = 0; + for (int i = 0; i < 17; i++) + { + sum += (idCard[i] - '0') * weights[i]; + } + var checkCode = checkCodes[sum % 11]; + return char.ToUpper(idCard[17]) == checkCode; + } + } + + /// + /// 验证规则 + /// + public class ValidationRule + { + public string PropertyName { get; set; } = string.Empty; + public Func Validate { get; set; } = _ => true; + public string ErrorMessage { get; set; } = string.Empty; + } + + /// + /// 验证器接口 + /// + public interface IValidator + { + ValidationResult Validate(T instance); + Task ValidateAsync(T instance); + } + + /// + /// 基于规则的验证器 + /// + internal class RuleBasedValidator : IValidator + { + private readonly List> _rules; + + public RuleBasedValidator(List> rules) + { + _rules = rules; + } + + public ValidationResult Validate(T instance) + { + var errors = new List(); + foreach (var rule in _rules) + { + try + { + if (!rule.Validate(instance)) + { + errors.Add(rule.ErrorMessage); + } + } + catch (Exception ex) + { + errors.Add($"{rule.PropertyName}验证异常: {ex.Message}"); + } + } + return new ValidationResult(errors.Count == 0, errors); + } + + public async Task ValidateAsync(T instance) + { + return await Task.Run(() => Validate(instance)); + } + } + + /// + /// 验证规则构建器静态扩展 + /// + public static class ValidationRuleBuilderExtensions + { + /// + /// 创建验证规则构建器 + /// + public static ValidationRuleBuilder CreateValidator() + { + return new ValidationRuleBuilder(); + } + } +} diff --git a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs index f5fdbaa..688b659 100644 --- a/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs +++ b/EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs @@ -1,4 +1,4 @@ -using EasyTool.Extension; +using EasyTool.ToolCategory; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace EasyTool.Tests @@ -7,7 +7,7 @@ namespace EasyTool.Tests public class CloneExtensionTests { [TestMethod()] - public void CloneTest() + public void DeepCloneTest() { var obj1 = new First() { @@ -24,7 +24,7 @@ public void CloneTest() MyProperty2 = "C", } }; - var obj2 = obj1.Clone(); + var obj2 = obj1.DeepClone(); Assert.AreEqual(obj1.MyProperty1, obj2.MyProperty1); Assert.AreEqual(obj1.Second1.MyProperty1, obj2.Second1.MyProperty1); diff --git a/EasyTool.CoreTests/CodeCategory/AesUtilTests.cs b/EasyTool.CoreTests/CodeCategory/AesUtilTests.cs index 26b46dd..b515b1b 100644 --- a/EasyTool.CoreTests/CodeCategory/AesUtilTests.cs +++ b/EasyTool.CoreTests/CodeCategory/AesUtilTests.cs @@ -18,7 +18,7 @@ public void EncryptSecret16Test() var sk = "1234567890123456"; var en = AesUtil.Encrypt(input, sk); var de = AesUtil.Decrypt(en, sk); - Assert.IsTrue(de == input); + Assert.AreEqual(input, de); } [TestMethod()] @@ -28,7 +28,7 @@ public void EncryptSecret24Test() var sk = "123456789012345678901234"; var en = AesUtil.Encrypt(input, sk); var de = AesUtil.Decrypt(en, sk); - Assert.IsTrue(de == input); + Assert.AreEqual(input, de); } [TestMethod()] @@ -38,7 +38,7 @@ public void EncryptSecret32Test() var sk = "12345678901234567890123456789012"; var en = AesUtil.Encrypt(input, sk); var de = AesUtil.Decrypt(en, sk); - Assert.IsTrue(de == input); + Assert.AreEqual(input, de); } } } \ No newline at end of file diff --git a/EasyTool.CoreTests/CodeCategory/DesUtilTests.cs b/EasyTool.CoreTests/CodeCategory/DesUtilTests.cs index 6ffd323..6924df3 100644 --- a/EasyTool.CoreTests/CodeCategory/DesUtilTests.cs +++ b/EasyTool.CoreTests/CodeCategory/DesUtilTests.cs @@ -18,7 +18,7 @@ public void EncryptSecret8Test() var sk = "12345678"; var en = DesUtil.Encrypt(input, sk); var de = DesUtil.Decrypt(en, sk); - Assert.IsTrue(de == input); + Assert.AreEqual(input, de); } } } \ No newline at end of file diff --git a/EasyTool.CoreTests/EasyTool.CoreTests.csproj b/EasyTool.CoreTests/EasyTool.CoreTests.csproj index 89de440..472342c 100644 --- a/EasyTool.CoreTests/EasyTool.CoreTests.csproj +++ b/EasyTool.CoreTests/EasyTool.CoreTests.csproj @@ -1,21 +1,23 @@ - + - .net6.0 - 11 - enable + net8.0 + true + latest + annotations enable - enable false true - + MSTest + $(NoWarn);MSTEST0001 + - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs b/EasyTool.CoreTests/IdentifierCategory/IDUtilTests.cs similarity index 83% rename from EasyTool.CoreTests/ToolCategory/IDUtilTests.cs rename to EasyTool.CoreTests/IdentifierCategory/IDUtilTests.cs index 7e796cf..e3d19b2 100644 --- a/EasyTool.CoreTests/ToolCategory/IDUtilTests.cs +++ b/EasyTool.CoreTests/IdentifierCategory/IDUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.IdentifierCategory; using System; using System.Collections.Generic; using System.Linq; @@ -19,7 +19,7 @@ public void NextSequenceUUID_AreGreaterThan() Thread.Sleep(10); var uuid2 = IdUtil.UUID(UUIDStyle.Sequence); - Assert.IsTrue(uuid2.ToString().CompareTo(uuid1.ToString()) > 0); + Assert.IsGreaterThan(uuid1.ToString(), uuid2.ToString()); } } } \ No newline at end of file diff --git a/EasyTool.CoreTests/MathCategory/MathUtilTests.cs b/EasyTool.CoreTests/MathCategory/MathUtilTests.cs index 9a5598b..9a7e7ab 100644 --- a/EasyTool.CoreTests/MathCategory/MathUtilTests.cs +++ b/EasyTool.CoreTests/MathCategory/MathUtilTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.MathCategory; using System; using System.Collections.Generic; using System.Text; @@ -13,7 +13,7 @@ public class MathUtilTests public void GcdTest() { var result = MathUtil.Gcd(5, 20); - Assert.IsTrue(result == 5); + Assert.AreEqual(5, result); } } } \ No newline at end of file diff --git a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs b/EasyTool.CoreTests/NetCategory/IpUtilTests.cs similarity index 90% rename from EasyTool.CoreTests/ToolCategory/IpUtilTests.cs rename to EasyTool.CoreTests/NetCategory/IpUtilTests.cs index e79f0a3..5714715 100644 --- a/EasyTool.CoreTests/ToolCategory/IpUtilTests.cs +++ b/EasyTool.CoreTests/NetCategory/IpUtilTests.cs @@ -1,6 +1,6 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.NetCategory; namespace EasyTool.Tests { @@ -101,10 +101,16 @@ public void IsPrivateIpv4_ValidPrivateIps_ReturnsTrue() /// 验证无效的 IPv4 地址,应引发 ArgumentException 异常。 /// [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void IsPrivateIpv4_InvalidIp_ThrowsException() { - IpUtil.IsPrivateIpv4("256.256.256.256"); + try + { + IpUtil.IsPrivateIpv4("256.256.256.256"); + Assert.Fail("Expected ArgumentException was not thrown."); + } + catch (ArgumentException) + { + } } /// @@ -120,10 +126,16 @@ public void IsPrivateIpv6_ValidPrivateIps_ReturnsTrue() /// 验证无效的 IPv6 地址,应引发 ArgumentException 异常。 /// [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void IsPrivateIpv6_InvalidIp_ThrowsException() { - IpUtil.IsPrivateIpv6("2001::1::2"); + try + { + IpUtil.IsPrivateIpv6("2001::1::2"); + Assert.Fail("Expected ArgumentException was not thrown."); + } + catch (ArgumentException) + { + } } /// diff --git a/EasyTool.CoreTests/Standardization/OptionTests.cs b/EasyTool.CoreTests/Standardization/OptionTests.cs index 9b79407..63ec0c9 100644 --- a/EasyTool.CoreTests/Standardization/OptionTests.cs +++ b/EasyTool.CoreTests/Standardization/OptionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.Standardization; using System; using System.Collections.Generic; using System.Linq; @@ -17,9 +17,9 @@ public void ToOptionsTest() { var options = new LogLevel().ToOptions(); Assert.IsNotNull(options); - Assert.IsTrue(options.Count == 4); - Assert.IsTrue(options[0].Value == "Debug"); - Assert.IsTrue(options[0].Text == "调试"); + Assert.HasCount(4, options); + Assert.AreEqual("Debug", options[0].Value); + Assert.AreEqual("调试", options[0].Text); } @@ -28,9 +28,9 @@ public void GetOptionsTest() { var options = IOption.GetOptions(); Assert.IsNotNull(options); - Assert.IsTrue(options.Count == 4); - Assert.IsTrue(options[0].Value == "Debug"); - Assert.IsTrue(options[0].Text == "调试"); + Assert.HasCount(4, options); + Assert.AreEqual("Debug", options[0].Value); + Assert.AreEqual("调试", options[0].Text); } public class LogLevel : IOption diff --git a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs index 6349bed..175b94d 100644 --- a/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs +++ b/EasyTool.CoreTests/ToolCategory/SimpleMapExtensionTests.cs @@ -1,5 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool; +using EasyTool.ToolCategory; using System; using System.Collections.Generic; diff --git a/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj b/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj index 0996f96..6471791 100644 --- a/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj +++ b/EasyTool.EmitMapper/EasyTool.EmitMapper.csproj @@ -1,13 +1,14 @@ - + - netstandard2.1;.net6.0 - 11 - enable + netstandard2.1 + latest + annotations $(MSBuildProjectName.Replace(" ", "_").Replace(".EmitMapper", "")) + Joce.EasyTool.EmitMapper 一个大西瓜,TimChen - 2023.0914.1 + 2026.0108.1 A open source C# tool to make .NET easy diff --git a/EasyTool.EmitMapper/EmitMapperCategory/EmitMapperExtension.cs b/EasyTool.EmitMapper/EmitMapperCategory/EmitMapperExtension.cs index 5ff2d69..2ba0579 100644 --- a/EasyTool.EmitMapper/EmitMapperCategory/EmitMapperExtension.cs +++ b/EasyTool.EmitMapper/EmitMapperCategory/EmitMapperExtension.cs @@ -17,7 +17,8 @@ public static TDestination EmitMapTo(this TSource obj) { if (obj == null) return default; - return ObjectMapperManager.DefaultInstance.GetMapper().Map(obj); + var mapper = ObjectMapperManager.DefaultInstance.GetMapper(); + return mapper != null ? mapper.Map(obj) : default; } /// diff --git a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj b/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj deleted file mode 100644 index 1c32465..0000000 --- a/EasyTool.EmitMapperTests/EasyTool.EmitMapperTests.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs b/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs deleted file mode 100644 index 1384f22..0000000 --- a/EasyTool.EmitMapperTests/EmitMapperCategory/EmitMapperExtensionTests.cs +++ /dev/null @@ -1,38 +0,0 @@ -using EasyTool.Extension; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace EasyTool.Tests -{ - [TestClass()] - public class EmitMapperExtensionTests - { - [TestMethod()] - public void EmitMapperTest() - { - var obj1 = new First() - { - MyProperty1 = 1, - MyProperty2 = "A" - }; - - var obj2 = obj1.EmitMapTo(); - - Assert.AreEqual(obj1.MyProperty1, obj2.MyProperty1); - Assert.AreEqual(obj1.MyProperty2, obj2.MyProperty2); - } - - [Serializable] - public class First - { - public int MyProperty1 { get; set; } - public string MyProperty2 { get; set; } - } - - [Serializable] - public class Second - { - public int MyProperty1 { get; set; } - public string MyProperty2 { get; set; } - } - } -} diff --git a/EasyTool.Image/EasyTool.Image.csproj b/EasyTool.Image/EasyTool.Image.csproj index 6a444e4..c6dccd0 100644 --- a/EasyTool.Image/EasyTool.Image.csproj +++ b/EasyTool.Image/EasyTool.Image.csproj @@ -1,13 +1,14 @@ - netstandard2.1;.net6.0 - 11 - enable + netstandard2.1 + latest + annotations $(MSBuildProjectName.Replace(" ", "_").Replace(".Core", "")) + Joce.EasyTool.Image 一个大西瓜,TimChen - 2023.0908.1 + 2026.0108.1 A open source C# tool to make .NET easy @@ -35,8 +36,8 @@ - - + + diff --git a/EasyTool.ImageTests/EasyTool.ImageTests.csproj b/EasyTool.ImageTests/EasyTool.ImageTests.csproj deleted file mode 100644 index 4c22132..0000000 --- a/EasyTool.ImageTests/EasyTool.ImageTests.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs b/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs deleted file mode 100644 index 76b1ec2..0000000 --- a/EasyTool.ImageTests/ImageCategory/ImageUtilTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Drawing; - -namespace EasyTool.CoreTests.ImageCategory -{ - [TestClass] - public class ImageUtilTests - { - /// - /// 图像分割方法测试 - /// 测试用到的ori和mask在测试类旁边的Reources中 - /// - [TestMethod] - public void MaskImageTest() - { - Image ori = new Bitmap(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory","Resources","ori.jpg")); - Image mask = new Bitmap(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory", "Resources", "mask.jpg")); - - Console.WriteLine($"ori-width:{ori.Width} | ori-height:{ori.Height}"); - Console.WriteLine($"mask-width:{mask.Width} | mask-height:{mask.Height}"); - - Image result = ImgUtil.MaskImage(mask, ori); - - Console.WriteLine($"result-width:{mask.Width} | result-height:{mask.Height}"); - result.Save(Path.Combine(Environment.CurrentDirectory.Split("bin")[0], "ImageCategory", "Resources", "result.jpg")); - } - - [TestMethod] - public void ResizeImageTest() - { - Console.WriteLine("Hello World"); - } - - [TestMethod] - public void CropImageTest() - { - Assert.Fail(); - } - - [TestMethod] - public void ConvertImageFormatTest() - { - Assert.Fail(); - } - - [TestMethod] - public void ConvertToBlackAndWhiteTest() - { - Assert.Fail(); - } - - [TestMethod] - public void AddTextWatermarkTest() - { - Assert.Fail(); - } - } -} diff --git a/EasyTool.ImageTests/ImageCategory/Resources/mask.jpg b/EasyTool.ImageTests/ImageCategory/Resources/mask.jpg deleted file mode 100644 index 655962a..0000000 Binary files a/EasyTool.ImageTests/ImageCategory/Resources/mask.jpg and /dev/null differ diff --git a/EasyTool.ImageTests/ImageCategory/Resources/ori.jpg b/EasyTool.ImageTests/ImageCategory/Resources/ori.jpg deleted file mode 100644 index 9d8282f..0000000 Binary files a/EasyTool.ImageTests/ImageCategory/Resources/ori.jpg and /dev/null differ diff --git a/EasyTool.ImageTests/ImageCategory/Resources/result.jpg b/EasyTool.ImageTests/ImageCategory/Resources/result.jpg deleted file mode 100644 index cb6bf3b..0000000 Binary files a/EasyTool.ImageTests/ImageCategory/Resources/result.jpg and /dev/null differ diff --git a/EasyTool.NPOI/EasyTool.NPOI.csproj b/EasyTool.NPOI/EasyTool.NPOI.csproj index 2b8deec..51f725b 100644 --- a/EasyTool.NPOI/EasyTool.NPOI.csproj +++ b/EasyTool.NPOI/EasyTool.NPOI.csproj @@ -1,30 +1,45 @@ - + - - netstandard2.1;.net6.0 - enable - 11 - - 依赖于NPOI 2.6.2 - 支持通过文件地址或者流的方式读取Excel文件获取工作簿对象IWorkbook, - 通过IWorkbook工作簿对象可以转化成Dataset对象 - 通过ISheet工作表对象可以转化成DataTable对象和List对象 - 以下是一些示例: - 获取数据集对象: - var dateSet = NPOIUtil.OpenWorkbook(path).ConvertToDataSet(); - var dateSet = NPOIUtil.ConvertToDataSet(path); - 获取工作表对象: - var sheet = NPOIUtil.OpenWorkbook(path).GetSheetAt(0); - 获取单表数据对象: - var dataTable = NPOIUtil.OpenWorkbook(path).GetSheetAt(0).ConvertToDataTable(); - List《T》 dataList = NPOIUtil.OpenWorkbook(path).GetSheetAt(0).ConvertToList《List《T》》(); - 从流读取工作簿对象(从流读取需要指定文件类型,缺省值为XLSX): - var workbook = NPOIUtil.OpenWorkbook(stream,ExcelWorkbookType.XLS); - - + + netstandard2.1 + annotations + latest + $(MSBuildProjectName.Replace(" ", "_").Replace(".NPOI", "")) - - - + Joce.EasyTool.NPOI + 一个大西瓜,TimChen + 2026.0108.1 + + 依赖于NPOI 2.7.5 + 支持通过文件地址或者流的方式读取Excel文件获取工作簿对象IWorkbook, + 通过IWorkbook工作簿对象可以转化成Dataset对象 + 通过ISheet工作表对象可以转化成DataTable对象和List对象 + + Tool Power NPOI Excel + https://github.com/dotnet-easy/easytool + https://easy-dotnet.com + README.md + LICENSE + logo.png + + + + + True + \ + + + True + \ + + + True + \ + + + + + + diff --git a/EasyTool.NPOI/ExcelWorkbookType.cs b/EasyTool.NPOI/OfficeCategory/ExcelWorkbookType.cs similarity index 100% rename from EasyTool.NPOI/ExcelWorkbookType.cs rename to EasyTool.NPOI/OfficeCategory/ExcelWorkbookType.cs diff --git a/EasyTool.NPOI/NPOIUtil.cs b/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs similarity index 82% rename from EasyTool.NPOI/NPOIUtil.cs rename to EasyTool.NPOI/OfficeCategory/NPOIUtil.cs index bd8e567..321eba6 100644 --- a/EasyTool.NPOI/NPOIUtil.cs +++ b/EasyTool.NPOI/OfficeCategory/NPOIUtil.cs @@ -133,7 +133,54 @@ public static IWorkbook OpenWorkbookFromStream(Stream stream, ExcelWorkbookType var property = t.GetType().GetProperty(headerRow.GetCell(m).StringCellValue); if (property == null) continue; - property.SetValue(t, value, null); + var targetType = property.PropertyType; + var underlyingType = Nullable.GetUnderlyingType(targetType) ?? targetType; + object? convertedValue = null; + if (underlyingType == typeof(string)) + { + convertedValue = value; + } + else if (string.IsNullOrWhiteSpace(value)) + { + convertedValue = null; + } + else if (underlyingType == typeof(int)) + { + convertedValue = int.Parse(value); + } + else if (underlyingType == typeof(long)) + { + convertedValue = long.Parse(value); + } + else if (underlyingType == typeof(double)) + { + convertedValue = double.Parse(value); + } + else if (underlyingType == typeof(decimal)) + { + convertedValue = decimal.Parse(value); + } + else if (underlyingType == typeof(float)) + { + convertedValue = float.Parse(value); + } + else if (underlyingType == typeof(bool)) + { + convertedValue = bool.Parse(value); + } + else if (underlyingType == typeof(DateTime)) + { + convertedValue = DateTime.Parse(value); + } + else if (underlyingType == typeof(Guid)) + { + convertedValue = Guid.Parse(value); + } + else + { + convertedValue = Convert.ChangeType(value, underlyingType); + } + property.SetValue(t, convertedValue, null); } list.Add(t); } @@ -191,7 +238,7 @@ public static bool ExportToExcel(IEnumerable dataSource,string path, out s IWorkbook workbook = workbookType.Equals(ExcelWorkbookType.XLSX) ? new XSSFWorkbook() : new HSSFWorkbook(); T t = dataSource.FirstOrDefault(); filename ??= typeof(T).Name; - ISheet sheet = workbook.CreateSheet(filename); + ISheet sheet = workbook.CreateSheet(filename!); IRow headerRow = sheet.CreateRow(0); var props = typeof(T).GetProperties().Where(x => x.PropertyType.IsPublic); int count = props.Count(); //T类型公开属性的数量 @@ -208,12 +255,12 @@ public static bool ExportToExcel(IEnumerable dataSource,string path, out s row.CreateCell(j).SetCellValue(props.ElementAt(j).GetValue(dataSource.ElementAt(i)).ToString()); } } - string filePath = $"{path}{filename}"; + string filePath = $"{path}{filename!}"; string extension = workbookType.Equals(ExcelWorkbookType.XLSX) ? ".xlsx" : ".xls"; int num = 1; while (File.Exists(filePath + extension)) { - filePath = $"{path}{filename}({num})"; + filePath = $"{path}{filename!}({num})"; num++; } using var fs = new FileStream(filePath + extension, FileMode.Create, FileAccess.Write); @@ -261,12 +308,12 @@ public static bool ExportToExcel(this DataTable dataTable, string path, out stri row.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString()); } } - string filePath = $"{path}{filename}"; + string filePath = $"{path}{filename!}"; string extension = workbookType.Equals(ExcelWorkbookType.XLSX) ? ".xlsx" : ".xls"; int num = 1; while (File.Exists(filePath + extension)) { - filePath = $"{path}{filename}({num})"; + filePath = $"{path}{filename!}({num})"; num++; } using var fs = new FileStream(filePath + extension, FileMode.Create, FileAccess.Write); diff --git a/EasyTool.NPOITests/EasyTool.NPOITests.csproj b/EasyTool.NPOITests/EasyTool.NPOITests.csproj deleted file mode 100644 index 6a5a733..0000000 --- a/EasyTool.NPOITests/EasyTool.NPOITests.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.NPOITests/NPOIUtilTests.cs b/EasyTool.NPOITests/NPOIUtilTests.cs deleted file mode 100644 index f2ec96d..0000000 --- a/EasyTool.NPOITests/NPOIUtilTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.NPOI; - -using System; -using System.Collections.Generic; -using System.Text; -using NPOI.SS.UserModel; -using System.Data; -using System.Text.Json; - -namespace EasyTool.NPOITests; - -[TestClass()] -public class NPOIUtilTests -{ - string path = @"../TempClass.xlsx"; - List dataList = new List() - { - new TempClass() - { - Name = "张三", - Age = 1, - Birthday = DateTime.Now - }, - new TempClass() - { - Name = "李四", - Age = 11, - Birthday = DateTime.Now.AddDays(1) - }, - new TempClass() - { - Name = "王五", - Age = 23, - Birthday = DateTime.Now.AddMonths(1) - } - }; - DataTable dataTable; - public NPOIUtilTests() - { - dataTable = new DataTable(); - dataTable.Columns.AddRange(new DataColumn[] - { - new DataColumn("Name",typeof(string)), - new DataColumn("Age",typeof(int)), - new DataColumn("Birthday",typeof(DateTime)) - }); - foreach (var item in dataList) - { - DataRow dataRow = dataTable.NewRow(); - dataRow["Name"] = item.Name; - dataRow["Age"] = item.Age; - dataRow["Birthday"] = item.Birthday; - dataTable.Rows.Add(dataRow); - } - } - - [TestMethod] - public void Test_ExportToExcel() - { - bool res = NPOIUtil.ExportToExcel(dataList, "../", out string msg); - bool res2 = NPOIUtil.ExportToExcel(dataList, "../", out string msg2, ExcelWorkbookType.XLS); - Assert.IsTrue(res && res2); - } - [TestMethod] - public void Test_ExportToExcelFromDatatable() - { - bool res = NPOIUtil.ExportToExcel(dataTable, "../", out string msg); - bool res2 = NPOIUtil.ExportToExcel(dataTable, "../", out string msg2, ExcelWorkbookType.XLS); - Assert.IsTrue(res && res2); - - } - [TestMethod] - public void Test_OpenWorkbookFromPath() - { - var workbook = NPOIUtil.OpenWorkbook(path); - Console.WriteLine(workbook.GetSheetName(0)); - Assert.IsNotNull(workbook); - } - [TestMethod] - public void Test_OpenWorkbookFromStream() - { - using Stream stream = File.OpenRead(path); - var workbook = NPOIUtil.OpenWorkbookFromStream(stream, ExcelWorkbookType.XLSX); - Console.WriteLine(workbook.GetSheetName(0)); - Assert.IsNotNull(workbook); - } - [TestMethod] - public void Test_ConvertToDataSet() - { - var workbook = NPOIUtil.OpenWorkbook(path); - var dataSet = workbook.ConvertToDataSet(); - Console.WriteLine(dataSet?.DataSetName); - Assert.IsNotNull(dataSet); - } - [TestMethod] - public void Test_ConvertToDataTable() - { - var workbook = NPOIUtil.OpenWorkbook(path); - var dataTable = workbook.GetSheetAt(0).ConvertToDatatable(); - Console.WriteLine(dataTable.TableName); - Assert.IsNotNull(dataTable); - } - [TestMethod] - public void Test_ConvertToList() where T : new() - { - var workbook = NPOIUtil.OpenWorkbook(path); - List dataList = workbook.GetSheetAt(0).ConvertToList(); - Console.WriteLine(JsonSerializer.Serialize(dataList)); - Assert.IsNotNull(dataList); - } -} -class TempClass -{ - public string Name { get; set; } - public int Age { get; set; } - public DateTime Birthday { get; set; } -} \ No newline at end of file diff --git a/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs b/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs index f259e81..4b2d623 100644 --- a/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs +++ b/EasyTool.Web/DevelopmentCategory/BuildDtoToTS.cs @@ -152,14 +152,27 @@ public static List GetDtos(Assembly assembly) foreach (var propertyType in propertyTypes) { var property = new DtoProperty(propertyType.PropertyType, propertyType.Name); - property.Title = propertyType.GetCustomAttribute()?.DisplayName ?? ""; - property.IsInverseProperty = propertyType.GetCustomAttribute() != null; + // 优先使用 DisplayAttribute 或 DescriptionAttribute,然后是 DisplayNameAttribute + property.Title = propertyType.GetCustomAttribute()?.GetName() + ?? propertyType.GetCustomAttribute()?.Description + ?? propertyType.GetCustomAttribute()?.DisplayName + ?? ""; + + property.IsInverseProperty = propertyType.GetCustomAttribute() != null; property.IsKey = propertyType.GetCustomAttribute() != null; property.IsRequired = propertyType.GetCustomAttribute() != null; property.StringLength = propertyType.GetCustomAttribute()?.MaximumLength ?? 0; - dto.Propertys.Add(property); + // 支持更多 .NET 约定特性 + property.IsEditable = propertyType.GetCustomAttribute() == null; + property.DataType = GetDataType(propertyType.GetCustomAttribute()); + property.RegularExpression = propertyType.GetCustomAttribute()?.Pattern; + property.RangeMinimum = propertyType.GetCustomAttribute()?.Minimum as double?; + property.RangeMaximum = propertyType.GetCustomAttribute()?.Maximum as double?; + property.IsForeignKey = propertyType.GetCustomAttribute() != null; + + dto.Propertys.Add(property); } dtos.Add(dto); @@ -188,7 +201,9 @@ public DtoClass(string name, string _namespace) } - //TODO:需要改造成使用.net约定的属性 + /// + /// DTO 属性信息,支持标准 .NET DataAnnotations 特性 + /// public class DtoProperty { public DtoProperty(Type type, string name) @@ -196,18 +211,79 @@ public DtoProperty(Type type, string name) Type = type; Name = name; } - public Type Type { get; set; } - public string Name { get; set; } - - public string Title { get; set; }//字段名称 - public bool IsInverseProperty { get; set; }//是否关联属性 + /// + /// 属性类型 + /// + public Type Type { get; set; } - public bool IsRequired { get; set; }//是否必填 + /// + /// 属性名称 + /// + public string Name { get; set; } - public int StringLength { get; set; }//字符串长度 + /// + /// 显示名称(支持 DisplayAttribute、DescriptionAttribute、DisplayNameAttribute) + /// + public string Title { get; set; } + + /// + /// 是否关联属性(InversePropertyAttribute) + /// + public bool IsInverseProperty { get; set; } + + /// + /// 是否必填(RequiredAttribute) + /// + public bool IsRequired { get; set; } + + /// + /// 字符串长度(StringLengthAttribute) + /// + public int StringLength { get; set; } + + /// + /// 是否主键(KeyAttribute) + /// + public bool IsKey { get; set; } + + /// + /// 是否可编辑(EditableAttribute) + /// + public bool IsEditable { get; set; } = true; + + /// + /// 数据类型(DataTypeAttribute) + /// + public string DataType { get; set; } + + /// + /// 正则表达式验证(RegularExpressionAttribute) + /// + public string RegularExpression { get; set; } + + /// + /// 范围最小值(RangeAttribute) + /// + public double? RangeMinimum { get; set; } + + /// + /// 范围最大值(RangeAttribute) + /// + public double? RangeMaximum { get; set; } + + /// + /// 是否外键(ForeignKeyAttribute) + /// + public bool IsForeignKey { get; set; } + } - public bool IsKey { get; set; }//是否主键 + /// + /// 获取 DataTypeAttribute 的数据类型名称 + /// + private static string GetDataType(DataTypeAttribute attribute) + { + return attribute?.DataType.ToString() ?? string.Empty; } } diff --git a/EasyTool.Web/EasyTool.Web.csproj b/EasyTool.Web/EasyTool.Web.csproj index b8b10a5..8698771 100644 --- a/EasyTool.Web/EasyTool.Web.csproj +++ b/EasyTool.Web/EasyTool.Web.csproj @@ -1,9 +1,39 @@ - + - - net6.0;netcoreapp3.1 - enable - + + net8.0 + annotations + latest + $(MSBuildProjectName.Replace(" ", "_").Replace(".Web", "")) + + Joce.EasyTool.Web + 一个大西瓜,TimChen + 2026.0108.1 + + A open source C# tool to make .NET easy + + Tool Power Web + https://github.com/dotnet-easy/easytool + https://easy-dotnet.com + README.md + LICENSE + logo.png + + + + + True + \ + + + True + \ + + + True + \ + + diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs deleted file mode 100644 index b164b9b..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildDtoToTSTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildDtoToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildDtoToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("BuildDtoTest")); - } - } - - [DtoComments("C#编译TS示例Dto")] - public class BuildDtoTest - { - public Guid Id { get; set; } - public string Name { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs deleted file mode 100644 index 4c0c15e..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildOptionToTSTests.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.ComponentModel; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildOptionToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildOptionToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("BuildOptionTest")); - } - - - } - - [OptionComments("C#编译TS示例Option")] - public class BuildOptionTest - { - [DisplayName("调试")] - public static string Debug { get; set; } = nameof(Debug); - [DisplayName("消息")] - public static string Info { get; set; } = nameof(Info); - [DisplayName("警告")] - public static string Warning { get; set; } = nameof(Warning); - [DisplayName("错误")] - public static string Error { get; set; } = nameof(Error); - } -} \ No newline at end of file diff --git a/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs b/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs deleted file mode 100644 index 6abcc64..0000000 --- a/EasyTool.WebTests/DevelopmentCategory/BuildWebApiToTSTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using EasyTool.Web.Development; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace EasyTool.WebTests -{ - [TestClass()] - public class BuildWebApiToTSTests - { - [TestMethod()] - public void BuildTest() - { - var toDto = BuildWebApiToTS.Build(this.GetType().Assembly); - Assert.IsTrue(toDto.Contains("GetTest")); - Assert.IsTrue(toDto.Contains("PostTest")); - } - } - - [ApiController] - public class BuildTestController - { - [ApiComments("GetTest Api")] - [HttpGet] - public string GetTest() - { - return null; - } - - [ApiComments("PostTest Api")] - [HttpPost] - public string PostTest() - { - return null; - } - } - -} \ No newline at end of file diff --git a/EasyTool.WebTests/EasyTool.WebTests.csproj b/EasyTool.WebTests/EasyTool.WebTests.csproj deleted file mode 100644 index b1bd052..0000000 --- a/EasyTool.WebTests/EasyTool.WebTests.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - false - true - - - - - - - - - - - - - - diff --git a/EasyTool.sln b/EasyTool.sln index d51f761..0e80faf 100644 --- a/EasyTool.sln +++ b/EasyTool.sln @@ -1,27 +1,18 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.4.33103.184 +# Visual Studio Version 18 +VisualStudioVersion = 18.4.11605.240 stable MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Core", "EasyTool.Core\EasyTool.Core.csproj", "{ACA106C6-039B-425C-89F9-7FE9042DC3C3}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.CoreTests", "EasyTool.CoreTests\EasyTool.CoreTests.csproj", "{7A101110-5202-44E9-ABE2-8388AB573932}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.EmitMapper", "EasyTool.EmitMapper\EasyTool.EmitMapper.csproj", "{986FCBD3-2A69-4012-BE41-FB4FF2906A05}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.EmitMapperTests", "EasyTool.EmitMapperTests\EasyTool.EmitMapperTests.csproj", "{8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Web", "EasyTool.Web\EasyTool.Web.csproj", "{578D6FC8-C937-4FAE-B776-9E52043BA8E0}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.WebTests", "EasyTool.WebTests\EasyTool.WebTests.csproj", "{E033014B-67D0-4B42-8507-B90ACE0BD059}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.NPOI", "EasyTool.NPOI\EasyTool.NPOI.csproj", "{573938DD-661A-4074-8A62-4FC651E97E13}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.NPOITests", "EasyTool.NPOITests\EasyTool.NPOITests.csproj", "{7AC7EC2E-003E-49E7-8124-09B88C8F8A49}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.Image", "EasyTool.Image\EasyTool.Image.csproj", "{F7AEE692-A41F-4B64-A659-B3F92EA03429}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyTool.ImageTests", "EasyTool.ImageTests\EasyTool.ImageTests.csproj", "{09E30ABC-1F36-4D65-8416-AF7C5C75DA65}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyTool.CoreTests", "EasyTool.CoreTests\EasyTool.CoreTests.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,42 +24,26 @@ Global {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {ACA106C6-039B-425C-89F9-7FE9042DC3C3}.Release|Any CPU.Build.0 = Release|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7A101110-5202-44E9-ABE2-8388AB573932}.Release|Any CPU.Build.0 = Release|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Debug|Any CPU.Build.0 = Debug|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Release|Any CPU.ActiveCfg = Release|Any CPU {986FCBD3-2A69-4012-BE41-FB4FF2906A05}.Release|Any CPU.Build.0 = Release|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8355B216-ED13-4C7F-BFD5-CB7F4DAB9443}.Release|Any CPU.Build.0 = Release|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {578D6FC8-C937-4FAE-B776-9E52043BA8E0}.Release|Any CPU.Build.0 = Release|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E033014B-67D0-4B42-8507-B90ACE0BD059}.Release|Any CPU.Build.0 = Release|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Debug|Any CPU.Build.0 = Debug|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Release|Any CPU.ActiveCfg = Release|Any CPU {573938DD-661A-4074-8A62-4FC651E97E13}.Release|Any CPU.Build.0 = Release|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7AC7EC2E-003E-49E7-8124-09B88C8F8A49}.Release|Any CPU.Build.0 = Release|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Debug|Any CPU.Build.0 = Debug|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Release|Any CPU.ActiveCfg = Release|Any CPU {F7AEE692-A41F-4B64-A659-B3F92EA03429}.Release|Any CPU.Build.0 = Release|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Debug|Any CPU.Build.0 = Debug|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Release|Any CPU.ActiveCfg = Release|Any CPU - {09E30ABC-1F36-4D65-8416-AF7C5C75DA65}.Release|Any CPU.Build.0 = Release|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,4 +51,4 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {960F4C21-B8CA-430B-B315-E5661C1C44B6} EndGlobalSection -EndGlobal +EndGlobal \ No newline at end of file diff --git a/README.md b/README.md index d9e715d..cb10214 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,26 @@

EasyTool

-
- 一个开源的 .NET 工具库, 使得开发变得更加有效率 +
+
- -[![pull_request](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg)](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml) +[![pull_request](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg)](https://github.com/dotnet-easy/easytool/actions/workflows/pull_request.yml/badge.svg) [![](https://img.shields.io/nuget/v/EasyTool.Core.svg)](https://www.nuget.org/packages/EasyTool.Core)

- 中文 | English + 中文 | English

## 📚 简介 Easytool 是一个功能丰富且易用的 .NET 工具库,旨在帮助开发者快速、便捷地完成各类开发任务。 这些封装的工具涵盖了字符串、数字、集合、编码、日期、文件、IO、加密、JSON、HTTP客户端等一系列操作, 可以满足各种不同的开发需求。 + > [More information](https://easy-dotnet.com/pages/easytool/) -> + ## 🚀 快速开始 -### 安装 + +### 安装 + ~~~ PM> Install-Package EasyTool.Core ~~~ @@ -28,6 +30,7 @@ dotnet add package EasyTool.Core ~~~ ### 使用 + 复制文件或者目录 ~~~csharp FileUtil.Copy(sourceDir, destinationDir, isOverwrite) @@ -37,20 +40,176 @@ FileUtil.Copy(sourceDir, destinationDir, isOverwrite) var a = CloneUtil.Clone(person); ~~~ - ## 🛠️ 目录 + Easytool 封装了开发过程中一些常用的方法 -| Catalog | Introduce | -| --------------------------------------------------|---------------------------------------------------------------------------------- | -| [clone](EasyTool.Core/CloneCategory/) | 使用 CloneUtil.Clone 方法实现 .NET 对象的深度复制 | -| [code](EasyTool.Core/CodeCategory/) | 提供基于 base32, base62 等编解码工具 | +--- -## 代码共享 +## 📁 项目结构(最新更新:2025-02-13) + +EasyTool.Core 采用**模块化分类结构**,所有工具按功能领域清晰划分到 15 个分类目录中: + +### 📂 分类概览 + +| 分类 | 文件数 | 功能描述 | +|------|--------|----------| +| **BusinessCategory** | 1 | 业务数据处理(社会信用代码) | +| **CodeCategory** | 5 | 加密/编码工具(AES/DES/编码/哈希/十六进制) | +| **CollectionsCategory** | 7 | 集合扩展操作(数组/字典/链表/列表/队列/栈) | +| **ColorCategory** | 1 | 颜色处理扩展 | +| **ConvertCategory** | 1 | 数据类型转换工具 | +| **DateTimeCategory** | 4 | 日期时间处理(扩展/工具/日历/计时器) | +| **IdentifierCategory** | 1 | 标识符生成工具(UUID/ObjectId/Snowflake) | +| **IOCategory** | 7 | 文件/流/压缩操作(文件系统/文件类型/流/监控/ZIP) | +| **MathCategory** | 4 | 数学工具(计算/预测/随机数) | +| **NetCategory** | 3 | 网络工具(HTTP/IP/URL) | +| **ReflectCategory** | 3 | 反射/类型/属性扩展 | +| **Standardization** | 3 | 标准化类型(Option/QueryPage/Result) | +| **SystemCategory** | 2 | 系统环境工具(环境变量/系统信息) | +| **TextCategory** | 9 | 文本处理工具(正则/字符串/分割/XML/表情/脱敏) | +| **ToolCategory** | 8 | 通用扩展方法(委托/枚举/异常/GUID/对象/映射/任务/分页) | + +### 📋 各分类详细说明 + +#### **BusinessCategory** - 业务数据处理 +``` +CreditCodeUtil.cs - 中国社会信用代码的验证和处理工具 +``` + +#### **CodeCategory** - 加密/编码工具 +``` +AesUtil.cs - AES 加密/解密(支持 ECB/CBC 模式) +DesUtil.cs - DES 加密/解密(支持 ECB 模式) +EncodingUtil.cs - 编码转换工具 +HashUtil.cs - 17 种哈希算法(加法/旋转/Bernstein/FNV/DJB/BKDR 等) +HexUtil.cs - 十六进制转换工具 +``` +#### **CollectionsCategory** - 集合扩展操作 +``` +ArrayExtension.cs - 数组操作扩展 +DictionaryExtension.cs - 字典操作扩展 +IEnumerableExtensions.cs - IEnumerable 集合遍历扩展 +LinkedListUtil.cs - 链表操作工具 +ListExtension.cs - 列表操作扩展 +QueueUtil.cs - 队列操作工具 +StackUtil.cs - 栈操作工具 +``` -## 社区交流 +#### **ColorCategory** - 颜色处理 +``` +ColorExtension.cs - 颜色扩展(RGB/HSV/HEX 转换) +``` -**微信:ygdxg8657 (备注进群) QQ群:543829648 903210423(已满)** +#### **ConvertCategory** - 数据类型转换 +``` +ConvertExtension.cs - 通用数据类型转换(ToByte/ToShort/ToInt/ToLong/ToFloat/ToDouble/ToDecimal) +``` -![easy-tool](https://raw.githubusercontent.com/dotnet-easy/easy-dotnet/main/files/img/easytool.png) +#### **DateTimeCategory** - 日期时间处理 +``` +DateTimeExtension.cs - DateTime 类型扩展方法 +DateTimeUtil.cs - 日期时间工具类 +LunarCalendarUtil.cs - 农历工具 +TimerUtil.cs - 计时器工具 +``` + +#### **IdentifierCategory** - 标识符生成 +``` +IdUtil.cs - ID 生成工具(有序 UUID/ObjectId/Snowflake ID) +``` + +#### **IOCategory** - 文件/流/压缩 +``` +FileSystemExtension.cs - 文件系统操作扩展 +FileTypeExtension.cs - 文件类型判断 +FileUtil.cs - 文件操作工具 +StreamExtension.cs - 流操作扩展 +Tailer.cs - 文件尾部追踪工具 +WatchMonitor.cs - 文件监控工具 +ZipUtil.cs - ZIP 压缩工具 +``` + +#### **MathCategory** - 数学工具 +``` +MathUtil.cs - 数学计算工具 +NumberExtension.cs - 数字类型扩展(偶数/质数/二进制/十六进制) +PredictUtil.cs - 预测算法工具 +RandomUtil.cs - 随机数生成工具 +``` + +#### **NetCategory** - 网络工具 +``` +HttpClientExtension.cs - HttpClient 扩展 +IpUtil.cs - IP 地址处理工具 +URLUtil.cs - URL 处理工具 +``` + +#### **ReflectCategory** - 反射/类型/属性扩展 +``` +PropertyInfoExtension.cs - PropertyInfo 扩展(值获取/设置/特性检查) +ReflectUtil.cs - 反射工具类 +TypeExtension.cs - Type 类型扩展(类型判断/友好名称/默认值) +``` + +#### **Standardization** - 标准化类型 +``` +Option.cs - 选项对象(用于前端下拉) +QueryPage.cs - 分页查询对象 +Result.cs - 统一结果对象 +``` + +#### **SystemCategory** - 系统环境工具 +``` +EnvUtil.cs - 环境变量工具 +SystemUtil.cs - 系统信息工具 +``` + +#### **TextCategory** - 文本处理工具(9个文件) +``` +RegexUtil.cs - 正则表达式工具 +StringBuilderExtension.cs - StringBuilder 扩展 +StringComparisonExtension.cs - 字符串比较扩展 +StringExtension.cs - 字符串验证扩展(邮箱/手机/URL/身份证等) +StrSplitter.cs - 字符串分割工具 +StrUtil.cs - 字符串处理工具(命名转换/空格处理) +XmlUtil.cs - XML 处理工具 +EmojiUtil.cs - 表情符号处理工具 +DesensitizedUtil.cs - 数据脱敏工具(手机号/身份证/银行卡等) +``` + +#### **ToolCategory** - 通用扩展方法(8个文件) +``` +DelegateExtension.cs - 委托扩展(安全调用) +EnumExtension.cs - 枚举扩展(获取描述) +ExceptionExtension.cs - 异常扩展(获取完整异常信息) +GuidExtension.cs - Guid 扩展(空值判断) +ObjectExtension.cs - 对象扩展(深拷贝/JSON序列化) +SimpleMapExtension.cs - 简单对象映射扩展 +TaskExtension.cs - Task 异步扩展(Fire-and-Forget) +PageUtil.cs - 分页工具(支持多种数据源和排序方式) +``` + +--- + +### 📈 优化历程 + +本次更新主要完成了以下结构优化工作: + +1. ✅ **ReflectCategory 扩展** - PropertyInfoExtension、TypeExtension 移入 +2. ✅ **TextCategory 扩展** - StringComparisonExtension、StringExtension、EmojiUtil、DesensitizedUtil、StringBuilderExtension 移入 +3. ✅ **CollectionsCategory 扩展** - IEnumerableExtensions 合并 +4. ✅ **IdentifierCategory 新建** - ID 生成工具独立分类 +5. ✅ **BusinessCategory 新建** - 业务数据处理独立分类 +6. ✅ **ColorCategory 精简** - 颜色处理单独分类 +7. ✅ **ToolCategory 优化** - SimpleMapExtension、PageUtil 移入 +8. ✅ **空壳文件清理** - 删除仅含 Obsolete 方法的文件 + +**最终状态**:**15 个分类,55 个源文件**,结构清晰、功能明确、无重复代码。 + +--- + +> 项目采用模块化设计,每个分类职责单一,便于查找和维护。所有工具类都使用静态方法,无需实例化即可使用。 + +## 代码共享