tutil is a compact Go utility package for sortable identifiers, hashing and HMAC helpers, interoperability-oriented AES/RSA/SM2/SM4 helpers, non-cryptographic random values, and MySQL DSN password escaping.
go get github.com/choveylee/tutilRequires Go 1.25 or later.
package main
import (
"fmt"
"github.com/choveylee/tutil"
)
func main() {
u := tutil.NewUid()
fmt.Println(u.String())
oid := tutil.NewOid()
fmt.Println(oid.Hex())
}Complete API reference: pkg.go.dev/github.com/choveylee/tutil.
- Identifiers -
Uid(ULID) andOid(12-byte BSON ObjectID), includingdatabase/sqlscanning,driver.Valuer, string parsing, and GORM schema type hints. - Hashing and HMAC - MD5, SHA-1, HMAC-MD5, HMAC-SHA1, and HMAC-SHA256 helpers. MD5 and SHA-1 are retained for compatibility scenarios.
- Symmetric cryptography - AES and SM4 ECB/CBC helpers with PKCS #7 or zero padding for interoperability with existing systems.
- Asymmetric cryptography - RSA PEM key generation, RSAES-OAEP encryption/decryption, and SHA-256 signing/verification, plus SM2 encryption/decryption via
gmsm. - Random values -
math/rand-based integers and strings intended only for non-security use. - MySQL DSN escaping -
MysqlDsnEncodeescapes the password portion of simpleuser:password@hostDSNs.
| Module | Role |
|---|---|
github.com/oklog/ulid/v2 |
ULID generation and parsing |
go.mongodb.org/mongo-driver/v2/bson |
ObjectID backing type |
github.com/tjfoc/gmsm |
SM2 / SM4 |
AesEncryptandAesDecryptoperate on exactly one AES block. Use the explicit ECB/CBC helpers for longer inputs.- AES-CBC uses the first key block as the IV when
ivis empty. This behavior is retained for legacy compatibility; new code should supply a fresh random IV. - Zero padding is lossy when plaintext can end with zero bytes.
RsaEncryptandRsaDecryptuse RSAES-OAEP with SHA-256.ResetRsaKeyTypeconfigures the process-wide default PEM encodings used byRSAKeyGeneratorandRSAKeyGeneratorTo. RSA parsing helpers detect key formats from the PEM block type.RSAKeyGeneratorToaccepts 1024-bit keys for legacy interoperability; new deployments should use at least 2048 bits.MysqlDsnEncodeis intentionally narrow in scope. Use a DSN parser for URL-style or driver-specific connection strings.
For new designs, prefer authenticated encryption such as AES-GCM over raw
ECB/CBC helpers. MD5, SHA-1, RSASSA-PKCS1-v1_5 signatures, 1024-bit RSA keys,
and math/rand helpers are retained for compatibility or non-security use
cases.
Issues and pull requests are welcome. Before submitting changes, run:
go test ./...
go vet ./...