Documentation
¶
Overview ¶
Package filekms implements crypto.Signer and crypto.Decrypter for keys stored on the filesystem.
Unless file is backed by in memory file-system this may be insecure. Keys MUST NOT be password protected. Keys may be base64 encoded.
Index ¶
- type Decrypter
- func (d *Decrypter) Algorithm() cryptokms.Algorithm
- func (d *Decrypter) CreatedAt() time.Time
- func (d *Decrypter) Decrypt(_ io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (d *Decrypter) DecryptContext(ctx context.Context, _ io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) ([]byte, error)
- func (d *Decrypter) HashFunc() crypto.Hash
- func (d *Decrypter) Public() crypto.PublicKey
- func (d *Decrypter) WithContext(ctx context.Context) *Decrypter
- type Signer
- func (s *Signer) Algorithm() cryptokms.Algorithm
- func (s *Signer) CreatedAt() time.Time
- func (s *Signer) HashFunc() crypto.Hash
- func (s *Signer) Public() crypto.PublicKey
- func (s *Signer) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)
- func (s *Signer) SignContext(ctx context.Context, _ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)
- func (s *Signer) WithContext(ctx context.Context) *Signer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decrypter ¶
type Decrypter struct {
// contains filtered or unexported fields
}
Decrypter.
Example ¶
package main import ( "context" "crypto/rand" "crypto/rsa" "fmt" "github.com/tprasadtp/cryptokms/filekms" ) func main() { ctx := context.Background() // Please replace this with path to your PEM encoded key file. keyFile := "internal/testdata/rsa-3072.pem" // Create a new Decrypter decrypter, err := filekms.NewDecrypter(keyFile) if err != nil { // TODO: Handle error panic(err) } // Message you want to encrypt // A nod to https://en.wikipedia.org/wiki/Stellar_classification. msg := []byte(`Oh Be A Fine Girl Kiss Me`) // Encrypt the message using public key. encrypted, err := rsa.EncryptOAEP( decrypter.HashFunc().New(), rand.Reader, decrypter.Public().(*rsa.PublicKey), msg, nil, ) if err != nil { // TODO: Handle error panic(err) } // Decrypt the message plaintext, err := decrypter.DecryptContext(ctx, nil, encrypted, nil) if err != nil { // TODO: Handle error panic(err) } fmt.Printf("Plaintext: %s", string(plaintext)) }
Output: Plaintext: Oh Be A Fine Girl Kiss Me
func NewDecrypter ¶
NewDecrypter returns a new decrypter based on key in the path specified.
func (*Decrypter) Decrypt ¶
func (d *Decrypter) Decrypt(_ io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) ([]byte, error)
Sign is a wrapper around SignContext.
func (*Decrypter) DecryptContext ¶
func (d *Decrypter) DecryptContext(ctx context.Context, _ io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) ([]byte, error)
DecryptContext decrypts the message with asymmetric key. The rand parameter is ignored, and it can be nil.
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
Signer.
Example ¶
package main import ( "context" "encoding/hex" "fmt" "github.com/tprasadtp/cryptokms" "github.com/tprasadtp/cryptokms/filekms" ) func main() { ctx := context.Background() // Please replace this with path to your PEM encoded key file. keyFile := "internal/testdata/ec-p256.pem" // Create a new Signer. signer, err := filekms.NewSigner(keyFile) if err != nil { // TODO: Handle error panic(err) } // Message you want to sign // A nod to https://en.wikipedia.org/wiki/Stellar_classification. msg := []byte(`Oh Be A Fine Girl Kiss Me`) // hash the message you want to sign. // with defined hash function. h := signer.HashFunc().New() h.Write(msg) digest := h.Sum(nil) // Sign the digest signature, err := signer.SignContext(ctx, nil, digest, nil) if err != nil { // TODO: Handle error panic(err) } // Verify the signature err = cryptokms.VerifyDigestSignature(signer.Public(), signer.HashFunc(), digest, signature) if err != nil { // TODO: Handle error panic(err) } fmt.Printf("Digest : %s\n", hex.EncodeToString(digest)) fmt.Printf("Signature: Verified\n") }
Output: Digest : 381d492615cee4337ef441d9fb2e3682c0306fb99b82ff966af4cc5dc8db61b7 Signature: Verified
func (*Signer) SignContext ¶
func (s *Signer) SignContext(ctx context.Context, _ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)
SignContext signs the given digest with asymmetric key. The random parameter is ignored, and thus it can be as nil and is always set to crypto/rand.Reader.
Click to show internal directories.
Click to hide internal directories.