Browse Source

update Decrypt function signature to match spec

use string for password, []byte for data
Steven Schobert 9 years ago
parent
commit
60e9b58a9d
1 changed files with 10 additions and 10 deletions
  1. 10 10
      rncryptor.go

+ 10 - 10
rncryptor.go

@@ -11,14 +11,14 @@ import(
   "golang.org/x/crypto/pbkdf2"
   "golang.org/x/crypto/pbkdf2"
 )
 )
 
 
-func Decrypt(password, text []byte) ([]byte, error) {
-  version         := text[:1]
-  options         := text[1:2]
-  encSalt         := text[2:10]
-  hmacSalt        := text[10:18]
-  iv              := text[18:34]
-  cipherText      := text[34:(len(text)-66+34)]
-  expectedHmac    := text[len(text)-32:len(text)]
+func Decrypt(password string, data []byte) ([]byte, error) {
+  version         := data[:1]
+  options         := data[1:2]
+  encSalt         := data[2:10]
+  hmacSalt        := data[10:18]
+  iv              := data[18:34]
+  cipherText      := data[34:(len(data)-66+34)]
+  expectedHmac    := data[len(data)-32:len(data)]
 
 
   msg := make([]byte, 0)
   msg := make([]byte, 0)
   msg = append(msg, version...)
   msg = append(msg, version...)
@@ -28,7 +28,7 @@ func Decrypt(password, text []byte) ([]byte, error) {
   msg = append(msg, iv...)
   msg = append(msg, iv...)
   msg = append(msg, cipherText...)
   msg = append(msg, cipherText...)
 
 
-  hmacKey := pbkdf2.Key(password, hmacSalt, 10000, 32, sha1.New)
+  hmacKey := pbkdf2.Key([]byte(password), hmacSalt, 10000, 32, sha1.New)
   testHmac := hmac.New(sha256.New, hmacKey)
   testHmac := hmac.New(sha256.New, hmacKey)
   testHmac.Write(msg)
   testHmac.Write(msg)
   testHmacVal := testHmac.Sum(nil)
   testHmacVal := testHmac.Sum(nil)
@@ -39,7 +39,7 @@ func Decrypt(password, text []byte) ([]byte, error) {
     return nil, errors.New("Password may be incorrect, or the data has been corrupted. (HMAC could not be verified)")
     return nil, errors.New("Password may be incorrect, or the data has been corrupted. (HMAC could not be verified)")
   }
   }
 
 
-  cipherKey := pbkdf2.Key(password, encSalt, 10000, 32, sha1.New)
+  cipherKey := pbkdf2.Key([]byte(password), encSalt, 10000, 32, sha1.New)
   cipherBlock, err := aes.NewCipher(cipherKey)
   cipherBlock, err := aes.NewCipher(cipherKey)
   if err != nil {
   if err != nil {
     return nil, err
     return nil, err