Org.BouncyCastle.Math.BigInteger.Xor C# (CSharp) Метод

Xor() публичный Метод

public Xor ( BigInteger value ) : BigInteger
value BigInteger
Результат BigInteger
		public BigInteger Xor(
			BigInteger value)
		{
			if (this.sign == 0)
				return value;

			if (value.sign == 0)
				return this;

			int[] aMag = this.sign > 0
				? this.magnitude
				: Add(One).magnitude;

			int[] bMag = value.sign > 0
				? value.magnitude
				: value.Add(One).magnitude;

			// TODO Can just replace with sign != value.sign?
			bool resultNeg = (sign < 0 && value.sign >= 0) || (sign >= 0 && value.sign < 0);
			int resultLength = System.Math.Max(aMag.Length, bMag.Length);
			int[] resultMag = new int[resultLength];

			int aStart = resultMag.Length - aMag.Length;
			int bStart = resultMag.Length - bMag.Length;

			for (int i = 0; i < resultMag.Length; ++i)
			{
				int aWord = i >= aStart ? aMag[i - aStart] : 0;
				int bWord = i >= bStart ? bMag[i - bStart] : 0;

				if (this.sign < 0)
				{
					aWord = ~aWord;
				}

				if (value.sign < 0)
				{
					bWord = ~bWord;
				}

				resultMag[i] = aWord ^ bWord;

				if (resultNeg)
				{
					resultMag[i] = ~resultMag[i];
				}
			}

			BigInteger result = new BigInteger(1, resultMag, true);

			// TODO Optimise this case
			if (resultNeg)
			{
				result = result.Not();
			}

			return result;
		}

Usage Example

Пример #1
0
		public int DoFinal(byte[] output, int outOff)
		{
			int extra = bufOff;
			if (!forEncryption)
			{
				if (extra < macSize)
					throw new InvalidCipherTextException("data too short");

				extra -= macSize;
			}

			if (extra > 0)
			{
				byte[] tmp = new byte[BlockSize];
				Array.Copy(bufBlock, 0, tmp, 0, extra);
				gCTRBlock(tmp, extra, output, outOff);
			}

			// Final gHASH
			BigInteger X = BigInteger.ValueOf(A.Length * 8).ShiftLeft(64).Add(
				BigInteger.ValueOf(totalLength * 8));
			//trace("len(A)||len(C): " + dumpBigInt(X));

			S = multiply(S.Xor(X), H);
			//trace("GHASH(H,A,C): " + dumpBigInt(S));

			// T = MSBt(GCTRk(J0,S))
			byte[] tBytes = new byte[BlockSize];
			cipher.ProcessBlock(J0, 0, tBytes, 0);
			//trace("E(K,Y0): " + new string(Hex.encode(tmp)));
			BigInteger T = S.Xor(new BigInteger(1, tBytes));

			// TODO Fix this if tagLength becomes configurable
			byte[] tag = asBlock(T);
			//trace("T: " + new string(Hex.encode(tag)));

			int resultLen = extra;

			if (forEncryption)
			{
				this.macBlock = tag;
				Array.Copy(tag, 0, output, outOff + bufOff, tag.Length);
				resultLen += tag.Length;
			}
			else
			{
				this.macBlock = new byte[macSize];
				Array.Copy(bufBlock, extra, macBlock, 0, macSize);
				if (!Arrays.AreEqual(tag, this.macBlock))
					throw new InvalidCipherTextException("mac check in GCM failed");
			}

			Reset(false);

			return resultLen;
		}
All Usage Examples Of Org.BouncyCastle.Math.BigInteger::Xor