コード例 #1
0
ファイル: session.go プロジェクト: jmptrader/gocrypto
// Marshal serialises a copy of the Identity. It is intended to support
// persistent Identities.
func Marshal(id *Identity) []byte {
	bufSize := ed25519.PrivateKeySize + ed25519.PublicKeySize
	bufSize += len(id.peers) * ed25519.PublicKeySize
	buf := sbuf.NewBuffer(bufSize)
	buf.Write(id.private[:])
	buf.Write(id.public[:])
	for i := range id.peers {
		buf.Write(id.peers[i][:])
	}

	return buf.Bytes()
}
コード例 #2
0
// utility function to construct arbitrary packed messages
func testPackMessage(length, sequence uint32, v uint8, pad uint16, mType MessageType, message []byte) []byte {
	buf := sbuf.NewBuffer(messageOverhead + len(message))
	buf.WriteByte(v)
	buf.WriteByte(uint8(mType))
	binary.Write(buf, binary.BigEndian, pad)

	// An sbuf won't fail to write unless it's out of memory, then this
	// whole house of cards is coming crashing down anyways.
	binary.Write(buf, binary.BigEndian, sequence)
	binary.Write(buf, binary.BigEndian, length)
	buf.Write(message)
	return buf.Bytes()
}
コード例 #3
0
ファイル: archive.go プロジェクト: jhautefeuille/filecrypt
// PackFiles walks the specified paths and archives them; the resulting
// archive is gzipped with maximum compression.
func PackFiles(paths []string) ([]byte, error) {
	// Create a buffer to write our archive to.
	buf := sbuf.NewBuffer(0)

	zbuf, err := gzip.NewWriterLevel(buf, gzip.BestCompression)
	if err != nil {
		return nil, err
	}

	// Create a new tar archive.
	tw := tar.NewWriter(zbuf)

	for _, walkPath := range paths {
		walker := func(path string, info os.FileInfo, err error) error {
			if info == nil {
				return fmt.Errorf("filecrypt: %s could not be read", path)
			}

			if !info.Mode().IsDir() && !info.Mode().IsRegular() {
				return errors.New("filecrypt: failed to compress " + path)
			}

			if Verbose {
				fmt.Println("Pack file", path)
			}

			filePath := filepath.Clean(path)
			hdr, err := tar.FileInfoHeader(info, filePath)
			if err != nil {
				return err
			}
			hdr.Name = filePath

			if err = tw.WriteHeader(hdr); err != nil {
				return err
			}

			if info.Mode().IsRegular() {
				file, err := os.Open(path)
				if err != nil {
					return err
				}

				_, err = io.Copy(tw, file)
				file.Close()
				return err
			}
			return nil

		}
		err := filepath.Walk(walkPath, walker)
		if err != nil {
			return nil, err
		}

	}

	tw.Close()
	zbuf.Close()

	return buf.Bytes(), nil
}