-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package generator | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"errors" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
ranKeyByteLength = 20 | ||
maxEncodeLen = 32 | ||
) | ||
|
||
type KeyGenerator struct { | ||
AESKey string | ||
} | ||
|
||
func encryptAESCFB(dst, src, key []byte) error { | ||
// Using IV same as key is probably bad | ||
iv := []byte(key)[:aes.BlockSize] | ||
aesBlockEncrypter, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv) | ||
aesEncrypter.XORKeyStream(dst, src) | ||
return nil | ||
} | ||
|
||
func decryptAESCFB(dst, src, key []byte) error { | ||
// Using IV same as key is probably bad | ||
iv := []byte(key)[:aes.BlockSize] | ||
aesBlockDecrypter, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv) | ||
aesDecrypter.XORKeyStream(dst, src) | ||
return nil | ||
} | ||
|
||
func NewKeyGenerator(key string) (*KeyGenerator, error) { | ||
l := len(key) | ||
if l != 16 && l != 24 && l != 32 { | ||
return nil, errors.New("invalid aes key length, should be 16, 24 or 32 bytes.") | ||
} | ||
return &KeyGenerator{ | ||
AESKey: key, | ||
}, nil | ||
} | ||
|
||
func (g *KeyGenerator) GenRandomKey(id int64) (string, error) { | ||
id_str := strconv.FormatInt(id, 16) | ||
split_str := "#" | ||
ranbuf := make([]byte, maxEncodeLen-len(id_str)-len(split_str)) | ||
_, err := rand.Read(ranbuf) | ||
if err != nil { | ||
return "", err | ||
} | ||
msg := string(ranbuf) + split_str + strconv.FormatInt(id, 16) | ||
encrypted := make([]byte, len(msg)) | ||
err = encryptAESCFB(encrypted, []byte(msg), []byte(g.AESKey)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(encrypted), nil | ||
} | ||
|
||
// get id from encrypt strings | ||
func (g *KeyGenerator) DecodeIdFromRandomKey(encrypted string) (int64, error) { | ||
decrypted := make([]byte, maxEncodeLen) | ||
byteArray, err := hex.DecodeString(encrypted) | ||
err = decryptAESCFB(decrypted, byteArray, []byte(g.AESKey)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
res := string(decrypted) | ||
split_index := strings.LastIndex(res, "#") | ||
if split_index == -1 { | ||
return 0, errors.New("invalid key format.") | ||
} | ||
device_id := res[split_index+1:] | ||
return strconv.ParseInt(string(device_id), 16, 64) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package generator | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestKeyGen(t *testing.T) { | ||
generator, err := NewKeyGenerator("INVALIDKEY") | ||
if err == nil { | ||
t.Error("should return error when key length is invalid") | ||
} | ||
testid := int64(10000) | ||
generator, err = NewKeyGenerator("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
key, err := generator.GenRandomKey(testid) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Log(key) | ||
id, err := generator.DecodeIdFromRandomKey(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if id != testid { | ||
t.Errorf("wrong id %d, want %d", id, testid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package generator | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
) | ||
|
||
const ( | ||
ranPasswordByteLength = 24 | ||
) | ||
|
||
// gen random base64 encoded password | ||
func GenRandomPassword() (string, error) { | ||
ranbuf := make([]byte, ranPasswordByteLength) | ||
_, err := rand.Read(ranbuf) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(ranbuf), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package generator | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPasswordGen(t *testing.T) { | ||
pass, err := GenRandomPassword() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Log(pass) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters