filevault
This commit is contained in:
parent
32b47a4cc0
commit
863674491c
1 changed files with 50 additions and 0 deletions
50
filevault.py
Normal file
50
filevault.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from os import path
|
||||
from os import makedirs
|
||||
from uuid import uuid4
|
||||
from hashlib import sha256
|
||||
from itertools import permutations
|
||||
|
||||
HEX="0123456789abcdef"
|
||||
|
||||
class Vault( object ):
|
||||
def __init__( self, vaultpath='vault', depth=3, salt='changeme' ):
|
||||
self.vaultpath = vaultpath
|
||||
self.depth = depth
|
||||
self.salt = salt
|
||||
self.initVault()
|
||||
|
||||
def initVault( self ):
|
||||
"""Build the vault directories if they don't exist"""
|
||||
if path.exists( self.vaultpath ):
|
||||
return True
|
||||
|
||||
perms = permutations( HEX * self.depth, self.depth )
|
||||
for perm in perms:
|
||||
try: makedirs( path.join( self.vaultpath, *perm ) )
|
||||
except OSError: pass
|
||||
|
||||
def _generate_filename( self, h, ext='', absolute=False ):
|
||||
"""Accept a hash, return a valid file path"""
|
||||
dirs = h[ 0 : self.depth ]
|
||||
if ext and not ext.startswith( '.' ): ext = '.' + ext
|
||||
if absolute == True:
|
||||
return path.join( self.vaultpath, *dirs ) + '/' + h + ext
|
||||
return path.join( *dirs ) + '/' + h + ext
|
||||
|
||||
def create_filename( self, seed, ext='', absolute=False ):
|
||||
"""
|
||||
Create a valid vault filename seeded with input. Optional extension.
|
||||
Path relative to vaultpath by default unless absolute is True.
|
||||
"""
|
||||
h = sha256( seed + self.salt ).hexdigest()
|
||||
return self._generate_filename( h, ext, absolute )
|
||||
|
||||
def create_random_filename( self, ext='', absolute=False ):
|
||||
"""
|
||||
Create a valid vault filename seeded with random input. Optional extension.
|
||||
Path relative to vaultpath by default unless absolute is True.
|
||||
"""
|
||||
h = sha256( str( uuid4() ) + self.salt ).hexdigest()
|
||||
return self._generate_filename( h, ext, absolute )
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue