// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name main.go // Created by DEV // Modified 16/01/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package main import ( "activething.com/go/gogo/core/bins/bfields" "activething.com/go/gogo/core/results" "activething.com/go/gogo/packs/pjson" "crypto/aes" "crypto/cipher" "crypto/rand" "fmt" "io" ) type A struct { CampoInt int `pack:"campo,q,e"` Cadena string `json:"cadena,e,b64"` Seguro string `pack:"seguro,e"` Arrays []byte `pack:"arr,b64"` } type encrypter struct { key []byte } func (e encrypter) Encrypt (data []byte) results.Bytes { b,er := aes.NewCipher(e.key) ct := make([]byte,aes.BlockSize+len(data)) iv := ct[:aes.BlockSize] if _, er = io.ReadFull(rand.Reader, iv); er != nil { return results.DoBytesError(fmt.Errorf("could not encrypt: %v", er)) } st := cipher.NewCFBEncrypter(b,iv) st.XORKeyStream(ct[aes.BlockSize:],data) return results.DoBytesSuccess(ct) } func (e encrypter) Decrypt (data string) (string,error) { ct := []byte(data) block, err := aes.NewCipher(e.key) if err != nil { return "", fmt.Errorf("could not create new cipher: %v", err) } if len(ct) < aes.BlockSize { return "", fmt.Errorf("invalid ciphertext block size") } iv := ct[:aes.BlockSize] ct = ct[aes.BlockSize:] stream := cipher.NewCFBDecrypter(block, iv) stream.XORKeyStream(ct, ct) return string(ct), nil } func main() { en := encrypter{key: []byte{0,1,2,3,45,6,6,7,8,9,0,1,1,1,1,1}} pk := pjson.NewPacker(pjson.WithEncrypter(en)) //pk := pjson.NewPacker() t := A { CampoInt: 23, Cadena: "Esto es una cadena", Seguro: "Esto debe de ir encruptdao", Arrays: []byte{233,222,221}, } /* pk.PackList(func(p packs.PackListBuilder, flags packs.Flags){ p.ItemUInt64(0,flags) p.ItemFloat64(7.8,flags) p.ItemList(func (p1 packs.PackListBuilder, f packs.Flags){ p1.ItemString("MICasa",flags) p1.ItemString("Coche",flags) },flags) p.ItemObject(func (b packs.PackObjectBuilder, f packs.Flags) { b.FieldString("field1","Esto es un campo",f) b.FieldDate("field2",time.Now(),f) },flags) p.ItemMap(func (b packs.PackMapBuilder,f packs.Flags){ b.KeyValueString(2,"Una cosita",0) b.KeyValueInt64(3.4,5,0) },flags) },0) errs.Test(pk.ErrorResult(),true) fmt.Printf("Paquete %s\n",pk.Packet()) */ pk.PackAny(t,0) //errs.Test(pk.Cause(),true) fmt.Printf("Paquete %s\n",pk.Packet()) res := en.Encrypt([]byte("Esto debe de ir encruptdao")) if res.IsError() { panic(res.Cause()) } dt := string(res.Success()) fmt.Printf("seguro %s\n",dt) st,er := en.Decrypt(dt) if er != nil { panic(er) } fmt.Printf("decrupt %s\n",st) f,_ := bfields.NewBField([]byte("MiNombre"),[]byte("221100")) fmt.Printf("field :%s\n",f.String()) }