github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/crypto/aes_gcm_test.go (about)

     1  // Copyright (c) 2016 Readium Foundation
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification,
     4  // are permitted provided that the following conditions are met:
     5  //
     6  // 1. Redistributions of source code must retain the above copyright notice, this
     7  //    list of conditions and the following disclaimer.
     8  // 2. Redistributions in binary form must reproduce the above copyright notice,
     9  //    this list of conditions and the following disclaimer in the documentation and/or
    10  //    other materials provided with the distribution.
    11  // 3. Neither the name of the organization nor the names of its contributors may be
    12  //    used to endorse or promote products derived from this software without specific
    13  //    prior written permission
    14  //
    15  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    16  // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    17  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    18  // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
    19  // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    20  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    21  // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    22  // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    23  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    24  // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    25  
    26  package crypto
    27  
    28  import (
    29  	"bytes"
    30  	"crypto/aes"
    31  	"crypto/cipher"
    32  	"encoding/hex"
    33  	"testing"
    34  )
    35  
    36  func TestEncryptGCM(t *testing.T) {
    37  	key, _ := hex.DecodeString("11754cd72aec309bf52f7687212e8957")
    38  
    39  	encrypter := NewAESGCMEncrypter()
    40  
    41  	data := []byte("The quick brown fox jumps over the lazy dog")
    42  
    43  	r := bytes.NewReader(data)
    44  	w := new(bytes.Buffer)
    45  
    46  	if err := encrypter.Encrypt(ContentKey(key), r, w); err != nil {
    47  		t.Fatal("Encryption failed", err)
    48  	}
    49  
    50  	block, _ := aes.NewCipher(key)
    51  	gcm, _ := cipher.NewGCM(block)
    52  
    53  	out := w.Bytes()
    54  	t.Logf("nonce size: %#v", gcm.NonceSize())
    55  	t.Logf("nonce: %#v", out[0:gcm.NonceSize()])
    56  	t.Logf("ciphertext: %#v", out[gcm.NonceSize():])
    57  	clear := make([]byte, 0)
    58  	clear, err := gcm.Open(clear, out[0:gcm.NonceSize()], out[gcm.NonceSize():], nil)
    59  
    60  	if err != nil {
    61  		t.Fatal("Decryption failed", err)
    62  	}
    63  
    64  	if diff := bytes.Compare(data, clear); diff != 0 {
    65  		t.Logf("Original: %#v", data)
    66  		t.Logf("After cycle: %#v", clear)
    67  		t.Errorf("Expected encryption-decryption to return original")
    68  	}
    69  }