Skip to content

Commit

Permalink
Fix key format of generate rsa private key
Browse files Browse the repository at this point in the history
  • Loading branch information
PichuChen committed Aug 2, 2024
1 parent 79f6c0a commit fcf75a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions activitypub/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ func Pubout(privateKeyPem []byte) []byte {
}

// encode to pem
keyBytes := x509.MarshalPKCS1PublicKey(&key.(*rsa.PrivateKey).PublicKey)
keyBytes, err := x509.MarshalPKIXPublicKey(key.(*rsa.PrivateKey).Public())
if err != nil {
slog.Error("Failed to marshal public key", "Error", err)
return nil
}

return pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Expand All @@ -178,7 +182,7 @@ func GeneratePrivateKey() string {
}

return string(pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Type: "PRIVATE KEY",
Bytes: keyBytes,
}))
}
Expand Down
48 changes: 48 additions & 0 deletions activitypub/signature/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,51 @@ func TestSignatureHeader(t *testing.T) {

}
}

func TestRSAPubout(t *testing.T) {
privateKeyPem := `-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC0S0NTogJhUZ56
3AmWu2BUhxafIVqdG30twyT8zqHsGGPtzITSo23XHZVD9MhEb94dWHv+Io2gXi04
Dq/w8MSudBiFxP7dkwPvH9BVmkIDAT4CXqqldPeE+gwDX3oGM/k3blbERi26EBJd
UDXFuGXXyJLzIXzFJ9xgZ9GohjUlKFJlLXhaErm4Du7BHFsnT02HSXstmfSnIwuX
/DH699FsWGzVDg2Z8ntAuLeVawzfLO6KmxunQLf3U6tX/NrgKrUbUcNuSvTdphFl
UnJDZiMAHd+xkWHez+s0FH3IJA1AXWbjRzsqh2vpzgtRKEVdCLsNo+dB8fdnOVV9
JmnKuLgfAgM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NhLS4bCKlUsGcMfvKe4Y
IGZM5kyAbOkl0oKPQVT/4lsbwExopj9M4oE4BPJcSRx4JWUghjDyHKBvUnMvnPCe
C61EeSrlwKTNRojY09kAKF3FaTaoyJDjeFcWbRJ4oX8QNLPGpzRZFrCCe1WgHRhQ
bgZ5TKo2v+nTWMJq1djPJQ==
-----END PRIVATE KEY-----
`
expected := `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtEtDU6ICYVGeetwJlrtg
VIcWnyFanRt9LcMk/M6h7Bhj7cyE0qNt1x2VQ/TIRG/eHVh7/iKNoF4tOA6v8PDE
rnQYhcT+3ZMD7x/QVZpCAwE+Al6qpXT3hPoMA196BjP5N25WxEYtuhASXVA1xbhl
18iS8yF8xSfcYGfRqIY1JShSZS14WhK5uA7uwRxbJ09Nh0l7LZn0pyMLl/wx+vfR
bFhs1Q4NmfJ7QLi3lWsM3yzuipsbp0C391OrV/za4Cq1G1HDbkr03aYRZVJyQ2Yj
AB3fsZFh3s/rNBR9yCQNQF1m40c7Kodr6c4LUShFXQi7DaPnQfH3ZzlVfSZpyri4
HwIDAQAB
-----END PUBLIC KEY-----
`

publicKeyPem := Pubout([]byte(privateKeyPem))

if string(publicKeyPem) != expected {
t.Errorf("Expected %s, got %s", expected, publicKeyPem)
}
}

0 comments on commit fcf75a6

Please sign in to comment.