diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d3fbf7..5774d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog: @go/crypto +## v1.5.1 (2026-06-08) +- **JS 对齐 & 缩写规范**: 将所有注册到 `jsmod` 的方法名统一为 PascalCase,并强制将缩写(如 MD5, SHA1, HMAC, RSA, AES, URL)全大写。 +- **源码纠偏**: 修正了 `hash.go` 中错误的缩写命名(`UrlBase64` -> `URLBase64`),并同步更新了 `encoding` v1.5.2 的 API 调用。 + ## [v1.3.2] - 2026-05-30 ### Added diff --git a/go.mod b/go.mod index 7a55abe..4767d47 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,10 @@ require ( apigo.cc/go/encoding v1.5.0 apigo.cc/go/jsmod v1.5.0 apigo.cc/go/safe v1.5.0 - golang.org/x/crypto v0.51.0 + golang.org/x/crypto v0.52.0 ) require ( apigo.cc/go/rand v1.5.0 // indirect - golang.org/x/sys v0.44.0 // indirect + golang.org/x/sys v0.45.0 // indirect ) diff --git a/go.sum b/go.sum index 23e6568..de7e21c 100644 --- a/go.sum +++ b/go.sum @@ -8,7 +8,5 @@ apigo.cc/go/rand v1.5.0 h1:1o8hh8fhdBuk1/h02IvugvamuT3dkWbVJrqEJVQKB2E= apigo.cc/go/rand v1.5.0/go.mod h1:Lh98S2dm9UY0X+M+kNQQEKyXHG5pcCKSFPyXN0QCGdk= apigo.cc/go/safe v1.5.0 h1:W1NblmcU8cex1f9Y5z8mNLUJOzZTE1s6fszb3FbhGnk= apigo.cc/go/safe v1.5.0/go.mod h1:OfQ5d6COePSGEuPvMeOk6KagX2sezw7nvKh7exj9SeM= -golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI= -golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8= -golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ= -golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988= +golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY= diff --git a/hash.go b/hash.go index a7c931e..f4b07ef 100644 --- a/hash.go +++ b/hash.go @@ -22,17 +22,17 @@ func MD5(data ...[]byte) []byte { // MD5ToHex 返回 MD5 的 Hex 字符串 func MD5ToHex(data []byte) string { - return encoding.HexToString(MD5(data)) + return encoding.Hex(MD5(data)) } // MD5ToBase64 返回 MD5 的 Base64 字符串 func MD5ToBase64(data []byte) string { - return encoding.Base64ToString(MD5(data)) + return encoding.Base64(MD5(data)) } -// MD5ToUrlBase64 返回 MD5 的 URL 安全 Base64 字符串 -func MD5ToUrlBase64(data []byte) string { - return encoding.UrlBase64ToString(MD5(data)) +// MD5ToURLBase64 返回 MD5 的 URL 安全 Base64 字符串 +func MD5ToURLBase64(data []byte) string { + return encoding.URLBase64(MD5(data)) } // Sha1 系列 @@ -44,9 +44,9 @@ func Sha1(data ...[]byte) []byte { return h.Sum(nil) } -func Sha1ToHex(data []byte) string { return encoding.HexToString(Sha1(data)) } -func Sha1ToBase64(data []byte) string { return encoding.Base64ToString(Sha1(data)) } -func Sha1ToUrlBase64(data []byte) string { return encoding.UrlBase64ToString(Sha1(data)) } +func Sha1ToHex(data []byte) string { return encoding.Hex(Sha1(data)) } +func Sha1ToBase64(data []byte) string { return encoding.Base64(Sha1(data)) } +func Sha1ToURLBase64(data []byte) string { return encoding.URLBase64(Sha1(data)) } // Sha256 系列 func Sha256(data ...[]byte) []byte { @@ -57,9 +57,9 @@ func Sha256(data ...[]byte) []byte { return h.Sum(nil) } -func Sha256ToHex(data []byte) string { return encoding.HexToString(Sha256(data)) } -func Sha256ToBase64(data []byte) string { return encoding.Base64ToString(Sha256(data)) } -func Sha256ToUrlBase64(data []byte) string { return encoding.UrlBase64ToString(Sha256(data)) } +func Sha256ToHex(data []byte) string { return encoding.Hex(Sha256(data)) } +func Sha256ToBase64(data []byte) string { return encoding.Base64(Sha256(data)) } +func Sha256ToURLBase64(data []byte) string { return encoding.URLBase64(Sha256(data)) } // Sha512 系列 func Sha512(data ...[]byte) []byte { @@ -70,12 +70,12 @@ func Sha512(data ...[]byte) []byte { return h.Sum(nil) } -func Sha512ToHex(data []byte) string { return encoding.HexToString(Sha512(data)) } -func Sha512ToBase64(data []byte) string { return encoding.Base64ToString(Sha512(data)) } -func Sha512ToUrlBase64(data []byte) string { return encoding.UrlBase64ToString(Sha512(data)) } +func Sha512ToHex(data []byte) string { return encoding.Hex(Sha512(data)) } +func Sha512ToBase64(data []byte) string { return encoding.Base64(Sha512(data)) } +func Sha512ToURLBase64(data []byte) string { return encoding.URLBase64(Sha512(data)) } // HMAC 系列 -func HmacMD5(key []byte, data ...[]byte) []byte { +func HMACMD5(key []byte, data ...[]byte) []byte { h := hmac.New(md5.New, key) for _, v := range data { h.Write(v) @@ -83,7 +83,7 @@ func HmacMD5(key []byte, data ...[]byte) []byte { return h.Sum(nil) } -func HmacSha1(key []byte, data ...[]byte) []byte { +func HMACSha1(key []byte, data ...[]byte) []byte { h := hmac.New(sha1.New, key) for _, v := range data { h.Write(v) @@ -91,7 +91,7 @@ func HmacSha1(key []byte, data ...[]byte) []byte { return h.Sum(nil) } -func HmacSha256(key []byte, data ...[]byte) []byte { +func HMACSha256(key []byte, data ...[]byte) []byte { h := hmac.New(sha256.New, key) for _, v := range data { h.Write(v) @@ -99,7 +99,7 @@ func HmacSha256(key []byte, data ...[]byte) []byte { return h.Sum(nil) } -func HmacSha512(key []byte, data ...[]byte) []byte { +func HMACSha512(key []byte, data ...[]byte) []byte { h := hmac.New(sha512.New, key) for _, v := range data { h.Write(v) diff --git a/hash_test.go b/hash_test.go index 1535544..6199f42 100644 --- a/hash_test.go +++ b/hash_test.go @@ -25,7 +25,7 @@ func TestHashCompatibility(t *testing.T) { key := []byte("key") mac := hmac.New(sha256.New, key) mac.Write(data) - if !bytes.Equal(crypto.HmacSha256(key, data), mac.Sum(nil)) { t.Error("HmacSha256 mismatch") } + if !bytes.Equal(crypto.HMACSha256(key, data), mac.Sum(nil)) { t.Error("HMACSha256 mismatch") } } func TestHashString(t *testing.T) { diff --git a/js_export.go b/js_export.go index c70c20a..3d8b6f0 100644 --- a/js_export.go +++ b/js_export.go @@ -7,41 +7,41 @@ import ( func init() { jsmod.Register("crypto", map[string]any{ // Hash - "md5": MD5, - "md5ToHex": MD5ToHex, - "md5ToBase64": MD5ToBase64, - "md5ToUrlBase64": MD5ToUrlBase64, - "sha1": Sha1, - "sha1ToHex": Sha1ToHex, - "sha1ToBase64": Sha1ToBase64, - "sha1ToUrlBase64": Sha1ToUrlBase64, - "sha256": Sha256, - "sha256ToHex": Sha256ToHex, - "sha256ToBase64": Sha256ToBase64, - "sha256ToUrlBase64": Sha256ToUrlBase64, - "sha512": Sha512, - "sha512ToHex": Sha512ToHex, - "sha512ToBase64": Sha512ToBase64, - "sha512ToUrlBase64": Sha512ToUrlBase64, + "MD5": MD5, + "MD5ToHex": MD5ToHex, + "MD5ToBase64": MD5ToBase64, + "MD5ToURLBase64": MD5ToURLBase64, + "Sha1": Sha1, + "Sha1ToHex": Sha1ToHex, + "Sha1ToBase64": Sha1ToBase64, + "Sha1ToURLBase64": Sha1ToURLBase64, + "Sha256": Sha256, + "Sha256ToHex": Sha256ToHex, + "Sha256ToBase64": Sha256ToBase64, + "Sha256ToURLBase64": Sha256ToURLBase64, + "Sha512": Sha512, + "Sha512ToHex": Sha512ToHex, + "Sha512ToBase64": Sha512ToBase64, + "Sha512ToURLBase64": Sha512ToURLBase64, // HMAC - "hmacMd5": HmacMD5, - "hmacSha1": HmacSha1, - "hmacSha256": HmacSha256, - "hmacSha512": HmacSha512, + "HMACMD5": HMACMD5, + "HMACSha1": HMACSha1, + "HMACSha256": HMACSha256, + "HMACSha512": HMACSha512, // Token - "makeToken": MakeToken, + "MakeToken": MakeToken, // AES GCM (Stateless) - "encrypt": func(data, key, iv []byte) ([]byte, error) { + "Encrypt": func(data, key, iv []byte) ([]byte, error) { s, err := NewAESGCMAndEraseKey(key, iv) if err != nil { return nil, err } return s.EncryptBytes(data) }, - "decrypt": func(data, key, iv []byte) ([]byte, error) { + "Decrypt": func(data, key, iv []byte) ([]byte, error) { s, err := NewAESGCMAndEraseKey(key, iv) if err != nil { return nil, err @@ -50,14 +50,14 @@ func init() { }, // AES CBC (Stateless) - "encryptCBC": func(data, key, iv []byte) ([]byte, error) { + "EncryptCBC": func(data, key, iv []byte) ([]byte, error) { s, err := NewAESCBCAndEraseKey(key, iv) if err != nil { return nil, err } return s.EncryptBytes(data) }, - "decryptCBC": func(data, key, iv []byte) ([]byte, error) { + "DecryptCBC": func(data, key, iv []byte) ([]byte, error) { s, err := NewAESCBCAndEraseKey(key, iv) if err != nil { return nil, err @@ -66,28 +66,28 @@ func init() { }, // RSA (Stateless) - "rsaEncrypt": func(data, pubKey []byte) ([]byte, error) { + "RSAEncrypt": func(data, pubKey []byte) ([]byte, error) { a, err := NewRSAAndEraseKey(nil, pubKey) if err != nil { return nil, err } return a.EncryptBytes(data) }, - "rsaDecrypt": func(data, privKey []byte) ([]byte, error) { + "RSADecrypt": func(data, privKey []byte) ([]byte, error) { a, err := NewRSAAndEraseKey(privKey, nil) if err != nil { return nil, err } return a.DecryptBytes(data) }, - "rsaSign": func(data, privKey []byte) ([]byte, error) { + "RSASign": func(data, privKey []byte) ([]byte, error) { a, err := NewRSAAndEraseKey(privKey, nil) if err != nil { return nil, err } return a.Sign(data) }, - "rsaVerify": func(data, sig, pubKey []byte) (bool, error) { + "RSAVerify": func(data, sig, pubKey []byte) (bool, error) { a, err := NewRSAAndEraseKey(nil, pubKey) if err != nil { return false, err @@ -96,10 +96,10 @@ func init() { }, // Key Generation - "generateRSAKeyPair": GenerateRSAKeyPair, - "generateECDSAKeyPair": GenerateECDSAKeyPair, - "generateEd25519KeyPair": GenerateEd25519KeyPair, - "generateX25519KeyPair": GenerateX25519KeyPair, - "deriveKey": DeriveKey, + "GenerateRSAKeyPair": GenerateRSAKeyPair, + "GenerateECDSAKeyPair": GenerateECDSAKeyPair, + "GenerateEd25519KeyPair": GenerateEd25519KeyPair, + "GenerateX25519KeyPair": GenerateX25519KeyPair, + "DeriveKey": DeriveKey, }) }