Package com.mku.salmon
Class SalmonFile
java.lang.Object
com.mku.salmon.SalmonFile
- All Implemented Interfaces:
IVirtualFile
A virtual file backed by an encrypted
IRealFile
on the real filesystem.
Supports operations for retrieving SalmonStream
for reading/decrypting
and writing/encrypting contents.-
Field Summary
-
Constructor Summary
ConstructorDescriptionSalmonFile
(IRealFile realFile) Provides a file handle that can be used to create encrypted files.SalmonFile
(IRealFile realFile, SalmonDrive drive) Provides a file handle that can be used to create encrypted files. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
autoRename
(SalmonFile file) copy
(IVirtualFile dir, BiConsumer<Long, Long> OnProgressListener) Copy a file to another directory.void
copyRecursively
(IVirtualFile dest, TriConsumer<IVirtualFile, Long, Long> progressListener, Function<IVirtualFile, String> autoRename, boolean autoRenameFolders, BiConsumer<IVirtualFile, Exception> onFailed) 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 realFilename) Create a file under this directorycreateFile
(String realFilename, byte[] key, byte[] fileNameNonce, byte[] fileNonce) Create a file under this directoryvoid
delete()
Delete this file.void
deleteRecursively
(TriConsumer<IVirtualFile, Long, Long> progressListener, BiConsumer<IVirtualFile, Exception> onFailed) boolean
exists()
Returns true if this file existsReturns the basename for the fileint
Return the AES block size for encryption / decryptionGet a child with this filename.int
Get the count of files and subdirectoriesprotected String
getDecryptedFilename
(String filename, byte[] key, byte[] nonce) Return the decrypted filename of a real filenamegetDrive()
Get the drive this file belongs to.protected String
getEncryptedFilename
(String filename, byte[] key, byte[] nonce) Return the encrypted filename of a virtual filenamebyte[]
Returns the current encryption keyGet the file chunk size from the header.byte[]
Returns the initial vector that is used for encryption / decryptionGet the customSalmonHeader
from this file.Retrieves a SalmonStream that will be used for decrypting the file contents.long
Returns the last date modified in millisecondslong
Returns the minimum part size that can be encrypted / decrypted in parallel aligning to the integrity chunk size if available.Get aSalmonStream
for encrypting/writing contents using the nonce in the header.Returns the virtual parent directorygetPath()
Return the path of the real file storedReturn the real fileReturn the path of the real fileReturn the current chunk size requested that will be used for integritybyte[]
Get the nonce that is used for encryption/decryption of this file.long
getSize()
Return the virtual size of the file excluding the header and hash signatures.getTag()
Get the file tag.boolean
Returns True if this is a directoryboolean
isFile()
Returns true if this is a fileLists files and directories under this directoryvoid
mkdir()
Create this directory.move
(IVirtualFile dir, BiConsumer<Long, Long> OnProgressListener) Move file to another directory.void
moveRecursively
(IVirtualFile dest, TriConsumer<IVirtualFile, Long, Long> progressListener, Function<IVirtualFile, String> autoRename, boolean autoRenameFolders, BiConsumer<IVirtualFile, Exception> onFailed) Move a directory recursivelyvoid
Rename the virtual file namevoid
Rename the virtual file namevoid
setAllowOverwrite
(boolean value) Warning! Allow overwriting on a current stream.void
setApplyIntegrity
(boolean integrity, byte[] hashKey, Integer requestChunkSize) Enable integrity with this file.void
setEncryptionKey
(byte[] encryptionKey) Sets the encryption keyvoid
setRequestedNonce
(byte[] nonce) Set the nonce for encryption/decryption for this file.void
Set the tag for this file.void
setVerifyIntegrity
(boolean integrity, byte[] hashKey) Enabled verification of file integrity during read() and write()
-
Field Details
-
separator
- See Also:
-
autoRename
-
-
Constructor Details
-
SalmonFile
Provides a file handle that can be used to create encrypted files. Requires a virtual drive that supports the underlying filesystem, see JavaFile implementation.- Parameters:
realFile
- The real file
-
SalmonFile
Provides a file handle that can be used to create encrypted files. Requires a virtual drive that supports the underlying filesystem, see JavaFile implementation.- Parameters:
drive
- The file virtual system that will be used with file operationsrealFile
- The real file
-
-
Method Details
-
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 customSalmonHeader
from this file.- Returns:
- The header
- Throws:
IOException
- Thrown if there is an IO error.
-
getInputStream
Retrieves a SalmonStream that will be used for decrypting the file contents.- Specified by:
getInputStream
in interfaceIVirtualFile
- Returns:
- The input stream
- Throws:
IOException
- Thrown if there is an IO error.SalmonSecurityException
- Thrown if there is a security exceptionIntegrityException
- Thrown if the data are corrupt or tampered with.
-
getOutputStream
Get aSalmonStream
for encrypting/writing contents using the nonce in the header.- Specified by:
getOutputStream
in interfaceIVirtualFile
- Returns:
- The output stream
- Throws:
SalmonSecurityException
- 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
-
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
- True if enable integrity verificationhashKey
- The hash key to be used for verification- Throws:
IOException
- Thrown if there is an IO error.
-
setApplyIntegrity
public void setApplyIntegrity(boolean integrity, byte[] hashKey, Integer 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:
SalmonSecurityException
- 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:
listFiles
in interfaceIVirtualFile
-
getChild
Get a child with this filename.- Specified by:
getChild
in interfaceIVirtualFile
- Parameters:
filename
- The filename to search for- Returns:
- The child file
- Throws:
SalmonSecurityException
- Thrown if there is a security exceptionIntegrityException
- Thrown if the data are corrupt or tampered with.IOException
- Thrown if there is an IO error.SalmonAuthException
- Thrown if there is an Authorization error
-
createDirectory
Creates a directory under this directory- Specified by:
createDirectory
in interfaceIVirtualFile
- Parameters:
dirName
- The name of the directory to be created- Throws:
IOException
-
createDirectory
public SalmonFile createDirectory(String dirName, byte[] key, byte[] dirNameNonce) throws IOException 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:
getRealFile
in interfaceIVirtualFile
-
isFile
public boolean isFile()Returns true if this is a file- Specified by:
isFile
in interfaceIVirtualFile
-
isDirectory
public boolean isDirectory()Returns True if this is a directory- Specified by:
isDirectory
in interfaceIVirtualFile
-
getPath
Return the path of the real file stored- Specified by:
getPath
in interfaceIVirtualFile
- Throws:
IOException
-
getRealPath
Return the path of the real file- Specified by:
getRealPath
in interfaceIVirtualFile
-
getBaseName
Returns the basename for the file- Specified by:
getBaseName
in interfaceIVirtualFile
- Throws:
IOException
-
getParent
Returns the virtual parent directory- Specified by:
getParent
in interfaceIVirtualFile
-
delete
public void delete()Delete this file.- Specified by:
delete
in interfaceIVirtualFile
-
mkdir
public void mkdir()Create this directory. Currently Not Supported- Specified by:
mkdir
in interfaceIVirtualFile
-
getLastDateTimeModified
public long getLastDateTimeModified()Returns the last date modified in milliseconds- Specified by:
getLastDateTimeModified
in interfaceIVirtualFile
-
getSize
Return the virtual size of the file excluding the header and hash signatures.- Specified by:
getSize
in interfaceIVirtualFile
- Throws:
IOException
-
createFile
Create a file under this directory- Specified by:
createFile
in interfaceIVirtualFile
- Parameters:
realFilename
- The real file name of the file (encrypted)- Throws:
IOException
-
createFile
public SalmonFile createFile(String realFilename, byte[] key, byte[] fileNameNonce, byte[] fileNonce) throws IOException Create a file under this directory- Parameters:
realFilename
- The real file name of the file (encrypted)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:
rename
in interfaceIVirtualFile
- Parameters:
newFilename
- The new filename this file will be renamed to- Throws:
IOException
-
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:
exists
in interfaceIVirtualFile
-
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
public SalmonFile move(IVirtualFile dir, BiConsumer<Long, Long> OnProgressListener) throws IOExceptionMove file to another directory.- Specified by:
move
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.OnProgressListener
- Observer to notify when move progress changes.- Returns:
- The file
- Throws:
IOException
- Thrown if there is an IO error.
-
copy
public SalmonFile copy(IVirtualFile dir, BiConsumer<Long, Long> OnProgressListener) throws IOExceptionCopy a file to another directory.- Specified by:
copy
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.OnProgressListener
- Observer to notify when copy progress changes.- Returns:
- The file
- Throws:
IOException
- Thrown if there is an IO error.
-
copyRecursively
public void copyRecursively(IVirtualFile dest, TriConsumer<IVirtualFile, Long, throws IOExceptionLong> progressListener, Function<IVirtualFile, String> autoRename, boolean autoRenameFolders, BiConsumer<IVirtualFile, Exception> onFailed) Copy a directory recursively- Specified by:
copyRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directoryprogressListener
- The progress listenerautoRename
- The autorename functiononFailed
- The callback when file copying has failed- Throws:
IOException
-
moveRecursively
public void moveRecursively(IVirtualFile dest, TriConsumer<IVirtualFile, Long, throws IOExceptionLong> progressListener, Function<IVirtualFile, String> autoRename, boolean autoRenameFolders, BiConsumer<IVirtualFile, Exception> onFailed) Move a directory recursively- Specified by:
moveRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directoryprogressListener
- The progress listenerautoRename
- The autorename functiononFailed
- Callback when move fails- Throws:
IOException
-
deleteRecursively
public void deleteRecursively(TriConsumer<IVirtualFile, Long, Long> progressListener, BiConsumer<IVirtualFile, Exception> onFailed) - Specified by:
deleteRecursively
in interfaceIVirtualFile
-
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
- Throws:
Exception
-