view src/plugins/plugin_xep_0300.py @ 1555:eb8aae35085b

plugin ip: local ip cache + DNS error detection
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents bb451fd1cea3
children 0de5f210fe56
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# SAT plugin for Hash functions (XEP-0300)
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.xish import domish
import hashlib


NS_HASHES = "urn:xmpp:hashes:1"

PLUGIN_INFO = {
    "name": "Cryptographic Hash Functions",
    "import_name": "XEP-0300",
    "type": "XEP",
    "protocols": ["XEP-0300"],
    "main": "XEP_0300",
    "handler": "no",
    "description": _("""Management of cryptographic hashes""")
}


class XEP_0300(object):
    ALGOS = {u'md5': hashlib.md5,
             u'sha-1': hashlib.sha1,
             u'sha-256': hashlib.sha256,
             u'sha-512': hashlib.sha512,
            }

    def __init__(self, host):
        log.info(_("plugin Hashes initialization"))

    def buidHash(self, file_obj=None, algo='sha-256'):
        """Compute hash and build hash element

        @param file_obj(file, None): file to use to calculate the hash
            if file_obj is None, en empty hash element will be returned (useful e.g. in XEP-0234)
        @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS
        @return (domish.Element): computed hash
        """
        hasher = self.ALGOS[algo]
        hash_elt = domish.Element((NS_HASHES, 'hash'))
        hash_elt['algo']=algo
        # TODO: actually hash, use deferToThread
        return hash_elt