Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

In this page you can find the example usage for java.security SecureRandom SecureRandom.

Prototype

public SecureRandom() 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:fr.paris.lutece.portal.web.admin.AdminPageJspBeanTest.java

private AdminUser getAdminUser() {
    String strRoleKey = "ROLE_" + new BigInteger(40, new SecureRandom()).toString(32);
    RBAC rbac = new RBAC();
    rbac.setResourceTypeKey(Page.RESOURCE_TYPE);
    rbac.setPermissionKey(PageResourceIdService.PERMISSION_MANAGE);
    rbac.setResourceId(RBAC.WILDCARD_RESOURCES_ID);
    rbac.setRoleKey(strRoleKey);/*  w  ww  .  j av  a 2 s  . co  m*/
    RBACHome.create(rbac);
    AdminRole role = new AdminRole();
    role.setKey(strRoleKey);
    role.setDescription(strRoleKey);
    AdminUser user = new AdminUser();
    Map<String, AdminRole> roles = new HashMap<>();
    roles.put(strRoleKey, role);
    user.setRoles(roles);
    return user;
}

From source file:plbtw.klmpk.barang.hilang.controller.DeveloperController.java

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public CustomResponseMessage addDeveloper(@RequestBody DeveloperRequest developerRequest) {
    try {//from  w  w w .j ava2s . c  om

        if (developerService.checkDeveloperExist(developerRequest.getEmail()) != null) {
            return new CustomResponseMessage(HttpStatus.METHOD_NOT_ALLOWED, "Email Already Used");
        }

        Developer developer = new Developer();
        developer.setRole(roleService.getRole(developerRequest.getIdrole()));
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[20];
        random.nextBytes(bytes);
        String token = bytes.toString();
        developer.setToken(token);
        developer.setSecretKey(token);
        developer.setEmail(developerRequest.getEmail());
        developer.setPassword(developerRequest.getPassword());
        developerService.addDeveloper(developer);
        List<Developer> result = new ArrayList<>();
        result.add(developer);
        return new CustomResponseMessage(HttpStatus.CREATED, "Developer Has Been Created", result);
    } catch (Exception ex) {
        return new CustomResponseMessage(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage());
    }
}

From source file:com.cloudant.sync.datastore.AttachmentStreamFactory.java

/**
 * Get stream for writing attachment data to disk.
 *
 * Opens the output stream using {@see FileUtils#openOutputStream(File)}.
 *
 * Data should be written to the stream unencoded, unencrypted.
 *
 * @param file File to write to.// w ww  .  ja va2 s  .  c  o m
 * @param encoding Encoding to use.
 * @return Stream for writing.
 * @throws IOException if there's a problem writing to disk, including issues with
 *      encryption (bad key length and other key issues).
 */
public OutputStream getOutputStream(File file, Attachment.Encoding encoding) throws IOException {

    // First, open a stream to the raw bytes on disk.
    // Then, if we have a key assume the file should be encrypted before writing,
    // so wrap the file stream in a stream which encrypts during writing.
    // If the attachment needs encoding, we need to encode the data before it
    // is encrypted, so wrap a stream which will encode (gzip) before passing
    // to encryption stream, or directly to file stream if not encrypting.
    //
    //  User writes [-> Encoding Stream] [-> Encryption Stream] -> write to disk

    OutputStream os = FileUtils.openOutputStream(file);

    if (key != null) {

        try {

            // Create IV
            byte[] iv = new byte[16];
            new SecureRandom().nextBytes(iv);

            os = new EncryptedAttachmentOutputStream(os, key, iv);

        } catch (InvalidKeyException ex) {
            // Replace with an IOException as we validate the key when opening
            // the databases and the key should be the same -- it's therefore
            // not worth forcing the developer to catch something they can't
            // fix during file read; generic IOException works better.
            throw new IOException("Bad key used to write file; check encryption key.", ex);
        } catch (InvalidAlgorithmParameterException ex) {
            // We are creating what should be a valid IV for AES, 16-bytes.
            // Therefore this shouldn't happen. Again, the developer cannot
            // fix it, so wrap in an IOException as we can't write the file.
            throw new IOException("Bad key used to write file; check encryption key.", ex);
        }

    }

    switch (encoding) {
    case Plain:
        break; // nothing to do
    case Gzip:
        os = new GZIPOutputStream(os);
    }

    return os;
}