Skip to content

Commit

Permalink
feat: add feature to generate public key
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashwat-K committed May 3, 2024
1 parent efde6cb commit 5a1a8ab
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
15 changes: 15 additions & 0 deletions common/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ func OpensslCheck() error {
return nil
}

// GeneratePublicKey - function to generate public key from private key
func GeneratePublicKey(privateKey string) (string, error) {
privateKeyPath, err := gen.CreateTempFile(privateKey)
if err != nil {
return "", err
}

publicKey, err := gen.ExecCommand("openssl", "", "rsa", "-in", privateKeyPath, "-pubout")
if err != nil {
return "", err
}

return publicKey, nil
}

// RandomPasswordGenerator - function to generate random password
func RandomPasswordGenerator() (string, error) {
randomPassword, err := gen.ExecCommand("openssl", "", "rand", fmt.Sprint(keylen))
Expand Down
34 changes: 34 additions & 0 deletions common/encrypt/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
sampleCsrMailId = "[email protected]"

sampleExpiryDays = 365

simplePrivateKeyPath = "../../samples/encrypt/private.pem"
simplePublicKeyPath = "../../samples/encrypt/public.pem"
)

// Testcase to check if OpensslCheck() is able to check if openssl is present in the system or not
Expand All @@ -40,6 +43,37 @@ func TestOpensslCheck(t *testing.T) {
assert.NoError(t, err)
}

func TestGeneratePublicKey(t *testing.T) {
privateKeyFile, err := os.Open(simplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKeyFile.Close()

privateKey, err := io.ReadAll(privateKeyFile)
if err != nil {
fmt.Println(err)
}

publicKeyFile, err := os.Open(simplePublicKeyPath)
if err != nil {
fmt.Println(err)
}
defer publicKeyFile.Close()

publicKey, err := io.ReadAll(publicKeyFile)
if err != nil {
fmt.Println(err)
}

result, err := GeneratePublicKey(string(privateKey))
if err != nil {
fmt.Println(err)
}

assert.Equal(t, result, string(publicKey))
}

// Testcase to check if RandomPasswordGenerator() is able to generate random password
func TestRandomPasswordGenerator(t *testing.T) {
result, err := RandomPasswordGenerator()
Expand Down
52 changes: 52 additions & 0 deletions samples/encrypt/private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDGsgw62gkpUTo+
WGhVG6/6i/WKZzwVB4Y+C6FM+IACTjmMT4gm4ku+ZOp3w7ZVWk41t0dvu+Jdra4v
XE82om9SJV8wvnbs/FadJLCSv4njrSN+h35nYE0IjnJTO5XUBeji7Bt4pbdeZLqh
oc5X7qRJXRf7ClD2SvIGu/l4iySUjSYquCKk2xzGjGjif4P07V0JQ1YbrHaKJW4r
LPFbpg366ini+qi4veMUJb5UMVPHal5vveP97Nm1NRcOb457vlaaxUtxAfwS9eZC
UQ/+Uae9jV3kSvl/0Ag2vwZBnfnyyRcKi36XHpvZ3/LqlcA0tkAZ+EV8ogLUhYb3
j4RaPSc8/NxKLHEZE3CW9xakJzEkuoOhQGHW3k/zxtJRFNXOd4Su3leYpwRYLCuU
d2QiAVu6TWKaIPzLEp2Z9wnDqwWczH1KiqodCPo4Q9WRartxRcAQvs04r1U2+x4t
7qrQUNRIr37I7860aYcAulGZyCziIn1F8T1gAq2RE8VMMwZcYgQ+CAEHbS16djh7
iuKrlCFqO5ySrDOUUdyUUpk5zWwsawEWEYcMd+aaJyBkcC5LJplneg/fDeeEz5vQ
6H4nVHdUARVggLfT5QVQZasw3gK8L/plU1dUXF8Uuo76FqDqvwK6NMooKxuE12JK
HpOi6FPoxxzD1fqck609r4Dv8VRalQIDAQABAoICADgEyLfq8vxIH2CmcjvUg4fi
38Y2ltTi+zEQ/FgEE6sSvNzXK26zDGHQd95uOkDUJgq08poI4ahBqENIBjlEEJib
WrwWZtcnyY+iYXnuMIUVMlWhA71HhFBuQMNAoFxXkjIwGQqpxgoB4jBhuxhFeuII
MhOf8/WMabIGWy5mJFkRdnpC0dmiwPklawi4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CAJmWObiZq0KYLuVE2Xx3xebkx
58p9MxMnsihxA05U3Fa2p5n9FMc1/N6ZMD4yujvQlZ3ZUro+e/HB1yFVW0be8JIk
yxJXll0Nj1Pn6+uhT0GdqNiiiQ2KQw==
-----END PRIVATE KEY-----
14 changes: 14 additions & 0 deletions samples/encrypt/public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxrIMOtoJKVE6PlhoVRuv
+ov1imc8FQeGPguhTPiAAk45jE+IJuJLvmTqd8O2VVpONbdHb7viXa2uL1xPNqJv
UiVfML527PxWnSSwkr+J460jfod+Z2BNCI5yUzuV1AXo4uwbeKW3XmS6oaHOV+6k
SV0X+wpQ9kryBrv5eIsklI0mKrgipNscxoxo4n+D9O1dCUNWG6x2iiVuKyzxW6YN
+uop4vqouL3jFCW+VDFTx2peb73j/ezZtTUXDm+Oe75WmsVLcQH8EvXmQlEP/lGn
vY1d5Er5f9AINr8GQZ358skXCot+lx6b2d/y6pXANLZAGfhFfKIC1IWG94+EWj0n
PPzcSixxGRNwlvcWpCcxJLqDoUBh1t5P88bSURTVzneErt5XmKcEWCwrlHdkIgFb
uk1imiD8yxKdmfcJw6sFnMx9SoqqHQj6OEPVkWq7cUXAEL7NOK9VNvseLe6q0FDU
SK9+yO/OtGmHALpRmcgs4iJ9RfE9YAKtkRPFTDMGXGIEPggBB20tenY4e4riq5Qh
ajuckqwzlFHclFKZOc1sLGsBFhGHDHfmmicgZHAuSyaZZ3oP3w3nhM+b0Oh+J1R3
VAEVYIC30+UFUGWrMN4CvC/6ZVNXVFxfFLqO+hag6r8CujTKKCsbhNdiSh6TouhT
6Mccw9X6nJOtPa+A7/FUWpUCAwEAAQ==
-----END PUBLIC KEY-----

0 comments on commit 5a1a8ab

Please sign in to comment.