Skip to content

Instantly share code, notes, and snippets.

@redgeoff
Last active January 17, 2024 09:43
Show Gist options
  • Save redgeoff/bd39ee4c5c492216be73c34e189d93c4 to your computer and use it in GitHub Desktop.
Save redgeoff/bd39ee4c5c492216be73c34e189d93c4 to your computer and use it in GitHub Desktop.
CouchDB Hash Password
#!/bin/bash
# Hashes a CouchDB user password
# Usage: couchdb-hash-password.sh url password
# e.g. couchdb-hash-password.sh http://admin:secret@localhost:5984 mypwd
# => -pbkdf2-7d66a7d6c73f83173f5d0f73b7570203f443dffd,8dab49491e28d59980a6436521822458,10
url=$1
password=$2
username=tmpuser
# Source: https://gist.github.com/cjus/1047794
function jsonval {
json=$1
prop=$2
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
}
# Create temporary user
curl -s -X PUT $url/_users/org.couchdb.user:$username \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{"name": "'$username'", "password": "'$password'", "roles": [], "type": "user"}' 2>/dev/null > /dev/null
# Get user data
user=`curl -s -X GET $url/_users/org.couchdb.user:$username`
# Parse JSON
rev=`jsonval $user _rev`
password_scheme=`jsonval $user password_scheme`
iterations=`jsonval $user iterations`
iterations=`echo $iterations | cut -d':' -f 2` # fix for jsonval
derived_key=`jsonval $user derived_key`
salt=`jsonval $user salt`
# Delete user
curl -s -X PUT $url/_users/org.couchdb.user:$username \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "If-Match: $rev" \
-d '{"_deleted":true}' 2>/dev/null > /dev/null
echo -$password_scheme-$derived_key,$salt,$iterations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment