update date, destination, integer

This commit is contained in:
Hayden Parker
2016-02-14 22:40:29 -08:00
parent e19d6fa0ab
commit f9706700a1
4 changed files with 37 additions and 18 deletions

View File

@ -7,13 +7,12 @@ import (
type Date [8]byte type Date [8]byte
// //
// Time takes the value stored in date as an 8 // Time takes the value stored in date as an 8 byte big-endian integer representing the
// byte big-endian integer representing the // number of milliseconds since the beginning of unix time and converts it to a Go time.Time
// number of milliseconds since the beginning
// of unix time and converts it to a go time.Time
// struct. // struct.
// //
func (date Date) Time() time.Time { func (date Date) Time() (date_time time.Time) {
seconds := Integer(date[:]) seconds := Integer(date[:])
return time.Unix(0, int64(seconds*1000000)) date_time = time.Unix(0, int64(seconds*1000000))
return
} }

View File

@ -7,6 +7,10 @@ import (
"strings" "strings"
) )
//
// A Destination is a KeysAndCert with functionallity
// for generating base32 and base64 addresses.
//
type Destination []byte type Destination []byte
func (destination Destination) PublicKey() (crypto.PublicKey, error) { func (destination Destination) PublicKey() (crypto.PublicKey, error) {
@ -21,12 +25,19 @@ func (destination Destination) Certificate() (Certificate, error) {
return KeysAndCert(destination).Certificate() return KeysAndCert(destination).Certificate()
} }
func (destination Destination) Base32Address() string { //
// Generate the I2P base32 address for this Destination.
//
func (destination Destination) Base32Address() (str string) {
hash := crypto.SHA256(destination) hash := crypto.SHA256(destination)
str := strings.Trim(base32.EncodeToString(hash[:]), "=") str = strings.Trim(base32.EncodeToString(hash[:]), "=")
return str + ".b32.i2p" str = str + ".b32.i2p"
return
} }
//
// Generate the I2P base64 address for this Destination.
//
func (destination Destination) Base64() string { func (destination Destination) Base64() string {
return base64.EncodeToString(destination) return base64.EncodeToString(destination)
} }

View File

@ -4,20 +4,22 @@ import (
"encoding/binary" "encoding/binary"
) )
const (
INTEGER_SIZE = 8
)
// //
// Interpret a slice of bytes from length 1 // Interpret a slice of bytes from length 0 to length 8 as a big-endian
// to length 8 as a big-endian integer and // integer and return an int representation.
// return an int representation.
// //
func Integer(number []byte) int { func Integer(number []byte) (value int) {
num_len := len(number) num_len := len(number)
if num_len < 8 { if num_len < INTEGER_SIZE {
number = append( number = append(
make([]byte, 8-num_len), make([]byte, INTEGER_SIZE-num_len),
number..., number...,
) )
} }
return int( value = int(binary.BigEndian.Uint64(number))
binary.BigEndian.Uint64(number), return
)
} }

View File

@ -18,3 +18,10 @@ func TestWorksWith1Byte(t *testing.T) {
t.Fatal("Integer() does not work with 1 byte") t.Fatal("Integer() does not work with 1 byte")
} }
} }
func TestIsZeroWithNoData(t *testing.T) {
i := Integer([]byte{})
if i != 0 {
t.Fatal("Integer() does not work with 0 bytes")
}
}