Skip to content

Commit

Permalink
wip: rsa encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Mar 13, 2023
1 parent b48b507 commit 40de61e
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cryptox/asymmetric/cryptoa.go
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,
}
}
49 changes: 49 additions & 0 deletions cryptox/asymmetric/rsa/input.go
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
}
73 changes: 73 additions & 0 deletions cryptox/asymmetric/rsa/key.go
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
}
}
26 changes: 26 additions & 0 deletions cryptox/asymmetric/rsa/output.go
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
}
42 changes: 42 additions & 0 deletions cryptox/asymmetric/rsa/rsa.go
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
)
14 changes: 14 additions & 0 deletions cryptox/symmetric/cipher.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/decrypt.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/encrypt.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/iv.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/key.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/method/method.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/mode/mode.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
14 changes: 14 additions & 0 deletions cryptox/symmetric/validator.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit 40de61e

Please # to comment.