def get_keypair(hostname): """Generate a dummy keypair for the given hostname. This method generates a dummy DSA keypair for the given hostname. It returns a tuple (pub, priv) where "pub" is a dict of values for the public key, and "priv" is a DSA128Key object containing the private key. Multiple calls to this method for the same hostname are guaranteed to produce the same key. To make this work we take advantage of the fact that DSA key generation is just "generate x by some random method, where 0 < x < q". Replace "some random method" with "sha1 hash of hostname" and we're all set. """ # Use pre-agreed parameters for p, q and g. q = DUMMY_Q p = DUMMY_P g = DUMMY_G # Generate private key x by "some random method". x = to_int(hashlib.sha1(hostname.encode("utf8")).hexdigest(), 16) assert x != 0, "SHA1(hostname) is zero - what are the odds?!" # Calculate public key y as usual. y = pow(g, x, p) data = {"algorithm": "DS", "p": to_hex(p), "q": to_hex(q), "g": to_hex(g), "y": to_hex(y), "x": to_hex(x)} privkey = jwt.DS128Key(data) del data["x"] return data, privkey
def get_keypair(hostname): """Generate a dummy keypair for the given hostname. This method generates a dummy DSA keypair for the given hostname. It returns a tuple (pub, priv) where "pub" is a dict of values for the public key, and "priv" is a DSA128Key object containing the private key. Multiple calls to this method for the same hostname are guaranteed to produce the same key. To make this work we take advantage of the fact that DSA key generation is just "generate x by some random method, where 0 < x < q". Replace "some random method" with "sha1 hash of hostname" and we're all set. """ # Use pre-agreed parameters for p, q and g. q = DUMMY_Q p = DUMMY_P g = DUMMY_G # Generate private key x by "some random method". x = to_int(hashlib.sha1(hostname.encode("utf8")).hexdigest(), 16) assert x != 0, "SHA1(hostname) is zero - what are the odds?!" # Calculate public key y as usual. y = pow(g, x, p) data = { "algorithm": "DS", "p": to_hex(p), "q": to_hex(q), "g": to_hex(g), "y": to_hex(y), "x": to_hex(x), } privkey = jwt.DS128Key(data) del data["x"] return data, privkey
def generate_keypair(): """Generate a new DSA keypair for use with PyBrowserID. This function returns a tuple (public_data, private_key) giving the JSON-serializable public-key data and the associated private key as a browserid.jwt.Key object. """ key = dsa.generate_private_key(1024, backend=backend) params = key.parameters().parameter_numbers() data = { "algorithm": "DS", "p": to_hex(params.p), "q": to_hex(params.q), "g": to_hex(params.g), "y": to_hex(key.public_key().public_numbers().y), "x": to_hex(key.private_numbers().x), } private_key = browserid.jwt.DS128Key(data) del data["x"] return data, private_key
914761181078121416297141767464747032219333582869739887 884736300667713296956049473944465827480687584552025991 717914841355273754193114413628325025151484385088161118 794329026966356844773094137980084703759603150591097278 715178348827663152700571998676478162596562814192444939 969198839004936798148664921543401849279637016264260100 884799833350543315289267376119637531072279656873496164 487439865534937842040868268534375254876875600122000071 183491091196621992223116828762911412383078024328333659 43400749509104482286419733"""), } DS128_KEY_DATA = { "p": to_hex("""6703904104057623261995085583676902361410672713749348 7374515589871295072792250899011720632358392764362903244 12395020783955234715731001076129344181463063193"""), "q": to_hex(1006478751418673383937866166434285354892250535133), "g": to_hex("""1801778249650423365253284139284406405780267098493217 0320675876307450879812560049234773036938891018778074993 01874343843218156663689824126183823813389886834"""), "y": to_hex("""4148629652526876030475847300836791685289385792662680 5886292874741635965095055693693232436255359496594291250 77637642734034732001089176915352691113947372211"""), "x": to_hex(487025797851506801093339352420308364866214860934), }
914761181078121416297141767464747032219333582869739887 884736300667713296956049473944465827480687584552025991 717914841355273754193114413628325025151484385088161118 794329026966356844773094137980084703759603150591097278 715178348827663152700571998676478162596562814192444939 969198839004936798148664921543401849279637016264260100 884799833350543315289267376119637531072279656873496164 487439865534937842040868268534375254876875600122000071 183491091196621992223116828762911412383078024328333659 43400749509104482286419733"""), } DS128_KEY_DATA = { "p": to_hex("""6703904104057623261995085583676902361410672713749348 7374515589871295072792250899011720632358392764362903244 12395020783955234715731001076129344181463063193"""), "q": to_hex(1006478751418673383937866166434285354892250535133), "g": to_hex("""1801778249650423365253284139284406405780267098493217 0320675876307450879812560049234773036938891018778074993 01874343843218156663689824126183823813389886834"""), "y": to_hex("""4148629652526876030475847300836791685289385792662680 5886292874741635965095055693693232436255359496594291250 77637642734034732001089176915352691113947372211"""), "x": to_hex(487025797851506801093339352420308364866214860934), } DS256_KEY_DATA = { "p": to_hex("""2711208960741861745308573380095332404137549620315947 9068314201104887216043109325809831713787118502848090805