-
-
Save fuadarradhi/cde6e187761b846e7416554205727d3c to your computer and use it in GitHub Desktop.
Revisions
-
soyart revised this gist
Sep 17, 2021 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -20,9 +20,6 @@ var ( salt = rand.Reader msg = "This is a secret message" rsaPubKey *string rsaPriKey *string ) @@ -60,7 +57,7 @@ func encrypt(plaintext string) string { os.Stderr.Write([]byte("Failed to parse public key\n")) panic(err) } pub := pubInterface.(*rsa.PublicKey) ciphertext, err := rsa.EncryptOAEP(hash, salt, pub, []byte(msg), nil) if err != nil { os.Stderr.Write([]byte("Failed to encrypt string\n")) @@ -75,7 +72,7 @@ func encrypt(plaintext string) string { func decrypt(ciphertext string) string { block, _ := pem.Decode([]byte(*rsaPriKey)) pri, err := x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { os.Stderr.Write([]byte("Failed to parse private key\n")) panic(err) -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 21 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,14 +7,18 @@ import ( "crypto/x509" "encoding/base64" "encoding/pem" // I deliberately don't use fmt package in this file // so that error messages can be directed to Stderr // and can be managed with shell scripts. // Previous versions did use fmt. "os" ) var ( hash = sha256.New() salt = rand.Reader msg = "This is a secret message" err error pub *rsa.PublicKey @@ -38,12 +42,12 @@ func main() { // Default key pair is declared at the end of this file. rsaPubKey = &defaultPubkey rsaPriKey = &defaultPriKey os.Stdout.Write([]byte("╭――――――――――――――――――――――――――――――――╮\n")) os.Stdout.Write([]byte("│ Default key pair will be used. │\n")) os.Stdout.Write([]byte("│ Provide enviroment variables │\n")) os.Stdout.Write([]byte("│ RSA_PUB_KEY and RSA_PRIV_KEY │\n")) os.Stdout.Write([]byte("│ to use your own key pair. │\n")) os.Stdout.Write([]byte("╰――――――――――――――――――――――――――――――――╯\n")) } decrypt(encrypt(msg)) @@ -53,34 +57,36 @@ func encrypt(plaintext string) string { block, _ := pem.Decode([]byte(*rsaPubKey)) pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { os.Stderr.Write([]byte("Failed to parse public key\n")) panic(err) } pub = pubInterface.(*rsa.PublicKey) ciphertext, err := rsa.EncryptOAEP(hash, salt, pub, []byte(msg), nil) if err != nil { os.Stderr.Write([]byte("Failed to encrypt string\n")) panic(err) } // Encode ciphertext in base64 for printing to screen. encoded := base64.URLEncoding.EncodeToString(ciphertext) os.Stdout.Write([]byte("Ciphertext:\n")) os.Stdout.Write([]byte(encoded + "\n")) return string(ciphertext) } func decrypt(ciphertext string) string { block, _ := pem.Decode([]byte(*rsaPriKey)) pri, err = x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { os.Stderr.Write([]byte("Failed to parse private key\n")) panic(err) } plaintext, err := rsa.DecryptOAEP(hash, salt, pri, []byte(ciphertext), nil) if err != nil { os.Stderr.Write([]byte("Failed to decrypt string\n")) panic(err) } os.Stdout.Write([]byte("Plaintext:\n")) os.Stdout.Write(plaintext) return string(plaintext) } @@ -125,4 +131,4 @@ lz5UUW+5iBe/z54HOdmBbVdd3G+fRlkCm9XUex0GlwaN3g45k8rcyJi/8iRexIpF 2c3olpk+wO+d7ciK+7Qc8uHYyZlnBxQu6FIRDTE/Y8QOkU97/BDSkYQ= -----END RSA PRIVATE KEY----- ` ) -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,8 +19,8 @@ var ( err error pub *rsa.PublicKey pri *rsa.PrivateKey rsaPubKey *string rsaPriKey *string ) func main() { @@ -31,13 +31,13 @@ func main() { envPriKey := os.Getenv("RSA_PRIV_KEY") if len(envPriKey) > 0 && len(envPubKey) > 0 { rsaPubKey = &envPubKey rsaPriKey = &envPriKey } else { // If key pair not given in the environment variables, use default pair. // Default key pair is declared at the end of this file. rsaPubKey = &defaultPubkey rsaPriKey = &defaultPriKey fmt.Println("╭―――――――――――――――――――――――――――――――╮") fmt.Println("│ Default key pair will be used │") fmt.Println("│ Provide enviroment variables │") @@ -50,7 +50,7 @@ func main() { } func encrypt(plaintext string) string { block, _ := pem.Decode([]byte(*rsaPubKey)) pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { fmt.Println("Failed to parse public key") @@ -69,7 +69,7 @@ func encrypt(plaintext string) string { } func decrypt(ciphertext string) string { block, _ := pem.Decode([]byte(*rsaPriKey)) pri, err = x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { fmt.Println("Failed to parse private key") @@ -125,4 +125,4 @@ lz5UUW+5iBe/z54HOdmBbVdd3G+fRlkCm9XUex0GlwaN3g45k8rcyJi/8iRexIpF 2c3olpk+wO+d7ciK+7Qc8uHYyZlnBxQu6FIRDTE/Y8QOkU97/BDSkYQ= -----END RSA PRIVATE KEY----- ` ) -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -36,11 +36,14 @@ func main() { } else { // If key pair not given in the environment variables, use default pair. // Default key pair is declared at the end of this file. usePubKey = &defaultPubkey usePriKey = &defaultPriKey fmt.Println("╭―――――――――――――――――――――――――――――――╮") fmt.Println("│ Default key pair will be used │") fmt.Println("│ Provide enviroment variables │") fmt.Println("│ RSA_PUB_KEY and RSA_PRIV_KEY │") fmt.Println("│ to use your own key pair. │") fmt.Println("╰―――――――――――――――――――――――――――――――╯") } decrypt(encrypt(msg)) -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 52 additions and 48 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -19,68 +19,28 @@ var ( err error pub *rsa.PublicKey pri *rsa.PrivateKey usePubKey *string usePriKey *string ) func main() { // Example of a command using environment variables: // $ RSA_PUB_KEY=$(< path/to/pub_key_file) RSA_PRIV_KEY=$(< path/to/priv_key_file) go run rsa.go envPubKey := os.Getenv("RSA_PUB_KEY") envPriKey := os.Getenv("RSA_PRIV_KEY") if len(envPriKey) > 0 && len(envPubKey) > 0 { usePubKey = &envPubKey usePriKey = &envPriKey } else { // If key pair not given in the environment variables, use default pair. // Default key pair is declared at the end of this file. fmt.Println("--------------------------") fmt.Println("Using default RSA key pair") fmt.Println("--------------------------") usePubKey = &defaultPubkey usePriKey = &defaultPriKey } decrypt(encrypt(msg)) @@ -99,6 +59,7 @@ func encrypt(plaintext string) string { fmt.Println("Failed to encrypt string") panic(err) } // Encode ciphertext in base64 for printing to screen. encoded := base64.URLEncoding.EncodeToString(ciphertext) fmt.Printf("Ciphertext:\n%s\n", encoded) return string(ciphertext) @@ -118,4 +79,47 @@ func decrypt(ciphertext string) string { } fmt.Printf("Plaintext:\n%s\n", string(plaintext)) return string(plaintext) } var ( defaultPubkey = ` -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAscetx8F1Q7H67ZSgIcTw zQfCf919iACii2o5sh+1l7N62nE9zBpSx3OEgNv64l8v4OchXMU8gKk28piExpdQ kvzDW5VK7STmEuIZ7IqWKsZge1YmGDsyIFw74V9Uslhc05t7VKYhWWFPAKfouPPM 3ZKe5ZiALAjvLVIEUYYnQ452H2RJGuGYJeKvPiNtOwKSLA/ROw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UGAUNkCgYEAwfrF 7UWIrCqx2abgsGNfHp4omwhfv8jpD4CGpXKGrHvnagGfLYABngbmIo0GLHyUR0Rm wE2qfeDp+64vvNj+RV4lRME1PNFsWxaJ8eMUHr06lDO51Cy1lhTWymT4NXj+Esys dFJvCElfwxbZjflyNf8hfkSa24Rfo6WoI9jV4UsCgYEA4HJZlrRVms2mjnmym/LI Xhu5F7v3DJMdmh7bgVWtls7gsCKRqigBvKHKvc2PF+bQ86HOcYNWxkv3i8wnwJVZ aI2MauHh7iHxd1ifYcKALVchSZ8sSP8hfmLJfOQdWwUWEO4UMLGTH3zgwNnfM7nO iOj8mQMUYIB2OaYuipTt0ukCgYEAl4qRHAdJea81GCNtv38ybVoDwPIu00ZjBNBU 4GXzXkbCCCfSMhqhqNIc8fsYSqLcuDxwxWUnf4W5ZfyzoKYpJwogtXD3ZVb6fsLB 662KJ2WPoP4z+9Ud22zWTHHLEwM+AnPRemJ4CZJA9MkiFu88UYDKqrlv/XSRvugI zlB07rcCgYEAueo9hE02p0iSqxXWru8zu7PxY8Gy2+tksMZb4PWB5C732BMr3ryP lz5UUW+5iBe/z54HOdmBbVdd3G+fRlkCm9XUex0GlwaN3g45k8rcyJi/8iRexIpF 2c3olpk+wO+d7ciK+7Qc8uHYyZlnBxQu6FIRDTE/Y8QOkU97/BDSkYQ= -----END RSA PRIVATE KEY----- ` ) -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,7 +73,6 @@ func main() { // If key pair not given in the environment variables, use default pair if len(envPriKey) > 0 && len(envPubKey) > 0 { usePriKey = &envPriKey usePubKey = &envPubKey } else { @@ -119,4 +118,4 @@ func decrypt(ciphertext string) string { } fmt.Printf("Plaintext:\n%s\n", string(plaintext)) return string(plaintext) } -
soyart revised this gist
Sep 16, 2021 . 1 changed file with 21 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,14 +12,15 @@ import ( ) var ( hash = sha256.New() salt = rand.Reader msg = "This is a secret" err error pub *rsa.PublicKey pri *rsa.PrivateKey usePriKey *string usePubKey *string defaultPubkey = ` -----BEGIN PUBLIC KEY----- @@ -65,27 +66,29 @@ lz5UUW+5iBe/z54HOdmBbVdd3G+fRlkCm9XUex0GlwaN3g45k8rcyJi/8iRexIpF func main() { // Example of a command using environment variables: // $ RSA_PRIV_KEY=$(< path/to/priv_key_file) RSA_PUB_KEY=$(< path/to/pub_key_file) go run rsa.go envPriKey := os.Getenv("RSA_PRIV_KEY") envPubKey := os.Getenv("RSA_PUB_KEY") // If key pair not given in the environment variables, use default pair if len(envPriKey) > 0 && len(envPubKey) > 0 { // Run RSA_PRIV_KEY=$(< path/to/priv_key_file) usePriKey = &envPriKey usePubKey = &envPubKey } else { fmt.Println("--------------------------") fmt.Println("Using default RSA key pair") fmt.Println("--------------------------") usePriKey = &defaultPriKey usePubKey = &defaultPubkey } decrypt(encrypt(msg)) } func encrypt(plaintext string) string { block, _ := pem.Decode([]byte(*usePubKey)) pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { fmt.Println("Failed to parse public key") @@ -103,7 +106,7 @@ func encrypt(plaintext string) string { } func decrypt(ciphertext string) string { block, _ := pem.Decode([]byte(*usePriKey)) pri, err = x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { fmt.Println("Failed to parse private key") -
soyart created this gist
Sep 15, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,119 @@ package main import ( "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/pem" "fmt" "os" ) var ( hash = sha256.New() salt = rand.Reader msg = "This is a secret" pub *rsa.PublicKey pri *rsa.PrivateKey err error usePriKey string usePubKey string defaultPubkey = ` -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAscetx8F1Q7H67ZSgIcTw zQfCf919iACii2o5sh+1l7N62nE9zBpSx3OEgNv64l8v4OchXMU8gKk28piExpdQ kvzDW5VK7STmEuIZ7IqWKsZge1YmGDsyIFw74V9Uslhc05t7VKYhWWFPAKfouPPM 3ZKe5ZiALAjvLVIEUYYnQ452H2RJGuGYJeKvPiNtOwKSLA/ROwvE/1I+0S+gq1hd +GbrYPJLfj77pkZJKnf/ye3rgbQglfBQzSHSDKuwC6xNZEWMR4DBzraE0MeKrNhN 4PFxKpkyVRPftrahbiTA6ohvoBsSyD+RdT1dRde4qbJGXuW6AQ2DQYNPWqTdVDNo kwIDAQAB -----END PUBLIC KEY----- ` defaultPriKey = ` -----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAscetx8F1Q7H67ZSgIcTwzQfCf919iACii2o5sh+1l7N62nE9 zBpSx3OEgNv64l8v4OchXMU8gKk28piExpdQkvzDW5VK7STmEuIZ7IqWKsZge1Ym GDsyIFw74V9Uslhc05t7VKYhWWFPAKfouPPM3ZKe5ZiALAjvLVIEUYYnQ452H2RJ GuGYJeKvPiNtOwKSLA/ROwvE/1I+0S+gq1hd+GbrYPJLfj77pkZJKnf/ye3rgbQg lfBQzSHSDKuwC6xNZEWMR4DBzraE0MeKrNhN4PFxKpkyVRPftrahbiTA6ohvoBsS yD+RdT1dRde4qbJGXuW6AQ2DQYNPWqTdVDNokwIDAQABAoIBACnS6iVGdAn7AyeF ga6wIF575twiBXhLffICiZRINXZ8+PgPEBTGVJcrrA6MshczgZYNiiHDHRq/tHea PhJiYshRwrv3AWuM9LuYibTGXdGuXeB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K+7Qc8uHYyZlnBxQu6FIRDTE/Y8QOkU97/BDSkYQ= -----END RSA PRIVATE KEY----- ` ) func main() { // If keypair not given in the environment variable, // use default pair envPriKey := os.Getenv("RSA_PRIV_KEY") envPubKey := os.Getenv("RSA_PUB_KEY") if len(envPriKey) > 0 && len(envPubKey) > 0 { usePriKey = envPriKey usePubKey = envPubKey } else { fmt.Println("-------------------------") fmt.Println("Using default RSA keypair") fmt.Println("-------------------------") usePriKey = defaultPriKey usePubKey = defaultPubkey } decrypt(encrypt(msg)) } func encrypt(plaintext string) string { block, _ := pem.Decode([]byte(usePubKey)) pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { fmt.Println("Failed to parse public key") panic(err) } pub = pubInterface.(*rsa.PublicKey) ciphertext, err := rsa.EncryptOAEP(hash, salt, pub, []byte(msg), nil) if err != nil { fmt.Println("Failed to encrypt string") panic(err) } encoded := base64.URLEncoding.EncodeToString(ciphertext) fmt.Printf("Ciphertext:\n%s\n", encoded) return string(ciphertext) } func decrypt(ciphertext string) string { block, _ := pem.Decode([]byte(usePriKey)) pri, err = x509.ParsePKCS1PrivateKey(block.Bytes) if err != nil { fmt.Println("Failed to parse private key") panic(err) } plaintext, err := rsa.DecryptOAEP(hash, salt, pri, []byte(ciphertext), nil) if err != nil { fmt.Println("Failed to decrypt string") panic(err) } fmt.Printf("Plaintext:\n%s\n", string(plaintext)) return string(plaintext) }