tor-dam

tor distributed announce mechanism (not a dht)
git clone https://git.parazyd.org/tor-dam
Log | Files | Refs | README | LICENSE

crypto.go (1623B)


      1 package main
      2 
      3 /*
      4  * Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org>
      5  *
      6  * This file is part of tor-dam
      7  *
      8  * This program is free software: you can redistribute it and/or modify
      9  * it under the terms of the GNU Affero General Public License as published by
     10  * the Free Software Foundation, either version 3 of the License, or
     11  * (at your option) any later version.
     12  *
     13  * This program is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  * GNU Affero General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Affero General Public License
     19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     20  */
     21 
     22 import (
     23 	"crypto/ed25519"
     24 	"crypto/rand"
     25 	"encoding/base64"
     26 	"io/ioutil"
     27 	"log"
     28 	"os"
     29 	"strings"
     30 )
     31 
     32 func generateED25519Keypair(dir string) error {
     33 	_, sk, err := ed25519.GenerateKey(rand.Reader)
     34 	if err != nil {
     35 		return err
     36 	}
     37 
     38 	if err := os.MkdirAll(dir, 0700); err != nil {
     39 		return err
     40 	}
     41 
     42 	seedpath := strings.Join([]string{dir, seedName}, "/")
     43 
     44 	log.Println("Writing ed25519 key seed to", seedpath)
     45 	return ioutil.WriteFile(seedpath,
     46 		[]byte(base64.StdEncoding.EncodeToString(sk.Seed())), 0600)
     47 }
     48 
     49 func loadED25519Seed(file string) (ed25519.PrivateKey, error) {
     50 	log.Println("Reading ed25519 seed from", file)
     51 
     52 	data, err := ioutil.ReadFile(file)
     53 	if err != nil {
     54 		return nil, err
     55 	}
     56 
     57 	dec, err := base64.StdEncoding.DecodeString(string(data))
     58 	if err != nil {
     59 		return nil, err
     60 	}
     61 
     62 	return ed25519.NewKeyFromSeed(dec), nil
     63 }