Picocrypt/src/Picocrypt.go

1783 lines
47 KiB
Go
Raw Normal View History

2021-11-13 13:03:21 +13:00
package main
/*
2021-12-23 05:56:33 +13:00
2022-05-03 10:05:54 +12:00
Picocrypt v1.28
2021-11-13 13:03:21 +13:00
Copyright (c) Evan Su (https://evansu.cc)
Released under a GNU GPL v3 License
https://github.com/HACKERALERT/Picocrypt
~ In cryptography we trust ~
*/
import (
_ "embed"
"archive/zip"
"bytes"
2022-03-20 05:43:32 +13:00
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/subtle"
2021-11-13 13:03:21 +13:00
"fmt"
"hash"
"image"
"image/color"
"io"
"math"
"math/big"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
2022-03-20 05:43:32 +13:00
"github.com/HACKERALERT/clipboard"
2022-04-14 13:32:11 +12:00
"github.com/HACKERALERT/crypto/argon2"
"github.com/HACKERALERT/crypto/blake2b"
"github.com/HACKERALERT/crypto/chacha20"
"github.com/HACKERALERT/crypto/hkdf"
"github.com/HACKERALERT/crypto/sha3"
2022-03-20 05:43:32 +13:00
"github.com/HACKERALERT/dialog"
"github.com/HACKERALERT/giu"
"github.com/HACKERALERT/infectious"
"github.com/HACKERALERT/serpent"
"github.com/HACKERALERT/zxcvbn-go"
2021-11-13 13:03:21 +13:00
)
// Generic variables
2022-05-03 10:05:54 +12:00
var version = "v1.28"
2021-11-13 13:03:21 +13:00
var window *giu.MasterWindow
var dpi float32
var mode string
var working bool
var recombine bool
2022-04-02 16:16:04 +13:00
// Popup modals
var modalId int // A hack to keep modals centered
var showPassgen bool // Password generator
var showKeyfile bool // Keyfile manager
var showProgress bool // Encryption/decryption progress
var showConfirmation bool // Confirm overwriting an existing file
// Input and output files
2021-11-13 13:03:21 +13:00
var onlyFiles []string
var onlyFolders []string
var allFiles []string
var inputLabel = "Drop files and folders into this window."
var inputFile string
2022-04-02 16:16:04 +13:00
var outputFile string
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// Password and generator variables
2021-11-13 13:03:21 +13:00
var password string
2022-04-02 16:16:04 +13:00
var cpassword string
2021-11-13 13:03:21 +13:00
var passwordStrength int
var passwordState = giu.InputTextFlagsPassword
var passwordStateLabel = "Show"
2022-04-02 16:16:04 +13:00
var passgenCopy = true
var passgenLength int32 = 32
var passgenUpper = true
var passgenLower = true
var passgenNums = true
var passgenSymbols = true
2021-11-13 13:03:21 +13:00
// Keyfile variables
var keyfile bool
var keyfiles []string
var keyfileOrderMatters bool
var keyfilePrompt = "None selected."
2022-04-02 16:16:04 +13:00
// Comments variables
var comments string
var commentsPrompt = "Comments:"
var commentsDisabled bool
2021-11-13 13:03:21 +13:00
// Advanced options
var paranoid bool
var reedsolo bool
var deleteWhenDone bool
var split bool
var splitSize string
2022-05-02 09:13:28 +12:00
var splitUnits = []string{"KiB", "MiB", "GiB", "TiB", "Total"}
2021-11-13 13:03:21 +13:00
var splitSelected int32 = 1
var compress bool
var keep bool
var kept bool
// Status variables
2022-04-14 13:32:11 +12:00
var startLabel = "Start"
2021-11-13 13:03:21 +13:00
var mainStatus = "Ready."
var mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
var popupStatus string
// Progress variables
var progress float32
var progressInfo string
2022-04-02 16:16:04 +13:00
// Reed-Solomon codecs
2021-11-13 13:03:21 +13:00
var rs1, _ = infectious.NewFEC(1, 3) // 1 data shard, 3 total -> 2 parity shards
var rs5, _ = infectious.NewFEC(5, 15)
var rs16, _ = infectious.NewFEC(16, 48)
var rs24, _ = infectious.NewFEC(24, 72)
var rs32, _ = infectious.NewFEC(32, 96)
var rs64, _ = infectious.NewFEC(64, 192)
2022-04-02 16:16:04 +13:00
var rs128, _ = infectious.NewFEC(128, 136) // Used for full Reed-Solomon on files
// A passthrough and related helpers to get compression progress
var compressDone int64
var compressTotal int64
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
type compressorProgress struct {
io.Reader
}
func (p *compressorProgress) Read(data []byte) (int, error) {
read, err := p.Reader.Read(data)
compressDone += int64(read)
progress = float32(compressDone) / float32(compressTotal)
giu.Update()
return read, err
}
// The graphical user interface
2021-11-13 13:03:21 +13:00
func draw() {
2022-03-20 16:23:11 +13:00
giu.SingleWindow().Flags(524351).Layout(
2021-11-13 13:03:21 +13:00
giu.Custom(func() {
2022-04-02 16:16:04 +13:00
if showPassgen {
giu.PopupModal("Generate password:##"+strconv.Itoa(modalId)).Flags(6).Layout(
2022-03-20 05:43:32 +13:00
giu.Row(
giu.Label("Length:"),
2022-04-02 16:16:04 +13:00
giu.SliderInt(&passgenLength, 4, 64).Size(giu.Auto),
2022-03-20 05:43:32 +13:00
),
2022-04-02 16:16:04 +13:00
giu.Checkbox("Uppercase", &passgenUpper),
giu.Checkbox("Lowercase", &passgenLower),
giu.Checkbox("Numbers", &passgenNums),
giu.Checkbox("Symbols", &passgenSymbols),
giu.Checkbox("Copy to clipboard", &passgenCopy),
2022-03-20 05:43:32 +13:00
giu.Row(
giu.Button("Cancel").Size(100, 0).OnClick(func() {
giu.CloseCurrentPopup()
2022-04-02 16:16:04 +13:00
showPassgen = false
2022-03-20 05:43:32 +13:00
}),
giu.Button("Generate").Size(100, 0).OnClick(func() {
2022-04-02 16:16:04 +13:00
password = genPassword()
cpassword = password
2022-03-20 05:43:32 +13:00
passwordStrength = zxcvbn.PasswordStrength(password, nil).Score
giu.CloseCurrentPopup()
2022-04-02 16:16:04 +13:00
showPassgen = false
2022-03-20 05:43:32 +13:00
}),
),
).Build()
2022-04-02 16:16:04 +13:00
giu.OpenPopup("Generate password:##" + strconv.Itoa(modalId))
2022-03-20 05:43:32 +13:00
giu.Update()
}
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
if showKeyfile {
2022-04-02 16:16:04 +13:00
giu.PopupModal("Manage keyfiles:##"+strconv.Itoa(modalId)).Flags(70).Layout(
2022-03-20 05:43:32 +13:00
giu.Label("Drag and drop your keyfiles here."),
2021-11-13 13:03:21 +13:00
giu.Custom(func() {
2022-03-20 05:43:32 +13:00
if mode != "decrypt" {
2022-04-02 16:16:04 +13:00
giu.Checkbox("Require correct order", &keyfileOrderMatters).Build()
giu.Tooltip("Decryption will require the correct keyfile order.").Build()
2022-03-20 05:43:32 +13:00
} else if keyfileOrderMatters {
2022-04-02 16:16:04 +13:00
giu.Label("Correct order is required.").Build()
2021-11-13 13:03:21 +13:00
}
}),
2022-04-02 16:16:04 +13:00
giu.Separator(),
2021-11-13 13:03:21 +13:00
giu.Custom(func() {
2022-03-20 05:43:32 +13:00
for _, i := range keyfiles {
2022-04-02 16:16:04 +13:00
giu.Label(filepath.Base(i)).Build()
2021-11-13 13:03:21 +13:00
}
}),
2022-03-20 05:43:32 +13:00
giu.Row(
2022-04-02 16:16:04 +13:00
giu.Button("Clear").Size(100, 0).OnClick(func() {
2022-03-20 05:43:32 +13:00
keyfiles = nil
2022-04-14 13:32:11 +12:00
if keyfile {
keyfilePrompt = "Keyfiles required."
} else {
keyfilePrompt = "None selected."
}
2022-04-02 16:16:04 +13:00
modalId++
2022-03-20 05:43:32 +13:00
}),
giu.Tooltip("Remove all keyfiles."),
2022-04-02 16:16:04 +13:00
giu.Button("Done").Size(100, 0).OnClick(func() {
2022-03-20 05:43:32 +13:00
giu.CloseCurrentPopup()
showKeyfile = false
}),
),
).Build()
2022-04-02 16:16:04 +13:00
giu.OpenPopup("Manage keyfiles:##" + strconv.Itoa(modalId))
2022-03-20 05:43:32 +13:00
giu.Update()
}
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
if showConfirmation {
2022-04-02 16:16:04 +13:00
giu.PopupModal("Warning:##"+strconv.Itoa(modalId)).Flags(6).Layout(
2022-03-20 05:43:32 +13:00
giu.Label("Output already exists. Overwrite?"),
giu.Row(
giu.Button("No").Size(100, 0).OnClick(func() {
giu.CloseCurrentPopup()
showConfirmation = false
}),
giu.Button("Yes").Size(100, 0).OnClick(func() {
giu.CloseCurrentPopup()
showConfirmation = false
2022-04-02 16:16:04 +13:00
modalId++
2022-03-20 05:43:32 +13:00
showProgress = true
2021-11-13 13:03:21 +13:00
giu.Update()
2022-03-20 05:43:32 +13:00
go func() {
work()
working = false
showProgress = false
giu.Update()
}()
}),
),
).Build()
2022-04-02 16:16:04 +13:00
giu.OpenPopup("Warning:##" + strconv.Itoa(modalId))
2022-03-20 05:43:32 +13:00
giu.Update()
}
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
if showProgress {
2022-04-02 16:16:04 +13:00
giu.PopupModal(" ##"+strconv.Itoa(modalId)).Flags(6).Layout(
2021-11-13 13:03:21 +13:00
giu.Row(
2022-04-02 16:16:04 +13:00
giu.ProgressBar(progress).Size(180, 0).Overlay(progressInfo),
2022-03-20 05:43:32 +13:00
giu.Button("Cancel").Size(58, 0).OnClick(func() {
working = false
2021-11-13 13:03:21 +13:00
}),
),
2022-03-20 05:43:32 +13:00
giu.Label(popupStatus),
).Build()
2022-04-02 16:16:04 +13:00
giu.OpenPopup(" ##" + strconv.Itoa(modalId))
2022-03-20 05:43:32 +13:00
giu.Update()
}
}),
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
giu.Row(
giu.Label(inputLabel),
giu.Custom(func() {
bw, _ := giu.CalcTextSize("Clear")
p, _ := giu.GetWindowPadding()
bw += p * 2
2022-04-02 16:16:04 +13:00
giu.Dummy((bw+p)/-dpi, 0).Build()
2022-03-20 05:43:32 +13:00
giu.SameLine()
giu.Style().SetDisabled(len(allFiles) == 0 && len(onlyFiles) == 0).To(
giu.Button("Clear").Size(bw/dpi, 0).OnClick(resetUI),
giu.Tooltip("Clear all input files and reset UI state."),
).Build()
}),
),
giu.Separator(),
giu.Style().SetDisabled(len(allFiles) == 0 && len(onlyFiles) == 0).To(
2022-04-02 16:16:04 +13:00
giu.Label("Password:"),
2022-03-20 05:43:32 +13:00
giu.Row(
giu.Button(passwordStateLabel).Size(54, 0).OnClick(func() {
if passwordState == giu.InputTextFlagsPassword {
passwordState = giu.InputTextFlagsNone
passwordStateLabel = "Hide"
} else {
passwordState = giu.InputTextFlagsPassword
passwordStateLabel = "Show"
}
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Toggle the visibility of password entries."),
2022-03-20 05:43:32 +13:00
giu.Button("Clear").Size(54, 0).OnClick(func() {
password = ""
2022-04-02 16:16:04 +13:00
cpassword = ""
2022-03-20 05:43:32 +13:00
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Clear the password entries."),
2022-03-20 05:43:32 +13:00
giu.Button("Copy").Size(54, 0).OnClick(func() {
clipboard.WriteAll(password)
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Copy the password into your clipboard."),
2022-03-20 05:43:32 +13:00
giu.Button("Paste").Size(54, 0).OnClick(func() {
tmp, _ := clipboard.ReadAll()
password = tmp
if mode != "decrypt" {
2022-04-02 16:16:04 +13:00
cpassword = tmp
2022-03-20 05:43:32 +13:00
}
passwordStrength = zxcvbn.PasswordStrength(password, nil).Score
giu.Update()
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Paste a password from your clipboard."),
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
giu.Style().SetDisabled(mode == "decrypt").To(
giu.Button("Create").Size(54, 0).OnClick(func() {
2022-04-02 16:16:04 +13:00
modalId++
showPassgen = true
2022-03-20 05:43:32 +13:00
}),
),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Generate a cryptographically secure password."),
2022-03-20 05:43:32 +13:00
),
giu.Row(
giu.InputText(&password).Flags(passwordState).Size(302/dpi).OnChange(func() {
passwordStrength = zxcvbn.PasswordStrength(password, nil).Score
}),
giu.Custom(func() {
c := giu.GetCanvas()
p := giu.GetCursorScreenPos()
2022-04-02 16:16:04 +13:00
col := color.RGBA{
uint8(0xc8 - 31*passwordStrength),
uint8(0x4c + 31*passwordStrength), 0x4b, 0xff,
2022-03-20 05:43:32 +13:00
}
if password == "" || mode == "decrypt" {
col = color.RGBA{0xff, 0xff, 0xff, 0x00}
}
2021-11-13 13:03:21 +13:00
2022-03-20 05:43:32 +13:00
path := p.Add(image.Pt(
2022-04-02 16:16:04 +13:00
int(math.Round(-20*float64(dpi))),
int(math.Round(12*float64(dpi))),
2022-03-20 05:43:32 +13:00
))
2022-04-02 16:16:04 +13:00
c.PathArcTo(path, 6*dpi, -math.Pi/2, math.Pi*(.4*float32(passwordStrength)-.1), -1)
2022-03-20 05:43:32 +13:00
c.PathStroke(col, false, 2)
}),
),
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
giu.Dummy(0, 0),
giu.Style().SetDisabled(password == "" || mode == "decrypt").To(
giu.Label("Confirm password:"),
giu.Row(
giu.InputText(&cpassword).Flags(passwordState).Size(302/dpi),
giu.Custom(func() {
c := giu.GetCanvas()
p := giu.GetCursorScreenPos()
col := color.RGBA{0x4c, 0xc8, 0x4b, 0xff}
if cpassword != password {
col = color.RGBA{0xc8, 0x4c, 0x4b, 0xff}
}
if password == "" || cpassword == "" || mode == "decrypt" {
col = color.RGBA{0xff, 0xff, 0xff, 0x00}
}
path := p.Add(image.Pt(
int(math.Round(-20*float64(dpi))),
int(math.Round(12*float64(dpi))),
))
c.PathArcTo(path, 6*dpi, 0, 2*math.Pi, -1)
c.PathStroke(col, false, 2)
}),
2022-03-20 05:43:32 +13:00
),
),
2022-04-02 16:16:04 +13:00
giu.Dummy(0, 0),
giu.Style().SetDisabled(mode == "decrypt" && !keyfile).To(
giu.Row(
giu.Label("Keyfiles:"),
giu.Button("Edit").Size(54, 0).OnClick(func() {
modalId++
showKeyfile = true
}),
giu.Tooltip("Manage your keyfiles."),
giu.Style().SetDisabled(mode == "decrypt").To(
giu.Button("Create").Size(54, 0).OnClick(func() {
2022-04-14 13:32:11 +12:00
f := dialog.File().Title("Choose where to save the keyfile.")
2022-04-02 16:16:04 +13:00
f.SetStartDir(func() string {
if len(onlyFiles) > 0 {
return filepath.Dir(onlyFiles[0])
}
return filepath.Dir(onlyFolders[0])
}())
2022-04-14 13:32:11 +12:00
f.SetInitFilename("Keyfile")
file, err := f.Save()
if file == "" || err != nil {
2022-04-02 16:16:04 +13:00
return
2021-11-13 13:03:21 +13:00
}
2022-03-20 05:43:32 +13:00
2022-04-02 16:16:04 +13:00
fout, _ := os.Create(file)
2022-04-19 09:46:13 +12:00
data := make([]byte, 1<<20)
2022-04-02 16:16:04 +13:00
rand.Read(data)
fout.Write(data)
fout.Close()
2021-11-13 13:03:21 +13:00
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Generate a cryptographically secure keyfile."),
),
giu.Style().SetDisabled(true).To(
2022-04-19 09:46:13 +12:00
giu.InputText(&keyfilePrompt).Size(giu.Auto),
2021-11-13 13:03:21 +13:00
),
2022-03-20 05:43:32 +13:00
),
),
),
giu.Separator(),
2022-05-02 09:13:28 +12:00
giu.Style().SetDisabled((mode == "decrypt" && comments == "") ||
(mode != "decrypt" && ((len(keyfiles) == 0 && password == "") || (password != cpassword)))).To(
giu.Style().SetDisabled(mode == "decrypt" && comments == "").To(
giu.Label(commentsPrompt),
giu.InputText(&comments).Size(giu.Auto).Flags(func() giu.InputTextFlags {
if commentsDisabled {
return giu.InputTextFlagsReadOnly
}
return giu.InputTextFlagsNone
}()),
),
),
2022-04-19 09:46:13 +12:00
giu.Style().SetDisabled((len(keyfiles) == 0 && password == "") ||
(mode == "encrypt" && password != cpassword)).To(
2022-03-20 05:43:32 +13:00
giu.Label("Advanced:"),
giu.Custom(func() {
if mode != "decrypt" {
giu.Row(
2022-04-02 16:16:04 +13:00
giu.Checkbox("Paranoid mode", &paranoid),
giu.Tooltip("Provides the highest level of security attainable."),
giu.Dummy(-170, 0),
2022-03-20 05:43:32 +13:00
giu.Style().SetDisabled(!(len(allFiles) > 1 || len(onlyFolders) > 0)).To(
giu.Checkbox("Compress files", &compress),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Compress files with Deflate before encrypting."),
2021-11-13 13:03:21 +13:00
),
2022-03-20 05:43:32 +13:00
).Build()
2022-04-02 16:16:04 +13:00
giu.Row(
giu.Checkbox("Reed-Solomon", &reedsolo),
giu.Tooltip("Prevent file corruption by erasure coding (slow)."),
giu.Dummy(-170, 0),
giu.Checkbox("Delete files", &deleteWhenDone),
giu.Tooltip("Delete the input files after encryption."),
).Build()
2022-03-20 05:43:32 +13:00
giu.Row(
2022-04-02 16:16:04 +13:00
giu.Checkbox("Split into chunks:", &split),
giu.Tooltip("Split the output file into smaller chunks."),
giu.Dummy(-170, 0),
giu.InputText(&splitSize).Size(86/dpi).Flags(1).OnChange(func() {
2022-03-20 05:43:32 +13:00
split = splitSize != ""
}),
2022-04-02 16:16:04 +13:00
giu.Tooltip("Choose the chunk size."),
giu.Combo("##splitter", splitUnits[splitSelected], splitUnits, &splitSelected).Size(68),
giu.Tooltip("Choose the chunk size units."),
2022-03-20 05:43:32 +13:00
).Build()
} else {
2022-04-02 16:16:04 +13:00
giu.Row(
giu.Checkbox("Force decrypt", &keep),
giu.Tooltip("Override security measures when decrypting."),
giu.Dummy(-170, 0),
giu.Checkbox("Delete volume", &deleteWhenDone),
giu.Tooltip("Delete the volume after a successful decryption."),
).Build()
2022-03-20 05:43:32 +13:00
}
}),
giu.Label("Save output as:"),
giu.Custom(func() {
w, _ := giu.GetAvailableRegion()
bw, _ := giu.CalcTextSize("Change")
p, _ := giu.GetWindowPadding()
bw += p * 2
dw := w - bw - p
giu.Style().SetDisabled(true).To(
2022-04-19 09:46:13 +12:00
giu.InputText(func() *string {
tmp := ""
if outputFile == "" {
return &tmp
}
tmp = filepath.Base(outputFile)
return &tmp
}()).Size(dw / dpi / dpi).Flags(16384),
2022-03-20 05:43:32 +13:00
).Build()
2022-04-14 13:32:11 +12:00
2022-03-20 05:43:32 +13:00
giu.SameLine()
giu.Button("Change").Size(bw/dpi, 0).OnClick(func() {
2022-04-14 13:32:11 +12:00
f := dialog.File().Title("Choose where to save the output. Don't include extensions.")
2022-04-02 16:16:04 +13:00
f.SetStartDir(func() string {
if len(onlyFiles) > 0 {
return filepath.Dir(onlyFiles[0])
}
return filepath.Dir(onlyFolders[0])
}())
2022-04-14 13:32:11 +12:00
// Prefill the filename
tmp := strings.TrimSuffix(filepath.Base(outputFile), ".pcv")
f.SetInitFilename(strings.TrimSuffix(tmp, filepath.Ext(tmp)))
if mode == "encrypt" && (len(allFiles) > 1 || len(onlyFolders) > 0) {
f.SetInitFilename("Encrypted")
}
file, err := f.Save()
if file == "" || err != nil {
2022-03-20 05:43:32 +13:00
return
}
2021-11-13 13:03:21 +13:00
2022-04-14 13:32:11 +12:00
// Add the correct extensions
2022-03-20 05:43:32 +13:00
if mode == "encrypt" {
if len(allFiles) > 1 || len(onlyFolders) > 0 {
2022-04-14 13:32:11 +12:00
file += ".zip.pcv"
2022-03-20 05:43:32 +13:00
} else {
2022-04-14 13:32:11 +12:00
file += filepath.Ext(inputFile) + ".pcv"
2022-03-20 05:43:32 +13:00
}
} else {
if strings.HasSuffix(inputFile, ".zip.pcv") {
file += ".zip"
} else {
tmp := strings.TrimSuffix(filepath.Base(inputFile), ".pcv")
2022-04-14 13:32:11 +12:00
file += filepath.Ext(tmp)
2022-03-20 05:43:32 +13:00
}
}
outputFile = file
}).Build()
2022-04-02 16:16:04 +13:00
giu.Tooltip("Save the output with a custom name and path.").Build()
2022-03-20 05:43:32 +13:00
}),
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
giu.Dummy(0, 0),
2022-03-20 05:43:32 +13:00
giu.Separator(),
2022-04-02 16:16:04 +13:00
giu.Dummy(0, 0),
2022-04-14 13:32:11 +12:00
giu.Button(startLabel).Size(giu.Auto, 34).OnClick(func() {
2022-03-20 05:43:32 +13:00
if keyfile && keyfiles == nil {
mainStatus = "Please select your keyfiles."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
return
}
2022-05-02 09:13:28 +12:00
tmp, err := strconv.Atoi(splitSize)
if split && (splitSize == "" || tmp <= 0 || err != nil) {
mainStatus = "Invalid split size."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
return
}
_, err = os.Stat(outputFile)
2022-03-20 05:43:32 +13:00
if err == nil {
2022-04-02 16:16:04 +13:00
modalId++
2022-03-20 05:43:32 +13:00
showConfirmation = true
giu.Update()
} else {
2022-04-02 16:16:04 +13:00
modalId++
2022-03-20 05:43:32 +13:00
showProgress = true
giu.Update()
go func() {
work()
working = false
showProgress = false
giu.Update()
}()
}
}),
giu.Style().SetColor(giu.StyleColorText, mainStatusColor).To(
giu.Label(mainStatus),
),
),
2021-11-13 13:03:21 +13:00
giu.Custom(func() {
2022-04-02 16:16:04 +13:00
window.SetSize(int(318*dpi), giu.GetCursorPos().Y+1)
2021-11-13 13:03:21 +13:00
}),
)
}
func onDrop(names []string) {
if showKeyfile {
keyfiles = append(keyfiles, names...)
2022-04-02 16:16:04 +13:00
// Remove duplicate keyfiles
2021-11-13 13:03:21 +13:00
var tmp []string
for _, i := range keyfiles {
duplicate := false
for _, j := range tmp {
if i == j {
duplicate = true
}
}
stat, _ := os.Stat(i)
if !duplicate && !stat.IsDir() {
tmp = append(tmp, i)
}
}
keyfiles = tmp
2022-04-02 16:16:04 +13:00
// Update the keyfile status
2021-11-13 13:03:21 +13:00
if len(keyfiles) == 1 {
2022-03-20 05:43:32 +13:00
keyfilePrompt = "Using 1 keyfile."
2021-11-13 13:03:21 +13:00
} else {
2022-03-20 05:43:32 +13:00
keyfilePrompt = fmt.Sprintf("Using %d keyfiles.", len(keyfiles))
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Recenter the keyfile modal
modalId++
2021-11-13 13:03:21 +13:00
return
}
2022-04-02 16:16:04 +13:00
// Clear variables and UI state
2021-11-13 13:03:21 +13:00
recombine = false
onlyFiles = nil
onlyFolders = nil
allFiles = nil
files, folders := 0, 0
2022-04-19 09:46:13 +12:00
size := 0
2021-11-13 13:03:21 +13:00
resetUI()
2022-04-02 16:16:04 +13:00
// One item dropped
2021-11-13 13:03:21 +13:00
if len(names) == 1 {
stat, _ := os.Stat(names[0])
2022-04-02 16:16:04 +13:00
// A folder was dropped
2021-11-13 13:03:21 +13:00
if stat.IsDir() {
folders++
2022-04-02 16:16:04 +13:00
mode = "encrypt"
2022-03-20 05:43:32 +13:00
inputLabel = "1 folder selected."
2022-04-14 13:32:11 +12:00
startLabel = "Encrypt"
2021-11-13 13:03:21 +13:00
onlyFolders = append(onlyFolders, names[0])
2022-03-20 05:43:32 +13:00
inputFile = filepath.Join(filepath.Dir(names[0]), "Encrypted") + ".zip"
2022-04-02 16:16:04 +13:00
outputFile = inputFile + ".pcv"
} else { // A file was dropped
2021-11-13 13:03:21 +13:00
files++
name := filepath.Base(names[0])
2022-04-02 16:16:04 +13:00
// Is the file a part of a split volume?
2021-11-13 13:03:21 +13:00
nums := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
endsNum := false
for _, i := range nums {
if strings.HasSuffix(names[0], i) {
endsNum = true
}
}
isSplit := strings.Contains(names[0], ".pcv.") && endsNum
// Decide if encrypting or decrypting
if strings.HasSuffix(names[0], ".pcv") || isSplit {
mode = "decrypt"
2022-04-14 13:32:11 +12:00
inputLabel = name
startLabel = "Decrypt"
2022-04-02 16:16:04 +13:00
commentsPrompt = "Comments (read-only):"
commentsDisabled = true
2021-11-13 13:03:21 +13:00
if isSplit {
ind := strings.Index(names[0], ".pcv")
names[0] = names[0][:ind+4]
inputFile = names[0]
outputFile = names[0][:ind]
recombine = true
} else {
outputFile = names[0][:len(names[0])-4]
}
2022-04-02 16:16:04 +13:00
// Open the input file in read-only mode
2021-11-13 13:03:21 +13:00
var fin *os.File
if isSplit {
fin, _ = os.Open(names[0] + ".0")
} else {
fin, _ = os.Open(names[0])
}
2022-04-02 16:16:04 +13:00
// Use regex to test if the input is a valid Picocrypt volume
2021-11-13 13:03:21 +13:00
tmp := make([]byte, 30)
fin.Read(tmp)
if string(tmp[:5]) == "v1.13" {
resetUI()
2022-04-02 16:16:04 +13:00
mainStatus = "Please use v1.13 to decrypt this file."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
fin.Close()
return
}
if valid, _ := regexp.Match(`^v\d\.\d{2}.{10}0?\d+`, tmp); !valid && !isSplit {
resetUI()
2022-04-02 16:16:04 +13:00
mainStatus = "This doesn't seem like a Picocrypt volume."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
fin.Close()
return
}
2022-04-02 16:16:04 +13:00
// Use regex to test if the volume is compatible
fin.Seek(0, 0)
2021-11-13 13:03:21 +13:00
tmp = make([]byte, 15)
fin.Read(tmp)
tmp, _ = rsDecode(rs5, tmp)
2022-04-02 16:16:04 +13:00
if valid, _ := regexp.Match(`^v1.1[456]$`, tmp); valid {
2021-11-13 13:03:21 +13:00
resetUI()
2022-04-02 16:16:04 +13:00
mainStatus = "Please use v1.16 to decrypt this file."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
fin.Close()
return
}
2022-04-02 16:16:04 +13:00
if valid, _ := regexp.Match(`^(v1.1[789])|(v1.2[01])$`, tmp); valid {
2021-12-23 06:03:38 +13:00
resetUI()
2022-04-02 16:16:04 +13:00
mainStatus = "Please use v1.21 to decrypt this file."
2021-12-23 06:03:38 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
fin.Close()
return
}
2022-04-02 16:16:04 +13:00
// Read comments from file and check for corruption
var err error
2021-11-13 13:03:21 +13:00
tmp = make([]byte, 15)
fin.Read(tmp)
tmp, err = rsDecode(rs5, tmp)
if err == nil {
2022-04-02 16:16:04 +13:00
commentsLength, _ := strconv.Atoi(string(tmp))
tmp = make([]byte, commentsLength*3)
2021-11-13 13:03:21 +13:00
fin.Read(tmp)
2022-04-02 16:16:04 +13:00
comments = ""
for i := 0; i < commentsLength*3; i += 3 {
2021-11-13 13:03:21 +13:00
t, err := rsDecode(rs1, tmp[i:i+3])
if err != nil {
2022-04-02 16:16:04 +13:00
comments = "Comments are corrupted."
2021-11-13 13:03:21 +13:00
break
}
2022-04-02 16:16:04 +13:00
comments += string(t)
2021-11-13 13:03:21 +13:00
}
} else {
2022-04-02 16:16:04 +13:00
comments = "Comments are corrupted."
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Read flags from file and check for corruption
2021-12-23 05:56:33 +13:00
flags := make([]byte, 15)
2021-11-13 13:03:21 +13:00
fin.Read(flags)
fin.Close()
2021-12-23 05:56:33 +13:00
flags, err = rsDecode(rs5, flags)
2021-11-13 13:03:21 +13:00
if err != nil {
2022-04-02 16:16:04 +13:00
mainStatus = "The volume header is damaged."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
return
}
2022-04-02 16:16:04 +13:00
// Update UI and variables according to flags
2021-12-23 05:56:33 +13:00
if flags[1] == 1 {
2021-11-13 13:03:21 +13:00
keyfile = true
2022-03-20 05:43:32 +13:00
keyfilePrompt = "Keyfiles required."
2021-11-13 13:03:21 +13:00
} else {
2022-03-20 05:43:32 +13:00
keyfilePrompt = "Not applicable."
2021-11-13 13:03:21 +13:00
}
2021-12-23 05:56:33 +13:00
if flags[2] == 1 {
2021-11-13 13:03:21 +13:00
keyfileOrderMatters = true
}
2022-04-02 16:16:04 +13:00
} else { // One file that is not a Picocrypt volume was dropped
2021-11-13 13:03:21 +13:00
mode = "encrypt"
2022-04-14 13:32:11 +12:00
inputLabel = name
startLabel = "Encrypt"
2021-11-13 13:03:21 +13:00
inputFile = names[0]
outputFile = names[0] + ".pcv"
}
// Add the file
onlyFiles = append(onlyFiles, names[0])
inputFile = names[0]
2022-04-19 09:46:13 +12:00
size += int(stat.Size())
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
} else { // There are multiple dropped items
2021-11-13 13:03:21 +13:00
mode = "encrypt"
2022-04-02 16:16:04 +13:00
// Go through each dropped item and add to corresponding slices
2021-11-13 13:03:21 +13:00
for _, name := range names {
stat, _ := os.Stat(name)
if stat.IsDir() {
folders++
onlyFolders = append(onlyFolders, name)
} else {
files++
onlyFiles = append(onlyFiles, name)
allFiles = append(allFiles, name)
2022-04-19 09:46:13 +12:00
size += int(stat.Size())
2021-11-13 13:03:21 +13:00
}
}
2022-04-02 16:16:04 +13:00
// Update UI with the number of files and folders selected
2021-11-13 13:03:21 +13:00
if folders == 0 {
2022-03-20 05:43:32 +13:00
inputLabel = fmt.Sprintf("%d files selected.", files)
2021-11-13 13:03:21 +13:00
} else if files == 0 {
2022-04-14 13:32:11 +12:00
inputLabel = fmt.Sprintf("%d folders selected.", folders)
2021-11-13 13:03:21 +13:00
} else {
if files == 1 && folders > 1 {
2022-03-20 05:43:32 +13:00
inputLabel = fmt.Sprintf("1 file and %d folders selected.", folders)
2021-11-13 13:03:21 +13:00
} else if folders == 1 && files > 1 {
2022-03-20 05:43:32 +13:00
inputLabel = fmt.Sprintf("%d files and 1 folder selected.", files)
2021-11-13 13:03:21 +13:00
} else if folders == 1 && files == 1 {
2022-03-20 05:43:32 +13:00
inputLabel = "1 file and 1 folder selected."
2021-11-13 13:03:21 +13:00
} else {
2022-03-20 05:43:32 +13:00
inputLabel = fmt.Sprintf("%d files and %d folders selected.", files, folders)
2021-11-13 13:03:21 +13:00
}
}
2022-04-14 13:32:11 +12:00
startLabel = "Encrypt"
2021-11-13 13:03:21 +13:00
// Set the input and output paths
2022-03-20 05:43:32 +13:00
inputFile = filepath.Join(filepath.Dir(names[0]), "Encrypted") + ".zip"
2022-04-02 16:16:04 +13:00
outputFile = inputFile + ".pcv"
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Recursively add all files in 'onlyFolders' to 'allFiles'
for _, name := range onlyFolders {
filepath.Walk(name, func(path string, _ os.FileInfo, _ error) error {
stat, _ := os.Stat(path)
if !stat.IsDir() {
allFiles = append(allFiles, path)
2022-04-19 09:46:13 +12:00
size += int(stat.Size())
2022-04-02 16:16:04 +13:00
}
return nil
})
2021-11-13 13:03:21 +13:00
}
2022-04-19 09:46:13 +12:00
inputLabel = fmt.Sprintf("%s (%s)", inputLabel, sizeify(int64(size)))
2021-11-13 13:03:21 +13:00
}
func work() {
2022-04-02 16:16:04 +13:00
// Show that Picocrypt is encrypting/decrypting
2022-03-20 05:43:32 +13:00
popupStatus = "Starting..."
2021-11-13 13:03:21 +13:00
mainStatus = "Working..."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
working = true
padded := false
2022-04-02 16:16:04 +13:00
giu.Update()
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// Cryptography!
var salt []byte // Argon2 salt, 16 bytes
var hkdfSalt []byte // HKDF-SHA3 salt, 32 bytes
var serpentSalt []byte // Serpent salt, 16 bytes
var nonce []byte // 24-byte XChaCha20 nonce
var keyHash []byte // SHA3-512 hash of encryption key
var _keyHash []byte // Same as 'keyHash', but used for comparison
var keyfileKey []byte // The SHA3-256 hashes of keyfiles
var keyfileHash []byte = make([]byte, 32) // The SHA3-256 of 'keyfileKey'
var _keyfileHash []byte // Same as 'keyfileHash', but used for comparison
var dataMac []byte // 64-byte authentication tag (BLAKE2b or HMAC-SHA3)
2021-11-13 13:03:21 +13:00
if mode == "encrypt" {
if compress {
2022-03-20 05:43:32 +13:00
popupStatus = "Compressing files..."
2021-11-13 13:03:21 +13:00
} else {
2022-03-20 05:43:32 +13:00
popupStatus = "Combining files..."
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Combine/compress all files into a .zip file
2021-11-13 13:03:21 +13:00
if len(allFiles) > 1 || len(onlyFolders) > 0 {
var rootDir string
if len(onlyFolders) > 0 {
rootDir = filepath.Dir(onlyFolders[0])
} else {
rootDir = filepath.Dir(onlyFiles[0])
}
file, err := os.Create(inputFile)
if err != nil {
mainStatus = "Access denied by operating system."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
return
}
2022-04-02 16:16:04 +13:00
compressTotal = 0
for _, path := range allFiles {
stat, _ := os.Stat(path)
compressTotal += stat.Size()
}
2021-11-13 13:03:21 +13:00
w := zip.NewWriter(file)
for i, path := range allFiles {
if !working {
2022-04-02 16:16:04 +13:00
mainStatus = "Operation cancelled by user."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
2021-11-13 13:03:21 +13:00
w.Close()
file.Close()
os.Remove(inputFile)
2022-04-02 16:16:04 +13:00
compressDone = 0
2021-11-13 13:03:21 +13:00
return
}
2022-04-02 16:16:04 +13:00
progressInfo = fmt.Sprintf("%d/%d", i+1, len(allFiles))
2021-11-13 13:03:21 +13:00
giu.Update()
2022-04-02 16:16:04 +13:00
// Don't add the volume to itself
2021-11-13 13:03:21 +13:00
if path == inputFile {
continue
}
stat, _ := os.Stat(path)
header, _ := zip.FileInfoHeader(stat)
header.Name = strings.TrimPrefix(path, rootDir)
header.Name = filepath.ToSlash(header.Name)
header.Name = strings.TrimPrefix(header.Name, "/")
if compress {
header.Method = zip.Deflate
} else {
header.Method = zip.Store
}
writer, _ := w.CreateHeader(header)
2022-04-02 16:16:04 +13:00
file, err := os.Open(path)
if err != nil {
mainStatus = "Access denied by operating system."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
os.Remove(inputFile)
compressDone = 0
return
}
// Use a passthrough to catch compression progress
prg := &compressorProgress{Reader: file}
io.Copy(writer, prg)
2021-11-13 13:03:21 +13:00
file.Close()
}
w.Close()
file.Close()
2022-04-02 16:16:04 +13:00
compressDone = 0
2021-11-13 13:03:21 +13:00
}
}
2022-04-02 16:16:04 +13:00
// Recombine a split file if necessary
2021-11-13 13:03:21 +13:00
if recombine {
2022-03-20 05:43:32 +13:00
popupStatus = "Recombining file..."
2021-11-13 13:03:21 +13:00
total := 0
2022-04-02 16:16:04 +13:00
totalBytes := int64(0)
done := 0
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// Find out the number of splitted chunks
2021-11-13 13:03:21 +13:00
for {
2022-04-02 16:16:04 +13:00
stat, err := os.Stat(fmt.Sprintf("%s.%d", inputFile, total))
2021-11-13 13:03:21 +13:00
if err != nil {
break
}
total++
2022-04-02 16:16:04 +13:00
totalBytes += stat.Size()
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Merge all chunks into one file
2021-11-13 13:03:21 +13:00
fout, _ := os.Create(inputFile)
for i := 0; i < total; i++ {
fin, _ := os.Open(fmt.Sprintf("%s.%d", inputFile, i))
for {
2022-04-19 09:46:13 +12:00
data := make([]byte, 1<<20)
2021-11-13 13:03:21 +13:00
read, err := fin.Read(data)
if err != nil {
break
}
data = data[:read]
fout.Write(data)
2022-04-02 16:16:04 +13:00
done += read
progressInfo = fmt.Sprintf("%d/%d", i+1, total)
progress = float32(done) / float32(totalBytes)
giu.Update()
2021-11-13 13:03:21 +13:00
}
fin.Close()
}
fout.Close()
progressInfo = ""
}
2022-04-02 16:16:04 +13:00
// Subtract the header size from the total size if decrypting
2021-11-13 13:03:21 +13:00
stat, _ := os.Stat(inputFile)
total := stat.Size()
if mode == "decrypt" {
2022-04-07 08:18:07 +12:00
total -= 789
2021-11-13 13:03:21 +13:00
}
// Open input file in read-only mode
fin, err := os.Open(inputFile)
if err != nil {
mainStatus = "Access denied by operating system."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
2022-04-02 16:16:04 +13:00
if recombine {
os.Remove(inputFile)
}
if len(allFiles) > 1 || len(onlyFolders) > 0 {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
return
}
var fout *os.File
2022-04-02 16:16:04 +13:00
// If encrypting, generate values and write to file
2021-11-13 13:03:21 +13:00
if mode == "encrypt" {
2022-03-20 05:43:32 +13:00
popupStatus = "Generating values..."
2021-11-13 13:03:21 +13:00
giu.Update()
2022-04-02 16:16:04 +13:00
// Create the output file
2021-11-13 13:03:21 +13:00
var err error
fout, err = os.Create(outputFile)
if err != nil {
mainStatus = "Access denied by operating system."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
2022-04-02 16:16:04 +13:00
fin.Close()
if len(allFiles) > 1 || len(onlyFolders) > 0 {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
return
}
2022-04-02 16:16:04 +13:00
// Set up cryptographic values
2021-11-13 13:03:21 +13:00
salt = make([]byte, 16)
hkdfSalt = make([]byte, 32)
serpentSalt = make([]byte, 16)
nonce = make([]byte, 24)
2022-04-02 16:16:04 +13:00
// Write the program version to file
2021-11-13 13:03:21 +13:00
fout.Write(rsEncode(rs5, []byte(version)))
2022-04-02 16:16:04 +13:00
// Encode and write the comment length to file
commentsLength := []byte(fmt.Sprintf("%05d", len(comments)))
commentsLength = rsEncode(rs5, commentsLength)
fout.Write(commentsLength)
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// Encode the comment and write to file
for _, i := range []byte(comments) {
2021-11-13 13:03:21 +13:00
fout.Write(rsEncode(rs1, []byte{i}))
}
2022-04-02 16:16:04 +13:00
// Configure flags and write to file
2021-12-23 05:56:33 +13:00
flags := make([]byte, 5)
2022-04-02 16:16:04 +13:00
if paranoid { // Paranoid mode selected
2021-11-13 13:03:21 +13:00
flags[0] = 1
}
2022-04-02 16:16:04 +13:00
if len(keyfiles) > 0 { // Keyfiles are being used
2021-11-13 13:03:21 +13:00
flags[1] = 1
}
2022-04-02 16:16:04 +13:00
if keyfileOrderMatters { // Order of keyfiles matter
2021-11-13 13:03:21 +13:00
flags[2] = 1
}
2022-04-02 16:16:04 +13:00
if reedsolo { // Full Reed-Solomon encoding is selected
2021-11-13 13:03:21 +13:00
flags[3] = 1
}
2022-04-19 09:46:13 +12:00
if total%(1<<20) >= 1<<20-128 { // Reed-Solomon internals
2021-11-13 13:03:21 +13:00
flags[4] = 1
}
2021-12-23 05:56:33 +13:00
flags = rsEncode(rs5, flags)
2021-11-13 13:03:21 +13:00
fout.Write(flags)
2022-04-02 16:16:04 +13:00
// Fill values with Go's CSPRNG
2021-11-13 13:03:21 +13:00
rand.Read(salt)
rand.Read(hkdfSalt)
rand.Read(serpentSalt)
rand.Read(nonce)
2022-04-02 16:16:04 +13:00
// Encode values with Reed-Solomon and write to file
fout.Write(rsEncode(rs16, salt))
fout.Write(rsEncode(rs32, hkdfSalt))
fout.Write(rsEncode(rs16, serpentSalt))
fout.Write(rsEncode(rs24, nonce))
// Write placeholders for future use
fout.Write(make([]byte, 192)) // Hash of encryption key
fout.Write(make([]byte, 96)) // Hash of keyfile key
fout.Write(make([]byte, 192)) // BLAKE2b/HMAC-SHA3 tag
} else { // Decrypting, read values from file and decode
2022-03-20 05:43:32 +13:00
popupStatus = "Reading values..."
2021-11-13 13:03:21 +13:00
giu.Update()
2022-04-02 16:16:04 +13:00
errs := make([]error, 10)
2021-11-13 13:03:21 +13:00
version := make([]byte, 15)
fin.Read(version)
2022-04-02 16:16:04 +13:00
_, errs[0] = rsDecode(rs5, version)
2021-11-13 13:03:21 +13:00
tmp := make([]byte, 15)
fin.Read(tmp)
2022-04-02 16:16:04 +13:00
tmp, errs[1] = rsDecode(rs5, tmp)
commentsLength, _ := strconv.Atoi(string(tmp))
fin.Read(make([]byte, commentsLength*3))
2022-04-07 08:18:07 +12:00
total -= int64(commentsLength) * 3
2021-11-13 13:03:21 +13:00
2021-12-23 05:56:33 +13:00
flags := make([]byte, 15)
2021-11-13 13:03:21 +13:00
fin.Read(flags)
2022-04-02 16:16:04 +13:00
flags, errs[2] = rsDecode(rs5, flags)
2021-12-23 05:56:33 +13:00
paranoid = flags[0] == 1
2021-11-13 13:03:21 +13:00
reedsolo = flags[3] == 1
padded = flags[4] == 1
salt = make([]byte, 48)
fin.Read(salt)
2022-04-02 16:16:04 +13:00
salt, errs[3] = rsDecode(rs16, salt)
2021-11-13 13:03:21 +13:00
hkdfSalt = make([]byte, 96)
fin.Read(hkdfSalt)
2022-04-02 16:16:04 +13:00
hkdfSalt, errs[4] = rsDecode(rs32, hkdfSalt)
2021-11-13 13:03:21 +13:00
serpentSalt = make([]byte, 48)
fin.Read(serpentSalt)
2022-04-02 16:16:04 +13:00
serpentSalt, errs[5] = rsDecode(rs16, serpentSalt)
2021-11-13 13:03:21 +13:00
nonce = make([]byte, 72)
fin.Read(nonce)
2022-04-02 16:16:04 +13:00
nonce, errs[6] = rsDecode(rs24, nonce)
2021-11-13 13:03:21 +13:00
_keyHash = make([]byte, 192)
fin.Read(_keyHash)
2022-04-02 16:16:04 +13:00
_keyHash, errs[7] = rsDecode(rs64, _keyHash)
2021-11-13 13:03:21 +13:00
_keyfileHash = make([]byte, 96)
fin.Read(_keyfileHash)
2022-04-02 16:16:04 +13:00
_keyfileHash, errs[8] = rsDecode(rs32, _keyfileHash)
2021-11-13 13:03:21 +13:00
dataMac = make([]byte, 192)
fin.Read(dataMac)
2022-04-02 16:16:04 +13:00
dataMac, errs[9] = rsDecode(rs64, dataMac)
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// If there was an issue during decoding, the header is corrupted
for _, err := range errs {
if err != nil {
if keep { // If the user chooses to force decrypt
kept = true
} else {
mainStatus = "The volume header is damaged."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
fin.Close()
if recombine {
os.Remove(inputFile)
}
return
}
2021-11-13 13:03:21 +13:00
}
}
}
2022-03-20 05:43:32 +13:00
popupStatus = "Deriving key..."
2021-11-13 13:03:21 +13:00
progress = 0
progressInfo = ""
giu.Update()
2022-04-02 16:16:04 +13:00
// Derive encryption keys and subkeys
2021-11-13 13:03:21 +13:00
var key []byte
2022-04-02 16:16:04 +13:00
if paranoid { // Overkilled parameters for paranoid mode
2021-11-13 13:03:21 +13:00
key = argon2.IDKey(
[]byte(password),
salt,
2022-04-19 09:46:13 +12:00
8, // 8 passes
1<<20, // 1 GiB memory
8, // 8 threads
32, // 32-byte output key
2021-11-13 13:03:21 +13:00
)
2022-04-02 16:16:04 +13:00
} else { // High Argon2 parameters by default
2021-11-13 13:03:21 +13:00
key = argon2.IDKey(
[]byte(password),
salt,
4,
2022-04-19 09:46:13 +12:00
1<<20,
2021-11-13 13:03:21 +13:00
4,
32,
)
}
2022-04-02 16:16:04 +13:00
// If the 'Cancel' button was pressed, cancel and clean up
2021-11-13 13:03:21 +13:00
if !working {
mainStatus = "Operation cancelled by user."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
2022-04-02 16:16:04 +13:00
fin.Close()
if mode == "encrypt" {
fout.Close()
2021-11-13 13:03:21 +13:00
}
if recombine {
os.Remove(inputFile)
}
2022-04-02 16:16:04 +13:00
if len(allFiles) > 1 || len(onlyFolders) > 0 {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
os.Remove(outputFile)
return
}
2022-04-02 16:16:04 +13:00
// If keyfiles are being used
2021-11-13 13:03:21 +13:00
if len(keyfiles) > 0 || keyfile {
2022-04-02 16:16:04 +13:00
if keyfileOrderMatters { // If order matters, hash progressively
2021-11-13 13:03:21 +13:00
var keysum = sha3.New256()
for _, path := range keyfiles {
kin, _ := os.Open(path)
kstat, _ := os.Stat(path)
kbytes := make([]byte, kstat.Size())
kin.Read(kbytes)
kin.Close()
keysum.Write(kbytes)
}
keyfileKey = keysum.Sum(nil)
keyfileSha3 := sha3.New256()
keyfileSha3.Write(keyfileKey)
keyfileHash = keyfileSha3.Sum(nil)
2022-04-02 16:16:04 +13:00
} else { // If order doesn't matter, hash individually and combine
2021-11-13 13:03:21 +13:00
var keysum []byte
for _, path := range keyfiles {
kin, _ := os.Open(path)
kstat, _ := os.Stat(path)
kbytes := make([]byte, kstat.Size())
kin.Read(kbytes)
kin.Close()
ksha3 := sha3.New256()
ksha3.Write(kbytes)
keyfileKey := ksha3.Sum(nil)
if keysum == nil {
keysum = keyfileKey
} else {
for i, j := range keyfileKey {
keysum[i] ^= j
}
}
}
keyfileKey = keysum
keyfileSha3 := sha3.New256()
keyfileSha3.Write(keysum)
keyfileHash = keyfileSha3.Sum(nil)
}
}
2022-04-02 16:16:04 +13:00
// Hash the encryption key (used to check if a password is correct when decrypting)
2021-11-13 13:03:21 +13:00
sha3_512 := sha3.New512()
sha3_512.Write(key)
keyHash = sha3_512.Sum(nil)
2022-04-02 16:16:04 +13:00
// Validate the password and/or keyfiles
2021-11-13 13:03:21 +13:00
if mode == "decrypt" {
2022-04-02 16:16:04 +13:00
incorrect := false
2021-11-13 13:03:21 +13:00
keyCorrect := true
keyfileCorrect := true
2022-04-02 16:16:04 +13:00
keyCorrect = subtle.ConstantTimeCompare(keyHash, _keyHash) == 1
2021-11-13 13:03:21 +13:00
if keyfile {
2022-04-02 16:16:04 +13:00
keyfileCorrect = subtle.ConstantTimeCompare(keyfileHash, _keyfileHash) == 1
incorrect = !keyCorrect || !keyfileCorrect
2021-11-13 13:03:21 +13:00
} else {
2022-04-02 16:16:04 +13:00
incorrect = !keyCorrect
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// If there's an issue with the password and/or keyfiles
if incorrect {
2021-11-13 13:03:21 +13:00
if keep {
kept = true
} else {
if !keyCorrect {
mainStatus = "The provided password is incorrect."
} else {
if keyfileOrderMatters {
2022-04-02 16:16:04 +13:00
mainStatus = "Incorrect keyfiles or order."
2021-11-13 13:03:21 +13:00
} else {
mainStatus = "Incorrect keyfiles."
}
}
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
2022-04-02 16:16:04 +13:00
fin.Close()
2021-11-13 13:03:21 +13:00
if recombine {
os.Remove(inputFile)
}
return
}
}
2022-04-02 16:16:04 +13:00
// Create the output file for decryption
2021-11-13 13:03:21 +13:00
var err error
fout, err = os.Create(outputFile)
if err != nil {
mainStatus = "Access denied by operating system."
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
2022-04-02 16:16:04 +13:00
fin.Close()
if recombine {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
return
}
}
if len(keyfiles) > 0 || keyfile {
2022-04-02 16:16:04 +13:00
// XOR the encryption key with the keyfile to make the master key
2021-11-13 13:03:21 +13:00
tmp := key
key = make([]byte, 32)
for i := range key {
key[i] = tmp[i] ^ keyfileKey[i]
}
}
done := 0
2022-04-19 09:46:13 +12:00
counterDone := 0
2021-11-13 13:03:21 +13:00
counter := 0
startTime := time.Now()
2022-04-19 09:46:13 +12:00
chacha, _ := chacha20.NewUnauthenticatedCipher(key, nonce)
2021-11-13 13:03:21 +13:00
// Use HKDF-SHA3 to generate a subkey
var mac hash.Hash
subkey := make([]byte, 32)
hkdf := hkdf.New(sha3.New256, key, hkdfSalt, nil)
hkdf.Read(subkey)
2021-12-23 05:56:33 +13:00
if paranoid {
2022-04-02 16:16:04 +13:00
mac = hmac.New(sha3.New512, subkey) // HMAC-SHA3
2021-12-23 05:56:33 +13:00
} else {
2022-04-02 16:16:04 +13:00
mac, _ = blake2b.New512(subkey) // Keyed BLAKE2b
2021-11-13 13:03:21 +13:00
}
// Generate another subkey and cipher (not used unless paranoid mode is checked)
serpentKey := make([]byte, 32)
hkdf.Read(serpentKey)
2022-04-19 09:46:13 +12:00
s, _ := serpent.NewCipher(serpentKey)
serpent := cipher.NewCTR(s, serpentSalt)
2021-11-13 13:03:21 +13:00
for {
2022-04-02 16:16:04 +13:00
// If the user cancels the process, stop and clean up
2021-11-13 13:03:21 +13:00
if !working {
mainStatus = "Operation cancelled by user."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
fin.Close()
fout.Close()
if recombine {
os.Remove(inputFile)
}
2022-04-02 16:16:04 +13:00
if len(allFiles) > 1 || len(onlyFolders) > 0 {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
os.Remove(outputFile)
return
}
2022-04-02 16:16:04 +13:00
// Read in data from the file
var src []byte
2021-11-13 13:03:21 +13:00
if mode == "decrypt" && reedsolo {
2022-04-19 09:46:13 +12:00
src = make([]byte, 1<<20/128*136)
2021-11-13 13:03:21 +13:00
} else {
2022-04-19 09:46:13 +12:00
src = make([]byte, 1<<20)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
size, err := fin.Read(src)
2021-11-13 13:03:21 +13:00
if err != nil {
break
}
2022-04-02 16:16:04 +13:00
src = src[:size]
dst := make([]byte, len(src))
2021-11-13 13:03:21 +13:00
2022-04-02 16:16:04 +13:00
// Do the actual encryption
2021-11-13 13:03:21 +13:00
if mode == "encrypt" {
if paranoid {
2022-04-02 16:16:04 +13:00
serpent.XORKeyStream(dst, src)
copy(src, dst)
2021-11-13 13:03:21 +13:00
}
2022-04-19 09:46:13 +12:00
chacha.XORKeyStream(dst, src)
2022-04-02 16:16:04 +13:00
mac.Write(dst)
2021-11-13 13:03:21 +13:00
if reedsolo {
2022-04-02 16:16:04 +13:00
copy(src, dst)
dst = nil
// If a full MiB is available
2022-04-19 09:46:13 +12:00
if len(src) == 1<<20 {
2022-04-02 16:16:04 +13:00
// Encode every chunk
2022-04-19 09:46:13 +12:00
for i := 0; i < 1<<20; i += 128 {
2022-04-02 16:16:04 +13:00
dst = append(dst, rsEncode(rs128, src[i:i+128])...)
2021-11-13 13:03:21 +13:00
}
} else {
2022-04-02 16:16:04 +13:00
// Encode the full chunks
chunks := math.Floor(float64(len(src)) / 128)
2021-11-13 13:03:21 +13:00
for i := 0; float64(i) < chunks; i++ {
2022-04-02 16:16:04 +13:00
dst = append(dst, rsEncode(rs128, src[i*128:(i+1)*128])...)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Pad and encode the final partial chunk
dst = append(dst, rsEncode(rs128, pad(src[int(chunks*128):]))...)
2021-11-13 13:03:21 +13:00
}
}
2022-04-02 16:16:04 +13:00
} else { // Decryption
2021-11-13 13:03:21 +13:00
if reedsolo {
2022-04-02 16:16:04 +13:00
copy(dst, src)
src = nil
// If a complete 1 MiB block is available
2022-04-19 09:46:13 +12:00
if len(dst) == 1<<20/128*136 {
2022-04-02 16:16:04 +13:00
// Decode every chunk
2022-04-19 09:46:13 +12:00
for i := 0; i < 1<<20/128*136; i += 136 {
2022-04-02 16:16:04 +13:00
tmp, err := rsDecode(rs128, dst[i:i+136])
2021-11-13 13:03:21 +13:00
if err != nil {
if keep {
kept = true
} else {
fin.Close()
fout.Close()
broken()
2022-04-02 16:16:04 +13:00
mainStatus = "The input file is irrecoverably damaged."
2021-11-13 13:03:21 +13:00
return
}
}
2022-04-19 10:09:12 +12:00
if i == 1113976 && done+1114112 >= int(total) && padded {
2021-11-13 13:03:21 +13:00
tmp = unpad(tmp)
}
2022-04-02 16:16:04 +13:00
src = append(src, tmp...)
2021-11-13 13:03:21 +13:00
}
} else {
2022-04-02 16:16:04 +13:00
// Decode the full chunks
chunks := len(dst)/136 - 1
2021-11-13 13:03:21 +13:00
for i := 0; i < chunks; i++ {
2022-04-02 16:16:04 +13:00
tmp, err := rsDecode(rs128, dst[i*136:(i+1)*136])
2021-11-13 13:03:21 +13:00
if err != nil {
if keep {
kept = true
} else {
fin.Close()
fout.Close()
broken()
2022-04-02 16:16:04 +13:00
mainStatus = "The input file is irrecoverably damaged."
2021-11-13 13:03:21 +13:00
return
}
}
2022-04-02 16:16:04 +13:00
src = append(src, tmp...)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Unpad and decode the final partial chunk
tmp, err := rsDecode(rs128, dst[int(chunks)*136:])
2021-11-13 13:03:21 +13:00
if err != nil {
if keep {
kept = true
} else {
fin.Close()
fout.Close()
broken()
2022-04-02 16:16:04 +13:00
mainStatus = "The input file is irrecoverably damaged."
2021-11-13 13:03:21 +13:00
return
}
}
2022-04-02 16:16:04 +13:00
src = append(src, unpad(tmp)...)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
dst = make([]byte, len(src))
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
mac.Write(src)
2022-04-19 09:46:13 +12:00
chacha.XORKeyStream(dst, src)
2021-11-13 13:03:21 +13:00
if paranoid {
2022-04-02 16:16:04 +13:00
copy(src, dst)
serpent.XORKeyStream(dst, src)
2021-11-13 13:03:21 +13:00
}
}
2022-04-02 16:16:04 +13:00
fout.Write(dst)
2021-11-13 13:03:21 +13:00
// Update stats
if mode == "decrypt" && reedsolo {
2022-05-02 09:13:28 +12:00
done += 1 << 20 / 128 * 136
2021-11-13 13:03:21 +13:00
} else {
2022-05-02 09:13:28 +12:00
done += 1 << 20
2021-11-13 13:03:21 +13:00
}
2022-05-02 09:13:28 +12:00
counterDone += 1 << 20
2021-11-13 13:03:21 +13:00
counter++
progress = float32(done) / float32(total)
2022-05-02 09:13:28 +12:00
elapsed := float64(time.Since(startTime)) / (1 << 20) / 1000
2022-04-19 09:46:13 +12:00
speed := float64(done) / elapsed / (1 << 20)
2022-05-02 09:13:28 +12:00
eta := int(math.Floor(float64(total-int64(done)) / (speed * (1 << 20))))
2022-04-19 09:46:13 +12:00
progress = float32(math.Min(float64(progress), 1)) // Cap progress to 100%
2021-11-13 13:03:21 +13:00
progressInfo = fmt.Sprintf("%.2f%%", progress*100)
2022-04-19 09:46:13 +12:00
popupStatus = fmt.Sprintf("Working at %.2f MiB/s (ETA: %s)", speed, humanize(eta))
2021-11-13 13:03:21 +13:00
giu.Update()
2022-04-19 09:46:13 +12:00
// If more than 256 GiB passed, change the nonce to prevent counter overflow
blocks := counterDone/64 + 1
if blocks+(1<<20/64) > 1<<32 {
nonce = make([]byte, 24)
hkdf.Read(nonce)
chacha, _ = chacha20.NewUnauthenticatedCipher(key, nonce)
counterDone = 0
}
2021-11-13 13:03:21 +13:00
}
if mode == "encrypt" {
2022-04-02 16:16:04 +13:00
// Seek back to header to write important values
fout.Seek(int64(309+len(comments)*3), 0)
2021-11-13 13:03:21 +13:00
fout.Write(rsEncode(rs64, keyHash))
fout.Write(rsEncode(rs32, keyfileHash))
fout.Write(rsEncode(rs64, mac.Sum(nil)))
} else {
// Validate the authenticity of decrypted data
if subtle.ConstantTimeCompare(mac.Sum(nil), dataMac) == 0 {
if keep {
kept = true
} else {
fin.Close()
fout.Close()
broken()
return
}
}
}
fin.Close()
fout.Close()
// Split files into chunks
if split {
var splitted []string
2022-03-20 05:43:32 +13:00
popupStatus = "Splitting file..."
2021-11-13 13:03:21 +13:00
stat, _ := os.Stat(outputFile)
size := stat.Size()
finished := 0
2022-04-02 16:16:04 +13:00
finishedRaw := 0
2021-11-13 13:03:21 +13:00
chunkSize, _ := strconv.Atoi(splitSize)
2022-05-02 09:13:28 +12:00
// User can choose KiB, MiB, GiB, TiB, or custom number of chunks
2021-11-13 13:03:21 +13:00
if splitSelected == 0 {
2022-04-19 09:46:13 +12:00
chunkSize *= 1 << 10
2021-11-13 13:03:21 +13:00
} else if splitSelected == 1 {
2022-04-19 09:46:13 +12:00
chunkSize *= 1 << 20
} else if splitSelected == 2 {
chunkSize *= 1 << 30
2022-05-02 09:13:28 +12:00
} else if splitSelected == 3 {
2022-04-19 09:46:13 +12:00
chunkSize *= 1 << 40
2022-05-02 09:13:28 +12:00
} else {
chunkSize = int(math.Ceil(float64(size) / float64(chunkSize)))
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Get the number of required chunks
2021-11-13 13:03:21 +13:00
chunks := int(math.Ceil(float64(size) / float64(chunkSize)))
2022-04-02 16:16:04 +13:00
progressInfo = fmt.Sprintf("%d/%d", finished+1, chunks)
giu.Update()
2021-11-13 13:03:21 +13:00
fin, _ := os.Open(outputFile)
2022-04-02 16:16:04 +13:00
for i := 0; i < chunks; i++ { // Make the chunks
2021-11-13 13:03:21 +13:00
fout, _ := os.Create(fmt.Sprintf("%s.%d", outputFile, i))
done := 0
2022-04-02 16:16:04 +13:00
// Copy data into the chunk
2021-11-13 13:03:21 +13:00
for {
2022-04-19 09:46:13 +12:00
data := make([]byte, 1<<20)
2022-05-02 09:13:28 +12:00
for done+len(data) > chunkSize {
data = make([]byte, int(math.Ceil(float64(len(data))/2)))
}
2021-11-13 13:03:21 +13:00
read, err := fin.Read(data)
if err != nil {
break
}
if !working {
fin.Close()
fout.Close()
2022-04-02 16:16:04 +13:00
if len(allFiles) > 1 || len(onlyFolders) > 0 {
os.Remove(inputFile)
}
2021-11-13 13:03:21 +13:00
mainStatus = "Operation cancelled by user."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
// If user cancels, remove the unfinished files
for _, j := range splitted {
os.Remove(j)
}
os.Remove(fmt.Sprintf("%s.%d", outputFile, i))
os.Remove(outputFile)
return
}
data = data[:read]
fout.Write(data)
done += read
if done >= chunkSize {
break
}
2022-04-02 16:16:04 +13:00
finishedRaw += read
progress = float32(finishedRaw) / float32(size)
giu.Update()
2021-11-13 13:03:21 +13:00
}
fout.Close()
2022-04-02 16:16:04 +13:00
// Update stats
2021-11-13 13:03:21 +13:00
finished++
2022-04-02 16:16:04 +13:00
if finished == chunks {
finished--
}
2021-11-13 13:03:21 +13:00
splitted = append(splitted, fmt.Sprintf("%s.%d", outputFile, i))
2022-04-02 16:16:04 +13:00
progressInfo = fmt.Sprintf("%d/%d", finished+1, chunks)
2021-11-13 13:03:21 +13:00
giu.Update()
}
2022-04-02 16:16:04 +13:00
2021-11-13 13:03:21 +13:00
fin.Close()
2021-11-20 13:05:13 +13:00
os.Remove(outputFile)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Remove the temporary file used to combine a splitted volume
2021-11-13 13:03:21 +13:00
if recombine {
os.Remove(inputFile)
}
2022-04-02 16:16:04 +13:00
// Delete the temporary zip file used to encrypt files
2021-11-13 13:03:21 +13:00
if len(allFiles) > 1 || len(onlyFolders) > 0 {
2021-11-20 13:05:13 +13:00
os.Remove(inputFile)
2021-11-13 13:03:21 +13:00
}
2022-04-02 16:16:04 +13:00
// Delete the input file(s) if the user chooses
2021-11-13 13:03:21 +13:00
if deleteWhenDone {
progressInfo = ""
2022-04-02 16:16:04 +13:00
popupStatus = "Deleting files..."
2021-11-13 13:03:21 +13:00
giu.Update()
if mode == "decrypt" {
if recombine {
total := 0
for {
_, err := os.Stat(fmt.Sprintf("%s.%d", inputFile, total))
if err != nil {
break
}
os.Remove(fmt.Sprintf("%s.%d", inputFile, total))
total++
}
} else {
os.Remove(inputFile)
}
} else {
for _, i := range onlyFiles {
os.Remove(i)
}
for _, i := range onlyFolders {
os.RemoveAll(i)
}
}
}
2022-04-02 16:16:04 +13:00
// All done, reset the UI
2021-11-13 13:03:21 +13:00
resetUI()
2022-04-02 16:16:04 +13:00
// If the user chose to keep a corrupted/modified file, let them know
2021-11-13 13:03:21 +13:00
if kept {
2022-04-02 16:16:04 +13:00
mainStatus = "The input file was modified. Please be careful."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0xff, 0x00, 0xff}
} else {
mainStatus = "Completed."
mainStatusColor = color.RGBA{0x00, 0xff, 0x00, 0xff}
}
2022-04-02 16:16:04 +13:00
// Clear some variables
2021-11-13 13:03:21 +13:00
working = false
kept = false
key = nil
2022-03-20 05:43:32 +13:00
popupStatus = "Ready."
2021-11-13 13:03:21 +13:00
}
// This function is run if an issue occurs during decryption
func broken() {
2022-04-02 16:16:04 +13:00
mainStatus = "The input file is damaged or modified."
2021-11-13 13:03:21 +13:00
mainStatusColor = color.RGBA{0xff, 0x00, 0x00, 0xff}
2022-04-02 16:16:04 +13:00
// Clean up files since decryption failed
2021-11-13 13:03:21 +13:00
if recombine {
os.Remove(inputFile)
}
os.Remove(outputFile)
}
// Reset the UI to a clean state with nothing selected or checked
func resetUI() {
mode = ""
onlyFiles = nil
onlyFolders = nil
allFiles = nil
2022-03-20 05:43:32 +13:00
inputLabel = "Drop files and folders into this window."
2022-04-14 13:32:11 +12:00
startLabel = "Start"
2021-11-13 13:03:21 +13:00
password = ""
2022-04-02 16:16:04 +13:00
cpassword = ""
2021-11-13 13:03:21 +13:00
keyfiles = nil
keyfile = false
keyfileOrderMatters = false
2022-03-20 05:43:32 +13:00
keyfilePrompt = "None selected."
2022-04-02 16:16:04 +13:00
comments = ""
commentsPrompt = "Comments:"
commentsDisabled = false
2021-11-13 13:03:21 +13:00
keep = false
reedsolo = false
split = false
splitSize = ""
splitSelected = 1
deleteWhenDone = false
paranoid = false
compress = false
inputFile = ""
outputFile = ""
progress = 0
progressInfo = ""
mainStatus = "Ready."
mainStatusColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
giu.Update()
}
// Reed-Solomon encoder
func rsEncode(rs *infectious.FEC, data []byte) []byte {
2022-04-02 16:16:04 +13:00
res := make([]byte, rs.Total())
2021-11-13 13:03:21 +13:00
rs.Encode(data, func(s infectious.Share) {
2022-04-02 16:16:04 +13:00
res[s.Number] = s.Data[0]
2021-11-13 13:03:21 +13:00
})
return res
}
// Reed-Solomon decoder
func rsDecode(rs *infectious.FEC, data []byte) ([]byte, error) {
tmp := make([]infectious.Share, rs.Total())
for i := 0; i < rs.Total(); i++ {
2022-04-02 16:16:04 +13:00
tmp[i].Number = i
tmp[i].Data = append(tmp[i].Data, data[i])
2021-11-13 13:03:21 +13:00
}
res, err := rs.Decode(nil, tmp)
2022-04-02 16:16:04 +13:00
// Force decode for the "Force decrypt" option
2021-11-13 13:03:21 +13:00
if err != nil {
if rs.Total() == 136 {
return data[:128], err
}
return data[:rs.Total()/3], err
}
return res, nil
}
2022-04-02 16:16:04 +13:00
// PKCS#7 pad (for use with Reed-Solomon)
2021-11-13 13:03:21 +13:00
func pad(data []byte) []byte {
padLen := 128 - len(data)%128
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
return append(data, padding...)
}
2022-04-02 16:16:04 +13:00
// PKCS#7 unpad
2021-11-13 13:03:21 +13:00
func unpad(data []byte) []byte {
length := len(data)
padLen := int(data[length-1])
return data[:length-padLen]
}
2022-04-02 16:16:04 +13:00
// Generate a cryptographically secure password
2021-11-13 13:03:21 +13:00
func genPassword() string {
chars := ""
2022-04-02 16:16:04 +13:00
if passgenUpper {
2021-11-13 13:03:21 +13:00
chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
2022-04-02 16:16:04 +13:00
if passgenLower {
2021-11-13 13:03:21 +13:00
chars += "abcdefghijklmnopqrstuvwxyz"
}
2022-04-02 16:16:04 +13:00
if passgenNums {
2021-11-13 13:03:21 +13:00
chars += "1234567890"
}
2022-04-02 16:16:04 +13:00
if passgenSymbols {
2021-11-13 13:03:21 +13:00
chars += "-=!@#$^&()_+?"
}
if chars == "" {
return chars
}
2022-04-02 16:16:04 +13:00
tmp := make([]byte, passgenLength)
for i := 0; i < int(passgenLength); i++ {
2021-11-13 13:03:21 +13:00
j, _ := rand.Int(rand.Reader, new(big.Int).SetUint64(uint64(len(chars))))
tmp[i] = chars[j.Int64()]
}
2022-04-02 16:16:04 +13:00
if passgenCopy {
2021-11-13 13:03:21 +13:00
clipboard.WriteAll(string(tmp))
}
return string(tmp)
}
// Convert seconds to HH:MM:SS
func humanize(seconds int) string {
hours := int(math.Floor(float64(seconds) / 3600))
seconds %= 3600
minutes := int(math.Floor(float64(seconds) / 60))
seconds %= 60
hours = int(math.Max(float64(hours), 0))
minutes = int(math.Max(float64(minutes), 0))
seconds = int(math.Max(float64(seconds), 0))
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
2022-04-19 09:46:13 +12:00
// Convert bytes to KiB, MiB, etc.
func sizeify(size int64) string {
if size >= int64(1<<40) {
return fmt.Sprintf("%.2fT", float64(size)/(1<<40))
} else if size >= int64(1<<30) {
return fmt.Sprintf("%.2fG", float64(size)/(1<<30))
} else if size >= int64(1<<20) {
return fmt.Sprintf("%.0fM", float64(size)/(1<<20))
} else {
return fmt.Sprintf("%.0fK", float64(size)/(1<<10))
}
}
2021-11-13 13:03:21 +13:00
func main() {
2022-04-02 16:16:04 +13:00
// Create the main window
window = giu.NewMasterWindow("Picocrypt", 318, 479, giu.MasterWindowFlagsNotResizable)
// Start the dialog module
2021-11-13 13:03:21 +13:00
dialog.Init()
// Set callbacks
window.SetDropCallback(onDrop)
window.SetCloseCallback(func() bool {
return !working
})
// Set universal DPI
dpi = giu.Context.GetPlatform().GetContentScale()
// Start the UI
window.Run(draw)
}