Package com.mku.salmonfs.file
Class AesFile
- java.lang.Object
-
- com.mku.salmonfs.file.AesFile
-
- All Implemented Interfaces:
IVirtualFile
public class AesFile extends java.lang.Object implements IVirtualFile
AnIVirtualFile
implementation of an encryption wrapper backed by a realIFile
. 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
Fields Modifier and Type Field Description static Function<IVirtualFile,java.lang.String>
autoRename
Provides an alternative file name.static java.lang.String
Separator
Directory separator.
-
Constructor Summary
Constructors Constructor Description AesFile(IFile realFile)
File 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.AesFile(IFile realFile, AesDrive drive)
File wrapper to create, read, and write encrypted files.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
autoRename(AesFile file)
Provides an alternative file name for an AesFile.AesFile
copy(IVirtualFile dir)
Copy file to another directory.AesFile
copy(IVirtualFile dir, IFile.CopyOptions options)
Copy file to another directory.void
copyRecursively(IVirtualFile dest)
Copy a directory recursivelyvoid
copyRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveCopyOptions options)
Copy a directory recursivelyAesFile
createDirectory(java.lang.String dirName)
Creates a directory under this directoryAesFile
createDirectory(java.lang.String dirName, byte[] key, byte[] dirNameNonce)
Creates a directory under this directoryAesFile
createFile(java.lang.String filename)
Create a file under this directoryAesFile
createFile(java.lang.String filename, byte[] key, byte[] fileNameNonce, byte[] fileNonce)
Create a file under this directoryvoid
delete()
Delete this file.void
deleteRecursively()
Delete all subdirectories and files.void
deleteRecursively(IVirtualFile.VirtualRecursiveDeleteOptions options)
Delete all subdirectories and files.boolean
exists()
Returns true if this file existsint
getBlockSize()
Return the AES block size for encryption / decryptionAesFile
getChild(java.lang.String filename)
Get a child with this filename.int
getChildrenCount()
Get the count of files and subdirectoriesprotected java.lang.String
getDecryptedFilename(java.lang.String filename, byte[] key, byte[] nonce)
Return the decrypted filename of a real filenameAesDrive
getDrive()
Get the drive this file belongs to.protected java.lang.String
getEncryptedFilename(java.lang.String filename, byte[] key, byte[] nonce)
Return the encrypted filename of a virtual filenamebyte[]
getEncryptionKey()
Returns the current encryption keyint
getFileChunkSize()
Get the file chunk size from the header.byte[]
getFileNonce()
Returns the initial vector that is used for encryption / decryptionHeader
getHeader()
Get the customHeader
from this file.AesStream
getInputStream()
Retrieves a AesStream that will be used for reading/decrypting the file contents.long
getLastDateModified()
Returns the last date modified in milliseconds.long
getLength()
Return the virtual size of the file excluding the header and hash signatures.long
getMinimumPartSize()
Returns the minimum part size that can be encrypted / decrypted in parallel aligning to the integrity chunk size if available.java.lang.String
getName()
Returns the virtual name of the fileRandomAccessStream
getOutputStream()
Get aAesStream
for encrypting/writing contents using the nonce in the header.RandomAccessStream
getOutputStream(byte[] nonce)
Get aAesStream
for encrypting/writing contents to this file.AesFile
getParent()
Returns the virtual parent directory.java.lang.String
getPath()
Return the virtual path for this file.IFile
getRealFile()
Return the real filejava.lang.String
getRealPath()
Return the path of the real file.int
getRequestedChunkSize()
Return the current chunk size requested that will be used for integritybyte[]
getRequestedNonce()
Get the nonce that is used for encryption/decryption of this file.java.lang.Object
getTag()
Get the file tag.boolean
isDirectory()
Check if this is a directory.boolean
isFile()
Check if this is a file.boolean
isIntegrityEnabled()
Return if integrity is setAesFile[]
listFiles()
Lists files and directories under this directoryvoid
mkdir()
Create this directory.AesFile
move(IVirtualFile dir)
Move file to another directory.AesFile
move(IVirtualFile dir, IFile.MoveOptions options)
Move file to another directory.void
moveRecursively(IVirtualFile dest)
Move a directory recursivelyvoid
moveRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveMoveOptions options)
Move a directory recursivelyvoid
rename(java.lang.String newFilename)
Rename the virtual file namevoid
rename(java.lang.String newFilename, byte[] nonce)
Rename the virtual file namevoid
setAllowOverwrite(boolean value)
Warning! Allow overwriting on a current stream.void
setApplyIntegrity(boolean integrity)
Enable integrity with this file.void
setApplyIntegrity(boolean integrity, byte[] hashKey)
Enable integrity with this file.void
setApplyIntegrity(boolean integrity, byte[] hashKey, int 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
setTag(java.lang.Object tag)
Set the tag for this file.void
setVerifyIntegrity(boolean integrity)
Enabled verification of file integrity during read() and write()void
setVerifyIntegrity(boolean integrity, byte[] hashKey)
Enabled verification of file integrity during read() and write()
-
-
-
Field Detail
-
Separator
public static final java.lang.String Separator
Directory separator.- See Also:
- Constant Field Values
-
autoRename
public static Function<IVirtualFile,java.lang.String> autoRename
Provides an alternative file name. Use this to rename files.
-
-
Constructor Detail
-
AesFile
public AesFile(IFile realFile)
File wrapper to be used to create, read, and write encrypted files.- Parameters:
realFile
- The real file
-
AesFile
public AesFile(IFile realFile, EncryptionFormat format)
File wrapper to be used to create, read, and write encrypted files.- Parameters:
realFile
- The real fileformat
- The format to use, seeEncryptionFormat
-
AesFile
public AesFile(IFile realFile, AesDrive drive)
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 Detail
-
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
public int getFileChunkSize() throws java.io.IOException
Get the file chunk size from the header.- Returns:
- The chunk size.
- Throws:
java.io.IOException
- Throws exceptions if the format is corrupt.
-
getHeader
public Header getHeader() throws java.io.IOException
Get the customHeader
from this file.- Returns:
- The header
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
getInputStream
public AesStream getInputStream() throws java.io.IOException
Retrieves a AesStream that will be used for reading/decrypting the file contents.- Specified by:
getInputStream
in interfaceIVirtualFile
- Returns:
- The input stream
- Throws:
java.io.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
public RandomAccessStream getOutputStream() throws java.io.IOException
Get aAesStream
for encrypting/writing contents using the nonce in the header.- Specified by:
getOutputStream
in 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 sequencejava.io.IOException
- Thrown if there is a problem with the stream.
-
getOutputStream
public RandomAccessStream getOutputStream(byte[] nonce) throws java.io.IOException
Get aAesStream
for 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().java.io.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
public void setVerifyIntegrity(boolean integrity) throws java.io.IOException
Enabled verification of file integrity during read() and write()- Parameters:
integrity
- False to disable integrity verification- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
setVerifyIntegrity
public void setVerifyIntegrity(boolean integrity, byte[] hashKey) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
setApplyIntegrity
public void setApplyIntegrity(boolean integrity) throws java.io.IOException
Enable integrity with this file.- Parameters:
integrity
- False to disable integrity- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
setApplyIntegrity
public void setApplyIntegrity(boolean integrity, byte[] hashKey) throws java.io.IOException
Enable integrity with this file.- Parameters:
integrity
- True to enable integrityhashKey
- The hash key to use- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
setApplyIntegrity
public void setApplyIntegrity(boolean integrity, byte[] hashKey, int requestChunkSize) throws java.io.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:
java.io.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
public byte[] getFileNonce() throws java.io.IOException
Returns the initial vector that is used for encryption / decryption- Returns:
- The file nonce
- Throws:
java.io.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
public AesFile[] listFiles()
Lists files and directories under this directory- Specified by:
listFiles
in interfaceIVirtualFile
- Returns:
- An array of files and subdirectories.
-
getChild
public AesFile getChild(java.lang.String filename) throws java.io.IOException
Get a child with this filename.- Specified by:
getChild
in 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.java.io.IOException
- Thrown if there is an IO error.AuthException
- Thrown if there is an Authorization error
-
createDirectory
public AesFile createDirectory(java.lang.String dirName) throws java.io.IOException
Creates a directory under this directory- Specified by:
createDirectory
in interfaceIVirtualFile
- Parameters:
dirName
- The name of the directory to be created- Returns:
- The new directory.
- Throws:
java.io.IOException
- if there was a problem with the stream
-
createDirectory
public AesFile createDirectory(java.lang.String dirName, byte[] key, byte[] dirNameNonce) throws java.io.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:
java.io.IOException
- Thrown when error during IO
-
getRealFile
public IFile getRealFile()
Return the real file- Specified by:
getRealFile
in interfaceIVirtualFile
- Returns:
- The real file
-
isFile
public boolean isFile()
Check if this is a file.- Specified by:
isFile
in interfaceIVirtualFile
- Returns:
- True if it is a file.
-
isDirectory
public boolean isDirectory()
Check if this is a directory.- Specified by:
isDirectory
in interfaceIVirtualFile
- Returns:
- True if it is a directory.
-
getPath
public java.lang.String getPath() throws java.io.IOException
Return the virtual path for this file.- Specified by:
getPath
in interfaceIVirtualFile
- Returns:
- The virtual path.
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
getRealPath
public java.lang.String getRealPath()
Return the path of the real file.- Specified by:
getRealPath
in interfaceIVirtualFile
- Returns:
- The real path.
-
getName
public java.lang.String getName() throws java.io.IOException
Returns the virtual name of the file- Specified by:
getName
in interfaceIVirtualFile
- Returns:
- The file name
- Throws:
java.io.IOException
- if there was a problem with the stream
-
getParent
public AesFile getParent()
Returns the virtual parent directory.- Specified by:
getParent
in interfaceIVirtualFile
- Returns:
- The parent directory.
-
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
-
getLastDateModified
public long getLastDateModified()
Returns the last date modified in milliseconds.- Specified by:
getLastDateModified
in interfaceIVirtualFile
- Returns:
- The last date modified in milliseconds.
-
getLength
public long getLength() throws java.io.IOException
Return the virtual size of the file excluding the header and hash signatures.- Specified by:
getLength
in interfaceIVirtualFile
- Returns:
- The size in bytes.
- Throws:
java.io.IOException
- if there was a problem with the stream
-
createFile
public AesFile createFile(java.lang.String filename) throws java.io.IOException
Create a file under this directory- Specified by:
createFile
in interfaceIVirtualFile
- Parameters:
filename
- The file name of the file.- Returns:
- The new file.
- Throws:
java.io.IOException
- if there was a problem with the stream
-
createFile
public AesFile createFile(java.lang.String filename, byte[] key, byte[] fileNameNonce, byte[] fileNonce) throws java.io.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:
java.io.IOException
- Thrown if there is an IO error.
-
rename
public void rename(java.lang.String newFilename) throws java.io.IOException
Rename the virtual file name- Specified by:
rename
in interfaceIVirtualFile
- Parameters:
newFilename
- The new filename this file will be renamed to- Throws:
java.io.IOException
- if there was a problem with the stream
-
rename
public void rename(java.lang.String newFilename, byte[] nonce) throws java.io.IOException
Rename the virtual file name- Parameters:
newFilename
- The new filename this file will be renamed tononce
- The nonce to use- Throws:
java.io.IOException
- Thrown when error during IO
-
exists
public boolean exists()
Returns true if this file exists- Specified by:
exists
in interfaceIVirtualFile
- Returns:
- True if exists.
-
getDecryptedFilename
protected java.lang.String getDecryptedFilename(java.lang.String filename, byte[] key, byte[] nonce) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
getEncryptedFilename
protected java.lang.String getEncryptedFilename(java.lang.String filename, byte[] key, byte[] nonce) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
getDrive
public AesDrive getDrive()
Get the drive this file belongs to.- Returns:
- The drive
-
setTag
public void setTag(java.lang.Object tag)
Set the tag for this file.- Parameters:
tag
- The file tag
-
getTag
public java.lang.Object getTag()
Get the file tag.- Returns:
- The file tag.
-
move
public AesFile move(IVirtualFile dir) throws java.io.IOException
Move file to another directory.- Specified by:
move
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.- Returns:
- The file
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
move
public AesFile move(IVirtualFile dir, IFile.MoveOptions options) throws java.io.IOException
Move file to another directory.- Specified by:
move
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.options
- The Options- Returns:
- The file
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copy
public AesFile copy(IVirtualFile dir) throws java.io.IOException
Copy file to another directory.- Specified by:
copy
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.- Returns:
- The file
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copy
public AesFile copy(IVirtualFile dir, IFile.CopyOptions options) throws java.io.IOException
Copy file to another directory.- Specified by:
copy
in interfaceIVirtualFile
- Parameters:
dir
- Target directory.options
- The options- Returns:
- The file
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copyRecursively
public void copyRecursively(IVirtualFile dest) throws java.io.IOException
Copy a directory recursively- Specified by:
copyRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directory- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copyRecursively
public void copyRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveCopyOptions options) throws java.io.IOException
Copy a directory recursively- Specified by:
copyRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directoryoptions
- The options- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
moveRecursively
public void moveRecursively(IVirtualFile dest) throws java.io.IOException
Move a directory recursively- Specified by:
moveRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directory- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
moveRecursively
public void moveRecursively(IVirtualFile dest, IVirtualFile.VirtualRecursiveMoveOptions options) throws java.io.IOException
Move a directory recursively- Specified by:
moveRecursively
in interfaceIVirtualFile
- Parameters:
dest
- The destination directoryoptions
- The options- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
deleteRecursively
public void deleteRecursively()
Delete all subdirectories and files.- Specified by:
deleteRecursively
in interfaceIVirtualFile
-
deleteRecursively
public void deleteRecursively(IVirtualFile.VirtualRecursiveDeleteOptions options)
Delete all subdirectories and files.- Specified by:
deleteRecursively
in interfaceIVirtualFile
- Parameters:
options
- The options
-
getMinimumPartSize
public long getMinimumPartSize() throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
autoRename
public static java.lang.String autoRename(AesFile file) throws java.lang.Exception
Provides an alternative file name for an AesFile. Use this to rename files.- Parameters:
file
- The file- Returns:
- The new file name
- Throws:
java.lang.Exception
- If a problem occurs
-
-