github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/crypto/extra25519/convert_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package extra25519_test
    19  
    20  import (
    21  	"bytes"
    22  	"crypto/ed25519"
    23  	"encoding/hex"
    24  	"fmt"
    25  	"os"
    26  
    27  	"github.com/zntrio/harp/v2/pkg/sdk/security/crypto/extra25519"
    28  )
    29  
    30  func ExamplePrivateKeyToCurve25519() {
    31  	dumper := hex.Dumper(os.Stdout)
    32  
    33  	// really silly seed for reproduciability
    34  	seed := make([]byte, 32)
    35  
    36  	fmt.Println("seed:")
    37  	dumper.Write(seed)
    38  
    39  	_, private, err := ed25519.GenerateKey(bytes.NewReader(seed))
    40  	fatal(err)
    41  
    42  	fmt.Println("private ed25519:")
    43  	dumper.Write(private)
    44  
    45  	var curvPriv [32]byte
    46  	extra25519.PrivateKeyToCurve25519(&curvPriv, private)
    47  
    48  	fmt.Println("private curve25519:")
    49  	dumper.Write(curvPriv[:])
    50  
    51  	// Output:
    52  	// seed:
    53  	// 00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    54  	// 00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    55  	// private ed25519:
    56  	// 00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    57  	// 00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    58  	// 00000040  3b 6a 27 bc ce b6 a4 2d  62 a3 a8 d0 2a 6f 0d 73  |;j'....-b...*o.s|
    59  	// 00000050  65 32 15 77 1d e2 43 a6  3a c0 48 a1 8b 59 da 29  |e2.w..C.:.H..Y.)|
    60  	// private curve25519:
    61  	// 00000060  50 46 ad c1 db a8 38 86  7b 2b bb fd d0 c3 42 3e  |PF....8.{+....B>|
    62  	// 00000070  58 b5 79 70 b5 26 7a 90  f5 79 60 92 4a 87 f1 56  |X.yp.&z..y`.J..V|
    63  }
    64  
    65  func ExamplePublicKeyToCurve25519() {
    66  	dumper := hex.Dumper(os.Stdout)
    67  
    68  	// really silly seed for reproduciability
    69  	seed := make([]byte, 32)
    70  
    71  	fmt.Println("seed:")
    72  	dumper.Write(seed)
    73  
    74  	public, _, err := ed25519.GenerateKey(bytes.NewReader(seed))
    75  	fatal(err)
    76  
    77  	fmt.Println("public ed25519:")
    78  	dumper.Write(public)
    79  
    80  	var curvPub [32]byte
    81  	extra25519.PublicKeyToCurve25519(&curvPub, public)
    82  
    83  	fmt.Println("public curve25519:")
    84  	dumper.Write(curvPub[:])
    85  
    86  	// Output:
    87  	// seed:
    88  	// 00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    89  	// 00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    90  	// public ed25519:
    91  	// 00000020  3b 6a 27 bc ce b6 a4 2d  62 a3 a8 d0 2a 6f 0d 73  |;j'....-b...*o.s|
    92  	// 00000030  65 32 15 77 1d e2 43 a6  3a c0 48 a1 8b 59 da 29  |e2.w..C.:.H..Y.)|
    93  	// public curve25519:
    94  	// 00000040  5b f5 5c 73 b8 2e be 22  be 80 f3 43 06 67 af 57  |[.\s..."...C.g.W|
    95  	// 00000050  0f ae 25 56 a6 41 5e 6b  30 d4 06 53 00 aa 94 7d  |..%V.A^k0..S...}|
    96  }
    97  
    98  func fatal(err error) {
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  }