Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,8 @@ func (x *ctr) XORKeyStream(dst, src []byte) {
}

func xorBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ {
n := min(len(b), len(a))
for i := range n {
dst[i] = a[i] ^ b[i]
}
return n
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/cloudtrust/zip

go 1.27.0

require (
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9
golang.org/x/crypto v0.55.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9 h1:K8gF0eekWPEX+57l30ixxzGhHH/qscI3JCnuhbN6V4M=
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9/go.mod h1:9BnoKCcgJ/+SLhfAXj15352hTOuVmG5Gzo8xNRINfqI=
golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M=
golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis=
8 changes: 4 additions & 4 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func readTestZip(t *testing.T, zt ZipTest) {
// test simultaneous reads
n := 0
done := make(chan bool)
for i := 0; i < 5; i++ {
for range 5 {
for j, ft := range zt.File {
go func(j int, ft ZipTestFile) {
readTestFile(t, zt, ft, z.File[j])
Expand Down Expand Up @@ -383,7 +383,7 @@ func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File) {
var c []byte
if ft.Content != nil {
c = ft.Content
} else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil {
} else if c, err = os.ReadFile("testdata/" + ft.File); err != nil {
t.Error(err)
return
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func TestInvalidFiles(t *testing.T) {
}

func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) {
data, err := ioutil.ReadFile(filepath.Join("testdata", fileName))
data, err := os.ReadFile(filepath.Join("testdata", fileName))
if err != nil {
panic("Error reading " + fileName + ": " + err.Error())
}
Expand Down Expand Up @@ -599,7 +599,7 @@ func TestIssue11146(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = ioutil.ReadAll(r)
_, err = io.ReadAll(r)
if err != io.ErrUnexpectedEOF {
t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err)
}
Expand Down
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (fi headerFileInfo) Size() int64 {
func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi headerFileInfo) ModTime() time.Time { return fi.fh.ModTime() }
func (fi headerFileInfo) Mode() os.FileMode { return fi.fh.Mode() }
func (fi headerFileInfo) Sys() interface{} { return fi.fh }
func (fi headerFileInfo) Sys() any { return fi.fh }

// FileInfoHeader creates a partially-populated FileHeader from an
// os.FileInfo.
Expand Down
6 changes: 3 additions & 3 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package zip
import (
"bytes"
"io"
"io/ioutil"

"math/rand"
"os"
"testing"
Expand Down Expand Up @@ -167,7 +167,7 @@ func testReadFile(t *testing.T, f *File, wt *WriteTest) {
if err != nil {
t.Fatal("opening:", err)
}
b, err := ioutil.ReadAll(rc)
b, err := io.ReadAll(rc)
if err != nil {
t.Fatal("reading:", err)
}
Expand All @@ -187,7 +187,7 @@ func BenchmarkCompressedZipGarbage(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
zw := NewWriter(&buf)
for j := 0; j < 3; j++ {
for range 3 {
w, _ := zw.CreateHeader(&FileHeader{
Name: "foo",
Method: Deflate,
Expand Down
12 changes: 6 additions & 6 deletions zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"

"sort"
"strings"
"testing"
Expand All @@ -22,7 +22,7 @@ func TestOver65kFiles(t *testing.T) {
buf := new(bytes.Buffer)
w := NewWriter(buf)
const nFiles = (1 << 16) + 42
for i := 0; i < nFiles; i++ {
for i := range nFiles {
_, err := w.CreateHeader(&FileHeader{
Name: fmt.Sprintf("%d.dat", i),
Method: Store, // avoid Issue 6136 and Issue 6138
Expand All @@ -42,7 +42,7 @@ func TestOver65kFiles(t *testing.T) {
if got := len(zr.File); got != nFiles {
t.Fatalf("File contains %d files, want %d", got, nFiles)
}
for i := 0; i < nFiles; i++ {
for i := range nFiles {
want := fmt.Sprintf("%d.dat", i)
if zr.File[i].Name != want {
t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want)
Expand Down Expand Up @@ -251,7 +251,7 @@ func testZip64(t testing.TB, size int64) *rleBuffer {
for i := range chunk {
chunk[i] = '.'
}
for i := 0; i < chunks; i++ {
for range chunks {
_, err := f.Write(chunk)
if err != nil {
t.Fatal("write chunk:", err)
Expand All @@ -277,13 +277,13 @@ func testZip64(t testing.TB, size int64) *rleBuffer {
t.Fatal("opening:", err)
}
rc.(*checksumReader).hash = fakeHash32{}
for i := 0; i < chunks; i++ {
for range chunks {
_, err := io.ReadFull(rc, chunk)
if err != nil {
t.Fatal("read:", err)
}
}
gotEnd, err := ioutil.ReadAll(rc)
gotEnd, err := io.ReadAll(rc)
if err != nil {
t.Fatal("read end:", err)
}
Expand Down
22 changes: 11 additions & 11 deletions zipcrypto.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package zip

import (
"io"
"bytes"
"hash/crc32"
"io"
)

type ZipCrypto struct {
password []byte
Keys [3]uint32
Keys [3]uint32
}

func NewZipCrypto(passphrase []byte) *ZipCrypto {
Expand All @@ -29,10 +29,10 @@ func (z *ZipCrypto) init() {
}

func (z *ZipCrypto) updateKeys(byteValue byte) {
z.Keys[0] = crc32update(z.Keys[0], byteValue);
z.Keys[1] += z.Keys[0] & 0xff;
z.Keys[1] = z.Keys[1] * 134775813 + 1;
z.Keys[2] = crc32update(z.Keys[2], (byte) (z.Keys[1] >> 24));
z.Keys[0] = crc32update(z.Keys[0], byteValue)
z.Keys[1] += z.Keys[0] & 0xff
z.Keys[1] = z.Keys[1]*134775813 + 1
z.Keys[2] = crc32update(z.Keys[2], (byte)(z.Keys[1]>>24))
}

func (z *ZipCrypto) magicByte() byte {
Expand All @@ -43,7 +43,7 @@ func (z *ZipCrypto) magicByte() byte {
func (z *ZipCrypto) Encrypt(data []byte) []byte {
length := len(data)
chiper := make([]byte, length)
for i := 0; i < length; i++ {
for i := range length {
v := data[i]
chiper[i] = v ^ z.magicByte()
z.updateKeys(v)
Expand All @@ -55,15 +55,15 @@ func (z *ZipCrypto) Decrypt(chiper []byte) []byte {
length := len(chiper)
plain := make([]byte, length)
for i, c := range chiper {
v := c ^ z.magicByte();
v := c ^ z.magicByte()
z.updateKeys(v)
plain[i] = v
}
return plain
}

func crc32update(pCrc32 uint32, bval byte) uint32 {
return crc32.IEEETable[(pCrc32 ^ uint32(bval)) & 0xff] ^ (pCrc32 >> 8)
return crc32.IEEETable[(pCrc32^uint32(bval))&0xff] ^ (pCrc32 >> 8)
}

func ZipCryptoDecryptor(r *io.SectionReader, password []byte) (*io.SectionReader, error) {
Expand Down Expand Up @@ -102,8 +102,8 @@ func (z *zipCryptoWriter) Write(p []byte) (n int, err error) {
return
}

func ZipCryptoEncryptor(i io.Writer, pass passwordFn, fw *fileWriter) (io.Writer, error) {
func ZipCryptoEncryptor(i io.Writer, pass passwordFn, fw *fileWriter) (io.Writer, error) {
z := NewZipCrypto(pass())
zc := &zipCryptoWriter{i, z, true, fw}
return zc, nil
}
}