seekctr

package module
v0.0.0-...-0ec476e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2023 License: GPL-3.0 Imports: 3 Imported by: 0

README

seekctr GoDoc

cipher.Stream does not implement io.Seeker despite XOR stream ciphers being seekable.

Usage

package main

import (
	"fmt"
	"io"
	"log"
	"os"

	"github.com/uhthomas/seekctr"
)

func main() {
	// your key and initialization vector
	var key, iv []byte
	// open the encrypted file or stream
	f, err := os.Open("encrypted file")
	if err != nil {
		log.Fatal(err)
	}
	r, err := seekctr.NewReader(f, key, iv)
	if err != nil {
		log.Fatal(err)
	}
	// Seek past the first 1Kb
	if _, err := r.Seek(1 << 10); err != nil {
		log.Fatal(err)
	}
	// copy the remaining contents to stdout
	if _, err := io.Copy(os.Stdout, r); err != nil {
		log.Fatal(err)
	}
}

Note

Recreating the original stream cipher with a new initialization vector (where iv += offset / block size) and discarding the remaining bytes (offset % block size) may be preferrable.

var key, iv [16]byte

b, err := aes.NewCipher(key[:])
if err != nil { ... }

offset := uint64(4 << 10)

// offset in chunks
chunks := uint64(int(offset) / b.BlockSize())

// iv += offset
var c uint16
for i := len(iv[:]) - 1; i >= 0; i-- {
	c = uint16(iv[i]) + uint16(chunks & 0xFF) + c
	iv[i], c, chunks = byte(c), c >> 8, chunks >> 8
}

// Reinitialize cipher
s := cipher.NewCTR(b, iv[:])

// Discard n bytes
d := make([]byte, int(offset) % b.BlockSize())
s.XORKeyStream(d, d)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader wraps and io.Reader and will decrypt anything read from it using the ctr.

func NewReader

func NewReader(r io.Reader, key, iv []byte) (*Reader, error)

NewReader returns an io.ReadSeekCloser. The key must be 16, 24 or 32 bytes in size and the iv must be 16 bytes.

func (*Reader) Close

func (r *Reader) Close() (err error)

Close closes the underlying reader if it is an io.Closer.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read will read len(p) bytes from r and decrypt them using ctr.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (ret int64, err error)

Seek will, if r is an io.Seeker, Seek to the offset given the whence and will then seek the ctr cipher.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer wraps an io.Writer and will encrypt anything written to it using the ctr.

func NewWriter

func NewWriter(w io.Writer, key, iv []byte) (*Writer, error)

NewWriter returns an io.WriteSeekCloser. The key must be 16, 24 or 32 bytes in size and the iv must be 16 bytes.

func (*Writer) Close

func (w *Writer) Close() (err error)

Close closes the underlying writer if it is an io.Closer.

func (*Writer) Seek

func (w *Writer) Seek(offset int64, whence int) (ret int64, err error)

Seek will, if w is an io.Seeker, Seek to the offset given the whence and will then seek the ctr cipher.

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write encrypts b using ctr and then writes to w.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL