examples: turn paths binary to Go documentation example (#405)
* examples: turn paths binary to Go documentation example * remove bip39 dependency
This commit is contained in:
parent
764234109f
commit
c3a1d71616
@ -1,68 +0,0 @@
|
||||
// Copyright (C) 2018 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/tyler-smith/go-bip39"
|
||||
|
||||
"storj.io/storj/pkg/paths"
|
||||
)
|
||||
|
||||
const mnemonic = "style inspire blade just ignore expose midnight maze " +
|
||||
"boring code burst host giraffe face parent basic ritual distance " +
|
||||
"trophy join relief hidden fine yard"
|
||||
|
||||
var path = paths.New("fold1/fold2/fold3/file.txt")
|
||||
|
||||
func main() {
|
||||
err := Main()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Main is the exported CLI executable function
|
||||
func Main() error {
|
||||
fmt.Println("mnemonic:", mnemonic)
|
||||
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("root key (%d bytes): %s\n", len(seed),
|
||||
hex.EncodeToString(seed))
|
||||
encryptedPath, err := path.Encrypt(seed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("path to encrypt:", path)
|
||||
fmt.Println("encrypted path: ", encryptedPath)
|
||||
decryptedPath, err := encryptedPath.Decrypt(seed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("decrypted path: ", decryptedPath)
|
||||
sharedPath := encryptedPath[2:]
|
||||
fmt.Println("shared path: ", encryptedPath[2:])
|
||||
derivedKey, err := decryptedPath.DeriveKey(seed, 2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("derived key (%d bytes): %s\n", len(derivedKey),
|
||||
hex.EncodeToString(derivedKey))
|
||||
decryptedPath, err = sharedPath.Decrypt(derivedKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("decrypted path: ", decryptedPath)
|
||||
|
||||
// implement Bytes() function
|
||||
var pathBytes = path.Bytes()
|
||||
fmt.Println("path in Bytes is: ", pathBytes)
|
||||
return nil
|
||||
}
|
1
go.mod
1
go.mod
@ -96,7 +96,6 @@ require (
|
||||
github.com/stretchr/testify v1.2.2
|
||||
github.com/tidwall/gjson v1.1.3 // indirect
|
||||
github.com/tidwall/match v0.0.0-20171002075945-1731857f09b1 // indirect
|
||||
github.com/tyler-smith/go-bip39 v0.0.0-20160629163856-8e7a99b3e716
|
||||
github.com/vivint/infectious v0.0.0-20180906161625-e155e6eb3575
|
||||
github.com/yuin/gopher-lua v0.0.0-20180918061612-799fa34954fb // indirect
|
||||
github.com/zeebo/admission v0.0.0-20180821192747-f24f2a94a40c
|
||||
|
2
go.sum
2
go.sum
@ -273,8 +273,6 @@ github.com/tidwall/gjson v1.1.3/go.mod h1:c/nTNbUr0E0OrXEhq1pwa8iEgc2DOt4ZZqAt1H
|
||||
github.com/tidwall/match v0.0.0-20171002075945-1731857f09b1 h1:pWIN9LOlFRCJFqWIOEbHLvY0WWJddsjH2FQ6N0HKZdU=
|
||||
github.com/tidwall/match v0.0.0-20171002075945-1731857f09b1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
github.com/tyler-smith/go-bip39 v0.0.0-20160629163856-8e7a99b3e716 h1:dSJdbB9MzZW7Fu9ayPGtM3hPRzbTbSZWvtHunBdRmao=
|
||||
github.com/tyler-smith/go-bip39 v0.0.0-20160629163856-8e7a99b3e716/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
|
||||
github.com/vivint/infectious v0.0.0-20180906161625-e155e6eb3575 h1:t0v1w3EiqMhDYBFbzwSfUHivx3yOMJG7R7YUm1Amlh8=
|
||||
github.com/vivint/infectious v0.0.0-20180906161625-e155e6eb3575/go.mod h1:5oyMAv4hrBEKqBwORFsiqIrCNCmL2qcZLQTdJLYeYIc=
|
||||
github.com/yuin/gopher-lua v0.0.0-20180918061612-799fa34954fb h1:Jmfk7z2f/+gxVFAgPsJMuczO1uEIxZy6wytTdeZ49lg=
|
||||
|
66
pkg/paths/examples_test.go
Normal file
66
pkg/paths/examples_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2018 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package paths_test
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"storj.io/storj/pkg/paths"
|
||||
)
|
||||
|
||||
func ExamplePath_Encrypt() {
|
||||
var path = paths.New("fold1/fold2/fold3/file.txt")
|
||||
|
||||
// seed
|
||||
seed := make([]byte, 64)
|
||||
for i := range seed {
|
||||
seed[i] = byte(i)
|
||||
}
|
||||
fmt.Printf("root key (%d bytes): %s\n", len(seed), hex.EncodeToString(seed))
|
||||
|
||||
// use the seed for encrypting the path
|
||||
encryptedPath, err := path.Encrypt(seed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("path to encrypt:", path)
|
||||
fmt.Println("encrypted path: ", encryptedPath)
|
||||
|
||||
// decrypting the path
|
||||
decryptedPath, err := encryptedPath.Decrypt(seed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("decrypted path: ", decryptedPath)
|
||||
|
||||
// handling of shared path
|
||||
sharedPath := encryptedPath[2:]
|
||||
fmt.Println("shared path: ", encryptedPath[2:])
|
||||
derivedKey, err := decryptedPath.DeriveKey(seed, 2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("derived key (%d bytes): %s\n", len(derivedKey), hex.EncodeToString(derivedKey))
|
||||
decryptedPath, err = sharedPath.Decrypt(derivedKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("decrypted path: ", decryptedPath)
|
||||
|
||||
// implement Bytes() function
|
||||
var pathBytes = path.Bytes()
|
||||
fmt.Println("path in Bytes is: ", pathBytes)
|
||||
|
||||
// Output:
|
||||
// root key (64 bytes): 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f
|
||||
// path to encrypt: fold1/fold2/fold3/file.txt
|
||||
// encrypted path: 1ziIKg8Mw9_ywBqSOm78gQ9aQVbrQ/1FWdS4WUzuWXBrNML_FzGH8O2usos/1TKa8xYYCrUBEvEGj7YENdwViZOYh/17JWesXCBVU9Nx8IdnwuNUuwqgGFKL_jH
|
||||
// decrypted path: fold1/fold2/fold3/file.txt
|
||||
// shared path: 1TKa8xYYCrUBEvEGj7YENdwViZOYh/17JWesXCBVU9Nx8IdnwuNUuwqgGFKL_jH
|
||||
// derived key (64 bytes): 2592f0694bc72a2719d09b7192b9b8f9e2517fda71419314d93a7c96844f28763ed3b829f3c9a09e812b402a66b1b0a832ae3cdd7435b7b496ca95eec108c629
|
||||
// decrypted path: fold3/file.txt
|
||||
// path in Bytes is: [102 111 108 100 49 47 102 111 108 100 50 47 102 111 108 100 51 47 102 105 108 101 46 116 120 116]
|
||||
}
|
Loading…
Reference in New Issue
Block a user