blob: 842024b44b966ae67f0cd3484c8230662e199f28 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2#
3# Copyright (c) 2011-2013, Regents of the University of California
4# Alexander Afanasyev
5#
6# GNU 3.0 license, See the LICENSE file for more information
7#
8# Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9#
10
11#
12# Based on PyCCN code, copyrighted and licensed as follows
13#
14# Copyright (c) 2011-2013, Regents of the University of California
15# BSD license, See the COPYING file for more information
16# Written by: Derek Kulinski <takeda@takeda.tk>
17# Jeff Burke <jburke@ucla.edu>
18#
19
Alexander Afanasyev39f53162013-07-18 16:24:31 -070020import ns.core
21import ns.ndnSIM
22from Name import Name
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070023
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070024class Key (object):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070025 def __init__ (self):
Alexander Afanasyev05d40942013-08-09 12:43:26 -070026 self.publicKeyID = None # SHA256 hash
27 self.fakeKey = 0
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070028
29 def generateRSA(self, numbits):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070030 randVar = ns.core.UniformVariable ()
31 self.fakeKey = randVar.GetInteger (0, 2147483647)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070032
33 def privateToDER(self):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070034 return self.fakeKey
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070035
36 def publicToDER(self):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070037 return self.privateToDER ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070038
39 def privateToPEM(self, filename = None, password = None):
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070040 if filename:
41 f = open(filename, 'w')
Alexander Afanasyev39f53162013-07-18 16:24:31 -070042 f.write (self.fakeKey)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070043 f.close()
44 else:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070045 return self.fakeKey
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070046
47 def publicToPEM(self, filename = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070048 return privateToPEM (filename)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070049
50 def fromDER(self, private = None, public = None):
51 if private:
Alexander Afanasyev05d40942013-08-09 12:43:26 -070052 self.fakeKey = hash(private)
Alexander Afanasyev39f53162013-07-18 16:24:31 -070053 elif public:
Alexander Afanasyev05d40942013-08-09 12:43:26 -070054 self.fakeKey = hash(public)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070055
56 def fromPEM(self, filename = None, private = None, public = None, password = None):
57 if filename:
58 f = open(filename, 'r')
Alexander Afanasyev05d40942013-08-09 12:43:26 -070059 self.fakeKey = hash(f.read ())
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070060 f.close()
61 elif private:
Alexander Afanasyev05d40942013-08-09 12:43:26 -070062 self.fakeKey = hash(private)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070063 elif public:
Alexander Afanasyev05d40942013-08-09 12:43:26 -070064 self.fakeKey = hash(public)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070065
66 @staticmethod
Alexander Afanasyev39f53162013-07-18 16:24:31 -070067 def createFromDER (private = None, public = None):
68 key = Key ()
69 key.fromDER (private, public)
70 return key
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070071
Alexander Afanasyev39f53162013-07-18 16:24:31 -070072 @staticmethod
73 def createFromPEM (filename = None, private = None, public = None, password = None):
74 key = Key ()
75 key.fromPEM (filename, private, public, password)
76 return key
77
78 @staticmethod
79 def getDefaultKey ():
80 context = ns.core.Simulator.GetContext ()
81 key = Key ()
82 key.fakeKey = context
83 return key