Document legacy RSA encryption compatibility

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-07-13 08:03:23 +08:00
parent 94d48cf82a
commit 2c075cd7bc
+21 -43
View File
@@ -10,70 +10,46 @@ import (
"fmt" "fmt"
"honoka-chan/config" "honoka-chan/config"
"os" "os"
"path/filepath"
"runtime"
) )
func RSAGen(bits int) { func RSAGen(bits int) {
//get current path
_, currentpath, _, _ := runtime.Caller(0)
currentpath = filepath.Dir(currentpath)
//----------------------------------------------private key
// GenerateKey generates an RSA keypair of the given bit size using the
// random source random (for example, crypto/rand.Reader).
privateKey, err := rsa.GenerateKey(rand.Reader, bits) privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil { if err != nil {
panic(err) panic(err)
} }
//serialize privatekey to ASN.1 der by x509.MarshalPKCS8PrivateKey privateKeyDER, err := x509.MarshalPKCS8PrivateKey(privateKey)
x509privatekey, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil { if err != nil {
panic(err) panic(err)
} }
//encode x509 to pem and save to file privateKeyFile, err := os.Create(config.PrivateKeyPath)
//1. create privatefile
privatekeyfile, err := os.Create(config.PrivateKeyPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer privatekeyfile.Close() defer privateKeyFile.Close()
//2. new a pem block struct object if err := pem.Encode(privateKeyFile, &pem.Block{
privatekeyblock := pem.Block{ Type: "PRIVATE KEY",
Type: "PRIVATE KEY", Bytes: privateKeyDER,
Headers: nil, }); err != nil {
Bytes: x509privatekey, panic(err)
} }
//3. save to file
pem.Encode(privatekeyfile, &privatekeyblock)
//----------------------------------------------public key publicKeyDER, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
//get public key
publickey := privateKey.PublicKey
//serialize publickey to ASN.1 der by x509.MarshalPKCS8PublicKey
x509publickey, _ := x509.MarshalPKIXPublicKey(&publickey)
//encode x509 to pem and save to file
//1. create publickeyfile
publickeyfile, err := os.Create(config.PublicKeyPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer publickeyfile.Close() publicKeyFile, err := os.Create(config.PublicKeyPath)
if err != nil {
//2. new a pem block struct object panic(err)
publickeyblock := pem.Block{ }
Type: "PUBLIC KEY", defer publicKeyFile.Close()
Headers: nil, if err := pem.Encode(publicKeyFile, &pem.Block{
Bytes: x509publickey, Type: "PUBLIC KEY",
Bytes: publicKeyDER,
}); err != nil {
panic(err)
} }
//3. save to file
pem.Encode(publickeyfile, &publickeyblock)
} }
func RSAEncrypt(plainText []byte, publickeypath string) ([]byte, error) { func RSAEncrypt(plainText []byte, publickeypath string) ([]byte, error) {
@@ -95,6 +71,7 @@ func RSAEncrypt(plainText []byte, publickeypath string) ([]byte, error) {
return nil, fmt.Errorf("PEM does not contain an RSA public key") return nil, fmt.Errorf("PEM does not contain an RSA public key")
} }
//nolint:staticcheck // Legacy clients require PKCS#1 v1.5 encryption.
return rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText) return rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
} }
@@ -104,6 +81,7 @@ func RSADecrypt(cipherText []byte) ([]byte, error) {
return nil, err return nil, err
} }
//nolint:staticcheck // Legacy clients send PKCS#1 v1.5 encrypted payloads.
return rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText) return rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText)
} }