Code Monkey home page Code Monkey logo

Comments (5)

tanner0101 avatar tanner0101 commented on May 12, 2024

Also base64 decode

extension CipherProtocol {
    public func decrypt(_ bytes: Bytes, key: Bytes? = nil) throws -> Bytes {
        return try decrypt(bytes, key: key ?? defaultKey, iv: defaultIV)
    }

    public func decrypt(_ string: String, key: Bytes? = nil, encoding: CipherEncoding = .base64) throws -> String {
        let bytes: Bytes

        switch encoding {
        case .hex:
            var data = Bytes()

            var gen = string.characters.makeIterator()
            while let c1 = gen.next(), let c2 = gen.next() {
                let s = String([c1, c2])

                guard let d = Byte(s, radix: 16) else {
                    break
                }

                data.append(d)
            }
            bytes = data
        case .base64:
            bytes = string.base64Decoded
        }

        let d = try decrypt(bytes, key: key)
        return d.string
    }
}

extension String {
    public var base64Decoded: Bytes {
        guard let data = NSData(base64Encoded: self, options: []) else { return [] }
        var bytes = Bytes(repeating: 0, count: data.length)
        data.getBytes(&bytes,  length: data.length)
        return bytes
    }
}```

from core.

loganwright avatar loganwright commented on May 12, 2024

We can't do this until Swift 4 because we can't add initializers to generic array extensions, let's revisit this at that point.

from core.

tanner0101 avatar tanner0101 commented on May 12, 2024

It should be able to work exactly the same way that base64Encoded works.

bytes.hexEncoded.string
string.bytes.hexDecoded

from core.

loganwright avatar loganwright commented on May 12, 2024

Yup, we can add properties, just can't do the traditional convertible pattern we've been using w/ the initializer .... but actually, we might be able to do this w/ arrays at least in 3.1. It can't conform to a protocol, but the behavior could be there.

Follow up, I'm 82% sure we have this functionality already, might just be different names.

from core.

tanner0101 avatar tanner0101 commented on May 12, 2024

Yeah I think it's just the naming right now. Same issue we had with base64 where you can only go Bytes -> String etc.

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.