Saludos hacia tiempo que no codeaba algo en haskell para postearlo aca,asi que esta vez me curre el cifrado base64 en haskell

Código: Seleccionar todo

import Codec.Binary.UTF8.Light(toBits,fromBits)
import Data.List(elemIndices,tails)
import Data.List.Split (splitOn)
import Data.Char(chr,ord)
data Option = Encrypt | Decrypt

base64 :: String -> Option -> String
base64 xs ys = case ys of
		  Encrypt -> cifrar xs
		  Decrypt -> descifrar (concat$splitOn "=" xs)
	where
	cONST = [chr x|x<-[0x41..0x5A]++[0x61..0x7A]++[0x30..0x39]++[0x2B,0x2F]]
	cifrar :: String -> String
	cifrar [] = []
	cifrar xs 
		|length xs == 0x01 = map(cONST!!)[var8,var9]++"=="
		|length xs == 0x02 = map(cONST !!)[var5,var6,var7]++"="
		|otherwise = map(cONST !!)[var,var2,var3,var4]++cifrar (drop 0x03 xs)
		where
		var=fromIntegral.fromBits.take 0x06.concat$[toBits.fromIntegral.ord$z | z <- take 0x03 xs]
		var2=fromIntegral.fromBits.take 0x06.drop 0x06.concat$[toBits.fromIntegral.ord$z | z <- take 0x03 xs]
		var3=fromIntegral.fromBits.take 0x06.drop 0x0C.concat$[toBits.fromIntegral.ord$z | z <- take 0x03 xs]
		var4=fromIntegral.fromBits.take 0x06.drop 0x12.concat$[toBits.fromIntegral.ord$z | z <- take 0x03 xs]
		var5=fromIntegral.fromBits.take 0x06.concat$[toBits.fromIntegral.ord$z | z <- xs]
		var6=fromIntegral.fromBits.take 0x06.drop 0x06.concat$[toBits.fromIntegral.ord$z | z <- xs]
		var7=fromIntegral.fromBits.take 0x06.drop 0x0C$concat([toBits.fromIntegral.ord$z | z <- xs])++[0x0,0x0]
		var8=fromIntegral.fromBits.take 0x06.concat$[toBits.fromIntegral.ord$z | z <- xs]
		var9=fromIntegral.fromBits.take 0x06.drop 0x06$concat([toBits.fromIntegral.ord$z | z <- xs])++[0x0,0x0,0x0,0x0]
	descifrar :: String -> String
	descifrar [] = []
	descifrar xs
		|length xs == 0x02 = [me6]
		|length xs == 0x03 = [me4,me5]
		|otherwise = [me,me2,me3]++descifrar (drop 0x04 xs)
		where
		me=chr.fromIntegral.fromBits.take 0x08.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- take 0x04 xs]
		me2=chr.fromIntegral.fromBits.take 0x08.drop 0x08.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- take 0x04 xs]
		me3=chr.fromIntegral.fromBits.take 0x08.drop 0x10.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- take 0x04 xs]
		me4=chr.fromIntegral.fromBits.take 0x08.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- xs]
		me5=chr.fromIntegral.fromBits.take 0x08.drop 0x08.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- xs]
		me6=chr.fromIntegral.fromBits.take 0x08.concat$[tails(toBits(fromIntegral((elemIndices x cONST) !! 0x0))) !! 0x02 | x <- xs]
Pastebin:[Enlace externo eliminado para invitados]

Muestra:

Código: Seleccionar todo

*Main> base64 "pim pam toma lacasitos" Encrypt
"cGltIHBhbSB0b21hIGxhY2FzaXRvcw=="
*Main> base64 "cGltIHBhbSB0b21hIGxhY2FzaXRvcw==" Decrypt
"pim pam toma lacasitos"
*Main>
Espero os guste el code, un saludo foro
Abolición para el torneo del toro de la vega. Death to the murderers of bulls.
Naker90 escribió:Se nota tu progreso con este lenguaje, muy buena maquina.
Saludos
Gracias crack, todos los dias me paso por la biblioteca de haskell para ir conociendo de todo en este lenguaje que me a gustado tanto.
Por cierto haber si algun moderador o admin me hace el favor de editar el post para actualizarlo con el nuevo code que he sacado ahora muchisimo mas optimizado y corto.

Código: Seleccionar todo

{-
Función: Base64
Autor: Strup
Descripción: Cifra una cadena a Base64 y la Descifra
Data: 28/04/2014 16:54
-}
import Codec.Binary.UTF8.Light(toBits,fromBits)
import Data.List.Split (splitOn,chunksOf)
import Data.List(elemIndices)
import Data.Char(chr,ord)
data Option = Encrypt | Decrypt

base64 :: String -> Option -> String
base64 xs ys = case ys of
			   Encrypt -> cifrar xs
			   Decrypt -> descifrar (concat$splitOn "=" xs)
	where
	cONST = [chr x|x<-[0x41..0x5A]++[0x61..0x7A]++[0x30..0x39]++[0x2B,0x2F]]
	cifrar :: String -> String
	cifrar [] = []
	cifrar xs 
		|length xs == 0x01 = enc++"=="
		|length xs == 0x02 = enc++"="
		|otherwise = enc++cifrar (drop 0x03 xs)
		where
		enc=map(cONST !!).map(fromIntegral).map(fromBits).map(\x -> if length x == 0x04 then x++[0x0,0x0] else if length x == 0x02 then x++[0x0,0x0,0x0,0x0] else x).chunksOf 0x06.concat$[toBits.fromIntegral.ord$z | z <- take 0x03 xs]
	
	descifrar :: String -> String
	descifrar [] = []
	descifrar xs |last des == '\NUL' = init$des|otherwise = des++descifrar (drop 0x04 xs)
		where
		des=map(chr).map(fromIntegral).map(fromBits).chunksOf 0x08.concat$[drop 0x02.toBits.fromIntegral$(elemIndices x cONST) !! 0x0| x <- take 0x04 xs]
Pastebin:[Enlace externo eliminado para invitados]

Muestra:

Código: Seleccionar todo

"ghci>" base64 "El aliento de mi gato huele a comida de gato" Encrypt
"RWwgYWxpZW50byBkZSBtaSBnYXRvIGh1ZWxlIGEgY29taWRhIGRlIGdhdG8="
(0.00 secs, 1035680 bytes)
"ghci>" base64 "RWwgYWxpZW50byBkZSBtaSBnYXRvIGh1ZWxlIGEgY29taWRhIGRlIGdhdG8=" Decrypt
"El aliento de mi gato huele a comida de gato"
(0.00 secs, 515092 bytes)
Espero os guste el code ami me encanta
Salu2

PD: para el modulo Codec.Binary.UTF8.Light hay que ir a cabal desde cmd en windows (terminal en linux xD) y teclear cabal install utf8-light. Para los curiosos, cabal solo se tiene si se instala haskell, o mas bien el glasgow haskell compiler (interprete y compilador de haskell).
Abolición para el torneo del toro de la vega. Death to the murderers of bulls.
NvK escribió:Te felicito por el código, se nota mucho tu avance en este lenguaje.
Gracias maquina, dentro de poco lo mas seguro que ya empiece con los punteros de haskell
.Podria haberlo acortado mas pero el code quedaria ilegible en 3 lineas (una mala practica desde mi punto de vista, al menos que sea una competicion para ver quien hace el code mas corto)
Abolición para el torneo del toro de la vega. Death to the murderers of bulls.
Responder

Volver a “Fuentes”