Search in sources :

Example 6 with Cryptor

use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.

the class CryptoFileSystemModule method provideCryptor.

@Provides
@PerFileSystem
public Cryptor provideCryptor(CryptorProvider cryptorProvider, @PathToVault Path pathToVault, CryptoFileSystemProperties properties) {
    return rethrowUnchecked(IOException.class).from(() -> {
        Path masterKeyPath = pathToVault.resolve(properties.masterkeyFilename());
        Path backupKeyPath = pathToVault.resolve(properties.masterkeyFilename() + Constants.MASTERKEY_BACKUP_SUFFIX);
        // since 1.3.0 a file system can only be created for existing vaults. initialization is done before.
        assert Files.exists(masterKeyPath);
        byte[] keyFileContents = Files.readAllBytes(masterKeyPath);
        Cryptor cryptor = cryptorProvider.createFromKeyFile(KeyFile.parse(keyFileContents), properties.passphrase(), properties.pepper(), Constants.VAULT_VERSION);
        Files.copy(masterKeyPath, backupKeyPath, REPLACE_EXISTING);
        return cryptor;
    });
}
Also used : Path(java.nio.file.Path) Cryptor(org.cryptomator.cryptolib.api.Cryptor) IOException(java.io.IOException) Provides(dagger.Provides)

Example 7 with Cryptor

use of org.cryptomator.cryptolib.api.Cryptor in project cryptofs by cryptomator.

the class CryptoFileSystemProvider method initialize.

/**
 * Creates a new vault at the given directory path.
 *
 * @param pathToVault Path to a not yet existing directory
 * @param masterkeyFilename Name of the masterkey file
 * @param pepper Application-specific pepper used during key derivation
 * @param passphrase Passphrase that should be used to unlock the vault
 * @throws NotDirectoryException If the given path is not an existing directory.
 * @throws IOException If the vault structure could not be initialized due to I/O errors
 * @since 1.3.2
 */
public static void initialize(Path pathToVault, String masterkeyFilename, byte[] pepper, CharSequence passphrase) throws NotDirectoryException, IOException {
    if (!Files.isDirectory(pathToVault)) {
        throw new NotDirectoryException(pathToVault.toString());
    }
    try (Cryptor cryptor = CRYPTOR_PROVIDER.createNew()) {
        // save masterkey file:
        Path masterKeyPath = pathToVault.resolve(masterkeyFilename);
        byte[] keyFileContents = cryptor.writeKeysToMasterkeyFile(Normalizer.normalize(passphrase, Form.NFC), pepper, Constants.VAULT_VERSION).serialize();
        Files.write(masterKeyPath, keyFileContents, CREATE_NEW, WRITE);
        // create "d/RO/OTDIRECTORY":
        String rootDirHash = cryptor.fileNameCryptor().hashDirectoryId(Constants.ROOT_DIR_ID);
        Path rootDirPath = pathToVault.resolve(Constants.DATA_DIR_NAME).resolve(rootDirHash.substring(0, 2)).resolve(rootDirHash.substring(2));
        Files.createDirectories(rootDirPath);
        // create "m":
        Files.createDirectory(pathToVault.resolve(Constants.METADATA_DIR_NAME));
    }
    assert containsVault(pathToVault, masterkeyFilename);
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) Cryptor(org.cryptomator.cryptolib.api.Cryptor)

Aggregations

Cryptor (org.cryptomator.cryptolib.api.Cryptor)7 Path (java.nio.file.Path)6 IOException (java.io.IOException)2 FileSystem (java.nio.file.FileSystem)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 FileSystemProvider (java.nio.file.spi.FileSystemProvider)2 KeyFile (org.cryptomator.cryptolib.api.KeyFile)2 Before (org.junit.Before)2 Provides (dagger.Provides)1 NotDirectoryException (java.nio.file.NotDirectoryException)1 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)1 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)1 Migrator (org.cryptomator.cryptofs.migration.api.Migrator)1 FileContentCryptor (org.cryptomator.cryptolib.api.FileContentCryptor)1 FileHeaderCryptor (org.cryptomator.cryptolib.api.FileHeaderCryptor)1 InvalidPassphraseException (org.cryptomator.cryptolib.api.InvalidPassphraseException)1 UnsupportedVaultFormatException (org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)1 Test (org.junit.Test)1