Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

In this page you can find the example usage for java.util Arrays copyOf.

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:Main.java

public static String completeJweFromSIM(String jweSIM) {
    // android.os.Debug.waitForDebugger();

    try {/*ww w.  ja  v  a  2 s.  com*/
        if (jweSIM != null && jweSIM.length() > 0) {
            String parts[] = jweSIM.split("\\.");
            ;
            if (parts != null && parts.length == 5) {
                // retrieve hmac key
                byte hmac_key[] = Base64.decode(parts[4], Base64.URL_SAFE);
                if (hmac_key != null && hmac_key.length == 16) {
                    // init hash instance
                    Mac hmac = Mac.getInstance("HmacSHA256", "SC");
                    hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));

                    byte[] aad = parts[0].getBytes();
                    long al = aad.length * 8;
                    byte[] iv_key = decodeB64(parts[2]);
                    byte[] cryptedBytes = decodeB64(parts[3]);

                    // build data to hash
                    byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8];
                    int offset = 0;
                    System.arraycopy(aad, offset, hmacData, 0, aad.length);
                    offset += aad.length;
                    System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length);
                    offset += iv_key.length;
                    System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length);
                    offset += cryptedBytes.length;
                    ByteBuffer buffer = ByteBuffer.allocate(8);
                    buffer.putLong(al);
                    System.arraycopy(buffer.array(), 0, hmacData, offset, 8);

                    // compute hac value
                    byte[] hmacValue = hmac.doFinal(hmacData);
                    // authentication tag
                    byte[] auth_tag = Arrays.copyOf(hmacValue, 16);
                    String auth_tag64 = encodeB64(auth_tag);

                    // A.2.7. Complete Representation
                    String finalString = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3] + "."
                            + auth_tag64;

                    //                  // just for verification
                    //                  byte jwt64 [] = decryptJWE(finalString, RsaKeyTim.privRsaKey);
                    //                  if(jwt64!=null) {
                    //                     String jws = new String(jwt64);
                    //                     Log.d("completeJweFromSIM", "jws verify Key TIM :"+verifyJWS(jws,RsaKeyTim.pubRsaKey));
                    //                  }

                    return finalString;
                }
            }
            // 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.llorllale.youtrack.api.session.UsernamePassword.java

/**
 * Ctor.//ww  w  .ja v  a  2 s.  c  om
 * @param youtrackUrl the URL of the YouTrack API endpoint
 * @param username the principal
 * @param password the credentials
 * @param httpClient http client to use to call the remote API
 * @since 0.1.0
 */
UsernamePassword(final URL youtrackUrl, String username, char[] password, HttpClient httpClient) {
    this.youtrackUrl = youtrackUrl;
    this.httpClient = httpClient;
    this.username = username;
    this.password = Arrays.copyOf(password, password.length);
}

From source file:com.opengamma.analytics.math.surface.DoublesSurface.java

/**
 * @param xData An array of <i>x</i> data, not null
 * @param yData An array of <i>y</i> data, not null
 * @param zData An array of <i>z</i> data, not null
 *///from  w  w  w.j  a  va 2s.c o m
public DoublesSurface(final double[] xData, final double[] yData, final double[] zData) {
    super();
    Validate.notNull(xData, "x data");
    Validate.notNull(yData, "y data");
    Validate.notNull(zData, "z data");
    Validate.isTrue(xData.length == yData.length);
    Validate.isTrue(xData.length == zData.length);
    _n = xData.length;
    _xData = Arrays.copyOf(xData, _n);
    _yData = Arrays.copyOf(yData, _n);
    _zData = Arrays.copyOf(zData, _n);
}

From source file:com.offbynull.peernetic.common.identification.IdGenerator.java

/**
 * Constructs a {@link DefaultIdGenerator} object using a {@link Random} as its underlying source.
 * @param random random number generator (should be a {@link SecureRandom} instance in most cases)
 * @param limit maximum value the id generated can be
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if limit is 0
 *//*  w w w .j  a v a2  s  .  c o  m*/
public IdGenerator(Random random, byte[] limit) {
    Validate.notNull(random);
    Validate.notNull(limit);
    Validate.isTrue(limit.length > 0 && !new BigInteger(1, limit).equals(BigInteger.ZERO));
    this.random = random;
    this.limit = Arrays.copyOf(limit, limit.length);
}

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.Reverse.java

/**
 * Reverses a vector in place//from  ww w .  j  a  va  2 s .  c o m
 * @param v1 the vector to be reversed
 * @return r a reversed copy of v1
 */
public static float[] stateless(float[] v1) {
    Validate.notNull(v1);
    float[] r = Arrays.copyOf(v1, v1.length);
    inPlace(r);
    return r;
}

From source file:com.frame.Conf.Utilidades.java

/**
 * //from w w  w . j  a  v  a2  s  .  com
 * @param keypass Es la llave plublica para enctriptar el texto
 * @param texto Texto a encriptar
 * @return retorna el hash del texto encriptado
 * @throws Exception 
 */
public String Encriptar(String keypass, String texto) throws Exception {

    String secretKey = keypass; //llave para encriptar datos
    String base64EncryptedString = "";

    try {

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        throw new Exception(ex.getMessage());
    }
    return base64EncryptedString;
}

From source file:Conexion.newClass.java

public String Decode(String textoEncriptado) throws Exception {

    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*from w  ww  .  ja  v  a2  s  .c o m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:de.tuberlin.uebb.jbop.access.ClassDescriptor.java

/**
 * Instantiates a new class descriptor./*w  ww .j  av  a 2s  . c o  m*/
 * 
 * @param name
 *          the name
 * @param classData
 *          the class data
 * @param file
 *          the file
 */
public ClassDescriptor(final String name, final byte[] classData, final String file) {
    Validate.notBlank(name);
    Validate.notNull(classData);
    Validate.isTrue(classData.length != 0);
    Validate.notBlank(file);
    this.name = name;
    this.classData = Arrays.copyOf(classData, classData.length);
    this.file = file;
}

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.Sort.java

/**
 * Sorts values statelessly in order given by enumeration
 * @param v1 the values to sort (a native backed array)
 * @param d the direction in which the sorted array should be returned (based on {@link direction})
 * @return tmp the sorted values//from  w w w  .j  a  v a2  s  .  co  m
 */
public static int[] stateless(int[] v1, direction d) {
    Validate.notNull(v1);
    int[] tmp = Arrays.copyOf(v1, v1.length);
    Arrays.sort(tmp);
    switch (d) {
    case ascend:
        break;
    case descend:
        Reverse.inPlace(tmp);
    }
    return tmp;
}

From source file:com.softenido.cafedark.io.virtual.VirtualFile.java

public VirtualFile(VirtualFile parent, String child) {
    String[] parentItems = parent.splitPath();
    String[] items = Arrays.copyOf(parentItems, parentItems.length + 1);
    items[parentItems.length] = child;//from  w  w w  .jav a  2 s .c  o  m
    this.fs = new ZipVirtualFileSystem(items);
}