mirror of
https://github.com/go-i2p/go-i2cp.git
synced 2025-06-07 17:04:59 -04:00
The whole thing compiles now, writing tests and fixing bugs
This commit is contained in:
@ -2,6 +2,7 @@ package go_i2cp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"strings"
|
||||
@ -16,7 +17,7 @@ type Destination struct {
|
||||
cert *Certificate
|
||||
sgk SignatureKeyPair
|
||||
signPubKey *big.Int
|
||||
pubKey []byte
|
||||
pubKey [PUB_KEY_SIZE]byte
|
||||
digest [DIGEST_SIZE]byte
|
||||
b32 string
|
||||
b64 string
|
||||
@ -26,21 +27,29 @@ func NewDestination() (dest Destination, err error) {
|
||||
nullCert := NewCertificate(CERTIFICATE_NULL)
|
||||
dest.cert = &nullCert
|
||||
dest.sgk, err = GetCryptoInstance().SignatureKeygen(DSA_SHA1)
|
||||
dest.signPubKey = dest.sgk.pub.Y
|
||||
dest.generateB32()
|
||||
dest.generateB64()
|
||||
return
|
||||
}
|
||||
|
||||
// TODO ensure that this function not setting dest.sgk is the right thing to do
|
||||
func NewDestinationFromMessage(stream *Stream) (dest Destination, err error) {
|
||||
signPubKey := make([]byte, 128)
|
||||
pubKey := make([]byte, PUB_KEY_SIZE)
|
||||
_, err = stream.Read(pubKey)
|
||||
_, err = stream.Read(signPubKey)
|
||||
dest.pubKey = pubKey
|
||||
dest.signPubKey.SetBytes(signPubKey)
|
||||
_, err = stream.Read(dest.pubKey[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dest.signPubKey, err = GetCryptoInstance().PublicKeyFromStream(DSA_SHA1, stream)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dest.sgk = SignatureKeyPair{}
|
||||
dest.sgk.priv.Y = dest.signPubKey
|
||||
dest.sgk.pub.Y = dest.signPubKey
|
||||
var cert Certificate
|
||||
cert, err = NewCertificateFromMessage(stream)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dest.cert = &cert
|
||||
dest.generateB32()
|
||||
dest.generateB64()
|
||||
@ -57,9 +66,7 @@ func NewDestinationFromStream(stream *Stream) (dest Destination, err error) {
|
||||
if pubKeyLen != PUB_KEY_SIZE {
|
||||
Fatal(tag, "Failed to load pub key len, %d != %d", pubKeyLen, PUB_KEY_SIZE)
|
||||
}
|
||||
pubKey := make([]byte, PUB_KEY_SIZE)
|
||||
_, err = stream.Read(pubKey)
|
||||
dest.pubKey = pubKey
|
||||
_, err = stream.Read(dest.pubKey[:])
|
||||
dest.generateB32()
|
||||
dest.generateB64()
|
||||
return
|
||||
@ -79,9 +86,9 @@ func NewDestinationFromBase64(base64 string) (dest Destination, err error) {
|
||||
replaced = strings.Replace(base64, "~", "/", -1)
|
||||
replaced = strings.Replace(replaced, "-", "+", -1)
|
||||
stream := NewStream([]byte(replaced))
|
||||
var decoded Stream
|
||||
var decoded *Stream
|
||||
decoded, err = GetCryptoInstance().DecodeStream(CODEC_BASE64, stream)
|
||||
return NewDestinationFromMessage(&decoded)
|
||||
return NewDestinationFromMessage(decoded)
|
||||
}
|
||||
|
||||
func NewDestinationFromFile(file *os.File) (dest Destination, err error) {
|
||||
@ -92,7 +99,7 @@ func NewDestinationFromFile(file *os.File) (dest Destination, err error) {
|
||||
func (dest *Destination) Copy() (newDest Destination) {
|
||||
newDest.cert = dest.cert
|
||||
newDest.signPubKey = dest.signPubKey
|
||||
copy(newDest.pubKey, dest.pubKey)
|
||||
newDest.pubKey = dest.pubKey
|
||||
newDest.sgk = dest.sgk
|
||||
newDest.b32 = dest.b32
|
||||
newDest.b64 = dest.b64
|
||||
@ -100,48 +107,59 @@ func (dest *Destination) Copy() (newDest Destination) {
|
||||
return
|
||||
}
|
||||
func (dest *Destination) WriteToFile(filename string) (err error) {
|
||||
stream := NewStream(make([]byte, DEST_SIZE))
|
||||
stream := NewStream(make([]byte, 0, DEST_SIZE))
|
||||
dest.WriteToStream(stream)
|
||||
var file *os.File
|
||||
file, err = os.Open(filename)
|
||||
stream.WriteTo(file)
|
||||
file.Close()
|
||||
return
|
||||
}
|
||||
func (dest *Destination) WriteToMessage(stream *Stream) (err error) {
|
||||
_, err = stream.Write(dest.pubKey)
|
||||
lena := len(dest.pubKey)
|
||||
_ = lena
|
||||
_, err = stream.Write(dest.pubKey[:])
|
||||
_, err = stream.Write(dest.signPubKey.Bytes()) //GetCryptoInstance().WriteSignatureToStream(&dest.sgk, stream)
|
||||
err = dest.cert.WriteToMessage(stream)
|
||||
lenb := stream.Len()
|
||||
_ = lenb
|
||||
return
|
||||
}
|
||||
func (dest *Destination) WriteToStream(stream *Stream) (err error) {
|
||||
err = dest.cert.WriteToStream(stream)
|
||||
err = GetCryptoInstance().WriteSignatureToStream(&dest.sgk, stream)
|
||||
err = stream.WriteUint16(PUB_KEY_SIZE)
|
||||
_, err = stream.Write(dest.pubKey)
|
||||
_, err = stream.Write(dest.pubKey[:])
|
||||
return
|
||||
}
|
||||
|
||||
//Doesn't seem to be used anywhere??
|
||||
func (dest *Destination) Verify() (verified bool, err error) {
|
||||
stream := NewStream(make([]byte, DEST_SIZE))
|
||||
stream := NewStream(make([]byte, 0, DEST_SIZE))
|
||||
dest.WriteToMessage(stream)
|
||||
stream.Write(dest.digest[:])
|
||||
return GetCryptoInstance().VerifyStream(&dest.sgk, stream)
|
||||
}
|
||||
|
||||
func (dest *Destination) generateB32() {
|
||||
stream := NewStream(make([]byte, DEST_SIZE))
|
||||
stream := NewStream(make([]byte, 0, DEST_SIZE))
|
||||
dest.WriteToMessage(stream)
|
||||
cpt := GetCryptoInstance()
|
||||
hash := cpt.HashStream(HASH_SHA256, stream)
|
||||
b32 := cpt.EncodeStream(CODEC_BASE32, hash)
|
||||
dest.b32 = string(b32.Bytes()) + ".b32.i2p"
|
||||
length := b32.Len()
|
||||
_ = length
|
||||
dest.b32 = string(b32.Bytes())
|
||||
dest.b32 += ".b32.i2p"
|
||||
Debug(tag, "New destination %s", dest.b32)
|
||||
}
|
||||
func (dest *Destination) generateB64() {
|
||||
stream := NewStream(make([]byte, DEST_SIZE))
|
||||
stream := NewStream(make([]byte, 0, DEST_SIZE))
|
||||
dest.WriteToMessage(stream)
|
||||
cpt := GetCryptoInstance()
|
||||
if stream.Len() > 0 {
|
||||
fmt.Printf("Stream len %d \n", stream.Len())
|
||||
}
|
||||
b64B := cpt.EncodeStream(CODEC_BASE64, stream)
|
||||
replaced := strings.Replace(string(b64B.Bytes()), "/", "~", -1)
|
||||
replaced = strings.Replace(replaced, "/", "~", -1)
|
||||
|
Reference in New Issue
Block a user