github.com/iDigitalFlame/xmt@v0.5.4/data/crypto/xor.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package crypto
    18  
    19  import "github.com/iDigitalFlame/xmt/data/crypto/subtle"
    20  
    21  // XOR is an alias for a byte array that acts as the XOR key data buffer.
    22  type XOR []byte
    23  
    24  // BlockSize returns the cipher's block size.
    25  func (x XOR) BlockSize() int {
    26  	return len(x)
    27  }
    28  
    29  // Operate preforms the XOR operation on the specified byte
    30  // array using the cipher as the key.
    31  func (x XOR) Operate(b []byte) {
    32  	if len(x) == 0 {
    33  		return
    34  	}
    35  	subtle.XorOp(b, x)
    36  }
    37  
    38  // Decrypt preforms the XOR operation on the specified byte array using the cipher
    39  // as the key.
    40  func (x XOR) Decrypt(dst, src []byte) {
    41  	subtle.XorBytes(dst, x, src)
    42  }
    43  
    44  // Encrypt preforms the XOR operation on the specified byte array using the cipher
    45  // as the key.
    46  func (x XOR) Encrypt(dst, src []byte) {
    47  	subtle.XorBytes(dst, x, src)
    48  }