Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.agreement.srp;
  
  
  
 public class SRP6Util
 {
     private static BigInteger ZERO = BigInteger.valueOf(0);
     private static BigInteger ONE = BigInteger.valueOf(1);
 
     public static BigInteger calculateK(Digest digestBigInteger NBigInteger g)
     {
         return hashPaddedPair(digestNNg);
     }
 
     public static BigInteger calculateU(Digest digestBigInteger NBigInteger ABigInteger B)
     {
         return hashPaddedPair(digestNAB);
     }
 
     public static BigInteger calculateX(Digest digestBigInteger Nbyte[] saltbyte[] identitybyte[] password)
     {
         byte[] output = new byte[digest.getDigestSize()];
 
         digest.update(identity, 0, identity.length);
         digest.update((byte)':');
         digest.update(password, 0, password.length);
         digest.doFinal(output, 0);
 
         digest.update(salt, 0, salt.length);
         digest.update(output, 0, output.length);
         digest.doFinal(output, 0);
 
         return new BigInteger(1, output);
     }
 
     public static BigInteger generatePrivateValue(Digest digestBigInteger NBigInteger gSecureRandom random)
     {
         int minBits = Math.min(256, N.bitLength() / 2);
         BigInteger min = .shiftLeft(minBits - 1);
         BigInteger max = N.subtract();
 
         return BigIntegers.createRandomInRange(minmaxrandom);
     }
 
     public static BigInteger validatePublicValue(BigInteger NBigInteger val)
         throws CryptoException
     {
         val = val.mod(N);
 
         // Check that val % N != 0
         if (val.equals())
         {
             throw new CryptoException("Invalid public value: 0");
         }
 
         return val;
     }
    
Computes the client evidence message (M1) according to the standard routine: M1 = H( A | B | S )

Parameters:
digest The Digest used as the hashing function H
N Modulus used to get the pad length
A The public client value
B The public server value
S The secret calculated by both sides
Returns:
M1 The calculated client evidence message
 
 	public static BigInteger calculateM1(Digest digestBigInteger NBigInteger ABigInteger BBigInteger S) {
 		BigInteger M1 = hashPaddedTriplet(digest,N,A,B,S);
 		return M1;
 	}

Computes the server evidence message (M2) according to the standard routine: M2 = H( A | M1 | S )

Parameters:
digest The Digest used as the hashing function H
N Modulus used to get the pad length
A The public client value
M1 The client evidence message
S The secret calculated by both sides
Returns:
M2 The calculated server evidence message
 
 	public static BigInteger calculateM2(Digest digestBigInteger NBigInteger ABigInteger M1BigInteger S){
 		BigInteger M2 = hashPaddedTriplet(digest,N,A,M1,S);
 		return M2;
 	}

Computes the final Key according to the standard routine: Key = H(S)

Parameters:
digest The Digest used as the hashing function H
N Modulus used to get the pad length
S The secret calculated by both sides
Returns:
 
	public static BigInteger calculateKey(Digest digestBigInteger NBigInteger S) {
		int padLength = (N.bitLength() + 7) / 8;
		byte[] _S = getPadded(S,padLength);
		digest.update(_S, 0, _S.length);
		byte[] output = new byte[digest.getDigestSize()];
        digest.doFinal(output, 0);
        return new BigInteger(1, output);
	}
	private static BigInteger hashPaddedTriplet(Digest digestBigInteger NBigInteger n1BigInteger n2BigInteger n3){
        int padLength = (N.bitLength() + 7) / 8;
        byte[] n1_bytes = getPadded(n1padLength);
        byte[] n2_bytes = getPadded(n2padLength);
        byte[] n3_bytes = getPadded(n3padLength);
        digest.update(n1_bytes, 0, n1_bytes.length);
        digest.update(n2_bytes, 0, n2_bytes.length);
        digest.update(n3_bytes, 0, n3_bytes.length);
        byte[] output = new byte[digest.getDigestSize()];
        digest.doFinal(output, 0);
        return new BigInteger(1, output);
    }
    private static BigInteger hashPaddedPair(Digest digestBigInteger NBigInteger n1BigInteger n2)
    {
        int padLength = (N.bitLength() + 7) / 8;
        byte[] n1_bytes = getPadded(n1padLength);
        byte[] n2_bytes = getPadded(n2padLength);
        digest.update(n1_bytes, 0, n1_bytes.length);
        digest.update(n2_bytes, 0, n2_bytes.length);
        byte[] output = new byte[digest.getDigestSize()];
        digest.doFinal(output, 0);
        return new BigInteger(1, output);
    }
    private static byte[] getPadded(BigInteger nint length)
    {
        byte[] bs = BigIntegers.asUnsignedByteArray(n);
        if (bs.length < length)
        {
            byte[] tmp = new byte[length];
            System.arraycopy(bs, 0, tmplength - bs.lengthbs.length);
            bs = tmp;
        }
        return bs;
    }
New to GrepCode? Check out our FAQ X