Golang utf16le encoding for ldap password attribut

2019-07-25 23:37发布

问题:

I'm trying to reset a MS Active Directory password attribute using ldap in Go. AD won't play nicely with ldap.PasswordModifyRequest so I'm using ldap.NewModifyRequest. (Using gopkg.in/ldap.v2)

AD will accept the password surrounded in quotes and utf16le encoded, in Python I can do this using

unicode_pass = unicode("\"secret\"", "iso-8859-1")
password_value = unicode_pass.encode("utf-16-le")
mod_attrs = [(ldap.MOD_REPLACE, "unicodePwd", [password_value])]
l.modify_s(user_dn, mod_attrs)

How can I do this in Go? Using ldap.NewModifyRequest and Replace I can change other attributes, but I need to pass Request []string for the updated value, that needs to be my encoded password, and I'm running into type mismatches when I play around with utf16.Encode...

modify := ldap.NewModifyRequest(dn)
modify.Replace("unicodePwd", []string{"encodedsecret"})

Thanks.

回答1:

You can use the golang.org/x/text/encoding/unicode package to encode your string as UTF16.

Using this package you can write something like this:

utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)

encoded, err := utf16.NewEncoder().String("encodedsecret")

modify := ldap.NewModifyRequest(dn)
modify.Replace("unicodePwd", []string{encoded})

// do something with modify


标签: go ldap