commit badb5f06272b4f2d47b86a7cbe5bdb6dd241d831 Author: Anatoly Prohacky Date: Sat Jul 30 18:30:44 2022 +1000 Инициализация библиотеки diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccfde3b --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..38bb28c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Утилиты работы с шифрами + diff --git a/base64.go b/base64.go new file mode 100644 index 0000000..f0b6efe --- /dev/null +++ b/base64.go @@ -0,0 +1,35 @@ +package ciphers + +import ( + "encoding/base64" + "strings" +) + +func Base64EncRsa(b1 []byte) string { + s1 := base64.StdEncoding.EncodeToString(b1) + s2 := "" + var LEN int = 76 + for len(s1) > 76 { + s2 = s2 + s1[:LEN] + "\n" + s1 = s1[LEN:] + } + s2 = s2 + s1 + return s2 +} + +func Base64DecRsa(s1 string) ([]byte, error) { + s1 = strings.Replace(s1, "\n", "", -1) + s1 = strings.Replace(s1, "\r", "", -1) + s1 = strings.Replace(s1, " ", "", -1) + return base64.StdEncoding.DecodeString(s1) +} + +func Base64Enc(b1 []byte) string { + + return base64.StdEncoding.EncodeToString(b1) +} + +func Base64Dec(s1 string) ([]byte, error) { + + return base64.StdEncoding.DecodeString(s1) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7c84267 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitstore.ru/tolikproh/chiphers + +go 1.18 diff --git a/rsa.go b/rsa.go new file mode 100644 index 0000000..4326fbb --- /dev/null +++ b/rsa.go @@ -0,0 +1,150 @@ +package ciphers + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "errors" + "hash" +) + +var ( + ErrPubKeyNotCorrect = errors.New("rsa public key is not correct") +) + +// GenerateKeyPair generates a new key pair +func GenerateKeyPair(bits int) (*rsa.PrivateKey, error) { + privkey, err := rsa.GenerateKey(rand.Reader, bits) + if err != nil { + return nil, err + } + return privkey, nil +} + +// PrivateKeyToBytes private key to bytes +func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte { + privBytes := pem.EncodeToMemory( + &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: x509.MarshalPKCS1PrivateKey(priv), + }, + ) + + return privBytes +} + +// PublicKeyToBytes public key to bytes +func PublicKeyToBytes(pub *rsa.PrivateKey) ([]byte, error) { + pubASN1, err := x509.MarshalPKIXPublicKey(&pub.PublicKey) + if err != nil { + return nil, err + } + + pubBytes := pem.EncodeToMemory(&pem.Block{ + Type: "PUBLIC KEY", + Bytes: pubASN1, + }) + + return pubBytes, nil +} + +func BytesRsaKeyToGo(priv, pub []byte) (*rsa.PrivateKey, error) { + + key, err := BytesToPrivateKey(priv) + if err != nil { + return nil, err + } + + pubKey, err := BytesToPublicKey(pub) + if err != nil { + return nil, err + } + + key.PublicKey = rsa.PublicKey{ + N: pubKey.N, + E: pubKey.E, + } + + return key, nil +} + +// +func BytesPublicKeyToGo(pub []byte) (*rsa.PublicKey, error) { + + pubKey, err := BytesToPublicKey(pub) + if err != nil { + return nil, err + } + + return &rsa.PublicKey{ + N: pubKey.N, + E: pubKey.E, + }, nil +} + +// BytesToPrivateKey bytes to private key +func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) { + block, _ := pem.Decode(priv) + enc := x509.IsEncryptedPEMBlock(block) + b := block.Bytes + var err error + if enc { + //log.Println("is encrypted pem block") + b, err = x509.DecryptPEMBlock(block, nil) + if err != nil { + return nil, err + } + } + key, err := x509.ParsePKCS1PrivateKey(b) + if err != nil { + return nil, err + } + return key, nil +} + +// BytesToPublicKey bytes to public key +func BytesToPublicKey(pub []byte) (*rsa.PublicKey, error) { + block, _ := pem.Decode(pub) + enc := x509.IsEncryptedPEMBlock(block) + b := block.Bytes + var err error + if enc { + //log.Println("is encrypted pem block") + b, err = x509.DecryptPEMBlock(block, nil) + if err != nil { + return nil, err + } + } + ifc, err := x509.ParsePKIXPublicKey(b) + if err != nil { + return nil, err + } + key, ok := ifc.(*rsa.PublicKey) + if !ok { + return nil, ErrPubKeyNotCorrect + } + return key, nil +} + +// EncryptWithPublicKey OAEP encrypts data with public key +func EncryptWithPublicKeyOAEP(hash hash.Hash, msg []byte, pub *rsa.PublicKey) ([]byte, error) { + //hash := sha512.New() + //hash := sha1.New() + ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, pub, msg, nil) + if err != nil { + return nil, err + } + return ciphertext, nil +} + +// DecryptWithPrivateKey OAEP decrypts data with private key +func DecryptWithPrivateKeyOAEP(hash hash.Hash, ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error) { + //hash := sha512.New() + //hash := sha1.New() + plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil) + if err != nil { + return nil, err + } + return plaintext, nil +}