]> gitweb @ CieloNegro.org - youtube-dl.git/commitdiff
[utils] Add pkcs1pad
authorYen Chi Hsuan <yan12125@gmail.com>
Mon, 27 Feb 2017 10:50:19 +0000 (18:50 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Tue, 28 Feb 2017 14:10:31 +0000 (22:10 +0800)
Used in daisuki.net (#4738)

test/test_utils.py
youtube_dl/utils.py

index 3cdb21d409b97c4dec4a1c205960ae133642c620..aefd94518c37e8cf1d33f788bbeefbd9470c5f1d 100644 (file)
@@ -52,6 +52,7 @@ from youtube_dl.utils import (
     parse_filesize,
     parse_count,
     parse_iso8601,
+    pkcs1pad,
     read_batch_urls,
     sanitize_filename,
     sanitize_path,
@@ -1104,6 +1105,14 @@ The first line
             ohdave_rsa_encrypt(b'aa111222', e, N),
             '726664bd9a23fd0c70f9f1b84aab5e3905ce1e45a584e9cbcf9bcc7510338fc1986d6c599ff990d923aa43c51c0d9013cd572e13bc58f4ae48f2ed8c0b0ba881')
 
+    def test_pkcs1pad(self):
+        data = [1, 2, 3]
+        padded_data = pkcs1pad(data, 32)
+        self.assertEqual(padded_data[:2], [0, 2])
+        self.assertEqual(padded_data[28:], [0, 1, 2, 3])
+
+        self.assertRaises(ValueError, pkcs1pad, data, 8)
+
     def test_encode_base_n(self):
         self.assertEqual(encode_base_n(0, 30), '0')
         self.assertEqual(encode_base_n(80, 30), '2k')
index 17b83794a2becf272005305442094223cd054638..8bd075eaf1c6c4e73d7ae962ccff144210d93dfb 100644 (file)
@@ -3336,6 +3336,21 @@ def ohdave_rsa_encrypt(data, exponent, modulus):
     return '%x' % encrypted
 
 
+def pkcs1pad(data, length):
+    """
+    Padding input data with PKCS#1 scheme
+
+    @param {int[]} data        input data
+    @param {int}   length      target length
+    @returns {int[]}           padded data
+    """
+    if len(data) > length - 11:
+        raise ValueError('Input data too long for PKCS#1 padding')
+
+    pseudo_random = [random.randint(0, 254) for _ in range(length - len(data) - 3)]
+    return [0, 2] + pseudo_random + [0] + data
+
+
 def encode_base_n(num, n, table=None):
     FULL_TABLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
     if not table: