123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- //using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
- #if UNITY_WSA && !UNITY_EDITOR && !ENABLE_IL2CPP
- using System.TypeFix;
- #endif
- //using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
- using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
- namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
- {
- /**
- * Generator for MGF1 as defined in Pkcs 1v2
- */
- public class Mgf1BytesGenerator : IDerivationFunction
- {
- private IDigest digest;
- private byte[] seed;
- private int hLen;
- /**
- * @param digest the digest to be used as the source of Generated bytes
- */
- public Mgf1BytesGenerator(
- IDigest digest)
- {
- this.digest = digest;
- this.hLen = digest.GetDigestSize();
- }
- public void Init(
- IDerivationParameters parameters)
- {
- if (!(typeof(MgfParameters).IsInstanceOfType(parameters)))
- {
- throw new ArgumentException("MGF parameters required for MGF1Generator");
- }
- MgfParameters p = (MgfParameters)parameters;
- seed = p.GetSeed();
- }
- /**
- * return the underlying digest.
- */
- public IDigest Digest
- {
- get
- {
- return digest;
- }
- }
- /**
- * int to octet string.
- */
- private void ItoOSP(
- int i,
- byte[] sp)
- {
- sp[0] = (byte)((uint) i >> 24);
- sp[1] = (byte)((uint) i >> 16);
- sp[2] = (byte)((uint) i >> 8);
- sp[3] = (byte)((uint) i >> 0);
- }
- /**
- * fill len bytes of the output buffer with bytes Generated from
- * the derivation function.
- *
- * @throws DataLengthException if the out buffer is too small.
- */
- public int GenerateBytes(
- byte[] output,
- int outOff,
- int length)
- {
- if ((output.Length - length) < outOff)
- {
- throw new DataLengthException("output buffer too small");
- }
- byte[] hashBuf = new byte[hLen];
- byte[] C = new byte[4];
- int counter = 0;
- digest.Reset();
- if (length > hLen)
- {
- do
- {
- ItoOSP(counter, C);
- digest.BlockUpdate(seed, 0, seed.Length);
- digest.BlockUpdate(C, 0, C.Length);
- digest.DoFinal(hashBuf, 0);
- Array.Copy(hashBuf, 0, output, outOff + counter * hLen, hLen);
- }
- while (++counter < (length / hLen));
- }
- if ((counter * hLen) < length)
- {
- ItoOSP(counter, C);
- digest.BlockUpdate(seed, 0, seed.Length);
- digest.BlockUpdate(C, 0, C.Length);
- digest.DoFinal(hashBuf, 0);
- Array.Copy(hashBuf, 0, output, outOff + counter * hLen, length - (counter * hLen));
- }
- return length;
- }
- }
- }
- #pragma warning restore
- #endif
|