diff --git a/base64x/base64x.go b/base64x/base64x.go index a987af8..ee69793 100644 --- a/base64x/base64x.go +++ b/base64x/base64x.go @@ -3,11 +3,11 @@ package base64x import "encoding/base64" // 普通的 -func Base64StdEncode(s string) string { +func StdEncode(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } -func Base64StdDecode(sEnc string) (string, error) { +func StdDecode(sEnc string) (string, error) { sDec, err := base64.StdEncoding.DecodeString(sEnc) if err != nil { return "", err @@ -16,11 +16,11 @@ func Base64StdDecode(sEnc string) (string, error) { } // URL和文件名安全的 -func Base64UrlEncode(s string) string { +func UrlEncode(s string) string { return base64.URLEncoding.EncodeToString([]byte(s)) } -func Base64UrlDecode(sEnc string) (string, error) { +func UrlDecode(sEnc string) (string, error) { sDec, err := base64.URLEncoding.DecodeString(sEnc) if err != nil { return "", err @@ -29,11 +29,11 @@ func Base64UrlDecode(sEnc string) (string, error) { } // 无填充 -func Base64RawEncode(s string) string { +func RawEncode(s string) string { return base64.RawStdEncoding.EncodeToString([]byte(s)) } -func Base64RawDecode(sEnc string) (string, error) { +func RawDecode(sEnc string) (string, error) { sDec, err := base64.RawStdEncoding.DecodeString(sEnc) if err != nil { return "", err @@ -41,11 +41,12 @@ func Base64RawDecode(sEnc string) (string, error) { return string(sDec), nil } -func Base64RawUrlEncode(s string) string { +// URL和文件名安全的,无填充 +func RawUrlEncode(s string) string { return base64.RawURLEncoding.EncodeToString([]byte(s)) } -func Base64RawUrlDecode(s string) (string, error) { +func RawUrlDecode(s string) (string, error) { decoded, err := base64.RawURLEncoding.DecodeString(s) return string(decoded), err } diff --git a/base64x/base64x_test.go b/base64x/base64x_test.go new file mode 100644 index 0000000..a053880 --- /dev/null +++ b/base64x/base64x_test.go @@ -0,0 +1,43 @@ +package base64x_test + +import ( + "testing" + + "code.yun.ink/pkg/encryptx/base64x" +) + +func TestBase64xStd(t *testing.T) { + original := "Hello, World!" + encoded := base64x.StdEncode(original) + decoded, err := base64x.StdDecode(encoded) + if err != nil { + t.Error(err) + } + if original != decoded { + t.Errorf("expected %q, got %q", original, decoded) + } +} + +func TestBase64xUrl(t *testing.T) { + original := "Hello, World!" + encoded := base64x.UrlEncode(original) + decoded, err := base64x.UrlDecode(encoded) + if err != nil { + t.Error(err) + } + if original != decoded { + t.Errorf("expected %q, got %q", original, decoded) + } +} + +func TestBase64xRaw(t *testing.T) { + original := "Hello, World!" + encoded := base64x.RawEncode(original) + decoded, err := base64x.RawDecode(encoded) + if err != nil { + t.Error(err) + } + if original != decoded { + t.Errorf("expected %q, got %q", original, decoded) + } +} diff --git a/bcryptx/bcryptx.go b/bcryptx/bcryptx.go new file mode 100644 index 0000000..598bd6d --- /dev/null +++ b/bcryptx/bcryptx.go @@ -0,0 +1,19 @@ +package bcryptx + +import "golang.org/x/crypto/bcrypt" + +// HashPassword 使用 bcrypt 算法对密码进行哈希处理 +func HashPassword(password string) (string, error) { + // 第二个参数是 cost 值,代表计算强度 + bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + return "", err + } + return string(bytes), nil +} + +// CheckPasswordHash 检查密码是否与哈希值匹配 +func CheckPasswordHash(password, hash string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + return err == nil +} diff --git a/bcryptx/bcryptx_test.go b/bcryptx/bcryptx_test.go new file mode 100644 index 0000000..d101bbc --- /dev/null +++ b/bcryptx/bcryptx_test.go @@ -0,0 +1,20 @@ +package bcryptx_test + +import ( + "testing" + + "code.yun.ink/pkg/encryptx/bcryptx" +) + +func TestDecryptBcrypt(t *testing.T) { + str, err := bcryptx.HashPassword("password") + if err != nil { + t.Error(err) + } + t.Log(str) + if bcryptx.CheckPasswordHash("password", str) { + t.Log("ok") + } else { + t.Error("error") + } +} diff --git a/encryptx.go b/encryptx.go index 6bc33fb..d6f9940 100644 --- a/encryptx.go +++ b/encryptx.go @@ -200,7 +200,7 @@ func Encrypt(encryptType EncryptType, data string, key string, options map[strin // 编码 case BASE64: - return base64x.Base64StdEncode(data), nil + return base64x.StdEncode(data), nil default: return "", fmt.Errorf("不支持的加密类型: %d", encryptType) @@ -267,7 +267,7 @@ func Decrypt(encryptType EncryptType, data string, key string, options map[strin // 编码 case BASE64: - return base64x.Base64StdDecode(data) + return base64x.StdDecode(data) default: return "", fmt.Errorf("不支持的加密类型: %d", encryptType)