internal

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: AGPL-3.0 Imports: 34 Imported by: 0

Documentation

Overview

Package internal implements file reading and processing, as well as generation.

Index

Constants

View Source
const (
	// BytesPerLine denominates the amount of bytes to be encoded per line of the serialized output
	BytesPerLine = 24
	// PdfTextFont gives the typeface as it is named in the PDF
	PdfTextFont = "Text"
	// PdfMonoFont gives the typeface for monospace passages as it is named in the PDF
	PdfMonoFont = "Mono"
	// PdfDataLineFontSize sets the font size of data lines in the PDF [pt]
	PdfDataLineFontSize = 11
)
View Source
const (
	// HeaderFieldVersion holds the name of the header field Version. Constant to avoid parsing issues.
	HeaderFieldVersion = "PaperCrypt Version"
	// HeaderFieldSerial holds the name of the header field for the serial number. Constant to avoid parsing issues.
	HeaderFieldSerial = "Content Serial"
	// HeaderFieldPurpose holds the name of the header field Purpose. Constant to avoid parsing issues.
	HeaderFieldPurpose = "Purpose"
	// HeaderFieldComment holds the name of the header field Comment. Constant to avoid parsing issues.
	HeaderFieldComment = "Comment"
	// HeaderFieldDate holds the name of the header field Date. Constant to avoid parsing issues.
	HeaderFieldDate = "Date"
	// HeaderFieldDataFormat holds the name of the header field Data Format. Constant to avoid parsing issues.
	HeaderFieldDataFormat = "Data Format"
	// HeaderFieldContentLength holds the name of the header field Content Length. Constant to avoid parsing issues.
	HeaderFieldContentLength = "Content Length"
	// HeaderFieldCRC24 holds the name of the header field for the CRC-24 checksum. Constant to avoid parsing issues.
	HeaderFieldCRC24 = "Content CRC-24"
	// HeaderFieldCRC32 holds the name of the header field for the CRC-32 checksum. Constant to avoid parsing issues.
	HeaderFieldCRC32 = "Content CRC-32"
	// HeaderFieldSHA256 holds the name of the header field for the SHA-256 checksum. Constant to avoid parsing issues.
	HeaderFieldSHA256 = "Content SHA-256"
	// HeaderFieldHeaderCRC32 holds the name of the header field for the CRC-32 checksum of the header. Constant to avoid parsing issues.
	HeaderFieldHeaderCRC32 = "Header CRC-32"
	// PDFHeaderSheetID holds the text label displayed in the PDF header for the sheet ID.
	PDFHeaderSheetID = "Sheet ID"
	// PDFHeading holds the title of the PDF document, as shown on the first page.
	PDFHeading = "PaperCrypt Recovery Sheet"
	// PDFSectionDescriptionHeading holds the title of the section describing the document.
	PDFSectionDescriptionHeading = "What is this?"
	// PDFSectionDescriptionContent holds the content of the section describing the document.
	PDFSectionDescriptionContent = "" /* 235-byte string literal not displayed */
	// PDFSectionRepresentationHeading holds the title of the section describing the data representation.
	PDFSectionRepresentationHeading = "Binary Data Representation"
	// PDFSectionRepresentationContent holds the content of the section describing the data representation.
	PDFSectionRepresentationContent = "" /* 556-byte string literal not displayed */
	// PDFSectionRecoveryHeading holds the title of the section describing how to recover the data.
	PDFSectionRecoveryHeading = "Recovering the data"
	// PDFSectionRecoveryContent holds the content of the section describing how to recover the data.
	PDFSectionRecoveryContent = "" /* 251-byte string literal not displayed */
	// PDFSectionRecoveryContentNo2D holds the content of the section describing how to recover the data, if no 2D code is present.
	PDFSectionRecoveryContentNo2D = "" /* 230-byte string literal not displayed */
)
View Source
const (
	// CRC24Polynomial defines the polynomial used by the CRC-24 algorithm. See https://en.wikipedia.org/wiki/Cyclic_redundancy_check#:~:text=OpenPGP%2C%20RTCM104v3-,0x864cfb,-0xDF3261. This is the polynomial used in OpenPGP and RTCM104v3, represented as a uint32.
	CRC24Polynomial = uint32(0x864CFB) // CRC-24 polynomial
	// CRC24Initial is the initial value used for CRC-24 calculations. This is the initial value used in OpenPGP and RTCM104v3, represented as a uint32.
	CRC24Initial = uint32(0xB704CE) // Initial value
	// CRC24TableSize is the size of the CRC-24 table used for faster computation.
	CRC24TableSize = uint32(256) // Table size for faster computation
)
View Source
const (
	// TimeStampFormatLong shows the full date and time precisely for humans. It is used for the container file, as well as the timestamp command-line parameter.
	TimeStampFormatLong = "Mon, 02 Jan 2006 15:04:05.000000000 -0700"
	// TimeStampFormatLongTZ is used for v1 backwards compatibility.
	TimeStampFormatLongTZ = "Mon, 02 Jan 2006 15:04:05.000000000 MST"
	// TimeStampFormatShort is used in parsing the timestamp command-line parameter [cmd/generateCmd].
	TimeStampFormatShort = "2006-01-02 15:04:05"
	// TimeStampFormatDate is used as an alternative format for the command-line parameter.
	TimeStampFormatDate = "2006-01-02"
	// TimeStampFormatPDFHeader is used exclusively for the timestamps in displayed PDF headers. Both for the normal papercrypt sheet and the phrase sheet.
	TimeStampFormatPDFHeader = "2006-01-02 15:04 -0700"
)
View Source
const (
	BytesPerLineV1 = 22 // As is done in paperkey (https://www.jabberwocky.com/software/paperkey/)
)

Variables

View Source
var (
	// PdfTextFontRegularBytes holds the font data for the text typeface, as embedded at compile time. For regular text.
	PdfTextFontRegularBytes []byte
	// PdfTextFontBoldBytes holds the font data for the text typeface, as embedded at compile time. For bold text.
	PdfTextFontBoldBytes []byte
	// PdfTextFontItalicBytes holds the font data for the text typeface, as embedded at compile time. For italic text.
	PdfTextFontItalicBytes []byte
)
View Source
var (
	// PdfMonoFontRegularBytes holds the font data for the monospace typeface, as embedded at compile time. For regular text.
	PdfMonoFontRegularBytes []byte
	// PdfMonoFontBoldBytes holds the font data for the monospace typeface, as embedded at compile time. For bold text.
	PdfMonoFontBoldBytes []byte
	// PdfMonoFontItalicBytes holds the font data for the monospace typeface, as embedded at compile time. For italic text.
	PdfMonoFontItalicBytes []byte
)
View Source
var (
	// URL is used to style URLs.
	URL = lipgloss.NewStyle().Foreground(lipgloss.Color("3")).Render

	// Warning is used to style warnings for the user.
	Warning = lipgloss.NewStyle().Foreground(lipgloss.Color("11")).Bold(true).Render

	// Bold is used to display the words from key generation more bold than other output
	Bold = lipgloss.NewStyle().Bold(true).Render
)
View Source
var VersionInfo goversion.Info

VersionInfo holds information about the version of this software, such as the version number and version control metadata.

Functions

func BytesFromBase64

func BytesFromBase64(data string) ([]byte, error)

BytesFromBase64 decodes a base64 string using base64.StdEncoding to a byte slice

func CloseFileIfNotStd

func CloseFileIfNotStd(file *os.File) error

CloseFileIfNotStd closes a file handle, if it is not an os Std file descriptor. This is done to properly close only those files this program opened.

func Crc24Checksum

func Crc24Checksum(data []byte) uint32

Crc24Checksum generates a CRC-24 checksum for the given data.

func DeserializeBinary

func DeserializeBinary(data *[]byte) ([]byte, error)

DeserializeBinary deserializes bytes from human-readable archive format encoded by SerializeBinary

func GenerateFromSeed

func GenerateFromSeed(seed int64, amount int, wordList *[]string) ([]string, error)

GenerateFromSeed selects a number of words from the given list using a seeded, non-cryptographic pseudo-random generator.

func GeneratePassphraseSheetPDF

func GeneratePassphraseSheetPDF(seed int64, words []string) ([]byte, error)

GeneratePassphraseSheetPDF creates a PDF file displaying the given words in three columns, the seed in the header.

func GenerateSerial

func GenerateSerial(length uint8) (string, error)

GenerateSerial generates a random serial number of length `length`.

func GetFileHandleCarefully

func GetFileHandleCarefully(path string, override bool) (*os.File, error)

GetFileHandleCarefully returns a file handle for the given path. will warn if the file already exists, and error if override is false. if path is empty, returns os.Stdout.

func NormalizeLineEndings

func NormalizeLineEndings(data []byte) []byte

NormalizeLineEndings cuts all "\r" from given input, normalizing to unix standard. This is used when reading papercrypt files.

func ParseHexUint32

func ParseHexUint32(hex string) (uint32, error)

ParseHexUint32 parses a uint32 number from a string of hexadecimal characters

func PrintInputAndGetReader

func PrintInputAndGetReader(inFileName string) (*os.File, error)

PrintInputAndGetReader prints the input source and returns the reader. if path is empty, returns os.Stdin. must be closed by the caller.

func PrintInputAndRead

func PrintInputAndRead(inFileName string) ([]byte, error)

PrintInputAndRead prints the input source and returns the contents of the file. if path is empty, returns os.Stdin.

func PrintWrittenSizeToDebug added in v2.0.2

func PrintWrittenSizeToDebug(size int, file *os.File)

PrintWrittenSizeToDebug logs the amount of data written in human-readable notation. A warning is issues when the size is 0.

func SensitivePrompt

func SensitivePrompt() ([]byte, error)

SensitivePrompt reads a password from the tty (if available) or stdin (if not).

func SerializeBinary

func SerializeBinary(data *[]byte, bytesPerLine int) string

SerializeBinary returns the encrypted binary data, formatted for restoration lines will hold 22 bytes of data, prefaces by the line number, followed by the CRC-24 of the line, bytes are printed as two base16 (hex) digits, separated by a space. Example:

1: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 <CRC-24 of this line>
2: ... <CRC-24 of this line>

10: ... <CRC-24 of this line> ... n-1: ... <CRC-24 of this line> n: <CRC-24 of the block>

See [example.pdf](example.pdf) for an example.

func SerializeBinaryV1

func SerializeBinaryV1(data *[]byte) string

SerializeBinaryV1 serializes binary data using SerializeBinary. Used for backwards compatibility.

func SerializeBinaryV2

func SerializeBinaryV2(data *[]byte) string

SerializeBinaryV2 serializes binary data using SerializeBinary.

func SliceHasString

func SliceHasString(slice []string, str string) bool

SliceHasString determines whether a string slice contains a given string

func SplitTextHeaderAndBody

func SplitTextHeaderAndBody(data []byte) ([]byte, []byte, error)

SplitTextHeaderAndBody splits the given byte slice, which should be a PaperCrypt document, into a header and body section.

func SprintBinarySize

func SprintBinarySize(size int) string

SprintBinarySize returns human-readable number in binary notation (KiB, MiB, GiB, TiB) for the given size in bytes.

func TextToHeaderMap

func TextToHeaderMap(text []byte) (map[string]string, error)

TextToHeaderMap converts a byte slice containing text headers into a map of header fields. Each header line should be in the format "Key: Value", with the key being the header field name and the value being the header field value. The function trims the "# " prefix from header lines, which is present in the serialized text format.

func ValidateCRC24

func ValidateCRC24(data []byte, checksum uint32) bool

ValidateCRC24 validates the CRC-24 checksum of the given data against the provided checksum. It returns true if the checksum matches, false otherwise.

func ValidateCRC32

func ValidateCRC32(data []byte, checksum uint32) bool

ValidateCRC32 validates the CRC-32 checksum of the given data against the provided checksum. It returns true if the checksum matches, false otherwise.

Types

type PaperCrypt

type PaperCrypt struct {
	// Version is the version of papercrypt used to generate the document.
	Version string `json:"v"`

	// DataFormat determines whether the data is raw (although still gzipped), or follows the PGP message format (gzipped).
	DataFormat PaperCryptDataFormat `json:"f"`

	// SerialNumber is the serial number of document, used to identify it. It is generated randomly if not provided.
	SerialNumber string `json:"sn"`

	// Purpose is the purpose of document
	Purpose string `json:"p"`

	// Comment is the comment on document
	Comment string `json:"cm"`

	// CreatedAt is the creation timestamp
	CreatedAt time.Time `json:"ct"`

	// DataCRC24 is the CRC-24 checksum of the encrypted data
	DataCRC24 uint32 `json:"d_c24"`

	// DataCRC32 is the CRC-32 checksum of the encrypted data
	DataCRC32 uint32 `json:"d_c32"`

	// DataSHA256 is the SHA-256 checksum of the encrypted data
	DataSHA256 [32]byte `json:"d_s256"`

	// Data is the contents of the document
	// it can be either of two formats:
	//   a) ASCII armored OpenPGP data, if DataFormat is PGP
	//      the contained message is gzipped before encryption
	//   b) Raw data of any kind, if DataFormat is Raw
	// either way, data is always gzipped after processing
	Data []byte `json:"d"`
}

PaperCrypt represents a PaperCrypt document. It contains metadata about the document, such as its version, serial number, purpose, comment, creation date, and the data itself.

func DeserializeV1Text

func DeserializeV1Text(data []byte, ignoreVersionMismatch bool, ignoreChecksumMismatch bool) (*PaperCrypt, error)

func DeserializeV2Text

func DeserializeV2Text(
	data []byte,
	ignoreVersionMismatch bool,
	ignoreChecksumMismatch bool,
) (*PaperCrypt, error)

DeserializeV2Text deserializes a PaperCrypt document from a byte slice containing text. It expects the text to be in the format defined by PaperCrypt version 2. (PaperCryptContainerVersionMajor2).

func NewPaperCrypt

func NewPaperCrypt(
	version string,
	data []byte,
	serialNumber string,
	purpose string,
	comment string,
	createdAt time.Time,
	format PaperCryptDataFormat,
) *PaperCrypt

NewPaperCrypt creates a new paper crypt.

func (*PaperCrypt) Decode

func (p *PaperCrypt) Decode(passphrase []byte) ([]byte, error)

Decode decodes and, if the data was encrypted with PaperCrypt (data format is PaperCryptDataFormatPGP), decrypts the data, returning the original binary data.

func (*PaperCrypt) GetBinarySerialized

func (p *PaperCrypt) GetBinarySerialized() (string, error)

GetBinarySerialized returns the binary serialized representation of the PaperCrypt document as a string.

func (*PaperCrypt) GetDataLength

func (p *PaperCrypt) GetDataLength() int

GetDataLength returns the length of the data in bytes as an integer.

func (*PaperCrypt) GetPDF

func (p *PaperCrypt) GetPDF(no2D bool, lowerCaseEncoding bool) ([]byte, error)

GetPDF returns the binary representation of the paper crypt The PDF will be generated to include some basic information about papercrypt, some metadata, optionally a 2D-Code, and the encrypted data.

The data will be formatted as

a) ASCII armored OpenPGP data, if --armor is specified
b) Base16 (hex) encoded binary data, if --armor is not specified

The PDF Document will have a header row, containing the following information:

  • Serial Number
  • Creation Date
  • Purpose

and, next to the markdown information, a 2D code containing the encrypted data.

func (*PaperCrypt) GetText

func (p *PaperCrypt) GetText(lowerCaseEncoding bool) ([]byte, error)

GetText returns the text representation of the paper crypt.

func (*PaperCrypt) MarshalJSON

func (p *PaperCrypt) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for PaperCrypt.

func (*PaperCrypt) UnmarshalJSON

func (p *PaperCrypt) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for PaperCrypt.

type PaperCryptContainerVersion

type PaperCryptContainerVersion uint32

PaperCryptContainerVersion is an enum (uint32) of versions of the container format

const (
	// PaperCryptContainerVersionUnknown represents any unknown version, which may be newer, or come from parsing invalid input
	PaperCryptContainerVersionUnknown PaperCryptContainerVersion = 0
	// PaperCryptContainerVersionMajor1 container format from PaperCryptV1, used for backwards compatibility
	PaperCryptContainerVersionMajor1 PaperCryptContainerVersion = 1
	// PaperCryptContainerVersionMajor2 container format for PaperCrypt
	PaperCryptContainerVersionMajor2 PaperCryptContainerVersion = 2
	// PaperCryptContainerVersionDevel is used instead of a set version number for development builds
	PaperCryptContainerVersionDevel PaperCryptContainerVersion = PaperCryptContainerVersion(
		0xFFFFFFFF,
	)
)

func PaperCryptContainerVersionFromString

func PaperCryptContainerVersionFromString(s string) PaperCryptContainerVersion

PaperCryptContainerVersionFromString parses a version string to discover the major version of this software

func (PaperCryptContainerVersion) String

String serializes the PaperCryptContainerVersion to a string of either a number corresponding to the major version, "devel" for a development build, or "unknown"

type PaperCryptDataFormat

type PaperCryptDataFormat uint8

PaperCryptDataFormat is an enum (uint8) of supported container formats

const (
	// PaperCryptDataFormatPGP marks that a container holds data enclosed in a PGP container
	PaperCryptDataFormatPGP PaperCryptDataFormat = 0
	// PaperCryptDataFormatRaw represents that the data encoded in the container is raw, i.e. has not been encrypted by papercrypt
	PaperCryptDataFormatRaw PaperCryptDataFormat = 1
)

func PaperCryptDataFormatFromString

func PaperCryptDataFormatFromString(s string) PaperCryptDataFormat

PaperCryptDataFormatFromString parses a container data format as a string, returning the corresponding enum value

func (PaperCryptDataFormat) String

func (f PaperCryptDataFormat) String() string

String serializes the enum value to a string deserializable by PaperCryptDataFormatFromString

type PaperCryptV1

type PaperCryptV1 struct {
	// Version is the version of papercrypt used to generate the document.
	Version string `json:"Version"`

	// Data is the encrypted data as a PGP message
	Data *crypto.PGPMessage `json:"Data"`

	// SerialNumber is the serial number of document, used to identify it. It is generated randomly if not provided.
	SerialNumber string `json:"SerialNumber"`

	// Purpose is the purpose of document
	Purpose string `json:"Purpose"`

	// Comment is the comment on document
	Comment string `json:"Comment"`

	// CreatedAt is the creation timestamp
	CreatedAt time.Time `json:"CreatedAt"`

	// DataCRC24 is the CRC-24 checksum of the encrypted data
	DataCRC24 uint32 `json:"DataCRC24"`

	// DataCRC32 is the CRC-32 checksum of the encrypted data
	DataCRC32 uint32 `json:"DataCRC32"`

	// DataSHA256 is the SHA-256 checksum of the encrypted data
	DataSHA256 [32]byte `json:"DataSHA256"`
}

func NewPaperCryptV1

func NewPaperCryptV1(version string, data *crypto.PGPMessage, serialNumber string, purpose string, comment string, createdAt time.Time) *PaperCryptV1

NewPaperCryptV1 creates a new paper crypt

func (*PaperCryptV1) ToNextVersion

func (p *PaperCryptV1) ToNextVersion() (*PaperCrypt, error)

ToNextVersion converts the PaperCryptV1 to the next version, PaperCrypt, by encoding format information and compressing the data.

Jump to

Keyboard shortcuts

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