【问题标题】:SHA1 hashing in SQLite: how?SQLite 中的 SHA1 哈希:如何?
【发布时间】:2011-03-11 21:03:35
【问题描述】:

并行处理多个数据库,需要使用散列密码初始化一些记录。在MS SQL server 中,有一些方便的函数允许动态散列:


HashBytes('SHA1', CONVERT(nvarchar(32), N'admin'))

有没有和SQLite类似的功能?

如果不是,这是最简单的解决方法(例如从SQL server 中选择并以某种方式将其插入SQLite 表中)?

首选的哈希算法是SHA1,密码存储在BLOB 列中。

更新:我在当前项目中使用 C# 语言。

【问题讨论】:

  • SHA-1 不是加密,而是加密哈希函数。
  • SHA1 不是散列密码的好选择,因为它是快速的方法,因此很容易进行暴力破解。看看 BCrypt 或 PBKDF2 等密钥派生函数,后者内置在 C# 中,因为 BCrypt 库可用。

标签: c# sqlite cryptography hash sha1


【解决方案1】:

SQLite3 中没有内置这样的函数。

但是你可以定义一个用户函数,例如sqlite3_create_function 如果您使用的是 C 接口,则使用它实现 SHA-1。 (但如果你有一个可编程接口,也许你可以在 SQL 引擎之外对密码进行 SHA-1。)

您也可以尝试查找/创建扩展程序并使用 the load_extension function 加载,但我没有这方面的经验。

编辑:

【讨论】:

    【解决方案2】:

    SQLite 不附带 SHA1,但添加起来相对容易。您没有说您使用的是什么语言,但您可以查看 create_functionsqlite3_result 的 C 文档。您还可以查看this example,了解如何使用 Ruby 将 SHA1 添加到 SQLite。

    对于System.Data.SQLite,它们被称为用户定义函数。您可以在主站点上查看this example

    【讨论】:

      【解决方案3】:

      请注意 sqlite 确实在 2017 年添加了 sha1() 扩展

      https://www.sqlite.org/src/file/ext/misc/sha1.c

      虽然它可能默认不启用。

      【讨论】:

        【解决方案4】:

        您可以像这样在 C# 中为 SHA1 创建自定义函数:

        [SQLiteFunction(Name = "Sha1", Arguments = 1, FuncType = FunctionType.Scalar)]
        public class Sha1 : SQLiteFunction
        {
            public override object Invoke(object[] args)
            {
                var buffer = args[0] as byte[];
        
                if ( buffer == null )
                {
                    var s = args[0] as string;
        
                    if ( s != null )
                        buffer = Encoding.Unicode.GetBytes(s);
                }
        
                if ( buffer == null )
                    return null;
        
                using ( var sha1 = SHA1.Create() )
                {
                    return sha1.ComputeHash(buffer);
                }
            }
        }
        

        可以为二进制数据或字符串调用此函数。字符串以其 Unicode 表示形式进行散列。这应该与 SQL Server 匹配。

        函数可以这样调用:

        select sha1('abc')
        select sha1(x'010203')
        

        【讨论】:

          【解决方案5】:

          以下构建具有动态库支持的最新sqlite,并编译sha1 extension。它还假设基于 debian 的 linux 发行版:

          sudo apt build-dep sqlite3 # fetches dependencies to compile sqlite3
          
          mkdir sqlite-compilation
          cd    sqlite-compilation
          
          wget -O sqlite.tar.gz https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release
          
          tar xzf sqlite.tar.gz
          
          mkdir build
          cd    build
            ../sqlite/configure
            make OPTS='-DSQLITE_ENABLE_LOAD_EXTENSION'
            ./sqlite3 -cmd 'pragma compile_options;' <<< .exit
          cd -
          
          cd sqlite/ext/misc
            # https://sqlite.org/src/file?name=ext/misc/sha1.c
            sed -i 's/int sqlite3_sha_init(/int sqlite3_extension_init(/' sha1.c # this is needed to give object file custom name, for example libSqlite3Sha1.so:
            gcc -g -O2 -shared -fPIC -I ../../../build -o libSqlite3Sha1.so ./sha1.c
            cp libSqlite3Sha1.so ../../../build/
          cd -
          

          结果你会得到:

          build/sqlite3           # sqlite3 binary
          build/libSqlite3Sha1.so # sha1 extension
          

          测试:

          cd build
            sqlite3 <<< '
          .load ./libSqlite3Sha1
          select sha1(1);
          .exit
            '
            # compare output with:
            echo -n 1 | sha1sum
          cd -
          

          【讨论】:

          • 谢谢!像魅力一样工作:)
          【解决方案6】:

          据我所知,SQLite 没有内置任何散列函数。

          a way 可以将自定义函数添加到 SQLite,但如果您只是在程序中计算 SHA1 哈希并将其存储在 SQlite 中,可能会更容易。

          为 SQLite 创建自定义函数在一定程度上取决于 API 和您使用的语言。我只有从 Python 创建 SQLite 函数的经验。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-24
            • 1970-01-01
            • 2011-06-23
            • 1970-01-01
            • 2010-09-07
            • 2017-10-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多