|
| 1 | +# pyapr1 - A Python implementation of the APR1 algorithm |
| 2 | +# |
| 3 | +# Copyright (c) 2015, Tilman Blumenbach |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of pyapr1 nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +# The to64() and hash_apr1() functions are based on code from the Apache Portable |
| 32 | +# Runtime Utility Library (namely, on the two functions to64() and |
| 33 | +# apr_md5_encode() from the file crypto/apr_md5.c). The licenses for that original |
| 34 | +# material are included below: |
| 35 | +# |
| 36 | +# ============================================================================ |
| 37 | +# |
| 38 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 39 | +# contributor license agreements. See the NOTICE file distributed with |
| 40 | +# this work for additional information regarding copyright ownership. |
| 41 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 42 | +# (the "License"); you may not use this file except in compliance with |
| 43 | +# the License. You may obtain a copy of the License at |
| 44 | +# |
| 45 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 46 | +# |
| 47 | +# Unless required by applicable law or agreed to in writing, software |
| 48 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 49 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 50 | +# See the License for the specific language governing permissions and |
| 51 | +# limitations under the License. |
| 52 | +# |
| 53 | +# |
| 54 | +# |
| 55 | +# The apr_md5_encode() routine uses much code obtained from the FreeBSD 3.0 |
| 56 | +# MD5 crypt() function, which is licenced as follows: |
| 57 | +# ---------------------------------------------------------------------------- |
| 58 | +# "THE BEER-WARE LICENSE" (Revision 42): |
| 59 | +# <[email protected]> wrote this file. As long as you retain this notice you |
| 60 | +# can do whatever you want with this stuff. If we meet some day, and you think |
| 61 | +# this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
| 62 | +# ---------------------------------------------------------------------------- |
| 63 | +# |
| 64 | +# ============================================================================ |
| 65 | + |
| 66 | +# 20250910 - This file was sourced from https://github.com/Tblue/pyapr1 and is used |
| 67 | +# to provide backward compatibility with older htpasswd tools, notably |
| 68 | +# apache 2.2 and lower. The apr1 algorithm is not considered secure and |
| 69 | +# bcrypt is currently the recommended algorithm. |
| 70 | + |
| 71 | +import os |
| 72 | +import sys |
| 73 | + |
| 74 | +from hashlib import md5 |
| 75 | +from time import sleep |
| 76 | + |
| 77 | + |
| 78 | +def to64(data, n_out): |
| 79 | + chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 80 | + out = "" |
| 81 | + |
| 82 | + for i in range(n_out): |
| 83 | + out += chars[data & 0x3F] |
| 84 | + data >>= 6 |
| 85 | + |
| 86 | + return out |
| 87 | + |
| 88 | + |
| 89 | +def mkint(data, *indexes): |
| 90 | + r = 0 |
| 91 | + for i, idx in enumerate(indexes): |
| 92 | + r |= data[idx] << 8 * (len(indexes) - i - 1) |
| 93 | + |
| 94 | + return r |
| 95 | + |
| 96 | + |
| 97 | +def hash_apr1(salt, password): |
| 98 | + sb = bytes(salt, "utf-8") |
| 99 | + pb = bytes(password, "utf-8") |
| 100 | + ph = md5() |
| 101 | + |
| 102 | + # First, the password. |
| 103 | + ph.update(pb) |
| 104 | + # Then, the magic string. |
| 105 | + ph.update(b"$apr1$") |
| 106 | + # Then, the salt. |
| 107 | + ph.update(sb) |
| 108 | + |
| 109 | + # Weird stuff. |
| 110 | + sandwich = md5(pb + sb + pb).digest() |
| 111 | + ndig, nrem = divmod(len(pb), ph.digest_size) |
| 112 | + for n in ndig * [ph.digest_size] + [nrem]: |
| 113 | + ph.update(sandwich[:n]) |
| 114 | + |
| 115 | + # Even more weird stuff. |
| 116 | + i = len(pb) |
| 117 | + while i: |
| 118 | + if i & 1: |
| 119 | + ph.update(b"\0") |
| 120 | + else: |
| 121 | + ph.update(pb[:1]) |
| 122 | + |
| 123 | + i >>= 1 |
| 124 | + |
| 125 | + final = ph.digest() |
| 126 | + for i in range(1000): |
| 127 | + maelstrom = md5() |
| 128 | + |
| 129 | + if i & 1: |
| 130 | + maelstrom.update(pb) |
| 131 | + else: |
| 132 | + maelstrom.update(final) |
| 133 | + |
| 134 | + if i % 3: |
| 135 | + maelstrom.update(sb) |
| 136 | + |
| 137 | + if i % 7: |
| 138 | + maelstrom.update(pb) |
| 139 | + |
| 140 | + if i & 1: |
| 141 | + maelstrom.update(final) |
| 142 | + else: |
| 143 | + maelstrom.update(pb) |
| 144 | + |
| 145 | + final = maelstrom.digest() |
| 146 | + |
| 147 | + pw_ascii = ( |
| 148 | + to64(mkint(final, 0, 6, 12), 4) |
| 149 | + + to64(mkint(final, 1, 7, 13), 4) |
| 150 | + + to64(mkint(final, 2, 8, 14), 4) |
| 151 | + + to64(mkint(final, 3, 9, 15), 4) |
| 152 | + + to64(mkint(final, 4, 10, 5), 4) |
| 153 | + + to64(mkint(final, 11), 2) |
| 154 | + ) |
| 155 | + |
| 156 | + return "$apr1$%s$%s" % (salt, pw_ascii) |
| 157 | + |
| 158 | + |
| 159 | +def generate_salt(): |
| 160 | + return to64(mkint(os.urandom(6), *range(6)), 8) |
0 commit comments