|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 8 | + "github.com/scaleway/tink-go-scwkms/integration/scwkms" |
| 9 | + "github.com/tink-crypto/tink-go/v2/aead" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + keyURIPrefix = "scw-kms://regions/fr-par/keys/" // Replace with your region |
| 14 | + keyURI = keyURIPrefix + "00000000-0000-0000-0000-000000000000" // Replace with your key ID |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + |
| 19 | + // Setup a scw configuration as usual |
| 20 | + config, err := scw.LoadConfig() |
| 21 | + if err != nil { |
| 22 | + log.Fatal(err) |
| 23 | + } |
| 24 | + profile, err := config.GetActiveProfile() |
| 25 | + if err != nil { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + // Create the Tink Scaleway client |
| 30 | + kms, _ := scwkms.NewClientWithOptions( |
| 31 | + keyURIPrefix, |
| 32 | + scw.WithProfile(profile), |
| 33 | + scw.WithEnv(), |
| 34 | + ) |
| 35 | + |
| 36 | + kekAEAD, _ := kms.GetAEAD(keyURI) |
| 37 | + |
| 38 | + dekTemplate := aead.AES256GCMKeyTemplate() // Your DEK is an AES-256-GCM key |
| 39 | + primitive := aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD) |
| 40 | + |
| 41 | + // Use the primitive to encrypt and decrypt data |
| 42 | + plaintext := []byte("Hello, World!") |
| 43 | + associatedData := []byte("example KMS envelope AEAD encryption") |
| 44 | + |
| 45 | + fmt.Println("Plaintext:", string(plaintext)) |
| 46 | + |
| 47 | + ciphertext, err := primitive.Encrypt(plaintext, associatedData) |
| 48 | + if err != nil { |
| 49 | + log.Fatal(err) |
| 50 | + } |
| 51 | + fmt.Println("Ciphertext:", ciphertext) |
| 52 | + |
| 53 | + plaintextBack, err := primitive.Decrypt(ciphertext, associatedData) |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + fmt.Println("Plaintext again:", string(plaintextBack)) |
| 58 | + |
| 59 | +} |
0 commit comments