Skip to content

Commit 4ad73f9

Browse files
committed
added two new valuable functions for dealing with binary data (e.g. binary representations of password hashes) and some cosmetics
1 parent 277f16d commit 4ad73f9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/core/common.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ def posixToNtSlashes(filepath):
14021402
"""
14031403
Replaces all occurances of Posix slashes (/) in provided
14041404
filepath with NT ones (/)
1405+
14051406
>>> posixToNtSlashes('C:/Windows')
14061407
'C:\\\\Windows'
14071408
"""
@@ -1412,6 +1413,7 @@ def ntToPosixSlashes(filepath):
14121413
"""
14131414
Replaces all occurances of NT slashes (\) in provided
14141415
filepath with Posix ones (/)
1416+
14151417
>>> ntToPosixSlashes('C:\\Windows')
14161418
'C:/Windows'
14171419
"""
@@ -1421,6 +1423,7 @@ def ntToPosixSlashes(filepath):
14211423
def isBase64EncodedString(subject):
14221424
"""
14231425
Checks if the provided string is Base64 encoded
1426+
14241427
>>> isBase64EncodedString('dGVzdA==')
14251428
True
14261429
>>> isBase64EncodedString('123456')
@@ -1432,6 +1435,7 @@ def isBase64EncodedString(subject):
14321435
def isHexEncodedString(subject):
14331436
"""
14341437
Checks if the provided string is hex encoded
1438+
14351439
>>> isHexEncodedString('DEADBEEF')
14361440
True
14371441
>>> isHexEncodedString('test')
@@ -1667,6 +1671,7 @@ def getCompiledRegex(regex, flags=0):
16671671
"""
16681672
Returns compiled regular expression and stores it in cache for further
16691673
usage
1674+
16701675
>>> getCompiledRegex('test') # doctest: +ELLIPSIS
16711676
<_sre.SRE_Pattern object at...
16721677
"""
@@ -2374,6 +2379,7 @@ def maskSensitiveData(msg):
23742379
def listToStrValue(value):
23752380
"""
23762381
Flattens list to a string value
2382+
23772383
>>> listToStrValue([1,2,3])
23782384
'1, 2, 3'
23792385
"""
@@ -2408,6 +2414,7 @@ def intersect(valueA, valueB):
24082414
"""
24092415
Returns intersection of the array-ized values
24102416
"""
2417+
24112418
retVal = None
24122419

24132420
if valueA and valueB:
@@ -2419,6 +2426,7 @@ def cpuThrottle(value):
24192426
"""
24202427
Does a CPU throttling for a lesser CPU consumption
24212428
"""
2429+
24222430
delay = 0.00001 * (value ** 2)
24232431
time.sleep(delay)
24242432

@@ -2451,6 +2459,7 @@ def normalizeUnicode(value):
24512459
Does an ASCII normalization of unicode strings
24522460
Reference: http://www.peterbe.com/plog/unicode-to-ascii
24532461
"""
2462+
24542463
retVal = value
24552464
if isinstance(value, unicode):
24562465
retVal = unicodedata.normalize('NFKD', value).encode('ascii','ignore')
@@ -2460,6 +2469,7 @@ def safeSQLIdentificatorNaming(name, isTable=False):
24602469
"""
24612470
Returns a safe representation of SQL identificator name
24622471
"""
2472+
24632473
retVal = name
24642474
if isinstance(name, basestring):
24652475
if isTable and Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE) and '.' not in name:
@@ -2480,6 +2490,7 @@ def unsafeSQLIdentificatorNaming(name):
24802490
"""
24812491
Extracts identificator's name from it's safe SQL representation
24822492
"""
2493+
24832494
retVal = name
24842495
if isinstance(name, basestring):
24852496
if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.ACCESS):
@@ -2489,3 +2500,28 @@ def unsafeSQLIdentificatorNaming(name):
24892500
if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE):
24902501
retVal = retVal.lstrip("%s." % DEFAULT_MSSQL_SCHEMA)
24912502
return retVal
2503+
2504+
def isBinaryData(value):
2505+
"""
2506+
Tests given value for binary content
2507+
"""
2508+
2509+
retVal = False
2510+
if isinstance(value, basestring):
2511+
retVal = reduce(lambda x, y: x or not (y in string.printable or ord(y) > 255), value, False)
2512+
return retVal
2513+
2514+
def getSafeHexEncodedBinaryData(value):
2515+
"""
2516+
Returns safe representation of given basestring value
2517+
2518+
>>> getSafeEncodedBinaryData(u'test123')
2519+
u'test123'
2520+
>>> getSafeEncodedBinaryData(u'test\01\02\03')
2521+
u'test\\1\\2\\3'
2522+
"""
2523+
2524+
retVal = value
2525+
if isinstance(value, basestring):
2526+
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\%x' % ord(y)), value, unicode())
2527+
return retVal

0 commit comments

Comments
 (0)