feat(uuid):完成UUID和短UUID生成功能。
This commit is contained in:
		| @@ -34,8 +34,8 @@ Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能 | ||||
|   - [ ] BlockHash 散列算法 | ||||
| - 唯一序列号生成器 | ||||
|   - [ ] 冰雹 ID 生成器(短主机精简日期版雪花 ID) | ||||
|   - [ ] UUID 生成器 | ||||
|   - [ ] short UUID 生成器 | ||||
|   - [x] UUID 生成器 | ||||
|   - [x] short UUID 生成器 | ||||
| - 验证码生成器 | ||||
|   - [x] 随机验证码生成算法 | ||||
| - 序列化算法 | ||||
|   | ||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @@ -12,6 +12,7 @@ require ( | ||||
|  | ||||
| require ( | ||||
| 	github.com/azr/phash v0.2.0 | ||||
| 	github.com/google/uuid v1.3.0 | ||||
| 	github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6 | ||||
| 	github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f | ||||
| 	go.uber.org/atomic v1.7.0 // indirect | ||||
|   | ||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							| @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs | ||||
| 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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||||
| github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||||
| 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||||
|   | ||||
							
								
								
									
										65
									
								
								serial_code/uuid/short_uuid.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								serial_code/uuid/short_uuid.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| package uuid | ||||
|  | ||||
| import ( | ||||
| 	"unsafe" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	letters    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" | ||||
| 	MIN_LENGTH = 2 // 最小长度为2的原因是Go中整型长度最大为64位,而UUID v4是128位长,所以最小长度为2。 | ||||
| 	MAX_LENGTH = 128 | ||||
| ) | ||||
|  | ||||
| // 将一个字节拆分成8个比特位,并以一个8位长字节数组输出。 | ||||
| func breakByte(b byte) []byte { | ||||
| 	bytes := make([]byte, 8) | ||||
| 	for i := byte(0); i < 8; i++ { | ||||
| 		mask := byte(0x1) << (7 - i) | ||||
| 		bytes[i] = (b & mask) >> (7 - i) | ||||
| 	} | ||||
| 	return bytes | ||||
| } | ||||
|  | ||||
| // 根据UUID v4生成一个指定长度的短UUID字符串。 | ||||
| func ShortUUID(length uint) string { | ||||
| 	var uuidLength uint | ||||
| 	if length < MIN_LENGTH { | ||||
| 		uuidLength = MIN_LENGTH | ||||
| 	} else if length > MAX_LENGTH { | ||||
| 		uuidLength = MAX_LENGTH | ||||
| 	} else { | ||||
| 		uuidLength = length | ||||
| 	} | ||||
| 	// UUID是一个16字节长的字节数组,每个字节都可以拆分成8个比特位,共128比特位。 | ||||
| 	newUUID := uuid.New() | ||||
| 	uuidBits := make([]byte, 0) | ||||
| 	for _, b := range newUUID[:] { | ||||
| 		uuidBits = append(uuidBits, breakByte(b)...) | ||||
| 	} | ||||
| 	var partitionLength uint = 0 | ||||
| 	if 128%uuidLength == 0 { | ||||
| 		partitionLength = 128 / uuidLength | ||||
| 	} else { | ||||
| 		partitionLength = 128/uuidLength + 1 | ||||
| 	} | ||||
| 	var paritionedUUID = make([][]byte, uuidLength) | ||||
| 	for i := uint(0); i < uuidLength; i++ { | ||||
| 		if i != uuidLength-1 { | ||||
| 			paritionedUUID[i] = uuidBits[i*partitionLength : (i+1)*partitionLength] | ||||
| 		} else { | ||||
| 			paritionedUUID[i] = uuidBits[i*partitionLength:] | ||||
| 		} | ||||
| 	} | ||||
| 	result := make([]byte, uuidLength) | ||||
| 	for i := uint(0); i < uuidLength; i++ { | ||||
| 		var assembledRune uint64 = 0 | ||||
| 		for j := uint(0); j < uint(len(paritionedUUID[i])); j++ { | ||||
| 			assembledRune |= uint64(paritionedUUID[i][j]) << (partitionLength - j - 1) | ||||
| 		} | ||||
| 		result[i] = letters[assembledRune%uint64(len(letters))] | ||||
| 	} | ||||
|  | ||||
| 	return *(*string)(unsafe.Pointer(&result)) | ||||
| } | ||||
							
								
								
									
										19
									
								
								serial_code/uuid/uuid.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								serial_code/uuid/uuid.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| // 提供UUID v4生成功能以及基于UUID v4生成短UUID的功能。 | ||||
| package uuid | ||||
|  | ||||
| import ( | ||||
| 	"encoding/hex" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| ) | ||||
|  | ||||
| // 生成一个UUID v4字节数组。 | ||||
| func New() []byte { | ||||
| 	newID := uuid.New() | ||||
| 	return newID[:] | ||||
| } | ||||
|  | ||||
| // 生成一个UUID v4字符串。 | ||||
| func NewString() string { | ||||
| 	return hex.EncodeToString(New()) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user