-
Notifications
You must be signed in to change notification settings - Fork 0
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
13 changed files
with
314 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package asymmetric | ||
|
||
import ( | ||
"github.com/suyuan32/knife/cryptox/asymmetric/rsa" | ||
) | ||
|
||
// NewRSA returns an RSA struct. | ||
func NewRSA() *rsa.RSA { | ||
return &rsa.RSA{ | ||
Standard: rsa.PKCS1, | ||
} | ||
} |
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,49 @@ | ||
// Copyright 2023 The Ryan SU Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rsa | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
"errors" | ||
) | ||
|
||
// InputFromBytes set input data from byte slice. | ||
func (s *RSA) InputFromBytes(data []byte) *RSA { | ||
s.InputData = data | ||
return s | ||
} | ||
|
||
// InputFromString set input data from string. | ||
func (s *RSA) InputFromString(data string) *RSA { | ||
s.InputData = []byte(data) | ||
return s | ||
} | ||
|
||
// InputFromBase64String set input data from base64 string. | ||
func (s *RSA) InputFromBase64String(data string) *RSA { | ||
result, err := base64.StdEncoding.DecodeString(data) | ||
s.Errors = errors.Join(s.Errors, err) | ||
s.InputData = result | ||
return s | ||
} | ||
|
||
// InputFromHexString set input data from hex string. | ||
func (s *RSA) InputFromHexString(data string) *RSA { | ||
result, err := hex.DecodeString(data) | ||
s.Errors = errors.Join(s.Errors, err) | ||
s.InputData = result | ||
return s | ||
} |
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,73 @@ | ||
// Copyright 2023 The Ryan SU Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rsa | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
errorNotValidPEMKey = errors.New("the key must be a PEM string encoded by PKCS1 or PKCS8") | ||
errorNotValidPrivateKey = errors.New("the key is not a valid RSA private key") | ||
errorNotValidPublicKey = errors.New(" the key is not a valid RSA public key") | ||
) | ||
|
||
// GenerateKeyPair set public key and private key for RSA struct. | ||
func (s *RSA) GenerateKeyPair(bits int) *RSA { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, bits) | ||
if err != nil { | ||
s.Errors = errors.Join(s.Errors, fmt.Errorf("rsa: generate key pair failed, err : %v", err)) | ||
return s | ||
} | ||
s.PrivateKey = privateKey | ||
s.PublicKey = &privateKey.PublicKey | ||
return s | ||
} | ||
|
||
// PrivateKeyFromPEM gets private key from a PEM byte slice. | ||
func (s *RSA) PrivateKeyFromPEM(data []byte) *RSA { | ||
block, _ := pem.Decode(data) | ||
if block == nil { | ||
s.Errors = errors.Join(s.Errors, errorNotValidPEMKey) | ||
return s | ||
} | ||
|
||
switch s.Standard { | ||
case PKCS1: | ||
if parse, err := x509.ParsePKCS1PrivateKey(block.Bytes); err != nil { | ||
s.Errors = errors.Join(s.Errors, errorNotValidPrivateKey) | ||
return s | ||
} else { | ||
s.PrivateKey = parse | ||
return s | ||
} | ||
case PKCS8: | ||
if parse, err := x509.ParsePKCS8PrivateKey(block.Bytes); err != nil { | ||
s.Errors = errors.Join(s.Errors, errorNotValidPrivateKey) | ||
return s | ||
} else { | ||
s.PrivateKey = parse.(*rsa.PrivateKey) | ||
return s | ||
} | ||
default: | ||
s.Errors = errors.Join(s.Errors, errorNotValidPrivateKey) | ||
return s | ||
} | ||
} |
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,26 @@ | ||
package rsa | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/hex" | ||
) | ||
|
||
// ToString output data with string type. | ||
func (s *RSA) ToString() (string, error) { | ||
return string(s.OutputData), s.Errors | ||
} | ||
|
||
// ToBytes output data with byte type. | ||
func (s *RSA) ToBytes() ([]byte, error) { | ||
return s.OutputData, s.Errors | ||
} | ||
|
||
// ToBase64String output data with base64 string. | ||
func (s *RSA) ToBase64String() (string, error) { | ||
return base64.StdEncoding.EncodeToString(s.OutputData), s.Errors | ||
} | ||
|
||
// ToHexString output data with hex string. | ||
func (s *RSA) ToHexString() (string, error) { | ||
return hex.EncodeToString(s.OutputData), s.Errors | ||
} |
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,42 @@ | ||
// Copyright 2023 The Ryan SU Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rsa | ||
|
||
import ( | ||
"crypto/rsa" | ||
) | ||
|
||
type RSA struct { | ||
PrivateKey *rsa.PrivateKey | ||
|
||
PublicKey *rsa.PublicKey | ||
|
||
Key []byte | ||
|
||
InputData []byte | ||
|
||
OutputData []byte | ||
|
||
Standard Standard | ||
|
||
Errors error | ||
} | ||
|
||
type Standard uint8 | ||
|
||
const ( | ||
PKCS1 Standard = 1 + iota | ||
PKCS8 | ||
) |
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
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