You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell使用qrcode库生成Google Authenticator二维码出错求助

Fixing QR Code Generation for Google Authenticator & Haskell QR Library Issues

Problem 1: Corrupted QR Code Content with qrcode Library

Looking at your code and the parsed QR content, two key issues are causing the broken result:

  1. Wrong QR Encoding Mode: You’re using the Alphanumeric mode, which has strict character set constraints. Even though your URL-encoded characters (like %3A/%2F) are technically allowed in this mode, the library’s encoding logic can fail with long, structured URL-encoded strings, leading to the garbled :00000000... output you see.
  2. Unnecessary URL Encoding: Google Authenticator expects the raw otpauth URI as QR code content—not a URL-encoded version. Your input string is double-encoded, which breaks the parsing flow for both the QR library and Google Authenticator.

Solution for Problem 1

Adjust your code to use the Byte (binary) mode (which reliably handles any 8-bit data) and pass the raw, unencoded otpauth URI:

testQR :: IO ()
testQR = do
  let path = "hello.pgm"
      otpURI = "otpauth://totp/Label:James?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=MyCompany"
  arrayToFile path . toArray . fromJust . encode (fromJust $ version 1) M Byte $ otpURI

This eliminates character set limitations and ensures the otpauth URI is encoded exactly as Google Authenticator expects it.

Problem 2: Missing C Library Error for haskell-qrencode

The haskell-qrencode package is a Haskell binding to the C library libqrencode, so you need to install the system-level development files for libqrencode first before setting up the Haskell package.

Solution for Problem 2

Install the C library based on your operating system:

  • Ubuntu/Debian:
    sudo apt-get update && sudo apt-get install libqrencode-dev
    
  • Fedora/RHEL:
    sudo dnf install libqrencode-devel
    
  • macOS (with Homebrew):
    brew install qrencode
    

Once the C library is installed, install the Haskell package via Cabal or Stack:

# Using Cabal
cabal install haskell-qrencode

# Using Stack
stack install haskell-qrencode

内容的提问来源于stack exchange,提问作者James Burton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:13:48