Skip to content

Commit ec10019

Browse files
authored
Merge pull request #6 from scaleway/doc/full-examples
Add a full example
2 parents 8a963a8 + 3467c08 commit ec10019

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,4 @@ The official documentation is available at https://developers.google.com/tink.
88
## Getting Started
99

1010
1. Create a Key in Scaleway's Key Manager and retrieve its ID.
11-
2. Check the [example](./integration/scwkms/scw_kms_client_test.go) to use the
12-
extension. Make sure to replace the client fields with your own
13-
configuration.
14-
3. Rather than configuring the provider in-app, you can also use Scaleway's
15-
configuration file and environment variables. Check [Scaleway's configuration
16-
documentation](https://www.scaleway.com/en/docs/developer-tools/scaleway-cli/reference-content/scaleway-configuration-file/)
17-
for more details.
11+
2. Once you have a key ID, you can reuse a full working example in `examples/` to get started.

examples/encrypt_decrypt/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module main
2+
3+
go 1.22.3
4+
5+
require (
6+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27
7+
github.com/scaleway/tink-go-scwkms v0.1.0
8+
github.com/tink-crypto/tink-go/v2 v2.2.0
9+
)
10+
11+
require (
12+
golang.org/x/crypto v0.23.0 // indirect
13+
golang.org/x/sys v0.20.0 // indirect
14+
google.golang.org/protobuf v1.34.1 // indirect
15+
gopkg.in/yaml.v2 v2.4.0 // indirect
16+
)

examples/encrypt_decrypt/go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27 h1:yGAraK1uUjlhSXgNMIy8o/J4LFNcy7yeipBqt9N9mVg=
4+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
5+
github.com/scaleway/tink-go-scwkms v0.1.0 h1:fp/g+9PzuZzFXn9JdVmGU163G5edLKb/w+dZdg3x7WA=
6+
github.com/scaleway/tink-go-scwkms v0.1.0/go.mod h1:3mfuRK8omCRCDgViq82RPcPEPeXSxPelNh2zKH6mugw=
7+
github.com/tink-crypto/tink-go/v2 v2.2.0 h1:L2Da0F2Udh2agtKztdr69mV/KpnY3/lGTkMgLTVIXlA=
8+
github.com/tink-crypto/tink-go/v2 v2.2.0/go.mod h1:JJ6PomeNPF3cJpfWC0lgyTES6zpJILkAX0cJNwlS3xU=
9+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
10+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
11+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
12+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
14+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
18+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

examples/encrypt_decrypt/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)