You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.6 KiB

// ------------------------------------------------------------------------
// 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/errs"
"activething.com/go/gogo/core/results"
"activething.com/go/gogo/packs/pjson"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"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.MakeBytesError(fmt.Errorf("could not encrypt: %v", er))
}
st := cipher.NewCFBEncrypter(b,iv)
st.XORKeyStream(ct[aes.BlockSize:],data)
return results.BytesSuccess(ct)
}
func (e encrypter) Decrypt (data string) (string,error) {
cipherText, er := base64.StdEncoding.DecodeString(data)
if er != nil {
return "", fmt.Errorf("could not base64 decode: %v", er)
}
block, err := aes.NewCipher(e.key)
if err != nil {
return "", fmt.Errorf("could not create new cipher: %v", er)
}
if len(cipherText) < aes.BlockSize {
return "", fmt.Errorf("invalid ciphertext block size")
}
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherText, cipherText)
return string(cipherText), 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.PackerWithEncrypter(en))
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.Error(),true)
fmt.Printf("Paquete %s\n",pk.Packet())
*/
pk.PackAny(t,0)
errs.Test(pk.Error(),true)
fmt.Printf("Paquete %s\n",pk.Packet())
/*
res := en.Encrypt([]byte("Esto debe de ir encruptdao"))
if res.IsError() {
panic(res.Error())
}
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)
*/
}

Powered by TurnKey Linux.