-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decrypt passwords and private keys of Honeybee's connection info
- Loading branch information
Showing
6 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package common | ||
|
||
import "crypto/rsa" | ||
|
||
var HoneybeePrivateKeyFileName = "honeybee.key" | ||
|
||
var HoneybeePrivateKey *rsa.PrivateKey |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package rsautil | ||
|
||
import ( | ||
"crypto/rsa" | ||
"os" | ||
) | ||
|
||
func ReadPrivateKey(privateKeyFilePath string) (*rsa.PrivateKey, error) { | ||
bytes, err := os.ReadFile(privateKeyFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
privKey, err := BytesToPrivateKey(bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return privKey, nil | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package rsautil | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha512" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"github.com/labstack/gommon/log" | ||
) | ||
|
||
// BytesToPrivateKey bytes to private key | ||
func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) { | ||
block, _ := pem.Decode(priv) | ||
b := block.Bytes | ||
var err error | ||
key, err := x509.ParsePKCS1PrivateKey(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return key, nil | ||
} | ||
|
||
// DecryptWithPrivateKey decrypts data with private key | ||
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) []byte { | ||
hash := sha512.New() | ||
plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
return plaintext | ||
} |
This file contains 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 characters