-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
322 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package request | ||
|
||
type UserLogin struct { | ||
Username string `json:"username" form:"username" validate:"required,min=3,max=255"` | ||
Password string `json:"password" form:"password" validate:"required,min=6,max=255"` | ||
Username string `json:"username" form:"username" validate:"required"` | ||
Password string `json:"password" form:"password" validate:"required"` | ||
SafeLogin bool `json:"safe_login" form:"safe_login"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package rsacrypto | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha512" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/pem" | ||
"fmt" | ||
) | ||
|
||
const ( | ||
keySize = 2048 // RSA key size in bits | ||
) | ||
|
||
// GenerateKey 生成RSA密钥对 | ||
func GenerateKey() (*rsa.PrivateKey, error) { | ||
return rsa.GenerateKey(rand.Reader, keySize) | ||
} | ||
|
||
// EncryptData 加密数据 | ||
func EncryptData(publicKey *rsa.PublicKey, data []byte) (string, error) { | ||
ciphertext, err := rsa.EncryptOAEP( | ||
sha512.New(), | ||
rand.Reader, | ||
publicKey, | ||
data, | ||
nil, | ||
) | ||
if err != nil { | ||
return "", fmt.Errorf("encryption failed: %v", err) | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(ciphertext), nil | ||
} | ||
|
||
// DecryptData 解密数据 | ||
func DecryptData(privateKey *rsa.PrivateKey, ciphertext string) ([]byte, error) { | ||
data, err := base64.StdEncoding.DecodeString(ciphertext) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode base64: %v", err) | ||
} | ||
|
||
plaintext, err := rsa.DecryptOAEP( | ||
sha512.New(), | ||
rand.Reader, | ||
privateKey, | ||
data, | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("decryption failed: %v", err) | ||
} | ||
|
||
return plaintext, nil | ||
} | ||
|
||
// PrivateKeyToString 将RSA私钥转换为PEM格式的字符串 | ||
func PrivateKeyToString(privateKey *rsa.PrivateKey) (string, error) { | ||
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey) | ||
privateKeyPEM := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: privateKeyBytes, | ||
}, | ||
) | ||
return string(privateKeyPEM), nil | ||
} | ||
|
||
// PublicKeyToString 将RSA公钥转换为PEM格式的字符串 | ||
func PublicKeyToString(publicKey *rsa.PublicKey) (string, error) { | ||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal public key: %v", err) | ||
} | ||
|
||
publicKeyPEM := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "PUBLIC KEY", | ||
Bytes: publicKeyBytes, | ||
}, | ||
) | ||
return string(publicKeyPEM), nil | ||
} |
Oops, something went wrong.