Source Code

;; title: encoding-utils
;; version: 1.0.0
;; summary: Encoding and hashing utilities
;; description: Hash functions and encoding - Clarity 4

;; Constants
(define-constant ERR-INVALID-INPUT (err u7100))

;; Read-Only Functions

(define-read-only (hash-uint (value uint))
  (ok (keccak256 value))
)

(define-read-only (hash-string (str (string-ascii 200)))
  (ok (keccak256 (unwrap-panic (to-consensus-buff? str))))
)

(define-read-only (hash-principal (address principal))
  (ok (keccak256 (unwrap-panic (to-consensus-buff? address))))
)

(define-read-only (hash-two-uints (a uint) (b uint))
  (ok (keccak256 (concat
    (unwrap-panic (to-consensus-buff? a))
    (unwrap-panic (to-consensus-buff? b))
  )))
)

(define-read-only (hash-tuple (data { amount: uint, recipient: principal }))
  (ok (keccak256 (unwrap-panic (to-consensus-buff? data))))
)

(define-read-only (create-message-hash
  (sender principal)
  (recipient principal)
  (amount uint)
  (nonce uint))
  (ok (keccak256 (unwrap-panic (to-consensus-buff? {
    sender: sender,
    recipient: recipient,
    amount: amount,
    nonce: nonce
  }))))
)

(define-read-only (sha256-uint (value uint))
  (ok (sha256 value))
)

(define-read-only (sha256-string (str (string-ascii 200)))
  (ok (sha256 (unwrap-panic (to-consensus-buff? str))))
)

(define-read-only (sha512-uint (value uint))
  (ok (sha512 value))
)

(define-read-only (sha512-string (str (string-ascii 200)))
  (ok (sha512 (unwrap-panic (to-consensus-buff? str))))
)

(define-read-only (sha512-256-uint (value uint))
  (ok (sha512/256 value))
)

(define-read-only (hash-list (items (list 10 uint)))
  (ok (keccak256 (unwrap-panic (to-consensus-buff? items))))
)

(define-read-only (create-commitment (secret uint))
  ;; Create commitment hash for commit-reveal schemes
  (ok (keccak256 (concat
    (unwrap-panic (to-consensus-buff? secret))
    (unwrap-panic (to-consensus-buff? stacks-block-time))
  )))
)

(define-read-only (verify-commitment (commitment (buff 32)) (secret uint) (timestamp uint))
  (is-eq
    commitment
    (keccak256 (concat
      (unwrap-panic (to-consensus-buff? secret))
      (unwrap-panic (to-consensus-buff? timestamp))
    ))
  )
)

(define-read-only (hash-password (password (string-ascii 100)) (salt (buff 16)))
  ;; Simple password hashing (production would use better KDF)
  (ok (sha512 (concat
    (unwrap-panic (to-consensus-buff? password))
    salt
  )))
)
;; Clarity 4 Enhanced Functions

;; 1. Clarity 4: principal-destruct? - Validate address principals
(define-read-only (validate-addr (addr principal))
  (principal-destruct? addr)
)

;; 2. Clarity 4: Format hash buffer
(define-read-only (format-hash (h (buff 32)))
  (ok h)
)

;; 3. Clarity 4: string-to-uint? - Parse number from string
(define-read-only (parse-num (n-str (string-ascii 20)))
  (match (string-to-uint? n-str)
    n (ok n)
    (err u998)
  )
)

;; 4. Clarity 4: burn-block-height - Track encoding timestamps
(define-read-only (get-enc-timestamps)
  (ok {
    stacks-time: stacks-block-time,
    burn-time: burn-block-height
  })
)

Functions (19)

FunctionAccessArgs
sha512-uintread-onlyvalue: uint
hash-uintread-onlyvalue: uint
hash-stringread-onlystr: (string-ascii 200
hash-principalread-onlyaddress: principal
hash-two-uintsread-onlya: uint, b: uint
hash-tupleread-onlydata: { amount: uint, recipient: principal }
create-message-hashread-onlysender: principal, recipient: principal, amount: uint, nonce: uint
sha256-uintread-onlyvalue: uint
sha256-stringread-onlystr: (string-ascii 200
sha512-stringread-onlystr: (string-ascii 200
sha512-256-uintread-onlyvalue: uint
hash-listread-onlyitems: (list 10 uint
create-commitmentread-onlysecret: uint
verify-commitmentread-onlycommitment: (buff 32
hash-passwordread-onlypassword: (string-ascii 100
validate-addrread-onlyaddr: principal
format-hashread-onlyh: (buff 32
parse-numread-onlyn-str: (string-ascii 20
get-enc-timestampsread-only