Skip to content

Instantly share code, notes, and snippets.

@wjn
Created February 27, 2014 16:40
Show Gist options
  • Save wjn/9253839 to your computer and use it in GitHub Desktop.
Save wjn/9253839 to your computer and use it in GitHub Desktop.
Generates an alpha numeric hash of a specified length using xslt 1. 0 and exlt math library.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:math="http://exslt.org/math"
extension-element-prefixes="math">
<xsl:template name="makeHash">
<xsl:param name="length">8</xsl:param>
<xsl:param name="count" select="$length"/>
<xsl:param name="hash"/>
<xsl:variable name="possibleFirst">xABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="possible">xABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789</xsl:variable>
<xsl:variable name="possibleFirstLength" select="string-length($possibleFirst)"/>
<xsl:variable name="possibleLength" select="string-length($possible)"/>
<xsl:choose>
<xsl:when test="$count = $length">
<xsl:variable name="position" select="floor(math:random()* $possibleFirstLength)" />
<xsl:call-template name="makeHash">
<xsl:with-param name="length" select="$length"/>
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="hash">
<xsl:value-of select="$hash"/>
<xsl:value-of select="substring($possibleFirst, $position, 1)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$count &gt; 0">
<xsl:variable name="position" select="floor(math:random()* $possibleLength)" />
<xsl:call-template name="makeHash">
<xsl:with-param name="length" select="$length"/>
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="hash">
<xsl:value-of select="$hash"/>
<xsl:value-of select="substring($possible, $position, 1)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$hash"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
@wdebusschere
Copy link

Wjn, I'm using this utility to generate a pasword, but i notice that lots of users have the same password, any idea why this is not more random? Users a created a signup, every day i have some..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment