Package com.mku.salmonfs.file
Class AesFile
java.lang.Object
com.mku.salmonfs.file.AesFile
- All Implemented Interfaces:
IVirtualFile
An
IVirtualFile implementation of an encryption wrapper backed by a real IFile.
You can use this file to browse a VirtualDrive.
Encrypt/Decrypt operations are accessible via getInputStream/getOutputStream.-
Nested Class Summary
Nested classes/interfaces inherited from interface com.mku.fs.file.IVirtualFile
IVirtualFile.VirtualRecursiveCopyOptions, IVirtualFile.VirtualRecursiveDeleteOptions, IVirtualFile.VirtualRecursiveMoveOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic Function<IVirtualFile,String> Provides an alternative file name.static final StringDirectory separator. -
Constructor Summary
ConstructorsConstructorDescriptionFile wrapper to be used to create, read, and write encrypted files.AesFile(IFile realFile, EncryptionFormat format) File wrapper to be used to create, read, and write encrypted files.File wrapper to create, read, and write encrypted files. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringautoRename(AesFile file) Provides an alternative file name for an AesFile.copy(IVirtualFile dir) Copy file to another directory.copy(IVirtualFile dir, IFile.CopyOptions options) Copy file to another directory.voidcopyRecursively(IVirtualFile dest) Copy a directory recursivelyvoidcopyRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveCopyOptions options) Copy a directory recursivelycreateDirectory(String dirName) Creates a directory under this directorycreateDirectory(String dirName, byte[] key, byte[] dirNameNonce) Creates a directory under this directorycreateFile(String filename) Create a file under this directorycreateFile(String filename, byte[] key, byte[] fileNameNonce, byte[] fileNonce) Create a file under this directoryvoiddelete()Delete this file.voidDelete all subdirectories and files.voidDelete all subdirectories and files.booleanexists()Returns true if this file existsintReturn the AES block size for encryption / decryptionGet a child with this filename.intGet the count of files and subdirectoriesprotected StringgetDecryptedFilename(String filename, byte[] key, byte[] nonce) Return the decrypted filename of a real filenamegetDrive()Get the drive this file belongs to.protected StringgetEncryptedFilename(String filename, byte[] key, byte[] nonce) Return the encrypted filename of a virtual filenamebyte[]Returns the current encryption keyintGet the file chunk size from the header.byte[]Returns the initial vector that is used for encryption / decryptionGet the customHeaderfrom this file.Retrieves a AesStream that will be used for reading/decrypting the file contents.longReturns the last date modified in milliseconds.longReturn the virtual size of the file excluding the header and hash signatures.longReturns the minimum part size that can be encrypted / decrypted in parallel aligning to the integrity chunk size if available.getName()Returns the virtual name of the fileGet aAesStreamfor encrypting/writing contents using the nonce in the header.getOutputStream(byte[] nonce) Get aAesStreamfor encrypting/writing contents to this file.Returns the virtual parent directory.getPath()Return the virtual path for this file.Return the real fileReturn the path of the real file.intReturn the current chunk size requested that will be used for integritybyte[]Get the nonce that is used for encryption/decryption of this file.getTag()Get the file tag.booleanCheck if this is a directory.booleanisFile()Check if this is a file.booleanReturn if integrity is setAesFile[]Lists files and directories under this directoryvoidmkdir()Create this directory.move(IVirtualFile dir) Move file to another directory.move(IVirtualFile dir, IFile.MoveOptions options) Move file to another directory.voidmoveRecursively(IVirtualFile dest) Move a directory recursivelyvoidmoveRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveMoveOptions options) Move a directory recursivelyvoidRename the virtual file namevoidRename the virtual file namevoidsetAllowOverwrite(boolean value) Warning! Allow overwriting on a current stream.voidsetApplyIntegrity(boolean integrity) Enable integrity with this file.voidsetApplyIntegrity(boolean integrity, byte[] hashKey) Enable integrity with this file.voidsetApplyIntegrity(boolean integrity, byte[] hashKey, int requestChunkSize) Enable integrity with this file.voidsetEncryptionKey(byte[] encryptionKey) Sets the encryption keyvoidsetRequestedNonce(byte[] nonce) Set the nonce for encryption/decryption for this file.voidSet the tag for this file.voidsetVerifyIntegrity(boolean integrity) Enabled verification of file integrity during read() and write()voidsetVerifyIntegrity(boolean integrity, byte[] hashKey) Enabled verification of file integrity during read() and write()
-
Field Details
-
Separator
Directory separator.- See Also:
-
autoRename
Provides an alternative file name. Use this to rename files.
-
-
Constructor Details
-
AesFile
File wrapper to be used to create, read, and write encrypted files.- Parameters:
realFile- The real file
-
AesFile
File wrapper to be used to create, read, and write encrypted files.- Parameters:
realFile- The real fileformat- The format to use, seeEncryptionFormat
-
AesFile
File wrapper to create, read, and write encrypted files. Requires a virtual drive that supports the underlying filesystem, see JavaFile implementation.- Parameters:
realFile- The real filedrive- The file virtual system that will be used with file operations
-
-
Method Details
-
isIntegrityEnabled
public boolean isIntegrityEnabled()Return if integrity is set- Returns:
- True if integrity is enabled.
-
getRequestedChunkSize
public int getRequestedChunkSize()Return the current chunk size requested that will be used for integrity- Returns:
- The requested chunk size
-
getFileChunkSize
Get the file chunk size from the header.- Returns:
- The chunk size.
- Throws:
IOException- Throws exceptions if the format is corrupt.
-
getHeader
Get the customHeaderfrom this file.- Returns:
- The header
- Throws:
IOException- Thrown if there is an IO error.
-
getInputStream
Retrieves a AesStream that will be used for reading/decrypting the file contents.- Specified by:
getInputStreamin interfaceIVirtualFile- Returns:
- The input stream
- Throws:
IOException- Thrown if there is an IO error.SecurityException- Thrown if there is a security exceptionIntegrityException- Thrown if the data are corrupt or tampered with.
-
getOutputStream
Get aAesStreamfor encrypting/writing contents using the nonce in the header.- Specified by:
getOutputStreamin interfaceIVirtualFile- Returns:
- The output stream
- Throws:
SecurityException- Thrown if there is a security exceptionIntegrityException- Thrown if the data are corrupt or tampered with.SequenceException- Thrown if there is an error with the nonce sequenceIOException- Thrown if there is a problem with the stream.
-
getOutputStream
Get aAesStreamfor encrypting/writing contents to this file.- Parameters:
nonce- Nonce to be used for encryption. Note that each file should have a unique nonce seeAesDrive.getNextNonce().- Returns:
- The output stream.
- Throws:
SecurityException- If overwriting existing files. See SetAllowOverwrite().IOException- If there is a problem creating the stream.
-
getEncryptionKey
public byte[] getEncryptionKey()Returns the current encryption key- Returns:
- The encryption key
-
setEncryptionKey
public void setEncryptionKey(byte[] encryptionKey) Sets the encryption key- Parameters:
encryptionKey- The AES encryption key to be used
-
setVerifyIntegrity
Enabled verification of file integrity during read() and write()- Parameters:
integrity- False to disable integrity verification- Throws:
IOException- Thrown if there is an IO error.
-
setVerifyIntegrity
Enabled verification of file integrity during read() and write()- Parameters:
integrity- True to enable integrity verificationhashKey- The hash key to be used for verification- Throws:
IOException- Thrown if there is an IO error.
-
setApplyIntegrity
Enable integrity with this file.- Parameters:
integrity- False to disable integrity- Throws:
IOException- Thrown if there is an IO error.
-
setApplyIntegrity
Enable integrity with this file.- Parameters:
integrity- True to enable integrityhashKey- The hash key to use- Throws:
IOException- Thrown if there is an IO error.
-
setApplyIntegrity
public void setApplyIntegrity(boolean integrity, byte[] hashKey, int requestChunkSize) throws IOException Enable integrity with this file.- Parameters:
integrity- True to enable integrityhashKey- The hash key to userequestChunkSize- 0 use default file chunk. A positive number to specify integrity chunks.- Throws:
IOException- Thrown if there is an IO error.
-
setAllowOverwrite
public void setAllowOverwrite(boolean value) Warning! Allow overwriting on a current stream. Overwriting is not a good idea because it will re-use the same IV. This is not recommended if you use the stream on storing files or generally data if prior version can be inspected by others. You should only use this setting for initial encryption with parallel streams and not for overwriting!- Parameters:
value- True to allow overwriting operations
-
getFileNonce
Returns the initial vector that is used for encryption / decryption- Returns:
- The file nonce
- Throws:
IOException- Thrown if there is an IO error.
-
setRequestedNonce
public void setRequestedNonce(byte[] nonce) Set the nonce for encryption/decryption for this file.- Parameters:
nonce- Nonce to be used.- Throws:
SecurityException- Thrown if there is a security exception
-
getRequestedNonce
public byte[] getRequestedNonce()Get the nonce that is used for encryption/decryption of this file.- Returns:
- The requested nonce
-
getBlockSize
public int getBlockSize()Return the AES block size for encryption / decryption- Returns:
- The block size
-
getChildrenCount
public int getChildrenCount()Get the count of files and subdirectories- Returns:
- The children count
-
listFiles
Lists files and directories under this directory- Specified by:
listFilesin interfaceIVirtualFile- Returns:
- An array of files and subdirectories.
-
getChild
Get a child with this filename.- Specified by:
getChildin interfaceIVirtualFile- Parameters:
filename- The filename to search for- Returns:
- The child file
- Throws:
SecurityException- Thrown if there is a security exceptionIntegrityException- Thrown if the data are corrupt or tampered with.IOException- Thrown if there is an IO error.AuthException- Thrown if there is an Authorization error
-
createDirectory
Creates a directory under this directory- Specified by:
createDirectoryin interfaceIVirtualFile- Parameters:
dirName- The name of the directory to be created- Returns:
- The new directory.
- Throws:
IOException- if there was a problem with the stream
-
createDirectory
Creates a directory under this directory- Parameters:
dirName- The name of the directory to be createdkey- The key that will be used to encrypt the directory namedirNameNonce- The nonce to be used for encrypting the directory name- Returns:
- The directory that was created
- Throws:
IOException- Thrown when error during IO
-
getRealFile
Return the real file- Specified by:
getRealFilein interfaceIVirtualFile- Returns:
- The real file
-
isFile
public boolean isFile()Check if this is a file.- Specified by:
isFilein interfaceIVirtualFile- Returns:
- True if it is a file.
-
isDirectory
public boolean isDirectory()Check if this is a directory.- Specified by:
isDirectoryin interfaceIVirtualFile- Returns:
- True if it is a directory.
-
getPath
Return the virtual path for this file.- Specified by:
getPathin interfaceIVirtualFile- Returns:
- The virtual path.
- Throws:
IOException- Thrown if there is an IO error.
-
getRealPath
Return the path of the real file.- Specified by:
getRealPathin interfaceIVirtualFile- Returns:
- The real path.
-
getName
Returns the virtual name of the file- Specified by:
getNamein interfaceIVirtualFile- Returns:
- The file name
- Throws:
IOException- if there was a problem with the stream
-
getParent
Returns the virtual parent directory.- Specified by:
getParentin interfaceIVirtualFile- Returns:
- The parent directory.
-
delete
public void delete()Delete this file.- Specified by:
deletein interfaceIVirtualFile
-
mkdir
public void mkdir()Create this directory. Currently Not Supported- Specified by:
mkdirin interfaceIVirtualFile
-
getLastDateModified
public long getLastDateModified()Returns the last date modified in milliseconds.- Specified by:
getLastDateModifiedin interfaceIVirtualFile- Returns:
- The last date modified in milliseconds.
-
getLength
Return the virtual size of the file excluding the header and hash signatures.- Specified by:
getLengthin interfaceIVirtualFile- Returns:
- The size in bytes.
- Throws:
IOException- if there was a problem with the stream
-
createFile
Create a file under this directory- Specified by:
createFilein interfaceIVirtualFile- Parameters:
filename- The file name of the file.- Returns:
- The new file.
- Throws:
IOException- if there was a problem with the stream
-
createFile
public AesFile createFile(String filename, byte[] key, byte[] fileNameNonce, byte[] fileNonce) throws IOException Create a file under this directory- Parameters:
filename- The file name.key- The key that will be used for encryptionfileNameNonce- The nonce for the encrypting the filenamefileNonce- The nonce for the encrypting the file contents- Returns:
- The file
- Throws:
IOException- Thrown if there is an IO error.
-
rename
Rename the virtual file name- Specified by:
renamein interfaceIVirtualFile- Parameters:
newFilename- The new filename this file will be renamed to- Throws:
IOException- if there was a problem with the stream
-
rename
Rename the virtual file name- Parameters:
newFilename- The new filename this file will be renamed tononce- The nonce to use- Throws:
IOException- Thrown when error during IO
-
exists
public boolean exists()Returns true if this file exists- Specified by:
existsin interfaceIVirtualFile- Returns:
- True if exists.
-
getDecryptedFilename
Return the decrypted filename of a real filename- Parameters:
filename- The filename of a real filekey- The encryption key if the file doesn't belong to a drivenonce- The nonce if the file doesn't belong to a drive- Returns:
- The decrypted filename
- Throws:
IOException- Thrown if there is an IO error.
-
getEncryptedFilename
Return the encrypted filename of a virtual filename- Parameters:
filename- The virtual filenamekey- The encryption key if the file doesn't belong to a drivenonce- The nonce if the file doesn't belong to a drive- Returns:
- The encrypted file name
- Throws:
IOException- Thrown if there is an IO error.
-
getDrive
Get the drive this file belongs to.- Returns:
- The drive
-
setTag
Set the tag for this file.- Parameters:
tag- The file tag
-
getTag
Get the file tag.- Returns:
- The file tag.
-
move
Move file to another directory.- Specified by:
movein interfaceIVirtualFile- Parameters:
dir- Target directory.- Returns:
- The file
- Throws:
IOException- Thrown if there is an IO error.
-
move
Move file to another directory.- Specified by:
movein interfaceIVirtualFile- Parameters:
dir- Target directory.options- The Options- Returns:
- The file
- Throws:
IOException- Thrown if there is an IO error.
-
copy
Copy file to another directory.- Specified by:
copyin interfaceIVirtualFile- Parameters:
dir- Target directory.- Returns:
- The file
- Throws:
IOException- Thrown if there is an IO error.
-
copy
Copy file to another directory.- Specified by:
copyin interfaceIVirtualFile- Parameters:
dir- Target directory.options- The options- Returns:
- The file
- Throws:
IOException- Thrown if there is an IO error.
-
copyRecursively
Copy a directory recursively- Specified by:
copyRecursivelyin interfaceIVirtualFile- Parameters:
dest- The destination directory- Throws:
IOException- Thrown if there is an IO error.
-
copyRecursively
public void copyRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveCopyOptions options) throws IOException Copy a directory recursively- Specified by:
copyRecursivelyin interfaceIVirtualFile- Parameters:
dest- The destination directoryoptions- The options- Throws:
IOException- Thrown if there is an IO error.
-
moveRecursively
Move a directory recursively- Specified by:
moveRecursivelyin interfaceIVirtualFile- Parameters:
dest- The destination directory- Throws:
IOException- Thrown if there is an IO error.
-
moveRecursively
public void moveRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveMoveOptions options) throws IOException Move a directory recursively- Specified by:
moveRecursivelyin interfaceIVirtualFile- Parameters:
dest- The destination directoryoptions- The options- Throws:
IOException- Thrown if there is an IO error.
-
deleteRecursively
public void deleteRecursively()Delete all subdirectories and files.- Specified by:
deleteRecursivelyin interfaceIVirtualFile
-
deleteRecursively
Delete all subdirectories and files.- Specified by:
deleteRecursivelyin interfaceIVirtualFile- Parameters:
options- The options
-
getMinimumPartSize
Returns the minimum part size that can be encrypted / decrypted in parallel aligning to the integrity chunk size if available.- Returns:
- The minimum part size
- Throws:
IOException- Thrown if there is an IO error.
-
autoRename
Provides an alternative file name for an AesFile. Use this to rename files.- Parameters:
file- The file- Returns:
- The new file name
- Throws:
Exception- If a problem occurs
-