Пример #1
0
func (l *localStorage) Store(data []byte) (string, error) {
	var ch []byte
	var err error
	if len(data) > 120 {
		ch, err = multihash.EncodeName(data[:120], "sha1")
		if err != nil {
			return "", err
		}
	} else {
		ch, err = multihash.EncodeName(data, "sha1")
		if err != nil {
			return "", err
		}
	}

	can := hex.EncodeToString(ch)
	if _, err := os.Stat("login"); os.IsNotExist(err) {
		os.Mkdir("login", 0777)
	}
	filename := "login/" + can
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		file, err := os.Create(filename)
		if err != nil {
			return "", err
		}
		defer file.Close()
		_, err = file.Write(data)
		if err != nil {
			return "", err
		}

		file.Sync()
		return can, nil
	} else {
		return "", errors.New("File Exists")
	}

	return "", nil
}
Пример #2
0
/**
* Generates a new, random peer identity.
*
* @return {Peer}
 */
func NewIdentity(cfg *config.Identity) (*Peer, error) {
	// Select curve given in config.
	var curve elliptic.Curve

	switch cfg.Curve {
	case 224:
		curve = elliptic.P224()
	case 256:
		curve = elliptic.P256()
	case 384:
		curve = elliptic.P384()
	case 521:
		curve = elliptic.P521()
	default:
		return nil, errors.New("Bad curve name in config.")
	}

	var (
		cSize       int = curve.Params().BitSize
		pubKeySize      = base64.StdEncoding.EncodedLen(cSize / 2)
		privKeySize     = base64.StdEncoding.EncodedLen(cSize / 4)
		bound           = cSize / 8

		pubKey []byte

		priv        []byte
		signingPriv *ecdsa.PrivateKey
		err         error
	)

	if len(cfg.PubKey) == pubKeySize && len(cfg.PrivKey) == privKeySize {
		// Parse saved keypair from config.

		pubKey, err = base64.StdEncoding.DecodeString(cfg.PubKey)
		if err != nil {
			return nil, err
		}

		decodedPriv, err := base64.StdEncoding.DecodeString(cfg.PrivKey)
		if err != nil {
			return nil, err
		}

		signingX := big.NewInt(0)
		signingY := big.NewInt(0)
		signingD := big.NewInt(0)

		signingX.SetBytes(pubKey[bound*2 : bound*3])
		signingY.SetBytes(pubKey[bound*3:])

		priv = decodedPriv[0:bound]
		signingD.SetBytes(decodedPriv[bound:])

		signingPriv = &ecdsa.PrivateKey{
			PublicKey: ecdsa.PublicKey{
				Curve: curve,
				X:     signingX,
				Y:     signingY,
			},
			D: signingD,
		}
	} else {
		var buff bytes.Buffer

		// Generate a random ECDH keypair.
		priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
		if err != nil {
			return nil, err
		}

		// Generate a random ECDSA keypair.
		signingPriv, err = ecdsa.GenerateKey(curve, rand.Reader)
		if err != nil {
			return nil, err
		}

		// Generate pubKey
		buff.Write(x.Bytes())
		buff.Write(y.Bytes())

		buff.Write(signingPriv.PublicKey.X.Bytes())
		buff.Write(signingPriv.PublicKey.Y.Bytes())

		pubKey = buff.Bytes()

		// Generate privKey
		var privKey bytes.Buffer
		privKey.Write(priv)
		privKey.Write(signingPriv.D.Bytes())

		// Save to config. ---- TO DO ?
		cfg.PubKey = base64.StdEncoding.EncodeToString(pubKey)
		cfg.PrivKey = base64.StdEncoding.EncodeToString(privKey.Bytes())
	}

	// Generate peer ID
	hash := sha1.New() // THIS NEEDS TO BE SHA256
	hash.Write(pubKey)

	id, err := mh.EncodeName(hash.Sum(nil), "sha1")
	if err != nil {
		return nil, err
	}

	return &Peer{
		id,
		pubKey,
		[]*ma.Multiaddr{},
		curve,
		priv,
		signingPriv,
	}, nil
}