SF.net SVN: fclient: [487] trunk/fcp2/src/fcp2/key.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-07-04 08:25:48
|
Revision: 487
http://fclient.svn.sourceforge.net/fclient/?rev=487&view=rev
Author: jUrner
Date: 2008-07-04 01:25:43 -0700 (Fri, 04 Jul 2008)
Log Message:
-----------
combed over keys. many a changes..
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-02 10:00:16 UTC (rev 486)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-04 08:25:43 UTC (rev 487)
@@ -24,6 +24,9 @@
_ReMatchExact = '\A%s\Z'
KeyTypesAll = {}
+# save url chars as defined in [freenet/src/support/URLEncoder.java]
+SafeURLChars = '*-_./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
+
#**************************************************************************************
# freenet base64 for keys
#**************************************************************************************
@@ -39,9 +42,7 @@
altchars = '-~'
# padding may be ommitted or not
- padding = 4 - len(string) % 4
- if padding:
- string += '=' * padding
+ string += '=' * (len(string) % 4)
return base64.b64decode(string, altchars)
@@ -56,7 +57,13 @@
return result
#**************************************************************************************
-#
+# freenet keys
+#
+# KeyType@base64(routingbKey), base64(cryptoKey), base64(extra)
+#
+# Freenet seems to use base64 without padding along with the following altchars for urlsave
+# encode: - for + and ~ for / see: [freenet/support/base64.java]
+#
#**************************************************************************************
#TODO: too bad, can not move this to types.py ...cross import
class TypeKey(object):
@@ -69,12 +76,12 @@
>>> key.toString()
'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1')
>>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
+ 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/')
>>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
+ 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
>>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0')
>>> key.toString()
@@ -115,19 +122,7 @@
return key.toString()
#****************************************************************************************
-# freenet keys
#
-# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
-#
-# all byte components are base64 encoded. Freenet uses base64 without padding
-# along with the following altchars for urlsave encode: - for + and ~ for /
-# see: freenet/support/base64.java
-#
-# so a key as the user gets it to see is:
-# KeyType@43 bytes, 43 bytes, 7 bytes ..of [A-Za-z0-9\-~]
-#
-# see: [freenet/src/support/base64.java]
-#
#***************************************************************************************
class _KeyMeta(type):
"""Metaclass for freenet keys"""
@@ -181,31 +176,31 @@
_key_pattern_ = '''
(?P<keyType>CHK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
- (?: / (?P<filename>[^/]+?)? (?: /)?)?
+ (?: / (?P<docName>[^/]+?)? (?: /)?)?
'''
KeyType = consts.ConstKeyType.CHK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData=None, filename=None):
+ def __init__(self, keyData=None, docName=None):
"""Creates a CHK key
@param keyData: (str) key data or None
- @param filename: (str) filename to add to the key or None
+ @param docName: (str) docName to add to the key or None
"""
self.keyData = keyData
- self.filename = filename
+ self.docName = docName
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
return out
@classmethod
@@ -213,7 +208,7 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- key = clss(d['keyData'], filename=d['filename'])
+ key = clss(d['keyData'], docName=d['docName'])
return key
@@ -221,29 +216,34 @@
_key_pattern_ = '''
(?P<keyType>SSK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
(
- (?: / (?P<filename>[^/]+?))
- (?: /)?
+ (?: / (?P<docName>[^/]+?))
+ -
+ (?: (?P<edition>[\d]+))
)?
+ (?: /)?
'''
KeyType = consts.ConstKeyType.SSK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData, filename=None):
- self.filename = filename
+ def __init__(self, keyData, docName=None, edition=0):
+ self.docName = docName
+ self.edition = edition
self.keyData = keyData
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ if self.edition is None:
+ raise ValueError('no edition number specified')
+ out += self.docName + '-' + str(self.edition) + '/'
return out
@classmethod
@@ -251,14 +251,17 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(d['keyData'], d['filename'])
+ edition = d['edition']
+ if edition is not None:
+ edition = int(edition)
+ return clss(d['keyData'], d['docName'], edition=edition)
class KeyKSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>KSK@)
- (?P<filename>[^/]+?)
+ (?P<docName>[^/]+?)
(?: /)?
'''
KeyType = consts.ConstKeyType.KSK
@@ -266,13 +269,13 @@
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, filename):
- self.filename = filename
+ def __init__(self, docName):
+ self.docName = docName
def toString(self):
out = self.KeyType
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
return out
@classmethod
@@ -280,20 +283,22 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(filename=d['filename'])
+ return clss(docName=d['docName'])
-
+#TODO: is docName obligatory?
class KeyUSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>USK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
- (?: / (?P<filename>[^/]+?) )
- (?: / (?P<edition>[\d]+))
+ (
+ (?: / (?P<docName>[^/]+?) )
+ (?: / (?P<edition>-?[\d]+))
+ )?
(?: /)?
'''
@@ -301,23 +306,23 @@
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData, filename=None, edition=0):
+ def __init__(self, keyData, docName=None, edition=0):
"""Creates a USK key
@param keyData: (str) public key
- @param filename: (str) filename
+ @param docName: (str) docName
@param edition: (int) edition number
"""
self.edition = edition
- self.filename = filename
+ self.docName = docName
self.keyData = keyData
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
if self.edition is not None:
out += str(self.edition) + '/'
return out
@@ -327,7 +332,10 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(d['keyData'], d['filename'], edition=d['edition'])
+ edition = d['edition']
+ if edition is not None:
+ edition = int(edition)
+ return clss(d['keyData'], d['docName'], edition=edition)
__all__ = [i for i in dir() if i[0].isupper() and not i.startswith('_')]
@@ -336,4 +344,4 @@
#*****************************************************************************
if __name__ == '__main__':
import doctest
- print 'doctests failed: %s/%s' % doctest.testmod()
+ #print 'doctests failed: %s/%s' % doctest.testmod()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|