Skip to content

Commit

Permalink
Use URL safe encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthb committed Nov 22, 2023
1 parent 53af4a6 commit c87621c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
9 changes: 2 additions & 7 deletions aesItemCrypter.go → aesitemcrypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ type aesCryptedItem struct {
func (c *aesCryptedItem) Encrypt(ctx context.Context,
item map[string]types.AttributeValue,
) (string, error) {
// Marshal the item into a JSON string
it, err := marshalJSON(item)
if err != nil {
return "", err
}
c.block.Encrypt(it, it)
return base64.StdEncoding.EncodeToString(it), nil
return base64.URLEncoding.EncodeToString(it), nil
}

// Decrypt decrypts a dynamodb item along with an encryption context.
Expand All @@ -51,18 +50,14 @@ func (c *aesCryptedItem) Decrypt(
ctx context.Context,
item string,
) (map[string]types.AttributeValue, error) {
// Decrypt the item with kmsKeyID
decodedItem, err := base64.StdEncoding.DecodeString(item)
decodedItem, err := base64.URLEncoding.DecodeString(item)
if err != nil {
return nil, err
}
c.block.Decrypt(decodedItem, decodedItem)

// Unmarshal the item into a map[string]types.AttributeValue
it := map[string]types.AttributeValue{}
if err := json.Unmarshal(decodedItem, &it); err != nil {
return nil, err
}

return it, nil
}
7 changes: 2 additions & 5 deletions kmsItemCrypter.go → kmsitemcrypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func (c *kmsCryptedItem) Encrypt(
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(out.CiphertextBlob), nil
return base64.URLEncoding.EncodeToString(out.CiphertextBlob), nil
}

// Decrypt decrypts a dynamodb item. If ctx contains an encryption context, it will be used
Expand All @@ -57,7 +56,7 @@ func (c *kmsCryptedItem) Decrypt(
ctx context.Context,
item string,
) (map[string]types.AttributeValue, error) {
decodedItem, err := base64.StdEncoding.DecodeString(item)
decodedItem, err := base64.URLEncoding.DecodeString(item)
if err != nil {
return nil, err
}
Expand All @@ -72,11 +71,9 @@ func (c *kmsCryptedItem) Decrypt(
if err != nil {
return nil, err
}

it := map[string]types.AttributeValue{}
if err := json.Unmarshal(out.Plaintext, &it); err != nil {
return nil, err
}

return it, nil
}

0 comments on commit c87621c

Please sign in to comment.