Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10433558
data_test.go
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
data_test.go
View Options
// Package data provides utilities for interacting with database data
// throughout Write.as.
package
data
import
(
"bytes"
"crypto/rand"
"strings"
"testing"
)
func
TestEncDec
(
t
*
testing
.
T
)
{
// Generate a random key with a valid length
k
:=
make
([]
byte
,
keyLen
)
_
,
err
:=
rand
.
Read
(
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
runEncDec
(
t
,
k
,
"this is my secret message™. 😄"
,
nil
)
runEncDec
(
t
,
k
,
"mygreatemailaddress@gmail.com"
,
nil
)
}
func
TestAuthentication
(
t
*
testing
.
T
)
{
// Generate a random key with a valid length
k
:=
make
([]
byte
,
keyLen
)
_
,
err
:=
rand
.
Read
(
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
runEncDec
(
t
,
k
,
"mygreatemailaddress@gmail.com"
,
func
(
c
[]
byte
)
[]
byte
{
c
[
0
]
=
'a'
t
.
Logf
(
"Modified: %s\n"
,
c
)
return
c
})
}
func
runEncDec
(
t
*
testing
.
T
,
k
[]
byte
,
plaintext
string
,
transform
func
([]
byte
)
[]
byte
)
{
t
.
Logf
(
"Plaintext: %s\n"
,
plaintext
)
// Encrypt the data
ciphertext
,
err
:=
Encrypt
(
k
,
plaintext
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
t
.
Logf
(
"Ciphertext: %s\n"
,
ciphertext
)
if
transform
!=
nil
{
ciphertext
=
transform
(
ciphertext
)
}
// Decrypt the data
decryptedText
,
err
:=
Decrypt
(
k
,
ciphertext
)
if
err
!=
nil
{
if
transform
!=
nil
&&
strings
.
Contains
(
err
.
Error
(),
"message authentication failed"
)
{
// We modified the ciphertext; make sure we're getting the right error
t
.
Logf
(
"%v\n"
,
err
)
return
}
t
.
Fatal
(
err
)
}
t
.
Logf
(
"Decrypted: %s\n"
,
string
(
decryptedText
))
if
!
bytes
.
Equal
([]
byte
(
plaintext
),
decryptedText
)
{
t
.
Errorf
(
"Plaintext mismatch: got %x vs %x"
,
plaintext
,
decryptedText
)
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Jan 20, 4:55 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3137650
Attached To
rWC Write.as Web Core
Event Timeline
Log In to Comment