arnaucube
arnaucube/blockchainIDsystemPublic Self-hosted repository
blockchainIDsystem/clientApp/clientAppRESTFunctions.go
clientApp/clientAppRESTFunctions.goRaw
  1package main
  2
  3import (
  4	"bytes"
  5	"encoding/json"
  6	"fmt"
  7	"net/http"
  8	"time"
  9
 10	ownrsa "./ownrsa"
 11	"github.com/fatih/color"
 12	"github.com/gorilla/mux"
 13)
 14
 15//generate key pair
 16//blind m
 17//unblind m
 18
 19func Index(w http.ResponseWriter, r *http.Request) {
 20	fmt.Fprintln(w, "serverIDsigner")
 21}
 22
 23func IDs(w http.ResponseWriter, r *http.Request) {
 24	//read the keys stored in /keys directory
 25	keys := readKeys("keys.json")
 26	saveKeys(keys, "keys.json")
 27
 28	jResp, err := json.Marshal(keys)
 29	check(err)
 30	fmt.Fprintln(w, string(jResp))
 31}
 32func NewID(w http.ResponseWriter, r *http.Request) {
 33	//generate RSA keys pair
 34	newKey := ownrsa.GenerateKeyPair()
 35
 36	key := ownrsa.PackKey(newKey)
 37	key.Date = time.Now()
 38	fmt.Println(key)
 39
 40	keys := readKeys("keys.json")
 41	keys = append(keys, key)
 42	saveKeys(keys, "keys.json")
 43
 44	jResp, err := json.Marshal(keys)
 45	check(err)
 46	fmt.Fprintln(w, string(jResp))
 47}
 48
 49type AskBlindSign struct {
 50	M string `json:"m"`
 51}
 52
 53func BlindAndVerify(w http.ResponseWriter, r *http.Request) {
 54	vars := mux.Vars(r)
 55	packPubK := vars["pubK"]
 56	color.Green(packPubK)
 57
 58	//read the keys stored in /keys directory
 59	keys := readKeys("keys.json")
 60
 61	var key ownrsa.RSA
 62	//search for complete key
 63	for _, k := range keys {
 64		fmt.Println(k.PubK)
 65		fmt.Println(packPubK)
 66		fmt.Println("")
 67		if k.PubK == packPubK {
 68			key = ownrsa.UnpackKey(k)
 69		}
 70	}
 71	//blind the key.PubK
 72	var m []int
 73	//convert packPubK to []bytes
 74	mBytes := []byte(packPubK)
 75	for _, byte := range mBytes {
 76		m = append(m, int(byte))
 77	}
 78	rVal := 101
 79	blinded := ownrsa.Blind(m, rVal, key.PubK, key.PrivK)
 80	fmt.Println(blinded)
 81
 82	//convert blinded to string
 83	var askBlindSign AskBlindSign
 84	askBlindSign.M = ownrsa.ArrayIntToString(blinded, "_")
 85
 86	//send to the serverIDsigner the key.PubK blinded
 87	color.Green(askBlindSign.M)
 88	body := new(bytes.Buffer)
 89	json.NewEncoder(body).Encode(askBlindSign)
 90	res, err := http.Post("http://"+config.ServerIDSigner.IP+":"+config.ServerIDSigner.Port+"/blindsign", "application/json", body)
 91	check(err)
 92	fmt.Println(res)
 93
 94	decoder := json.NewDecoder(res.Body)
 95	//var sigmaString string
 96	err = decoder.Decode(&askBlindSign)
 97	if err != nil {
 98		panic(err)
 99	}
100	defer r.Body.Close()
101
102	fmt.Println("sigmaString")
103	fmt.Println(askBlindSign)
104	sigma := ownrsa.StringToArrayInt(askBlindSign.M, "_")
105	fmt.Println(sigma)
106
107	//unblind the response
108	//TODO
109	//després de la blindsign response, demanar al serverIDsigner la pubK
110	//unblinded := ownrsa.Unblind(sigma, rVal, )
111
112	jResp, err := json.Marshal(keys)
113	check(err)
114	fmt.Fprintln(w, string(jResp))
115}