func Encryption(key, auth string, raw []byte, pad int) (*Encrypt, error) {
	dc, err := base64.URLEncoding.DecodeString(key)
	if err != nil {
		return nil, err
	}

	da, err := base64.URLEncoding.DecodeString(auth)
	if err != nil {
		return nil, err
	}

	salt, _ := salt(16)

	priv, x, y, err := serverKey()
	if err != nil {
		return nil, err
	}
	pub := elliptic.Marshal(elliptic.P256(), x, y)

	px, py := elliptic.Unmarshal(elliptic.P256(), dc)
	ix, _ := elliptic.P256().ScalarMult(px, py, priv)

	b := bytes.NewBufferString("Content-Encoding: auth")
	b.WriteByte(0)

	prk, err := hkdf(da, ix.Bytes(), b.Bytes(), 32)
	if err != nil {
		return nil, err
	}

	ctx := context(dc, pub)

	ci := info("aesgcm", ctx)
	ck, err := hkdf(salt, prk, ci, 16)
	if err != nil {
		return nil, err
	}

	ni := info("nonce", ctx)
	n, err := hkdf(salt, prk, ni, 12)
	if err != nil {
		return nil, err
	}

	bl, err := aes.NewCipher(ck)
	if err != nil {
		return nil, err
	}

	cp, err := cipher.NewGCM(bl)
	if err != nil {
		return nil, err
	}

	buf := padding(pad)
	buf.Write(raw)
	result := cp.Seal([]byte{}, n, buf.Bytes(), nil)
	return &Encrypt{
		Payload:    result,
		Salt:       salt,
		PublickKey: pub,
	}, nil
}
// Encryption return encrypted messsage for web push api.
Encryption return encrypted messsage for web push api.
// Encryption return encrypted messsage for web push api .
func Encryption(key, auth string, raw []byte, pad int) (*Encrypt, error) {
	dc, err := base64.URLEncoding.DecodeString(key)
	if err != nil {
		return nil, err
	}
	da, err := base64.URLEncoding.DecodeString(auth)
	if err != nil {
		return nil, err
	}
	salt, _ := salt(16)
	priv, x, y, err := serverKey()
	if err != nil {
		return nil, err
	}
	pub := elliptic.Marshal(elliptic.P256(), x, y)
	px, py := elliptic.Unmarshal(elliptic.P256(), dc)
	ix, _ := elliptic.P256().ScalarMult(px, py, priv)
	b := bytes.NewBufferString("Content-Encoding: auth")
	b.WriteByte(0)
	prk, err := hkdf(da, ix.Bytes(), b.Bytes(), 32)
	if err != nil {
		return nil, err
	}
	ctx := context(dc, pub)
	ci := info("aesgcm", ctx)
	ck, err := hkdf(salt, prk, ci, 16)
	if err != nil {
		return nil, err
	}
	ni := info("nonce", ctx)
	n, err := hkdf(salt, prk, ni, 12)
	if err != nil {
		return nil, err
	}
	bl, err := aes.NewCipher(ck)
	if err != nil {
		return nil, err
	}
	cp, err := cipher.NewGCM(bl)
	if err != nil {
		return nil, err
	}
	buf := padding(pad)
	buf.Write(raw)
	result := cp.Seal([]byte{}, n, buf.Bytes(), nil)
	return &Encrypt{
		Payload:    result,
		Salt:       salt,
		PublickKey: pub,
	}, nil
}
func Encryption ( key , auth string , raw [ ] byte , pad int ) ( * Encrypt , error ) { dc , err := base64 . URLEncoding . DecodeString ( key ) if err != nil { return nil , err } da , err := base64 . URLEncoding . DecodeString ( auth ) if err != nil { return nil , err } salt , _ := salt ( 16 ) priv , x , y , err := serverKey ( ) if err != nil { return nil , err } pub := elliptic . Marshal ( elliptic . P256 ( ) , x , y ) px , py := elliptic . Unmarshal ( elliptic . P256 ( ) , dc ) ix , _ := elliptic . P256 ( ) . ScalarMult ( px , py , priv ) b := bytes . NewBufferString ( " " ) b . WriteByte ( 0 ) prk , err := hkdf ( da , ix . Bytes ( ) , b . Bytes ( ) , 32 ) if err != nil { return nil , err } ctx := context ( dc , pub ) ci := info ( " " , ctx ) ck , err := hkdf ( salt , prk , ci , 16 ) if err != nil { return nil , err } ni := info ( " " , ctx ) n , err := hkdf ( salt , prk , ni , 12 ) if err != nil { return nil , err } bl , err := aes . NewCipher ( ck ) if err != nil { return nil , err } cp , err := cipher . NewGCM ( bl ) if err != nil { return nil , err } buf := padding ( pad ) buf . Write ( raw ) result := cp . Seal ( [ ] byte { } , n , buf . Bytes ( ) , nil ) return & Encrypt { Payload : result , Salt : salt , PublickKey : pub , } , nil }
Encryption return encrypted messsage for web push api.
Encryption return encrypted messsage for web push api .