gpt4 book ai didi

hash - 在终端中生成盐渍哈希

转载 作者:行者123 更新时间:2023-12-01 00:18:32 24 4
gpt4 key购买 nike

我想制作一个生成 的 AppleScript盐渍 与终端哈希。是否有特定的终端命令可以生成加盐散列,最好是像 SHA-512 这样的安全命令?如果可能的话,我想要一个单行的,这样我就可以将它与 do shell script 命令一起使用。我在网上搜索,但没有找到在终端中生成加盐哈希的方法,只是一个普通的。

我正在运行 OS X Mavericks 10.9.5。

最佳答案

据我了解,至少在概念上,您所要求的需要两个步骤:

  • 获取随机盐值。
  • 将盐值与输入文本(密码)连接起来并计算组合值的哈希值。

  • 为了以后验证,您必须将盐与生成的散列一起存储。

    以下 AppleScript 处理程序封装了提供必需功能的 shell 函数 - 它们前面是示例调用。

    免责声明:我对这个领域的理解是有限的,所以对这些函数持保留态度(哈!)。

    盐生成功能非常感谢地改编自 this post

    # Sample text to hash.
    set passwd to "somePassword"

    # Generate salt value with 10 chars, amounting to about a 64-bit value.
    set salt to generateSalt(10)

    # Compute hash from combined salt and input value.
    set hash to getSha512(salt & passwd)


    # SYNOPSIS
    # getSha512(text)
    # DESCRIPTION
    # Calculates and outputs TEXT's hash value using the SHA-512 (SHA-2) algorithm.
    # Output is a 128-characters string composed of lowercase hexadecimal digits.
    # To create a salted hash, obtain a salt with generateSalt() first and
    # prepend it to the text to hash.
    # PREREQUISITES
    # Requires either the sha512sum or the shasum utility. One or the other should be
    # available on BSD/OSX and Linux systems.
    # EXAMPLE
    # set salt to generateSalt(20)
    # set hash to getSha512(salt & passwd)
    on getSha512(txt)
    do shell script "
    getSha512() {
    local -a shaCmd
    if command -v sha512sum &>/dev/null; then
    shaCmd=( sha512sum )
    elif command -v shasum &>/dev/null; then
    shaCmd=( shasum -a 512 )
    else
    { echo 'ERROR: Cannot locate SHA-512-generating utility.' >&2; return 1; }
    fi
    # Invoke the SHA-generating command and output the first space-separated field.
    # (The subsequent fields indicate the mode and input filename.)
    \"${shaCmd[@]}\" <<<\"$1\" | cut -d' ' -f1
    return \"${PIPESTATUS[0]}\"
    }
    getSha512 " & quoted form of txt
    end getSha512

    # SYNOPSIS
    # generateSalt(numChars)
    # DESCRIPTION
    # Generates NUMCHARS random *printable* ASCII characters that can serve as
    # cryptographic salt. Due to the range of printable characters, each character
    # returned contains ca. 6.55 bits of information.
    # Thus, for instance, to get a 64-bit salt value, specify 10 for NUMCHARS.
    # For a 128-bit value, specify 20.
    # Use /dev/urandom as the source of random data.
    # PREREQUISITES
    # File /dev/urandom as a source of random bytes.
    # The `head` utility must support the -c option to extract a number of *bytes*.
    # Both BSD/OSX and Linux systems fulfill these requirements.
    # EXAMPLE
    # set salt to generateSalt(20) # get a ca. 128-bit salt value as 20 printable ASCII chars.
    on generateSalt(numChars)
    do shell script "
    generateSalt() {
    [[ -c /dev/urandom ]] || { echo 'ERROR: Random source /dev/urandom not available.' >&2; return 1; }
    LC_ALL=C tr -cd '!\"#$%&'\\''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' < /dev/urandom | head -c $1
    }
    generateSalt " & numChars
    end generateSalt

    关于hash - 在终端中生成盐渍哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30222622/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com