Skip to content

Commit

Permalink
chore(hasher): better persistent hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Dec 14, 2023
1 parent cf10223 commit d3b6a5f
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions internal/types/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package types

import (
"bytes"
"crypto/md5"
"encoding/gob"
"fmt"
"encoding/hex"
"reflect"
"strconv"

"github.com/gotd/td/tg"
)
Expand All @@ -25,12 +25,20 @@ type HashableFileStruct struct {
}

func (f *HashableFileStruct) Pack() string {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(f)
return FromBytes(b.Bytes())
}
hasher := md5.New()
val := reflect.ValueOf(*f)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)

var fieldValue []byte
switch field.Kind() {
case reflect.String:
fieldValue = []byte(field.String())
case reflect.Int64:
fieldValue = []byte(strconv.FormatInt(field.Int(), 10))
}

func FromBytes(data []byte) string {
result := md5.Sum(data)
return fmt.Sprintf("%x", result)
hasher.Write(fieldValue)
}
return hex.EncodeToString(hasher.Sum(nil))
}

0 comments on commit d3b6a5f

Please sign in to comment.