This commit is contained in:
Diyath Sahan Rajapakshe 2023-11-13 17:30:27 +05:30 committed by GitHub
commit 4dca59dbd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 46 deletions

47
go.mod
View File

@ -1,30 +1,37 @@
module github.com/ipfs-key module github.com/whyrusleeping/ipfs-key
go 1.17 go 1.17
require github.com/libp2p/go-libp2p-core v0.16.1 require github.com/libp2p/go-libp2p-core v0.20.1
require ( require (
github.com/btcsuite/btcd v0.22.1 // indirect github.com/btcsuite/btcd v0.23.4 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/ipfs/go-cid v0.0.7 // indirect github.com/ipfs/go-cid v0.4.1 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.32.1 // indirect
github.com/libp2p/go-openssl v0.1.0 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.4.1 // indirect github.com/multiformats/go-multiaddr v0.12.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.4.1 // indirect github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.0.14 // indirect github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect golang.org/x/crypto v0.15.0 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/sys v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
) )

51
main.go
View File

@ -1,20 +1,21 @@
package main package main
import ( import (
// Update the import path for crypto
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
crp "github.com/libp2p/go-libp2p-core/crypto" crp "github.com/libp2p/go-libp2p/core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer" peer "github.com/libp2p/go-libp2p/core/peer"
) )
func main() { func main() {
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate") size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
typ := flag.String("type", "", "select type of key to generate (RSA, Ed25519, Secp256k1 or ECDSA)") typ := flag.String("type", "", "select the type of key to generate (RSA, Ed25519, Secp256k1, or ECDSA)")
key := flag.String("key", "", "specify the location of the key to decode it's peerID") key := flag.String("key", "", "specify the location of the key to decode its peer ID")
flag.Parse() flag.Parse()
@ -42,69 +43,67 @@ func readKey(keyLoc *string, typ *string) error {
fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc) fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc)
var unmarshalPrivateKeyFucn func(data []byte) (crp.PrivKey, error) var unmarshalPrivateKeyFunc func(data []byte) (crp.PrivKey, error)
// rsa and ed25519 unmarshalPrivateKeyFucn are for backward compatibility
// for keys saved with raw(), to read such keys, specify the key type
switch strings.ToLower(*typ) { switch strings.ToLower(*typ) {
case "rsa": case "rsa":
unmarshalPrivateKeyFucn = crp.UnmarshalRsaPrivateKey unmarshalPrivateKeyFunc = crp.UnmarshalRsaPrivateKey
case "ed25519": case "ed25519":
unmarshalPrivateKeyFucn = crp.UnmarshalEd25519PrivateKey unmarshalPrivateKeyFunc = crp.UnmarshalEd25519PrivateKey
default: default:
unmarshalPrivateKeyFucn = crp.UnmarshalPrivateKey unmarshalPrivateKeyFunc = crp.UnmarshalPrivateKey
} }
prvk, err := unmarshalPrivateKeyFucn(data) prvKey, err := unmarshalPrivateKeyFunc(data)
if err != nil { if err != nil {
return err return err
} }
id, err := peer.IDFromPrivateKey(prvk) id, err := peer.IDFromPrivateKey(prvKey)
if err != nil { if err != nil {
return err return err
} }
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvk.Type().String(), id.Pretty()) fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvKey.Type().String(), id)
return err return nil
} }
func genKey(typ *string, size *int) error { func genKey(typ *string, size *int) error {
var atyp int var keyType int
switch strings.ToLower(*typ) { switch strings.ToLower(*typ) {
case "rsa": case "rsa":
atyp = crp.RSA keyType = crp.RSA
case "ed25519": case "ed25519":
atyp = crp.Ed25519 keyType = crp.Ed25519
case "secp256k1": case "secp256k1":
atyp = crp.Secp256k1 keyType = crp.Secp256k1
case "ecdsa": case "ecdsa":
atyp = crp.ECDSA keyType = crp.ECDSA
default: default:
return fmt.Errorf("unrecognized key type: %s", *typ) return fmt.Errorf("unrecognized key type: %s", *typ)
} }
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ) fmt.Fprintf(os.Stderr, "Generating a %d-bit %s key...\n", *size, *typ)
priv, pub, err := crp.GenerateKeyPair(atyp, *size) prvKey, pubKey, err := crp.GenerateKeyPair(keyType, *size)
if err != nil { if err != nil {
return err return err
} }
pid, err := peer.IDFromPublicKey(pub) id, err := peer.IDFromPublicKey(pubKey)
if err != nil { if err != nil {
return err return err
} }
data, err := crp.MarshalPrivateKey(priv) data, err := crp.MarshalPrivateKey(prvKey)
if err != nil { if err != nil {
return err return err
} }
_, err = os.Stdout.Write(data) _, err = os.Stdout.Write(data)
if err != nil { if err != nil {
return nil return err
} }
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for generated key: %s\n", pid.Pretty()) fmt.Fprintf(os.Stderr, "Success!\nID for the generated key: %s\n", id)
return err return nil
} }