示例#1
0
文件: spki.go 项目: hlandau/degoutils
// If isPrivate is false, the public key is at privateKey[32:].
//
// Otherwise the private key is at privateKey[0:32] and the public key is of course
// as always at privateKey[32:64].
func LoadEd25519Key(v []interface{}, privateKey *[64]byte) (isPrivate bool, err error) {
	r := sx.Q1bsyt(v, "private-key ed25519")
	if r == nil {
		r = sx.Q1bsyt(v, "public-key ed25519")
	} else {
		isPrivate = true
	}

	if r == nil {
		return false, ErrMalformedKey
	}

	b, ok := r[0].(string)
	if !ok || (isPrivate && len(b) != 64) || (!isPrivate && len(b) != 32) {
		return false, ErrMalformedKey
	}

	if isPrivate {
		copy(privateKey[:], b)
	} else {
		copy(privateKey[32:], b)
	}

	return
}
示例#2
0
文件: file.go 项目: hlandau/degoutils
func LoadFileInfo(x []interface{}) (*FileInfo, error) {
	fi := &FileInfo{}

	r := sx.Q1bsyt(x, "file-representation")
	if r == nil {
		return nil, ErrMalformedFileInfo
	}

	// length
	lv := sx.Q1bsyt(r, "length")
	if lv == nil || len(lv) == 0 {
		return nil, ErrMalformedFileInfo
	}
	switch lvi := lv[0].(type) {
	case int:
		fi.Length = int64(lvi)
	case int64:
		fi.Length = lvi
	default:
		return nil, ErrMalformedFileInfo
	}

	// hash
	ht, h, err := LoadHash(r)
	if err != nil {
		return nil, ErrMalformedFileInfo
	}
	fi.Hash = h
	fi.HashType = ht

	// filename (optional)
	fnv := sx.Q1bsyt(r, "filename")
	if fnv != nil {
		if len(fnv) == 0 {
			return nil, ErrMalformedFileInfo
		}
		if fnvs, ok := fnv[0].(string); ok {
			fi.Filename = fnvs
		} else {
			return nil, ErrMalformedFileInfo
		}
	}

	return fi, nil
}
示例#3
0
文件: sx_test.go 项目: hlandau/sx
func TestQuery(t *testing.T) {
	if !sx.Hhy([]interface{}{"alpha"}, "alpha") {
		t.Fatalf("...")
	}

	xs, err := sx.SX.Parse([]byte(`
    (alpha)
    (beta
      (x)
      (y 42)
      (z))
    (gamma)
    (delta)
  `))
	if err != nil {
		t.Fatalf("failed to parse: %v", err)
	}

	if !sx.Hhy(xs[0], "alpha") {
		t.Fatalf("...")
	}

	if sx.Q1bhy(xs, "alpha") == nil {
		t.Fatalf("...")
	}

	if sx.Q1bhyt(xs, "beta") == nil {
		t.Fatalf("...")
	}

	r := sx.Q1bsyt(xs, "beta y")
	if r == nil {
		t.Fatalf("no match")
	}

	out, err := sx.SXCanonical.String(r)
	if err != nil {
		t.Fatalf("cannot serialize: %v", err)
	}

	if out != "42 " {
		t.Fatalf("mismatch: %#v", out)
	}
}
示例#4
0
文件: hash.go 项目: hlandau/degoutils
func LoadHash(x []interface{}) (HashType, []byte, error) {
	r := sx.Q1bsyt(x, "hash")
	if r == nil || len(r) < 2 {
		return "", nil, ErrMalformedHash
	}

	hts, ok := r[0].(string)
	if !ok {
		return "", nil, ErrMalformedHash
	}

	b, ok := r[1].(string)
	if !ok {
		return "", nil, ErrMalformedHash
	}

	ht, ok := ParseHashType(hts)
	if !ok {
		return "", nil, ErrMalformedHash
	}

	return ht, []byte(b), nil
}