build(init):项目加入版本库控制。
This commit is contained in:
		
							
								
								
									
										198
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										198
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,198 @@
 | 
				
			|||||||
 | 
					# Windows thumbnail cache files
 | 
				
			||||||
 | 
					Thumbs.db
 | 
				
			||||||
 | 
					Thumbs.db:encryptable
 | 
				
			||||||
 | 
					ehthumbs.db
 | 
				
			||||||
 | 
					ehthumbs_vista.db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Dump file
 | 
				
			||||||
 | 
					*.stackdump
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Folder config file
 | 
				
			||||||
 | 
					[Dd]esktop.ini
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Recycle Bin used on file shares
 | 
				
			||||||
 | 
					$RECYCLE.BIN/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Windows Installer files
 | 
				
			||||||
 | 
					*.cab
 | 
				
			||||||
 | 
					*.msi
 | 
				
			||||||
 | 
					*.msix
 | 
				
			||||||
 | 
					*.msm
 | 
				
			||||||
 | 
					*.msp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Windows shortcuts
 | 
				
			||||||
 | 
					*.lnk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*~
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# temporary files which can be created if a process still has a handle open of a deleted file
 | 
				
			||||||
 | 
					.fuse_hidden*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# KDE directory preferences
 | 
				
			||||||
 | 
					.directory
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Linux trash folder which might appear on any partition or disk
 | 
				
			||||||
 | 
					.Trash-*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# .nfs files are created when an open file is removed but is still being accessed
 | 
				
			||||||
 | 
					.nfs*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# General
 | 
				
			||||||
 | 
					.DS_Store
 | 
				
			||||||
 | 
					.AppleDouble
 | 
				
			||||||
 | 
					.LSOverride
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Icon must end with two \r
 | 
				
			||||||
 | 
					Icon
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Thumbnails
 | 
				
			||||||
 | 
					._*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Files that might appear in the root of a volume
 | 
				
			||||||
 | 
					.DocumentRevisions-V100
 | 
				
			||||||
 | 
					.fseventsd
 | 
				
			||||||
 | 
					.Spotlight-V100
 | 
				
			||||||
 | 
					.TemporaryItems
 | 
				
			||||||
 | 
					.Trashes
 | 
				
			||||||
 | 
					.VolumeIcon.icns
 | 
				
			||||||
 | 
					.com.apple.timemachine.donotpresent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Directories potentially created on remote AFP share
 | 
				
			||||||
 | 
					.AppleDB
 | 
				
			||||||
 | 
					.AppleDesktop
 | 
				
			||||||
 | 
					Network Trash Folder
 | 
				
			||||||
 | 
					Temporary Items
 | 
				
			||||||
 | 
					.apdisk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Swap
 | 
				
			||||||
 | 
					[._]*.s[a-v][a-z]
 | 
				
			||||||
 | 
					!*.svg # comment out if you don't need vector files
 | 
				
			||||||
 | 
					[._]*.sw[a-p]
 | 
				
			||||||
 | 
					[._]s[a-rt-v][a-z]
 | 
				
			||||||
 | 
					[._]ss[a-gi-z]
 | 
				
			||||||
 | 
					[._]sw[a-p]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Session
 | 
				
			||||||
 | 
					Session.vim
 | 
				
			||||||
 | 
					Sessionx.vim
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Temporary
 | 
				
			||||||
 | 
					.netrwhist
 | 
				
			||||||
 | 
					*~
 | 
				
			||||||
 | 
					# Auto-generated tag files
 | 
				
			||||||
 | 
					tags
 | 
				
			||||||
 | 
					# Persistent undo
 | 
				
			||||||
 | 
					[._]*.un~
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
 | 
				
			||||||
 | 
					# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# User-specific stuff
 | 
				
			||||||
 | 
					.idea/**/workspace.xml
 | 
				
			||||||
 | 
					.idea/**/tasks.xml
 | 
				
			||||||
 | 
					.idea/**/usage.statistics.xml
 | 
				
			||||||
 | 
					.idea/**/dictionaries
 | 
				
			||||||
 | 
					.idea/**/shelf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# AWS User-specific
 | 
				
			||||||
 | 
					.idea/**/aws.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Generated files
 | 
				
			||||||
 | 
					.idea/**/contentModel.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Sensitive or high-churn files
 | 
				
			||||||
 | 
					.idea/**/dataSources/
 | 
				
			||||||
 | 
					.idea/**/dataSources.ids
 | 
				
			||||||
 | 
					.idea/**/dataSources.local.xml
 | 
				
			||||||
 | 
					.idea/**/sqlDataSources.xml
 | 
				
			||||||
 | 
					.idea/**/dynamic.xml
 | 
				
			||||||
 | 
					.idea/**/uiDesigner.xml
 | 
				
			||||||
 | 
					.idea/**/dbnavigator.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Gradle
 | 
				
			||||||
 | 
					.idea/**/gradle.xml
 | 
				
			||||||
 | 
					.idea/**/libraries
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Gradle and Maven with auto-import
 | 
				
			||||||
 | 
					# When using Gradle or Maven with auto-import, you should exclude module files,
 | 
				
			||||||
 | 
					# since they will be recreated, and may cause churn.  Uncomment if using
 | 
				
			||||||
 | 
					# auto-import.
 | 
				
			||||||
 | 
					# .idea/artifacts
 | 
				
			||||||
 | 
					# .idea/compiler.xml
 | 
				
			||||||
 | 
					# .idea/jarRepositories.xml
 | 
				
			||||||
 | 
					# .idea/modules.xml
 | 
				
			||||||
 | 
					# .idea/*.iml
 | 
				
			||||||
 | 
					# .idea/modules
 | 
				
			||||||
 | 
					# *.iml
 | 
				
			||||||
 | 
					# *.ipr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# CMake
 | 
				
			||||||
 | 
					cmake-build-*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Mongo Explorer plugin
 | 
				
			||||||
 | 
					.idea/**/mongoSettings.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# File-based project format
 | 
				
			||||||
 | 
					*.iws
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# IntelliJ
 | 
				
			||||||
 | 
					out/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# mpeltonen/sbt-idea plugin
 | 
				
			||||||
 | 
					.idea_modules/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# JIRA plugin
 | 
				
			||||||
 | 
					atlassian-ide-plugin.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Cursive Clojure plugin
 | 
				
			||||||
 | 
					.idea/replstate.xml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# SonarLint plugin
 | 
				
			||||||
 | 
					.idea/sonarlint/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Crashlytics plugin (for Android Studio and IntelliJ)
 | 
				
			||||||
 | 
					com_crashlytics_export_strings.xml
 | 
				
			||||||
 | 
					crashlytics.properties
 | 
				
			||||||
 | 
					crashlytics-build.properties
 | 
				
			||||||
 | 
					fabric.properties
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Editor-based Rest Client
 | 
				
			||||||
 | 
					.idea/httpRequests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Android studio 3.1+ serialized cache file
 | 
				
			||||||
 | 
					.idea/caches/build_file_checksums.ser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.vscode/*
 | 
				
			||||||
 | 
					!.vscode/settings.json
 | 
				
			||||||
 | 
					!.vscode/tasks.json
 | 
				
			||||||
 | 
					!.vscode/launch.json
 | 
				
			||||||
 | 
					!.vscode/extensions.json
 | 
				
			||||||
 | 
					!.vscode/*.code-snippets
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Local History for Visual Studio Code
 | 
				
			||||||
 | 
					.history/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Built Visual Studio Code Extensions
 | 
				
			||||||
 | 
					*.vsix
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# If you prefer the allow list template instead of the deny list, see community template:
 | 
				
			||||||
 | 
					# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Binaries for programs and plugins
 | 
				
			||||||
 | 
					*.exe
 | 
				
			||||||
 | 
					*.exe~
 | 
				
			||||||
 | 
					*.dll
 | 
				
			||||||
 | 
					*.so
 | 
				
			||||||
 | 
					*.dylib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Test binary, built with `go test -c`
 | 
				
			||||||
 | 
					*.test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Output of the go coverage tool, specifically when used with LiteIDE
 | 
				
			||||||
 | 
					*.out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Dependency directories (remove the comment below to include it)
 | 
				
			||||||
 | 
					# vendor/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Go workspace file
 | 
				
			||||||
 | 
					go.work
 | 
				
			||||||
							
								
								
									
										113
									
								
								cmd/aes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								cmd/aes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/aes"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						aesKey     string
 | 
				
			||||||
 | 
						aesResult  types.ResultEncoding    = types.ResultInHex
 | 
				
			||||||
 | 
						aesPadding types.EncryptionPadding = types.PKCS7Padding
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var aesCommand = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "aes",
 | 
				
			||||||
 | 
						Short: "AES加密算法工具",
 | 
				
			||||||
 | 
						Long:  `使用AES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var aesEncryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encrypt",
 | 
				
			||||||
 | 
						Short: "使用AES加密算法加密",
 | 
				
			||||||
 | 
						Long:  "使用AES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   aesEncryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func aesEncryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch aesPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", aesPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encodedResult, err := aes.Encrypt([]byte(args[0]), []byte(aesKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("加密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch aesResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", base64.ToBase64(encodedResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var aesDecryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decrypt",
 | 
				
			||||||
 | 
						Short: "使用AES加密算法解密",
 | 
				
			||||||
 | 
						Long:  "使用AES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   aesDecryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func aesDecryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch aesPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", aesPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							data []byte
 | 
				
			||||||
 | 
							err  error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						switch aesResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							data, err = base64.FromBase64(args[0])
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							data, err = hex.FromHex(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败,提供的密文无法解析,%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decodeResult, err := aes.Decrypt(data, []byte(aesKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("解密结果:%s\n", decodeResult)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						aesCommand.PersistentFlags().StringVarP(&aesKey, "key", "k", "", "密钥")
 | 
				
			||||||
 | 
						aesCommand.PersistentFlags().VarP(&aesResult, "output", "o", "输入或输出使用Base64编码或者Hex编码,默认为Hex编码。")
 | 
				
			||||||
 | 
						aesCommand.PersistentFlags().VarP(&aesPadding, "padding", "p", "填充方式,可选值:pkcs7、zero、none,默认使用pkcs7。")
 | 
				
			||||||
 | 
						aesCommand.AddCommand(aesEncryptCmd, aesDecryptCmd)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(aesCommand)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										45
									
								
								cmd/base64.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								cmd/base64.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var base64Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "base64",
 | 
				
			||||||
 | 
						Short: "对给定内容进行base64编码或解码",
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var base64EncodeCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encode",
 | 
				
			||||||
 | 
						Short: "对给定内容进行base64编码",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   base64EncodeExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func base64EncodeExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						fmt.Printf("Base64编码结果:%s\n", base64.ToBase64([]byte(args[0])))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var base64DecodeCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decode",
 | 
				
			||||||
 | 
						Short: "对给定内容进行base64解码",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   base64DecodeExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func base64DecodeExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						decoded, err := base64.FromBase64(args[0])
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("Base64解码失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("Base64解码结果:%s\n", decoded)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						base64Cmd.AddCommand(base64EncodeCmd, base64DecodeCmd)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(base64Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										50
									
								
								cmd/crc16.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/crc16.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc16"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						crc16Algorithm types.CRC16Algorithm = types.CRC16_IBM
 | 
				
			||||||
 | 
						crc16File      bool
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var crc16Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "crc16",
 | 
				
			||||||
 | 
						Short: `CRC16校验`,
 | 
				
			||||||
 | 
						Long:  `对给定的内容进行CRC16校验计算。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   crc16Execute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func crc16Execute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							result []byte
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if crc16File {
 | 
				
			||||||
 | 
							result, err = crc16.SumFile(args[0], crc16Algorithm.IntoCRC16Mode())
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							result = crc16.CRC16([]byte(args[0]), crc16Algorithm.IntoCRC16Mode())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("计算CRC16校验值时发生错误:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var builder strings.Builder
 | 
				
			||||||
 | 
						for _, b := range result {
 | 
				
			||||||
 | 
							builder.WriteString(fmt.Sprintf(" %02X", b))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("CRC16校验值:%s\n", builder.String())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						crc16Cmd.PersistentFlags().VarP(&crc16Algorithm, "algorithm", "a", "CRC16算法,可选值有:ccitt, ccitt-false, scsi, ibm, mbus,缺省值为ibm。")
 | 
				
			||||||
 | 
						crc16Cmd.PersistentFlags().BoolVarP(&crc16File, "file", "f", false, "从文件中读取内容")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(crc16Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										50
									
								
								cmd/crc32.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/crc32.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc32"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						crc32Algorithm types.CRC32Algorithm = types.CRC32_IEEE
 | 
				
			||||||
 | 
						crc32File      bool
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var crc32Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "crc32",
 | 
				
			||||||
 | 
						Short: `CRC32校验`,
 | 
				
			||||||
 | 
						Long:  `对给定的内容进行CRC32校验计算。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   crc32Execute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func crc32Execute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							result []byte
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if crc32File {
 | 
				
			||||||
 | 
							result, err = crc32.SumFile(args[0], crc32Algorithm.IntoCRC32Mode())
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							result = crc32.CRC32([]byte(args[0]), crc32Algorithm.IntoCRC32Mode())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("计算CRC32校验值时发生错误:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var builder strings.Builder
 | 
				
			||||||
 | 
						for _, b := range result {
 | 
				
			||||||
 | 
							builder.WriteString(fmt.Sprintf(" %02X", b))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("CRC32校验值:%s\n", builder.String())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						crc32Cmd.PersistentFlags().VarP(&crc32Algorithm, "algorithm", "a", "CRC32算法,可选值有:ieee, castagnoli, koopman,缺省值为ieee。")
 | 
				
			||||||
 | 
						crc32Cmd.PersistentFlags().BoolVarP(&crc32File, "file", "f", false, "从文件中读取内容")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(crc32Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										50
									
								
								cmd/crc64.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/crc64.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc64"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						crc64Algorithm types.CRC64Algorithm = types.CRC64_ISO
 | 
				
			||||||
 | 
						crc64File      bool
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var crc64Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "crc64",
 | 
				
			||||||
 | 
						Short: `CRC64校验`,
 | 
				
			||||||
 | 
						Long:  `对给定的内容进行CRC64校验计算。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   crc64Execute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func crc64Execute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							result []byte
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if crc64File {
 | 
				
			||||||
 | 
							result, err = crc64.SumFile(args[0], crc64Algorithm.IntoCRC64Mode())
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							result = crc64.CRC64([]byte(args[0]), crc64Algorithm.IntoCRC64Mode())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("计算CRC64校验值时发生错误:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var builder strings.Builder
 | 
				
			||||||
 | 
						for _, b := range result {
 | 
				
			||||||
 | 
							builder.WriteString(fmt.Sprintf(" %02X", b))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("CRC64校验值:%s\n", builder.String())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						crc64Cmd.PersistentFlags().VarP(&crc64Algorithm, "algorithm", "a", "CRC64算法,可选值有:iso, ecma,缺省值为iso。")
 | 
				
			||||||
 | 
						crc64Cmd.PersistentFlags().BoolVarP(&crc64File, "file", "f", false, "从文件中读取内容")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(crc64Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										50
									
								
								cmd/crc8.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cmd/crc8.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc8"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						crc8Algorithm types.CRC8Algorithm
 | 
				
			||||||
 | 
						crc8File      bool
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var crc8Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "crc8",
 | 
				
			||||||
 | 
						Short: `CRC8校验`,
 | 
				
			||||||
 | 
						Long:  `对给定的内容进行CRC8校验计算。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   crc8Execute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func crc8Execute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							result []byte
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if crc8File {
 | 
				
			||||||
 | 
							result, err = crc8.SumFile(args[0], crc8Algorithm.IntoCRC8Mode())
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							result = crc8.CRC8([]byte(args[0]), crc8Algorithm.IntoCRC8Mode())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("计算CRC8校验值时发生错误:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var builder strings.Builder
 | 
				
			||||||
 | 
						for _, b := range result {
 | 
				
			||||||
 | 
							builder.WriteString(fmt.Sprintf(" %02X", b))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("CRC8校验值:%s\n", builder.String())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						crc8Cmd.PersistentFlags().VarP(&crc8Algorithm, "algorithm", "a", "CRC8算法,可选值有:crc8, cdma2000, darc, dvb-s2, ebu, i-code, itu, maxim, rohc, wcdma")
 | 
				
			||||||
 | 
						crc8Cmd.PersistentFlags().BoolVarP(&crc8File, "file", "f", false, "从文件中读取内容")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(crc8Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										113
									
								
								cmd/des.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								cmd/des.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/des"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						desKey     string
 | 
				
			||||||
 | 
						desResult  types.ResultEncoding    = types.ResultInHex
 | 
				
			||||||
 | 
						desPadding types.EncryptionPadding = types.PKCS7Padding
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var desCommand = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "des",
 | 
				
			||||||
 | 
						Short: "DES加密算法工具",
 | 
				
			||||||
 | 
						Long:  `使用DES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var desEncryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encrypt",
 | 
				
			||||||
 | 
						Short: "使用DES加密算法加密",
 | 
				
			||||||
 | 
						Long:  `使用DES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   desEncryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var desDecryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decrypt",
 | 
				
			||||||
 | 
						Short: "使用DES加密算法解密",
 | 
				
			||||||
 | 
						Long:  `使用DES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   desDecryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func desEncryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch desPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", desPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encodedResult, err := des.Encrypt([]byte(args[0]), []byte(desKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("加密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch desResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", base64.ToBase64URLEncoded(encodedResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func desDecryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch desPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", desPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							data []byte
 | 
				
			||||||
 | 
							err  error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						switch desResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							data, err = base64.FromBase64(args[0])
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							data, err = hex.FromHex(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败,提供的密文无法解析,%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decodedResult, err := des.Decrypt(data, []byte(desKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败,%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("解密结果:%s\n", decodedResult)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						desCommand.PersistentFlags().VarP(&desResult, "output", "o", "密文的编码方式,可选Base64或者Hex,同时对加密和解密过程有效,默认使用Hex编码。")
 | 
				
			||||||
 | 
						desCommand.PersistentFlags().StringVarP(&desKey, "key", "k", "", "密钥。")
 | 
				
			||||||
 | 
						desCommand.PersistentFlags().VarP(&desPadding, "padding", "p", "填充方式,支持pkcs7、zero和none,默认为pkcs7。")
 | 
				
			||||||
 | 
						desCommand.AddCommand(desEncryptCmd, desDecryptCmd)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(desCommand)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										39
									
								
								cmd/hail.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								cmd/hail.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serial_code/hail"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						hailHostSerial int64
 | 
				
			||||||
 | 
						hailPrefix     string
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var hailCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "hail",
 | 
				
			||||||
 | 
						Short: "使用冰雹ID生成算法生成一个唯一ID",
 | 
				
			||||||
 | 
						Run:   hailGenerateExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func hailGenerateExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						hail.Initialize(hailHostSerial)
 | 
				
			||||||
 | 
						engine, err := hail.Get()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("未能初始化冰雹ID生成器: %s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(hailPrefix) > 0 {
 | 
				
			||||||
 | 
							fmt.Printf("生成的ID:%s\n", engine.GeneratePrefixedString(hailPrefix))
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Printf("生成的ID:%s\n", engine.GenerateString())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						hailCmd.Flags().Int64VarP(&hailHostSerial, "host-serial", "s", 1, "设定冰雹ID生成器的主机编号,默认为1。")
 | 
				
			||||||
 | 
						hailCmd.Flags().StringVarP(&hailPrefix, "prefix", "p", "", "设定生成的ID的前缀内容,默认为空。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(hailCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										54
									
								
								cmd/md5.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								cmd/md5.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/md5"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						md5File   bool
 | 
				
			||||||
 | 
						md5Result types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var md5Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "md5",
 | 
				
			||||||
 | 
						Short: "使用md5算法计算文件或者字符串的哈希值。",
 | 
				
			||||||
 | 
						Long:  `使用md5算法计算文件或者字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   md5HashExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func md5HashExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							hashResult []byte
 | 
				
			||||||
 | 
							err        error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if md5File {
 | 
				
			||||||
 | 
							hashResult, err = md5.SumFile(args[0])
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							hashResult = md5.MD5([]byte(args[0]))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("散列计算失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch md5Result {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						md5Cmd.PersistentFlags().BoolVarP(&md5File, "file", "f", false, "使用文件模式。")
 | 
				
			||||||
 | 
						md5Cmd.PersistentFlags().VarP(&md5Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(md5Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										48
									
								
								cmd/phash.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								cmd/phash.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/phash"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						phashResult types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var phashCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "phash",
 | 
				
			||||||
 | 
						Short: "使用pHash算法计算指定图像文件的感知哈希值。",
 | 
				
			||||||
 | 
						Long:  `使用pHash算法计算指定图像文件的感知哈希值。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   phashExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func phashExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							hashResult []byte
 | 
				
			||||||
 | 
							err        error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						hashResult, err = phash.HashFile(args[0])
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("散列计算失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch phashResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("感知计算结果:%s\n", base64.ToBase64(hashResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("感知计算结果:%s\n", hex.ToHex(hashResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						phashCmd.Flags().VarP(&phashResult, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(phashCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										35
									
								
								cmd/rootCmd.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								cmd/rootCmd.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/cmd/rsa"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rootCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "agtools",
 | 
				
			||||||
 | 
						Short: "AGTools是一个基于ag_toolsbox开发的工具集合",
 | 
				
			||||||
 | 
						Long:  `AGTools是一个基于ag_toolsbox开发的工具集合,主要提供了常用加密、散列、时间生成、随机字符串生成等方法。`,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var versionCommand = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "version",
 | 
				
			||||||
 | 
						Short: "输出当前工具版本号",
 | 
				
			||||||
 | 
						Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
							fmt.Println("AGTools v0.0.1")
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rootCmd.AddCommand(versionCommand)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(rsa.GetRsaCmd())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Execute() {
 | 
				
			||||||
 | 
						if err := rootCmd.Execute(); err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法执行命令: %s\n", err)
 | 
				
			||||||
 | 
							os.Exit(1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										126
									
								
								cmd/rsa/decryption.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								cmd/rsa/decryption.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,126 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaDecryptionFromFile     bool
 | 
				
			||||||
 | 
						rsaDecryptionToFile       bool
 | 
				
			||||||
 | 
						rsaDecryptionFromEncoding types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
						rsaDecryptionInputFile    string
 | 
				
			||||||
 | 
						rsaDecryptionOutputFile   string
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rsaDecryptionCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decrypt",
 | 
				
			||||||
 | 
						Short: `使用RSA算法对给定的内容进行解密`,
 | 
				
			||||||
 | 
						Long:  `使用RSA算法对给定的内容进行解密,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行解密,否则从命令行读取内容进行解密。如果指定了输出文件,则将解密后的内容写入到输出文件中,否则将解密后的内容输出到命令行。`,
 | 
				
			||||||
 | 
						Args:  cobra.MaximumNArgs(1),
 | 
				
			||||||
 | 
						Run:   rsaDecryptionExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func rsaDecryptionExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							content []byte
 | 
				
			||||||
 | 
							cert    []byte
 | 
				
			||||||
 | 
							keys    *rsa.KeyPair
 | 
				
			||||||
 | 
							err     error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if len(privateKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(privateKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Println("必须提供RSA私钥文件才能够解密。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						keys, err = rsa.NewFromPEM(cert)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法解析RSA私钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if rsaDecryptionFromFile {
 | 
				
			||||||
 | 
							if len(rsaDecryptionInputFile) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输入文件名。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							file, err := os.Open(rsaDecryptionInputFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if len(args) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定待解密的内容。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							switch rsaDecryptionFromEncoding {
 | 
				
			||||||
 | 
							case types.ResultInHex:
 | 
				
			||||||
 | 
								content, err = hex.FromHex(args[0])
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Printf("无法解析十六进制编码:%s\n", err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							case types.ResultInBase64:
 | 
				
			||||||
 | 
								content, err = base64.FromBase64(args[0])
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Printf("无法解析Base64编码:%s\n", err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decrypted, err := keys.Decrypt(content)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法解密:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if rsaDecryptionToFile {
 | 
				
			||||||
 | 
							if len(rsaDecryptionOutputFile) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输出文件名。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							file, err := os.OpenFile(rsaDecryptionOutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开输出文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							defer file.Close()
 | 
				
			||||||
 | 
							_, err = file.Write(decrypted)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法写入输出文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Printf("解密结果:%s\n", decrypted)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionFromFile, "from-file", "f", false, "从文件中读取待解密的内容")
 | 
				
			||||||
 | 
						rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionToFile, "to-file", "t", false, "将解密后的内容写入到文件中")
 | 
				
			||||||
 | 
						rsaDecryptionCmd.Flags().VarP(&rsaDecryptionFromEncoding, "from-encoding", "e", "指定输出格式,可选值为hex、base64,默认值为hex。对文件输入格式不起作用。")
 | 
				
			||||||
 | 
						rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionInputFile, "input-file", "i", "", "指定输入文件名")
 | 
				
			||||||
 | 
						rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionOutputFile, "output-file", "o", "", "指定输出文件名")
 | 
				
			||||||
 | 
						rsaCmd.AddCommand(rsaDecryptionCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										127
									
								
								cmd/rsa/encryption.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										127
									
								
								cmd/rsa/encryption.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,127 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaEncryptFromFile   bool
 | 
				
			||||||
 | 
						rsaEncryptToFile     bool
 | 
				
			||||||
 | 
						rsaResultOutput      types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
						rsaEncryptInputFile  string
 | 
				
			||||||
 | 
						rsaEncryptOutputFile string
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rsaEncryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encrypt",
 | 
				
			||||||
 | 
						Short: `使用RSA算法对给定的内容进行加密`,
 | 
				
			||||||
 | 
						Long:  `使用RSA算法对给定的内容进行加密,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行加密,否则从命令行读取内容进行加密。如果指定了输出文件,则将加密后的内容写入到输出文件中,否则将加密后的内容输出到命令行。`,
 | 
				
			||||||
 | 
						Args:  cobra.MaximumNArgs(1),
 | 
				
			||||||
 | 
						Run:   rsaEncryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func rsaEncryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							content []byte
 | 
				
			||||||
 | 
							cert    []byte
 | 
				
			||||||
 | 
							keys    *rsa.KeyPair
 | 
				
			||||||
 | 
							err     error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if len(publicKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(publicKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if len(privateKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(privateKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Println("必须指定RSA公钥文件或RSA私钥文件。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						keys, err = rsa.NewFromPEM(cert)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法解析RSA密钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if rsaEncryptFromFile {
 | 
				
			||||||
 | 
							if len(rsaEncryptInputFile) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输入文件。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							file, err := os.Open(rsaEncryptInputFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if len(args) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定要加密的内容。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content = []byte(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encrypted, err := keys.Encrypt(content)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法加密内容:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(rsaEncryptOutputFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.OpenFile(rsaEncryptOutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法创建RSA加密文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							_, err = file.Write(encrypted)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法写入RSA加密文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							fmt.Printf("已将加密结果写入到文件:%s\n", rsaEncryptOutputFile)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							switch rsaResultOutput {
 | 
				
			||||||
 | 
							case types.ResultInBase64:
 | 
				
			||||||
 | 
								fmt.Printf("加密结果:%s\n", base64.ToBase64(encrypted))
 | 
				
			||||||
 | 
							case types.ResultInHex:
 | 
				
			||||||
 | 
								fallthrough
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								fmt.Printf("加密结果:%s\n", hex.ToHex(encrypted))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rsaEncryptCmd.PersistentFlags().BoolVarP(&rsaEncryptFromFile, "from-file", "f", false, "从输入文件中读取内容进行加密。")
 | 
				
			||||||
 | 
						rsaEncryptCmd.PersistentFlags().BoolVarP(&rsaEncryptToFile, "to-file", "t", false, "将加密后的内容写入到输出文件中。")
 | 
				
			||||||
 | 
						rsaEncryptCmd.PersistentFlags().StringVarP(&rsaEncryptInputFile, "input-file", "i", "", "输入文件。")
 | 
				
			||||||
 | 
						rsaEncryptCmd.PersistentFlags().StringVarP(&rsaEncryptOutputFile, "output-file", "o", "", "输出文件。")
 | 
				
			||||||
 | 
						rsaEncryptCmd.PersistentFlags().VarP(&rsaResultOutput, "output-encode", "e", "加密结果输出格式,可选值有:hex,base64,缺省值为hex。对输出到文件的方式无效。")
 | 
				
			||||||
 | 
						rsaCmd.AddCommand(rsaEncryptCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										71
									
								
								cmd/rsa/key.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								cmd/rsa/key.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaKeyOutputFile string
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var keyGenCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "keygen",
 | 
				
			||||||
 | 
						Short: "生成RSA密钥对",
 | 
				
			||||||
 | 
						Long:  `生成RSA密钥对,密钥对将会被保存到指定的文件中。`,
 | 
				
			||||||
 | 
						Args:  cobra.NoArgs,
 | 
				
			||||||
 | 
						Run:   keyGenExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func keyGenExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						if len(rsaKeyOutputFile) == 0 {
 | 
				
			||||||
 | 
							fmt.Println("必须指定密钥输出文件名的前缀。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						keyPair, err := rsa.NewKeyPair(rsaKeyLength.IntoRSAKeyLength())
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法生成RSA密钥对:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						privateKeyBytes, err := rsa.EncodePrivateKey(keyPair.PrivateKey)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法编码RSA私钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						privateKeyFile, err := os.OpenFile(rsaKeyOutputFile+".pri.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法创建RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer privateKeyFile.Close()
 | 
				
			||||||
 | 
						_, err = privateKeyFile.Write(privateKeyBytes)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法写入RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						publicKeyBytes, err := rsa.EncodePublicKey(keyPair.PublicKey)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法编码RSA公钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						publicKeyFile, err := os.OpenFile(rsaKeyOutputFile+".pub.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法创建RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer publicKeyFile.Close()
 | 
				
			||||||
 | 
						_, err = publicKeyFile.Write(publicKeyBytes)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法写入RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("RSA密钥对已经保存到文件:%s.pri.pem, %s.pub.pem\n", rsaKeyOutputFile, rsaKeyOutputFile)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						keyGenCmd.Flags().StringVarP(&rsaKeyOutputFile, "output", "o", "", "用于保存密钥输出文件名的前缀。")
 | 
				
			||||||
 | 
						rsaCmd.AddCommand(keyGenCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								cmd/rsa/rsa.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								cmd/rsa/rsa.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaKeyLength   types.RSAKeyBits = types.RSA2048
 | 
				
			||||||
 | 
						publicKeyFile  string
 | 
				
			||||||
 | 
						privateKeyFile string
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rsaCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:              "rsa",
 | 
				
			||||||
 | 
						Short:            `RSA加密算法工具`,
 | 
				
			||||||
 | 
						Long:             `使用RSA算法对给定的内容进行加密或解密,生成RSA密钥。`,
 | 
				
			||||||
 | 
						TraverseChildren: true,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rsaCmd.PersistentFlags().VarP(&rsaKeyLength, "key-length", "l", "RSA密钥长度,可选值有:1024, 2048,缺省值为2048。")
 | 
				
			||||||
 | 
						rsaCmd.PersistentFlags().StringVarP(&publicKeyFile, "public-key", "u", "", "RSA公钥文件,用于加密。")
 | 
				
			||||||
 | 
						rsaCmd.PersistentFlags().StringVarP(&privateKeyFile, "private-key", "r", "", "RSA私钥文件,用于解密。")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetRsaCmd() *cobra.Command {
 | 
				
			||||||
 | 
						return rsaCmd
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										96
									
								
								cmd/rsa/sign.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								cmd/rsa/sign.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,96 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaSignFile      bool
 | 
				
			||||||
 | 
						rsaSignInputFile string
 | 
				
			||||||
 | 
						rsaSignEncoding  types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rsaSignCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "sign",
 | 
				
			||||||
 | 
						Short: `使用RSA算法对给定的内容进行签名`,
 | 
				
			||||||
 | 
						Long:  `使用RSA算法对给定的内容进行签名,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行签名,否则从命令行读取内容进行签名。`,
 | 
				
			||||||
 | 
						Args:  cobra.MaximumNArgs(1),
 | 
				
			||||||
 | 
						Run:   rsaSignExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func rsaSignExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							content []byte
 | 
				
			||||||
 | 
							cert    []byte
 | 
				
			||||||
 | 
							keys    *rsa.KeyPair
 | 
				
			||||||
 | 
							err     error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if len(privateKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(privateKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Println("必须提供RSA私钥文件才能够签名。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						keys, err = rsa.NewFromPEM(cert)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法解析RSA私钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if rsaSignFile {
 | 
				
			||||||
 | 
							if len(rsaSignInputFile) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输入文件名。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							file, err := os.Open(rsaSignInputFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if len(args) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输入内容。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content = []byte(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						signature, err := keys.Sign(content)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法签名:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch rsaSignEncoding {
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fmt.Printf("签名为:%s\n", hex.ToHex(signature))
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("签名为:%s\n", base64.ToBase64(signature))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rsaSignCmd.Flags().BoolVarP(&rsaSignFile, "file", "f", false, "指定对文件内容进行签名")
 | 
				
			||||||
 | 
						rsaSignCmd.Flags().StringVarP(&rsaSignInputFile, "input", "i", "", "指定读入内容的文件名")
 | 
				
			||||||
 | 
						rsaSignCmd.Flags().VarP(&rsaSignEncoding, "encoding", "e", "指定签名输出编码方式,可选值为hex或base64")
 | 
				
			||||||
 | 
						rsaCmd.AddCommand(rsaSignCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										119
									
								
								cmd/rsa/verify.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								cmd/rsa/verify.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,119 @@
 | 
				
			|||||||
 | 
					package rsa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						rsaVerifyFile      bool
 | 
				
			||||||
 | 
						rsaVerifyInputFile string
 | 
				
			||||||
 | 
						rsaVerifyEncoding  types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rsaVerifyCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "verify",
 | 
				
			||||||
 | 
						Short: `使用RSA算法对给定的内容进行验证`,
 | 
				
			||||||
 | 
						Long:  `使用RSA算法对给定的内容进行验证,生成RSA密钥。如果指定了输入文件,则从输入文件中读取内容进行验证,否则从命令行读取内容进行验证。`,
 | 
				
			||||||
 | 
						Args:  cobra.MaximumNArgs(2),
 | 
				
			||||||
 | 
						Run:   rsaVerifyExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func rsaVerifyExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							content []byte
 | 
				
			||||||
 | 
							cert    []byte
 | 
				
			||||||
 | 
							keys    *rsa.KeyPair
 | 
				
			||||||
 | 
							err     error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if len(publicKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(publicKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA公钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else if len(privateKeyFile) > 0 {
 | 
				
			||||||
 | 
							file, err := os.Open(privateKeyFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cert, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取RSA私钥文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Println("必须提供RSA密钥文件才能够验证。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						keys, err = rsa.NewFromPEM(cert)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("无法解析RSA密钥:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if rsaVerifyFile {
 | 
				
			||||||
 | 
							if len(rsaVerifyInputFile) == 0 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定输入文件名。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							file, err := os.Open(rsaVerifyInputFile)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法打开输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content, err = io.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法读取输入文件:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if len(args) < 1 {
 | 
				
			||||||
 | 
								fmt.Println("必须指定待验证内容。")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							content = []byte(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var signature []byte
 | 
				
			||||||
 | 
						switch rsaVerifyEncoding {
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							signature, err = hex.FromHex(args[len(args)-1])
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法解析签名内容:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							signature, err = base64.FromBase64(args[len(args)-1])
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fmt.Printf("无法解析签名内容:%s\n", err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Println("不支持的签名内容编码方式。")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err = keys.Verify(content, signature); err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("验证失败,%s\n", err)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							fmt.Println("验证成功。")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						rsaVerifyCmd.Flags().BoolVarP(&rsaVerifyFile, "file", "f", false, "指定从文件中读入待验证内容")
 | 
				
			||||||
 | 
						rsaVerifyCmd.Flags().StringVarP(&rsaVerifyInputFile, "input", "i", "", "指定读入内容的文件名")
 | 
				
			||||||
 | 
						rsaVerifyCmd.Flags().VarP(&rsaVerifyEncoding, "encoding", "e", "指定签名内容的编码方式,可选值为hex和base64,默认为hex")
 | 
				
			||||||
 | 
						rsaCmd.AddCommand(rsaVerifyCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										54
									
								
								cmd/sha1.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								cmd/sha1.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/sha1"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						sha1File   bool
 | 
				
			||||||
 | 
						sha1Result types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var sha1Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "sha1",
 | 
				
			||||||
 | 
						Short: "使用Sha1算法计算文件或者字符串的哈希值",
 | 
				
			||||||
 | 
						Long:  `使用Sha1算法计算文件或者字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   sha1HashExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func sha1HashExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							hashResult []byte
 | 
				
			||||||
 | 
							err        error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if sha1File {
 | 
				
			||||||
 | 
							hashResult, err = sha1.SumFile(args[0])
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							hashResult = sha1.Sha1([]byte(args[0]))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("散列计算失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch sha1Result {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						sha1Cmd.Flags().BoolVarP(&sha1File, "file", "f", false, "计算文件的Sha1哈希值。")
 | 
				
			||||||
 | 
						sha1Cmd.Flags().VarP(&sha1Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(sha1Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										54
									
								
								cmd/sha256.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								cmd/sha256.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/sha256"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						sha256File   bool
 | 
				
			||||||
 | 
						sha256Result types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var sha256Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "sha256",
 | 
				
			||||||
 | 
						Short: "使用Sha256算法计算文件或字符串的哈希值。",
 | 
				
			||||||
 | 
						Long:  `使用Sha256算法计算文件或字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   sha256HashExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func sha256HashExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							hashResult []byte
 | 
				
			||||||
 | 
							err        error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if sha256File {
 | 
				
			||||||
 | 
							hashResult, err = sha256.SumFile256(args[0])
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							hashResult = sha256.Sha256([]byte(args[0]))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("散列计算失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch sha256Result {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						sha256Cmd.Flags().BoolVarP(&sha256File, "file", "f", false, "计算文件的Sha256哈希值。")
 | 
				
			||||||
 | 
						sha256Cmd.Flags().VarP(&sha256Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(sha256Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										54
									
								
								cmd/sha512.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								cmd/sha512.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/sha512"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						sha512File   bool
 | 
				
			||||||
 | 
						sha512Result types.ResultEncoding = types.ResultInHex
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var sha512Cmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "sha512",
 | 
				
			||||||
 | 
						Short: "使用Sha512算法计算文件或字符串的哈希值。",
 | 
				
			||||||
 | 
						Long:  `使用Sha512算法计算文件或字符串的哈希值,支持文件和字符串两种模式。文件模式使用 -f 参数,不使用 -f 参数时将使用字符串模式。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   sha512HashExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func sha512HashExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							hashResult []byte
 | 
				
			||||||
 | 
							err        error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						if sha512File {
 | 
				
			||||||
 | 
							hashResult, err = sha512.SumFile512(args[0])
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							hashResult = sha512.Sha512([]byte(args[0]))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("散列计算失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch sha512Result {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", base64.ToBase64(hashResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("散列结果:%s\n", hex.ToHex(hashResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						sha512Cmd.Flags().BoolVarP(&sha512File, "file", "f", false, "计算文件的Sha512哈希值。")
 | 
				
			||||||
 | 
						sha512Cmd.Flags().VarP(&sha512Result, "output", "o", "计算结果编码输出的格式,可选Base64或者Hex编码,默认使用Hex编码。")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(sha512Cmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										51
									
								
								cmd/spiral.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								cmd/spiral.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/spiral"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var spiralCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "spiral",
 | 
				
			||||||
 | 
						Short: "螺旋随机密钥自解密算法工具",
 | 
				
			||||||
 | 
						Long:  `使用螺旋随机密钥自解密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var spiralEncodeCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encode",
 | 
				
			||||||
 | 
						Short: "使用螺旋随机密钥自解密算法加密",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   spiralEncodeExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var spiralDecodeCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decode",
 | 
				
			||||||
 | 
						Short: "使用螺旋随机密钥自解密算法解密",
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   spiralDecodeExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func spiralEncodeExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						encodedResult, err := spiral.Encrypt(args[0])
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("加密失败,%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("加密结果:%s\n", encodedResult)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func spiralDecodeExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						decodedResult, err := spiral.Decrypt(args[0])
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败,%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("解密结果:%s\n", decodedResult)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						spiralCmd.AddCommand(spiralEncodeCmd, spiralDecodeCmd)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(spiralCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										111
									
								
								cmd/tdes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								cmd/tdes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,111 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/tools/types"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/tdes"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/base64"
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/serialize/hex"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						tdesKey     string
 | 
				
			||||||
 | 
						tdesResult  types.ResultEncoding    = types.ResultInHex
 | 
				
			||||||
 | 
						tdesPadding types.EncryptionPadding = types.PKCS7Padding
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var tdesCommand = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "3des",
 | 
				
			||||||
 | 
						Short: "3DES加密算法工具",
 | 
				
			||||||
 | 
						Long:  `使用3DES加密算法加密或解密数据,在使用的时候注意明文和密文中如果包含空格或特殊字符,需要使用双引号包裹起来。`,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var tdesEncryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "encrypt",
 | 
				
			||||||
 | 
						Short: "使用3DES加密算法加密",
 | 
				
			||||||
 | 
						Long:  `使用3DES加密算法加密,支持三种填充方式:PKCS7、Zero和无填充。默认输出十六进制编码字符串。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   tdesEncryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func tdesEncryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch tdesPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", tdesPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						encodedResult, err := tdes.Encrypt([]byte(args[0]), []byte(tdesKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("加密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch tdesResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", base64.ToBase64(encodedResult))
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("加密结果:%s\n", hex.ToHex(encodedResult))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var tdesDecryptCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "decrypt",
 | 
				
			||||||
 | 
						Short: "使用3DES加密算法解密",
 | 
				
			||||||
 | 
						Long:  `使用3DES加密算法解密,支持三种填充方式:PKCS7、Zero和无填充。默认输入十六进制编码字符串。`,
 | 
				
			||||||
 | 
						Args:  cobra.MinimumNArgs(1),
 | 
				
			||||||
 | 
						Run:   tdesDecryptExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func tdesDecryptExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						var pad encryption.PaddingMode
 | 
				
			||||||
 | 
						switch tdesPadding {
 | 
				
			||||||
 | 
						case types.PKCS7Padding:
 | 
				
			||||||
 | 
							pad = encryption.PKCS7Padding
 | 
				
			||||||
 | 
						case types.ZeroPadding:
 | 
				
			||||||
 | 
							pad = encryption.ZeroPadding
 | 
				
			||||||
 | 
						case types.NoPadding:
 | 
				
			||||||
 | 
							pad = encryption.NoPadding
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							fmt.Printf("不支持的填充方式:%s\n", tdesPadding)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var decodedResult []byte
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
						switch tdesResult {
 | 
				
			||||||
 | 
						case types.ResultInBase64:
 | 
				
			||||||
 | 
							decodedResult, err = base64.FromBase64(args[0])
 | 
				
			||||||
 | 
						case types.ResultInHex:
 | 
				
			||||||
 | 
							fallthrough
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							decodedResult, err = hex.FromHex(args[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						decodedResult, err = tdes.Decrypt(decodedResult, []byte(tdesKey), pad)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("解密失败:%s\n", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Printf("解密结果:%s\n", decodedResult)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						tdesCommand.PersistentFlags().StringVarP(&tdesKey, "key", "k", "", "加密密钥。")
 | 
				
			||||||
 | 
						tdesCommand.PersistentFlags().VarP(&tdesResult, "output", "o", "输入或输出使用编码格式,可选Base64或Hex,默认为Hex。")
 | 
				
			||||||
 | 
						tdesCommand.PersistentFlags().VarP(&tdesPadding, "padding", "p", "填充方式,支持pkcs7、zero和none,默认为pkcs7。")
 | 
				
			||||||
 | 
						tdesCommand.AddCommand(tdesEncryptCmd, tdesDecryptCmd)
 | 
				
			||||||
 | 
						rootCmd.AddCommand(tdesCommand)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										28
									
								
								cmd/verify_code.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								cmd/verify_code.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						verifyCode "archgrid.xyz/ag/toolsbox/random/verify_code"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						verifyCodeLength int
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var verifyCodeCmd = &cobra.Command{
 | 
				
			||||||
 | 
						Use:   "verify_code",
 | 
				
			||||||
 | 
						Short: "生成指定长度的随机验证码",
 | 
				
			||||||
 | 
						Run:   verifyCodeExecute,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func verifyCodeExecute(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
						code := verifyCode.RandStr(verifyCodeLength)
 | 
				
			||||||
 | 
						fmt.Printf("生成的验证码为:%s\n", code)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						verifyCodeCmd.PersistentFlags().IntVarP(&verifyCodeLength, "length", "l", 10, "要生成的验证码长度,默认生成长度为10个字符的验证码")
 | 
				
			||||||
 | 
						rootCmd.AddCommand(verifyCodeCmd)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										25
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					module archgrid.xyz/ag/tools
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.20
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require github.com/spf13/cobra v1.7.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require (
 | 
				
			||||||
 | 
						github.com/azr/gift v1.1.2 // indirect
 | 
				
			||||||
 | 
						github.com/azr/phash v0.2.0 // indirect
 | 
				
			||||||
 | 
						github.com/disintegration/imaging v1.6.2 // indirect
 | 
				
			||||||
 | 
						github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6 // indirect
 | 
				
			||||||
 | 
						github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f // indirect
 | 
				
			||||||
 | 
						go.uber.org/atomic v1.7.0 // indirect
 | 
				
			||||||
 | 
						go.uber.org/multierr v1.6.0 // indirect
 | 
				
			||||||
 | 
						go.uber.org/zap v1.24.0 // indirect
 | 
				
			||||||
 | 
						golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require (
 | 
				
			||||||
 | 
						archgrid.xyz/ag/toolsbox v0.1.1
 | 
				
			||||||
 | 
						github.com/inconshreveable/mousetrap v1.1.0 // indirect
 | 
				
			||||||
 | 
						github.com/spf13/pflag v1.0.5 // indirect
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					replace archgrid.xyz/ag/toolsbox v0.1.1 => git.archgrid.xyz/xt/ag_toolsbox v0.1.1
 | 
				
			||||||
							
								
								
									
										41
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					git.archgrid.xyz/xt/ag_toolsbox v0.1.0 h1:8/yp9wHf631K9IY7o4RZAJGGlLCBMYQOG/LHYlqUlzI=
 | 
				
			||||||
 | 
					git.archgrid.xyz/xt/ag_toolsbox v0.1.0/go.mod h1:iaTTuz0BZi3b3+9+J9lGujfY42sUIEBPxnEes8lr6iA=
 | 
				
			||||||
 | 
					git.archgrid.xyz/xt/ag_toolsbox v0.1.1 h1:QJX+fEEx1+yQIQEbAaLQXuOWnZb8HK6Y4ReBAIIWCQs=
 | 
				
			||||||
 | 
					git.archgrid.xyz/xt/ag_toolsbox v0.1.1/go.mod h1:iaTTuz0BZi3b3+9+J9lGujfY42sUIEBPxnEes8lr6iA=
 | 
				
			||||||
 | 
					github.com/azr/gift v1.1.2 h1:EbQ8/1QMtDfz5Beqg+RY5F21KbwGhE8aWSEbF1pp95A=
 | 
				
			||||||
 | 
					github.com/azr/gift v1.1.2/go.mod h1:bDKvjyxgachY3zdk831G99y+VANype25eu37uhm3khI=
 | 
				
			||||||
 | 
					github.com/azr/phash v0.2.0 h1:F6qkeYlwuMUMkUAJkQFElGrQzFnneJwV+L23VrEQ0cU=
 | 
				
			||||||
 | 
					github.com/azr/phash v0.2.0/go.mod h1:vUennaUN3i09UA33YxHpCR5l2CeENoCRB2Jo6pvWNf4=
 | 
				
			||||||
 | 
					github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
 | 
				
			||||||
 | 
					github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
				
			||||||
 | 
					github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
				
			||||||
 | 
					github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
 | 
				
			||||||
 | 
					github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
 | 
				
			||||||
 | 
					github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6 h1:IIVxLyDUYErC950b8kecjoqDet8P5S4lcVRUOM6rdkU=
 | 
				
			||||||
 | 
					github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6/go.mod h1:JslaLRrzGsOKJgFEPBP65Whn+rdwDQSk0I0MCRFe2Zw=
 | 
				
			||||||
 | 
					github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 | 
				
			||||||
 | 
					github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
 | 
				
			||||||
 | 
					github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
				
			||||||
 | 
					github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
				
			||||||
 | 
					github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
 | 
				
			||||||
 | 
					github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8=
 | 
				
			||||||
 | 
					github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
 | 
				
			||||||
 | 
					github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
 | 
				
			||||||
 | 
					github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 | 
				
			||||||
 | 
					github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
				
			||||||
 | 
					github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
				
			||||||
 | 
					github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 | 
				
			||||||
 | 
					go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
 | 
				
			||||||
 | 
					go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
 | 
				
			||||||
 | 
					go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
 | 
				
			||||||
 | 
					go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
 | 
				
			||||||
 | 
					go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
 | 
				
			||||||
 | 
					go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
 | 
				
			||||||
 | 
					golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
 | 
				
			||||||
 | 
					golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 h1:/eM0PCrQI2xd471rI+snWuu251/+/jpBpZqir2mPdnU=
 | 
				
			||||||
 | 
					golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
 | 
				
			||||||
 | 
					golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
				
			||||||
 | 
					golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 | 
				
			||||||
 | 
					golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
 | 
					gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
							
								
								
									
										7
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "archgrid.xyz/ag/tools/cmd"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						cmd.Execute()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										52
									
								
								types/crc16.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								types/crc16.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc16"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CRC16Algorithm string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						CRC16_CCITT       CRC16Algorithm = "ccitt"
 | 
				
			||||||
 | 
						CRC16_CCITT_FALSE CRC16Algorithm = "ccitt-false"
 | 
				
			||||||
 | 
						CRC16_SCSI        CRC16Algorithm = "scsi"
 | 
				
			||||||
 | 
						CRC16_IBM         CRC16Algorithm = "ibm"
 | 
				
			||||||
 | 
						CRC16_MBUS        CRC16Algorithm = "mbus"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC16Algorithm) String() string {
 | 
				
			||||||
 | 
						return string(*a)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC16Algorithm) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "ccitt", "ccitt-false", "scsi", "ibm", "mbus":
 | 
				
			||||||
 | 
							*a = CRC16Algorithm(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的CRC16算法:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC16Algorithm) Type() string {
 | 
				
			||||||
 | 
						return "CRC16Algorithm"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a CRC16Algorithm) IntoCRC16Mode() crc16.CRC16Mode {
 | 
				
			||||||
 | 
						switch a {
 | 
				
			||||||
 | 
						case CRC16_CCITT:
 | 
				
			||||||
 | 
							return crc16.CCITT
 | 
				
			||||||
 | 
						case CRC16_CCITT_FALSE:
 | 
				
			||||||
 | 
							return crc16.CCITT_FALSE
 | 
				
			||||||
 | 
						case CRC16_SCSI:
 | 
				
			||||||
 | 
							return crc16.SCSI
 | 
				
			||||||
 | 
						case CRC16_IBM:
 | 
				
			||||||
 | 
							return crc16.IBM
 | 
				
			||||||
 | 
						case CRC16_MBUS:
 | 
				
			||||||
 | 
							return crc16.MBUS
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return crc16.IBM
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										46
									
								
								types/crc32.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								types/crc32.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc32"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CRC32Algorithm string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						CRC32_IEEE       CRC32Algorithm = "ieee"
 | 
				
			||||||
 | 
						CRC32_CASTAGNOLI CRC32Algorithm = "castagnoli"
 | 
				
			||||||
 | 
						CRC32_KOOPMAN    CRC32Algorithm = "koopman"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC32Algorithm) String() string {
 | 
				
			||||||
 | 
						return string(*a)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC32Algorithm) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "ieee", "castagnoli", "koopman":
 | 
				
			||||||
 | 
							*a = CRC32Algorithm(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的CRC32算法:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC32Algorithm) Type() string {
 | 
				
			||||||
 | 
						return "CRC32Algorithm"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a CRC32Algorithm) IntoCRC32Mode() crc32.CRC32Mode {
 | 
				
			||||||
 | 
						switch a {
 | 
				
			||||||
 | 
						case CRC32_IEEE:
 | 
				
			||||||
 | 
							return crc32.IEEE
 | 
				
			||||||
 | 
						case CRC32_CASTAGNOLI:
 | 
				
			||||||
 | 
							return crc32.Castagnoli
 | 
				
			||||||
 | 
						case CRC32_KOOPMAN:
 | 
				
			||||||
 | 
							return crc32.Koopman
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return crc32.IEEE
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										43
									
								
								types/crc64.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								types/crc64.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc64"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CRC64Algorithm string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						CRC64_ISO  CRC64Algorithm = "iso"
 | 
				
			||||||
 | 
						CRC64_ECMA CRC64Algorithm = "ecma"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC64Algorithm) String() string {
 | 
				
			||||||
 | 
						return string(*a)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC64Algorithm) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "iso", "ecma":
 | 
				
			||||||
 | 
							*a = CRC64Algorithm(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的CRC64算法:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC64Algorithm) Type() string {
 | 
				
			||||||
 | 
						return "CRC64Algorithm"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a CRC64Algorithm) IntoCRC64Mode() crc64.CRC64Mode {
 | 
				
			||||||
 | 
						switch a {
 | 
				
			||||||
 | 
						case CRC64_ISO:
 | 
				
			||||||
 | 
							return crc64.ISO
 | 
				
			||||||
 | 
						case CRC64_ECMA:
 | 
				
			||||||
 | 
							return crc64.ECMA
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return crc64.ISO
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										67
									
								
								types/crc8.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								types/crc8.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/hash/crc8"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CRC8Algorithm string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						CRC8_Origin   CRC8Algorithm = "crc8"
 | 
				
			||||||
 | 
						CRC8_CDMA2000 CRC8Algorithm = "cdma2000"
 | 
				
			||||||
 | 
						CRC8_DARC     CRC8Algorithm = "darc"
 | 
				
			||||||
 | 
						CRC8_DVB_S2   CRC8Algorithm = "dvb-s2"
 | 
				
			||||||
 | 
						CRC8_EBU      CRC8Algorithm = "ebu"
 | 
				
			||||||
 | 
						CRC8_I_CODE   CRC8Algorithm = "i-code"
 | 
				
			||||||
 | 
						CRC8_ITU      CRC8Algorithm = "itu"
 | 
				
			||||||
 | 
						CRC8_MAXIM    CRC8Algorithm = "maxim"
 | 
				
			||||||
 | 
						CRC8_ROHC     CRC8Algorithm = "rohc"
 | 
				
			||||||
 | 
						CRC8_WCDMA    CRC8Algorithm = "wcdma"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC8Algorithm) String() string {
 | 
				
			||||||
 | 
						return string(*a)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC8Algorithm) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "crc8", "cdma2000", "darc", "dvb-s2", "ebu", "i-code", "itu", "maxim", "rohc", "wcdma":
 | 
				
			||||||
 | 
							*a = CRC8Algorithm(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的CRC8算法:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a *CRC8Algorithm) Type() string {
 | 
				
			||||||
 | 
						return "CRC8Algorithm"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (a CRC8Algorithm) IntoCRC8Mode() crc8.CRC8Mode {
 | 
				
			||||||
 | 
						switch a {
 | 
				
			||||||
 | 
						case CRC8_Origin:
 | 
				
			||||||
 | 
							return crc8.ORIGIN
 | 
				
			||||||
 | 
						case CRC8_CDMA2000:
 | 
				
			||||||
 | 
							return crc8.CDMA2000
 | 
				
			||||||
 | 
						case CRC8_DARC:
 | 
				
			||||||
 | 
							return crc8.DARC
 | 
				
			||||||
 | 
						case CRC8_DVB_S2:
 | 
				
			||||||
 | 
							return crc8.DVB_S2
 | 
				
			||||||
 | 
						case CRC8_EBU:
 | 
				
			||||||
 | 
							return crc8.EBU
 | 
				
			||||||
 | 
						case CRC8_I_CODE:
 | 
				
			||||||
 | 
							return crc8.I_CODE
 | 
				
			||||||
 | 
						case CRC8_ITU:
 | 
				
			||||||
 | 
							return crc8.ITU
 | 
				
			||||||
 | 
						case CRC8_MAXIM:
 | 
				
			||||||
 | 
							return crc8.MAXIM
 | 
				
			||||||
 | 
						case CRC8_ROHC:
 | 
				
			||||||
 | 
							return crc8.ROHC
 | 
				
			||||||
 | 
						case CRC8_WCDMA:
 | 
				
			||||||
 | 
							return crc8.WCDMA
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return crc8.ORIGIN
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										28
									
								
								types/encoding.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								types/encoding.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ResultEncoding string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						ResultInBase64 ResultEncoding = "base64"
 | 
				
			||||||
 | 
						ResultInHex    ResultEncoding = "hex"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (re *ResultEncoding) String() string {
 | 
				
			||||||
 | 
						return string(*re)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (re *ResultEncoding) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "base64", "hex":
 | 
				
			||||||
 | 
							*re = ResultEncoding(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的编码方式:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (re *ResultEncoding) Type() string {
 | 
				
			||||||
 | 
						return "ResultEncoding"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										32
									
								
								types/encryption_padding.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								types/encryption_padding.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type EncryptionPadding string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						PKCS7Padding EncryptionPadding = "pkcs7"
 | 
				
			||||||
 | 
						ZeroPadding  EncryptionPadding = "zero"
 | 
				
			||||||
 | 
						NoPadding    EncryptionPadding = "no"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ep *EncryptionPadding) String() string {
 | 
				
			||||||
 | 
						return string(*ep)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ep *EncryptionPadding) Set(s string) error {
 | 
				
			||||||
 | 
						switch strings.ToLower(s) {
 | 
				
			||||||
 | 
						case "pkcs7", "zero", "no":
 | 
				
			||||||
 | 
							*ep = EncryptionPadding(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的填充方式:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ep *EncryptionPadding) Type() string {
 | 
				
			||||||
 | 
						return "EncryptionPadding"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										43
									
								
								types/rsa.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								types/rsa.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"archgrid.xyz/ag/toolsbox/encryption/rsa"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type RSAKeyBits string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						RSA1024 RSAKeyBits = "1024"
 | 
				
			||||||
 | 
						RSA2048 RSAKeyBits = "2048"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *RSAKeyBits) String() string {
 | 
				
			||||||
 | 
						return string(*r)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *RSAKeyBits) Set(s string) error {
 | 
				
			||||||
 | 
						switch s {
 | 
				
			||||||
 | 
						case "1024", "2048":
 | 
				
			||||||
 | 
							*r = RSAKeyBits(s)
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("不支持的RSA密钥长度:%s", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r *RSAKeyBits) Type() string {
 | 
				
			||||||
 | 
						return "RSAKeyBits"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (r RSAKeyBits) IntoRSAKeyLength() rsa.KeyLength {
 | 
				
			||||||
 | 
						switch r {
 | 
				
			||||||
 | 
						case RSA1024:
 | 
				
			||||||
 | 
							return rsa.RSA1024
 | 
				
			||||||
 | 
						case RSA2048:
 | 
				
			||||||
 | 
							return rsa.RSA2048
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return rsa.RSA2048
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user