2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-25 21:28:16 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package encryption_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/encryption"
|
2019-07-05 09:36:35 +01:00
|
|
|
"storj.io/storj/pkg/paths"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleEncryptPath() {
|
2019-07-05 09:36:35 +01:00
|
|
|
path := paths.NewUnencrypted("fold1/fold2/fold3/file.txt")
|
2018-10-25 21:28:16 +01:00
|
|
|
|
|
|
|
// seed
|
|
|
|
seed := new(storj.Key)
|
|
|
|
for i := range seed {
|
|
|
|
seed[i] = byte(i)
|
|
|
|
}
|
|
|
|
fmt.Printf("root key (%d bytes): %s\n", len(seed), hex.EncodeToString(seed[:]))
|
|
|
|
|
2019-07-05 09:36:35 +01:00
|
|
|
store := encryption.NewStore()
|
|
|
|
store.SetDefaultKey(seed)
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
// use the seed for encrypting the path
|
2019-07-05 09:36:35 +01:00
|
|
|
encryptedPath, err := encryption.EncryptPath("bucket", path, storj.EncAESGCM, store)
|
2018-10-25 21:28:16 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("path to encrypt:", path)
|
|
|
|
fmt.Println("encrypted path: ", encryptedPath)
|
|
|
|
|
|
|
|
// decrypting the path
|
2019-07-05 09:36:35 +01:00
|
|
|
decryptedPath, err := encryption.DecryptPath("bucket", encryptedPath, storj.EncAESGCM, store)
|
2018-10-25 21:28:16 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("decrypted path: ", decryptedPath)
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// root key (32 bytes): 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
|
|
|
|
// path to encrypt: fold1/fold2/fold3/file.txt
|
2019-07-05 09:36:35 +01:00
|
|
|
// encrypted path: OHzjTiBUvLmgQouCAYdu74MlqDl791aOka_EBzlAb_rR/RT0pG5y4lHFVRi1sHtwjZ1B7DeVbRvpyMfO6atfOefSC/rXJX6O9Pk4rGtnlLUIUoc9Gz0y6N-xemdNyAasbo3dQm/qiEo3IYUlA989mKFE7WB98GHJK88AI98hhUgwv39ePexslzg
|
2018-10-25 21:28:16 +01:00
|
|
|
// decrypted path: fold1/fold2/fold3/file.txt
|
|
|
|
}
|