Documentation
¶
Index ¶
- Variables
- func Base64(filename string) (b []byte, err error)
- func CopyFile(src, dst string) (int64, error)
- func DownloadFromWebStore(extensionID string, filename string) error
- func ID(filename string) (id string, err error)
- func IDFromPubKey(pubKey []byte) (string, error)
- func LoadPrivateKey(filename string) (*rsa.PrivateKey, error)
- func NewPrivateKey() (*rsa.PrivateKey, error)
- func Pack(src string, dst string, pk *rsa.PrivateKey) (err error)
- func PackZipToCRX(zip io.ReadSeeker, w io.Writer, pk *rsa.PrivateKey) error
- func PrivateKeyToPEM(key *rsa.PrivateKey) []byte
- func ReadZipFile(filename string) (*bytes.Reader, error)
- func SavePrivateKey(filename string, key *rsa.PrivateKey) error
- func SetDefaultKeySize(size int)
- func SetWebStoreURL(u string)
- func Unpack(filename string) error
- func UnpackTo(filename string, dirname string) error
- func Unzip(r io.ReaderAt, size int64, unpacked string) error
- func UnzipTo(basepath string, filename string) error
- func WritePrivateKey(w io.Writer, key *rsa.PrivateKey) error
- func Zip(dst io.Writer, dirname string) error
- func ZipTo(filename string, dirname string) error
- type Extension
- func (e Extension) Base64() ([]byte, error)
- func (e Extension) ID() (string, error)
- func (e Extension) IsCRX3() bool
- func (e Extension) IsDir() bool
- func (e Extension) IsEmpty() bool
- func (e Extension) IsZip() bool
- func (e Extension) Pack(pk *rsa.PrivateKey) error
- func (e Extension) PackTo(dst string, pk *rsa.PrivateKey) error
- func (e Extension) String() string
- func (e Extension) Unpack() error
- func (e Extension) Unzip() error
- func (e Extension) WriteTo(w io.Writer, pk *rsa.PrivateKey) error
- func (e Extension) Zip() error
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknownFileExtension = errors.New("crx3: unknown file extension") ErrUnsupportedFileFormat = errors.New("crx3: unsupported file format") ErrExtensionNotSpecified = errors.New("crx3: extension id not specified") ErrPathNotFound = errors.New("crx3: filepath not found") ErrPrivateKeyNotFound = errors.New("crx3: private key not found") ErrInvalidReader = errors.New("crx3: invalid reader") )
Functions ¶
func Base64 ¶ added in v1.3.0
Base64 encodes an extension file to a base64 string. It returns a bytes and an error encountered while encodes, if any.
func CopyFile ¶ added in v1.4.0
CopyFile copies the contents of the source file 'src' to the destination file 'dst'. It returns the number of bytes copied and any encountered error. If the source file does not exist, is not a regular file, or other errors occur during the copy, it returns an error with a descriptive message.
func DownloadFromWebStore ¶
DownloadFromWebStore downloads a Chrome extension from the web store. ExtensionID can be an identifier or an url.
func ID ¶ added in v1.2.0
ID returns the extension ID extracted from a CRX (Chrome Extension) file specified by 'filename'. It checks if the file is in the CRX format, reads its header and signed data, and then converts the CRX ID into a string representation
func IDFromPubKey ¶ added in v1.5.1
IDFromPubKey generates the Chrome Extension ID from a public key. It handles PEM formatting, base64 decoding, and SHA-256 hashing to produce the ID. Returns the ID or an error if the key processing fails.
func LoadPrivateKey ¶
func LoadPrivateKey(filename string) (*rsa.PrivateKey, error)
LoadPrivateKey loads the RSA private key from the specified 'filename' into memory. It returns the loaded private key or an error if the key cannot be loaded.
func NewPrivateKey ¶
func NewPrivateKey() (*rsa.PrivateKey, error)
NewPrivateKey returns a new RSA private key with a bit size of 4096.
func Pack ¶
func Pack(src string, dst string, pk *rsa.PrivateKey) (err error)
Pack packs a zip file or unzipped directory into a crx extension. It takes the source 'src' (zip file or directory), target 'dst' CRX file path, and a private key 'pk' (optional). If 'pk' is nil, it generates a new private key. It creates a CRX extension from the source and writes it to the destination.
func PackZipToCRX ¶ added in v1.5.0
func PackZipToCRX(zip io.ReadSeeker, w io.Writer, pk *rsa.PrivateKey) error
PackZipToCRX reads a ZIP archive from the provided Reader, signs it using the provided RSA private key, and writes the signed CRX file to the provided Writer. This function is essential for producing production-ready CRX files that require digital signatures to be installed in browsers. The function will return an error if any issues occur during the zip reading, signing, or CRX writing processes.
func PrivateKeyToPEM ¶ added in v1.5.0
func PrivateKeyToPEM(key *rsa.PrivateKey) []byte
PublicKeyToPEM converts the provided RSA public key to a PEM-encoded byte slice.
func ReadZipFile ¶ added in v1.5.0
ReadZipFile opens and reads the contents of a zip file specified by 'filename'. It can handle both direct paths to zip files or directories. If 'filename' is a directory, the function zips its contents into a buffer and returns a reader for that buffer. If 'filename' is a zip file, it reads the file into a buffer and returns a reader for it. The function returns a *bytes.Reader to allow random access reads, which is particularly useful for large files. It returns an error if the file cannot be opened, read, or if the path does not correspond to a zip file or directory.
func SavePrivateKey ¶
func SavePrivateKey(filename string, key *rsa.PrivateKey) error
SavePrivateKey saves the provided 'key' private key to the specified 'filename'. If 'key' is nil, it generates a new private key and saves it to the file.
func SetDefaultKeySize ¶ added in v1.5.0
func SetDefaultKeySize(size int)
SetDefaultKeySize sets the global default key size for RSA key generation. It accepts key sizes of 2048, 3072, or 4096 bits. If a size outside these specified options is passed, the function panics to prevent the use of an unsupported key size, which could lead to security vulnerabilities. This strict enforcement helps ensure that only strong, widely accepted key sizes are used throughout the application.
Usage:
SetDefaultKeySize(2048) // sets the default key size to 2048 bits SetDefaultKeySize(4096) // sets the default key size to 4096 bits
Valid key sizes are:
- 2048 bits
- 3072 bits
- 4096 bits
Panics:
This function will panic if any key size other than the above mentioned valid sizes is attempted to be set. This is a deliberate design choice to catch incorrect key size settings during the development phase.
func SetWebStoreURL ¶ added in v1.3.0
func SetWebStoreURL(u string)
SetWebStoreURL sets the web store url to download extensions.
func Unpack ¶
Unpack unpacks a CRX (Chrome Extension) file specified by 'filename' to its original contents. It checks if the file is in the CRX format, reads its header and signed data, and then extracts and decompresses the original contents. The unpacked contents are placed in a directory with the same name as the original file (without the '.crx' extension).
func UnpackTo ¶ added in v1.4.0
UnpackTo unpacks a CRX (Chrome Extension) file specified by 'filename' to the directory 'dirname'. If 'dirname' does not exist, it creates the directory before unpacking.
func Unzip ¶
Unzip extracts all files and directories from the provided ZIP archive. It takes an io.ReaderAt 'r', the size 'size' of the ZIP archive, and the target directory 'unpacked' for extraction. It iterates through the archive, creating directories and writing files as necessary.
func UnzipTo ¶ added in v1.4.0
UnzipTo extracts the contents of a ZIP archive specified by 'filename' to the 'basepath' directory. It opens the ZIP file, creates the necessary directory structure, and extracts all files.
func WritePrivateKey ¶ added in v1.5.0
func WritePrivateKey(w io.Writer, key *rsa.PrivateKey) error
WritePrivateKey writes the RSA private key to the provided io.Writer in the PEM format. The function expects a non-nil *rsa.PrivateKey. If the key is nil, it returns an ErrPrivateKeyNotFound error. This function handles the marshalling of the private key into PKCS#8 format and then encodes it into PEM format before writing.
Parameters:
w : An io.Writer to which the PEM encoded private key will be written. key : A non-nil *rsa.PrivateKey that will be marshalled and written.
Returns:
An error if the private key is nil, if there is a marshalling error, or if writing to the io.Writer fails. The error includes a descriptive message to aid in debugging.
Usage example:
file, err := os.Create("private_key.pem") if err != nil { log.Fatal(err) } defer file.Close() privKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { log.Fatal(err) } if err := WritePrivateKey(file, privKey); err != nil { log.Printf("Failed to write private key: %v", err) }
Note:
This function does not close the io.Writer; the caller is responsible for managing the writer's lifecycle, including opening and closing it.
Types ¶
type Extension ¶
type Extension string
Extension represents an extension for google chrome.
func (Extension) ID ¶ added in v1.2.0
ID calculates the Chrome Extension ID for the Extension instance. It supports directories, ZIP archives, and CRX3 files. If the extension is unpacked, contained in a ZIP archive, or is a CRX3 file with a specified key in its manifest, the ID is generated from this key. The function returns an error if the extension is empty, the file cannot be read, the key is not found, or the file format is unsupported.
func (Extension) Pack ¶
func (e Extension) Pack(pk *rsa.PrivateKey) error
Pack packs zip file or an unpacked directory into a CRX3 file.
func (Extension) PackTo ¶
func (e Extension) PackTo(dst string, pk *rsa.PrivateKey) error
PackTo packs zip file or an unpacked directory into a CRX3 file.
func (Extension) WriteTo ¶ added in v1.5.0
WriteTo packs the contents of the Extension into a CRX file and writes it to the provided io.Writer. This method requires a non-nil *rsa.PrivateKey to sign the CRX package. The Extension must not be empty, and its associated zip file must be readable and correctly formatted.
Parameters:
w - The io.Writer where the CRX file will be written. pk - The RSA private key used for signing the CRX file.
Returns:
An error if the Extension is empty, if the private key is nil, if there are issues reading the zip file associated with the Extension, or if there is a failure during the packing process. Errors are wrapped with context to provide more details about the failure.
Usage example:
ext := Extension("path/to/your/extension/folder") // OR zip file pk, err := rsa.GenerateKey(rand.Reader, 4096) if err != nil { log.Fatalf("Failed to generate private key: %v", err) } var buf bytes.Buffer if err := ext.WriteTo(&buf, pk); err != nil { log.Printf("Failed to write CRX: %v", err) } else { // Use buf to save CRX to a file or further processing }