Esempio n. 1
0
// Used for tests. Everytime its load key from file.
func RSAEncrypt(data []byte) ([]byte, error) {
	pathToKey, cfgErr := cfg.GetStr(cfg.STR_KEYS_PATH)
	if cfgErr != nil {
		return nil, cfgErr
	}

	pemData, err := ioutil.ReadFile(pathToKey + "public_key.pem")
	if err != nil {
		return nil, err
	}
	// Extract the PEM-encoded data block
	block, _ := pem.Decode(pemData)
	if block == nil {
		return nil, errors.New("Empty block.")
	}

	// Decode the RSA public key
	key, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return nil, err
	}
	pkey := key.(*rsa.PublicKey)
	// Encrypt the data
	out, err := rsa.EncryptPKCS1v15(rand.Reader, pkey, data)
	if err != nil {
		return nil, err
	}

	return out, nil
}
Esempio n. 2
0
func connectToDB() error {
	var err error

	user, _ = cfg.GetStr(cfg.STR_DB_USER)
	pass, _ = cfg.GetStr(cfg.STR_DB_PASS)
	host, _ = cfg.GetStr(cfg.STR_DB_HOST)
	port, _ = cfg.GetStr(cfg.STR_DB_PORT)
	dbName, _ = cfg.GetStr(cfg.STR_DB_NAME)

	db, err = sql.Open("mysql", user+":"+pass+"@tcp("+host+":"+port+")/"+dbName+"?parseTime=true")
	if err != nil {
		return err
	}

	return nil
}
Esempio n. 3
0
// Should be run only once.
func loadPrivateKey() error {
	pathToKey, cfgErr := cfg.GetStr(cfg.STR_KEYS_PATH)
	if cfgErr != nil {
		return cfgErr
	}

	pemData, err := ioutil.ReadFile(pathToKey + "private_key.pem")
	if err != nil {
		return err
	}

	block, _ := pem.Decode(pemData)
	if block == nil {
		return errors.New("Pem block is empty.")
	}
	if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
		return errors.New("Invalid pem block.")
	}

	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		return errors.New("Bad private key.")
	}

	privateKey = priv

	return nil
}
Esempio n. 4
0
func sendAppleMsg(device string, msg *models.PushMessage) error {
	pathToKey, cfgErr := cfg.GetStr(cfg.STR_KEYS_PATH)
	if cfgErr != nil {
		return cfgErr
	}

	apn, err := apns.NewClient("gateway.sandbox.push.apple.com:2195", pathToKey+"cert.pem", pathToKey+"key-noenc.pem")
	if err != nil {
		return err
	}

	apn.MAX_PAYLOAD_SIZE = 2048

	payload := &ApplePayload{}
	payload.Aps.Alert = msg.Message
	payload.Aps.Title = "MastersLunch"
	payload.Aps.Sound = "bingbong.aiff"
	payload.Aps.Badge = 42

	bytes, _ := json.Marshal(payload)

	fmt.Printf("Notification sender debug: %v\n", string(bytes))
	fmt.Printf("Notification sender debug: Trying to send Apple device notifications to %v\n", device)

	err = apn.SendPayloadString(device, bytes, 5)
	if err != nil {
		fmt.Printf("Notification sender !!!ERROR: Sending notification to Apple device failed: %s\n", err.Error())
	}

	return err
}