예제 #1
0
파일: web.go 프로젝트: LDLN/web-app
func createUser(username, password, skek string) bool {

	// hashed_password
	hashed_password := cryptoWrapper.HashPassword(username, password)

	// encrypt kek
	ps := []string{password, username, salt}
	key := []byte(string([]rune(strings.Join(ps, "-"))[0:32]))
	pkek := []byte(skek)
	encrypted_kek := hex.EncodeToString(cryptoWrapper.Encrypt(key, pkek))

	// generate rsa keypair for user
	size := 1024
	priv, err := rsa.GenerateKey(rand.Reader, size)
	if err != nil {
		revel.TRACE.Println("failed to generate key")
	}
	if bits := priv.N.BitLen(); bits != size {
		revel.TRACE.Println("key too short (%d vs %d)", bits, size)
	}
	pub, err := x509.MarshalPKIXPublicKey(&priv.PublicKey)
	rsa_public_string := hex.EncodeToString(pub)

	revel.TRACE.Println(priv)

	// encrypt rsa private keypair
	encrypted_rsa_private := hex.EncodeToString(cryptoWrapper.Encrypt(key, x509.MarshalPKCS1PrivateKey(priv)))

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// save user object
	dbu := session.DB("landline").C("Users")

	user_object_map := make(map[string]string)
	user_object_map["username"] = username
	user_object_map["hashed_password"] = hashed_password
	user_object_map["encrypted_kek"] = encrypted_kek
	user_object_map["encrypted_rsa_private"] = encrypted_rsa_private
	user_object_map["rsa_public"] = rsa_public_string

	err = dbu.Insert(user_object_map)
	if err != nil {
		panic(err)
	}

	return true
}
예제 #2
0
func (c SyncableObjects) CreateObjectAction(object_key string) revel.Result {

	revel.TRACE.Println(c.Params.Values)

	// build kv map
	key_values := make(map[string]interface{})

	// parse params and put into map
	for k, v := range c.Params.Values {
		if k != "object_key" {
			key_values[k] = v[0]
		}
	}
	revel.TRACE.Println(key_values)

	// convert map to json to string of json
	key_values_map, err := json.Marshal(key_values)
	if err != nil {
		revel.TRACE.Println(err)
	}
	key_values_string := string(key_values_map[:])
	revel.TRACE.Println(key_values_string)

	// encrypt json string
	kv_string_encrypted := hex.EncodeToString(cryptoWrapper.Encrypt([]byte(c.Session["kek"]), []byte(key_values_string)))
	revel.TRACE.Println(kv_string_encrypted)

	// test decrypt
	kv_hex, err := hex.DecodeString(kv_string_encrypted)
	if err != nil {
		revel.TRACE.Println(err)
	}
	kv_plain := string(cryptoWrapper.Decrypt([]byte(c.Session["kek"]), kv_hex))
	revel.TRACE.Println(kv_plain)

	// create object
	object_map := make(map[string]interface{})
	uuid, err := uuid.NewV4()
	object_map["uuid"] = uuid.String()
	object_map["object_type"] = object_key
	object_map["key_value_pairs"] = kv_string_encrypted
	object_map["time_modified_since_creation"] = float64(0)

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// insert into db
	dbc := session.DB("landline").C("SyncableObjects")
	err = dbc.Insert(object_map)
	if err != nil {
		panic(err)
	}

	// redirect
	c.Flash.Success("Object created")
	return c.Redirect(routes.SyncableObjects.ViewObject(object_key, object_map["uuid"].(string)))
}
예제 #3
0
파일: main.go 프로젝트: LDLN/serial-server
func main() {

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// connect to port
	c := &serial.Config{Name: "/dev/ttyS0", Baud: 38400}
	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal(err)
	}

	// write message
	n, err := s.Write([]byte("LDLN Serial is listening"))
	if err != nil {
		log.Fatal(err)
	}

	// read messages
	for {
		buf := make([]byte, 2048)
		n, err = s.Read(buf)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Incoming on serial: %q", buf[:n])

		// split by double pipes /
		// first part is user:pass
		// second part is object type
		// third part is key/value pairs, separated by pipes |
		// each key/value pair is seperated by :
		// i.e. user:pass/memo/timestamp:xxx|gps:xxx|humidity:xxx|temp:xxx|soilsensor:xxx|dfo10sensor:xxx|gyro:xxx
		// i.e. user:pass/object_type/timestamp:xxx|gps:xxx|humidity:xxx|temp:xxx|soilsensor:xxx|dfo10sensor:xxx|gyro:xxx

		// convert string to JSON to map[string]interface{}
		v := make(map[string]interface{})
		err := json.Unmarshal(buf[:n], &v)
		if err != nil {
			log.Printf("Not a JSON object")
		} else {

			// get auth
			username := v["username"].(string)
			password := v["password"].(string)

			// create object
			object_map := make(map[string]interface{})
			object_map["uuid"] = v["uuid"].(string)
			object_map["object_type"] = v["object_type"].(string)
			object_map["time_modified_since_creation"] = v["time_modified_since_creation"].(float64)

			// encrypted payload
			// object_map["key_value_pairs"] = v["key_value_pairs"].(string)

			// plaintext to be encrypted payload
			byt, err := json.Marshal(v["key_value_pairs_plaintext"].(map[string]interface{}))
			if err != nil {
				panic(err)
			}
			log.Printf(string(byt[:]))

			dek := cryptoWrapper.GetKeyFromUsernamePassword(username, password)
			ciphertext := cryptoWrapper.Encrypt(dek, byt)
			if ciphertext != nil {
				object_map["key_value_pairs"] = ciphertext

				// db insert
				mc := session.DB("landline").C("SyncableObjects")
				err = mc.Insert(object_map)
				if err != nil {
					panic(err)
				}
				log.Printf("Inserted object %q into database.", v["uuid"].(string))
			} else {
				s.Write([]byte("Encryption failed"))
				log.Printf("Encryption failed")
			}
		}
	}
}