Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Created April 4, 2018 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanbrujo/83a6558c7f72d0afabf71eeb8461858f to your computer and use it in GitHub Desktop.
Save juanbrujo/83a6558c7f72d0afabf71eeb8461858f to your computer and use it in GitHub Desktop.
VueJS method to decode Base64 encoded strings
methods: {
decodeBase64: function (string) {
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
var result = ''
var i = 0
do {
var b1 = characters.indexOf(string.charAt(i++))
var b2 = characters.indexOf(string.charAt(i++))
var b3 = characters.indexOf(string.charAt(i++))
var b4 = characters.indexOf(string.charAt(i++))
var a = ((b1 & 0x3F) << 2) | ((b2 >> 4) & 0x3)
var b = ((b2 & 0xF) << 4) | ((b3 >> 2) & 0xF)
var c = ((b3 & 0x3) << 6) | (b4 & 0x3F)
result += String.fromCharCode(a) + (b ? String.fromCharCode(b) : '') + (c ? String.fromCharCode(c) : '')
} while (i < string.length)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment