Git Product home page Git Product logo

ecc25519's Introduction

ecc25519

combine golang ed25519 and curve25519 libray in one

ed25519 folder is from https://github.com/agl/ed25519

curve25519 folder is from https://github.com/golang/crypto/tree/master/curve25519

This Libray implement ed25519 and curve25519 in same private key and public key

package ecc25519

import(
	"github.com/golang/go/src/pkg/encoding/hex"
	"strings"
	"fmt"
	"testing"
)

var curve Curve;
var src string = "You can always define a function in Go to do what you want, than assign the function to console.log in javascript.";
var srcdata []byte;

func Test_All(t *testing.T)  {
	CreateKey();
	DataSign()
	DataCrypt();

	SetExistKey();
	DataSign()
	DataCrypt();
}
func CreateKey()  {
	fmt.Println("create a new pair key")
	curve.MakeKey();
	fmt.Println("private key(32 bytes):",curve.GetPrivateString());
	curve.GetPublicString();
	fmt.Println("public key(32 bytes):",curve.GetPublicString());
}
func SetExistKey()  {
	//set a exist key for curve
	keys := "0824E6110F5E0BD6500855C4CF48BD15BB435175D34DC472BED58605634BDD7BDE0C2B412AB884AB9678791CF043ACD8A55F8DC5488A84C7B94E731F7F206D32"
	curve.SetKeyString(keys);
}
func DataSign() {
	//sign and verfy
	sign := curve.Sign(srcdata);
	if(curve.Verify(sign,srcdata)){
		fmt.Println("verify success")
	}else{
		fmt.Println("verify failed")
	}
	//modify sign data
	sign[20] = 3;
	if(curve.Verify(sign,srcdata)){
		fmt.Println("verify success")
	}else{
		fmt.Println("verify failed")
	}
}

func DataCrypt()  {
	srcdata = []byte(src);
	//encrypt data length at most 64 bytes
	var data []byte;
	if len(srcdata)>64 {
		data = srcdata[:64]
	}else{
		data = srcdata;
	}
	//begin encrypt
	enc,err := curve.Encrypt(data)
	if err!=nil{
		fmt.Println(err);
		return;
	}
	//print ciphertext to hex
	PrintHex("ciphertext:",enc);
	//begin decrypt
	dec,err := curve.Decrypt(enc)
	if err!=nil{
		fmt.Println(err);
	}
	//print plaintext
	fmt.Println("plaintext:",string(dec));
}
func PrintHex(flag string,data []byte)  {
	hex := hex.EncodeToString(data);
	hex = strings.ToUpper(hex);
	fmt.Println(flag,hex);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.