github.com/hashicorp/vault/sdk@v0.11.0/helper/xor/xor.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package xor
     5  
     6  import (
     7  	"encoding/base64"
     8  	"fmt"
     9  )
    10  
    11  // XORBytes takes two byte slices and XORs them together, returning the final
    12  // byte slice. It is an error to pass in two byte slices that do not have the
    13  // same length.
    14  func XORBytes(a, b []byte) ([]byte, error) {
    15  	if len(a) != len(b) {
    16  		return nil, fmt.Errorf("length of byte slices is not equivalent: %d != %d", len(a), len(b))
    17  	}
    18  
    19  	buf := make([]byte, len(a))
    20  
    21  	for i := range a {
    22  		buf[i] = a[i] ^ b[i]
    23  	}
    24  
    25  	return buf, nil
    26  }
    27  
    28  // XORBase64 takes two base64-encoded strings and XORs the decoded byte slices
    29  // together, returning the final byte slice. It is an error to pass in two
    30  // strings that do not have the same length to their base64-decoded byte slice.
    31  func XORBase64(a, b string) ([]byte, error) {
    32  	aBytes, err := base64.StdEncoding.DecodeString(a)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("error decoding first base64 value: %w", err)
    35  	}
    36  	if aBytes == nil || len(aBytes) == 0 {
    37  		return nil, fmt.Errorf("decoded first base64 value is nil or empty")
    38  	}
    39  
    40  	bBytes, err := base64.StdEncoding.DecodeString(b)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("error decoding second base64 value: %w", err)
    43  	}
    44  	if bBytes == nil || len(bBytes) == 0 {
    45  		return nil, fmt.Errorf("decoded second base64 value is nil or empty")
    46  	}
    47  
    48  	return XORBytes(aBytes, bBytes)
    49  }