Make repository work with go get

This commit is contained in:
Albert Strasheim
2013-11-21 12:01:25 -08:00
parent 5f328bb7a3
commit 9c5d08f665
12 changed files with 38 additions and 110 deletions

46
padding/padding.go Normal file
View File

@@ -0,0 +1,46 @@
// Package padding adds and removes padding for AES-CBC mode.
//
// Copyright (c) 2013 CloudFlare, Inc.
package padding
import "errors"
// The final byte of a padded []byte indicates the number of padding
// bytes that were added. The padding bytes are always NUL bytes and
// up to 16 bytes may be added.
//
// Examples:
//
// 1. Data to be padded has a length divisible by 16. 16 bytes will be
// added where the first 15 are 0x00 and the final byte is 0x10.
//
// 2. Data to be padded has a length with remainder 15 when divided by
// 16. One byte will be added and that byte will be 0x01 (indicating
// one byte of padding).
//
// 3. Data to be padded has a length with remainder 2 when divided by
// 16. 14 bytes will be added. The first 13 will be 0x00 and then final
// byte will be 0x0e.
//
// Removing padding is trivial: the number of bytes specified by the
// final byte are removed.
// RemovePadding removes padding from data that was added with
// AddPadding
func RemovePadding(b []byte) ([]byte, error) {
l := int(b[len(b)-1])
if l > 16 {
return nil, errors.New("Padding incorrect")
}
return b[:len(b)-l], nil
}
// AddPadding adds padding to a block of data
func AddPadding(b []byte) []byte {
l := 16 - len(b)%16
padding := make([]byte, l)
padding[l-1] = byte(l)
return append(b, padding...)
}

72
padding/padding_test.go Normal file
View File

@@ -0,0 +1,72 @@
// padding_test.go: tests for padding.go
//
// Copyright (c) 2013 CloudFlare, Inc.
package padding
import (
"bytes"
"testing"
)
func assert(t *testing.T, b bool) {
if !b {
t.Fail()
}
}
func TestAddPadding(t *testing.T) {
b := make([]byte, 16)
c := AddPadding(b)
assert(t, len(c) == 32)
assert(t, c[31] == 16)
b = make([]byte, 32)
c = AddPadding(b)
assert(t, len(c) == 48)
assert(t, c[47] == 16)
b = make([]byte, 1)
c = AddPadding(b)
assert(t, len(c) == 16)
assert(t, c[15] == 15)
b = make([]byte, 15)
c = AddPadding(b)
assert(t, len(c) == 16)
assert(t, c[15] == 1)
}
func TestRemovePadding(t *testing.T) {
b := []byte("0123456789ABCDEF")
c := AddPadding(b)
assert(t, len(c) == 32)
assert(t, c[31] == 16)
assert(t, bytes.Compare(c[:16], b[:16]) == 0)
d, err := RemovePadding(c)
assert(t, err == nil)
assert(t, len(d) == 16)
assert(t, bytes.Compare(b, d) == 0)
b = []byte("0123456789")
c = AddPadding(b)
assert(t, len(c) == 16)
assert(t, c[15] == 6)
assert(t, bytes.Compare(c[:10], b[:10]) == 0)
d, err = RemovePadding(c)
assert(t, err == nil)
assert(t, len(d) == 10)
assert(t, bytes.Compare(b, d) == 0)
}
func TestDetectBadPadding(t *testing.T) {
b := []byte("0123456789ABCDEF")
c := AddPadding(b)
assert(t, len(c) == 32)
assert(t, c[31] == 16)
assert(t, bytes.Compare(c[:16], b[:16]) == 0)
c[31] = 42
d, err := RemovePadding(c)
assert(t, err != nil)
assert(t, d == nil)
}