view src/plugins/plugin_xep_0300.py @ 1606:de785fcf9a7b

jp (base, file): file command and progress fixes and adaptation to new API: - progress use new API, and signals to monitor progression and avoid use of progressGet before progress is actually started - progress behaviour on event is managed by callbacks which are Jp attributes - progress with unknown or 0 size don't show the progress bar - better handling of errors - CommandAnswering is update to manage actions instead of the deprecated askConfirmation - managed actions use callback easy to associate with an action type. - file command is updated to manage these changes and the recent changes in backend behaviour - verbosity is used to display more or less message when sending/receiving a file - destination path can be specified in file receive - file receive doesn't stop yet, still need some work in the backend
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2015 23:42:21 +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