#!/usr/bin/python

# Copyright (C) 2009 Oracle and/or its affiliates. All rights reserved.
# Author: Waseem Daher
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.


## (Partially) implement the parts of hashlib we need, 
## for our Py2.3 and Py2.4 clients
from sha import sha as sha1
import sys

if sys.version_info >= (2, 6, 2, 'final', 0):  # subprocess_compat comes from 2.6.2
    import subprocess
else:
    import subprocess_compat as subprocess

class CompatSHA256:
    def __init__(self, contents):
        p = subprocess.Popen(['/usr/bin/sha256sum', '-b'],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                             stdin=subprocess.PIPE)
        stdout, stderr = p.communicate(contents)
        if p.returncode:
            raise subprocess.CalledProcessError(p.returncode, 'sha256sum')

        self._digest = stdout.split(' ')[0]

    def hexdigest(self):
        return self._digest

def sha256(contents):
    return CompatSHA256(contents)
