Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Apr 29, 2023
1 parent a16e448 commit 3a39abd
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 20 deletions.
23 changes: 16 additions & 7 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package codec

import "strings"
import (
"bytes"
"strings"
)

func NewAES(keyTypes ...string) *AES {
c := &AES{}
Expand Down Expand Up @@ -73,16 +76,22 @@ func GenAESKey(key []byte, typ ...string) []byte {
if !ok {
keyLen = aes128KeyLen
}
return FixedAESKey(keyLen, key)
}

var FixedAESKey = FixedKeyByWhitespacePrefix

func FixedKeyByWhitespacePrefix(keyLen int, key []byte) []byte {
if len(key) == keyLen {
return key
}

k := make([]byte, keyLen)
copy(k, key)
for i := keyLen; i < len(key); {
for j := 0; j < keyLen && i < len(key); j, i = j+1, i+1 {
k[j] ^= key[i]
}
if len(key) < keyLen {
remains := keyLen - len(key)
copy(k, bytes.Repeat([]byte(` `), remains))
copy(k[remains:], key)
} else {
copy(k, key)
}
return k
}
28 changes: 28 additions & 0 deletions aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,32 @@ func TestAes(t *testing.T) {
t.Fatal(`failed`)
}
//t.Fatal(crypto.Decode(crypted, paykey))

GenAESKey([]byte(`2`))
}

func TestGenAESKey(t *testing.T) {
key := GenAESKey([]byte(`2`))
t.Logf(`key: [%s]`, key)
if string(key) != ` 2` {
t.Fatal(`failed`)
}

key = GenAESKey([]byte(`123`))
t.Logf(`key: [%s]`, key)
if string(key) != ` 123` {
t.Fatal(`failed`)
}

key = GenAESKey([]byte(``))
t.Logf(`key: [%s]`, key)
if string(key) != ` ` {
t.Fatal(`failed`)
}

key = GenAESKey([]byte(`12345678901234567890`))
t.Logf(`key: [%s]`, key)
if string(key) != `1234567890123456` {
t.Fatal(`failed`)
}
}
38 changes: 25 additions & 13 deletions des.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@

package codec

import "crypto/des"
import (
"bytes"
"crypto/des"
)

var (
_ Codec = NewDESCBC()
_ Codec = NewDESECB()
)

func GenDESKey(key []byte) []byte {
size := des.BlockSize
kkey := make([]byte, 0, size)
ede2Key := []byte(key)
length := len(ede2Key)
if length > size {
kkey = append(kkey, ede2Key[:size]...)
} else {
div := size / length
mod := size % length
return FixedDESKey(des.BlockSize, key)
}

var FixedDESKey = FixedKeyByRepeatContent

func FixedKeyByRepeatContent(keyLen int, key []byte) []byte {
if len(key) == keyLen {
return key
}
k := make([]byte, keyLen)
length := len(key)
if length == 0 {
copy(k, bytes.Repeat([]byte(` `), keyLen))
} else if length < keyLen {
div := keyLen / length
mod := keyLen % length
for i := 0; i < div; i++ {
kkey = append(kkey, ede2Key...)
copy(k[length*i:], key)
}
kkey = append(kkey, ede2Key[:mod]...)
copy(k[length*div:], key[:mod])
} else {
copy(k, key)
}
return kkey
return k
}
26 changes: 26 additions & 0 deletions des_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codec

import (
"bytes"
"testing"
)

func TestGenDESKey(t *testing.T) {
key := GenDESKey([]byte(``))
t.Logf(`key: [%s]`, key)
if string(key) != ` ` {
t.Fatal(`failed`)
}

key = GenDESKey([]byte(`123`))
t.Logf(`key: [%s]`, key)
if !bytes.Equal(key, []byte(`12312312`)) {
t.Fatalf(`failed: %s`, key)
}

key = GenDESKey([]byte(`12345678901234567890`))
t.Logf(`key: [%s]`, key)
if string(key) != `12345678` {
t.Fatal(`failed`)
}
}

0 comments on commit 3a39abd

Please sign in to comment.