packs2
DEV 3 years ago
parent 5f6fca2986
commit 2b12abf8ed

@ -24,129 +24,33 @@
package main package main
import ( import (
"activething.com/go/gogo/core/bins/bfields" "activething.com/go/gogo/core/times"
"activething.com/go/gogo/core/results" "activething.com/go/gogo/core/times/bdates"
"activething.com/go/gogo/packs/pjson"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt" "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() { func main() {
en := encrypter{key: []byte{0,1,2,3,45,6,6,7,8,9,0,1,1,1,1,1}} fmt.Printf("1970 %d \n",times.DayOfWeek(1970,1,1))
fmt.Printf("2020 %d \n",times.DayOfWeek(2020,1,1))
pk := pjson.NewPacker(pjson.WithEncrypter(en)) fmt.Printf("2021 %d \n",times.DayOfWeek(2021,1,1))
//pk := pjson.NewPacker() fmt.Printf("2022 %d \n",times.DayOfWeek(2022,1,1))
fmt.Printf("2023 %d \n",times.DayOfWeek(2023,1,1))
t := A { fmt.Printf("2024 %d \n",times.DayOfWeek(2024,1,1))
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())
*/
a := bdates.BYearAtt(bdates.BYearAttLeap)
n := a.SetStartDay(6)
fmt.Printf(" %d = %b \n ",n.StartDay(),n)
n = a.SetStartDay(3)
fmt.Printf(" %d = %b \n ",n.StartDay(),n)
pk.PackAny(t,0) for i:=2020; i<=2024; i++ {
//errs.Test(pk.Cause(),true) y := bdates.MakeBYear(i)
fmt.Printf("Paquete %s\n",pk.Packet()) fmt.Printf("%d Bisiesto %s - dia de la semana %b \n",i,y.Atts().IsLeap(),y.Atts().StartDay())
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())
} }

@ -23,9 +23,106 @@
package bdates package bdates
import "activething.com/go/gogo/core/times"
type ( type (
BYear [12]BMonth BYear struct {
atts BYearAtt
first byte
year byte
months [12]BMonth
}
) )
func NewBYear (year int) *BYear {
if year < minBYear || year > maxBYear {
panic(G.ErrInvalidYear)
}
at := BYearAtt(BYearAttSet)
if times.IsYearLeap(year) {
at|=BYearAttLeap
}
return &BYear {
atts : at,
first: byte(times.DayOfWeek(year,1,1)),
year : byte(year-1900),
}
}
func (y *BYear) Atts () BYearAtt {
return y.atts }
func (y *BYear) First () int {
return int(y.first) }
func (y *BYear) Year () int {
return minBYear + int(y.year) }
func (y *BYear) Clear (month ...int) {
if len(month) == 0 {
for i := 0; i < 12; i++ {
y.months[i] = 0
}
return
}
for _,i := range month {
if !checkMonthNumber(i){
panic(G.ErrInvalidMonth)
}
y.months[i]=0
}
}
func (y *BYear) GetDay (month, day int) bool {
if !checkMonthNumber(month) {
panic(G.ErrInvalidMonth )
}
if day < 1 || day > y.daysInMonth(month) {
panic(G.ErrInvalidDay)
}
return (uint32(1 << day) & uint32(y.months[month])) != 0
}
func (y *BYear) SetDay (month, day int, active bool) {
if !checkMonthNumber(month) {
panic(G.ErrInvalidMonth )
}
if day < 1 || day > y.daysInMonth(month) {
panic(G.ErrInvalidDay)
}
month-=1
if active {
y.months[month] = y.months[month] | BMonth(1 << day)
return
}
y.months[month] = y.months[month] &^ BMonth(1 << day)
}
func (y *BYear) GetMonth (month int) BMonth {
if !checkMonthNumber(month) {
panic(G.ErrInvalidMonth)
}
return y.months[month-1]
}
func (y BYear) daysInMonth (month int) int {
d := [12]int{ 31,28,31,30,31,30,31,30,31,30,31 }
if month == 2 && y.Atts().IsLeap() {
return 29
}
return d[month-1]
}

@ -0,0 +1,51 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name byear_att.go
// Created by DEV
// Modified 21/02/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 bdates
const(
BYearAttSet BYearAtt = iota << 1
BYearAttLeap
)
type BYearAtt byte
func (a BYearAtt) IsSet () bool {
return a&BYearAttSet != 0 }
func (a BYearAtt) IsLeap () bool {
return a&BYearAttLeap != 0 }

@ -0,0 +1,59 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name gogo.go
// Created by DEV
// Modified 21/02/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 bdates
import "activething.com/go/gogo/core/errs"
const(
minBYear int = 1900
maxBYear int = 2155
)
var (
G = struct {
ErrInvalidDay errs.ErrCode
ErrInvalidMonth errs.ErrCode
ErrInvalidYear errs.ErrCode
}{
ErrInvalidDay : errs.ErrCode("core:times.bdates.invalid-day"),
ErrInvalidMonth : errs.ErrCode("core:times.bdates.invalid-month"),
ErrInvalidYear : errs.ErrCode("core:times.bdates.invalid-year"),
}
)
func checkDayNumber (day int) bool {
return day >=1 && day <=7 }
func checkMonthNumber(month int) bool {
return month >=1 && month <=12 }

@ -23,7 +23,9 @@
package times package times
import "activething.com/go/gogo/core/errs" import (
"activething.com/go/gogo/core/errs"
)
@ -69,3 +71,17 @@ func DaysIn (year, month int) int {
return d[month] return d[month]
} }
func DayOfWeek (year, month, day int) int {
if month < 3 {
month += 12
year--
}
J := year / 100
K := year % 100
h := (day + (13*(month+1))/5 + K + K/4 + J/4 + 5*J) % 7
if h <= 1 {
return h+6
}
return h-1
}

Loading…
Cancel
Save

Powered by TurnKey Linux.