使用子集合生成集合的算法

时间:2016-10-27 15:43:38

标签: laravel collections

我需要做的是按country_id分发我的用户。

所以,我有:

steady_clock

它为我提供了同一国家/地区内的小型用户集合的集合。

小集合的东西有不同的大小。

填写主要收藏品的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

以下是我的回答,如果您有更好的方法,请发帖!

 /**
 * @param $userGroups
 * @return int
 */
private function getMaxCompetitorByEntity($userGroups):int
{
    $max = 0;
    foreach ($userGroups as $userGroup) {
        if (sizeof($userGroup) > $max) {
            $max = sizeof($userGroup);
        }

    }
    return $max;
}

package doccentertestingencryption;

/**
 *
 */


import java.security.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.security.cert.*;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import doccentertestingencryption.CryptoConstants.*;
//import doccentertestingencryption.sLinksBC;
import doccentertestingencryption.SignData;
import doccentertestingencryption.FileLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
//import ResourceReader;

public class EncryptData
{
  private String certFile = null;
  private String encString = null;
  private String encryptMode = null;
  private X509Certificate cert = null;
  private SecretKey symmKey = null;

  CryptoConstants crypto = null;

 public EncryptData() throws Exception 

 {
    try
    {
        crypto = new CryptoConstants();
        certFile = crypto.getCertFileName();
        if (certFile != null)
         try
         {
            initSecurity();
         }

         catch  ( ExceptionInInitializerError e )
         {
             throw e;
         }

    }
    catch(ExceptionInInitializerError ex )
    {
        throw ex;
    }
 } // end contructor EncryptData

 public EncryptData(String vendorAcro) throws Exception
 {

    crypto = new CryptoConstants();
    certFile =  crypto.getCertFileName(vendorAcro);
    if (certFile != null)
      try
      {
         initSecurity();
      }
      catch  ( Exception e )
      {
          throw e;
      }
    else
         throw new Exception(vendorAcro+"certFile not found in CryptoConstants class") ;

 }// end contructor EncryptData

 private void initSecurity() throws CertificateException, Exception 

 {
        try
        {    
             Security.addProvider(new BouncyCastleProvider());
             CertificateFactory cf = CertificateFactory.getInstance(CryptoConstants.certType);
             //InputStream inStream = this.getClass().getResourceAsStream(certFile);
             InputStream inStream = this.getClass().getResourceAsStream(certFile);
            // ByteArrayOutputStream baos = new ByteArrayOutputStream();    

             //cert = (X509Certificate) cf.generateCertificate(inStream);
             cert = (X509Certificate) cf.generateCertificate(inStream);
             if (cert == null)
               throw new Exception("in EncryptData.init: cert = null");

             KeyGenerator keyGen = KeyGenerator.getInstance(CryptoConstants.symmEncryptMode);
             symmKey = keyGen.generateKey();
        }
        catch(ExceptionInInitializerError ex)
        {
            throw ex;
        }
 }

  @SuppressWarnings("null")
 public String encrypt(String strToEncrypt) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException 
 {
     try
     {
     //String encString= null;
     byte dataToEncrypt[] = strToEncrypt.getBytes();
     Cipher symmCipher = null;
         try {
             symmCipher = Cipher.getInstance(CryptoConstants.symmEncryptMode);
         } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
             Logger.getLogger(EncryptData.class.getName()).log(Level.SEVERE, null, ex);
         }
     symmCipher.init(Cipher.ENCRYPT_MODE, symmKey);
     byte[] encrypted  = symmCipher.doFinal(dataToEncrypt);
     encString= new String(Base64.encode(encrypted));
     encString = URLEncoder.encode(encString, "UTF-8");
    return(encString);
     }
     catch (ExceptionInInitializerError ei )
     {
         throw ei;
     }
} //end method create Signature

 public String encryptSymmKey() throws  Exception
 {
    Cipher asymmCipher = Cipher.getInstance(CryptoConstants.asymmEncryptCipher, "BC");
    asymmCipher.init(Cipher.ENCRYPT_MODE, cert.getPublicKey());
    byte[] encSymmKeyBytes = asymmCipher.doFinal(symmKey.getEncoded());
    String encSymmKeyString= new String(Base64.encode(encSymmKeyBytes));
    encSymmKeyString = URLEncoder.encode(encSymmKeyString, "UTF-8");
    return(encSymmKeyString);
} //end method create Signature

 public String encryptQSValues(String unencryptedQS) throws  Exception
 {
   String encryptedQS = null;
   String nameNequals = null;
   String value = null;
   String nameValuePair = null;

   int i = 0;
   try
   {
     encryptedQS = "ENCQS="+ encrypt(unencryptedQS);
     encryptedQS = encryptedQS +"&KS="+ encryptSymmKey();
   }
   catch (Exception e)
   {
     throw e;
   }

  return(encryptedQS);
} //end method encryptQSValues

  /**
   * Returns the signature
   * @return String
   */
  public String getEncString()
  {
     return encString;
  }

  public Provider[] getProviders()
  {
    Provider provs[] = Security.getProviders();
    return provs;
  }

  public static void dumpProviders()
  {
    Provider provs[] = Security.getProviders();
    for (int i=0; i< provs.length;i++)
     System.out.println("providers["+i+"]= "+provs[i].getName());
  }


} // end EncryptData