diff --git a/cryptox/asymmetric/cryptoa.go b/cryptox/asymmetric/cryptoa.go new file mode 100644 index 0000000..0b3f53a --- /dev/null +++ b/cryptox/asymmetric/cryptoa.go @@ -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, + } +} diff --git a/cryptox/asymmetric/rsa/input.go b/cryptox/asymmetric/rsa/input.go new file mode 100644 index 0000000..4267190 --- /dev/null +++ b/cryptox/asymmetric/rsa/input.go @@ -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 +} diff --git a/cryptox/asymmetric/rsa/key.go b/cryptox/asymmetric/rsa/key.go new file mode 100644 index 0000000..2dd8b71 --- /dev/null +++ b/cryptox/asymmetric/rsa/key.go @@ -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 + } +} diff --git a/cryptox/asymmetric/rsa/output.go b/cryptox/asymmetric/rsa/output.go new file mode 100644 index 0000000..4a224eb --- /dev/null +++ b/cryptox/asymmetric/rsa/output.go @@ -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 +} diff --git a/cryptox/asymmetric/rsa/rsa.go b/cryptox/asymmetric/rsa/rsa.go new file mode 100644 index 0000000..333195a --- /dev/null +++ b/cryptox/asymmetric/rsa/rsa.go @@ -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 +) diff --git a/cryptox/symmetric/cipher.go b/cryptox/symmetric/cipher.go index 6c88ce0..0306ff4 100644 --- a/cryptox/symmetric/cipher.go +++ b/cryptox/symmetric/cipher.go @@ -1,3 +1,17 @@ +// 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 symmetric import ( diff --git a/cryptox/symmetric/decrypt.go b/cryptox/symmetric/decrypt.go index c2e88b2..3235f8c 100644 --- a/cryptox/symmetric/decrypt.go +++ b/cryptox/symmetric/decrypt.go @@ -1,3 +1,17 @@ +// 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 symmetric import ( diff --git a/cryptox/symmetric/encrypt.go b/cryptox/symmetric/encrypt.go index 0fa5ab6..93feaec 100644 --- a/cryptox/symmetric/encrypt.go +++ b/cryptox/symmetric/encrypt.go @@ -1,3 +1,17 @@ +// 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 symmetric import ( diff --git a/cryptox/symmetric/iv.go b/cryptox/symmetric/iv.go index 0cba2aa..94277ff 100644 --- a/cryptox/symmetric/iv.go +++ b/cryptox/symmetric/iv.go @@ -1,3 +1,17 @@ +// 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 symmetric import ( diff --git a/cryptox/symmetric/key.go b/cryptox/symmetric/key.go index 77884ec..36c8a31 100644 --- a/cryptox/symmetric/key.go +++ b/cryptox/symmetric/key.go @@ -1,3 +1,17 @@ +// 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 symmetric import ( diff --git a/cryptox/symmetric/method/method.go b/cryptox/symmetric/method/method.go index 5c921a2..f878292 100644 --- a/cryptox/symmetric/method/method.go +++ b/cryptox/symmetric/method/method.go @@ -1,3 +1,17 @@ +// 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 method // MethodType is the encryption method such as AES. diff --git a/cryptox/symmetric/mode/mode.go b/cryptox/symmetric/mode/mode.go index 3c4564f..5c7c5ce 100644 --- a/cryptox/symmetric/mode/mode.go +++ b/cryptox/symmetric/mode/mode.go @@ -1,3 +1,17 @@ +// 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 mode // ModeType is the encrypted mode. diff --git a/cryptox/symmetric/validator.go b/cryptox/symmetric/validator.go index 8562e92..6a95d9b 100644 --- a/cryptox/symmetric/validator.go +++ b/cryptox/symmetric/validator.go @@ -1,3 +1,17 @@ +// 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 symmetric import (