Last active
May 31, 2019 21:07
-
-
Save DazWilkin/cd49c35e7b53e699919bf5fcf710026b to your computer and use it in GitHub Desktop.
Google Cloud KMS & Tink
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 characters
package main | |
import ( | |
"encoding/base64" | |
"flag" | |
"fmt" | |
"log" | |
"github.com/google/tink/go/aead" | |
"github.com/google/tink/go/core/registry" | |
"github.com/google/tink/go/integration/gcpkms" | |
"github.com/google/tink/go/keyset" | |
) | |
var ( | |
project = flag.String("project", "", "Project ID") | |
location = flag.String("location", "", Keyring Location") | |
keyring = flag.String("keyring", "", "Keyring ID") | |
key = flag.String("key", "", "Key ID") | |
) | |
func main() { | |
flag.Parse() | |
keyURI := fmt.Sprintf( | |
"gcp-kms://projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", | |
*project, | |
*location, | |
*keyring, | |
*key) | |
gcpclient, err := gcpkms.NewGCPClient(keyURI) | |
if err != nil { | |
log.Fatal(err) | |
} | |
//_, err = gcpclient.LoadCredentials(*credentialsFile) | |
_, err = gcpclient.LoadDefaultCredentials() | |
if err != nil { | |
log.Fatal(err) | |
} | |
registry.RegisterKMSClient(gcpclient) | |
dek := aead.AES128CTRHMACSHA256KeyTemplate() | |
kh, err := keyset.NewHandle(aead.KMSEnvelopeAEADKeyTemplate(keyURI, dek)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
a, err := aead.New(kh) | |
if err != nil { | |
log.Fatal(err) | |
} | |
ct, err := a.Encrypt([]byte("manifest"), []byte("associated data")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pt, err := a.Decrypt(ct, []byte("associated data")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Cipher text: \n%s\n\n", base64.StdEncoding.EncodeToString(ct)) | |
fmt.Printf("Plain text: %s\n", pt) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment