def getDirectorySignature(directory, pkey): digest = mixminion.ServerInfo._getMultisignedDirectoryDigest(directory) signature = pk_sign(digest, pkey) encKey = formatBase64(pk_encode_public_key(pkey)) encSig = formatBase64(signature) encDigest = formatBase64(digest) return ("[Signed-Directory]\nDirectory-Identity: %s\n" "Directory-Digest: %s\nDirectory-Signature: %s\n")%( encKey,encDigest,encSig)
def _getDigestImpl(info, regex, digestField=None, sigField=None, rsa=None): """Helper method. Calculates the correct digest of a server descriptor or directory (as provided in a string). If rsa is provided, signs the digest and creates a new descriptor. Otherwise just returns the digest. info -- the string to digest or sign. regex -- a compiled regex that matches the line containing the digest and the line containing the signature. digestField -- If not signing, None. Otherwise, the name of the digest field. sigField -- If not signing, None. Otherwise, the name of the signature field. rsa -- our public key """ info = _cleanForDigest(info) def replaceFn(m): s = m.group(0) return s[:s.index(':') + 1] info = regex.sub(replaceFn, info, 2) digest = sha1(info) if rsa is None: return digest signature = pk_sign(digest, rsa) digest = formatBase64(digest) signature = formatBase64(signature) def replaceFn2(s, digest=digest, signature=signature, digestField=digestField, sigField=sigField): if s.group(0).startswith(digestField): return "%s: %s" % (digestField, digest) else: assert s.group(0).startswith(sigField) return "%s: %s" % (sigField, signature) info = regex.sub(replaceFn2, info, 2) return info
def _getDigestImpl(info, regex, digestField=None, sigField=None, rsa=None): """Helper method. Calculates the correct digest of a server descriptor or directory (as provided in a string). If rsa is provided, signs the digest and creates a new descriptor. Otherwise just returns the digest. info -- the string to digest or sign. regex -- a compiled regex that matches the line containing the digest and the line containing the signature. digestField -- If not signing, None. Otherwise, the name of the digest field. sigField -- If not signing, None. Otherwise, the name of the signature field. rsa -- our public key """ info = _cleanForDigest(info) def replaceFn(m): s = m.group(0) return s[:s.index(':')+1] info = regex.sub(replaceFn, info, 2) digest = sha1(info) if rsa is None: return digest signature = pk_sign(digest,rsa) digest = formatBase64(digest) signature = formatBase64(signature) def replaceFn2(s, digest=digest, signature=signature, digestField=digestField, sigField=sigField): if s.group(0).startswith(digestField): return "%s: %s" % (digestField, digest) else: assert s.group(0).startswith(sigField) return "%s: %s" % (sigField, signature) info = regex.sub(replaceFn2, info, 2) return info