Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
827 daniel-mar 1
<?php
2
 
3
/**
4
 * SSH2 Signature Handler
5
 *
6
 * PHP version 5
7
 *
8
 * Handles signatures in the format used by SSH2
9
 *
874 daniel-mar 10
 * @category  Crypt
11
 * @package   Common
827 daniel-mar 12
 * @author    Jim Wigginton <terrafrost@php.net>
13
 * @copyright 2016 Jim Wigginton
14
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
15
 * @link      http://phpseclib.sourceforge.net
16
 */
17
 
18
namespace phpseclib3\Crypt\DSA\Formats\Signature;
19
 
20
use phpseclib3\Common\Functions\Strings;
21
use phpseclib3\Math\BigInteger;
22
 
23
/**
24
 * SSH2 Signature Handler
25
 *
874 daniel-mar 26
 * @package Common
827 daniel-mar 27
 * @author  Jim Wigginton <terrafrost@php.net>
874 daniel-mar 28
 * @access  public
827 daniel-mar 29
 */
30
abstract class SSH2
31
{
32
    /**
33
     * Loads a signature
34
     *
874 daniel-mar 35
     * @access public
827 daniel-mar 36
     * @param string $sig
37
     * @return mixed
38
     */
39
    public static function load($sig)
40
    {
41
        if (!is_string($sig)) {
42
            return false;
43
        }
44
 
45
        $result = Strings::unpackSSH2('ss', $sig);
46
        if ($result === false) {
47
            return false;
48
        }
49
        list($type, $blob) = $result;
50
        if ($type != 'ssh-dss' || strlen($blob) != 40) {
51
            return false;
52
        }
53
 
54
        return [
55
            'r' => new BigInteger(substr($blob, 0, 20), 256),
56
            's' => new BigInteger(substr($blob, 20), 256)
57
        ];
58
    }
59
 
60
    /**
61
     * Returns a signature in the appropriate format
62
     *
874 daniel-mar 63
     * @access public
827 daniel-mar 64
     * @param \phpseclib3\Math\BigInteger $r
65
     * @param \phpseclib3\Math\BigInteger $s
66
     * @return string
67
     */
68
    public static function save(BigInteger $r, BigInteger $s)
69
    {
70
        if ($r->getLength() > 160 || $s->getLength() > 160) {
71
            return false;
72
        }
73
        return Strings::packSSH2(
74
            'ss',
75
            'ssh-dss',
76
            str_pad($r->toBytes(), 20, "\0", STR_PAD_LEFT) .
77
            str_pad($s->toBytes(), 20, "\0", STR_PAD_LEFT)
78
        );
79
    }
80
}