ソースを参照

python & shortcodes

oyiadin 6 年 前
コミット
5744e5f494
4 ファイル変更68 行追加0 行削除
  1. 2 0
      .gitignore
  2. 0 0
      example_site/.gitkeep
  3. 55 0
      hugo-encryptor.py
  4. 11 0
      shortcodes/hugo-encryptor.html

+ 2 - 0
.gitignore

@@ -1 +1,3 @@
 node_modules/
+.DS_Store
+

+ 0 - 0
example_site/.gitkeep


+ 55 - 0
hugo-encryptor.py

@@ -0,0 +1,55 @@
+# coding=utf-8
+import os
+import base64
+
+from bs4 import BeautifulSoup
+from Crypto.Cipher import AES
+
+
+class DESCrypt(object):
+    LEN = 16
+    def __init__(self, key: str):
+        if len(key) > DESCrypt.LEN:
+            raise ValueError('password too long')
+
+        self.key = key.encode().ljust(DESCrypt.LEN, b'\x00')
+        self.mode = AES.MODE_CBC
+
+    def encrypt(self, text: bytes):
+        cryptor = AES.new(self.key, self.mode, self.key)
+        padlen = DESCrypt.LEN - len(text) % DESCrypt.LEN
+
+        padlen = padlen if padlen != 0 else DESCrypt.LEN
+
+        for i in range(padlen):
+            text += chr(i+1).encode()
+
+        return cryptor.encrypt(text)
+
+
+if __name__ == '__main__':
+    for dirpath, dirnames, filenames in os.walk('.'):
+        for filename in filenames:
+            if not filename.lower().endswith('.html'):
+                continue
+
+            fullpath = os.path.join(dirpath, filename)
+
+            soup = BeautifulSoup(open(fullpath), features="html5lib")
+            block = soup.find('div', {'id': 'hugo-encryptor'})
+
+            if block is None:
+                pass
+
+            else:
+                print(fullpath)
+
+                cryptor = DESCrypt(block['data-password'])
+                text = ''.join(map(str, block.contents)).encode()
+                written = base64.b64encode(cryptor.encrypt(text))
+
+                del block['data-password']
+                block.string = written.decode()
+
+                with open(fullpath, 'w') as f:
+                    f.write(str(soup))

+ 11 - 0
shortcodes/hugo-encryptor.html

@@ -0,0 +1,11 @@
+该文章已被加密,请输入密码以查看。
+
+<!-- 以上内容出现在首页等可能出现 summary 的地方 -->
+
+<!--more-->
+
+<!-- 解密用的代码在这,正文里出现 -->
+
+<div id="hugo-encryptor" data-password="{{ .Get 0 }}">
+{{ .Inner }}
+</div>